|
| 1 | +// WARNING! WARNING! WARNING! |
| 2 | +// This file is a temporary hack to enable development of pytorch quantization |
| 3 | +// |
| 4 | +// It effectively wraps Caffe2 ops as is through custom jit ops mechanism |
| 5 | +// It obviously has terrible performance - caffe2 operator instance is created |
| 6 | +// on each invocation and also creation involves creating a protobuf (sigh...) |
| 7 | +// |
| 8 | +// Our plan is to implement quantized operators natively in c10 as operators and |
| 9 | +// also enforce some additional contracts on operator semantics: |
| 10 | +// - explicitly express weights prepacking as a separate operator to signify |
| 11 | +// reliance on weights being constant |
| 12 | +// - don't modify arguments of the op (OperatorDef) to store data |
| 13 | +// - explicitly model figuring out quantization params for dynamic quantization |
| 14 | +// instead of memorizing the first batch's params |
| 15 | + |
| 16 | +#include <torch/csrc/jit/custom_operator.h> |
| 17 | +#include <torch/csrc/jit/operator.h> |
| 18 | + |
| 19 | +#include <caffe2/core/operator.h> |
| 20 | +#include <caffe2/core/tensor_int8.h> |
| 21 | +#include <torch/csrc/autograd/variable.h> |
| 22 | + |
| 23 | +namespace torch { |
| 24 | +namespace jit { |
| 25 | + |
| 26 | +using caffe2::int8::Int8TensorCPU; |
| 27 | + |
| 28 | +namespace { |
| 29 | + |
| 30 | +caffe2::Tensor from_at_tensor(const c10::IValue& v) { |
| 31 | + return caffe2::Tensor(autograd::Variable(std::move(v).toTensor()).data()); |
| 32 | +} |
| 33 | + |
| 34 | +Int8TensorCPU from_proxy(const c10::IValue& proxy) { |
| 35 | + auto t = std::move(proxy).toTuple(); |
| 36 | + Int8TensorCPU r; |
| 37 | + r.t = from_at_tensor(t->elements()[0]); |
| 38 | + r.scale = t->elements()[1].toDouble(); |
| 39 | + r.zero_point = t->elements()[2].toInt(); |
| 40 | + return r; |
| 41 | +} |
| 42 | + |
| 43 | +at::Tensor to_proxy(const caffe2::Tensor& t) { |
| 44 | + return autograd::make_variable(at::Tensor(t.UnsafeSharedInstance()), false); |
| 45 | +} |
| 46 | + |
| 47 | +c10::intrusive_ptr<c10::ivalue::Tuple> to_proxy(const Int8TensorCPU& t) { |
| 48 | + return c10::ivalue::Tuple::create({to_proxy(t.t), t.scale, t.zero_point}); |
| 49 | +} |
| 50 | + |
| 51 | +// TODO: replace this with c10 registration when it's ready |
| 52 | +RegisterOperators reg({ |
| 53 | + Operator( |
| 54 | + // NOTE: we put outout in double parens because it's an output of type |
| 55 | + // tuple, not a tuple of multiple outputs |
| 56 | + "c10::quantized_relu((Tensor, float, int) self) -> ((Tensor, float, int))", |
| 57 | + // TODO: can't use C++ inference - doesn't work yet for tuple types |
| 58 | + [](Stack& stack) { |
| 59 | + AT_ASSERT(caffe2::GetRegisteredOperators().count( |
| 60 | + caffe2::OpRegistryKey("Relu", "DNNLOWP"))) |
| 61 | + |
| 62 | + // TODO: refactor the underlying op implementation and inline it in |
| 63 | + // c10 kernel |
| 64 | + caffe2::Workspace ws; |
| 65 | + ws.CreateBlob("X")->Reset( |
| 66 | + new Int8TensorCPU(from_proxy(std::move(peek(stack, 0, 1))))); |
| 67 | + |
| 68 | + auto def = caffe2::CreateOperatorDef( |
| 69 | + "Relu", "proxy", {"X"}, {"Y"}, caffe2::DeviceOption(), "DNNLOWP"); |
| 70 | + auto op = caffe2::CreateOperator(def, &ws); |
| 71 | + |
| 72 | + op->Run(); |
| 73 | + |
| 74 | + drop(stack, 1); |
| 75 | + pack(stack, to_proxy(ws.GetBlob("Y")->Get<Int8TensorCPU>())); |
| 76 | + return 0; |
| 77 | + }), |
| 78 | + |
| 79 | + Operator( |
| 80 | + "c10::quantize(Tensor X, float? scale = None, int? zero_point = None) -> ((Tensor, float, int))", |
| 81 | + [](Stack& stack) { |
| 82 | + AT_ASSERT(caffe2::GetRegisteredOperators().count( |
| 83 | + caffe2::OpRegistryKey("Quantize", "DNNLOWP"))) |
| 84 | + |
| 85 | + // TODO: refactor the underlying op implementation and inline it in |
| 86 | + // c10 kernel |
| 87 | + caffe2::Workspace ws; |
| 88 | + ws.CreateBlob("X")->Reset( |
| 89 | + new caffe2::Tensor(from_at_tensor(std::move(peek(stack, 0, 3))))); |
| 90 | + |
| 91 | + auto def = caffe2::CreateOperatorDef( |
| 92 | + "Quantize", |
| 93 | + "proxy", |
| 94 | + {"X"}, |
| 95 | + {"Y"}, |
| 96 | + caffe2::DeviceOption(), |
| 97 | + "DNNLOWP"); |
| 98 | + auto s = peek(stack, 1, 3).toOptional<float>(); |
| 99 | + if (s.has_value()) { |
| 100 | + def.add_arg()->CopyFrom(caffe2::MakeArgument("Y_scale", *s)); |
| 101 | + } |
| 102 | + auto zp = peek(stack, 2, 3).toOptional<int32_t>(); |
| 103 | + if (zp.has_value()) { |
| 104 | + def.add_arg()->CopyFrom(caffe2::MakeArgument("Y_zero_point", *zp)); |
| 105 | + } |
| 106 | + auto op = caffe2::CreateOperator(def, &ws); |
| 107 | + |
| 108 | + op->Run(); |
| 109 | + |
| 110 | + drop(stack, 3); |
| 111 | + pack(stack, to_proxy(ws.GetBlob("Y")->Get<Int8TensorCPU>())); |
| 112 | + return 0; |
| 113 | + }), |
| 114 | + |
| 115 | + Operator( |
| 116 | + "c10::dequantize((Tensor, float, int) x_q) -> Tensor", |
| 117 | + // TODO: can't use C++ inference - doesn't work yet for tuple types |
| 118 | + [](Stack& stack) { |
| 119 | + AT_ASSERT(caffe2::GetRegisteredOperators().count( |
| 120 | + caffe2::OpRegistryKey("Dequantize", "DNNLOWP"))) |
| 121 | + |
| 122 | + // TODO: refactor the underlying op implementation and inline it in |
| 123 | + // c10 kernel |
| 124 | + caffe2::Workspace ws; |
| 125 | + ws.CreateBlob("X")->Reset( |
| 126 | + new Int8TensorCPU(from_proxy(std::move(peek(stack, 0, 1))))); |
| 127 | + |
| 128 | + auto def = caffe2::CreateOperatorDef( |
| 129 | + "Dequantize", |
| 130 | + "proxy", |
| 131 | + {"X"}, |
| 132 | + {"Y"}, |
| 133 | + caffe2::DeviceOption(), |
| 134 | + "DNNLOWP"); |
| 135 | + auto op = caffe2::CreateOperator(def, &ws); |
| 136 | + |
| 137 | + op->Run(); |
| 138 | + |
| 139 | + drop(stack, 1); |
| 140 | + pack(stack, to_proxy(ws.GetBlob("Y")->Get<caffe2::Tensor>())); |
| 141 | + return 0; |
| 142 | + }), |
| 143 | +}); |
| 144 | +} // namespace |
| 145 | +} // namespace jit |
| 146 | +} // namespace torch |
0 commit comments