From 1991a89f4001a373d045ac56a838864d2a89d8ec Mon Sep 17 00:00:00 2001 From: wilfChen Date: Fri, 15 May 2020 11:16:30 +0800 Subject: [PATCH] LayerNormGrad fix & codex --- .../gpu/arrays/unsorted_segment_sum_gpu_kernel.cc | 1 - mindspore/ccsrc/kernel/gpu/data/dataset_utils.cc | 2 -- mindspore/ccsrc/kernel/gpu/data/dataset_utils.h | 1 - .../ccsrc/kernel/gpu/math/broadcast_gpu_kernel.h | 13 ++++++------- .../kernel/gpu/math/broadcast_grad_gpu_kernel.h | 2 -- .../kernel/gpu/nn/layer_norm_grad_gpu_kernel.h | 4 ++-- mindspore/ccsrc/kernel/gpu/nn/rmsprop_gpu_kernel.cc | 1 - tests/st/ops/gpu/test_layer_norm_grad_op.py | 8 ++++---- 8 files changed, 12 insertions(+), 20 deletions(-) diff --git a/mindspore/ccsrc/kernel/gpu/arrays/unsorted_segment_sum_gpu_kernel.cc b/mindspore/ccsrc/kernel/gpu/arrays/unsorted_segment_sum_gpu_kernel.cc index 283512c77d3..9962d559882 100644 --- a/mindspore/ccsrc/kernel/gpu/arrays/unsorted_segment_sum_gpu_kernel.cc +++ b/mindspore/ccsrc/kernel/gpu/arrays/unsorted_segment_sum_gpu_kernel.cc @@ -37,6 +37,5 @@ MS_REG_GPU_KERNEL_TWO( UnsortedSegmentSum, KernelAttr().AddInputAttr(kNumberTypeInt32).AddInputAttr(kNumberTypeInt64).AddOutputAttr(kNumberTypeInt32), UnsortedSegmentSumGpuKernel, int, int64_t) - } // namespace kernel } // namespace mindspore diff --git a/mindspore/ccsrc/kernel/gpu/data/dataset_utils.cc b/mindspore/ccsrc/kernel/gpu/data/dataset_utils.cc index 42c322069ff..846a63f84fd 100644 --- a/mindspore/ccsrc/kernel/gpu/data/dataset_utils.cc +++ b/mindspore/ccsrc/kernel/gpu/data/dataset_utils.cc @@ -18,7 +18,6 @@ namespace mindspore { namespace kernel { - size_t UnitSizeInBytes(const mindspore::TypeId &t) { size_t bytes = 0; switch (t) { @@ -65,6 +64,5 @@ int ElementNums(const std::vector &shape) { return nums; } - } // namespace kernel } // namespace mindspore diff --git a/mindspore/ccsrc/kernel/gpu/data/dataset_utils.h b/mindspore/ccsrc/kernel/gpu/data/dataset_utils.h index 7fe7c7ee1d2..b52f79d6f3c 100644 --- a/mindspore/ccsrc/kernel/gpu/data/dataset_utils.h +++ b/mindspore/ccsrc/kernel/gpu/data/dataset_utils.h @@ -21,7 +21,6 @@ #include "ir/dtype/type.h" namespace mindspore { namespace kernel { - size_t UnitSizeInBytes(const mindspore::TypeId &t); int ElementNums(const std::vector &shape); } // namespace kernel diff --git a/mindspore/ccsrc/kernel/gpu/math/broadcast_gpu_kernel.h b/mindspore/ccsrc/kernel/gpu/math/broadcast_gpu_kernel.h index f173b48ee6c..1b597cea70f 100644 --- a/mindspore/ccsrc/kernel/gpu/math/broadcast_gpu_kernel.h +++ b/mindspore/ccsrc/kernel/gpu/math/broadcast_gpu_kernel.h @@ -27,7 +27,6 @@ #include "kernel/gpu/kernel_constants.h" namespace mindspore { namespace kernel { - template class BroadcastOpGpuKernel : public GpuKernel { public: @@ -70,14 +69,14 @@ class BroadcastOpGpuKernel : public GpuKernel { output_num_ *= shape3[i]; } int offset = shape3.size() - shape1.size(); - for (size_t i = 0; i < shape1.size(); i++) { - lhs_shape_[i + offset] = shape1[i]; - input1_num_ *= shape1[i]; + for (size_t j = 0; j < shape1.size(); j++) { + lhs_shape_[j + offset] = shape1[j]; + input1_num_ *= shape1[j]; } offset = shape3.size() - shape2.size(); - for (size_t i = 0; i < shape2.size(); i++) { - rhs_shape_[i + offset] = shape2[i]; - input2_num_ *= shape2[i]; + for (size_t k = 0; k < shape2.size(); k++) { + rhs_shape_[k + offset] = shape2[k]; + input2_num_ *= shape2[k]; } InitSizeLists(); diff --git a/mindspore/ccsrc/kernel/gpu/math/broadcast_grad_gpu_kernel.h b/mindspore/ccsrc/kernel/gpu/math/broadcast_grad_gpu_kernel.h index c3e974b4d6b..2c15e688293 100644 --- a/mindspore/ccsrc/kernel/gpu/math/broadcast_grad_gpu_kernel.h +++ b/mindspore/ccsrc/kernel/gpu/math/broadcast_grad_gpu_kernel.h @@ -1,4 +1,3 @@ - /** * Copyright 2020 Huawei Technologies Co., Ltd * @@ -28,7 +27,6 @@ #include "kernel/gpu/kernel_constants.h" namespace mindspore { namespace kernel { - template class BroadcastOpGradGpuKernel : public GpuKernel { public: diff --git a/mindspore/ccsrc/kernel/gpu/nn/layer_norm_grad_gpu_kernel.h b/mindspore/ccsrc/kernel/gpu/nn/layer_norm_grad_gpu_kernel.h index 1f0ffc14a4a..72cfc23266f 100644 --- a/mindspore/ccsrc/kernel/gpu/nn/layer_norm_grad_gpu_kernel.h +++ b/mindspore/ccsrc/kernel/gpu/nn/layer_norm_grad_gpu_kernel.h @@ -36,8 +36,8 @@ class LayerNormGradGpuKernel : public GpuKernel { bool Launch(const std::vector &inputs, const std::vector &, const std::vector &outputs, uintptr_t stream_ptr) override { - auto dy = GetDeviceAddress(inputs, 0); - auto x = GetDeviceAddress(inputs, 1); + auto x = GetDeviceAddress(inputs, 0); + auto dy = GetDeviceAddress(inputs, 1); auto var = GetDeviceAddress(inputs, 2); auto mean = GetDeviceAddress(inputs, 3); auto gamma = GetDeviceAddress(inputs, 4); diff --git a/mindspore/ccsrc/kernel/gpu/nn/rmsprop_gpu_kernel.cc b/mindspore/ccsrc/kernel/gpu/nn/rmsprop_gpu_kernel.cc index 85aabe57565..707aa776470 100644 --- a/mindspore/ccsrc/kernel/gpu/nn/rmsprop_gpu_kernel.cc +++ b/mindspore/ccsrc/kernel/gpu/nn/rmsprop_gpu_kernel.cc @@ -44,6 +44,5 @@ MS_REG_GPU_KERNEL_ONE(ApplyCenteredRMSProp, .AddInputAttr(kNumberTypeFloat32) .AddOutputAttr(kNumberTypeFloat32), RMSPropGpuKernel, float) - } // namespace kernel } // namespace mindspore diff --git a/tests/st/ops/gpu/test_layer_norm_grad_op.py b/tests/st/ops/gpu/test_layer_norm_grad_op.py index 0cef113d7cd..8b6bf6018ee 100644 --- a/tests/st/ops/gpu/test_layer_norm_grad_op.py +++ b/tests/st/ops/gpu/test_layer_norm_grad_op.py @@ -80,7 +80,7 @@ def test_layernormgrad0(): gamma_ms = Tensor(gamma_np) net = LayerNormGradNet(begin_norm_axis, begin_params_axis) - dx_ms, dg_ms, db_ms = net(dy_ms, x_ms, var_ms, mean_ms, gamma_ms) + dx_ms, dg_ms, db_ms = net(x_ms, dy_ms, var_ms, mean_ms, gamma_ms) assert np.allclose(dx_ms.asnumpy(), dx_np, rtol=1e-6, atol=1e-6) assert np.allclose(dg_ms.asnumpy(), dg_np, rtol=1e-6, atol=1e-3) @@ -107,7 +107,7 @@ def test_layernormgrad1(): gamma_ms = Tensor(gamma_np) net = LayerNormGradNet(begin_norm_axis, begin_params_axis) - dx_ms, dg_ms, db_ms = net(dy_ms, x_ms, var_ms, mean_ms, gamma_ms) + dx_ms, dg_ms, db_ms = net(x_ms, dy_ms, var_ms, mean_ms, gamma_ms) assert np.allclose(dx_ms.asnumpy(), dx_np, rtol=1e-6, atol=1e-6) assert np.allclose(dg_ms.asnumpy(), dg_np, rtol=1e-6, atol=1e-3) @@ -133,8 +133,8 @@ def test_layernormgrad2(): gamma_ms = Tensor(gamma_np) net = LayerNormGradNet(begin_norm_axis, begin_params_axis) - dx_ms, dg_ms, db_ms = net(dy_ms, x_ms, var_ms, mean_ms, gamma_ms) + dx_ms, dg_ms, db_ms = net(x_ms, dy_ms, var_ms, mean_ms, gamma_ms) assert np.allclose(dx_ms.asnumpy(), dx_np, rtol=1e-6, atol=1e-6) assert np.allclose(dg_ms.asnumpy(), dg_np, rtol=1e-6, atol=1e-3) - assert np.allclose(db_ms.asnumpy(), db_np, rtol=1e-6, atol=1e-3) \ No newline at end of file + assert np.allclose(db_ms.asnumpy(), db_np, rtol=1e-6, atol=1e-3)