1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
#include "caffe2/operators/minmax_ops.h"
#include "caffe2/utils/eigen_utils.h"
namespace caffe2 {
REGISTER_CPU_OPERATOR(Max, MaxOp<float, CPUContext>);
REGISTER_CPU_OPERATOR(Min, MinOp<float, CPUContext>);
OPERATOR_SCHEMA(Max)
.NumInputs(1, INT_MAX)
.NumOutputs(1)
.IdenticalTypeAndShapeOfInput(0)
.AllowInplace({{0, 0}})
.SetDoc(R"DOC(
Element-wise max of an arbitrary number of input tensors. This operation can be
performed in-place, by using the first input blob as the output blob. All inputs
must have the same shape and data type, and the output will have the same shape
as the inputs.
Github Link:
- https://github.com/pytorch/pytorch/blob/master/caffe2/operators/minmax_ops.cc
<details>
<summary> <b>Example</b> </summary>
**Code**
```
workspace.ResetWorkspace()
op = core.CreateOperator(
"Max",
["X", "Y", "Z"],
["X"],
)
workspace.FeedBlob("X", (np.random.rand(3,3)).astype(np.float32))
workspace.FeedBlob("Y", (np.random.rand(3,3)).astype(np.float32))
workspace.FeedBlob("Z", (np.random.rand(3,3)).astype(np.float32))
print("X:", workspace.FetchBlob("X"))
print("Y:", workspace.FetchBlob("Y"))
print("Z:", workspace.FetchBlob("Z"))
workspace.RunOperatorOnce(op)
print("Max:", workspace.FetchBlob("X"))
```
**Result**
```
X:
[[0.4496477 0.07061381 0.7139333 ]
[0.83203 0.05970785 0.72786295]
[0.75988126 0.04601283 0.32820013]]
Y:
[[0.05683139 0.16872478 0.671098 ]
[0.70739156 0.09878621 0.03416285]
[0.34087983 0.94986707 0.67263436]]
Z:
[[0.48051122 0.07141234 0.85264146]
[0.77086854 0.22082241 0.13154659]
[0.42401117 0.995431 0.4263775 ]]
Max:
[[0.48051122 0.16872478 0.85264146]
[0.83203 0.22082241 0.72786295]
[0.75988126 0.995431 0.67263436]]
```
</details>
)DOC")
.Input(
0,
"X, Y, ...",
"*(type: Tensor`<Ord>`)* List of input tensors with the same shape.")
.Output(
0,
"M",
"*(type: Tensor`<Ord>`)* Output tensor with same dimensions as input(s)."
"Contains the maximum valued element at each location.")
.InheritOnnxSchema();
OPERATOR_SCHEMA(Min)
.NumInputs(1, INT_MAX)
.NumOutputs(1)
.IdenticalTypeAndShapeOfInput(0)
.AllowInplace({{0, 0}})
.SetDoc(R"DOC(
Element-wise min of an arbitrary number of input tensors. This operation can be performed in-place, by using the first input blob as the output blob. All inputs must have the same shape and data type, and the output will have the same shape as the inputs.
Github Link:
- https://github.com/pytorch/pytorch/blob/master/caffe2/operators/minmax_ops.cc
<details>
<summary> <b>Example</b> </summary>
**Code**
```
workspace.ResetWorkspace()
op = core.CreateOperator(
"Min",
["X", "Y", "Z"],
["X"],
)
workspace.FeedBlob("X", (np.random.rand(2,2)).astype(np.float32))
workspace.FeedBlob("Y", (np.random.rand(2,2)).astype(np.float32))
workspace.FeedBlob("Z", (np.random.rand(2,2)).astype(np.float32))
print("X:", workspace.FetchBlob("X"))
print("Y:", workspace.FetchBlob("Y"))
print("Z:", workspace.FetchBlob("Z"))
workspace.RunOperatorOnce(op)
print("Min:", workspace.FetchBlob("X"))
```
**Result**
```
X:
[[0.32731926 0.4939747 ]
[0.29242373 0.43460014]]
Y:
[[0.40928316 0.916115 ]
[0.77526504 0.29339448]]
Z:
[[0.7899794 0.90335774]
[0.82599413 0.2843068 ]]
Min:
[[0.32731926 0.4939747 ]
[0.29242373 0.2843068 ]]
```
</details>
)DOC")
.Input(
0,
"X, Y, ...",
"*(type: Tensor`<Ord>`)* List of input tensors with the same shape.")
.Output(
0,
"M",
"*(type: Tensor`<Ord>`)* Output tensor with same dimensions as input(s)."
"Contains the minimum valued element at each location.")
.InheritOnnxSchema();
template <typename T, class Context>
bool MaxOp<T, Context>::Compute() {
auto& input0 = Input(0);
const int N = input0.numel();
T* output_data = Output(0)->template mutable_data<T>();
for (int i = 1; i < InputSize(); i++) {
auto input_data = Input(i).template data<T>();
EigenVectorMap<T> output_vec(output_data, N);
output_vec = output_vec.cwiseMax(ConstEigenVectorMap<T>(input_data, N));
}
return true;
}
template <typename T, class Context>
bool MinOp<T, Context>::Compute() {
auto& input0 = Input(0);
const int N = input0.numel();
T* output_data = Output(0)->template mutable_data<T>();
for (int i = 1; i < InputSize(); i++) {
auto input_data = Input(i).template data<T>();
EigenVectorMap<T> output_vec(output_data, N);
output_vec = output_vec.cwiseMin(ConstEigenVectorMap<T>(input_data, N));
}
return true;
}
} // namespace caffe2
|