Caffe2 - C++ API
A deep learning, cross platform ML framework
elementwise_sum_op.cc
1 #include "caffe2/operators/utility_ops.h"
2 
3 namespace caffe2 {
4 
5 REGISTER_CPU_OPERATOR(Sum, SumOp<CPUContext>);
6 
7 OPERATOR_SCHEMA(Sum)
8  .NumInputs(1, INT_MAX)
9  .NumOutputs(1)
10  .AllowInplace({{0, 0}})
11  .CostInferenceFunction(CostInferenceForSum)
12  .InputsCanCrossDevices()
13  .IdenticalTypeAndShapeOfInput(0)
14  .SetDoc(R"DOC(
15 Element-wise sum of each of the input tensors. The first input tensor can be used
16 in-place as the output tensor, in which case the sum will be done in place and
17 results will be accumulated the first input tensor. All inputs and outputs must
18 have the same shape and data type.
19 
20 Github Links:
21 
22 - https://github.com/pytorch/pytorch/blob/master/caffe2/operators/elementwise_sum_op.cc
23 
24 
25 <details>
26 
27 <summary> <b>Example</b> </summary>
28 
29 **Code**
30 
31 ```
32 
33 workspace.ResetWorkspace()
34 
35 op = core.CreateOperator(
36  "Sum",
37  ["A", "B"],
38  ["C"],
39 )
40 
41 workspace.FeedBlob("A", np.array([[1,2],[3,4]]).astype(np.float32))
42 workspace.FeedBlob("B", np.array([[5,6],[7,8]]).astype(np.float32))
43 print("A:", workspace.FetchBlob("A"))
44 print("B:", workspace.FetchBlob("B"))
45 workspace.RunOperatorOnce(op)
46 print("C:", workspace.FetchBlob("A"))
47 
48 ```
49 
50 **Result**
51 
52 ```
53 
54 A: [[1. 2.]
55  [3. 4.]]
56 B: [[5. 6.]
57  [7. 8.]]
58 C: [[1. 2.]
59  [3. 4.]]
60 
61 ```
62 
63 </details>
64 
65 <details>
66 
67 <summary> <b>Example 2</b> </summary>
68 
69 **Code**
70 
71 ```
72 
73 workspace.ResetWorkspace()
74 
75 op = core.CreateOperator(
76  "Sum",
77  ["A", "B"],
78  ["A"], // inplace
79 )
80 
81 workspace.FeedBlob("A", np.array([[1,2,5],[8,3,4]]).astype(np.float32))
82 workspace.FeedBlob("B", np.array([[9,5,6],[6,7,8]]).astype(np.float32))
83 print("A:", workspace.FetchBlob("A"))
84 print("B:", workspace.FetchBlob("B"))
85 workspace.RunOperatorOnce(op)
86 print("A after Sum:", workspace.FetchBlob("A"))
87 
88 ```
89 
90 **Result**
91 
92 ```
93 
94 A: [[1. 2. 5.]
95  [8. 3. 4.]]
96 B: [[9. 5. 6.]
97  [6. 7. 8.]]
98 A after Sum: [[10. 7. 11.]
99  [14. 10. 12.]]
100 
101 ```
102 
103 </details>
104 
105 )DOC")
106  .Input(
107  0,
108  "A",
109  "*(type: Tensor`<float>`)* First tensor to be added element-wise.")
110  .Input(
111  1,
112  "B",
113  "*(type: Tensor`<float>`)* Second tensor to be added element-wise.")
114  .Output(0, "C", "*(type: Tensor`<float>`)* Sum of A and B.")
115  .InheritOnnxSchema();
116 }
Definition: OpClasses.h:414
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13