1 #include "caffe2/operators/locally_connected_op.h" 6 #include "caffe2/operators/locally_connected_op_impl.h" 12 constexpr
char kLCDoc[] = R
"DOC( 13 Note that other parameters, such as the stride and 14 kernel size, or the pads' sizes in each direction are not necessary for input 15 because they are provided by the ConvPoolOpBase operator. Various dimension 16 checks are done implicitly, and the sizes are specified in the Input docs for 17 this operator. As is expected, the filter is locally connected with a subset of 18 the image and the bias is added; this is done throughout the image data and the 19 output is computed. As a side note on the implementation layout: 20 locally_connected_op_impl.h is the templated implementation of the 21 locally_connected_op.h file, which is why they are separate files. 24 std::function<void(OpSchema&)> LCDocGenerator(const char* dim) {
25 return [dim](OpSchema& schema) {
27 The locally connected operator consumes an input vector, a {dim}filter blob 28 and a bias blob and computes the output. {lc_doc})DOC"; 29 c10::ReplaceAll(doc, "{dim}", dim);
30 c10::ReplaceAll(doc,
"{lc_doc}", kLCDoc);
35 "The filter blob that will be used in the locally connected op; " 36 "has size (YH * YW * M x C x kH x kW) if order == NCHW else " 37 "(YH * YW * M * KH * KW * C), where YH and YW are the height " 38 "and width of the output image, C is the number of channels, and kH " 39 "and kW are the height and width of the kernel.");
43 "The 1D bias blob that is added through the locally connected op; " 44 "has size (YH * YW * M).");
48 "Output data blob that contains the result of the locally connected op." 49 "The output dimensions are functions of the kernel size, stride size, " 57 REGISTER_CPU_OPERATOR(LC, LocallyConnectedOp<float, CPUContext>);
62 .TensorInferenceFunction(ConvPoolOpBase<CPUContext>::TensorInferenceForLC)
63 .FillUsing(LCDocGenerator(
""));
65 REGISTER_CPU_OPERATOR(LC1D, LocallyConnectedOp<float, CPUContext>);
70 .TensorInferenceFunction(ConvPoolOpBase<CPUContext>::TensorInferenceForLC)
71 .FillUsing(LCDocGenerator(
"1D "));
73 REGISTER_CPU_OPERATOR(LC2D, LocallyConnectedOp<float, CPUContext>);
78 .TensorInferenceFunction(ConvPoolOpBase<CPUContext>::TensorInferenceForLC)
79 .FillUsing(LCDocGenerator(
"2D "));
81 REGISTER_CPU_OPERATOR(LC3D, LocallyConnectedOp<float, CPUContext>);
86 .TensorInferenceFunction(ConvPoolOpBase<CPUContext>::TensorInferenceForLC)
87 .FillUsing(LCDocGenerator(
"3D "));
89 REGISTER_CPU_OPERATOR(
91 LocallyConnectedGradientOp<float, CPUContext>);
93 OPERATOR_SCHEMA(LCGradient).NumInputs(2, 3).NumOutputs(1, 3);
95 REGISTER_CPU_OPERATOR(
97 LocallyConnectedGradientOp<float, CPUContext>);
99 OPERATOR_SCHEMA(LC1DGradient).NumInputs(2, 3).NumOutputs(1, 3);
101 REGISTER_CPU_OPERATOR(
103 LocallyConnectedGradientOp<float, CPUContext>);
105 OPERATOR_SCHEMA(LC2DGradient).NumInputs(2, 3).NumOutputs(1, 3);
107 REGISTER_CPU_OPERATOR(
109 LocallyConnectedGradientOp<float, CPUContext>);
111 OPERATOR_SCHEMA(LC3DGradient).NumInputs(2, 3).NumOutputs(1, 3);
115 class GetLocallyConnectedGradient :
public GradientMakerBase {
116 using GradientMakerBase::GradientMakerBase;
118 std::vector<OperatorDef> GetGradientDefs()
override {
119 CAFFE_ENFORCE(def_.input_size() == 3 || def_.input_size() == 2);
120 ArgumentHelper argsHelper(def_);
121 const bool compute_dX =
122 !argsHelper.GetSingleArgument<
bool>(
"no_gradient_to_input", 0);
124 if (def_.input_size() == 3) {
126 return SingleGradientDef(
127 def_.type() +
"Gradient",
129 std::vector<string>{I(0), I(1), GO(0)},
130 std::vector<string>{GI(1), GI(2), GI(0)});
132 return SingleGradientDef(
133 def_.type() +
"Gradient",
135 std::vector<string>{I(0), I(1), GO(0)},
136 std::vector<string>{GI(1), GI(2)});
140 return SingleGradientDef(
141 def_.type() +
"Gradient",
143 std::vector<string>{I(0), I(1), GO(0)},
144 std::vector<string>{GI(1), GI(0)},
145 std::vector<Argument>{MakeArgument<int>(
"no_bias", 1)});
147 return SingleGradientDef(
148 def_.type() +
"Gradient",
150 std::vector<string>{I(0), I(1), GO(0)},
151 std::vector<string>{GI(1)},
152 std::vector<Argument>{MakeArgument<int>(
"no_bias", 1)});
160 REGISTER_GRADIENT(LC, GetLocallyConnectedGradient);
161 REGISTER_GRADIENT(LC1D, GetLocallyConnectedGradient);
162 REGISTER_GRADIENT(LC2D, GetLocallyConnectedGradient);
163 REGISTER_GRADIENT(LC3D, GetLocallyConnectedGradient);
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...