/
usr
/
local
/
lib64
/
python3.6
/
site-packages
/
torch
/
include
/
caffe2
/
operators
/
/usr/local/lib64/python3.6/site-packages/torch/include/caffe2/operators
mkdir
upload
Name
Size
Mode
Actions
abs_op.h
705
0644
edit
dl
rm
accumulate_op.h
1073
0644
edit
dl
rm
accuracy_op.h
652
0644
edit
dl
rm
acos_op.h
711
0644
edit
dl
rm
activation_ops_cudnn.h
4122
0644
edit
dl
rm
affine_channel_op.h
3450
0644
edit
dl
rm
alias_with_name.h
1234
0644
edit
dl
rm
apmeter_op.h
1027
0644
edit
dl
rm
arg_ops.h
2319
0644
edit
dl
rm
asin_op.h
711
0644
edit
dl
rm
assert_op.h
1335
0644
edit
dl
rm
async_net_barrier_op.h
904
0644
edit
dl
rm
atan_op.h
711
0644
edit
dl
rm
batch_box_cox_op.h
2287
0644
edit
dl
rm
batch_bucketize_op.h
720
0644
edit
dl
rm
batch_gather_ops.h
5264
0644
edit
dl
rm
batch_matmul_op.h
9602
0644
edit
dl
rm
batch_moments_op.h
3364
0644
edit
dl
rm
batch_permutation_op.h
954
0644
edit
dl
rm
batch_sparse_to_dense_op.h
6147
0644
edit
dl
rm
bbox_transform_op.h
2668
0644
edit
dl
rm
bisect_percentile_op.h
4921
0644
edit
dl
rm
boolean_mask_ops.h
2665
0644
edit
dl
rm
boolean_unmask_ops.h
378
0644
edit
dl
rm
box_with_nms_limit_op.h
4960
0644
edit
dl
rm
bucketize_op.h
1361
0644
edit
dl
rm
byte_weight_dequant_op.h
1722
0644
edit
dl
rm
cast_op.h
1393
0644
edit
dl
rm
cbrt_op.h
723
0644
edit
dl
rm
cc_bmm_bg_op.h
3894
0644
edit
dl
rm
ceil_op.h
782
0644
edit
dl
rm
channel_backprop_stats_op.h
737
0644
edit
dl
rm
channel_shuffle_op.h
1902
0644
edit
dl
rm
channel_stats_op.h
1807
0644
edit
dl
rm
clip_op.h
1639
0644
edit
dl
rm
collect_and_distribute_fpn_rpn_proposals_op.h
6875
0644
edit
dl
rm
concat_split_op.h
11850
0644
edit
dl
rm
conditional_op.h
487
0644
edit
dl
rm
conv_op.h
3125
0644
edit
dl
rm
conv_op_cache_cudnn.h
1935
0644
edit
dl
rm
conv_op_impl.h
28729
0644
edit
dl
rm
conv_op_shared.h
672
0644
edit
dl
rm
conv_pool_op_base.h
32109
0644
edit
dl
rm
conv_transpose_op.h
1727
0644
edit
dl
rm
conv_transpose_op_impl.h
18264
0644
edit
dl
rm
conv_transpose_op_mobile.h
1470
0644
edit
dl
rm
conv_transpose_op_mobile_impl.h
19587
0644
edit
dl
rm
conv_transpose_unpool_op_base.h
10303
0644
edit
dl
rm
copy_op.h
1296
0644
edit
dl
rm
copy_rows_to_tensor_op.h
2599
0644
edit
dl
rm
cosh_op.h
711
0644
edit
dl
rm
cosine_embedding_criterion_op.h
1127
0644
edit
dl
rm
cos_op.h
705
0644
edit
dl
rm
counter_ops.h
4596
0644
edit
dl
rm
create_scope_op.h
5232
0644
edit
dl
rm
cross_entropy_op.h
4420
0644
edit
dl
rm
ctc_beam_search_decoder_op.h
1102
0644
edit
dl
rm
ctc_greedy_decoder_op.h
817
0644
edit
dl
rm
cube_op.h
723
0644
edit
dl
rm
dataset_ops.h
5501
0644
edit
dl
rm
data_couple.h
464
0644
edit
dl
rm
deform_conv_op.h
3543
0644
edit
dl
rm
deform_conv_op_impl.h
13171
0644
edit
dl
rm
dense_vector_to_id_list_op.h
1797
0644
edit
dl
rm
distance_op.h
8419
0644
edit
dl
rm
do_op.h
6981
0644
edit
dl
rm
dropout_op.h
1516
0644
edit
dl
rm
elementwise_add_op.h
2024
0644
edit
dl
rm
elementwise_div_op.h
1224
0644
edit
dl
rm
elementwise_linear_op.h
1170
0644
edit
dl
rm
elementwise_logical_ops.h
5083
0644
edit
dl
rm
elementwise_mul_op.h
1224
0644
edit
dl
rm
elementwise_ops.h
19115
0644
edit
dl
rm
elementwise_ops_utils.h
1008
0644
edit
dl
rm
elementwise_op_test.h
9237
0644
edit
dl
rm
elementwise_sub_op.h
2025
0644
edit
dl
rm
elu_op.h
875
0644
edit
dl
rm
enforce_finite_op.h
2303
0644
edit
dl
rm
ensure_clipped_op.h
1608
0644
edit
dl
rm
ensure_cpu_output_op.h
1465
0644
edit
dl
rm
erf_op.h
751
0644
edit
dl
rm
expand_op.h
3877
0644
edit
dl
rm
expand_squeeze_dims_op.h
3451
0644
edit
dl
rm
exp_op.h
425
0644
edit
dl
rm
fc_inference.h
775
0644
edit
dl
rm
feature_maps_ops.h
32437
0644
edit
dl
rm
feed_blob_op.h
802
0644
edit
dl
rm
filler_op.h
18431
0644
edit
dl
rm
find_duplicate_elements_op.h
1563
0644
edit
dl
rm
find_op.h
2055
0644
edit
dl
rm
flatten_op.h
1525
0644
edit
dl
rm
flexible_top_k.h
936
0644
edit
dl
rm
floor_op.h
788
0644
edit
dl
rm
free_op.h
777
0644
edit
dl
rm
fully_connected_op.h
9351
0644
edit
dl
rm
fused_rowwise_8bit_conversion_ops.h
6601
0644
edit
dl
rm
fused_rowwise_nbitfake_conversion_ops.h
4375
0644
edit
dl
rm
fused_rowwise_nbit_conversion_ops.h
8723
0644
edit
dl
rm
fused_rowwise_random_quantization_ops.h
2607
0644
edit
dl
rm
gather_fused_8bit_rowwise_op.h
2179
0644
edit
dl
rm
gather_op.h
7505
0644
edit
dl
rm
gather_ranges_to_dense_op.h
8188
0644
edit
dl
rm
gelu_op.h
1452
0644
edit
dl
rm
generate_proposals_op.h
6256
0644
edit
dl
rm
generate_proposals_op_util_boxes.h
14309
0644
edit
dl
rm
generate_proposals_op_util_nms.h
26214
0644
edit
dl
rm
generate_proposals_op_util_nms_gpu.h
2128
0644
edit
dl
rm
given_tensor_byte_string_to_uint8_fill_op.h
2150
0644
edit
dl
rm
given_tensor_fill_op.h
3002
0644
edit
dl
rm
glu_op.h
1458
0644
edit
dl
rm
group_norm_op.h
8967
0644
edit
dl
rm
gru_unit_op.h
6626
0644
edit
dl
rm
half_float_ops.h
2732
0644
edit
dl
rm
hard_sigmoid_op.h
994
0644
edit
dl
rm
heatmap_max_keypoint_op.h
939
0644
edit
dl
rm
histogram_op.h
2421
0644
edit
dl
rm
h_softmax_op.h
4954
0644
edit
dl
rm
if_op.h
1764
0644
edit
dl
rm
im2col_op.h
8943
0644
edit
dl
rm
index_hash_ops.h
2232
0644
edit
dl
rm
index_ops.h
3155
0644
edit
dl
rm
inference_lstm_op.h
9881
0644
edit
dl
rm
instance_norm_op.h
7441
0644
edit
dl
rm
integral_image_op.h
923
0644
edit
dl
rm
is_empty_op.h
558
0644
edit
dl
rm
jsd_op.h
721
0644
edit
dl
rm
key_split_ops.h
1400
0644
edit
dl
rm
layer_norm_op.h
8098
0644
edit
dl
rm
leaky_relu_op.h
1111
0644
edit
dl
rm
lengths_pad_op.h
2574
0644
edit
dl
rm
lengths_reducer_fused_8bit_rowwise_ops.h
5532
0644
edit
dl
rm
lengths_reducer_fused_nbit_rowwise_ops.h
23465
0644
edit
dl
rm
lengths_reducer_ops.h
23315
0644
edit
dl
rm
lengths_reducer_rowwise_8bit_ops.h
6180
0644
edit
dl
rm
lengths_tile_op.h
582
0644
edit
dl
rm
lengths_top_k_op.h
1358
0644
edit
dl
rm
length_split_op.h
2259
0644
edit
dl
rm
listwise_l2r_op.h
1677
0644
edit
dl
rm
load_save_op.h
14091
0644
edit
dl
rm
load_save_op_util.h
1642
0644
edit
dl
rm
locally_connected_op.h
3872
0644
edit
dl
rm
locally_connected_op_impl.h
26495
0644
edit
dl
rm
locally_connected_op_util.h
1332
0644
edit
dl
rm
local_response_normalization_op.h
2804
0644
edit
dl
rm
log1p_op.h
717
0644
edit
dl
rm
logit_op.h
1129
0644
edit
dl
rm
log_op.h
431
0644
edit
dl
rm
loss_op.h
1058
0644
edit
dl
rm
lpnorm_op.h
1279
0644
edit
dl
rm
lstm_unit_op.h
6733
0644
edit
dl
rm
lstm_utils.h
9424
0644
edit
dl
rm
map_ops.h
8011
0644
edit
dl
rm
margin_ranking_criterion_op.h
1113
0644
edit
dl
rm
matmul_op.h
2843
0644
edit
dl
rm
max_pool_with_index_gpu.h
1155
0644
edit
dl
rm
mean_op.h
3252
0644
edit
dl
rm
merge_id_lists_op.h
2570
0644
edit
dl
rm
minmax_ops.h
3829
0644
edit
dl
rm
mish_op.h
794
0644
edit
dl
rm
mod_op.h
984
0644
edit
dl
rm
moments_op.h
4051
0644
edit
dl
rm
multi_class_accuracy_op.h
539
0644
edit
dl
rm
negate_gradient_op.h
566
0644
edit
dl
rm
negative_op.h
451
0644
edit
dl
rm
ngram_ops.h
2644
0644
edit
dl
rm
normalize_l1_op.h
1075
0644
edit
dl
rm
normalize_op.h
3013
0644
edit
dl
rm
no_default_engine_op.h
1063
0644
edit
dl
rm
numpy_tile_op.h
3643
0644
edit
dl
rm
one_hot_ops.h
2562
0644
edit
dl
rm
onnx_while_op.h
10655
0644
edit
dl
rm
operator_fallback_gpu.h
4155
0644
edit
dl
rm
op_utils_cudnn.h
2112
0644
edit
dl
rm
order_switch_ops.h
2149
0644
edit
dl
rm
pack_rnn_sequence_op.h
3074
0644
edit
dl
rm
pack_segments.h
2729
0644
edit
dl
rm
pad_op.h
2902
0644
edit
dl
rm
partition_ops.h
9958
0644
edit
dl
rm
percentile_op.h
1009
0644
edit
dl
rm
perplexity_op.h
447
0644
edit
dl
rm
piecewise_linear_transform_op.h
8281
0644
edit
dl
rm
pool_op.h
8525
0644
edit
dl
rm
pool_op_util.h
1105
0644
edit
dl
rm
pow_op.h
4677
0644
edit
dl
rm
prefetch_op.h
4661
0644
edit
dl
rm
prelu_op.h
1067
0644
edit
dl
rm
prepend_dim_op.h
2760
0644
edit
dl
rm
quantile_op.h
4120
0644
edit
dl
rm
quant_decode_op.h
5370
0644
edit
dl
rm
rank_loss_op.h
820
0644
edit
dl
rm
reciprocal_op.h
721
0644
edit
dl
rm
reducer_functors.h
24556
0644
edit
dl
rm
reduce_front_back_max_ops.h
4399
0644
edit
dl
rm
reduce_front_back_sum_mean_ops.h
5337
0644
edit
dl
rm
reduce_ops.h
9962
0644
edit
dl
rm
reduction_ops.h
5944
0644
edit
dl
rm
relu_n_op.h
990
0644
edit
dl
rm
relu_op.h
624
0644
edit
dl
rm
remove_data_blocks_op.h
2651
0644
edit
dl
rm
replace_nan_op.h
1170
0644
edit
dl
rm
reshape_op.h
5723
0644
edit
dl
rm
resize_3d_op.h
2677
0644
edit
dl
rm
resize_op.h
2307
0644
edit
dl
rm
reverse_packed_segs_op.h
2772
0644
edit
dl
rm
rmac_regions_op.h
708
0644
edit
dl
rm
rms_norm_op.h
2968
0644
edit
dl
rm
roi_align_gradient_op.h
1486
0644
edit
dl
rm
roi_align_op.h
2857
0644
edit
dl
rm
roi_align_rotated_gradient_op.h
1369
0644
edit
dl
rm
roi_align_rotated_op.h
1636
0644
edit
dl
rm
roi_pool_op.h
2503
0644
edit
dl
rm
rowmul_op.h
1947
0644
edit
dl
rm
rsqrt_op.h
729
0644
edit
dl
rm
scale_blobs_op.h
1458
0644
edit
dl
rm
scale_op.h
1019
0644
edit
dl
rm
segment_reduction_op.h
71022
0644
edit
dl
rm
self_binning_histogram_op.h
6258
0644
edit
dl
rm
selu_op.h
1545
0644
edit
dl
rm
sequence_ops.h
8264
0644
edit
dl
rm
shape_op.h
1638
0644
edit
dl
rm
sigmoid_op.h
639
0644
edit
dl
rm
sinh_op.h
711
0644
edit
dl
rm
sinusoid_position_encoding_op.h
2834
0644
edit
dl
rm
sin_op.h
705
0644
edit
dl
rm
slice_op.h
10071
0644
edit
dl
rm
softmax_op.h
1174
0644
edit
dl
rm
softmax_utils.h
447
0644
edit
dl
rm
softmax_with_loss_op.h
2883
0644
edit
dl
rm
softplus_op.h
781
0644
edit
dl
rm
softsign_op.h
675
0644
edit
dl
rm
space_batch_op.h
6848
0644
edit
dl
rm
sparse_dropout_with_replacement_op.h
1122
0644
edit
dl
rm
sparse_itemwise_dropout_with_replacement_op.h
1163
0644
edit
dl
rm
sparse_lp_regularizer_op.h
1130
0644
edit
dl
rm
sparse_normalize_op.h
834
0644
edit
dl
rm
sparse_to_dense_mask_op.h
10051
0644
edit
dl
rm
sparse_to_dense_op.h
3977
0644
edit
dl
rm
spatial_batch_norm_op.h
15175
0644
edit
dl
rm
spatial_softmax_with_loss_op.h
2182
0644
edit
dl
rm
sqrt_op.h
448
0644
edit
dl
rm
sqr_op.h
431
0644
edit
dl
rm
square_root_divide_op.h
1857
0644
edit
dl
rm
stats_put_ops.h
2813
0644
edit
dl
rm
stop_gradient.h
548
0644
edit
dl
rm
string_ops.h
2067
0644
edit
dl
rm
stump_func_op.h
2112
0644
edit
dl
rm
summarize_op.h
1875
0644
edit
dl
rm
swish_op.h
772
0644
edit
dl
rm
tanh_op.h
723
0644
edit
dl
rm
tan_op.h
705
0644
edit
dl
rm
tensor_protos_db_input.h
3633
0644
edit
dl
rm
text_file_reader_utils.h
2900
0644
edit
dl
rm
thresholded_relu_op.h
1137
0644
edit
dl
rm
tile_op.h
8741
0644
edit
dl
rm
top_k.h
1061
0644
edit
dl
rm
transpose_op.h
2082
0644
edit
dl
rm
tt_linear_op.h
6501
0644
edit
dl
rm
unique_ops.h
1666
0644
edit
dl
rm
unsafe_coalesce.h
2481
0644
edit
dl
rm
upsample_op.h
2246
0644
edit
dl
rm
utility_ops.h
49994
0644
edit
dl
rm
variable_length_sequence_padding.h
1378
0644
edit
dl
rm
weighted_multi_sampling_op.h
602
0644
edit
dl
rm
weighted_sample_op.h
739
0644
edit
dl
rm
while_op.h
1961
0644
edit
dl
rm
zero_gradient_op.h
347
0644
edit
dl
rm
Edit:
/usr/local/lib64/python3.6/site-packages/torch/include/caffe2/operators/elementwise_ops.h
(19115B)
#ifndef CAFFE2_OPERATORS_ELEMENTWISE_OPS_H_ #define CAFFE2_OPERATORS_ELEMENTWISE_OPS_H_ #include <iterator> #include <string> #include <tuple> #include <vector> #include "caffe2/core/common_omp.h" #include "caffe2/core/context.h" #include "caffe2/core/logging.h" #include "caffe2/core/operator.h" #include "caffe2/core/tensor.h" #include "caffe2/operators/elementwise_ops_utils.h" #include "caffe2/utils/eigen_utils.h" #include "caffe2/utils/math.h" namespace caffe2 { using NumericTypes = TensorTypes<int32_t, int64_t, float, double>; using IntTypes = TensorTypes<int32_t, int64_t>; using BoolTypes = TensorTypes<bool>; using IntBoolTypes = TensorTypes<int32_t, int64_t, bool>; // discrete types struct SameTypeAsInput { template <typename T> using type = T; }; template <typename R> struct FixedType { template <typename T> using type = R; }; template < typename InputTypes, class Context, class Functor, class OutputTypeMap = SameTypeAsInput> class UnaryElementwiseWithArgsOp final : public Operator<Context> { public: USE_OPERATOR_CONTEXT_FUNCTIONS; template <class... Args> explicit UnaryElementwiseWithArgsOp(Args&&... args) : Operator<Context>(std::forward<Args>(args)...), functor_(*this) {} bool RunOnDevice() override { return DispatchHelper<InputTypes>::call(this, Input(0)); } template <typename T> bool DoRunWithType() { const auto& X = Input(0); auto* Y = Output( 0, X.sizes(), at::dtype<typename OutputTypeMap::template type<T>>()); return functor_( X.numel(), X.template data<T>(), Y->template mutable_data<typename OutputTypeMap::template type<T>>(), &context_); } private: Functor functor_; }; // UnaryFunctorWithDefaultCtor is a functor that can be used as the functor of // an UnaryElementwiseWithArgsOp. It simply forwards the operator() call into // another functor that doesn't accept arguments in its constructor. template <class Functor> struct UnaryFunctorWithDefaultCtor { explicit UnaryFunctorWithDefaultCtor(OperatorBase& /* op */) {} template <typename TIn, typename TOut, class Context> bool operator()(const int size, const TIn* X, TOut* Y, Context* context) const { return functor(size, X, Y, context); } Functor functor{}; }; // UnaryElementwiseOp is a wrapper around UnaryElementwiseWithArgsOp, with the // difference that it takes a functor with default constructor, e.g. that does // not need to take into consideration any arguments during operator creation. template < typename InputTypes, class Context, class Functor, class OutputTypeMap = SameTypeAsInput> using UnaryElementwiseOp = UnaryElementwiseWithArgsOp< InputTypes, Context, UnaryFunctorWithDefaultCtor<Functor>, OutputTypeMap>; template < typename InputTypes, class Context, class Functor, class OutputTypeMap = SameTypeAsInput> class BinaryElementwiseWithArgsOp final : public Operator<Context> { public: USE_OPERATOR_CONTEXT_FUNCTIONS; template <class... Args> explicit BinaryElementwiseWithArgsOp(Args&&... args) : Operator<Context>(std::forward<Args>(args)...), OP_SINGLE_ARG(bool, "broadcast", legacy_broadcast_, false), OP_SINGLE_ARG(int, "axis", axis_, -1), OP_SINGLE_ARG(string, "axis_str", axis_str_, string("")), OP_SINGLE_ARG(string, "order", order_, "NCHW"), functor_(*this) { if (legacy_broadcast_) { if (axis_ != -1) { // Get axis from an explicit axis argument. CAFFE_ENFORCE_EQ( axis_str_.size(), 0U, "Args axis and axis_str cannot be used simultaneously."); } else if (axis_str_.size()) { // Get the axis index semantically. CAFFE_ENFORCE_EQ( axis_str_.size(), 1U, "Unsupported axis string", axis_str_); const size_t semantic_axis_ = order_.find(axis_str_); CAFFE_ENFORCE_NE( semantic_axis_, string::npos, "Unrecognizable axis string ", axis_str_, " from order string ", order_); axis_ = semantic_axis_; } else { CAFFE_ENFORCE( axis_ == -1 && axis_str_.empty(), "Do not specify axis or axis_str if broadcast is not enabled."); } } } bool RunOnDevice() override { return DispatchHelper<InputTypes>::call(this, Input(0)); } template <typename T> bool DoRunWithType() { const auto& A = Input(0); const auto& B = Input(1); const T* A_data = A.template data<T>(); const T* B_data = B.template data<T>(); std::vector<int> A_dims; std::vector<int> B_dims; std::vector<int64_t> C_dims; if (legacy_broadcast_) { CAFFE_ENFORCE( !IsInputOutputAlias(1, 0), "In-place is allowed only with the first tensor when " "legacy-broadcasting"); C_dims = A.sizes().vec(); if (B.numel() == 1) { A_dims = {static_cast<int>(A.numel())}; B_dims = {1}; } else { size_t pre, n, post; std::tie(pre, n, post) = elementwise_ops_utils::ComputeLegacyBroadcastSizes(A, B, axis_); A_dims = { static_cast<int>(pre), static_cast<int>(n), static_cast<int>(post)}; B_dims = {static_cast<int>(n), 1}; } } else { std::copy( A.sizes().cbegin(), A.sizes().cend(), std::back_inserter(A_dims)); std::copy( B.sizes().cbegin(), B.sizes().cend(), std::back_inserter(B_dims)); // TODO: change the types to vector<int64_t> auto C_dims_int = elementwise_ops_utils::ComputeBinaryBroadcastForwardDims( A_dims, B_dims); std::copy( C_dims_int.cbegin(), C_dims_int.cend(), std::back_inserter(C_dims)); if (IsInputOutputAlias(0, 0)) { CAFFE_ENFORCE_EQ(C_dims_int, A_dims); } else if (IsInputOutputAlias(1, 0)) { CAFFE_ENFORCE_EQ(C_dims_int, B_dims); } } auto* C = Output( 0, C_dims, at::dtype<typename OutputTypeMap::template type<T>>()); auto* C_data = C->template mutable_data<typename OutputTypeMap::template type<T>>(); return functor_.Forward(A_dims, B_dims, A_data, B_data, C_data, &context_); } private: const bool legacy_broadcast_; int axis_; const std::string axis_str_; const std::string order_; Functor functor_; }; template < typename InputTypes, class Context, class Functor, class OutputTypeMap = SameTypeAsInput, class GradientTypeMap = SameTypeAsInput> class BinaryElementwiseWithArgsGradientOp final : public Operator<Context> { public: USE_OPERATOR_CONTEXT_FUNCTIONS; template <class... Args> explicit BinaryElementwiseWithArgsGradientOp(Args&&... args) : Operator<Context>(std::forward<Args>(args)...), OP_SINGLE_ARG(bool, "broadcast", legacy_broadcast_, false), OP_SINGLE_ARG(int, "axis", axis_, -1), OP_SINGLE_ARG(string, "axis_str", axis_str_, ""), OP_SINGLE_ARG(string, "order", order_, "NCHW"), functor_(*this) { if (legacy_broadcast_) { if (axis_ != -1) { // Get axis from an explicit axis argument. CAFFE_ENFORCE_EQ( axis_str_.size(), 0U, "Args axis and axis_str cannot be used simultaneously."); } else if (axis_str_.size()) { // Get the axis index semantically. CAFFE_ENFORCE_EQ( axis_str_.size(), 1U, "Unsupported axis string", axis_str_); const size_t semantic_axis_ = order_.find(axis_str_); CAFFE_ENFORCE_NE( semantic_axis_, string::npos, "Unrecognizable axis string ", axis_str_, " from order string ", order_); axis_ = semantic_axis_; } else { CAFFE_ENFORCE( axis_ == -1 && axis_str_.empty(), "Do not specify axis or axis_str if broadcast is not enabled."); } } } bool RunOnDevice() override { return DispatchHelper<InputTypes>::call(this, Input(1)); } template <typename T> bool DoRunWithType() { const auto& dC = Input(0); const auto& A = Input(1); const auto& B = Input(2); vector<int> A_dims; vector<int> B_dims; if (legacy_broadcast_) { if (B.numel() == 1) { A_dims = {static_cast<int>(A.numel())}; B_dims = {1}; } else { size_t pre, n, post; std::tie(pre, n, post) = elementwise_ops_utils::ComputeLegacyBroadcastSizes(A, B, axis_); A_dims = { static_cast<int>(pre), static_cast<int>(n), static_cast<int>(post)}; B_dims = {static_cast<int>(n), 1}; } } else { std::copy( A.sizes().cbegin(), A.sizes().cend(), std::back_inserter(A_dims)); std::copy( B.sizes().cbegin(), B.sizes().cend(), std::back_inserter(B_dims)); } const typename OutputTypeMap::template type<T>* C_data = nullptr; if (InputSize() == 4) { const auto& C = Input(3); C_data = C.template data<typename OutputTypeMap::template type<T>>(); } const auto* dC_data = dC.template data<typename GradientTypeMap::template type<T>>(); const T* A_data = A.template data<T>(); const T* B_data = B.template data<T>(); auto* dA = Output( 0, A.sizes(), at::dtype<typename GradientTypeMap::template type<T>>()); auto* dB = Output( 1, B.sizes(), at::dtype<typename GradientTypeMap::template type<T>>()); auto* dA_data = dA->template mutable_data<typename GradientTypeMap::template type<T>>(); auto* dB_data = dB->template mutable_data<typename GradientTypeMap::template type<T>>(); return functor_.Backward( A_dims, B_dims, dC_data, A_data, B_data, C_data, dA_data, dB_data, &context_); } private: const bool legacy_broadcast_; int axis_; const std::string axis_str_; const std::string order_; Functor functor_; }; template <class Functor> struct BinaryFunctorWithDefaultCtor { explicit BinaryFunctorWithDefaultCtor(OperatorBase& /* op */) {} template <typename TIn, typename TOut, class Context> bool Forward( const std::vector<int>& A_dims, const std::vector<int>& B_dims, const TIn* A_data, const TIn* B_data, TOut* C_data, Context* context) const { return functor.Forward(A_dims, B_dims, A_data, B_data, C_data, context); } template <typename TGrad, typename TIn, typename TOut, class Context> bool Backward( const std::vector<int>& A_dims, const std::vector<int>& B_dims, const TGrad* dC_data, const TIn* A_data, const TIn* B_data, const TOut* C_data, TGrad* dA_data, TGrad* dB_data, Context* context) const { return functor.Backward( A_dims, B_dims, dC_data, A_data, B_data, C_data, dA_data, dB_data, context); } Functor functor{}; }; template <class Functor> struct BinaryFunctorWithBroadcastOptionsCtor { explicit BinaryFunctorWithBroadcastOptionsCtor(OperatorBase& op) : functor{op.GetSingleArgument<bool>("allow_broadcast_fastpath", false)} {} template <typename TIn, typename TOut, class Context> bool Forward( const std::vector<int>& A_dims, const std::vector<int>& B_dims, const TIn* A_data, const TIn* B_data, TOut* C_data, Context* context) const { return functor.Forward(A_dims, B_dims, A_data, B_data, C_data, context); } template <typename TGrad, typename TIn, typename TOut, class Context> bool Backward( const std::vector<int>& A_dims, const std::vector<int>& B_dims, const TGrad* dC_data, const TIn* A_data, const TIn* B_data, const TOut* C_data, TGrad* dA_data, TGrad* dB_data, Context* context) const { return functor.Backward( A_dims, B_dims, dC_data, A_data, B_data, C_data, dA_data, dB_data, context); } Functor functor; }; // BinaryElementwiseOp is a wrapper around BinaryElementwiseWithArgsOp, with the // difference that it takes a functor with default constructor, e.g. that does // not need to take into consideration any arguments during operator creation. template < typename InputTypes, class Context, class Functor, class TypeMap = SameTypeAsInput> using BinaryElementwiseOp = BinaryElementwiseWithArgsOp< InputTypes, Context, BinaryFunctorWithDefaultCtor<Functor>, TypeMap>; // BinaryElementwiseGradientOp is a wrapper around // BinaryElementwiseGradientWithArgsOp, with the difference that it takes a // functor with default constructor, e.g. that does not need to take into // consideration any arguments during operator creation. template < typename InputTypes, class Context, class Functor, class OutputTypeMap = SameTypeAsInput, class GradientTypeMap = SameTypeAsInput> using BinaryElementwiseGradientOp = BinaryElementwiseWithArgsGradientOp< InputTypes, Context, BinaryFunctorWithDefaultCtor<Functor>, OutputTypeMap, GradientTypeMap>; // BinaryElementwiseBroadcastOp is a wrapper around BinaryElementwiseWithArgsOp, // with the difference that it takes a functor with a constructor that accepts // broadcast-related arguments (just a single boolean for whether broadcast // fastpaths are allowed at the time this comment was written). template < typename InputTypes, class Context, class Functor, class TypeMap = SameTypeAsInput> using BinaryElementwiseBroadcastOp = BinaryElementwiseWithArgsOp< InputTypes, Context, BinaryFunctorWithBroadcastOptionsCtor<Functor>, TypeMap>; // BinaryElementwiseGradientBroadcastOp is a wrapper around // BinaryElementwiseWithArgsGradientOp, with the difference that it takes a // functor with a constructor that accepts broadcast-related arguments (just a // single boolean for whether broadcast fastpaths are allowed at the time this // comment was written). template < typename InputTypes, class Context, class Functor, class OutputTypeMap = SameTypeAsInput, class GradientTypeMap = SameTypeAsInput> using BinaryElementwiseGradientBroadcastOp = BinaryElementwiseWithArgsGradientOp< InputTypes, Context, BinaryFunctorWithBroadcastOptionsCtor<Functor>, OutputTypeMap, GradientTypeMap>; // Forward-only Unary Functors. template <class Context> struct NotFunctor { bool operator()(const int N, const bool* X, bool* Y, Context* context) const { math::Not(N, X, Y, context); return true; } }; template <class Context> struct SignFunctor { template <typename T> bool operator()(const int N, const T* X, T* Y, Context* context) const { math::Sign(N, X, Y, context); return true; } }; // Forward-only Binary Functors. #define C10_DECLARE_FORWARD_ONLY_BINARY_FUNCTOR(FunctorName) \ template <class Context> \ struct FunctorName##Functor { \ template <typename TIn, typename TOut> \ bool Forward( \ const std::vector<int>& A_dims, \ const std::vector<int>& B_dims, \ const TIn* A, \ const TIn* B, \ TOut* C, \ Context* context) const { \ math::FunctorName( \ A_dims.size(), \ A_dims.data(), \ B_dims.size(), \ B_dims.data(), \ A, \ B, \ C, \ context); \ return true; \ } \ }; // Compare functors. C10_DECLARE_FORWARD_ONLY_BINARY_FUNCTOR(EQ); C10_DECLARE_FORWARD_ONLY_BINARY_FUNCTOR(NE); C10_DECLARE_FORWARD_ONLY_BINARY_FUNCTOR(LT); C10_DECLARE_FORWARD_ONLY_BINARY_FUNCTOR(LE); C10_DECLARE_FORWARD_ONLY_BINARY_FUNCTOR(GT); C10_DECLARE_FORWARD_ONLY_BINARY_FUNCTOR(GE); // Logical functors. C10_DECLARE_FORWARD_ONLY_BINARY_FUNCTOR(And); C10_DECLARE_FORWARD_ONLY_BINARY_FUNCTOR(Or); C10_DECLARE_FORWARD_ONLY_BINARY_FUNCTOR(Xor); // Bitwise functors. C10_DECLARE_FORWARD_ONLY_BINARY_FUNCTOR(BitwiseAnd); C10_DECLARE_FORWARD_ONLY_BINARY_FUNCTOR(BitwiseOr); C10_DECLARE_FORWARD_ONLY_BINARY_FUNCTOR(BitwiseXor); #undef C10_DECLARE_FORWARD_ONLY_BINARY_FUNCTOR namespace SRLHelper { template <typename T> void sum2one(const T* a, T* y, size_t n); template <typename T> void RunWithBroadcastFront(const T* a, T* y, size_t pre, size_t n, CPUContext*); template <typename T> void RunWithBroadcastBack(const T* a, T* y, size_t post, size_t n, CPUContext*); template <typename T> void RunWithBroadcast2( const T* a, T* y, size_t pre, size_t n, size_t post, CPUContext*); } // namespace SRLHelper // Sum reduction operator that is used for computing the gradient in cases // where the forward op is in broadcast mode. template <class Context> class SumReduceLikeOp final : public Operator<Context> { public: USE_OPERATOR_CONTEXT_FUNCTIONS; template <class... Args> explicit SumReduceLikeOp(Args&&... args) : Operator<Context>(std::forward<Args>(args)...), OP_SINGLE_ARG(int, "axis", axis_, -1), OP_SINGLE_ARG(string, "axis_str", axis_str_, ""), OP_SINGLE_ARG(string, "order", order_, "NCHW") { if (axis_ != -1) { // Get axis from an explicit axis argument. CAFFE_ENFORCE_EQ( axis_str_.size(), 0U, "Args axis and axis_str cannot be used simultaneously."); } else if (axis_str_.size()) { // Get the axis index semantically. CAFFE_ENFORCE_EQ( axis_str_.size(), 1U, "Unsupported axis string", axis_str_); size_t semantic_axis = order_.find(axis_str_); CAFFE_ENFORCE_NE( semantic_axis, string::npos, "Unrecognizable axis string ", axis_str_, " from order string ", order_); axis_ = semantic_axis; } } bool RunOnDevice() override { return DispatchHelper<TensorTypes<float, double>>::call(this, Input(0)); } template <typename T> bool DoRunWithType(); private: int axis_; string axis_str_; string order_; Tensor ones_{Context::GetDeviceType()}; Tensor sum_buffer_{Context::GetDeviceType()}; }; } // namespace caffe2 #endif // CAFFE2_OPERATORS_ELEMENTWISE_OPS_H_
Save
cmd:
run