Caffe2 - C++ API
A deep learning, cross platform ML framework
int8_conv_op.cc
1 #include "caffe2/operators/quantized/int8_conv_op.h"
2 
3 namespace caffe2 {
4 
5 REGISTER_CPU_OPERATOR(Int8Conv, int8::Int8ConvOp<int8::Activation::NONE>);
6 
7 const char kConvDoc_int8[] = R"DOC(
8 [Only NHWC order is supported now]Note that other parameters, such as the stride and
9 kernel size, or the pads' sizes in each direction are not necessary for input
10 because they are provided by the ConvPoolOpBase operator. Various dimension
11 checks are done implicitly, and the sizes are specified in the Input docs for
12 this operator. As is expected, the filter is convolved with a subset of the
13 image and the bias is added; this is done throughout the image data and the
14 output is computed. As a side note on the implementation layout:
15 conv_op_impl.h is the templated implementation of the conv_op.h file, which is
16 why they are separate files.
17 )DOC";
18 
19 std::function<void(OpSchema&)> ConvDocGenerator(
20  const char* dim,
21  bool relu_fused = false) {
22  return [=](OpSchema& schema) {
23  string doc = R"DOC(
24 The convolution operator consumes an input vector, a {dim}filter blob
25 and a bias blob and computes the output. {conv_doc})DOC";
26  c10::ReplaceAll(doc, "{dim}", dim);
27  c10::ReplaceAll(doc, "{conv_doc}", kConvDoc_int8);
28  schema.SetDoc(doc);
29  schema.Input(
30  0,
31  "X",
32  "Input data blob from previous layer; has size (N x C x H x W), "
33  "where N is the batch size, C is the number of channels, "
34  "and H and W are the height and width. Note that this is for the NCHW "
35  "usage. On the other hand, the NHWC Op has a different set of "
36  "dimension constraints. ");
37  schema.Input(
38  1,
39  "filter",
40  "The filter blob that will be used in the "
41  "convolutions; has size (M x C x kH x kW), where C is the number of "
42  "channels, and kH and kW are the height and width of the kernel.");
43  schema.Input(
44  2,
45  "bias",
46  "The 1D bias blob that is added through the "
47  "convolution; has size (M).");
48  schema.Output(0, "Y", relu_fused ?
49  "Output data blob that contains the result of the "
50  "convolution. The output dimensions are functions of the kernel size, "
51  "stride size, and pad lengths. Output will go through rectified linear "
52  "function, where y = max(0, x)." :
53  "Output data blob that contains the result of the "
54  "convolution. The output dimensions are functions of the kernel size, "
55  "stride size, and pad lengths.");
56  };
57 }
58 
59 OPERATOR_SCHEMA(Int8Conv)
60  .NumInputs(2, 3)
61  .NumOutputs(1)
62  .Arg("Y_scale", "Output tensor quantization scale")
63  .Arg("Y_zero_point", "Output tensor quantization offset")
64  .TensorInferenceFunction(ConvPoolOpBase<CPUContext>::TensorInferenceForConv)
65  .CostInferenceFunction(OpSchema::CostInferenceFunctionType(
66  ConvPoolOpBase<CPUContext>::CostInferenceForConv))
67  .FillUsing(ConvDocGenerator(""));
68 
69 OPERATOR_SCHEMA(Int8ConvRelu)
70  .NumInputs(2, 3)
71  .NumOutputs(1)
72  .Arg("Y_scale", "Output tensor quantization scale")
73  .Arg("Y_zero_point", "Output tensor quantization offset")
74  .TensorInferenceFunction(ConvPoolOpBase<CPUContext>::TensorInferenceForConv)
75  .CostInferenceFunction(OpSchema::CostInferenceFunctionType(
76  ConvPoolOpBase<CPUContext>::CostInferenceForConv))
77  .FillUsing(ConvDocGenerator("", true));
78 
79 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13
std::function< struct Cost(const OperatorDef &, const vector< TensorShape > &)> CostInferenceFunctionType
Registers a function that takes in an OperatorDef and a series of input shapes and returns the total ...