From 0f578db968ae319a58cd395510111856f9864fec Mon Sep 17 00:00:00 2001 From: Leo Chen Date: Thu, 29 Apr 2021 20:47:24 +0800 Subject: [PATCH] [NPU] refine FillNpuTensorWithConstant (#32682) --- paddle/fluid/operators/npu_op_runner.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/paddle/fluid/operators/npu_op_runner.h b/paddle/fluid/operators/npu_op_runner.h index 5506ddd896..cfc933c7a7 100644 --- a/paddle/fluid/operators/npu_op_runner.h +++ b/paddle/fluid/operators/npu_op_runner.h @@ -90,6 +90,9 @@ aclrtStream GetCurrentNPUStream(int device_id = -1); template void FillNpuTensorWithConstant(Tensor *tensor, T val) { + // NOTE(zhiqiu): we found that power sometimes returns 0 when val is small + // like 1e-8. + constexpr float MIN_PRECISION_FOR_POWER = 1e-3; PADDLE_ENFORCE_EQ( tensor->IsInitialized(), true, platform::errors::InvalidArgument("The tensor should be initialized.")); @@ -97,7 +100,8 @@ void FillNpuTensorWithConstant(Tensor *tensor, T val) { platform::is_npu_place(tensor->place()), true, platform::errors::InvalidArgument("The tensor should be on NPUPlace.")); // do async for better performance - if (typeid(float) == typeid(T) || typeid(platform::float16) == typeid(T)) { + if ((typeid(float) == typeid(T) || typeid(platform::float16) == typeid(T)) && + static_cast(val) > MIN_PRECISION_FOR_POWER) { Tensor tmp(tensor->type()); tmp.Resize(tensor->dims()); tmp.mutable_data(tensor->place()); -- GitLab