Fix the nan bug when passing all zero values into clip_by_norm_op. (#30777)

revert-31068-fix_conv3d_windows
Zhen Wang 4 years ago committed by GitHub
parent 3858f458ea
commit 53d01afed6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -81,7 +81,12 @@ class ClipByNormKernel : public framework::OpKernel<T> {
*context.template device_context<DeviceContext>().eigen_device();
auto temp = (x_norm <= max_norm).template cast<T>();
auto scaling = temp + (static_cast<T>(1) - temp) * max_norm / x_norm;
auto epsilon =
((x_norm <= static_cast<T>(1e-30)).all().template cast<T>()) *
static_cast<T>(1e-6);
auto scaling =
temp + (static_cast<T>(1) - temp) * max_norm / (x_norm + epsilon);
Eigen::array<int, 1> one_dim{{1}};
Eigen::DSizes<int, 1> m_dsize(input->numel());
if (context.GetPlace() == platform::CPUPlace()) {

Loading…
Cancel
Save