Caffe2 - C++ API
A deep learning, cross platform ML framework
given_tensor_fill_op.cc
1 #include "caffe2/operators/given_tensor_fill_op.h"
2 
3 namespace caffe2 {
4 
5 REGISTER_CPU_OPERATOR(GivenTensorFill, GivenTensorFillOp<float, CPUContext>);
6 REGISTER_CPU_OPERATOR(
7  GivenTensorDoubleFill,
8  GivenTensorFillOp<double, CPUContext>);
9 REGISTER_CPU_OPERATOR(GivenTensorBoolFill, GivenTensorFillOp<bool, CPUContext>);
10 REGISTER_CPU_OPERATOR(GivenTensorIntFill, GivenTensorFillOp<int, CPUContext>);
11 REGISTER_CPU_OPERATOR(
12  GivenTensorInt64Fill,
13  GivenTensorFillOp<int64_t, CPUContext>);
14 REGISTER_CPU_OPERATOR(
15  GivenTensorStringFill,
16  GivenTensorFillOp<std::string, CPUContext>);
17 
18 NO_GRADIENT(GivenTensorFill);
19 NO_GRADIENT(GivenTensorDoubleFill);
20 NO_GRADIENT(GivenTensorBoolFill);
21 NO_GRADIENT(GivenTensorIntFill);
22 NO_GRADIENT(GivenTensorInt64Fill);
23 NO_GRADIENT(GivenTensorStringFill);
24 
25 OPERATOR_SCHEMA(GivenTensorFill)
26  .NumInputs(0, 1)
27  .NumOutputs(1)
28  .AllowInplace({{0, 0}})
29  .SetDoc(R"DOC(
30 This op fills an output tensor with the data specified by the *value* and *dtype* arguments. The output tensor shape is specified by the *shape* argument. Beware, when using this argument *value* should have a value for every element of the *output*, as missing values will not be initialized automatically. 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.
31 
32 *Note: Do not set the shape argument and pass in an input at the same time.*
33 
34 Github Links:
35 - https://github.com/caffe2/caffe2/blob/master/caffe2/operators/given_tensor_fill_op.h
36 - https://github.com/caffe2/caffe2/blob/master/caffe2/operators/given_tensor_fill_op.cc
37 
38 <details>
39 
40 <summary> <b>Example</b> </summary>
41 
42 **Code**
43 
44 ```
45 
46 workspace.ResetWorkspace()
47 
48 op = core.CreateOperator(
49  "GivenTensorFill",
50  [],
51  ["out"],
52  values=[1., 2., 3.],
53  shape=[3],
54 )
55 
56 workspace.RunOperatorOnce(op)
57 print("Out:\n", workspace.FetchBlob("out"))
58 
59 ```
60 
61 **Result**
62 
63 ```
64 
65 Out:
66  [1. 2. 3.]
67 
68 ```
69 
70 </details>
71 
72 )DOC")
73  .Arg(
74  "values",
75  "*(type depends on dtype, Required=True)* The value of the elements to go in the *output* tensor.",
76  true /* required */)
77  .Arg(
78  "dtype",
79  "The data type for the elements of the output tensor. Strictly must be one of the types from DataType enum in TensorProto.")
80  .Arg(
81  "shape",
82  "*(type: [int])* Desired shape of the *output* tensor.")
83  .Arg(
84  "extra_shape",
85  "*(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.")
86  .Arg(
87  "input_as_shape",
88  "*(type: bool; default: False)* set to *True* to use the *input* as shape. First, input must be in CPU context.")
89  .Input(
90  0,
91  "input",
92  "(Optional) 1D tensor specifying the shape of the output. Must be used with *input_as_shape=True*")
93  .Output(
94  0,
95  "output",
96  "Output tensor with desired dimension filled with specified data. 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.")
97  .TensorInferenceFunction(FillerTensorInference<>);
98 
99 OPERATOR_SCHEMA(GivenTensorDoubleFill)
100  .NumInputs(0, 1)
101  .NumOutputs(1)
102  .AllowInplace({{0, 0}})
103  .Arg(
104  "values",
105  "The value for the elements of the output tensor.",
106  true /* required */)
107  .Arg(
108  "shape",
109  "The shape of the output tensor."
110  "Cannot set the shape argument and pass in an input at the same time.")
111  .Arg(
112  "extra_shape",
113  "The additional dimensions appended at the end of the shape indicated"
114  "by the input blob."
115  "Cannot set the extra_shape argument when there is no input blob.")
116  .Arg(
117  "input_as_shape",
118  "1D tensor containing the desired output shape. First input must be in CPU context.")
119  .TensorInferenceFunction(
120  FillerTensorInference<TensorProto_DataType_DOUBLE>);
121 
122 OPERATOR_SCHEMA(GivenTensorBoolFill)
123  .NumInputs(0, 1)
124  .NumOutputs(1)
125  .AllowInplace({{0, 0}})
126  .Arg(
127  "values",
128  "The value for the elements of the output tensor.",
129  true /* required */)
130  .Arg(
131  "shape",
132  "The shape of the output tensor."
133  "Cannot set the shape argument and pass in an input at the same time.")
134  .Arg(
135  "extra_shape",
136  "The additional dimensions appended at the end of the shape indicated"
137  "by the input blob."
138  "Cannot set the extra_shape argument when there is no input blob.")
139  .Arg(
140  "input_as_shape",
141  "1D tensor containing the desired output shape. First input must be in CPU context.")
142  .TensorInferenceFunction(FillerTensorInference<TensorProto_DataType_BOOL>);
143 
144 OPERATOR_SCHEMA(GivenTensorIntFill)
145  .NumInputs(0, 1)
146  .NumOutputs(1)
147  .AllowInplace({{0, 0}})
148  .Arg(
149  "values",
150  "The value for the elements of the output tensor.",
151  true /* required */)
152  .Arg(
153  "shape",
154  "The shape of the output tensor."
155  "Cannot set the shape argument and pass in an input at the same time.")
156  .Arg(
157  "extra_shape",
158  "The additional dimensions appended at the end of the shape indicated"
159  "by the input blob."
160  "Cannot set the extra_shape argument when there is no input blob.")
161  .Arg(
162  "input_as_shape",
163  "1D tensor containing the desired output shape. First input must be in CPU context.")
164  .TensorInferenceFunction(FillerTensorInference<TensorProto_DataType_INT32>);
165 
166 OPERATOR_SCHEMA(GivenTensorInt64Fill)
167  .NumInputs(0, 1)
168  .NumOutputs(1)
169  .AllowInplace({{0, 0}})
170  .Arg(
171  "values",
172  "The value for the elements of the output tensor.",
173  true /* required */)
174  .Arg(
175  "shape",
176  "The shape of the output tensor."
177  "Cannot set the shape argument and pass in an input at the same time.")
178  .Arg(
179  "extra_shape",
180  "The additional dimensions appended at the end of the shape indicated"
181  "by the input blob."
182  "Cannot set the extra_shape argument when there is no input blob.")
183  .Arg(
184  "input_as_shape",
185  "1D tensor containing the desired output shape. First input must be in CPU context.")
186  .TensorInferenceFunction(FillerTensorInference<TensorProto_DataType_INT64>);
187 
188 OPERATOR_SCHEMA(GivenTensorStringFill)
189  .NumInputs(0, 1)
190  .NumOutputs(1)
191  .AllowInplace({{0, 0}})
192  .Arg(
193  "values",
194  "The value for the elements of the output tensor.",
195  true /* required */)
196  .Arg(
197  "shape",
198  "The shape of the output tensor."
199  "Cannot set the shape argument and pass in an input at the same time.")
200  .Arg(
201  "extra_shape",
202  "The additional dimensions appended at the end of the shape indicated"
203  "by the input blob."
204  "Cannot set the extra_shape argument when there is no input blob.")
205  .Arg(
206  "input_as_shape",
207  "1D tensor containing the desired output shape. First input must be in CPU context.")
208  .TensorInferenceFunction(
209  FillerTensorInference<TensorProto_DataType_STRING>);
210 
211 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13