Caffe2 - C++ API
A deep learning, cross platform ML framework
filler_op.cc
1 #include "caffe2/operators/filler_op.h"
2 
3 namespace caffe2 {
4 
5 template <>
6 bool RangeFillOp<float, CPUContext>::Fill(
7  TensorCPU* output) {
8  float* data = output->mutable_data<float>();
9  for (int i = 0; i < output->size(); ++i) {
10  data[i] = i;
11  }
12  return true;
13 }
14 
15 template <>
16 template <typename T>
17 bool DiagonalFillOp<CPUContext>::FillWithType(TensorCPU* output) {
18  VerifyOutputShape(output);
19  T value = OperatorBase::GetSingleArgument<T>("value", 0);
20  auto* data = output->template mutable_data<T>();
21  // first fill everything with 0
22  math::Set<T, CPUContext>(output->size(), T(0), data, &context_);
23  // then calculate step size for diagonal
24  auto step = GetStepSize(output);
25  for (TIndex i = 0; i < output->size(); i += step) {
26  math::Set<T, CPUContext>(1, value, data, &context_);
27  data += step;
28  }
29  return true;
30 }
31 
32 REGISTER_CPU_OPERATOR(UniformFill, UniformFillOp<float, CPUContext>);
33 REGISTER_CPU_OPERATOR(UniformIntFill, UniformFillOp<int, CPUContext>);
34 REGISTER_CPU_OPERATOR(UniqueUniformFill, UniqueUniformFillOp<CPUContext>);
35 REGISTER_CPU_OPERATOR(ConstantFill, ConstantFillOp<CPUContext>);
36 REGISTER_CPU_OPERATOR(DiagonalFill, DiagonalFillOp<CPUContext>);
37 REGISTER_CPU_OPERATOR(GaussianFill, GaussianFillOp<float, CPUContext>);
38 REGISTER_CPU_OPERATOR(XavierFill, XavierFillOp<float, CPUContext>);
39 REGISTER_CPU_OPERATOR(MSRAFill, MSRAFillOp<float, CPUContext>);
40 REGISTER_CPU_OPERATOR(RangeFill, RangeFillOp<float, CPUContext>);
41 REGISTER_CPU_OPERATOR(LengthsRangeFill, LengthsRangeFillOp<CPUContext>);
42 
43 OPERATOR_SCHEMA(ConstantFill)
44  .NumInputs(0, 1)
45  .NumOutputs(1)
46  .AllowInplace({{0, 0}})
47  .TensorInferenceFunction(FillerTensorInference<>)
48  .SetDoc(R"DOC(
49 The operator fills the elements of the output tensor with a constant value
50 specified by the 'value' argument.
51 
52 The data type is specified by the 'dtype' argument. The 'dtype' argument must
53 be one of the data types specified in the 'DataType' enum field in the
54 TensorProto message. If the 'dtype' argument is not provided, the data type of
55 'value' is used.
56 
57 The output tensor shape is specified by the 'shape' argument. If the number of
58 input is 1, the shape will be identical to that of the input at run time with
59 optional additional dimensions appended at the end as specified by 'extra_shape'
60 argument. In that case the 'shape' argument should not be set.
61 
62 If input_as_shape is set to true, then the input should be a 1D tensor
63 containing the desired output shape (the dimensions specified in extra_shape
64 will also be appended)
65 
66 NOTE: Currently, it supports data type of float, int32, int64, and bool.
67 )DOC")
68  .Arg("value", "The value for the elements of the output tensor.")
69  .Arg(
70  "dtype",
71  "The data type for the elements of the output tensor."
72  "Strictly must be one of the types from DataType enum in TensorProto.")
73  .Arg(
74  "shape",
75  "The shape of the output tensor."
76  "Cannot set the shape argument and pass in an input at the same time.")
77  .Arg(
78  "extra_shape",
79  "The additional dimensions appended at the end of the shape indicated"
80  "by the input blob."
81  "Cannot set the extra_shape argument when there is no input blob.")
82  .Arg(
83  "input_as_shape",
84  "1D tensor containing the desired output shape. First input must be in CPU context.")
85  .Input(0, "input", "Input tensor (optional) to provide shape information.")
86  .Output(
87  0,
88  "output",
89  "Output tensor of constant values specified by 'value'"
90  "argument and its type is specified by the 'dtype' argument");
91 
92 OPERATOR_SCHEMA(DiagonalFill)
93  .NumInputs(0, 1)
94  .NumOutputs(1)
95  .AllowInplace({{0, 0}})
96  .TensorInferenceFunction(FillerTensorInference<>)
97  .SetDoc(R"DOC(
98 The operator fills the diagonal elements of the output tensor (>= 2D)
99 with a constant value specified by the 'value' argument, and others 0. If
100 number of dimensions of the output tensor is greater than 2, all dimensions
101 must be equal.
102 
103 The data type is specified by the 'dtype' argument. The 'dtype' argument must
104 be one of the data types specified in the 'DataType' enum field in the
105 TensorProto message. If the 'dtype' argument is not provided, the data type of
106 'value' is used.
107 
108 The output tensor shape is specified by the 'shape' argument. If the number of
109 input is 1, the shape will be identical to that of the input at run time with
110 optional additional dimensions appended at the end as specified by 'extra_shape'
111 argument. In that case the 'shape' argument should not be set.
112 
113 If input_as_shape is set to true, then the input should be a 1D tensor
114 containing the desired output shape (the dimensions specified in extra_shape
115 will also be appended)
116 
117 NOTE: Currently, it supports data type of float, int32, int64, and bool.
118 )DOC")
119  .Arg("value", "The value for the elements of the output tensor.")
120  .Arg(
121  "dtype",
122  "The data type for the elements of the output tensor."
123  "Strictly must be one of the types from DataType enum in TensorProto.")
124  .Arg(
125  "shape",
126  "The shape of the output tensor."
127  "Cannot set the shape argument and pass in an input at the same time.")
128  .Arg(
129  "extra_shape",
130  "The additional dimensions appended at the end of the shape indicated"
131  "by the input blob."
132  "Cannot set the extra_shape argument when there is no input blob.")
133  .Arg("input_as_shape", "1D tensor containing the desired output shape")
134  .Input(0, "input", "Input tensor (optional) to provide shape information.")
135  .Output(
136  0,
137  "output",
138  "Output tensor"
139  "argument and its type is specified by the 'dtype' argument");
140 
141 OPERATOR_SCHEMA(UniformFill)
142  .NumInputs({0, 1, 3})
143  .NumOutputs(1)
144  .AllowInplace({{0, 0}})
145  .TensorInferenceFunction(FillerTensorInference<>)
146  .SetDoc(R"DOC(
147 Fill the output tensor with FLOAT samples from uniform distribution [min, max].
148 
149 The range can be defined either by arguments or input blobs. If the range is
150 given by input blobs, you also need to give the shape as input. When the range
151 is given as arguments, this operator enforces min <= max. When the range is
152 given as inputs, the constraint is not enforced. When MAX < MIN, the first
153 dimension of the output is set to 0. This behavior is allowed so that
154 dynamically sampling indices into a dynamically sized tensor is possible.
155 
156 The shape of the output can be given as argument or input.
157 )DOC")
158  .Arg("min", "minimum value, inclusive")
159  .Arg("max", "maximum value, inclusive")
160  .Arg("shape", "shape of the output, do not set when input_as_shape=1")
161  .Arg(
162  "input_as_shape",
163  "set to 1 to use the first input as shape. First input must be in CPU context.")
164  .Input(
165  0,
166  "SHAPE",
167  "1-D tensor of the shape of the output, "
168  "must be used with input_as_shape")
169  .Input(1, "MIN", "scalar blob of mininum value")
170  .Input(2, "MAX", "scalar blob of maximum value")
171  .Output(0, "OUTPUT", "output tensor");
172 OPERATOR_SCHEMA(UniformIntFill)
173  .NumInputs({0, 1, 3})
174  .NumOutputs(1)
175  .AllowInplace({{0, 0}})
176  .TensorInferenceFunction(FillerTensorInference<>)
177  .SetDoc(R"DOC(
178 Like `UniformFill` but fill with INT32.
179 )DOC");
180 OPERATOR_SCHEMA(UniqueUniformFill)
181  .NumInputs(0, 2)
182  .NumOutputs(1)
183  .AllowInplace({{0, 0}})
184  .TensorInferenceFunction(FillerTensorInference<>)
185  .SetDoc(R"DOC(
186 Fill the output tensor with uniform samples between min and max (inclusive).
187 If the second input is given, its elements will be excluded from uniform
188 sampling. Using the second input will require you to provide shape via the first
189 input.
190 )DOC")
191  .Arg("min", "Minimum value, inclusive")
192  .Arg("max", "Maximum value, inclusive")
193  .Arg(
194  "dtype",
195  "The data type for the elements of the output tensor."
196  "Strictly must be one of the types from DataType enum in TensorProto."
197  "This only supports INT32 and INT64 now. If not set, assume INT32")
198  .Arg(
199  "shape",
200  "The shape of the output tensor."
201  "Cannot set the shape argument and pass in an input at the same time.")
202  .Arg(
203  "extra_shape",
204  "The additional dimensions appended at the end of the shape indicated"
205  "by the input blob. "
206  "Cannot set the extra_shape argument when there is no input blob.")
207  .Arg(
208  "input_as_shape",
209  "1D tensor containing the desired output shape. First input must be in CPU context.")
210  .Input(0, "input", "Input tensor to provide shape information")
211  .Input(
212  1,
213  "avoid",
214  "(optional) Avoid elements in this tensor. Elements must be unique.")
215  .Output(0, "output", "Output tensor of unique uniform samples");
216 OPERATOR_SCHEMA(GaussianFill)
217  .NumInputs(0, 1)
218  .NumOutputs(1)
219  .AllowInplace({{0, 0}})
220  .TensorInferenceFunction(FillerTensorInference<>);
221 OPERATOR_SCHEMA(XavierFill)
222  .NumInputs(0, 1)
223  .NumOutputs(1)
224  .AllowInplace({{0, 0}})
225  .TensorInferenceFunction(FillerTensorInference<>);
226 OPERATOR_SCHEMA(MSRAFill)
227  .NumInputs(0, 1)
228  .NumOutputs(1)
229  .AllowInplace({{0, 0}})
230  .TensorInferenceFunction(FillerTensorInference<>);
231 OPERATOR_SCHEMA(RangeFill)
232  .NumInputs(0, 1)
233  .NumOutputs(1)
234  .AllowInplace({{0, 0}})
235  .TensorInferenceFunction(FillerTensorInference<>);
236 
237 NO_GRADIENT(UniformFill);
238 NO_GRADIENT(UniformIntFill);
239 NO_GRADIENT(UniqueUniformFill);
240 NO_GRADIENT(ConstantFill);
241 NO_GRADIENT(DiagonalFill);
242 NO_GRADIENT(GaussianFill);
243 NO_GRADIENT(XavierFill);
244 NO_GRADIENT(MSRAFill);
245 NO_GRADIENT(RangeFill);
246 
247 OPERATOR_SCHEMA(LengthsRangeFill)
248  .NumInputs(1)
249  .NumOutputs(1)
250  .SetDoc(R"DOC(
251 Convert a length vector to a range sequence. For example, input=[4,3,1], the
252 output would be [0,1,2,3,0,1,2,0].
253 )DOC")
254  .Input(0, "lengths", "1D tensor of int32 or int64 segment lengths.")
255  .Output(
256  0,
257  "range_sequence",
258  "1D tensor whose size is the sum of `lengths`");
259 NO_GRADIENT(LengthsRangeFill);
260 
261 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...