1 #include "caffe2/operators/filler_op.h" 6 bool RangeFillOp<float, CPUContext>::Fill(
Tensor* output) {
7 float* data = output->template mutable_data<float>();
8 for (
int i = 0; i < output->numel(); ++i) {
16 bool DiagonalFillOp<CPUContext>::FillWithType(
Tensor* output) {
17 VerifyOutputShape(output);
18 T value = OperatorBase::GetSingleArgument<T>(
"value", 0);
19 auto* data = output->template mutable_data<T>();
21 math::Set<T, CPUContext>(output->numel(),
T(0), data, &context_);
23 auto step = GetStepSize(output);
24 for (int64_t i = 0; i < output->numel(); i += step) {
25 math::Set<T, CPUContext>(1, value, data, &context_);
31 REGISTER_CPU_OPERATOR(UniformFill, UniformFillOp<float, CPUContext>);
32 REGISTER_CPU_OPERATOR(UniformIntFill, UniformFillOp<int, CPUContext>);
33 REGISTER_CPU_OPERATOR(UniqueUniformFill, UniqueUniformFillOp<CPUContext>);
34 REGISTER_CPU_OPERATOR(ConstantFill, ConstantFillOp<CPUContext>);
35 REGISTER_CPU_OPERATOR(DiagonalFill, DiagonalFillOp<CPUContext>);
36 REGISTER_CPU_OPERATOR(GaussianFill, GaussianFillOp<float, CPUContext>);
37 REGISTER_CPU_OPERATOR(XavierFill, XavierFillOp<float, CPUContext>);
38 REGISTER_CPU_OPERATOR(MSRAFill, MSRAFillOp<float, CPUContext>);
39 REGISTER_CPU_OPERATOR(RangeFill, RangeFillOp<float, CPUContext>);
40 REGISTER_CPU_OPERATOR(LengthsRangeFill, LengthsRangeFillOp<CPUContext>);
42 OPERATOR_SCHEMA(ConstantFill)
45 .AllowInplace({{0, 0}})
46 .TensorInferenceFunction(FillerTensorInference<>)
48 This operator fills the elements of the output tensor with a constant value 49 specified by the `value` argument. 51 - The data type is specified by the `dtype` argument 53 - Currently, the data types supported are *float*, *int32*, *int64*, and *bool* 55 - If the `dtype` argument is not provided, the data type of `value` is used 57 - The output tensor shape is either specified by the `shape` argument or will 58 match the shape of the input tensor if one is provided (if an input tensor is 59 provided, a shape argument should not be set) 61 - Optional additional dimensions can be appended at the end as specified by 62 `extra_shape` argument 64 - If `input_as_shape` is set to True, the input should be a 1D tensor 65 containing the desired output shape (the dimensions specified in `extra_shape` 66 will also be appended) 68 When specifying `dtype` argument, use the integer keys from the *DataType* enum 78 BYTE = 3; // BYTE, when deserialized, is going to be restored as uint8. 83 UINT16 = 8; // uint16_t 85 INT64 = 10; // int64_t 86 FLOAT16 = 12; // at::Half 87 DOUBLE = 13; // double 93 - https://github.com/pytorch/pytorch/blob/master/caffe2/operators/filler_op.cc 97 <summary> <b>Example</b> </summary> 102 workspace.ResetWorkspace() 104 op = core.CreateOperator( 111 workspace.RunOperatorOnce(op) 112 print("Y:", workspace.FetchBlob("Y")) 118 Y: [[[0. 0. 0. 0. 0.] 127 <summary> <b>Example 2</b> </summary> 132 workspace.ResetWorkspace() 134 op = core.CreateOperator( 143 workspace.FeedBlob("X", (np.random.randint(100, size=(3,3))).astype(np.float32)) 144 print("X:", workspace.FetchBlob("X")) 145 workspace.RunOperatorOnce(op) 146 print("Y:", workspace.FetchBlob("Y")) 181 "*(type: primitive; default: 0.0f) value to populate output tensor with.")
184 "*(type: int)* The data type for the elements of the output tensor. " 185 "Strictly must be one of the types from *DataType* enum in TensorProto.")
188 "*(type: int | Tuple(int))* Shape of the output tensor. Cannot pass an " 189 "input blob and this arg at the same time.")
192 "*(type: int | Tuple(int))* Additional dimensions appended at the end " 193 "of the shape indicated by the input blob. Cannot set this" 194 "argument when there is no input blob.")
197 "*(type: int | Tuple(int))* 1D tensor containing the desired output " 198 "shape. First input must be in CPU context.")
202 "*(type: Tensor)* [OPTIONAL] Input tensor to provide shape information.")
206 "*(type: Tensor)* Output tensor of constant values.");
208 OPERATOR_SCHEMA(DiagonalFill)
211 .AllowInplace({{0, 0}})
212 .TensorInferenceFunction(FillerTensorInference<>)
214 The operator fills the diagonal elements of the output tensor (>= 2D) 215 with a constant value specified by the 'value' argument, and others 0. If 216 number of dimensions of the output tensor is greater than 2, all dimensions 219 The data type is specified by the 'dtype' argument. The 'dtype' argument must 220 be one of the data types specified in the 'DataType' enum field in the 221 TensorProto message. If the 'dtype' argument is not provided, the data type of 224 The output tensor shape is specified by the 'shape' argument. If the number of 225 input is 1, the shape will be identical to that of the input at run time with 226 optional additional dimensions appended at the end as specified by 'extra_shape' 227 argument. In that case the 'shape' argument should not be set. 229 If input_as_shape is set to true, then the input should be a 1D tensor 230 containing the desired output shape (the dimensions specified in extra_shape 231 will also be appended) 233 NOTE: Currently, it supports data type of float, int32, int64, and bool. 235 .Arg("value",
"The value for the elements of the output tensor.")
238 "The data type for the elements of the output tensor." 239 "Strictly must be one of the types from DataType enum in TensorProto.")
242 "The shape of the output tensor." 243 "Cannot set the shape argument and pass in an input at the same time.")
246 "The additional dimensions appended at the end of the shape indicated" 248 "Cannot set the extra_shape argument when there is no input blob.")
249 .Arg(
"input_as_shape",
"1D tensor containing the desired output shape")
250 .Input(0,
"input",
"Input tensor (optional) to provide shape information.")
255 "argument and its type is specified by the 'dtype' argument");
257 OPERATOR_SCHEMA(UniformFill)
258 .NumInputs({0, 1, 3})
260 .AllowInplace({{0, 0}})
261 .TensorInferenceFunction(FillerTensorInference<>)
263 Fill the output tensor with float samples from uniform distribution [`min`, `max`]. 265 - The range can be defined either by arguments or input blobs. `min` and `max` are inclusive. 266 - If the range is given by input blobs, you also need to give the shape as input. 267 - When the range is given as arguments, this operator enforces min <= max. When the range is given as inputs, the constraint is not enforced. 268 - When the range is given as inputs and max < min, the first dimension of the output is set to 0. This behavior is allowed so that dynamically sampling indices into a dynamically sized tensor is possible. 269 - The shape of the output can be given as argument or input. 272 - https://github.com/caffe2/caffe2/blob/master/caffe2/operators/filler_op.h 273 - https://github.com/caffe2/caffe2/blob/master/caffe2/operators/filler_op.cc 277 <summary> <b>Example</b> </summary> 283 workspace.ResetWorkspace() 285 op_1 = core.CreateOperator( 294 op_2 = core.CreateOperator( 296 ["shape", "min", "max"], 302 workspace.RunOperatorOnce(op_1) 303 print("output (op_1):\n", workspace.FetchBlob("output")) 305 // Test input-based op 306 workspace.ResetWorkspace() 307 workspace.FeedBlob("shape", np.array([5,5])) 308 workspace.FeedBlob("min", np.array(13.8, dtype=np.float32)) 309 workspace.FeedBlob("max", np.array(19.3, dtype=np.float32)) 310 workspace.RunOperatorOnce(op_2) 311 print("output (op_2):\n", workspace.FetchBlob("output")) 320 [[8.894862 8.225005 6.7890406] 321 [9.588293 7.1072135 7.7234955] 322 [8.210596 6.0202913 9.665462 ]] 324 [[18.965155 15.603871 15.038921 17.14872 18.134571] 325 [18.84237 17.845276 19.214737 16.970337 15.494069] 326 [18.754795 16.724329 15.311974 16.962536 18.60965 ] 327 [15.186268 15.264773 18.73341 19.077969 14.237255] 328 [15.917589 15.844325 16.248466 17.006554 17.502048]] 335 .Arg("min",
"(*float*): minimum value, inclusive")
336 .Arg(
"max",
"(*float*): maximum value, inclusive")
337 .Arg(
"shape",
"(*Tuple(int)*): shape of the output, do not set when `input_as_shape`=1")
340 "(*int*): set to 1 to use the first input as shape; `shape` input must be in CPU context")
344 "(*Tensor`<int>`*): 1-D tensor of the shape of the output, must be used with `input_as_shape` argument")
345 .Input(1,
"min",
"(*Tensor`<float>`*): scalar tensor containing minimum value, inclusive")
346 .Input(2,
"max",
"(*Tensor`<float>`*): scalar tensor containing maximum value, inclusive")
347 .Output(0,
"output",
"(*Tensor`<float>`*): filled output tensor");
348 OPERATOR_SCHEMA(UniformIntFill)
349 .NumInputs({0, 1, 3})
351 .AllowInplace({{0, 0}})
352 .TensorInferenceFunction(FillerTensorInference<TensorProto_DataType_INT32>)
354 Fill the output tensor with int32 samples from uniform distribution [`min`, `max`]. 356 - The range can be defined either by arguments or input blobs. `min` and `max` are inclusive. 357 - If the range is given by input blobs, you also need to give the shape as input. 358 - When the range is given as arguments, this operator enforces min <= max. When the range is given as inputs, the constraint is not enforced. 359 - When the range is given as inputs and max < min, the first dimension of the output is set to 0. This behavior is allowed so that dynamically sampling indices into a dynamically sized tensor is possible. 360 - The shape of the output can be given as argument or input. 363 - https://github.com/caffe2/caffe2/blob/master/caffe2/operators/filler_op.h 364 - https://github.com/caffe2/caffe2/blob/master/caffe2/operators/filler_op.cc 368 <summary> <b>Example</b> </summary> 374 workspace.ResetWorkspace() 376 op_1 = core.CreateOperator( 385 op_2 = core.CreateOperator( 387 ["shape", "min", "max"], 393 workspace.RunOperatorOnce(op_1) 394 print("output (op_1):\n", workspace.FetchBlob("output")) 396 // Test input-based op 397 workspace.ResetWorkspace() 398 workspace.FeedBlob("shape", np.array([5,5])) 399 workspace.FeedBlob("min", np.array(13, dtype=np.int32)) 400 workspace.FeedBlob("max", np.array(19, dtype=np.int32)) 401 workspace.RunOperatorOnce(op_2) 402 print("output (op_2):\n", workspace.FetchBlob("output")) 426 .Arg("min",
"(*int*): minimum value, inclusive")
427 .Arg(
"max",
"(*int*): maximum value, inclusive")
430 "(*Tuple(int)*): shape of the output, do not set when `input_as_shape`=1")
433 "(*int*): set to 1 to use the first input as shape; `shape` input must be in CPU context")
434 .Input(0,
"shape",
"(*Tensor`<int>`*): 1-D tensor of the shape of the output, must be used with `input_as_shape` argument")
435 .Input(1,
"min",
"(*Tensor`<int>`*): scalar tensor containing minimum value, inclusive")
436 .Input(2,
"max",
"(*Tensor`<int>`*): scalar tensor containing maximum value, inclusive")
437 .Output(0,
"output",
"(*Tensor`<int>`*): filled output tensor");
438 OPERATOR_SCHEMA(UniqueUniformFill)
441 .AllowInplace({{0, 0}})
442 .TensorInferenceFunction(FillerTensorInference<>)
444 Fill the output tensor with uniform samples between min and max (inclusive). 445 If the second input is given, its elements will be excluded from uniform 446 sampling. Using the second input will require you to provide shape via the first 449 .Arg("min",
"Minimum value, inclusive")
450 .Arg(
"max",
"Maximum value, inclusive")
453 "The data type for the elements of the output tensor." 454 "Strictly must be one of the types from DataType enum in TensorProto." 455 "This only supports INT32 and INT64 now. If not set, assume INT32")
458 "The shape of the output tensor." 459 "Cannot set the shape argument and pass in an input at the same time.")
462 "The additional dimensions appended at the end of the shape indicated" 463 "by the input blob. " 464 "Cannot set the extra_shape argument when there is no input blob.")
467 "1D tensor containing the desired output shape. First input must be in CPU context.")
468 .Input(0,
"input",
"Input tensor to provide shape information")
472 "(optional) Avoid elements in this tensor. Elements must be unique.")
473 .Output(0,
"output",
"Output tensor of unique uniform samples");
474 OPERATOR_SCHEMA(GaussianFill)
477 .AllowInplace({{0, 0}})
478 .TensorInferenceFunction(FillerTensorInference<>)
480 This op fills an output tensor with samples drawn from a normal distribution specified by the mean and standard deviation arguments. The output tensor shape is specified by the *shape* argument. However, if *input_as_shape* is set to *true*, then the *input* should be a 1D tensor containing the desired output shape (the dimensions specified in *extra_shape* will also be appended). In this case, the *shape* argument should **not** be set. 482 *Note: cannot set the shape argument and pass in an input at the same time.* 485 - https://github.com/caffe2/caffe2/blob/master/caffe2/operators/filler_op.h 486 - https://github.com/caffe2/caffe2/blob/master/caffe2/operators/filler_op.cc 490 <summary> <b>Example</b> </summary> 496 workspace.ResetWorkspace() 498 op = core.CreateOperator( 507 workspace.RunOperatorOnce(op) 508 print("Out:\n", workspace.FetchBlob("out")) 517 [[1.2084167 2.3336504 2.827349 ] 518 [2.7108908 0.9374752 1.7173369 ] 519 [0.03320992 2.1775863 1.0894578 ]] 528 "*(type: float; default: 0.)* Mean of the distribution to draw from.")
531 "*(type: float; default: 1.)* Standard deviation of the distribution to draw from.")
534 "*(type: [int])* Desired shape of the *output* tensor.")
537 "*(type: [int])* The additional dimensions appended at the end of the *shape* indicated by the input blob. Cannot set the *extra_shape* argument when there is no input blob.")
540 "*(type: bool; default: False)* set to *True* to use the *input* as shape. First, input must be in CPU context.")
544 "(Optional) 1D tensor specifying the shape of the output. Must be used with *input_as_shape=True*")
548 "Output tensor of random values drawn from a normal distribution. If the shape argument is set, this is the shape specified, and if the *input* exists and *input_as_shape=True*, it is the shape specified by the *input* tensor.");
549 OPERATOR_SCHEMA(XavierFill)
552 .AllowInplace({{0, 0}})
553 .TensorInferenceFunction(FillerTensorInference<>)
555 This op fills an output tensor with values sampled from a uniform distribution with the range determined by the desired shape of the output. Rather, than specifying the range of values manually, the novelty of Xavier Fill is that it automatically scales the range of the distribution it draws from based on the size of the desired output tensor. For more information check out the paper [Understanding the difficulty of training deep feedforward neural networks](http://proceedings.mlr.press/v9/glorot10a/glorot10a.pdf). The output tensor shape is specified by the *shape* argument. However, if *input_as_shape* is set to *true*, then the *input* should be a 1D tensor containing the desired output shape (the dimensions specified in *extra_shape* will also be appended). In this case, the *shape* argument should **not** be set. 557 *Note: Do not set the shape argument and pass in an input at the same time.* 560 - https://github.com/caffe2/caffe2/blob/master/caffe2/operators/filler_op.h 561 - https://github.com/caffe2/caffe2/blob/master/caffe2/operators/filler_op.cc 565 <summary> <b>Example</b> </summary> 571 workspace.ResetWorkspace() 573 op = core.CreateOperator( 580 workspace.RunOperatorOnce(op) 581 print("Out:\n", workspace.FetchBlob("out")) 590 [[-0.8412168 0.33207083 -0.88418937] 591 [ 0.43059897 -0.8340702 0.07781601] 592 [ 0.93261135 -0.24542928 -0.3980782 ]] 601 "*(type: [int])* Desired shape of the *output* tensor.")
604 "*(type: [int])* The additional dimensions appended at the end of the *shape* indicated by the input blob. Cannot set the *extra_shape* argument when there is no input blob.")
607 "*(type: bool; default: False)* set to *True* to use the *input* as shape. First, input must be in CPU context.")
611 "(Optional) 1D tensor specifying the shape of the output. Must be used with *input_as_shape=True*")
615 "Output tensor of random values drawn from an automatically scaled uniform distribution, based on the size of the output tensor. If the shape argument is set, this is the shape specified by the shape argument, and if the *input* exists and *input_as_shape=True*, it is the shape specified by the *input* tensor.");
617 OPERATOR_SCHEMA(MSRAFill)
620 .AllowInplace({{0, 0}})
621 .TensorInferenceFunction(FillerTensorInference<>);
622 OPERATOR_SCHEMA(RangeFill)
625 .AllowInplace({{0, 0}})
626 .TensorInferenceFunction(FillerTensorInference<>);
628 NO_GRADIENT(UniformFill);
629 NO_GRADIENT(UniformIntFill);
630 NO_GRADIENT(UniqueUniformFill);
631 NO_GRADIENT(ConstantFill);
632 NO_GRADIENT(DiagonalFill);
633 NO_GRADIENT(GaussianFill);
634 NO_GRADIENT(XavierFill);
635 NO_GRADIENT(MSRAFill);
636 NO_GRADIENT(RangeFill);
638 OPERATOR_SCHEMA(LengthsRangeFill)
642 The *LengthsRangeFill* op takes a single input *lengths* and outputs a single tensor *range_sequence*. For each element of *lengths*, the op appends the range(0,lengths) vector to the end of *range_sequence*. For example, if input=[2,4,1], the output would be [0,1,0,1,2,3,0]. 645 - https://github.com/caffe2/caffe2/blob/master/caffe2/operators/filler_op.h 646 - https://github.com/caffe2/caffe2/blob/master/caffe2/operators/filler_op.cc 650 <summary> <b>Example</b> </summary> 656 workspace.ResetWorkspace() 658 op = core.CreateOperator( 664 workspace.FeedBlob("lengths", np.array([2,4,1]).astype(np.int32)) 665 print("lengths:\n", workspace.FetchBlob("lengths")) 667 workspace.RunOperatorOnce(op) 668 print("range_sequence: \n", workspace.FetchBlob("range_sequence")) 686 .Input(0, "lengths",
"1D tensor of int32 or int64 segment lengths.")
690 "1D tensor whose size is the sum of *lengths*");
691 NO_GRADIENT(LengthsRangeFill);
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...