From b6ca73331a61d60b6724f539c8dfc7c5242831de Mon Sep 17 00:00:00 2001 From: yangxiaoyu14 Date: Tue, 28 Nov 2023 08:08:39 +0000 Subject: [PATCH 1/2] softmax_test --- op_acc_stable_run.py | 5 +++-- tests/softmax_test.py | 13 +++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/op_acc_stable_run.py b/op_acc_stable_run.py index 4f6d2b9..179daa2 100644 --- a/op_acc_stable_run.py +++ b/op_acc_stable_run.py @@ -1,3 +1,4 @@ + # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -95,7 +96,7 @@ def check_tensor_aadiff(x, y): if x.dtype == paddle.bfloat16: x = x.astype(paddle.float32) y = y.astype(paddle.float32) - assert paddle.max(paddle.abs(x - y)).numpy()[0] == 0, "aadiff check failed" + assert paddle.max(paddle.abs(x - y)).numpy() == 0, "aadiff check failed" def check_aadiff(x, y): @@ -227,4 +228,4 @@ def check_tensor_diff(x, y, *, atol, rtol, err_msg=""): y = y.astype(paddle.float32) x = x.numpy() y = y.numpy() - np.testing.assert_allclose(x, y, atol=atol, rtol=rtol, err_msg=err_msg) + np.testing.assert_allclose(x, y, atol=atol, rtol=rtol, err_msg=err_msg) \ No newline at end of file diff --git a/tests/softmax_test.py b/tests/softmax_test.py index 3da9663..baaa5be 100644 --- a/tests/softmax_test.py +++ b/tests/softmax_test.py @@ -15,12 +15,14 @@ from op_acc_stable_run import check_tensor_diff, op_acc_stable_run - class SoftmaxTest: + def __init__(self, shape, axis, dtype): + self.shape = shape + self.axis = axis + self.dtype = dtype + def set_configs(self, paddle): - self.shape = [4096, 128] - self.dtype = "float32" - self.axis = -1 + self.tmp_cache_path = "." self.inputs = { "x": paddle.randn(self.shape, dtype=self.dtype), "y_grad": paddle.randn(self.shape, dtype=self.dtype), @@ -43,6 +45,5 @@ def check_diff(self, paddle, pd_ret, th_ret): for pd, th in zip(pd_ret, th_ret): check_tensor_diff(pd, th, atol=1e-6, rtol=1e-6) - if __name__ == "__main__": - op_acc_stable_run(SoftmaxTest) + op_acc_stable_run(SoftmaxTest(shape = [1, 1024, 254208], axis=-1, dtype ='float32')) From e0a8d04bf855569e641e4b17d3ee877c4e133389 Mon Sep 17 00:00:00 2001 From: yangxiaoyu14 Date: Wed, 29 Nov 2023 12:27:32 +0000 Subject: [PATCH 2/2] sum_test --- op_acc_stable_run.py | 3 +-- tests/softmax_test.py | 13 ++++++------ tests/sum_test.py | 48 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 9 deletions(-) create mode 100644 tests/sum_test.py diff --git a/op_acc_stable_run.py b/op_acc_stable_run.py index 179daa2..bbe3aaa 100644 --- a/op_acc_stable_run.py +++ b/op_acc_stable_run.py @@ -1,4 +1,3 @@ - # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -96,7 +95,7 @@ def check_tensor_aadiff(x, y): if x.dtype == paddle.bfloat16: x = x.astype(paddle.float32) y = y.astype(paddle.float32) - assert paddle.max(paddle.abs(x - y)).numpy() == 0, "aadiff check failed" + assert paddle.max(paddle.abs(x - y)).numpy()[0] == 0, "aadiff check failed" def check_aadiff(x, y): diff --git a/tests/softmax_test.py b/tests/softmax_test.py index baaa5be..6728cca 100644 --- a/tests/softmax_test.py +++ b/tests/softmax_test.py @@ -15,14 +15,12 @@ from op_acc_stable_run import check_tensor_diff, op_acc_stable_run -class SoftmaxTest: - def __init__(self, shape, axis, dtype): - self.shape = shape - self.axis = axis - self.dtype = dtype +class SoftmaxTest: def set_configs(self, paddle): - self.tmp_cache_path = "." + self.shape = [4096, 128] + self.dtype = "float32" + self.axis = -1 self.inputs = { "x": paddle.randn(self.shape, dtype=self.dtype), "y_grad": paddle.randn(self.shape, dtype=self.dtype), @@ -45,5 +43,6 @@ def check_diff(self, paddle, pd_ret, th_ret): for pd, th in zip(pd_ret, th_ret): check_tensor_diff(pd, th, atol=1e-6, rtol=1e-6) + if __name__ == "__main__": - op_acc_stable_run(SoftmaxTest(shape = [1, 1024, 254208], axis=-1, dtype ='float32')) + op_acc_stable_run(SoftmaxTest) \ No newline at end of file diff --git a/tests/sum_test.py b/tests/sum_test.py new file mode 100644 index 0000000..c406ead --- /dev/null +++ b/tests/sum_test.py @@ -0,0 +1,48 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +from op_acc_stable_run import check_tensor_diff, op_acc_stable_run + +class SumTest: + def __init__(self, x_shape, dtype, axis, keepdim): + self.x_shape = x_shape + self.dtype = dtype + self.axis = axis + self.keepdim = keepdim + + + def set_configs(self, paddle): + self.tmp_cache_path = "." + self.inputs = { + "x": paddle.randn(self.x_shape, dtype=self.dtype) , + } + + def run_paddle(self, paddle): + x = self.inputs["x"] + y = paddle.sum(x, axis=self.axis, keepdim=self.keepdim) + return y + + def run_torch(self, torch): + x = self.inputs["x"] + y = torch.sum(x, dim=self.axis, keepdim=self.keepdim) + return y + + def check_diff(self, paddle, pd_ret, th_ret): + for pd, th in zip(pd_ret, th_ret): + check_tensor_diff(pd, th, atol=1e-6, rtol=1e-6) + +if __name__ == "__main__": + op_acc_stable_run(SumTest(x_shape = [1, 1024, 254208], dtype ='float32', axis = -1, keepdim = True)) + op_acc_stable_run(SumTest(x_shape = [1, 1024, 254208], dtype ='float32', axis = -1, keepdim = False))