Skip to content

Commit 99a5d19

Browse files
ssnlfacebook-github-bot
authored andcommitted
Rename elementwise_mean to mean (pytorch#13419)
Summary: Closes pytorch#12459 Pull Request resolved: pytorch#13419 Differential Revision: D12883299 Pulled By: SsnL fbshipit-source-id: 8b4512ff73b66fdc674412904dbb3bf497ba70a7
1 parent a5b627a commit 99a5d19

33 files changed

+247
-244
lines changed

aten/src/ATen/core/Reduction.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Reduction {
77
// Ideally, this would be a scoped enum, but jit doesn't support that
88
enum Reduction {
99
None, // Do not reduce
10-
ElementwiseMean, // Sum losses and take mean over each individually computed loss element
10+
Mean, // (Possibly weighted) mean of losses
1111
Sum, // Sum losses
1212
END
1313
};

aten/src/ATen/native/Loss.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace {
99
static inline at::Tensor apply_loss_reduction(const at::Tensor& unreduced, int64_t reduction) {
10-
if (reduction == Reduction::ElementwiseMean) {
10+
if (reduction == Reduction::Mean) {
1111
return unreduced.mean();
1212
} else if (reduction == Reduction::Sum) {
1313
return unreduced.sum();
@@ -81,7 +81,7 @@ Tensor kl_div_backward_cpu(const Tensor& grad, const Tensor& input, const Tensor
8181
}
8282
});
8383
});
84-
if (reduction == Reduction::ElementwiseMean) {
84+
if (reduction == Reduction::Mean) {
8585
return grad_input / input.numel();
8686
}
8787
return grad_input;
@@ -119,7 +119,7 @@ Tensor binary_cross_entropy_with_logits_backward(const Tensor& grad, const Tenso
119119
grad_input.mul_(weight);
120120
}
121121

122-
if (reduction == Reduction::ElementwiseMean) {
122+
if (reduction == Reduction::Mean) {
123123
return grad_input / input.numel();
124124
}
125125

aten/src/ATen/native/LossCTC.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ Tensor ctc_loss(const Tensor& log_probs, const Tensor& targets, IntList input_le
353353
} else {
354354
res = std::get<0>(at::_ctc_loss(log_probs, targets, input_lengths, target_lengths, BLANK));
355355
}
356-
if (reduction == Reduction::ElementwiseMean) {
356+
if (reduction == Reduction::Mean) {
357357
auto target_lengths_t = at::tensor(target_lengths, res.options().device(at::Device(at::Device::Type::CPU)).dtype(kLong)).toType(res.type());
358358
return (res / target_lengths_t).mean();
359359
} else if (reduction == Reduction::Sum) {

aten/src/ATen/native/cuda/Loss.cu

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Tensor kl_div_backward_cuda(const Tensor& grad, const Tensor& input, const Tenso
3131
AT_DISPATCH_FLOATING_TYPES_AND_HALF(input.type(), "kl_div_backward", [&]() {
3232
kl_div_backward_kernel<scalar_t>(grad_input, target, grad_expand);
3333
});
34-
if (reduction == Reduction::ElementwiseMean) {
34+
if (reduction == Reduction::Mean) {
3535
return grad_input / input.numel();
3636
}
3737
return grad_input;

aten/src/ATen/native/native_functions.yaml

+8-8
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@
461461
CPU: _cosh_out_cpu
462462
CUDA: _cosh_out_cuda
463463

464-
- func: cosine_embedding_loss(Tensor input1, Tensor input2, Tensor target, double margin=0.0, int64_t reduction=Reduction::ElementwiseMean) -> Tensor
464+
- func: cosine_embedding_loss(Tensor input1, Tensor input2, Tensor target, double margin=0.0, int64_t reduction=Reduction::Mean) -> Tensor
465465

466466
- func: cudnn_affine_grid_generator(Tensor theta, int64_t N, int64_t C, int64_t H, int64_t W) -> Tensor
467467
return:
@@ -568,10 +568,10 @@
568568

569569
- func: cumprod_out(Tensor result, Tensor self, int64_t dim) -> Tensor
570570

571-
- func: ctc_loss(Tensor log_probs, Tensor targets, IntList input_lengths, IntList target_lengths, int64_t blank=0, int64_t reduction=Reduction::ElementwiseMean) -> Tensor
571+
- func: ctc_loss(Tensor log_probs, Tensor targets, IntList input_lengths, IntList target_lengths, int64_t blank=0, int64_t reduction=Reduction::Mean) -> Tensor
572572

573573
# convenience function that converts to intlists for you
574-
- func: ctc_loss(Tensor log_probs, Tensor targets, Tensor input_lengths, Tensor target_lengths, int64_t blank=0, int64_t reduction=Reduction::ElementwiseMean) -> Tensor
574+
- func: ctc_loss(Tensor log_probs, Tensor targets, Tensor input_lengths, Tensor target_lengths, int64_t blank=0, int64_t reduction=Reduction::Mean) -> Tensor
575575

576576
- func: _ctc_loss(Tensor log_probs, Tensor targets, IntList input_lengths, IntList target_lengths, int64_t blank=0) -> (Tensor, Tensor)
577577
dispatch:
@@ -831,7 +831,7 @@
831831

832832
- func: hamming_window(int64_t window_length, bool periodic, double alpha, double beta, TensorOptions options={}) -> Tensor
833833

834-
- func: hinge_embedding_loss(Tensor self, Tensor target, double margin=1.0, int64_t reduction=Reduction::ElementwiseMean) -> Tensor
834+
- func: hinge_embedding_loss(Tensor self, Tensor target, double margin=1.0, int64_t reduction=Reduction::Mean) -> Tensor
835835

836836
- func: ger(Tensor self, Tensor vec2) -> Tensor
837837
variants: function, method
@@ -937,9 +937,9 @@
937937
variants: function, method
938938
device_guard: false
939939

940-
- func: kl_div(Tensor self, Tensor target, int64_t reduction=Reduction::ElementwiseMean) -> Tensor
940+
- func: kl_div(Tensor self, Tensor target, int64_t reduction=Reduction::Mean) -> Tensor
941941

942-
- func: kl_div_backward(Tensor grad_output, Tensor self, Tensor target, int64_t reduction=Reduction::ElementwiseMean) -> Tensor
942+
- func: kl_div_backward(Tensor grad_output, Tensor self, Tensor target, int64_t reduction=Reduction::Mean) -> Tensor
943943
dispatch:
944944
CPU: kl_div_backward_cpu
945945
CUDA: kl_div_backward_cuda
@@ -1054,7 +1054,7 @@
10541054

10551055
- func: logsumexp_out(Tensor result, Tensor self, int64_t dim, bool keepdim=False) -> Tensor
10561056

1057-
- func: margin_ranking_loss(Tensor input1, Tensor input2, Tensor target, double margin=0.0, int64_t reduction=Reduction::ElementwiseMean) -> Tensor
1057+
- func: margin_ranking_loss(Tensor input1, Tensor input2, Tensor target, double margin=0.0, int64_t reduction=Reduction::Mean) -> Tensor
10581058

10591059
- func: matmul(Tensor self, Tensor other) -> Tensor
10601060
variants: function, method
@@ -1699,7 +1699,7 @@
16991699

17001700
- func: _trilinear(Tensor i1, Tensor i2, Tensor i3, IntList expand1, IntList expand2, IntList expand3, IntList sumdim, int64_t unroll_dim=1) -> Tensor
17011701

1702-
- func: triplet_margin_loss(Tensor anchor, Tensor positive, Tensor negative, double margin=1.0, double p=2, double eps=1e-6, bool swap=false, int64_t reduction=Reduction::ElementwiseMean) -> Tensor
1702+
- func: triplet_margin_loss(Tensor anchor, Tensor positive, Tensor negative, double margin=1.0, double p=2, double eps=1e-6, bool swap=false, int64_t reduction=Reduction::Mean) -> Tensor
17031703

17041704
- func: trunc(Tensor self) -> Tensor
17051705
variants: function, method

aten/src/ATen/nn.yaml

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,52 @@
11
# Loss functions
22

3-
- name: binary_cross_entropy(Tensor self, Tensor target, Tensor weight={}, int64_t reduction=Reduction::ElementwiseMean)
3+
- name: binary_cross_entropy(Tensor self, Tensor target, Tensor weight={}, int64_t reduction=Reduction::Mean)
44
cname: BCECriterion
55
scalar_check:
66
output: reduction != Reduction::None || self_->dim() == 0
77

8-
- name: l1_loss(Tensor self, Tensor target, int64_t reduction=Reduction::ElementwiseMean)
8+
- name: l1_loss(Tensor self, Tensor target, int64_t reduction=Reduction::Mean)
99
cname: AbsCriterion
1010
scalar_check:
1111
output: reduction != Reduction::None || self_->dim() == 0
1212

13-
- name: mse_loss(Tensor self, Tensor target, int64_t reduction=Reduction::ElementwiseMean)
13+
- name: mse_loss(Tensor self, Tensor target, int64_t reduction=Reduction::Mean)
1414
cname: MSECriterion
1515
scalar_check:
1616
output: reduction != Reduction::None || self_->dim() == 0
1717

18-
- name: multi_margin_loss(Tensor self, LongTensor target, Scalar p=1, Scalar margin=1, Tensor weight={}, int64_t reduction=Reduction::ElementwiseMean)
18+
- name: multi_margin_loss(Tensor self, LongTensor target, Scalar p=1, Scalar margin=1, Tensor weight={}, int64_t reduction=Reduction::Mean)
1919
cname: MultiMarginCriterion
2020
scalar_check:
2121
output: reduction != Reduction::None || self_->dim() == 0
2222

23-
- name: multilabel_margin_loss(Tensor self, LongTensor target, int64_t reduction=Reduction::ElementwiseMean)
23+
- name: multilabel_margin_loss(Tensor self, LongTensor target, int64_t reduction=Reduction::Mean)
2424
cname: MultiLabelMarginCriterion
2525
buffers: [is_target]
2626
scalar_check:
2727
output: reduction != Reduction::None || self_->dim() == 0
2828
is_target: target_->dim() == 0
2929

30-
- name: nll_loss(Tensor self, LongTensor target, Tensor weight={}, int64_t reduction=Reduction::ElementwiseMean, int64_t ignore_index=-100)
30+
- name: nll_loss(Tensor self, LongTensor target, Tensor weight={}, int64_t reduction=Reduction::Mean, int64_t ignore_index=-100)
3131
cname: ClassNLLCriterion
3232
buffers: [total_weight]
3333
scalar_check:
3434
output: reduction != Reduction::None || self_->dim() == 0
3535
total_weight: 'true'
3636

37-
- name: nll_loss2d(Tensor self, LongTensor target, Tensor weight={}, int64_t reduction=Reduction::ElementwiseMean, int64_t ignore_index=-100)
37+
- name: nll_loss2d(Tensor self, LongTensor target, Tensor weight={}, int64_t reduction=Reduction::Mean, int64_t ignore_index=-100)
3838
cname: SpatialClassNLLCriterion
3939
buffers: [total_weight]
4040
scalar_check:
4141
output: reduction != Reduction::None || self_->dim() == 0
4242
total_weight: 'true'
4343

44-
- name: smooth_l1_loss(Tensor self, Tensor target, int64_t reduction=Reduction::ElementwiseMean)
44+
- name: smooth_l1_loss(Tensor self, Tensor target, int64_t reduction=Reduction::Mean)
4545
cname: SmoothL1Criterion
4646
scalar_check:
4747
output: reduction != Reduction::None || self_->dim() == 0
4848

49-
- name: soft_margin_loss(Tensor self, Tensor target, int64_t reduction=Reduction::ElementwiseMean)
49+
- name: soft_margin_loss(Tensor self, Tensor target, int64_t reduction=Reduction::Mean)
5050
cname: SoftMarginCriterion
5151
scalar_check:
5252
output: reduction != Reduction::None || self_->dim() == 0

aten/src/ATen/test/basic.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ static void test(Type& type) {
271271
CATCH_SECTION("dispatch") {
272272
Tensor tensor = randn({20, 20});
273273
Tensor other = randn({20, 20});
274-
auto result = tensor.m(relu).m(mse_loss, other, Reduction::ElementwiseMean);
274+
auto result = tensor.m(relu).m(mse_loss, other, Reduction::Mean);
275275
CATCH_REQUIRE(result.allclose(mse_loss(relu(tensor), other)));
276276
}
277277
CATCH_SECTION("core") {

aten/src/THCUNN/generic/AbsCriterion.cu

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void THNN_(AbsCriterion_updateOutput)(
3030
thrust::device_ptr<scalar_t> target_data(THCTensor_(data)(state, target));
3131
accreal sum = thrust::inner_product(input_data, input_data+size, target_data, (accreal)0, thrust::plus<accreal>(), abs_functor<scalar_t, accreal>());
3232

33-
if (reduction == Reduction::ElementwiseMean)
33+
if (reduction == Reduction::Mean)
3434
sum /= size;
3535

3636
THCTensor_(free)(state, input);
@@ -63,7 +63,7 @@ void THNN_(AbsCriterion_updateGradInput)(
6363
THCUNN_check_dim_size(state, gradOutput, 1, 0, 1);
6464

6565
ptrdiff_t size = THCTensor_(nElement)(state, input);
66-
scalar_t norm = ScalarConvert<double, scalar_t>::to(reduction == Reduction::ElementwiseMean ? 1./size : 1.);
66+
scalar_t norm = ScalarConvert<double, scalar_t>::to(reduction == Reduction::Mean ? 1./size : 1.);
6767

6868
input = THCTensor_(newContiguous)(state, input);
6969
target = THCTensor_(newContiguous)(state, target);

aten/src/THCUNN/generic/BCECriterion.cu

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void THNN_(BCECriterion_updateOutput)(
5757
);
5858
}
5959

60-
if (reduction == Reduction::ElementwiseMean)
60+
if (reduction == Reduction::Mean)
6161
sum /= size;
6262

6363
THCTensor_(free)(state, input);
@@ -95,7 +95,7 @@ void THNN_(BCECriterion_updateGradInput)(
9595
THCUNN_check_dim_size(state, gradOutput, 1, 0, 1);
9696

9797
ptrdiff_t size = THCTensor_(nElement)(state, input);
98-
scalar_t norm = ScalarConvert<accreal, scalar_t>::to((reduction == Reduction::ElementwiseMean ? accreal(1)/size : accreal(1)) * THCTensor_(get1d)(state, gradOutput, 0));
98+
scalar_t norm = ScalarConvert<accreal, scalar_t>::to((reduction == Reduction::Mean ? accreal(1)/size : accreal(1)) * THCTensor_(get1d)(state, gradOutput, 0));
9999

100100
input = THCTensor_(newContiguous)(state, input);
101101
target = THCTensor_(newContiguous)(state, target);

aten/src/THCUNN/generic/ClassNLLCriterion.cu

+4-4
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void THNN_(ClassNLLCriterion_updateOutput)(
8888
input_data,
8989
target_data,
9090
weights_data,
91-
reduction == Reduction::ElementwiseMean,
91+
reduction == Reduction::Mean,
9292
n_classes,
9393
ignore_index
9494
);
@@ -101,7 +101,7 @@ void THNN_(ClassNLLCriterion_updateOutput)(
101101
input_data,
102102
target_data,
103103
weights_data,
104-
reduction == Reduction::ElementwiseMean,
104+
reduction == Reduction::Mean,
105105
THCTensor_(size)(state, input, 0),
106106
THCTensor_(size)(state, input, 1),
107107
n_classes,
@@ -205,7 +205,7 @@ void THNN_(ClassNLLCriterion_updateGradInput)(
205205
weights_data,
206206
target_data,
207207
total_weight_data,
208-
reduction == Reduction::ElementwiseMean,
208+
reduction == Reduction::Mean,
209209
n_classes,
210210
ignore_index
211211
);
@@ -217,7 +217,7 @@ void THNN_(ClassNLLCriterion_updateGradInput)(
217217
target_data,
218218
weights_data,
219219
total_weight_data,
220-
reduction == Reduction::ElementwiseMean,
220+
reduction == Reduction::Mean,
221221
THCTensor_(size)(state, input, 0),
222222
THCTensor_(size)(state, input, 1),
223223
n_classes,

aten/src/THCUNN/generic/DistKLDivCriterion.cu

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void THNN_(DistKLDivCriterion_updateOutput)(
3535
thrust::device_ptr<scalar_t> target_data(THCTensor_(data)(state, target));
3636
sum = thrust::inner_product(input_data, input_data+size, target_data, (accreal) 0, thrust::plus<accreal>(), kl_functor<scalar_t, accreal>());
3737

38-
if (reduction == Reduction::ElementwiseMean)
38+
if (reduction == Reduction::Mean)
3939
sum /= size;
4040

4141
THCTensor_(free)(state, input);
@@ -70,7 +70,7 @@ void THNN_(DistKLDivCriterion_updateGradInput)(
7070
THCUNN_check_dim_size(state, gradOutput, 1, 0, 1);
7171

7272
ptrdiff_t size = THCTensor_(nElement)(state, input);
73-
scalar_t norm = (reduction == Reduction::ElementwiseMean ? ScalarConvert<accreal, scalar_t>::to(accreal(1)/size) : ScalarConvert<int, scalar_t>::to(1));
73+
scalar_t norm = (reduction == Reduction::Mean ? ScalarConvert<accreal, scalar_t>::to(accreal(1)/size) : ScalarConvert<int, scalar_t>::to(1));
7474

7575
input = THCTensor_(newContiguous)(state, input);
7676
target = THCTensor_(newContiguous)(state, target);

aten/src/THCUNN/generic/MSECriterion.cu

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void THNN_(MSECriterion_updateOutput)(
3030
input_data, input_data+size, target_data, (accreal) 0,
3131
thrust::plus<accreal>(), mse_functor<scalar_t, accreal>());
3232

33-
if (reduction == Reduction::ElementwiseMean)
33+
if (reduction == Reduction::Mean)
3434
sum /= size;
3535

3636
THCTensor_(free)(state, input);
@@ -64,7 +64,7 @@ void THNN_(MSECriterion_updateGradInput)(
6464
ptrdiff_t size = THCTensor_(nElement)(state, input);
6565

6666
THCUNN_check_dim_size(state, gradOutput, 1, 0, 1);
67-
accreal norm = reduction == Reduction::ElementwiseMean ? (accreal)(2)/size : (accreal)(2);
67+
accreal norm = reduction == Reduction::Mean ? (accreal)(2)/size : (accreal)(2);
6868
norm *= ScalarConvert<scalar_t, accreal>::to(THCTensor_(get1d)(state, gradOutput, 0));
6969

7070
input = THCTensor_(newContiguous)(state, input);

aten/src/THCUNN/generic/MultiLabelMarginCriterion.cu

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void THNN_(MultiLabelMarginCriterion_updateOutput)(
3333
THCIndexTensor_(data)(state, target),
3434
THCTensor_(data)(state, istarget),
3535
1, dim,
36-
reduction == Reduction::ElementwiseMean
36+
reduction == Reduction::Mean
3737
);
3838
THCudaCheck(cudaGetLastError());
3939
}
@@ -59,7 +59,7 @@ void THNN_(MultiLabelMarginCriterion_updateOutput)(
5959
THCIndexTensor_(data)(state, target),
6060
THCTensor_(data)(state, istarget),
6161
nframe, dim,
62-
reduction == Reduction::ElementwiseMean
62+
reduction == Reduction::Mean
6363
);
6464
THCudaCheck(cudaGetLastError());
6565
THCTensor_(set1d)(state, output, 0, ScalarConvert<accreal, scalar_t>::to(THCTensor_(sumall)(state, output_tmp)));
@@ -122,7 +122,7 @@ void THNN_(MultiLabelMarginCriterion_updateGradInput)(
122122
THCIndexTensor_(data)(state, target),
123123
THCTensor_(data)(state, istarget),
124124
1, gradInput->size(0),
125-
reduction == Reduction::ElementwiseMean,
125+
reduction == Reduction::Mean,
126126
reduction != Reduction::None);
127127

128128
}
@@ -145,7 +145,7 @@ void THNN_(MultiLabelMarginCriterion_updateGradInput)(
145145
THCIndexTensor_(data)(state, target),
146146
THCTensor_(data)(state, istarget),
147147
gradInput->size(0), gradInput->size(1),
148-
reduction == Reduction::ElementwiseMean,
148+
reduction == Reduction::Mean,
149149
reduction != Reduction::None);
150150
}
151151
else

0 commit comments

Comments
 (0)