Caffe2 - C++ API
A deep learning, cross platform ML framework
given_tensor_byte_string_to_uint8_fill_op.cc
1 #include "caffe2/operators/given_tensor_byte_string_to_uint8_fill_op.h"
2 
3 namespace caffe2 {
4 REGISTER_CPU_OPERATOR(
5  GivenTensorByteStringToUInt8Fill,
6  GivenTensorByteStringToUInt8FillOp<CPUContext>);
7 
8 NO_GRADIENT(GivenTensorByteStringToUInt8Fill);
9 
10 OPERATOR_SCHEMA(GivenTensorByteStringToUInt8Fill)
11  .NumInputs(0, 1)
12  .NumOutputs(1)
13  .AllowInplace({{0, 0}})
14  .SetDoc(R"DOC(
15 This op fills a uint8 output tensor with the data specified by the *value* argument. The data must previously be serialized as a byte string. 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.
16 
17 This op allows us to write uint8 tensors to Protobuf as byte strings and read them back as uint8 tensors in order to avoid the Protobuf uint32_t varint encoding size penalty.
18 
19 <details>
20 
21 <summary> <b>Example</b> </summary>
22 
23 **Code**
24 
25 ```
26 
27 workspace.ResetWorkspace()
28 
29 val = np.array([1, 2, 3], dtype=np.uint8)
30 op = core.CreateOperator(
31  "GivenTensorByteStringToUInt8Fill",
32  [],
33  ["out"],
34  values=[val.tobytes()],
35  shape=val.shape,
36 )
37 
38 workspace.RunOperatorOnce(op)
39 print("Out:\n", workspace.FetchBlob("out"))
40 
41 ```
42 
43 **Result**
44 
45 ```
46 
47 Out:
48  [1 2 3]
49 
50 ```
51 
52 </details>
53 
54 )DOC")
55  .Arg(
56  "values",
57  "The value for the elements of the output tensor.",
58  true /* required */)
59  .Arg(
60  "shape",
61  "The shape of the output tensor."
62  "Cannot set the shape argument and pass in an input at the same time.")
63  .Arg(
64  "extra_shape",
65  "The additional dimensions appended at the end of the shape indicated"
66  "by the input blob."
67  "Cannot set the extra_shape argument when there is no input blob.")
68  .Arg(
69  "input_as_shape",
70  "1D tensor containing the desired output shape. First input must be in CPU context.")
71  .TensorInferenceFunction(
72  FillerTensorInference<TensorProto_DataType_STRING>);
73 
74 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13