From 5d141b3194bdec38b5de43415edb8746fdb2c277 Mon Sep 17 00:00:00 2001 From: lixiang <88304454@qq.com> Date: Mon, 10 Jan 2022 10:05:30 +0800 Subject: [PATCH 1/7] Fix erfinv and swapaxes --- oneflow/core/functional/impl/math_functor.cpp | 3 --- oneflow/user/kernels/erfinv_kernel.cpp | 18 ++++++++++-------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/oneflow/core/functional/impl/math_functor.cpp b/oneflow/core/functional/impl/math_functor.cpp index 3113d23d122..4d54c9fea6b 100644 --- a/oneflow/core/functional/impl/math_functor.cpp +++ b/oneflow/core/functional/impl/math_functor.cpp @@ -648,9 +648,6 @@ class SwapaxesFunctor { << "], but got " << dim_1 << ")"; return Transpose2dim(x, dim0, dim1); } - - private: - std::shared_ptr op_; }; class ArangeFunctor { diff --git a/oneflow/user/kernels/erfinv_kernel.cpp b/oneflow/user/kernels/erfinv_kernel.cpp index 5593ba6a228..5b499d3b2b4 100644 --- a/oneflow/user/kernels/erfinv_kernel.cpp +++ b/oneflow/user/kernels/erfinv_kernel.cpp @@ -52,14 +52,16 @@ class CpuErfinvKernel final : public user_op::OpKernel { dem = (d[1] * z + d[0]) * z + static_cast(1.0); y_ptr[i] = std::copysign(num, x) / dem; } - y_ptr[i] = y_ptr[i] - - (std::erf(y_ptr[i]) - x) - / ((static_cast(2.0) / static_cast(std::sqrt(M_PI))) - * std::exp(-y_ptr[i] * y_ptr[i])); - y_ptr[i] = y_ptr[i] - - (std::erf(y_ptr[i]) - x) - / ((static_cast(2.0) / static_cast(std::sqrt(M_PI))) - * std::exp(-y_ptr[i] * y_ptr[i])); + y_ptr[i] = + y_ptr[i] + - (std::erf(y_ptr[i]) - x) + / ((static_cast(2.0) / static_cast(std::sqrt(static_cast(M_PI)))) + * std::exp(-y_ptr[i] * y_ptr[i])); + y_ptr[i] = + y_ptr[i] + - (std::erf(y_ptr[i]) - x) + / ((static_cast(2.0) / static_cast(std::sqrt(static_cast(M_PI)))) + * std::exp(-y_ptr[i] * y_ptr[i])); } } From 8b3542f6d3cb76e20fb1ea353b4a20f338822c6a Mon Sep 17 00:00:00 2001 From: lixiang <88304454@qq.com> Date: Mon, 10 Jan 2022 10:27:09 +0800 Subject: [PATCH 2/7] Fix --- oneflow/user/kernels/erfinv_kernel.cpp | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/oneflow/user/kernels/erfinv_kernel.cpp b/oneflow/user/kernels/erfinv_kernel.cpp index 5b499d3b2b4..57c4202103c 100644 --- a/oneflow/user/kernels/erfinv_kernel.cpp +++ b/oneflow/user/kernels/erfinv_kernel.cpp @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ #include "oneflow/core/framework/framework.h" -#include +#define PI 3.14159265358979323846L namespace oneflow { template @@ -52,16 +52,14 @@ class CpuErfinvKernel final : public user_op::OpKernel { dem = (d[1] * z + d[0]) * z + static_cast(1.0); y_ptr[i] = std::copysign(num, x) / dem; } - y_ptr[i] = - y_ptr[i] - - (std::erf(y_ptr[i]) - x) - / ((static_cast(2.0) / static_cast(std::sqrt(static_cast(M_PI)))) - * std::exp(-y_ptr[i] * y_ptr[i])); - y_ptr[i] = - y_ptr[i] - - (std::erf(y_ptr[i]) - x) - / ((static_cast(2.0) / static_cast(std::sqrt(static_cast(M_PI)))) - * std::exp(-y_ptr[i] * y_ptr[i])); + y_ptr[i] = y_ptr[i] + - (std::erf(y_ptr[i]) - x) + / ((static_cast(2.0) / static_cast(std::sqrt(static_cast(PI)))) + * std::exp(-y_ptr[i] * y_ptr[i])); + y_ptr[i] = y_ptr[i] + - (std::erf(y_ptr[i]) - x) + / ((static_cast(2.0) / static_cast(std::sqrt(static_cast(PI)))) + * std::exp(-y_ptr[i] * y_ptr[i])); } } From 75fcc2eced5e59d0fe54c482b594dd3d1708f58c Mon Sep 17 00:00:00 2001 From: lixiang <88304454@qq.com> Date: Mon, 10 Jan 2022 19:14:55 +0800 Subject: [PATCH 3/7] Fix bug and add test --- oneflow/user/kernels/erfinv_kernel.cpp | 10 ++++++-- python/oneflow/test/modules/test_erfinv.py | 27 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/oneflow/user/kernels/erfinv_kernel.cpp b/oneflow/user/kernels/erfinv_kernel.cpp index 57c4202103c..23e2e2100a7 100644 --- a/oneflow/user/kernels/erfinv_kernel.cpp +++ b/oneflow/user/kernels/erfinv_kernel.cpp @@ -39,8 +39,14 @@ class CpuErfinvKernel final : public user_op::OpKernel { T z, num, dem; T x = x_ptr[i]; // Promise the correctness of inplace version. T x_abs = std::abs(x); - if (x_abs > 1.0) y_ptr[i] = std::numeric_limits::quiet_NaN(); - if (x_abs == 1.0) y_ptr[i] = std::copysign(std::numeric_limits::infinity(), x); + if (x_abs > 1.0) { + y_ptr[i] = std::numeric_limits::quiet_NaN(); + continue; + } + if (x_abs == 1.0) { + y_ptr[i] = std::copysign(std::numeric_limits::infinity(), x); + continue; + } if (x_abs <= static_cast(central_range)) { z = x * x; num = (((a[3] * z + a[2]) * z + a[1]) * z + a[0]); diff --git a/python/oneflow/test/modules/test_erfinv.py b/python/oneflow/test/modules/test_erfinv.py index da01255481c..5c96d4a1e84 100644 --- a/python/oneflow/test/modules/test_erfinv.py +++ b/python/oneflow/test/modules/test_erfinv.py @@ -27,8 +27,35 @@ from oneflow.test_utils.automated_test_util import * +def _test_flow_erfinv_with_1_data(test_case, device): + x = flow.tensor(np.ones((5, 5)), dtype=flow.float32, device=flow.device(device)) + of_out = flow.erfinv(x) + np_out = np.full((5, 5), np.inf) + test_case.assertTrue(np.array_equal(of_out.numpy(), np_out)) + + +def _test_flow_erfinv_with_gt_1_data(test_case, device): + x = flow.tensor( + np.arange(2, 22).reshape(4, 5), dtype=flow.float32, device=flow.device(device) + ) + np_out = np.full((4, 5), np.nan) + of_out = ",".join(str(i) for i in flow.erfinv(x).numpy()) + np_out = ",".join(str(i) for i in np_out) + test_case.assertEqual(of_out, np_out) + + @flow.unittest.skip_unless_1n1d() class TestErfinvModule(flow.unittest.TestCase): + def test_flow_erfinv(test_case): + arg_dict = OrderedDict() + arg_dict["test_fun"] = [ + _test_flow_erfinv_with_1_data, + _test_flow_erfinv_with_gt_1_data, + ] + arg_dict["device"] = ["cpu", "cuda"] + for arg in GenArgList(arg_dict): + arg[0](test_case, *arg[1:]) + @autotest(check_graph=True, auto_backward=False) def test_flow_erfinv_with_random_data(test_case): device = random_device() From 45986a3641cd80e5ba77c6cf85ae533d9c688aaf Mon Sep 17 00:00:00 2001 From: lixiang <88304454@qq.com> Date: Mon, 10 Jan 2022 19:29:00 +0800 Subject: [PATCH 4/7] Modify name --- python/oneflow/test/modules/test_erfinv.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/python/oneflow/test/modules/test_erfinv.py b/python/oneflow/test/modules/test_erfinv.py index 5c96d4a1e84..1b65892bc34 100644 --- a/python/oneflow/test/modules/test_erfinv.py +++ b/python/oneflow/test/modules/test_erfinv.py @@ -27,14 +27,14 @@ from oneflow.test_utils.automated_test_util import * -def _test_flow_erfinv_with_1_data(test_case, device): +def _test_flow_erfinv_with_inf_data(test_case, device): x = flow.tensor(np.ones((5, 5)), dtype=flow.float32, device=flow.device(device)) of_out = flow.erfinv(x) np_out = np.full((5, 5), np.inf) test_case.assertTrue(np.array_equal(of_out.numpy(), np_out)) -def _test_flow_erfinv_with_gt_1_data(test_case, device): +def _test_flow_erfinv_with_nan_data(test_case, device): x = flow.tensor( np.arange(2, 22).reshape(4, 5), dtype=flow.float32, device=flow.device(device) ) @@ -49,8 +49,8 @@ class TestErfinvModule(flow.unittest.TestCase): def test_flow_erfinv(test_case): arg_dict = OrderedDict() arg_dict["test_fun"] = [ - _test_flow_erfinv_with_1_data, - _test_flow_erfinv_with_gt_1_data, + _test_flow_erfinv_with_inf_data, + _test_flow_erfinv_with_nan_data, ] arg_dict["device"] = ["cpu", "cuda"] for arg in GenArgList(arg_dict): From f76a57be595c8901d68eb41f64852e3eea7b8fe4 Mon Sep 17 00:00:00 2001 From: lixiang <88304454@qq.com> Date: Mon, 10 Jan 2022 19:45:09 +0800 Subject: [PATCH 5/7] Fix arg --- python/oneflow/test/modules/test_erfinv.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/python/oneflow/test/modules/test_erfinv.py b/python/oneflow/test/modules/test_erfinv.py index 1b65892bc34..8d6d8d85972 100644 --- a/python/oneflow/test/modules/test_erfinv.py +++ b/python/oneflow/test/modules/test_erfinv.py @@ -38,10 +38,9 @@ def _test_flow_erfinv_with_nan_data(test_case, device): x = flow.tensor( np.arange(2, 22).reshape(4, 5), dtype=flow.float32, device=flow.device(device) ) + of_out = flow.erfinv(x) np_out = np.full((4, 5), np.nan) - of_out = ",".join(str(i) for i in flow.erfinv(x).numpy()) - np_out = ",".join(str(i) for i in np_out) - test_case.assertEqual(of_out, np_out) + test_case.assertTrue(np.array_equal(of_out.numpy(), np_out, equal_nan=True)) @flow.unittest.skip_unless_1n1d() From 01b4f47ee99dfca737354dabe39d71e396bb7f00 Mon Sep 17 00:00:00 2001 From: lixiang <88304454@qq.com> Date: Tue, 11 Jan 2022 10:57:53 +0800 Subject: [PATCH 6/7] Modify pi --- oneflow/user/kernels/erfinv_kernel.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/oneflow/user/kernels/erfinv_kernel.cpp b/oneflow/user/kernels/erfinv_kernel.cpp index 23e2e2100a7..3e9d402be2d 100644 --- a/oneflow/user/kernels/erfinv_kernel.cpp +++ b/oneflow/user/kernels/erfinv_kernel.cpp @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ #include "oneflow/core/framework/framework.h" -#define PI 3.14159265358979323846L +#include namespace oneflow { template @@ -60,11 +60,11 @@ class CpuErfinvKernel final : public user_op::OpKernel { } y_ptr[i] = y_ptr[i] - (std::erf(y_ptr[i]) - x) - / ((static_cast(2.0) / static_cast(std::sqrt(static_cast(PI)))) + / ((static_cast(2.0) / static_cast(std::sqrt(M_PI))) * std::exp(-y_ptr[i] * y_ptr[i])); y_ptr[i] = y_ptr[i] - (std::erf(y_ptr[i]) - x) - / ((static_cast(2.0) / static_cast(std::sqrt(static_cast(PI)))) + / ((static_cast(2.0) / static_cast(std::sqrt(M_PI))) * std::exp(-y_ptr[i] * y_ptr[i])); } } From be5b25451f4c6f6183b454ab5fb0fea6cb06c382 Mon Sep 17 00:00:00 2001 From: lixiang <88304454@qq.com> Date: Tue, 11 Jan 2022 15:04:51 +0800 Subject: [PATCH 7/7] Fix --- oneflow/user/kernels/erfinv_kernel.cpp | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/oneflow/user/kernels/erfinv_kernel.cpp b/oneflow/user/kernels/erfinv_kernel.cpp index 3e9d402be2d..dffa9372146 100644 --- a/oneflow/user/kernels/erfinv_kernel.cpp +++ b/oneflow/user/kernels/erfinv_kernel.cpp @@ -31,6 +31,7 @@ class CpuErfinvKernel final : public user_op::OpKernel { const T* x_ptr = x->dptr(); T* y_ptr = y->mut_dptr(); constexpr float central_range = 0.7; + const T temp = static_cast(2.0) / static_cast(std::sqrt(M_PI)); T a[4] = {T(0.886226899), T(-1.645349621), T(0.914624893), T(-0.140543331)}; T b[4] = {T(-2.118377725), T(1.442710462), T(-0.329097515), T(0.012229801)}; T c[4] = {T(-1.970840454), T(-1.624906493), T(3.429567803), T(1.641345311)}; @@ -58,14 +59,8 @@ class CpuErfinvKernel final : public user_op::OpKernel { dem = (d[1] * z + d[0]) * z + static_cast(1.0); y_ptr[i] = std::copysign(num, x) / dem; } - y_ptr[i] = y_ptr[i] - - (std::erf(y_ptr[i]) - x) - / ((static_cast(2.0) / static_cast(std::sqrt(M_PI))) - * std::exp(-y_ptr[i] * y_ptr[i])); - y_ptr[i] = y_ptr[i] - - (std::erf(y_ptr[i]) - x) - / ((static_cast(2.0) / static_cast(std::sqrt(M_PI))) - * std::exp(-y_ptr[i] * y_ptr[i])); + y_ptr[i] = y_ptr[i] - (std::erf(y_ptr[i]) - x) / (temp * std::exp(-y_ptr[i] * y_ptr[i])); + y_ptr[i] = y_ptr[i] - (std::erf(y_ptr[i]) - x) / (temp * std::exp(-y_ptr[i] * y_ptr[i])); } }