From 7689b6aaa4c816ec15eb1e3683b72bed06a3064e Mon Sep 17 00:00:00 2001 From: Guo Sheng Date: Mon, 23 Dec 2019 21:00:25 +0800 Subject: [PATCH] Fix default label dim of label_smooth_op. test=develop (#21862) --- paddle/fluid/operators/label_smooth_op.cc | 2 +- paddle/fluid/operators/label_smooth_op.cu | 4 ++-- paddle/fluid/operators/label_smooth_op.h | 4 ++-- .../tests/unittests/test_label_smooth_op.py | 18 ++++++++++++++++++ 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/paddle/fluid/operators/label_smooth_op.cc b/paddle/fluid/operators/label_smooth_op.cc index b76ccbcfce..f47233b6e1 100644 --- a/paddle/fluid/operators/label_smooth_op.cc +++ b/paddle/fluid/operators/label_smooth_op.cc @@ -37,7 +37,7 @@ class LabelSmoothOp : public framework::OperatorWithKernel { auto noise_dims = ctx->GetInputDim("PriorDist"); auto noise_numel = paddle::framework::product(noise_dims); PADDLE_ENFORCE( - in_dims[1] == noise_numel, + in_dims[in_dims.size() - 1] == noise_numel, "The number of elements in Input(PriorDist) must be equal to the " "dimension of each label."); } diff --git a/paddle/fluid/operators/label_smooth_op.cu b/paddle/fluid/operators/label_smooth_op.cu index 89f1d28e99..33ae35a81f 100644 --- a/paddle/fluid/operators/label_smooth_op.cu +++ b/paddle/fluid/operators/label_smooth_op.cu @@ -34,7 +34,7 @@ __global__ void LabelSmoothRunDistKernel(const int N, const float epsilon, const T* dist_data, T* dst) { int idx = blockDim.x * blockIdx.x + threadIdx.x; for (; idx < N; idx += blockDim.x * gridDim.x) { - int dist_idx = idx - (idx / dist_numel) * dist_numel; + int dist_idx = idx % dist_numel; dst[idx] = static_cast(1 - epsilon) * src[idx] + static_cast(epsilon) * dist_data[dist_idx]; } @@ -56,7 +56,7 @@ class LabelSmoothGPUKernel : public framework::OpKernel { auto* out_t = ctx.Output("Out"); auto* in_t = ctx.Input("X"); auto* dist_t = ctx.Input("PriorDist"); - auto label_dim = in_t->dims()[1]; + auto label_dim = in_t->dims()[in_t->dims().size() - 1]; auto epsilon = ctx.Attr("epsilon"); auto& dev = *ctx.template device_context().eigen_device(); auto size_prob = in_t->numel(); diff --git a/paddle/fluid/operators/label_smooth_op.h b/paddle/fluid/operators/label_smooth_op.h index f3da17de01..760d542505 100644 --- a/paddle/fluid/operators/label_smooth_op.h +++ b/paddle/fluid/operators/label_smooth_op.h @@ -27,7 +27,7 @@ class LabelSmoothKernel : public framework::OpKernel { auto* out_t = ctx.Output("Out"); auto* in_t = ctx.Input("X"); auto* dist_t = ctx.Input("PriorDist"); - auto label_dim = in_t->dims()[1]; + auto label_dim = in_t->dims()[in_t->dims().size() - 1]; out_t->mutable_data(ctx.GetPlace()); auto epsilon = ctx.Attr("epsilon"); @@ -39,7 +39,7 @@ class LabelSmoothKernel : public framework::OpKernel { out.device(dev) = static_cast(1 - epsilon) * in + static_cast(epsilon) * - dist.broadcast(Eigen::DSizes(in_t->numel())); + dist.broadcast(Eigen::DSizes(in_t->numel() / label_dim)); } else { out.device(dev) = static_cast(1 - epsilon) * in + static_cast(epsilon / label_dim); diff --git a/python/paddle/fluid/tests/unittests/test_label_smooth_op.py b/python/paddle/fluid/tests/unittests/test_label_smooth_op.py index 0103b86491..222e1321fe 100644 --- a/python/paddle/fluid/tests/unittests/test_label_smooth_op.py +++ b/python/paddle/fluid/tests/unittests/test_label_smooth_op.py @@ -53,5 +53,23 @@ class TestLabelSmoothOpWithPriorDist(TestLabelSmoothOp): self.outputs = {'Out': smoothed_label} +class TestLabelSmoothOp3D(TestLabelSmoothOp): + def setUp(self): + super(TestLabelSmoothOp3D, self).setUp() + self.inputs['X'] = self.inputs['X'].reshape( + [2, -1, self.inputs['X'].shape[-1]]) + self.outputs['Out'] = self.outputs['Out'].reshape(self.inputs['X'] + .shape) + + +class TestLabelSmoothOpWithPriorDist3D(TestLabelSmoothOpWithPriorDist): + def setUp(self): + super(TestLabelSmoothOpWithPriorDist3D, self).setUp() + self.inputs['X'] = self.inputs['X'].reshape( + [2, -1, self.inputs['X'].shape[-1]]) + self.outputs['Out'] = self.outputs['Out'].reshape(self.inputs['X'] + .shape) + + if __name__ == '__main__': unittest.main() -- GitLab