1 #ifndef CAFFE2_OPERATORS_REDUCTION_OPS_H_ 2 #define CAFFE2_OPERATORS_REDUCTION_OPS_H_ 4 #include "caffe2/core/common_omp.h" 5 #include "caffe2/core/context.h" 6 #include "caffe2/core/logging.h" 7 #include "caffe2/core/operator.h" 8 #include "caffe2/utils/math.h" 12 template <
typename T,
class Context>
15 USE_OPERATOR_CONTEXT_FUNCTIONS;
19 average_(OperatorBase::GetSingleArgument<bool>(
"average",
false)) {}
24 bool RunOnDevice()
override 26 #if defined(__has_feature) 27 #if __has_feature(__address_sanitizer__) 28 __attribute__((__no_sanitize__(
"float-divide-by-zero")))
33 auto* sum = Output(0);
34 sum->Resize(vector<TIndex>());
35 T* data = sum->template mutable_data<T>();
36 math::Sum<T, Context>(
37 X.size(), X.template data<T>(), data, &context_, &scratch_);
39 math::Scale<T, Context>(
41 static_cast<T
>(1.) / X.size(),
42 sum->template data<T>(),
54 template <
typename T,
class Context>
57 USE_OPERATOR_CONTEXT_FUNCTIONS;
63 bool RunOnDevice()
override {
65 auto* sum = Output(0);
66 sum->Resize(vector<TIndex>());
67 T* data = sum->template mutable_data<T>();
68 math::Sum<T, Context>(
69 X.size(), X.template data<T>(), data, &context_, &scratch_);
77 template <
typename T,
class Context>
80 USE_OPERATOR_CONTEXT_FUNCTIONS;
84 average_(OperatorBase::GetSingleArgument<bool>(
"average",
false)) {}
86 const OperatorDef& operator_def,
92 bool RunOnDevice()
override;
98 template <
class Context>
102 USE_OPERATOR_CONTEXT_FUNCTIONS;
104 bool RunOnDevice()
override {
108 template <
typename T>
109 bool DoRunWithType() {
110 bool average = OperatorBase::GetSingleArgument<bool>(
"average",
false);
112 auto* sum = Output(0);
113 sum->Resize(vector<TIndex>());
114 math::SumSqr<T, Context>(
116 X.template data<T>(),
117 sum->template mutable_data<T>(),
121 math::Scale<T, Context>(
123 float(1.) / X.size(),
124 sum->template data<T>(),
125 sum->template mutable_data<T>(),
135 template <
typename T,
class Context,
bool ROWWISE>
139 USE_OPERATOR_CONTEXT_FUNCTIONS;
141 bool RunOnDevice()
override {
143 CAFFE_ENFORCE_EQ(X.ndim(), 3);
145 const int batch_size = X.dim32(0);
146 const int M = X.dim32(1);
147 const int N = X.dim32(2);
150 ROWWISE ? Y->Resize(batch_size, M) : Y->Resize(batch_size, N);
153 math::RowwiseMax<T, Context>(
156 X.template data<T>(),
157 Y->template mutable_data<T>(),
160 const int input_size = N * M;
161 for (
int i = 0; i < batch_size; ++i) {
162 math::ColwiseMax<T, Context>(
165 X.template data<T>() + i * input_size,
166 Y->template mutable_data<T>() + i * N,
174 template <
typename T,
class Context,
bool ROWWISE>
178 USE_OPERATOR_CONTEXT_FUNCTIONS;
180 bool RunOnDevice()
override;
Tensor is the basic class in Caffe2 that stores a contiguous memory with its shape information...
Workspace is a class that holds all the related objects created during runtime: (1) all blobs...
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...