From 9bf7003902c9eacf5e2f1f7a5d039c08fd66cb0c Mon Sep 17 00:00:00 2001 From: hong <43953930+phlrain@users.noreply.github.com> Date: Fri, 17 Jul 2020 17:18:31 +0800 Subject: [PATCH] fix softmax with cross entropy out of bound; test=develop (#25549) (#25567) * fix softmax with cross entropy out of bound; test=develop (#25549) * fix index error; test=develop --- paddle/fluid/operators/softmax_with_cross_entropy_op.cu | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/paddle/fluid/operators/softmax_with_cross_entropy_op.cu b/paddle/fluid/operators/softmax_with_cross_entropy_op.cu index dbda4b9b7e..80837bb3c3 100644 --- a/paddle/fluid/operators/softmax_with_cross_entropy_op.cu +++ b/paddle/fluid/operators/softmax_with_cross_entropy_op.cu @@ -28,9 +28,11 @@ __global__ void CrossEntropyGrad(T* logit_grad, const int64_t* labels, i += blockDim.x * gridDim.x) { int idx_n = i / remain; int idx_remain = i % remain; - int idx = idx_n * d + labels[i] * remain + idx_remain; - logit_grad[idx] -= - ignore_index == labels[i] ? static_cast(0.) : static_cast(1.); + int tmp = labels[i]; + if (ignore_index != tmp) { + int idx = idx_n * d + tmp * remain + idx_remain; + logit_grad[idx] -= static_cast(1.); + } } } -- GitLab