1 #ifndef CAFFE2_OPERATORS_AFFINE_CHANNEL_OP_H_ 2 #define CAFFE2_OPERATORS_AFFINE_CHANNEL_OP_H_ 6 #include "caffe2/core/context.h" 7 #include "caffe2/core/logging.h" 8 #include "caffe2/core/operator.h" 9 #include "caffe2/utils/math.h" 13 template <
typename T,
class Context>
16 USE_OPERATOR_CONTEXT_FUNCTIONS;
18 template <
class... Args>
21 order_(StringToStorageOrder(
22 this->
template GetSingleArgument<std::string>(
"order",
"NCHW"))),
23 OP_SINGLE_ARG(
bool,
"is_learnable", is_learnable_,
false) {
24 CAFFE_ENFORCE_NE(order_, StorageOrder::UNKNOWN);
27 bool RunOnDevice()
override {
28 return order_ == StorageOrder::NCHW ? RunOnDeviceWithOrderNCHW()
29 : RunOnDeviceWithOrderNHWC();
32 bool RunOnDeviceWithOrderNCHW() {
33 const auto& X =
Input(0);
34 const auto& scale =
Input(1);
35 const auto& bias =
Input(2);
39 !IsInputOutputAlias(0, 0),
40 "In-place affine_channel_op is not supported when " 41 "is_learnable = true.");
43 const int N = X.dim32(0);
44 const int C = X.dim32(1);
45 const int HxW = X.numel() / (N * C);
46 auto* Y = Output(0, X.sizes(), at::dtype<T>());
47 math::AffineChannel<T, Context, StorageOrder::NCHW>(
52 scale.template data<T>(),
53 bias.template data<T>(),
54 Y->template mutable_data<T>(),
59 bool RunOnDeviceWithOrderNHWC() {
60 const auto& X =
Input(0);
61 const auto& scale =
Input(1);
62 const auto& bias =
Input(2);
66 !IsInputOutputAlias(0, 0),
67 "In-place affine_channel_op is not supported when " 68 "is_learnable = true.");
70 const int ndim = X.dim();
71 const int N = X.dim32(0);
72 const int C = X.dim32(ndim - 1);
73 const int HxW = X.numel() / (N * C);
75 Output(0, X.sizes(), at::dtype<T>());
76 math::AffineChannel<T, Context, StorageOrder::NHWC>(
81 scale.template data<T>(),
82 bias.template data<T>(),
83 Y->template mutable_data<T>(),
89 const StorageOrder order_;
90 const bool is_learnable_;
93 template <
typename T,
class Context>
96 USE_OPERATOR_CONTEXT_FUNCTIONS;
98 template <
class... Args>
101 order_(StringToStorageOrder(
102 this->
template GetSingleArgument<std::string>(
"order",
"NCHW"))),
103 OP_SINGLE_ARG(
bool,
"is_learnable", is_learnable_,
false) {
104 CAFFE_ENFORCE_NE(order_, StorageOrder::UNKNOWN);
107 bool RunOnDevice()
override {
108 return order_ == StorageOrder::NCHW ? RunOnDeviceWithOrderNCHW()
109 : RunOnDeviceWithOrderNHWC();
112 bool RunOnDeviceWithOrderNCHW();
114 bool RunOnDeviceWithOrderNHWC();
117 const StorageOrder order_;
118 const bool is_learnable_;
123 #endif // CAFFE2_OPERATORS_AFFINE_CHANNEL_OP_H_
const Tensor & Input(int idx, DeviceType type=Context::GetDeviceType())
Retrieve a non-owning reference to the input at position 'idx' for this operator. ...
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...