/usr/local/lib64/python3.6/site-packages/torch/include/caffe2/operators
NameSizeModeActions
abs_op.h7050644editdlrm
accumulate_op.h10730644editdlrm
accuracy_op.h6520644editdlrm
acos_op.h7110644editdlrm
activation_ops_cudnn.h41220644editdlrm
affine_channel_op.h34500644editdlrm
alias_with_name.h12340644editdlrm
apmeter_op.h10270644editdlrm
arg_ops.h23190644editdlrm
asin_op.h7110644editdlrm
assert_op.h13350644editdlrm
async_net_barrier_op.h9040644editdlrm
atan_op.h7110644editdlrm
batch_box_cox_op.h22870644editdlrm
batch_bucketize_op.h7200644editdlrm
batch_gather_ops.h52640644editdlrm
batch_matmul_op.h96020644editdlrm
batch_moments_op.h33640644editdlrm
batch_permutation_op.h9540644editdlrm
batch_sparse_to_dense_op.h61470644editdlrm
bbox_transform_op.h26680644editdlrm
bisect_percentile_op.h49210644editdlrm
boolean_mask_ops.h26650644editdlrm
boolean_unmask_ops.h3780644editdlrm
box_with_nms_limit_op.h49600644editdlrm
bucketize_op.h13610644editdlrm
byte_weight_dequant_op.h17220644editdlrm
cast_op.h13930644editdlrm
cbrt_op.h7230644editdlrm
cc_bmm_bg_op.h38940644editdlrm
ceil_op.h7820644editdlrm
channel_backprop_stats_op.h7370644editdlrm
channel_shuffle_op.h19020644editdlrm
channel_stats_op.h18070644editdlrm
clip_op.h16390644editdlrm
collect_and_distribute_fpn_rpn_proposals_op.h68750644editdlrm
concat_split_op.h118500644editdlrm
conditional_op.h4870644editdlrm
conv_op.h31250644editdlrm
conv_op_cache_cudnn.h19350644editdlrm
conv_op_impl.h287290644editdlrm
conv_op_shared.h6720644editdlrm
conv_pool_op_base.h321090644editdlrm
conv_transpose_op.h17270644editdlrm
conv_transpose_op_impl.h182640644editdlrm
conv_transpose_op_mobile.h14700644editdlrm
conv_transpose_op_mobile_impl.h195870644editdlrm
conv_transpose_unpool_op_base.h103030644editdlrm
copy_op.h12960644editdlrm
copy_rows_to_tensor_op.h25990644editdlrm
cosh_op.h7110644editdlrm
cosine_embedding_criterion_op.h11270644editdlrm
cos_op.h7050644editdlrm
counter_ops.h45960644editdlrm
create_scope_op.h52320644editdlrm
cross_entropy_op.h44200644editdlrm
ctc_beam_search_decoder_op.h11020644editdlrm
ctc_greedy_decoder_op.h8170644editdlrm
cube_op.h7230644editdlrm
dataset_ops.h55010644editdlrm
data_couple.h4640644editdlrm
deform_conv_op.h35430644editdlrm
deform_conv_op_impl.h131710644editdlrm
dense_vector_to_id_list_op.h17970644editdlrm
distance_op.h84190644editdlrm
do_op.h69810644editdlrm
dropout_op.h15160644editdlrm
elementwise_add_op.h20240644editdlrm
elementwise_div_op.h12240644editdlrm
elementwise_linear_op.h11700644editdlrm
elementwise_logical_ops.h50830644editdlrm
elementwise_mul_op.h12240644editdlrm
elementwise_ops.h191150644editdlrm
elementwise_ops_utils.h10080644editdlrm
elementwise_op_test.h92370644editdlrm
elementwise_sub_op.h20250644editdlrm
elu_op.h8750644editdlrm
enforce_finite_op.h23030644editdlrm
ensure_clipped_op.h16080644editdlrm
ensure_cpu_output_op.h14650644editdlrm
erf_op.h7510644editdlrm
expand_op.h38770644editdlrm
expand_squeeze_dims_op.h34510644editdlrm
exp_op.h4250644editdlrm
fc_inference.h7750644editdlrm
feature_maps_ops.h324370644editdlrm
feed_blob_op.h8020644editdlrm
filler_op.h184310644editdlrm
find_duplicate_elements_op.h15630644editdlrm
find_op.h20550644editdlrm
flatten_op.h15250644editdlrm
flexible_top_k.h9360644editdlrm
floor_op.h7880644editdlrm
free_op.h7770644editdlrm
fully_connected_op.h93510644editdlrm
fused_rowwise_8bit_conversion_ops.h66010644editdlrm
fused_rowwise_nbitfake_conversion_ops.h43750644editdlrm
fused_rowwise_nbit_conversion_ops.h87230644editdlrm
fused_rowwise_random_quantization_ops.h26070644editdlrm
gather_fused_8bit_rowwise_op.h21790644editdlrm
gather_op.h75050644editdlrm
gather_ranges_to_dense_op.h81880644editdlrm
gelu_op.h14520644editdlrm
generate_proposals_op.h62560644editdlrm
generate_proposals_op_util_boxes.h143090644editdlrm
generate_proposals_op_util_nms.h262140644editdlrm
generate_proposals_op_util_nms_gpu.h21280644editdlrm
given_tensor_byte_string_to_uint8_fill_op.h21500644editdlrm
given_tensor_fill_op.h30020644editdlrm
glu_op.h14580644editdlrm
group_norm_op.h89670644editdlrm
gru_unit_op.h66260644editdlrm
half_float_ops.h27320644editdlrm
hard_sigmoid_op.h9940644editdlrm
heatmap_max_keypoint_op.h9390644editdlrm
histogram_op.h24210644editdlrm
h_softmax_op.h49540644editdlrm
if_op.h17640644editdlrm
im2col_op.h89430644editdlrm
index_hash_ops.h22320644editdlrm
index_ops.h31550644editdlrm
inference_lstm_op.h98810644editdlrm
instance_norm_op.h74410644editdlrm
integral_image_op.h9230644editdlrm
is_empty_op.h5580644editdlrm
jsd_op.h7210644editdlrm
key_split_ops.h14000644editdlrm
layer_norm_op.h80980644editdlrm
leaky_relu_op.h11110644editdlrm
lengths_pad_op.h25740644editdlrm
lengths_reducer_fused_8bit_rowwise_ops.h55320644editdlrm
lengths_reducer_fused_nbit_rowwise_ops.h234650644editdlrm
lengths_reducer_ops.h233150644editdlrm
lengths_reducer_rowwise_8bit_ops.h61800644editdlrm
lengths_tile_op.h5820644editdlrm
lengths_top_k_op.h13580644editdlrm
length_split_op.h22590644editdlrm
listwise_l2r_op.h16770644editdlrm
load_save_op.h140910644editdlrm
load_save_op_util.h16420644editdlrm
locally_connected_op.h38720644editdlrm
locally_connected_op_impl.h264950644editdlrm
locally_connected_op_util.h13320644editdlrm
local_response_normalization_op.h28040644editdlrm
log1p_op.h7170644editdlrm
logit_op.h11290644editdlrm
log_op.h4310644editdlrm
loss_op.h10580644editdlrm
lpnorm_op.h12790644editdlrm
lstm_unit_op.h67330644editdlrm
lstm_utils.h94240644editdlrm
map_ops.h80110644editdlrm
margin_ranking_criterion_op.h11130644editdlrm
matmul_op.h28430644editdlrm
max_pool_with_index_gpu.h11550644editdlrm
mean_op.h32520644editdlrm
merge_id_lists_op.h25700644editdlrm
minmax_ops.h38290644editdlrm
mish_op.h7940644editdlrm
mod_op.h9840644editdlrm
moments_op.h40510644editdlrm
multi_class_accuracy_op.h5390644editdlrm
negate_gradient_op.h5660644editdlrm
negative_op.h4510644editdlrm
ngram_ops.h26440644editdlrm
normalize_l1_op.h10750644editdlrm
normalize_op.h30130644editdlrm
no_default_engine_op.h10630644editdlrm
numpy_tile_op.h36430644editdlrm
one_hot_ops.h25620644editdlrm
onnx_while_op.h106550644editdlrm
operator_fallback_gpu.h41550644editdlrm
op_utils_cudnn.h21120644editdlrm
order_switch_ops.h21490644editdlrm
pack_rnn_sequence_op.h30740644editdlrm
pack_segments.h27290644editdlrm
pad_op.h29020644editdlrm
partition_ops.h99580644editdlrm
percentile_op.h10090644editdlrm
perplexity_op.h4470644editdlrm
piecewise_linear_transform_op.h82810644editdlrm
pool_op.h85250644editdlrm
pool_op_util.h11050644editdlrm
pow_op.h46770644editdlrm
prefetch_op.h46610644editdlrm
prelu_op.h10670644editdlrm
prepend_dim_op.h27600644editdlrm
quantile_op.h41200644editdlrm
quant_decode_op.h53700644editdlrm
rank_loss_op.h8200644editdlrm
reciprocal_op.h7210644editdlrm
reducer_functors.h245560644editdlrm
reduce_front_back_max_ops.h43990644editdlrm
reduce_front_back_sum_mean_ops.h53370644editdlrm
reduce_ops.h99620644editdlrm
reduction_ops.h59440644editdlrm
relu_n_op.h9900644editdlrm
relu_op.h6240644editdlrm
remove_data_blocks_op.h26510644editdlrm
replace_nan_op.h11700644editdlrm
reshape_op.h57230644editdlrm
resize_3d_op.h26770644editdlrm
resize_op.h23070644editdlrm
reverse_packed_segs_op.h27720644editdlrm
rmac_regions_op.h7080644editdlrm
rms_norm_op.h29680644editdlrm
roi_align_gradient_op.h14860644editdlrm
roi_align_op.h28570644editdlrm
roi_align_rotated_gradient_op.h13690644editdlrm
roi_align_rotated_op.h16360644editdlrm
roi_pool_op.h25030644editdlrm
rowmul_op.h19470644editdlrm
rsqrt_op.h7290644editdlrm
scale_blobs_op.h14580644editdlrm
scale_op.h10190644editdlrm
segment_reduction_op.h710220644editdlrm
self_binning_histogram_op.h62580644editdlrm
selu_op.h15450644editdlrm
sequence_ops.h82640644editdlrm
shape_op.h16380644editdlrm
sigmoid_op.h6390644editdlrm
sinh_op.h7110644editdlrm
sinusoid_position_encoding_op.h28340644editdlrm
sin_op.h7050644editdlrm
slice_op.h100710644editdlrm
softmax_op.h11740644editdlrm
softmax_utils.h4470644editdlrm
softmax_with_loss_op.h28830644editdlrm
softplus_op.h7810644editdlrm
softsign_op.h6750644editdlrm
space_batch_op.h68480644editdlrm
sparse_dropout_with_replacement_op.h11220644editdlrm
sparse_itemwise_dropout_with_replacement_op.h11630644editdlrm
sparse_lp_regularizer_op.h11300644editdlrm
sparse_normalize_op.h8340644editdlrm
sparse_to_dense_mask_op.h100510644editdlrm
sparse_to_dense_op.h39770644editdlrm
spatial_batch_norm_op.h151750644editdlrm
spatial_softmax_with_loss_op.h21820644editdlrm
sqrt_op.h4480644editdlrm
sqr_op.h4310644editdlrm
square_root_divide_op.h18570644editdlrm
stats_put_ops.h28130644editdlrm
stop_gradient.h5480644editdlrm
string_ops.h20670644editdlrm
stump_func_op.h21120644editdlrm
summarize_op.h18750644editdlrm
swish_op.h7720644editdlrm
tanh_op.h7230644editdlrm
tan_op.h7050644editdlrm
tensor_protos_db_input.h36330644editdlrm
text_file_reader_utils.h29000644editdlrm
thresholded_relu_op.h11370644editdlrm
tile_op.h87410644editdlrm
top_k.h10610644editdlrm
transpose_op.h20820644editdlrm
tt_linear_op.h65010644editdlrm
unique_ops.h16660644editdlrm
unsafe_coalesce.h24810644editdlrm
upsample_op.h22460644editdlrm
utility_ops.h499940644editdlrm
variable_length_sequence_padding.h13780644editdlrm
weighted_multi_sampling_op.h6020644editdlrm
weighted_sample_op.h7390644editdlrm
while_op.h19610644editdlrm
zero_gradient_op.h3470644editdlrm
Edit: /usr/local/lib64/python3.6/site-packages/torch/include/caffe2/operators/conv_pool_op_base.h (32109B)
#ifndef CAFFE2_OPERATORS_CONV_POOL_OP_BASE_H_ #define CAFFE2_OPERATORS_CONV_POOL_OP_BASE_H_ #include #include #include "caffe2/core/context.h" #include "caffe2/core/logging.h" #include "caffe2/core/operator.h" #include "caffe2/core/types.h" #include "caffe2/proto/caffe2_legacy.pb.h" #include "caffe2/utils/math.h" // This macro is here just to allow us to experiment with padding values that // determines, when we have an odd number of pads, which side gets the one // additional pad value, the head side, or the tail side. Setting it to false // will enable the TensorFlow behavior, and setting it to true will enable // a behavior more consistent with Caffe and CuDNN. // This only affects the case when you set legacy pad to VALID or SAME. The // behavior inherits from the early designs of Google's CNN implementation, // where padding values are implicitly calculated instead of explicitly // specified. This is still the case with TensorFlow. Many frameworks have // followed a slightly different approach of explicitly giving padding values, // in which case the value of this constant value does not matter. const bool CAFFE2_PAD_HEAD_MORE = false; namespace caffe2 { template class ConvPoolOpBase : public Operator { public: USE_OPERATOR_CONTEXT_FUNCTIONS; explicit ConvPoolOpBase(const OperatorDef& operator_def, Workspace* ws) : Operator(operator_def, ws), legacy_pad_( static_cast(this->template GetSingleArgument( "legacy_pad", LegacyPadding::NOTSET))), global_pooling_( this->template GetSingleArgument("global_pooling", 0)), kernel_(this->template GetRepeatedArgument("kernels")), dilation_(this->template GetRepeatedArgument("dilations")), stride_(this->template GetRepeatedArgument("strides")), pads_(this->template GetRepeatedArgument("pads")), float16_compute_( this->template GetSingleArgument("float16_compute", false)), group_(this->template GetSingleArgument("group", 1)), order_(StringToStorageOrder( this->template GetSingleArgument("order", "NCHW"))), shared_buffer_( this->template GetSingleArgument("shared_buffer", 0)), ws_(ws) { // For the padding, they should either be the legacy padding strategy // (VALID or SAME), or an explicit, non-negative value. if (legacy_pad_ == LegacyPadding::VALID || legacy_pad_ == LegacyPadding::SAME) { CAFFE_ENFORCE( !OperatorBase::HasArgument("pads"), "If you use legacy padding VALID or SAME, you should not specify " "any specific padding values."); } // Get old arguments values. if (OperatorBase::HasArgument("kernel")) { kernel_.resize(2, this->template GetSingleArgument("kernel", 0)); } else if ( OperatorBase::HasArgument("kernel_h") && OperatorBase::HasArgument("kernel_w")) { kernel_.push_back(this->template GetSingleArgument("kernel_h", 0)); kernel_.push_back(this->template GetSingleArgument("kernel_w", 0)); } if (OperatorBase::HasArgument("stride")) { stride_.resize(2, this->template GetSingleArgument("stride", 0)); } else if ( OperatorBase::HasArgument("stride_h") && OperatorBase::HasArgument("stride_w")) { stride_.push_back(this->template GetSingleArgument("stride_h", 0)); stride_.push_back(this->template GetSingleArgument("stride_w", 0)); } if (OperatorBase::HasArgument("dilation")) { dilation_.resize(2, this->template GetSingleArgument("dilation", 0)); } else if ( OperatorBase::HasArgument("dilation_h") && OperatorBase::HasArgument("dilation_w")) { dilation_.push_back( this->template GetSingleArgument("dilation_h", 0)); dilation_.push_back( this->template GetSingleArgument("dilation_w", 0)); } if (OperatorBase::HasArgument("pad")) { CAFFE_ENFORCE( legacy_pad_ != LegacyPadding::VALID && legacy_pad_ != LegacyPadding::SAME, "If you use legacy padding VALID or SAME, you should not specify " "any specific padding values."); pads_.resize(4, this->template GetSingleArgument("pad", 0)); } else if ( OperatorBase::HasArgument("pad_t") && OperatorBase::HasArgument("pad_l") && OperatorBase::HasArgument("pad_b") && OperatorBase::HasArgument("pad_r")) { CAFFE_ENFORCE( legacy_pad_ != LegacyPadding::VALID && legacy_pad_ != LegacyPadding::SAME, "If you use legacy padding VALID or SAME, you should not specify " "any specific padding values."); pads_.push_back(this->template GetSingleArgument("pad_t", 0)); pads_.push_back(this->template GetSingleArgument("pad_l", 0)); pads_.push_back(this->template GetSingleArgument("pad_b", 0)); pads_.push_back(this->template GetSingleArgument("pad_r", 0)); } // Fill default values. if (kernel_.size() == 0) { kernel_.assign({0, 0}); } if (stride_.size() == 0) { stride_.resize(kernel_.size(), 1); } if (pads_.size() == 0) { pads_.resize(kernel_.size() * 2, 0); } if (dilation_.size() == 0) { dilation_.resize(kernel_.size(), 1); } CAFFE_ENFORCE_EQ(stride_.size(), kernel_.size()); CAFFE_ENFORCE_EQ(dilation_.size(), kernel_.size()); if (legacy_pad_ != LegacyPadding::VALID && legacy_pad_ != LegacyPadding::SAME) { CAFFE_ENFORCE_EQ(pads_.size(), 2 * kernel_.size()); } if (global_pooling_) { for (size_t dim = 0; dim < kernel_.size(); ++dim) { CAFFE_ENFORCE( pads_[2 * dim] == 0 && pads_[2 * dim + 1] == 0 && dilation_[dim] == 1 && stride_[dim] == 1, "If global_pooling is set pad, dilation and stride shouldn't be set."); } } // Check kernel only if we are doing conv or pooling. The reason is that a // few other ops, like PadImage, are also using this base class. We really // need to clean this up. if (operator_def.name().find("Conv") == 0 || operator_def.name().find("Pool") != std::string::npos) { for (size_t dim = 0; dim < kernel_.size(); ++dim) { CAFFE_ENFORCE_GE(pads_[dim], 0); CAFFE_ENFORCE_GE(pads_[kernel_.size() + dim], 0); CAFFE_ENFORCE( kernel_[dim], "If you are doing convolution or pooling, you will need to set " "explicitly the kernel size."); } } for (size_t dim = 0; dim < kernel_.size(); ++dim) { CAFFE_ENFORCE_GE(kernel_[dim], 0); CAFFE_ENFORCE_GE(dilation_[dim], 0); CAFFE_ENFORCE_GE(stride_[dim], 0); } } // Returns the input image dimensions for the current storage order type. vector GetDims(const Tensor& input) { vector dims; switch (order_) { case StorageOrder::NCHW: dims.assign(input.sizes().begin() + 2, input.sizes().end()); break; case StorageOrder::NHWC: dims.assign(input.sizes().begin() + 1, input.sizes().end() - 1); break; default: CAFFE_THROW("Unknown storage order : ", order_); } return dims; } // Returns the size of the input image for the current storage type. int GetDimsSize(const Tensor& input) { int size = 0; switch (order_) { case StorageOrder::NCHW: size = std::accumulate( input.sizes().begin() + 2, input.sizes().end(), 1, std::multiplies()); break; case StorageOrder::NHWC: size = std::accumulate( input.sizes().begin() + 1, input.sizes().end() - 1, 1, std::multiplies()); break; default: CAFFE_THROW("Unknown storage order : ", order_); } return size; } // Gets the output size. The output channel is manually provided since // it may not be identical to the input channels. // This function can be used in the forward functions to obtain the output // sizes. // Note(jiayq): the templatization of this function is mainly to help // implementations that do not use first-class Tensor objects, such as the // MKL operator. One can still call this function with dummy // Tensor objects in order to obtain the sizes. std::vector GetOutputSize(const Tensor& input, int output_channel) { CAFFE_ENFORCE_GE(input.dim(), 2); const int inner_size = input.size_from_dim(1); CAFFE_ENFORCE_GT(inner_size, 0); std::vector output_dims; InferOutputSize64( input.sizes(), output_channel, order_, global_pooling_, legacy_pad_, dilation_, stride_, &kernel_, &pads_, &output_dims); return output_dims; } void SetOutputSize(const Tensor& input, Tensor* output, int output_channel) { const int inner_size = input.size_from_dim(1); CAFFE_ENFORCE_GT(inner_size, 0); std::vector output_dims; InferOutputSize( input.sizes(), output_channel, order_, global_pooling_, legacy_pad_, dilation_, stride_, &kernel_, &pads_, &output_dims); output->Resize(output_dims); } // Helper function that is also called from OperatorSchema. Modified // kernel parameters and output output_dims and channel_first. static void InferOutputSize( const at::IntArrayRef& input_dims, const int output_channel, const StorageOrder order, const bool global_pooling, const LegacyPadding legacy_pad, const std::vector& dilation, const std::vector& stride, std::vector* kernel, std::vector* pads, std::vector* output_dims) { CAFFE_ENFORCE_NE(order, StorageOrder::UNKNOWN); const int ndim = input_dims.size() - 2; output_dims->resize(ndim + 2); output_dims->front() = input_dims.front(); if (order == StorageOrder::NCHW) { output_dims->at(1) = output_channel; } else { output_dims->back() = output_channel; } const int offset = order == StorageOrder::NCHW ? 2 : 1; if (global_pooling) { std::copy_n(input_dims.cbegin() + offset, ndim, kernel->begin()); std::fill_n(output_dims->begin() + offset, ndim, 1LL); } else { for (int i = 0; i < ndim; ++i) { ComputeSizeAndPad( input_dims[i + offset], stride[i], kernel->at(i), dilation[i], legacy_pad, &pads->at(i), &pads->at(i + ndim), &output_dims->at(i + offset)); } } } static void InferOutputSize64( const at::IntArrayRef& input_dims, const int output_channel, const StorageOrder order, const bool global_pooling, const LegacyPadding legacy_pad, const std::vector& dilation, const std::vector& stride, std::vector* kernel, std::vector* pads, std::vector* output_dims) { CAFFE_ENFORCE_NE(order, StorageOrder::UNKNOWN); const int ndim = input_dims.size() - 2; output_dims->resize(ndim + 2); output_dims->front() = input_dims.front(); if (order == StorageOrder::NCHW) { output_dims->at(1) = output_channel; } else { output_dims->back() = output_channel; } const int offset = order == StorageOrder::NCHW ? 2 : 1; if (global_pooling) { std::copy_n(input_dims.cbegin() + offset, ndim, kernel->begin()); std::fill_n(output_dims->begin() + offset, ndim, 1LL); } else { for (int i = 0; i < ndim; ++i) { ComputeSizeAndPad64( input_dims[i + offset], stride[i], kernel->at(i), dilation[i], legacy_pad, &pads->at(i), &pads->at(i + ndim), &output_dims->at(i + offset)); } } } // ComputePads could be used in backward functions to figure out the padding // values for the given input. void ComputePads(const vector& dims) { if (global_pooling_) { kernel_ = dims; } else if (legacy_pad_ != LegacyPadding::NOTSET) { int output_unused; // NOLINTNEXTLINE(clang-diagnostic-sign-compare) for (int dim = 0; dim < dims.size(); ++dim) { ComputeSizeAndPad( dims[dim], stride_[dim], kernel_[dim], dilation_[dim], legacy_pad_, &pads_[dim], &pads_[dims.size() + dim], &output_unused); } } } bool HasPad() const { if (kernel_.size() == 2) { return pad_t() > 0 || pad_b() > 0 || pad_l() > 0 || pad_r() > 0; } return std::any_of( pads_.cbegin(), pads_.cend(), [](const int x) { return x > 0; }); } bool HasStride() const { if (kernel_.size() == 2) { return stride_h() > 1 || stride_w() > 1; } return std::any_of( stride_.cbegin(), stride_.cend(), [](const int x) { return x > 1; }); } void SetDeviceTensor(const std::vector& data, Tensor* tensor) { bool reset_tensor_device_ = false; // NOLINTNEXTLINE(clang-diagnostic-sign-compare) if (tensor->numel() != data.size()) { tensor->Resize(data.size()); reset_tensor_device_ = true; } else { const int* tensor_data = tensor->template data(); for (int d_i = 0; d_i < data.size(); ++d_i) { if (tensor_data[d_i] != data[d_i]) { reset_tensor_device_ = true; break; } } } if (reset_tensor_device_) { context_.template Copy( data.size(), data.data(), tensor->template mutable_data()); } } template void SetBiasMultiplier(const int size, Tensor* bias_multiplier_) { if (bias_multiplier_->numel() != size) { // If the helper bias multiplier is not image size, reshape and fill it // with one. bias_multiplier_->Resize(std::vector{size}); math::Set( size, static_cast(1), bias_multiplier_->template mutable_data(), &context_); } } bool RunOnDevice() override { if (!global_pooling_) { for (size_t dim = 0; dim < kernel_.size(); ++dim) { CAFFE_ENFORCE_GT(kernel_[dim], 0); } } switch (order_) { case StorageOrder::NHWC: // VLOG(2) << "Running NHWC"; return RunOnDeviceWithOrderNHWC(); case StorageOrder::NCHW: // VLOG(2) << "Running NCHW"; return RunOnDeviceWithOrderNCHW(); default: CAFFE_THROW("Unknown Storage order: ", order_); } } // The actual function that does the computation, if the different // storage order leads to different implementations. virtual bool RunOnDeviceWithOrderNHWC() { CAFFE_NOT_IMPLEMENTED; } virtual bool RunOnDeviceWithOrderNCHW() { CAFFE_NOT_IMPLEMENTED; } static struct OpSchema::Cost CostInferenceForConv( const OperatorDef& def, const vector& inputs) { CAFFE_ENFORCE_GE(inputs.size(), 2, "Conv requires at least 2 inputs"); struct OpSchema::Cost c; const TensorShape X = inputs[0]; const TensorShape W = inputs[1]; const TensorShape Y = TensorInferenceForConv(def, inputs)[0]; ArgumentHelper helper(def); const auto order = StringToStorageOrder(helper.GetSingleArgument("order", "NCHW")); uint64_t N; uint64_t Y_h; uint64_t Y_w = 1; uint64_t Y_t = 1; uint64_t kernel_h; uint64_t kernel_w = 1; uint64_t kernel_t = 1; uint64_t in_channels; uint64_t out_channels; if (X.dims_size() == 0 || W.dims_size() == 0) { return c; } N = X.dims(0); if (X.dims_size() == 5) { // 3D convolution if (order == StorageOrder::NHWC) { Y_t = Y.dims(1); Y_h = Y.dims(2); Y_w = Y.dims(3); kernel_t = W.dims(1); kernel_h = W.dims(2); kernel_w = W.dims(3); in_channels = W.dims(4); out_channels = W.dims(0); } else { Y_t = Y.dims(2); Y_h = Y.dims(3); Y_w = Y.dims(4); kernel_t = W.dims(2); kernel_h = W.dims(3); kernel_w = W.dims(4); in_channels = W.dims(1); out_channels = W.dims(0); } } else if (X.dims_size() == 4) { // 2D convolution CAFFE_ENFORCE_EQ(W.dims_size(), 4, "Conv2D should have 4D filter tensor"); if (order == StorageOrder::NHWC) { Y_h = Y.dims(1); Y_w = Y.dims(2); kernel_h = W.dims(1); kernel_w = W.dims(2); in_channels = W.dims(3); out_channels = W.dims(0); } else { Y_h = Y.dims(2); Y_w = Y.dims(3); kernel_h = W.dims(2); kernel_w = W.dims(3); in_channels = W.dims(1); out_channels = W.dims(0); } } else { // 1D convolution CAFFE_ENFORCE_EQ(W.dims_size(), 3, "Conv1D should have 3D filter tensor"); if (order == StorageOrder::NHWC) { Y_h = Y.dims(1); kernel_h = W.dims(1); in_channels = W.dims(2); out_channels = W.dims(0); } else { Y_h = Y.dims(2); kernel_h = W.dims(2); in_channels = W.dims(1); out_channels = W.dims(0); } } uint64_t nElemX = nElemFromDim(X); uint64_t nElemW = nElemFromDim(W); uint64_t nElemBias = inputs.size() > 2 ? nElemFromDim(inputs[2]) : 0; auto const& X_elemenet_size_byte = DataTypeToTypeMeta(X.data_type()).itemsize(); auto const& Y_element_size_byte = DataTypeToTypeMeta(Y.data_type()).itemsize(); auto const& W_element_size_byte = DataTypeToTypeMeta(W.data_type()).itemsize(); // grouping is NOT properly handled yet c.flops = N * Y_t * Y_h * Y_w * kernel_t * kernel_w * kernel_h * in_channels * out_channels * 2; c.bytes_read = (nElemX + nElemW + nElemBias) * X_elemenet_size_byte; c.bytes_written = N * out_channels * Y_t * Y_h * Y_w * Y_element_size_byte; c.params_bytes = out_channels * in_channels * kernel_t * kernel_h * kernel_w * W_element_size_byte; return c; } static vector TensorInferenceForSchema( const OperatorDef& def, const vector& in, int output_channel) { ArgumentHelper helper(def); CAFFE_ENFORCE_GT(in.size(), 0U); CAFFE_ENFORCE_GT(in[0].dims_size(), 0); vector pads = helper.GetRepeatedArgument("pads"); vector kernel = helper.GetRepeatedArgument("kernels"); vector strides = helper.GetRepeatedArgument("strides"); vector dilations = helper.GetRepeatedArgument("dilation"); if (helper.HasArgument("pad")) { pads.resize(4, helper.GetSingleArgument("pad", 0)); } else if ( helper.HasArgument("pad_t") && helper.HasArgument("pad_l") && helper.HasArgument("pad_b") && helper.HasArgument("pad_r")) { pads.push_back(helper.GetSingleArgument("pad_t", 0)); pads.push_back(helper.GetSingleArgument("pad_l", 0)); pads.push_back(helper.GetSingleArgument("pad_b", 0)); pads.push_back(helper.GetSingleArgument("pad_r", 0)); } if (helper.HasArgument("kernel")) { kernel.resize(2, helper.GetSingleArgument("kernel", 1)); } else if ( helper.HasArgument("kernel_h") && helper.HasArgument("kernel_w")) { kernel.push_back(helper.GetSingleArgument("kernel_h", 1)); kernel.push_back(helper.GetSingleArgument("kernel_w", 1)); } if (helper.HasArgument("stride")) { strides.resize(2, helper.GetSingleArgument("stride", 1)); } else if ( helper.HasArgument("stride_h") && helper.HasArgument("stride_w")) { strides.push_back(helper.GetSingleArgument("stride_h", 1)); strides.push_back(helper.GetSingleArgument("stride_w", 1)); } if (helper.HasArgument("dilation")) { strides.resize(2, helper.GetSingleArgument("dilation", 1)); } else if ( helper.HasArgument("dilation_h") && helper.HasArgument("dilation_w")) { strides.push_back(helper.GetSingleArgument("dilation_h", 1)); strides.push_back(helper.GetSingleArgument("dilation_w", 1)); } auto check_and_set_default_value = [](vector& vec, int size, int value) { if (vec.size() == 0) { vec.resize(size, value); } }; check_and_set_default_value(kernel, 2, 1); check_and_set_default_value(strides, kernel.size(), 1); check_and_set_default_value(pads, kernel.size() * 2, 0); check_and_set_default_value(dilations, kernel.size(), 1); std::vector output_dims; ConvPoolOpBase::InferOutputSize( GetDimsVector(in[0]), output_channel, StringToStorageOrder(helper.GetSingleArgument("order", "NCHW")), helper.GetSingleArgument("global_pooling", 0), static_cast( helper.GetSingleArgument("legacy_pad", LegacyPadding::NOTSET)), dilations, strides, &kernel, &pads, &output_dims); return {CreateTensorShape(output_dims, TensorProto::FLOAT)}; } static std::vector TensorInferenceForConv( const OperatorDef& def, const std::vector& in) { if (in[0].unknown_shape()) { std::vector out(1); out[0].set_unknown_shape(true); return out; } return TensorInferenceForSchema(def, in, in[1].dims(0)); } static std::vector TensorInferenceForPool( const OperatorDef& def, const std::vector& in) { if (in[0].unknown_shape()) { std::vector out(1); out[0].set_unknown_shape(true); return out; } ArgumentHelper helper(def); auto order = StringToStorageOrder(helper.GetSingleArgument("order", "NCHW")); int num_channels = (order == StorageOrder::NCHW ? in[0].dims(1) : in[0].dims(3)); return TensorInferenceForSchema(def, in, num_channels); } static std::vector TensorInferenceForLC( const OperatorDef& def, const std::vector& in) { if (in[0].unknown_shape()) { std::vector out(1); out[0].set_unknown_shape(true); return out; } const int img_ndim = in[0].dims_size() - 2; return TensorInferenceForSchema(def, in, in[1].dims(img_ndim)); } virtual ~ConvPoolOpBase() {} protected: LegacyPadding legacy_pad_; bool global_pooling_; vector kernel_; vector dilation_; vector stride_; vector pads_; bool float16_compute_; int group_; StorageOrder order_; bool shared_buffer_; Workspace* ws_; static inline void ComputeSizeAndPad( const int in_size, const int stride, const int kernel, const int dilation, LegacyPadding legacy_pad, int* pad_head, int* pad_tail, int* out_size) { const int dkernel = dilation * (kernel - 1) + 1; switch (legacy_pad) { case LegacyPadding::NOTSET: // We will just use the direct padding head and tail values, but we // will verify that they are non-negative. CAFFE_ENFORCE_GE(in_size + *pad_head + *pad_tail, dkernel); *out_size = static_cast( static_cast(in_size + *pad_head + *pad_tail - dkernel) / stride + 1); break; case LegacyPadding::VALID: *pad_head = 0; *pad_tail = 0; *out_size = (in_size - dkernel) / stride + 1; break; case LegacyPadding::SAME: { CAFFE_ENFORCE( 1 == dilation, "Dilation not supported for legacy padding."); int legacy_target_size = (in_size + stride - 1) / stride; int pad_needed = (legacy_target_size - 1) * stride + kernel - in_size; if (CAFFE2_PAD_HEAD_MORE) { *pad_head = (pad_needed + 1) / 2; } else { *pad_head = pad_needed / 2; } *pad_tail = pad_needed - *pad_head; *out_size = (in_size + pad_needed - dkernel) / stride + 1; break; } case LegacyPadding::CAFFE_LEGACY_POOLING: // This is in order to adapt Caffe's pooling padding case. In this case, // we will only use pad_head and will compute pad_tail to match the // old caffe pooling strategy. Also see caffe2_legacy.proto for more // details. CAFFE_ENFORCE_GE(*pad_head, 0); // Here, notice that caffe casts UP while caffe2 casts DOWN for the // output size computation. *out_size = std::ceil( static_cast(in_size + *pad_head * 2 - kernel) / stride + 1); // If we have padding, caffe also ensures that the last pooling starts // strictly inside the image (instead of at the padding); otherwise clip // the last. if (*pad_head > 0 && (*out_size - 1) * stride >= in_size + *pad_head) { --*out_size; } // Now, compare the output size with the standard Caffe2 output size. // The // caffe2 standard output size should always be no larger than the // output // size of caffe. int standard_out_size = static_cast( static_cast(in_size + *pad_head * 2 - kernel) / stride + 1); CAFFE_ENFORCE_GE( *out_size, standard_out_size, "This should never happen. If this happens, double check the logic " "above."); if (*out_size > standard_out_size) { LOG(WARNING) << "You are hitting a case where Caffe's legacy padding calculation " "is hit. This leads to inefficient and sometimes incorrect " "results. We are keeping this behavior for backward compatibility" ", but you are strongly recommended to move away from it."; } *pad_tail = *pad_head + stride * (*out_size - standard_out_size); break; } } static inline void ComputeSizeAndPad64( const int in_size, const int stride, const int kernel, const int dilation, LegacyPadding legacy_pad, int* pad_head, int* pad_tail, int64_t* out_size) { const int dkernel = dilation * (kernel - 1) + 1; switch (legacy_pad) { case LegacyPadding::NOTSET: // We will just use the direct padding head and tail values, but we // will verify that they are non-negative. CAFFE_ENFORCE_GE(in_size + *pad_head + *pad_tail, dkernel); *out_size = static_cast( static_cast(in_size + *pad_head + *pad_tail - dkernel) / stride + 1); break; case LegacyPadding::VALID: *pad_head = 0; *pad_tail = 0; *out_size = (in_size - dkernel) / stride + 1; break; case LegacyPadding::SAME: { CAFFE_ENFORCE( 1 == dilation, "Dilation not supported for legacy padding."); int legacy_target_size = (in_size + stride - 1) / stride; int pad_needed = (legacy_target_size - 1) * stride + kernel - in_size; if (CAFFE2_PAD_HEAD_MORE) { *pad_head = (pad_needed + 1) / 2; } else { *pad_head = pad_needed / 2; } *pad_tail = pad_needed - *pad_head; *out_size = (in_size + pad_needed - dkernel) / stride + 1; break; } case LegacyPadding::CAFFE_LEGACY_POOLING: // This is in order to adapt Caffe's pooling padding case. In this case, // we will only use pad_head and will compute pad_tail to match the // old caffe pooling strategy. Also see caffe2_legacy.proto for more // details. CAFFE_ENFORCE_GE(*pad_head, 0); // Here, notice that caffe casts UP while caffe2 casts DOWN for the // output size computation. *out_size = std::ceil( static_cast(in_size + *pad_head * 2 - kernel) / stride + 1); // If we have padding, caffe also ensures that the last pooling starts // strictly inside the image (instead of at the padding); otherwise clip // the last. if (*pad_head > 0 && (*out_size - 1) * stride >= in_size + *pad_head) { --*out_size; } // Now, compare the output size with the standard Caffe2 output size. // The // caffe2 standard output size should always be no larger than the // output // size of caffe. int standard_out_size = static_cast( static_cast(in_size + *pad_head * 2 - kernel) / stride + 1); CAFFE_ENFORCE_GE( *out_size, standard_out_size, "This should never happen. If this happens, double check the logic " "above."); if (*out_size > standard_out_size) { LOG(WARNING) << "You are hitting a case where Caffe's legacy padding calculation " "is hit. This leads to inefficient and sometimes incorrect " "results. We are keeping this behavior for backward compatibility" ", but you are strongly recommended to move away from it."; } *pad_tail = *pad_head + stride * (*out_size - standard_out_size); break; } } // Accessors for 2D conv params. inline int pad_t() const { return pads_[0]; } inline int pad_l() const { return pads_[1]; } inline int pad_b() const { return pads_[2]; } inline int pad_r() const { return pads_[3]; } inline int kernel_h() const { return kernel_[0]; } inline int kernel_w() const { return kernel_[1]; } inline int stride_h() const { return stride_[0]; } inline int stride_w() const { return stride_[1]; } inline int dilation_h() const { return dilation_[0]; } inline int dilation_w() const { return dilation_[1]; } private: inline void AllocateAndCopy(const vector& vec, Tensor& tensor) { tensor.Resize(vec.size()); context_.template CopyFromCPU( vec.size(), vec.data(), tensor.template mutable_data()); } #define USE_CONV_POOL_BASE_FUNCTIONS(Context) \ USE_OPERATOR_FUNCTIONS(Context); \ using ConvPoolOpBase::pads_; \ using ConvPoolOpBase::pad_t; \ using ConvPoolOpBase::pad_l; \ using ConvPoolOpBase::pad_b; \ using ConvPoolOpBase::pad_r; \ using ConvPoolOpBase::legacy_pad_; \ using ConvPoolOpBase::global_pooling_; \ using ConvPoolOpBase::kernel_; \ using ConvPoolOpBase::kernel_h; \ using ConvPoolOpBase::kernel_w; \ using ConvPoolOpBase::dilation_; \ using ConvPoolOpBase::dilation_h; \ using ConvPoolOpBase::dilation_w; \ using ConvPoolOpBase::stride_; \ using ConvPoolOpBase::stride_h; \ using ConvPoolOpBase::stride_w; \ using ConvPoolOpBase::group_; \ using ConvPoolOpBase::order_; \ using ConvPoolOpBase::shared_buffer_; \ using ConvPoolOpBase::GetDims; \ using ConvPoolOpBase::GetDimsSize; \ using ConvPoolOpBase::SetDeviceTensor; \ using ConvPoolOpBase::HasPad; \ using ConvPoolOpBase::HasStride; \ using ConvPoolOpBase::ws_ }; } // namespace caffe2 #endif // CAFFE2_OPERATORS_CONV_POOL_OP_BASE_H_