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(Tensor* output) {
7  float* data = output->template mutable_data<float>();
8  for (int i = 0; i < output->numel(); ++i) {
9  data[i] = i;
10  }
11  return true;
12 }
13 
14 template <>
15 template <typename T>
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>();
20  // first fill everything with 0
21  math::Set<T, CPUContext>(output->numel(), T(0), data, &context_);
22  // then calculate step size for diagonal
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_);
26  data += step;
27  }
28  return true;
29 }
30 
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>);
41 
42 OPERATOR_SCHEMA(ConstantFill)
43  .NumInputs(0, 1)
44  .NumOutputs(1)
45  .AllowInplace({{0, 0}})
46  .TensorInferenceFunction(FillerTensorInference<>)
47  .SetDoc(R"DOC(
48 This operator fills the elements of the output tensor with a constant value
49 specified by the `value` argument.
50 
51 - The data type is specified by the `dtype` argument
52 
53 - Currently, the data types supported are *float*, *int32*, *int64*, and *bool*
54 
55 - If the `dtype` argument is not provided, the data type of `value` is used
56 
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)
60 
61 - Optional additional dimensions can be appended at the end as specified by
62 `extra_shape` argument
63 
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)
67 
68 When specifying `dtype` argument, use the integer keys from the *DataType* enum
69 in TensorProto:
70 
71 ```
72 message TensorProto {
73  ...
74  enum DataType {
75  UNDEFINED = 0;
76  FLOAT = 1; // float
77  INT32 = 2; // int
78  BYTE = 3; // BYTE, when deserialized, is going to be restored as uint8.
79  STRING = 4; // string
80  BOOL = 5; // bool
81  UINT8 = 6; // uint8_t
82  INT8 = 7; // int8_t
83  UINT16 = 8; // uint16_t
84  INT16 = 9; // int16_t
85  INT64 = 10; // int64_t
86  FLOAT16 = 12; // at::Half
87  DOUBLE = 13; // double
88  }
89 ```
90 
91 Github Links:
92 
93 - https://github.com/pytorch/pytorch/blob/master/caffe2/operators/filler_op.cc
94 
95 <details>
96 
97 <summary> <b>Example</b> </summary>
98 
99 **Code**
100 
101 ```
102 workspace.ResetWorkspace()
103 
104 op = core.CreateOperator(
105  "ConstantFill",
106  [],
107  ["Y"],
108  shape=(1,5,5)
109 )
110 
111 workspace.RunOperatorOnce(op)
112 print("Y:", workspace.FetchBlob("Y"))
113 ```
114 
115 **Result**
116 
117 ```
118 Y: [[[0. 0. 0. 0. 0.]
119  [0. 0. 0. 0. 0.]
120  [0. 0. 0. 0. 0.]
121  [0. 0. 0. 0. 0.]
122  [0. 0. 0. 0. 0.]]]
123 ```
124 </details>
125 
126 <details>
127 <summary> <b>Example 2</b> </summary>
128 
129 **Code**
130 
131 ```
132 workspace.ResetWorkspace()
133 
134 op = core.CreateOperator(
135  "ConstantFill",
136  ["X"],
137  ["Y"],
138  value=4.0,
139  dtype=1,
140  extra_shape=(1,2)
141 )
142 
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"))
147 ```
148 
149 **Result**
150 
151 ```
152 X: [[86. 30. 84.]
153  [34. 51. 9.]
154  [29. 86. 59.]]
155 Y: [[[[4. 4.]]
156 
157  [[4. 4.]]
158 
159  [[4. 4.]]]
160 
161 
162  [[[4. 4.]]
163 
164  [[4. 4.]]
165 
166  [[4. 4.]]]
167 
168 
169  [[[4. 4.]]
170 
171  [[4. 4.]]
172 
173  [[4. 4.]]]]
174 ```
175 
176 </details>
177 
178 )DOC")
179  .Arg(
180  "value",
181  "*(type: primitive; default: 0.0f) value to populate output tensor with.")
182  .Arg(
183  "dtype",
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.")
186  .Arg(
187  "shape",
188  "*(type: int | Tuple(int))* Shape of the output tensor. Cannot pass an "
189  "input blob and this arg at the same time.")
190  .Arg(
191  "extra_shape",
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.")
195  .Arg(
196  "input_as_shape",
197  "*(type: int | Tuple(int))* 1D tensor containing the desired output "
198  "shape. First input must be in CPU context.")
199  .Input(
200  0,
201  "X",
202  "*(type: Tensor)* [OPTIONAL] Input tensor to provide shape information.")
203  .Output(
204  0,
205  "Y",
206  "*(type: Tensor)* Output tensor of constant values.");
207 
208 OPERATOR_SCHEMA(DiagonalFill)
209  .NumInputs(0, 1)
210  .NumOutputs(1)
211  .AllowInplace({{0, 0}})
212  .TensorInferenceFunction(FillerTensorInference<>)
213  .SetDoc(R"DOC(
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
217 must be equal.
218 
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
222 'value' is used.
223 
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.
228 
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)
232 
233 NOTE: Currently, it supports data type of float, int32, int64, and bool.
234 )DOC")
235  .Arg("value", "The value for the elements of the output tensor.")
236  .Arg(
237  "dtype",
238  "The data type for the elements of the output tensor."
239  "Strictly must be one of the types from DataType enum in TensorProto.")
240  .Arg(
241  "shape",
242  "The shape of the output tensor."
243  "Cannot set the shape argument and pass in an input at the same time.")
244  .Arg(
245  "extra_shape",
246  "The additional dimensions appended at the end of the shape indicated"
247  "by the input blob."
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.")
251  .Output(
252  0,
253  "output",
254  "Output tensor"
255  "argument and its type is specified by the 'dtype' argument");
256 
257 OPERATOR_SCHEMA(UniformFill)
258  .NumInputs({0, 1, 3})
259  .NumOutputs(1)
260  .AllowInplace({{0, 0}})
261  .TensorInferenceFunction(FillerTensorInference<>)
262  .SetDoc(R"DOC(
263 Fill the output tensor with float samples from uniform distribution [`min`, `max`].
264 
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.
270 
271 Github Links:
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
274 
275 <details>
276 
277 <summary> <b>Example</b> </summary>
278 
279 **Code**
280 
281 ```
282 
283 workspace.ResetWorkspace()
284 
285 op_1 = core.CreateOperator(
286  "UniformFill",
287  [],
288  ["output"],
289  min=5.5,
290  max=10.5,
291  shape=(3,3)
292 )
293 
294 op_2 = core.CreateOperator(
295  "UniformFill",
296  ["shape", "min", "max"],
297  ["output"],
298  input_as_shape=1
299 )
300 
301 // Test arg-based op
302 workspace.RunOperatorOnce(op_1)
303 print("output (op_1):\n", workspace.FetchBlob("output"))
304 
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"))
312 
313 ```
314 
315 **Result**
316 
317 ```
318 
319 output (op_1):
320  [[8.894862 8.225005 6.7890406]
321  [9.588293 7.1072135 7.7234955]
322  [8.210596 6.0202913 9.665462 ]]
323 output (op_2):
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]]
329 
330 ```
331 
332 </details>
333 
334 )DOC")
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")
338  .Arg(
339  "input_as_shape",
340  "(*int*): set to 1 to use the first input as shape; `shape` input must be in CPU context")
341  .Input(
342  0,
343  "shape",
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})
350  .NumOutputs(1)
351  .AllowInplace({{0, 0}})
352  .TensorInferenceFunction(FillerTensorInference<TensorProto_DataType_INT32>)
353  .SetDoc(R"DOC(
354 Fill the output tensor with int32 samples from uniform distribution [`min`, `max`].
355 
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.
361 
362 Github Links:
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
365 
366 <details>
367 
368 <summary> <b>Example</b> </summary>
369 
370 **Code**
371 
372 ```
373 
374 workspace.ResetWorkspace()
375 
376 op_1 = core.CreateOperator(
377  "UniformIntFill",
378  [],
379  ["output"],
380  min=5,
381  max=10,
382  shape=(3,3)
383 )
384 
385 op_2 = core.CreateOperator(
386  "UniformIntFill",
387  ["shape", "min", "max"],
388  ["output"],
389  input_as_shape=1
390 )
391 
392 // Test arg-based op
393 workspace.RunOperatorOnce(op_1)
394 print("output (op_1):\n", workspace.FetchBlob("output"))
395 
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"))
403 
404 ```
405 
406 **Result**
407 
408 ```
409 
410 output (op_1):
411  [[ 6 10 7]
412  [ 5 10 6]
413  [ 7 5 10]]
414 output (op_2):
415  [[19 13 15 13 13]
416  [14 17 14 15 15]
417  [17 14 19 13 13]
418  [17 18 16 13 18]
419  [14 15 16 18 16]]
420 
421 ```
422 
423 </details>
424 
425  )DOC")
426  .Arg("min", "(*int*): minimum value, inclusive")
427  .Arg("max", "(*int*): maximum value, inclusive")
428  .Arg(
429  "shape",
430  "(*Tuple(int)*): shape of the output, do not set when `input_as_shape`=1")
431  .Arg(
432  "input_as_shape",
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)
439  .NumInputs(0, 2)
440  .NumOutputs(1)
441  .AllowInplace({{0, 0}})
442  .TensorInferenceFunction(FillerTensorInference<>)
443  .SetDoc(R"DOC(
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
447 input.
448 )DOC")
449  .Arg("min", "Minimum value, inclusive")
450  .Arg("max", "Maximum value, inclusive")
451  .Arg(
452  "dtype",
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")
456  .Arg(
457  "shape",
458  "The shape of the output tensor."
459  "Cannot set the shape argument and pass in an input at the same time.")
460  .Arg(
461  "extra_shape",
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.")
465  .Arg(
466  "input_as_shape",
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")
469  .Input(
470  1,
471  "avoid",
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)
475  .NumInputs(0, 1)
476  .NumOutputs(1)
477  .AllowInplace({{0, 0}})
478  .TensorInferenceFunction(FillerTensorInference<>)
479  .SetDoc(R"DOC(
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.
481 
482 *Note: cannot set the shape argument and pass in an input at the same time.*
483 
484 Github Links:
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
487 
488 <details>
489 
490 <summary> <b>Example</b> </summary>
491 
492 **Code**
493 
494 ```
495 
496 workspace.ResetWorkspace()
497 
498 op = core.CreateOperator(
499  "GaussianFill",
500  [],
501  ["out"],
502  shape=[3,3],
503  mean=2.0,
504  std=1.1
505 )
506 
507 workspace.RunOperatorOnce(op)
508 print("Out:\n", workspace.FetchBlob("out"))
509 
510 ```
511 
512 **Result**
513 
514 ```
515 
516 Out:
517  [[1.2084167 2.3336504 2.827349 ]
518  [2.7108908 0.9374752 1.7173369 ]
519  [0.03320992 2.1775863 1.0894578 ]]
520 
521 ```
522 
523 </details>
524 
525 )DOC")
526  .Arg(
527  "mean",
528  "*(type: float; default: 0.)* Mean of the distribution to draw from.")
529  .Arg(
530  "std",
531  "*(type: float; default: 1.)* Standard deviation of the distribution to draw from.")
532  .Arg(
533  "shape",
534  "*(type: [int])* Desired shape of the *output* tensor.")
535  .Arg(
536  "extra_shape",
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.")
538  .Arg(
539  "input_as_shape",
540  "*(type: bool; default: False)* set to *True* to use the *input* as shape. First, input must be in CPU context.")
541  .Input(
542  0,
543  "input",
544  "(Optional) 1D tensor specifying the shape of the output. Must be used with *input_as_shape=True*")
545  .Output(
546  0,
547  "output",
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)
550  .NumInputs(0, 1)
551  .NumOutputs(1)
552  .AllowInplace({{0, 0}})
553  .TensorInferenceFunction(FillerTensorInference<>)
554  .SetDoc(R"DOC(
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.
556 
557 *Note: Do not set the shape argument and pass in an input at the same time.*
558 
559 Github Links:
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
562 
563 <details>
564 
565 <summary> <b>Example</b> </summary>
566 
567 **Code**
568 
569 ```
570 
571 workspace.ResetWorkspace()
572 
573 op = core.CreateOperator(
574  "XavierFill",
575  [],
576  ["out"],
577  shape=[3,3],
578 )
579 
580 workspace.RunOperatorOnce(op)
581 print("Out:\n", workspace.FetchBlob("out"))
582 
583 ```
584 
585 **Result**
586 
587 ```
588 
589 Out:
590  [[-0.8412168 0.33207083 -0.88418937]
591  [ 0.43059897 -0.8340702 0.07781601]
592  [ 0.93261135 -0.24542928 -0.3980782 ]]
593 
594 ```
595 
596 </details>
597 
598 )DOC")
599  .Arg(
600  "shape",
601  "*(type: [int])* Desired shape of the *output* tensor.")
602  .Arg(
603  "extra_shape",
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.")
605  .Arg(
606  "input_as_shape",
607  "*(type: bool; default: False)* set to *True* to use the *input* as shape. First, input must be in CPU context.")
608  .Input(
609  0,
610  "input",
611  "(Optional) 1D tensor specifying the shape of the output. Must be used with *input_as_shape=True*")
612  .Output(
613  0,
614  "output",
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.");
616 
617 OPERATOR_SCHEMA(MSRAFill)
618  .NumInputs(0, 1)
619  .NumOutputs(1)
620  .AllowInplace({{0, 0}})
621  .TensorInferenceFunction(FillerTensorInference<>);
622 OPERATOR_SCHEMA(RangeFill)
623  .NumInputs(0, 1)
624  .NumOutputs(1)
625  .AllowInplace({{0, 0}})
626  .TensorInferenceFunction(FillerTensorInference<>);
627 
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);
637 
638 OPERATOR_SCHEMA(LengthsRangeFill)
639  .NumInputs(1)
640  .NumOutputs(1)
641  .SetDoc(R"DOC(
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].
643 
644 Github Links:
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
647 
648 <details>
649 
650 <summary> <b>Example</b> </summary>
651 
652 **Code**
653 
654 ```
655 
656 workspace.ResetWorkspace()
657 
658 op = core.CreateOperator(
659  "LengthsRangeFill",
660  ["lengths"],
661  ["range_sequence"],
662 )
663 
664 workspace.FeedBlob("lengths", np.array([2,4,1]).astype(np.int32))
665 print("lengths:\n", workspace.FetchBlob("lengths"))
666 
667 workspace.RunOperatorOnce(op)
668 print("range_sequence: \n", workspace.FetchBlob("range_sequence"))
669 
670 ```
671 
672 **Result**
673 
674 ```
675 
676 lengths:
677  [2 4 1]
678 range_sequence:
679  [0 1 0 1 2 3 0]
680 
681 ```
682 
683 </details>
684 
685 )DOC")
686  .Input(0, "lengths", "1D tensor of int32 or int64 segment lengths.")
687  .Output(
688  0,
689  "range_sequence",
690  "1D tensor whose size is the sum of *lengths*");
691 NO_GRADIENT(LengthsRangeFill);
692 
693 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13