3 #include "caffe2/ideep/ideep_utils.h" 4 #include "caffe2/proto/caffe2_legacy.pb.h" 10 USE_IDEEP_DEF_ALIASES();
11 USE_IDEEP_OPERATOR_FUNCTIONS();
16 static_cast<LegacyPadding>(OperatorBase::GetSingleArgument<int>(
18 LegacyPadding::NOTSET))),
19 kernel_(OperatorBase::GetRepeatedArgument<int>(
"kernels")),
20 stride_(OperatorBase::GetRepeatedArgument<int>(
"strides")),
21 pads_(OperatorBase::GetRepeatedArgument<int>(
"pads")),
22 adj_(OperatorBase::GetRepeatedArgument<int>(
"adjs")),
24 OperatorBase::GetSingleArgument<int>(
"shared_buffer", 0)) {
27 if (legacy_pad_ == LegacyPadding::VALID ||
28 legacy_pad_ == LegacyPadding::SAME) {
31 "If you use legacy padding VALID or SAME, you should not specify " 32 "any specific padding values.");
36 kernel_.resize(2, OperatorBase::GetSingleArgument<int>(
"kernel", 0));
40 kernel_.push_back(OperatorBase::GetSingleArgument<int>(
"kernel_h", 0));
41 kernel_.push_back(OperatorBase::GetSingleArgument<int>(
"kernel_w", 0));
45 stride_.resize(2, OperatorBase::GetSingleArgument<int>(
"stride", 0));
49 stride_.push_back(OperatorBase::GetSingleArgument<int>(
"stride_h", 0));
50 stride_.push_back(OperatorBase::GetSingleArgument<int>(
"stride_w", 0));
54 adj_.resize(2, OperatorBase::GetSingleArgument<int>(
"adj", 0));
58 adj_.push_back(OperatorBase::GetSingleArgument<int>(
"adj_h", 0));
59 adj_.push_back(OperatorBase::GetSingleArgument<int>(
"adj_w", 0));
64 legacy_pad_ != LegacyPadding::VALID &&
65 legacy_pad_ != LegacyPadding::SAME,
66 "If you use legacy padding VALID or SAME, you should not specify " 67 "any specific padding values.");
68 pads_.resize(4, OperatorBase::GetSingleArgument<int>(
"pad", 0));
75 legacy_pad_ != LegacyPadding::VALID &&
76 legacy_pad_ != LegacyPadding::SAME,
77 "If you use legacy padding VALID or SAME, you should not specify " 78 "any specific padding values.");
79 pads_.push_back(OperatorBase::GetSingleArgument<int>(
"pad_t", 0));
80 pads_.push_back(OperatorBase::GetSingleArgument<int>(
"pad_l", 0));
81 pads_.push_back(OperatorBase::GetSingleArgument<int>(
"pad_b", 0));
82 pads_.push_back(OperatorBase::GetSingleArgument<int>(
"pad_r", 0));
86 if (kernel_.empty()) {
87 kernel_.assign({0, 0});
90 if (stride_.empty()) {
91 stride_.assign(kernel_.size(), 1);
95 pads_.assign(kernel_.size() * 2, 0);
99 adj_.assign(kernel_.size(), 0);
102 CAFFE_ENFORCE_EQ(stride_.size(), kernel_.size());
103 CAFFE_ENFORCE_EQ(adj_.size(), kernel_.size());
105 if (legacy_pad_ != LegacyPadding::VALID &&
106 legacy_pad_ != LegacyPadding::SAME) {
107 CAFFE_ENFORCE_EQ(pads_.size(), 2 * kernel_.size());
110 for (
int dim = 0; dim < kernel_.size(); ++dim) {
111 CAFFE_ENFORCE_GT(kernel_[dim], 0);
112 CAFFE_ENFORCE_GT(stride_[dim], 0);
113 CAFFE_ENFORCE_GE(adj_[dim], 0);
114 CAFFE_ENFORCE_LE(adj_[dim], stride_[dim]);
119 const ideep::tensor& Input(
int index) {
120 return OperatorBase::template Input<ideep::tensor>(index);
122 ideep::tensor* Output(
int index) {
123 return OperatorBase::template Output<ideep::tensor>(index);
126 ideep::tensor::dims pad_tl()
const {
127 return {pad_t(), pad_l()};
130 ideep::tensor::dims pad_br()
const {
131 return {pad_b(), pad_r()};
134 ideep::tensor::dims CalcOutputDims(
135 const ideep::tensor& input,
136 int output_channel) {
137 CAFFE_ENFORCE_GT(input.get_size(), 0);
139 int N = input.get_dim(0);
140 ideep::tensor::dims output_dims;
141 auto input_dims = input.get_dims();
143 dims.assign(input_dims.begin() + 2, input_dims.end());
144 for (
int dim = 0; dim < dims.size(); ++dim) {
154 output_dims.push_back(dim_size);
157 output_dims.insert(output_dims.begin(), {N, output_channel});
161 bool RunOnDevice()
override {
163 return RunOnDeviceWithOrderNCHW();
164 }
catch (ideep::error& e) {
165 LOG(ERROR) <<
"IDEEP error:" << e.message;
170 virtual bool RunOnDeviceWithOrderNCHW() {
171 CAFFE_THROW(
"Not implemented");
175 LegacyPadding legacy_pad_;
187 inline int pad_t()
const {
191 inline int pad_l()
const {
195 inline int pad_b()
const {
199 inline int pad_r()
const {
203 inline int kernel_h()
const {
207 inline int kernel_w()
const {
211 inline int stride_h()
const {
215 inline int stride_w()
const {
219 inline int adj_h()
const {
223 inline int adj_w()
const {
227 inline void ComputeSizeAndPad(
235 switch (legacy_pad_) {
236 case LegacyPadding::NOTSET:
237 CAFFE_ENFORCE_GE(*pad_head, 0);
238 CAFFE_ENFORCE_GE(*pad_tail, 0);
240 (in_size - 1) * stride + kernel + adj - *pad_head - *pad_tail;
244 case LegacyPadding::VALID:
245 case LegacyPadding::SAME:
248 *out_size = (in_size - 1) * stride + kernel + adj;
250 case LegacyPadding::CAFFE_LEGACY_POOLING:
251 LOG(FATAL) <<
"CAFFE_LEGACY_POOLING is no longer supported.";
257 #define USE_IDEEP_CONV_TRANSPOSE_UNPOOL_BASE_FUNCTIONS() \ 258 USE_OPERATOR_BASE_FUNCTIONS; \ 259 using IDEEPConvTransposeUnpoolBase::Input; \ 260 using IDEEPConvTransposeUnpoolBase::Output;
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 ...
bool HasArgument(const string &name) const
Checks if the operator has an argument of the given name.