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