Skip to content

Commit fad750a

Browse files
authored
[NPU] Fix dropout bug in downscale_in_infer mode (#1346)
1 parent 6dc996f commit fad750a

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

backends/npu/kernels/dropout_kernel.cc

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -400,14 +400,15 @@ void DropoutRawKernel(const Context& dev_ctx,
400400
EXEC_NPU_CMD(
401401
aclnnDropoutDoMask, dev_ctx, tmp_x, *mask, dropout_prob, tmp_out);
402402
}
403+
404+
if (!is_upscale) {
405+
phi::Scalar revert_scale = static_cast<T>(1.0 - dropout_prob);
406+
EXEC_NPU_CMD(aclnnInplaceMuls, dev_ctx, *out, revert_scale);
407+
}
403408
} else {
404409
if (!is_upscale) {
405-
const auto& muls_runner =
406-
NpuOpRunner("Muls",
407-
{x},
408-
{*out},
409-
{{"value", static_cast<float>(1 - dropout_prob)}});
410-
muls_runner.Run(stream);
410+
phi::Scalar down_scale = static_cast<T>(1.0 - dropout_prob);
411+
EXEC_NPU_CMD(aclnnMuls, dev_ctx, x, down_scale, *out);
411412
return;
412413
}
413414
TensorCopy(dev_ctx, x, false, out);
@@ -531,7 +532,6 @@ void DropoutGradRawKernel(const Context& dev_ctx,
531532
return;
532533
}
533534

534-
dropout_prob = is_upscale ? dropout_prob : 0.0;
535535
if (dx->dtype() == phi::DataType::FLOAT64) {
536536
// transform dx
537537
phi::DenseTensor tmp_dx;
@@ -563,6 +563,11 @@ void DropoutGradRawKernel(const Context& dev_ctx,
563563
} else {
564564
EXEC_NPU_CMD(aclnnDropoutDoMask, dev_ctx, dout, mask, dropout_prob, *dx);
565565
}
566+
567+
if (!is_upscale) {
568+
phi::Scalar revert_scale = static_cast<T>(1.0 - dropout_prob);
569+
EXEC_NPU_CMD(aclnnInplaceMuls, dev_ctx, *dx, revert_scale);
570+
}
566571
return;
567572
}
568573

backends/npu/tests/unittests/test_dropout_op_npu.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,21 @@ def setUp(self):
228228
self.outputs = {"Out": self.inputs["X"]}
229229

230230

231+
@skip_check_grad_ci(reason="For inference, check_grad is not required.")
232+
class TestDropoutOpInferenceDownscale(TestDropoutOpInference):
233+
def setUp(self):
234+
self.op_type = "dropout"
235+
self.set_npu()
236+
self.init_dtype()
237+
self.inputs = {"X": np.random.random((32, 64, 3)).astype(self.dtype)}
238+
self.attrs = {
239+
"dropout_prob": 0.75,
240+
"is_test": True,
241+
"dropout_implementation": "downgrade_in_infer",
242+
}
243+
self.outputs = {"Out": self.inputs["X"] * (1.0 - self.attrs["dropout_prob"])}
244+
245+
231246
class TestDropoutOpWithSeed(TestDropoutOp):
232247
# the seed is a Tensor
233248
def setUp(self):

0 commit comments

Comments
 (0)