From 370cdc164ed2779bad7477c9745b8bbd3cace8d0 Mon Sep 17 00:00:00 2001 From: ZhengBicheng Date: Thu, 22 Sep 2022 16:43:52 +0800 Subject: [PATCH 01/16] =?UTF-8?q?=E4=B9=8B=E5=89=8D=E7=9A=84pr=E5=90=88?= =?UTF-8?q?=E5=B9=B6=E4=BA=86=E5=A4=A7=E9=87=8F=E9=94=99=E8=AF=AF=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=EF=BC=8C=E9=87=8D=E6=96=B0=E6=8F=90=E4=BA=A4=E4=B8=80?= =?UTF-8?q?=E4=BB=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fluid/tests/unittests/test_frexp_api.py | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 python/paddle/fluid/tests/unittests/test_frexp_api.py diff --git a/python/paddle/fluid/tests/unittests/test_frexp_api.py b/python/paddle/fluid/tests/unittests/test_frexp_api.py new file mode 100644 index 00000000000000..7e2733ef0544b3 --- /dev/null +++ b/python/paddle/fluid/tests/unittests/test_frexp_api.py @@ -0,0 +1,106 @@ +# Copyright (c) 2022 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 __future__ import print_function +import unittest +import numpy as np +import paddle +import paddle.tensor.math as math +import paddle.fluid + + +class TestFrexpAPI(unittest.TestCase): + + def setUp(self): + np.random.seed(1024) + self.rtol = 1e-5 + self.atol = 1e-8 + self.place = paddle.CUDAPlace(0) if paddle.is_compiled_with_cuda() \ + else paddle.CPUPlace() + self.set_input() + + def set_input(self): + self.x_np_1 = np.random.uniform(-3, 3, [10, 12]).astype('float32') + self.x_np_2 = np.random.uniform(-1, 1, [4, 5, 2]).astype('float32') + + # 静态图单测 + def test_static_api(self): + # 开启静态图模式 + paddle.enable_static() + with paddle.static.program_guard(paddle.static.Program()): + input_data_1 = paddle.fluid.data('X', self.x_np_1.shape, + self.x_np_1.dtype) + out1 = math.frexp(input_data_1) + # out1 = frexp(input_data_1) + # 计算静态图结果 + exe = paddle.static.Executor(self.place) + res_1 = exe.run(feed={'X': self.x_np_1}, fetch_list=[out1]) + + with paddle.static.program_guard(paddle.static.Program()): + input_data_2 = paddle.fluid.data('X', self.x_np_2.shape, + self.x_np_2.dtype) + out2 = math.frexp(input_data_2) + # out2 = frexp(input_data_2) + # 计算静态图结果 + exe = paddle.static.Executor(self.place) + res_2 = exe.run(feed={'X': self.x_np_2}, fetch_list=[out2]) + + out_ref_1 = np.frexp(self.x_np_1) + # 对比静态图与 numpy 实现函数计算结果是否相同 + for n, p in zip(out_ref_1, res_1): + np.testing.assert_allclose(n, p, rtol=self.rtol, atol=self.atol) + + out_ref_2 = np.frexp(self.x_np_2) + # 对比静态图与 numpy 实现函数计算结果是否相同 + for n, p in zip(out_ref_2, res_2): + np.testing.assert_allclose(n, p, rtol=self.rtol, atol=self.atol) + + # 动态图单测 + def test_dygraph_api(self): + # 关闭静态图模式 + paddle.disable_static(self.place) + input_num = paddle.to_tensor(self.x_np_1) + # 测试动态图 tensor.frexp 和 paddle.tensor.math.frexp 计算结果 + out1 = np.frexp(self.x_np_1) + out2 = math.frexp(input_num) + # out2 = frexp(input_num) + np.testing.assert_allclose(out1, out2, rtol=1e-05) + + out1 = np.frexp(self.x_np_1) + out2 = input_num.frexp() + np.testing.assert_allclose(out1, out2, rtol=1e-05) + + input_num = paddle.to_tensor(self.x_np_2) + out1 = np.frexp(self.x_np_2) + out2 = math.frexp(input_num) + # out2 = frexp(input_num) + np.testing.assert_allclose(out1, out2, rtol=1e-05) + + out1 = np.frexp(self.x_np_2) + out2 = input_num.frexp() + np.testing.assert_allclose(out1, out2, rtol=1e-05) + paddle.enable_static() + + +class TestSplitsFloat64(TestFrexpAPI): + """ + Test num_or_sections which is an integer and data type is float32. + """ + + def set_input(self): + self.x_np_1 = np.random.uniform(-3, 3, [10, 12]).astype('float64') + self.x_np_2 = np.random.uniform(-1, 1, [4, 5, 2]).astype('float64') + + +if __name__ == "__main__": + unittest.main() From 5b0a5c98b04a69dbb66bc934aaddd8854ad0baf1 Mon Sep 17 00:00:00 2001 From: ZhengBicheng Date: Thu, 22 Sep 2022 16:44:33 +0800 Subject: [PATCH 02/16] =?UTF-8?q?=E4=B9=8B=E5=89=8D=E7=9A=84pr=E5=90=88?= =?UTF-8?q?=E5=B9=B6=E4=BA=86=E5=A4=A7=E9=87=8F=E9=94=99=E8=AF=AF=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=EF=BC=8C=E9=87=8D=E6=96=B0=E6=8F=90=E4=BA=A4=E4=B8=80?= =?UTF-8?q?=E4=BB=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/paddle/__init__.py | 325 +++++-------------------------- python/paddle/tensor/__init__.py | 266 ++++--------------------- python/paddle/tensor/math.py | 42 ++++ 3 files changed, 124 insertions(+), 509 deletions(-) diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index 4e983f43723581..ce0ed2f2ad0484 100755 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -286,6 +286,7 @@ from .tensor.math import frac # noqa: F401 from .tensor.math import sgn # noqa: F401 from .tensor.math import take # noqa: F401 +from .tensor.math import frexp # noqa: F401 from .tensor.random import bernoulli # noqa: F401 from .tensor.random import poisson # noqa: F401 @@ -388,283 +389,49 @@ disable_static() __all__ = [ # noqa - 'iinfo', - 'dtype', - 'uint8', - 'int8', - 'int16', - 'int32', - 'int64', - 'float16', - 'float32', - 'float64', - 'bfloat16', - 'bool', - 'complex64', - 'complex128', - 'addmm', - 'allclose', - 'isclose', - 't', - 'add', - 'subtract', - 'diag', - 'diagflat', - 'isnan', - 'scatter_nd_add', - 'unstack', - 'get_default_dtype', - 'save', - 'multinomial', - 'get_cuda_rng_state', - 'rank', - 'empty_like', - 'eye', - 'cumsum', - 'cumprod', - 'logcumsumexp', - 'logit', - 'LazyGuard', - 'sign', - 'is_empty', - 'equal', - 'equal_all', - 'is_tensor', - 'is_complex', - 'is_integer', - 'cross', - 'where', - 'log1p', - 'cos', - 'tan', - 'mean', - 'mode', - 'mv', - 'in_dynamic_mode', - 'min', - 'amin', - 'any', - 'slice', - 'normal', - 'logsumexp', - 'full', - 'unsqueeze', - 'unsqueeze_', - 'argmax', - 'Model', - 'summary', - 'flops', - 'sort', - 'searchsorted', - 'bucketize', - 'split', - 'vsplit', - 'logical_and', - 'full_like', - 'less_than', - 'kron', - 'clip', - 'Tensor', - 'crop', - 'ParamAttr', - 'stanh', - 'randint', - 'randint_like', - 'assign', - 'gather', - 'scale', - 'zeros', - 'rsqrt', - 'squeeze', - 'squeeze_', - 'to_tensor', - 'gather_nd', - 'isinf', - 'uniform', - 'floor_divide', - 'remainder', - 'floor_mod', - 'roll', - 'batch', - 'max', - 'amax', - 'logical_or', - 'bitwise_and', - 'bitwise_or', - 'bitwise_xor', - 'bitwise_not', - 'mm', - 'flip', - 'rot90', - 'bincount', - 'histogram', - 'multiplex', - 'CUDAPlace', - 'NPUPlace', - 'empty', - 'shape', - 'real', - 'imag', - 'is_floating_point', - 'complex', - 'reciprocal', - 'rand', - 'less_equal', - 'triu', - 'sin', - 'dist', - 'unbind', - 'meshgrid', - 'arange', - 'load', - 'numel', - 'median', - 'nanmedian', - 'quantile', - 'nanquantile', - 'no_grad', - 'set_grad_enabled', - 'is_grad_enabled', - 'mod', - 'abs', - 'tril', - 'pow', - 'zeros_like', - 'maximum', - 'topk', - 'index_select', - 'CPUPlace', - 'matmul', - 'seed', - 'acos', - 'logical_xor', - 'exp', - 'expm1', - 'bernoulli', - 'poisson', - 'sinh', - 'round', - 'DataParallel', - 'argmin', - 'prod', - 'broadcast_shape', - 'conj', - 'neg', - 'lgamma', - 'lerp', - 'erfinv', - 'inner', - 'outer', - 'square', - 'divide', - 'ceil', - 'atan', - 'atan2', - 'rad2deg', - 'deg2rad', - 'gcd', - 'lcm', - 'expand', - 'broadcast_to', - 'ones_like', - 'index_sample', - 'cast', - 'grad', - 'all', - 'ones', - 'not_equal', - 'sum', - 'nansum', - 'nanmean', - 'count_nonzero', - 'tile', - 'greater_equal', - 'isfinite', - 'create_parameter', - 'dot', - 'increment', - 'erf', - 'bmm', - 'chunk', - 'tolist', - 'tensordot', - 'greater_than', - 'shard_index', - 'argsort', - 'tanh', - 'tanh_', - 'transpose', - 'randn', - 'strided_slice', - 'unique', - 'unique_consecutive', - 'set_cuda_rng_state', - 'set_printoptions', - 'std', - 'flatten', - 'asin', - 'multiply', - 'disable_static', - 'masked_select', - 'var', - 'trace', - 'enable_static', - 'scatter_nd', - 'set_default_dtype', - 'disable_signal_handler', - 'expand_as', - 'stack', - 'sqrt', - 'randperm', - 'linspace', - 'logspace', - 'reshape', - 'reshape_', - 'reverse', - 'nonzero', - 'CUDAPinnedPlace', - 'logical_not', - 'add_n', - 'minimum', - 'scatter', - 'scatter_', - 'floor', - 'cosh', - 'log', - 'log2', - 'log10', - 'concat', - 'check_shape', - 'trunc', - 'frac', - 'digamma', - 'standard_normal', - 'diagonal', - 'broadcast_tensors', - 'einsum', - 'set_flags', - 'get_flags', - 'asinh', - 'acosh', - 'atanh', - 'as_complex', - 'as_real', - 'diff', - 'angle', - 'fmax', - 'fmin', - 'moveaxis', - 'repeat_interleave', - 'clone', - 'kthvalue', - 'renorm', - 'take_along_axis', - 'put_along_axis', - 'heaviside', - 'tril_indices', - 'index_add', - "index_add_", - 'sgn', - 'triu_indices', - 'take', + 'iinfo', 'dtype', 'uint8', 'int8', 'int16', 'int32', 'int64', 'float16', + 'float32', 'float64', 'bfloat16', 'bool', 'complex64', 'complex128', + 'addmm', 'allclose', 'isclose', 't', 'add', 'subtract', 'diag', 'diagflat', + 'isnan', 'scatter_nd_add', 'unstack', 'get_default_dtype', 'save', + 'multinomial', 'get_cuda_rng_state', 'rank', 'empty_like', 'eye', 'cumsum', + 'cumprod', 'logcumsumexp', 'logit', 'LazyGuard', 'sign', 'is_empty', + 'equal', 'equal_all', 'is_tensor', 'is_complex', 'is_integer', 'cross', + 'where', 'log1p', 'cos', 'tan', 'mean', 'mode', 'mv', 'in_dynamic_mode', + 'min', 'amin', 'any', 'slice', 'normal', 'logsumexp', 'full', 'unsqueeze', + 'unsqueeze_', 'argmax', 'Model', 'summary', 'flops', 'sort', 'searchsorted', + 'bucketize', 'split', 'vsplit', 'logical_and', 'full_like', 'less_than', + 'kron', 'clip', 'Tensor', 'crop', 'ParamAttr', 'stanh', 'randint', + 'randint_like', 'assign', 'gather', 'scale', 'zeros', 'rsqrt', 'squeeze', + 'squeeze_', 'to_tensor', 'gather_nd', 'isinf', 'uniform', 'floor_divide', + 'remainder', 'floor_mod', 'roll', 'batch', 'max', 'amax', 'logical_or', + 'bitwise_and', 'bitwise_or', 'bitwise_xor', 'bitwise_not', 'mm', 'flip', + 'rot90', 'bincount', 'histogram', 'multiplex', 'CUDAPlace', 'NPUPlace', + 'empty', 'shape', 'real', 'imag', 'is_floating_point', 'complex', + 'reciprocal', 'rand', 'less_equal', 'triu', 'sin', 'dist', 'unbind', + 'meshgrid', 'arange', 'load', 'numel', 'median', 'nanmedian', 'quantile', + 'nanquantile', 'no_grad', 'set_grad_enabled', 'is_grad_enabled', 'mod', + 'abs', 'tril', 'pow', 'zeros_like', 'maximum', 'topk', 'index_select', + 'CPUPlace', 'matmul', 'seed', 'acos', 'logical_xor', 'exp', 'expm1', + 'bernoulli', 'poisson', 'sinh', 'round', 'DataParallel', 'argmin', 'prod', + 'broadcast_shape', 'conj', 'neg', 'lgamma', 'lerp', 'erfinv', 'inner', + 'outer', 'square', 'divide', 'ceil', 'atan', 'atan2', 'rad2deg', 'deg2rad', + 'gcd', 'lcm', 'expand', 'broadcast_to', 'ones_like', 'index_sample', 'cast', + 'grad', 'all', 'ones', 'not_equal', 'sum', 'nansum', 'nanmean', + 'count_nonzero', 'tile', 'greater_equal', 'isfinite', 'create_parameter', + 'dot', 'increment', 'erf', 'bmm', 'chunk', 'tolist', 'tensordot', + 'greater_than', 'shard_index', 'argsort', 'tanh', 'tanh_', 'transpose', + 'randn', 'strided_slice', 'unique', 'unique_consecutive', + 'set_cuda_rng_state', 'set_printoptions', 'std', 'flatten', 'asin', + 'multiply', 'disable_static', 'masked_select', 'var', 'trace', + 'enable_static', 'scatter_nd', 'set_default_dtype', + 'disable_signal_handler', 'expand_as', 'stack', 'sqrt', 'randperm', + 'linspace', 'logspace', 'reshape', 'reshape_', 'reverse', 'nonzero', + 'CUDAPinnedPlace', 'logical_not', 'add_n', 'minimum', 'scatter', 'scatter_', + 'floor', 'cosh', 'log', 'log2', 'log10', 'concat', 'check_shape', 'trunc', + 'frac', 'digamma', 'standard_normal', 'diagonal', 'broadcast_tensors', + 'einsum', 'set_flags', 'get_flags', 'asinh', 'acosh', 'atanh', 'as_complex', + 'as_real', 'diff', 'angle', 'fmax', 'fmin', 'moveaxis', 'repeat_interleave', + 'clone', 'kthvalue', 'renorm', 'take_along_axis', 'put_along_axis', + 'heaviside', 'tril_indices', 'index_add', "index_add_", 'sgn', + 'triu_indices', 'take', 'frexp' ] diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index ba7dd5d0cec529..9b401dee8e370e 100755 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -239,6 +239,7 @@ from .math import frac # noqa: F401 from .math import sgn # noqa: F401 from .math import take # noqa: F401 +from .math import frexp # noqa: F401 from .random import multinomial # noqa: F401 from .random import standard_normal # noqa: F401 @@ -287,236 +288,41 @@ # this list used in math_op_patch.py for _binary_creator_ tensor_method_func = [ # noqa - 'matmul', - 'dot', - 'cov', - 'corrcoef', - 'norm', - 'cond', - 'transpose', - 'lstsq', - 'dist', - 't', - 'cross', - 'cholesky', - 'bmm', - 'histogram', - 'bincount', - 'mv', - 'matrix_power', - 'qr', - 'eigvals', - 'eigvalsh', - 'abs', - 'acos', - 'all', - 'any', - 'asin', - 'atan', - 'ceil', - 'ceil_', - 'cos', - 'cosh', - 'cumsum', - 'cumprod', - 'logcumsumexp', - 'logit', - 'exp', - 'exp_', - 'floor', - 'floor_', - 'increment', - 'log', - 'log2', - 'log10', - 'logsumexp', - 'multiplex', - 'pow', - 'prod', - 'reciprocal', - 'reciprocal_', - 'round', - 'round_', - 'rsqrt', - 'rsqrt_', - 'scale', - 'scale_', - 'sign', - 'sin', - 'sinh', - 'sqrt', - 'sqrt_', - 'square', - 'stanh', - 'sum', - 'nansum', - 'nanmean', - 'count_nonzero', - 'tanh', - 'tanh_', - 'add_n', - 'max', - 'amax', - 'maximum', - 'min', - 'amin', - 'minimum', - 'fmax', - 'fmin', - 'mm', - 'inner', - 'outer', - 'divide', - 'floor_divide', - 'remainder', - 'remainder_', - 'mod', - 'floor_mod', - 'multiply', - 'add', - 'add_', - 'subtract', - 'subtract_', - 'atan', - 'logsumexp', - 'inverse', - 'log1p', - 'erf', - 'addmm', - 'clip', - 'clip_', - 'trace', - 'kron', - 'kthvalue', - 'isfinite', - 'isinf', - 'isnan', - 'broadcast_shape', - 'conj', - 'neg', - 'lgamma', - 'equal', - 'equal_all', - 'greater_equal', - 'greater_than', - 'is_empty', - 'less_equal', - 'less_than', - 'logical_and', - 'logical_not', - 'logical_or', - 'logical_xor', - 'not_equal', - 'allclose', - 'isclose', - 'is_tensor', - 'cast', - 'concat', - 'expand', - 'broadcast_to', - 'expand_as', - 'flatten', - 'flatten_', - 'gather', - 'gather_nd', - 'reshape', - 'reshape_', - 'reverse', - 'scatter', - 'scatter_', - 'scatter_nd_add', - 'scatter_nd', - 'shard_index', - 'slice', - 'split', - 'vsplit', - 'chunk', - 'tensordot', - 'squeeze', - 'squeeze_', - 'stack', - 'strided_slice', - 'transpose', - 'unique', - 'unique_consecutive', - 'unsqueeze', - 'unsqueeze_', - 'unstack', - 'flip', - 'rot90', - 'unbind', - 'roll', - 'tile', - 'argmax', - 'argmin', - 'argsort', - 'masked_select', - 'topk', - 'where', - 'index_select', - 'nonzero', - 'sort', - 'index_sample', - 'mean', - 'std', - 'var', - 'numel', - 'median', - 'nanmedian', - 'quantile', - 'nanquantile', - 'is_complex', - 'is_integer', - 'rank', - 'shape', - 'real', - 'imag', - 'is_floating_point', - 'digamma', - 'diagonal', - 'trunc', - 'frac', - 'bitwise_and', - 'bitwise_or', - 'bitwise_xor', - 'bitwise_not', - 'broadcast_tensors', - 'eig', - 'uniform_', - 'multi_dot', - 'solve', - 'cholesky_solve', - 'triangular_solve', - 'asinh', - 'atanh', - 'acosh', - 'lu', - 'lu_unpack', - 'as_complex', - 'as_real', - 'rad2deg', - 'deg2rad', - 'gcd', - 'lcm', - 'diff', - "mode", - 'lerp', - 'lerp_', - 'erfinv', - 'erfinv_', - 'angle', - 'moveaxis', - 'repeat_interleave', - 'take_along_axis', - 'put_along_axis', - 'put_along_axis_', - 'exponential_', - 'heaviside', - 'index_add', - "index_add_", - 'take', - 'bucketize', - 'sgn', + 'matmul', 'dot', 'cov', 'corrcoef', 'norm', 'cond', 'transpose', 'lstsq', + 'dist', 't', 'cross', 'cholesky', 'bmm', 'histogram', 'bincount', 'mv', + 'matrix_power', 'qr', 'eigvals', 'eigvalsh', 'abs', 'acos', 'all', 'any', + 'asin', 'atan', 'ceil', 'ceil_', 'cos', 'cosh', 'cumsum', 'cumprod', + 'logcumsumexp', 'logit', 'exp', 'exp_', 'floor', 'floor_', 'increment', + 'log', 'log2', 'log10', 'logsumexp', 'multiplex', 'pow', 'prod', + 'reciprocal', 'reciprocal_', 'round', 'round_', 'rsqrt', 'rsqrt_', 'scale', + 'scale_', 'sign', 'sin', 'sinh', 'sqrt', 'sqrt_', 'square', 'stanh', 'sum', + 'nansum', 'nanmean', 'count_nonzero', 'tanh', 'tanh_', 'add_n', 'max', + 'amax', 'maximum', 'min', 'amin', 'minimum', 'fmax', 'fmin', 'mm', 'inner', + 'outer', 'divide', 'floor_divide', 'remainder', 'remainder_', 'mod', + 'floor_mod', 'multiply', 'add', 'add_', 'subtract', 'subtract_', 'atan', + 'logsumexp', 'inverse', 'log1p', 'erf', 'addmm', 'clip', 'clip_', 'trace', + 'kron', 'kthvalue', 'isfinite', 'isinf', 'isnan', 'broadcast_shape', 'conj', + 'neg', 'lgamma', 'equal', 'equal_all', 'greater_equal', 'greater_than', + 'is_empty', 'less_equal', 'less_than', 'logical_and', 'logical_not', + 'logical_or', 'logical_xor', 'not_equal', 'allclose', 'isclose', + 'is_tensor', 'cast', 'concat', 'expand', 'broadcast_to', 'expand_as', + 'flatten', 'flatten_', 'gather', 'gather_nd', 'reshape', 'reshape_', + 'reverse', 'scatter', 'scatter_', 'scatter_nd_add', 'scatter_nd', + 'shard_index', 'slice', 'split', 'vsplit', 'chunk', 'tensordot', 'squeeze', + 'squeeze_', 'stack', 'strided_slice', 'transpose', 'unique', + 'unique_consecutive', 'unsqueeze', 'unsqueeze_', 'unstack', 'flip', 'rot90', + 'unbind', 'roll', 'tile', 'argmax', 'argmin', 'argsort', 'masked_select', + 'topk', 'where', 'index_select', 'nonzero', 'sort', 'index_sample', 'mean', + 'std', 'var', 'numel', 'median', 'nanmedian', 'quantile', 'nanquantile', + 'is_complex', 'is_integer', 'rank', 'shape', 'real', 'imag', + 'is_floating_point', 'digamma', 'diagonal', 'trunc', 'frac', 'bitwise_and', + 'bitwise_or', 'bitwise_xor', 'bitwise_not', 'broadcast_tensors', 'eig', + 'uniform_', 'multi_dot', 'solve', 'cholesky_solve', 'triangular_solve', + 'asinh', 'atanh', 'acosh', 'lu', 'lu_unpack', 'as_complex', 'as_real', + 'rad2deg', 'deg2rad', 'gcd', 'lcm', 'diff', "mode", 'lerp', 'lerp_', + 'erfinv', 'erfinv_', 'angle', 'moveaxis', 'repeat_interleave', + 'take_along_axis', 'put_along_axis', 'put_along_axis_', 'exponential_', + 'heaviside', 'index_add', "index_add_", 'take', 'bucketize', 'sgn', 'frexp' ] # this list used in math_op_patch.py for magic_method bind diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 4807b4c7d5920c..de154a0786feb5 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -4915,3 +4915,45 @@ def take(x, index, mode='raise', name=None): out = input_1d.index_select(index_1d).reshape(index.shape) return out + +def frexp(x): + """ + The function used to decompose a floating point number into mantissa and exponent. + + Args: + x (Tensor): The input tensor, which data type should be float32, float64. + + Returns: + mantissa(Tensor): A mantissa Tensor.The shape and data type of mantissa tensor and exponential tensor are + the same as those of input. + exponent(Tensor): A exponent Tensor.The shape and data type of mantissa tensor and exponential tensor are + the same as those of input. + + Examples: + .. code-block:: Python + + import paddle + + x = paddle.to_tensor([[1,2,3,4]]) + print(paddle.tensor.math.frexp(x)) + #(Tensor(shape=[1, 4], dtype=float32, place=Place(cpu), stop_gradient=True,[[0.50000000, 0.50000000, 0.75000000, 0.50000000]]), + Tensor(shape=[1, 4], dtype=float32, place=Place(cpu), stop_gradient=True,[[1., 2., 2., 3.]])) + """ + if x.dtype not in [paddle.float32, paddle.float64]: + raise TypeError( + "The data type of input must be one of ['float32', 'float64'], but got {}" + .format(x.dtype)) + input_x = paddle.abs(x) + exponent = paddle.floor(paddle.log2(input_x)) + exponent = paddle.where(paddle.isinf(exponent), paddle.full_like(exponent, 0), exponent) + + # 0填充 + mantissa = paddle.divide(input_x, 2 ** exponent) + # 计算exponent + exponent = paddle.where((mantissa <= -1), paddle.add(exponent, paddle.ones_like(exponent)), exponent) + exponent = paddle.where((mantissa >= 1), paddle.add(exponent, paddle.ones_like(exponent)), exponent) + mantissa = paddle.where((mantissa <= -1), paddle.divide(mantissa, 2 ** paddle.ones_like(exponent)), mantissa) + mantissa = paddle.where((mantissa >= -1), paddle.divide(mantissa, 2 ** paddle.ones_like(exponent)), mantissa) + + mantissa = paddle.where((x < 0), mantissa * -1, mantissa) + return mantissa, exponent From c0af897d0d469e983cc1f79bf5a3b0b4a0e2e816 Mon Sep 17 00:00:00 2001 From: ZhengBicheng Date: Thu, 22 Sep 2022 18:10:25 +0800 Subject: [PATCH 03/16] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/paddle/__init__.py | 326 ++++++++++++++++++++++++++----- python/paddle/tensor/__init__.py | 267 +++++++++++++++++++++---- 2 files changed, 512 insertions(+), 81 deletions(-) diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index ce0ed2f2ad0484..7daf8eb92e35f3 100755 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -387,51 +387,285 @@ os.environ.setdefault('runtime_include_dir', runtime_include_dir) disable_static() - __all__ = [ # noqa - 'iinfo', 'dtype', 'uint8', 'int8', 'int16', 'int32', 'int64', 'float16', - 'float32', 'float64', 'bfloat16', 'bool', 'complex64', 'complex128', - 'addmm', 'allclose', 'isclose', 't', 'add', 'subtract', 'diag', 'diagflat', - 'isnan', 'scatter_nd_add', 'unstack', 'get_default_dtype', 'save', - 'multinomial', 'get_cuda_rng_state', 'rank', 'empty_like', 'eye', 'cumsum', - 'cumprod', 'logcumsumexp', 'logit', 'LazyGuard', 'sign', 'is_empty', - 'equal', 'equal_all', 'is_tensor', 'is_complex', 'is_integer', 'cross', - 'where', 'log1p', 'cos', 'tan', 'mean', 'mode', 'mv', 'in_dynamic_mode', - 'min', 'amin', 'any', 'slice', 'normal', 'logsumexp', 'full', 'unsqueeze', - 'unsqueeze_', 'argmax', 'Model', 'summary', 'flops', 'sort', 'searchsorted', - 'bucketize', 'split', 'vsplit', 'logical_and', 'full_like', 'less_than', - 'kron', 'clip', 'Tensor', 'crop', 'ParamAttr', 'stanh', 'randint', - 'randint_like', 'assign', 'gather', 'scale', 'zeros', 'rsqrt', 'squeeze', - 'squeeze_', 'to_tensor', 'gather_nd', 'isinf', 'uniform', 'floor_divide', - 'remainder', 'floor_mod', 'roll', 'batch', 'max', 'amax', 'logical_or', - 'bitwise_and', 'bitwise_or', 'bitwise_xor', 'bitwise_not', 'mm', 'flip', - 'rot90', 'bincount', 'histogram', 'multiplex', 'CUDAPlace', 'NPUPlace', - 'empty', 'shape', 'real', 'imag', 'is_floating_point', 'complex', - 'reciprocal', 'rand', 'less_equal', 'triu', 'sin', 'dist', 'unbind', - 'meshgrid', 'arange', 'load', 'numel', 'median', 'nanmedian', 'quantile', - 'nanquantile', 'no_grad', 'set_grad_enabled', 'is_grad_enabled', 'mod', - 'abs', 'tril', 'pow', 'zeros_like', 'maximum', 'topk', 'index_select', - 'CPUPlace', 'matmul', 'seed', 'acos', 'logical_xor', 'exp', 'expm1', - 'bernoulli', 'poisson', 'sinh', 'round', 'DataParallel', 'argmin', 'prod', - 'broadcast_shape', 'conj', 'neg', 'lgamma', 'lerp', 'erfinv', 'inner', - 'outer', 'square', 'divide', 'ceil', 'atan', 'atan2', 'rad2deg', 'deg2rad', - 'gcd', 'lcm', 'expand', 'broadcast_to', 'ones_like', 'index_sample', 'cast', - 'grad', 'all', 'ones', 'not_equal', 'sum', 'nansum', 'nanmean', - 'count_nonzero', 'tile', 'greater_equal', 'isfinite', 'create_parameter', - 'dot', 'increment', 'erf', 'bmm', 'chunk', 'tolist', 'tensordot', - 'greater_than', 'shard_index', 'argsort', 'tanh', 'tanh_', 'transpose', - 'randn', 'strided_slice', 'unique', 'unique_consecutive', - 'set_cuda_rng_state', 'set_printoptions', 'std', 'flatten', 'asin', - 'multiply', 'disable_static', 'masked_select', 'var', 'trace', - 'enable_static', 'scatter_nd', 'set_default_dtype', - 'disable_signal_handler', 'expand_as', 'stack', 'sqrt', 'randperm', - 'linspace', 'logspace', 'reshape', 'reshape_', 'reverse', 'nonzero', - 'CUDAPinnedPlace', 'logical_not', 'add_n', 'minimum', 'scatter', 'scatter_', - 'floor', 'cosh', 'log', 'log2', 'log10', 'concat', 'check_shape', 'trunc', - 'frac', 'digamma', 'standard_normal', 'diagonal', 'broadcast_tensors', - 'einsum', 'set_flags', 'get_flags', 'asinh', 'acosh', 'atanh', 'as_complex', - 'as_real', 'diff', 'angle', 'fmax', 'fmin', 'moveaxis', 'repeat_interleave', - 'clone', 'kthvalue', 'renorm', 'take_along_axis', 'put_along_axis', - 'heaviside', 'tril_indices', 'index_add', "index_add_", 'sgn', - 'triu_indices', 'take', 'frexp' + 'iinfo', + 'dtype', + 'uint8', + 'int8', + 'int16', + 'int32', + 'int64', + 'float16', + 'float32', + 'float64', + 'bfloat16', + 'bool', + 'complex64', + 'complex128', + 'addmm', + 'allclose', + 'isclose', + 't', + 'add', + 'subtract', + 'diag', + 'diagflat', + 'isnan', + 'scatter_nd_add', + 'unstack', + 'get_default_dtype', + 'save', + 'multinomial', + 'get_cuda_rng_state', + 'rank', + 'empty_like', + 'eye', + 'cumsum', + 'cumprod', + 'logcumsumexp', + 'logit', + 'LazyGuard', + 'sign', + 'is_empty', + 'equal', + 'equal_all', + 'is_tensor', + 'is_complex', + 'is_integer', + 'cross', + 'where', + 'log1p', + 'cos', + 'tan', + 'mean', + 'mode', + 'mv', + 'in_dynamic_mode', + 'min', + 'amin', + 'any', + 'slice', + 'normal', + 'logsumexp', + 'full', + 'unsqueeze', + 'unsqueeze_', + 'argmax', + 'Model', + 'summary', + 'flops', + 'sort', + 'searchsorted', + 'bucketize', + 'split', + 'vsplit', + 'logical_and', + 'full_like', + 'less_than', + 'kron', + 'clip', + 'Tensor', + 'crop', + 'ParamAttr', + 'stanh', + 'randint', + 'randint_like', + 'assign', + 'gather', + 'scale', + 'zeros', + 'rsqrt', + 'squeeze', + 'squeeze_', + 'to_tensor', + 'gather_nd', + 'isinf', + 'uniform', + 'floor_divide', + 'remainder', + 'floor_mod', + 'roll', + 'batch', + 'max', + 'amax', + 'logical_or', + 'bitwise_and', + 'bitwise_or', + 'bitwise_xor', + 'bitwise_not', + 'mm', + 'flip', + 'rot90', + 'bincount', + 'histogram', + 'multiplex', + 'CUDAPlace', + 'NPUPlace', + 'empty', + 'shape', + 'real', + 'imag', + 'is_floating_point', + 'complex', + 'reciprocal', + 'rand', + 'less_equal', + 'triu', + 'sin', + 'dist', + 'unbind', + 'meshgrid', + 'arange', + 'load', + 'numel', + 'median', + 'nanmedian', + 'quantile', + 'nanquantile', + 'no_grad', + 'set_grad_enabled', + 'is_grad_enabled', + 'mod', + 'abs', + 'tril', + 'pow', + 'zeros_like', + 'maximum', + 'topk', + 'index_select', + 'CPUPlace', + 'matmul', + 'seed', + 'acos', + 'logical_xor', + 'exp', + 'expm1', + 'bernoulli', + 'poisson', + 'sinh', + 'round', + 'DataParallel', + 'argmin', + 'prod', + 'broadcast_shape', + 'conj', + 'neg', + 'lgamma', + 'lerp', + 'erfinv', + 'inner', + 'outer', + 'square', + 'divide', + 'ceil', + 'atan', + 'atan2', + 'rad2deg', + 'deg2rad', + 'gcd', + 'lcm', + 'expand', + 'broadcast_to', + 'ones_like', + 'index_sample', + 'cast', + 'grad', + 'all', + 'ones', + 'not_equal', + 'sum', + 'nansum', + 'nanmean', + 'count_nonzero', + 'tile', + 'greater_equal', + 'isfinite', + 'create_parameter', + 'dot', + 'increment', + 'erf', + 'bmm', + 'chunk', + 'tolist', + 'tensordot', + 'greater_than', + 'shard_index', + 'argsort', + 'tanh', + 'tanh_', + 'transpose', + 'randn', + 'strided_slice', + 'unique', + 'unique_consecutive', + 'set_cuda_rng_state', + 'set_printoptions', + 'std', + 'flatten', + 'asin', + 'multiply', + 'disable_static', + 'masked_select', + 'var', + 'trace', + 'enable_static', + 'scatter_nd', + 'set_default_dtype', + 'disable_signal_handler', + 'expand_as', + 'stack', + 'sqrt', + 'randperm', + 'linspace', + 'logspace', + 'reshape', + 'reshape_', + 'reverse', + 'nonzero', + 'CUDAPinnedPlace', + 'logical_not', + 'add_n', + 'minimum', + 'scatter', + 'scatter_', + 'floor', + 'cosh', + 'log', + 'log2', + 'log10', + 'concat', + 'check_shape', + 'trunc', + 'frac', + 'digamma', + 'standard_normal', + 'diagonal', + 'broadcast_tensors', + 'einsum', + 'set_flags', + 'get_flags', + 'asinh', + 'acosh', + 'atanh', + 'as_complex', + 'as_real', + 'diff', + 'angle', + 'fmax', + 'fmin', + 'moveaxis', + 'repeat_interleave', + 'clone', + 'kthvalue', + 'renorm', + 'take_along_axis', + 'put_along_axis', + 'heaviside', + 'tril_indices', + 'index_add', + "index_add_", + 'sgn', + 'triu_indices', + 'take', + 'frexp' ] diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index 9b401dee8e370e..5fd6d577556f99 100755 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -286,43 +286,240 @@ from .einsum import einsum # noqa: F401 +# this list used in math_op_patch.py for _binary_creator_ # this list used in math_op_patch.py for _binary_creator_ tensor_method_func = [ # noqa - 'matmul', 'dot', 'cov', 'corrcoef', 'norm', 'cond', 'transpose', 'lstsq', - 'dist', 't', 'cross', 'cholesky', 'bmm', 'histogram', 'bincount', 'mv', - 'matrix_power', 'qr', 'eigvals', 'eigvalsh', 'abs', 'acos', 'all', 'any', - 'asin', 'atan', 'ceil', 'ceil_', 'cos', 'cosh', 'cumsum', 'cumprod', - 'logcumsumexp', 'logit', 'exp', 'exp_', 'floor', 'floor_', 'increment', - 'log', 'log2', 'log10', 'logsumexp', 'multiplex', 'pow', 'prod', - 'reciprocal', 'reciprocal_', 'round', 'round_', 'rsqrt', 'rsqrt_', 'scale', - 'scale_', 'sign', 'sin', 'sinh', 'sqrt', 'sqrt_', 'square', 'stanh', 'sum', - 'nansum', 'nanmean', 'count_nonzero', 'tanh', 'tanh_', 'add_n', 'max', - 'amax', 'maximum', 'min', 'amin', 'minimum', 'fmax', 'fmin', 'mm', 'inner', - 'outer', 'divide', 'floor_divide', 'remainder', 'remainder_', 'mod', - 'floor_mod', 'multiply', 'add', 'add_', 'subtract', 'subtract_', 'atan', - 'logsumexp', 'inverse', 'log1p', 'erf', 'addmm', 'clip', 'clip_', 'trace', - 'kron', 'kthvalue', 'isfinite', 'isinf', 'isnan', 'broadcast_shape', 'conj', - 'neg', 'lgamma', 'equal', 'equal_all', 'greater_equal', 'greater_than', - 'is_empty', 'less_equal', 'less_than', 'logical_and', 'logical_not', - 'logical_or', 'logical_xor', 'not_equal', 'allclose', 'isclose', - 'is_tensor', 'cast', 'concat', 'expand', 'broadcast_to', 'expand_as', - 'flatten', 'flatten_', 'gather', 'gather_nd', 'reshape', 'reshape_', - 'reverse', 'scatter', 'scatter_', 'scatter_nd_add', 'scatter_nd', - 'shard_index', 'slice', 'split', 'vsplit', 'chunk', 'tensordot', 'squeeze', - 'squeeze_', 'stack', 'strided_slice', 'transpose', 'unique', - 'unique_consecutive', 'unsqueeze', 'unsqueeze_', 'unstack', 'flip', 'rot90', - 'unbind', 'roll', 'tile', 'argmax', 'argmin', 'argsort', 'masked_select', - 'topk', 'where', 'index_select', 'nonzero', 'sort', 'index_sample', 'mean', - 'std', 'var', 'numel', 'median', 'nanmedian', 'quantile', 'nanquantile', - 'is_complex', 'is_integer', 'rank', 'shape', 'real', 'imag', - 'is_floating_point', 'digamma', 'diagonal', 'trunc', 'frac', 'bitwise_and', - 'bitwise_or', 'bitwise_xor', 'bitwise_not', 'broadcast_tensors', 'eig', - 'uniform_', 'multi_dot', 'solve', 'cholesky_solve', 'triangular_solve', - 'asinh', 'atanh', 'acosh', 'lu', 'lu_unpack', 'as_complex', 'as_real', - 'rad2deg', 'deg2rad', 'gcd', 'lcm', 'diff', "mode", 'lerp', 'lerp_', - 'erfinv', 'erfinv_', 'angle', 'moveaxis', 'repeat_interleave', - 'take_along_axis', 'put_along_axis', 'put_along_axis_', 'exponential_', - 'heaviside', 'index_add', "index_add_", 'take', 'bucketize', 'sgn', 'frexp' + 'matmul', + 'dot', + 'cov', + 'corrcoef', + 'norm', + 'cond', + 'transpose', + 'lstsq', + 'dist', + 't', + 'cross', + 'cholesky', + 'bmm', + 'histogram', + 'bincount', + 'mv', + 'matrix_power', + 'qr', + 'eigvals', + 'eigvalsh', + 'abs', + 'acos', + 'all', + 'any', + 'asin', + 'atan', + 'ceil', + 'ceil_', + 'cos', + 'cosh', + 'cumsum', + 'cumprod', + 'logcumsumexp', + 'logit', + 'exp', + 'exp_', + 'floor', + 'floor_', + 'increment', + 'log', + 'log2', + 'log10', + 'logsumexp', + 'multiplex', + 'pow', + 'prod', + 'reciprocal', + 'reciprocal_', + 'round', + 'round_', + 'rsqrt', + 'rsqrt_', + 'scale', + 'scale_', + 'sign', + 'sin', + 'sinh', + 'sqrt', + 'sqrt_', + 'square', + 'stanh', + 'sum', + 'nansum', + 'nanmean', + 'count_nonzero', + 'tanh', + 'tanh_', + 'add_n', + 'max', + 'amax', + 'maximum', + 'min', + 'amin', + 'minimum', + 'fmax', + 'fmin', + 'mm', + 'inner', + 'outer', + 'divide', + 'floor_divide', + 'remainder', + 'remainder_', + 'mod', + 'floor_mod', + 'multiply', + 'add', + 'add_', + 'subtract', + 'subtract_', + 'atan', + 'logsumexp', + 'inverse', + 'log1p', + 'erf', + 'addmm', + 'clip', + 'clip_', + 'trace', + 'kron', + 'kthvalue', + 'isfinite', + 'isinf', + 'isnan', + 'broadcast_shape', + 'conj', + 'neg', + 'lgamma', + 'equal', + 'equal_all', + 'greater_equal', + 'greater_than', + 'is_empty', + 'less_equal', + 'less_than', + 'logical_and', + 'logical_not', + 'logical_or', + 'logical_xor', + 'not_equal', + 'allclose', + 'isclose', + 'is_tensor', + 'cast', + 'concat', + 'expand', + 'broadcast_to', + 'expand_as', + 'flatten', + 'flatten_', + 'gather', + 'gather_nd', + 'reshape', + 'reshape_', + 'reverse', + 'scatter', + 'scatter_', + 'scatter_nd_add', + 'scatter_nd', + 'shard_index', + 'slice', + 'split', + 'vsplit', + 'chunk', + 'tensordot', + 'squeeze', + 'squeeze_', + 'stack', + 'strided_slice', + 'transpose', + 'unique', + 'unique_consecutive', + 'unsqueeze', + 'unsqueeze_', + 'unstack', + 'flip', + 'rot90', + 'unbind', + 'roll', + 'tile', + 'argmax', + 'argmin', + 'argsort', + 'masked_select', + 'topk', + 'where', + 'index_select', + 'nonzero', + 'sort', + 'index_sample', + 'mean', + 'std', + 'var', + 'numel', + 'median', + 'nanmedian', + 'quantile', + 'nanquantile', + 'is_complex', + 'is_integer', + 'rank', + 'shape', + 'real', + 'imag', + 'is_floating_point', + 'digamma', + 'diagonal', + 'trunc', + 'frac', + 'bitwise_and', + 'bitwise_or', + 'bitwise_xor', + 'bitwise_not', + 'broadcast_tensors', + 'eig', + 'uniform_', + 'multi_dot', + 'solve', + 'cholesky_solve', + 'triangular_solve', + 'asinh', + 'atanh', + 'acosh', + 'lu', + 'lu_unpack', + 'as_complex', + 'as_real', + 'rad2deg', + 'deg2rad', + 'gcd', + 'lcm', + 'diff', + "mode", + 'lerp', + 'lerp_', + 'erfinv', + 'erfinv_', + 'angle', + 'moveaxis', + 'repeat_interleave', + 'take_along_axis', + 'put_along_axis', + 'put_along_axis_', + 'exponential_', + 'heaviside', + 'index_add', + "index_add_", + 'take', + 'bucketize', + 'sgn', + 'frexp' ] # this list used in math_op_patch.py for magic_method bind From 813fc2613635e32a034552e83d6945367183c16f Mon Sep 17 00:00:00 2001 From: ZhengBicheng Date: Thu, 22 Sep 2022 18:45:43 +0800 Subject: [PATCH 04/16] =?UTF-8?q?=E6=94=B9=E5=9B=9E=E5=8E=9F=E6=9D=A5?= =?UTF-8?q?=E7=9A=84=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/paddle/__init__.py | 325 +++++-------------------------- python/paddle/tensor/__init__.py | 266 ++++--------------------- 2 files changed, 80 insertions(+), 511 deletions(-) diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index 7daf8eb92e35f3..50961a321d1928 100755 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -388,284 +388,49 @@ disable_static() __all__ = [ # noqa - 'iinfo', - 'dtype', - 'uint8', - 'int8', - 'int16', - 'int32', - 'int64', - 'float16', - 'float32', - 'float64', - 'bfloat16', - 'bool', - 'complex64', - 'complex128', - 'addmm', - 'allclose', - 'isclose', - 't', - 'add', - 'subtract', - 'diag', - 'diagflat', - 'isnan', - 'scatter_nd_add', - 'unstack', - 'get_default_dtype', - 'save', - 'multinomial', - 'get_cuda_rng_state', - 'rank', - 'empty_like', - 'eye', - 'cumsum', - 'cumprod', - 'logcumsumexp', - 'logit', - 'LazyGuard', - 'sign', - 'is_empty', - 'equal', - 'equal_all', - 'is_tensor', - 'is_complex', - 'is_integer', - 'cross', - 'where', - 'log1p', - 'cos', - 'tan', - 'mean', - 'mode', - 'mv', - 'in_dynamic_mode', - 'min', - 'amin', - 'any', - 'slice', - 'normal', - 'logsumexp', - 'full', - 'unsqueeze', - 'unsqueeze_', - 'argmax', - 'Model', - 'summary', - 'flops', - 'sort', - 'searchsorted', - 'bucketize', - 'split', - 'vsplit', - 'logical_and', - 'full_like', - 'less_than', - 'kron', - 'clip', - 'Tensor', - 'crop', - 'ParamAttr', - 'stanh', - 'randint', - 'randint_like', - 'assign', - 'gather', - 'scale', - 'zeros', - 'rsqrt', - 'squeeze', - 'squeeze_', - 'to_tensor', - 'gather_nd', - 'isinf', - 'uniform', - 'floor_divide', - 'remainder', - 'floor_mod', - 'roll', - 'batch', - 'max', - 'amax', - 'logical_or', - 'bitwise_and', - 'bitwise_or', - 'bitwise_xor', - 'bitwise_not', - 'mm', - 'flip', - 'rot90', - 'bincount', - 'histogram', - 'multiplex', - 'CUDAPlace', - 'NPUPlace', - 'empty', - 'shape', - 'real', - 'imag', - 'is_floating_point', - 'complex', - 'reciprocal', - 'rand', - 'less_equal', - 'triu', - 'sin', - 'dist', - 'unbind', - 'meshgrid', - 'arange', - 'load', - 'numel', - 'median', - 'nanmedian', - 'quantile', - 'nanquantile', - 'no_grad', - 'set_grad_enabled', - 'is_grad_enabled', - 'mod', - 'abs', - 'tril', - 'pow', - 'zeros_like', - 'maximum', - 'topk', - 'index_select', - 'CPUPlace', - 'matmul', - 'seed', - 'acos', - 'logical_xor', - 'exp', - 'expm1', - 'bernoulli', - 'poisson', - 'sinh', - 'round', - 'DataParallel', - 'argmin', - 'prod', - 'broadcast_shape', - 'conj', - 'neg', - 'lgamma', - 'lerp', - 'erfinv', - 'inner', - 'outer', - 'square', - 'divide', - 'ceil', - 'atan', - 'atan2', - 'rad2deg', - 'deg2rad', - 'gcd', - 'lcm', - 'expand', - 'broadcast_to', - 'ones_like', - 'index_sample', - 'cast', - 'grad', - 'all', - 'ones', - 'not_equal', - 'sum', - 'nansum', - 'nanmean', - 'count_nonzero', - 'tile', - 'greater_equal', - 'isfinite', - 'create_parameter', - 'dot', - 'increment', - 'erf', - 'bmm', - 'chunk', - 'tolist', - 'tensordot', - 'greater_than', - 'shard_index', - 'argsort', - 'tanh', - 'tanh_', - 'transpose', - 'randn', - 'strided_slice', - 'unique', - 'unique_consecutive', - 'set_cuda_rng_state', - 'set_printoptions', - 'std', - 'flatten', - 'asin', - 'multiply', - 'disable_static', - 'masked_select', - 'var', - 'trace', - 'enable_static', - 'scatter_nd', - 'set_default_dtype', - 'disable_signal_handler', - 'expand_as', - 'stack', - 'sqrt', - 'randperm', - 'linspace', - 'logspace', - 'reshape', - 'reshape_', - 'reverse', - 'nonzero', - 'CUDAPinnedPlace', - 'logical_not', - 'add_n', - 'minimum', - 'scatter', - 'scatter_', - 'floor', - 'cosh', - 'log', - 'log2', - 'log10', - 'concat', - 'check_shape', - 'trunc', - 'frac', - 'digamma', - 'standard_normal', - 'diagonal', - 'broadcast_tensors', - 'einsum', - 'set_flags', - 'get_flags', - 'asinh', - 'acosh', - 'atanh', - 'as_complex', - 'as_real', - 'diff', - 'angle', - 'fmax', - 'fmin', - 'moveaxis', - 'repeat_interleave', - 'clone', - 'kthvalue', - 'renorm', - 'take_along_axis', - 'put_along_axis', - 'heaviside', - 'tril_indices', - 'index_add', - "index_add_", - 'sgn', - 'triu_indices', - 'take', - 'frexp' + 'iinfo', 'dtype', 'uint8', 'int8', 'int16', 'int32', 'int64', 'float16', + 'float32', 'float64', 'bfloat16', 'bool', 'complex64', 'complex128', + 'addmm', 'allclose', 'isclose', 't', 'add', 'subtract', 'diag', 'diagflat', + 'isnan', 'scatter_nd_add', 'unstack', 'get_default_dtype', 'save', + 'multinomial', 'get_cuda_rng_state', 'rank', 'empty_like', 'eye', 'cumsum', + 'cumprod', 'logcumsumexp', 'logit', 'LazyGuard', 'sign', 'is_empty', + 'equal', 'equal_all', 'is_tensor', 'is_complex', 'is_integer', 'cross', + 'where', 'log1p', 'cos', 'tan', 'mean', 'mode', 'mv', 'in_dynamic_mode', + 'min', 'amin', 'any', 'slice', 'normal', 'logsumexp', 'full', 'unsqueeze', + 'unsqueeze_', 'argmax', 'Model', 'summary', 'flops', 'sort', 'searchsorted', + 'bucketize', 'split', 'vsplit', 'logical_and', 'full_like', 'less_than', + 'kron', 'clip', 'Tensor', 'crop', 'ParamAttr', 'stanh', 'randint', + 'randint_like', 'assign', 'gather', 'scale', 'zeros', 'rsqrt', 'squeeze', + 'squeeze_', 'to_tensor', 'gather_nd', 'isinf', 'uniform', 'floor_divide', + 'remainder', 'floor_mod', 'roll', 'batch', 'max', 'amax', 'logical_or', + 'bitwise_and', 'bitwise_or', 'bitwise_xor', 'bitwise_not', 'mm', 'flip', + 'rot90', 'bincount', 'histogram', 'multiplex', 'CUDAPlace', 'NPUPlace', + 'empty', 'shape', 'real', 'imag', 'is_floating_point', 'complex', + 'reciprocal', 'rand', 'less_equal', 'triu', 'sin', 'dist', 'unbind', + 'meshgrid', 'arange', 'load', 'numel', 'median', 'nanmedian', 'quantile', + 'nanquantile', 'no_grad', 'set_grad_enabled', 'is_grad_enabled', 'mod', + 'abs', 'tril', 'pow', 'zeros_like', 'maximum', 'topk', 'index_select', + 'CPUPlace', 'matmul', 'seed', 'acos', 'logical_xor', 'exp', 'expm1', + 'bernoulli', 'poisson', 'sinh', 'round', 'DataParallel', 'argmin', 'prod', + 'broadcast_shape', 'conj', 'neg', 'lgamma', 'lerp', 'erfinv', 'inner', + 'outer', 'square', 'divide', 'ceil', 'atan', 'atan2', 'rad2deg', 'deg2rad', + 'gcd', 'lcm', 'expand', 'broadcast_to', 'ones_like', 'index_sample', 'cast', + 'grad', 'all', 'ones', 'not_equal', 'sum', 'nansum', 'nanmean', + 'count_nonzero', 'tile', 'greater_equal', 'isfinite', 'create_parameter', + 'dot', 'increment', 'erf', 'bmm', 'chunk', 'tolist', 'tensordot', + 'greater_than', 'shard_index', 'argsort', 'tanh', 'tanh_', 'transpose', + 'randn', 'strided_slice', 'unique', 'unique_consecutive', + 'set_cuda_rng_state', 'set_printoptions', 'std', 'flatten', 'asin', + 'multiply', 'disable_static', 'masked_select', 'var', 'trace', + 'enable_static', 'scatter_nd', 'set_default_dtype', + 'disable_signal_handler', 'expand_as', 'stack', 'sqrt', 'randperm', + 'linspace', 'logspace', 'reshape', 'reshape_', 'reverse', 'nonzero', + 'CUDAPinnedPlace', 'logical_not', 'add_n', 'minimum', 'scatter', 'scatter_', + 'floor', 'cosh', 'log', 'log2', 'log10', 'concat', 'check_shape', 'trunc', + 'frac', 'digamma', 'standard_normal', 'diagonal', 'broadcast_tensors', + 'einsum', 'set_flags', 'get_flags', 'asinh', 'acosh', 'atanh', 'as_complex', + 'as_real', 'diff', 'angle', 'fmax', 'fmin', 'moveaxis', 'repeat_interleave', + 'clone', 'kthvalue', 'renorm', 'take_along_axis', 'put_along_axis', + 'heaviside', 'tril_indices', 'index_add', "index_add_", 'sgn', + 'triu_indices', 'take', 'frexp' ] diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index 5fd6d577556f99..5b9a62113c0608 100755 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -289,237 +289,41 @@ # this list used in math_op_patch.py for _binary_creator_ # this list used in math_op_patch.py for _binary_creator_ tensor_method_func = [ # noqa - 'matmul', - 'dot', - 'cov', - 'corrcoef', - 'norm', - 'cond', - 'transpose', - 'lstsq', - 'dist', - 't', - 'cross', - 'cholesky', - 'bmm', - 'histogram', - 'bincount', - 'mv', - 'matrix_power', - 'qr', - 'eigvals', - 'eigvalsh', - 'abs', - 'acos', - 'all', - 'any', - 'asin', - 'atan', - 'ceil', - 'ceil_', - 'cos', - 'cosh', - 'cumsum', - 'cumprod', - 'logcumsumexp', - 'logit', - 'exp', - 'exp_', - 'floor', - 'floor_', - 'increment', - 'log', - 'log2', - 'log10', - 'logsumexp', - 'multiplex', - 'pow', - 'prod', - 'reciprocal', - 'reciprocal_', - 'round', - 'round_', - 'rsqrt', - 'rsqrt_', - 'scale', - 'scale_', - 'sign', - 'sin', - 'sinh', - 'sqrt', - 'sqrt_', - 'square', - 'stanh', - 'sum', - 'nansum', - 'nanmean', - 'count_nonzero', - 'tanh', - 'tanh_', - 'add_n', - 'max', - 'amax', - 'maximum', - 'min', - 'amin', - 'minimum', - 'fmax', - 'fmin', - 'mm', - 'inner', - 'outer', - 'divide', - 'floor_divide', - 'remainder', - 'remainder_', - 'mod', - 'floor_mod', - 'multiply', - 'add', - 'add_', - 'subtract', - 'subtract_', - 'atan', - 'logsumexp', - 'inverse', - 'log1p', - 'erf', - 'addmm', - 'clip', - 'clip_', - 'trace', - 'kron', - 'kthvalue', - 'isfinite', - 'isinf', - 'isnan', - 'broadcast_shape', - 'conj', - 'neg', - 'lgamma', - 'equal', - 'equal_all', - 'greater_equal', - 'greater_than', - 'is_empty', - 'less_equal', - 'less_than', - 'logical_and', - 'logical_not', - 'logical_or', - 'logical_xor', - 'not_equal', - 'allclose', - 'isclose', - 'is_tensor', - 'cast', - 'concat', - 'expand', - 'broadcast_to', - 'expand_as', - 'flatten', - 'flatten_', - 'gather', - 'gather_nd', - 'reshape', - 'reshape_', - 'reverse', - 'scatter', - 'scatter_', - 'scatter_nd_add', - 'scatter_nd', - 'shard_index', - 'slice', - 'split', - 'vsplit', - 'chunk', - 'tensordot', - 'squeeze', - 'squeeze_', - 'stack', - 'strided_slice', - 'transpose', - 'unique', - 'unique_consecutive', - 'unsqueeze', - 'unsqueeze_', - 'unstack', - 'flip', - 'rot90', - 'unbind', - 'roll', - 'tile', - 'argmax', - 'argmin', - 'argsort', - 'masked_select', - 'topk', - 'where', - 'index_select', - 'nonzero', - 'sort', - 'index_sample', - 'mean', - 'std', - 'var', - 'numel', - 'median', - 'nanmedian', - 'quantile', - 'nanquantile', - 'is_complex', - 'is_integer', - 'rank', - 'shape', - 'real', - 'imag', - 'is_floating_point', - 'digamma', - 'diagonal', - 'trunc', - 'frac', - 'bitwise_and', - 'bitwise_or', - 'bitwise_xor', - 'bitwise_not', - 'broadcast_tensors', - 'eig', - 'uniform_', - 'multi_dot', - 'solve', - 'cholesky_solve', - 'triangular_solve', - 'asinh', - 'atanh', - 'acosh', - 'lu', - 'lu_unpack', - 'as_complex', - 'as_real', - 'rad2deg', - 'deg2rad', - 'gcd', - 'lcm', - 'diff', - "mode", - 'lerp', - 'lerp_', - 'erfinv', - 'erfinv_', - 'angle', - 'moveaxis', - 'repeat_interleave', - 'take_along_axis', - 'put_along_axis', - 'put_along_axis_', - 'exponential_', - 'heaviside', - 'index_add', - "index_add_", - 'take', - 'bucketize', - 'sgn', - 'frexp' + 'matmul', 'dot', 'cov', 'corrcoef', 'norm', 'cond', 'transpose', 'lstsq', + 'dist', 't', 'cross', 'cholesky', 'bmm', 'histogram', 'bincount', 'mv', + 'matrix_power', 'qr', 'eigvals', 'eigvalsh', 'abs', 'acos', 'all', 'any', + 'asin', 'atan', 'ceil', 'ceil_', 'cos', 'cosh', 'cumsum', 'cumprod', + 'logcumsumexp', 'logit', 'exp', 'exp_', 'floor', 'floor_', 'increment', + 'log', 'log2', 'log10', 'logsumexp', 'multiplex', 'pow', 'prod', + 'reciprocal', 'reciprocal_', 'round', 'round_', 'rsqrt', 'rsqrt_', 'scale', + 'scale_', 'sign', 'sin', 'sinh', 'sqrt', 'sqrt_', 'square', 'stanh', 'sum', + 'nansum', 'nanmean', 'count_nonzero', 'tanh', 'tanh_', 'add_n', 'max', + 'amax', 'maximum', 'min', 'amin', 'minimum', 'fmax', 'fmin', 'mm', 'inner', + 'outer', 'divide', 'floor_divide', 'remainder', 'remainder_', 'mod', + 'floor_mod', 'multiply', 'add', 'add_', 'subtract', 'subtract_', 'atan', + 'logsumexp', 'inverse', 'log1p', 'erf', 'addmm', 'clip', 'clip_', 'trace', + 'kron', 'kthvalue', 'isfinite', 'isinf', 'isnan', 'broadcast_shape', 'conj', + 'neg', 'lgamma', 'equal', 'equal_all', 'greater_equal', 'greater_than', + 'is_empty', 'less_equal', 'less_than', 'logical_and', 'logical_not', + 'logical_or', 'logical_xor', 'not_equal', 'allclose', 'isclose', + 'is_tensor', 'cast', 'concat', 'expand', 'broadcast_to', 'expand_as', + 'flatten', 'flatten_', 'gather', 'gather_nd', 'reshape', 'reshape_', + 'reverse', 'scatter', 'scatter_', 'scatter_nd_add', 'scatter_nd', + 'shard_index', 'slice', 'split', 'vsplit', 'chunk', 'tensordot', 'squeeze', + 'squeeze_', 'stack', 'strided_slice', 'transpose', 'unique', + 'unique_consecutive', 'unsqueeze', 'unsqueeze_', 'unstack', 'flip', 'rot90', + 'unbind', 'roll', 'tile', 'argmax', 'argmin', 'argsort', 'masked_select', + 'topk', 'where', 'index_select', 'nonzero', 'sort', 'index_sample', 'mean', + 'std', 'var', 'numel', 'median', 'nanmedian', 'quantile', 'nanquantile', + 'is_complex', 'is_integer', 'rank', 'shape', 'real', 'imag', + 'is_floating_point', 'digamma', 'diagonal', 'trunc', 'frac', 'bitwise_and', + 'bitwise_or', 'bitwise_xor', 'bitwise_not', 'broadcast_tensors', 'eig', + 'uniform_', 'multi_dot', 'solve', 'cholesky_solve', 'triangular_solve', + 'asinh', 'atanh', 'acosh', 'lu', 'lu_unpack', 'as_complex', 'as_real', + 'rad2deg', 'deg2rad', 'gcd', 'lcm', 'diff', "mode", 'lerp', 'lerp_', + 'erfinv', 'erfinv_', 'angle', 'moveaxis', 'repeat_interleave', + 'take_along_axis', 'put_along_axis', 'put_along_axis_', 'exponential_', + 'heaviside', 'index_add', "index_add_", 'take', 'bucketize', 'sgn', 'frexp' ] # this list used in math_op_patch.py for magic_method bind From 0780713cb2e696744d813549f3165e8542b67628 Mon Sep 17 00:00:00 2001 From: ZhengBicheng Date: Fri, 23 Sep 2022 00:03:14 +0800 Subject: [PATCH 05/16] =?UTF-8?q?=E6=8C=89=E7=85=A7=E8=A6=81=E6=B1=82?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fluid/tests/unittests/test_frexp_api.py | 54 ++++++++----------- python/paddle/tensor/__init__.py | 1 - python/paddle/tensor/math.py | 30 +++++------ 3 files changed, 36 insertions(+), 49 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_frexp_api.py b/python/paddle/fluid/tests/unittests/test_frexp_api.py index 7e2733ef0544b3..6c611a4f855bae 100644 --- a/python/paddle/fluid/tests/unittests/test_frexp_api.py +++ b/python/paddle/fluid/tests/unittests/test_frexp_api.py @@ -11,11 +11,9 @@ # 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 __future__ import print_function import unittest import numpy as np import paddle -import paddle.tensor.math as math import paddle.fluid @@ -31,7 +29,6 @@ def setUp(self): def set_input(self): self.x_np_1 = np.random.uniform(-3, 3, [10, 12]).astype('float32') - self.x_np_2 = np.random.uniform(-1, 1, [4, 5, 2]).astype('float32') # 静态图单测 def test_static_api(self): @@ -40,31 +37,16 @@ def test_static_api(self): with paddle.static.program_guard(paddle.static.Program()): input_data_1 = paddle.fluid.data('X', self.x_np_1.shape, self.x_np_1.dtype) - out1 = math.frexp(input_data_1) - # out1 = frexp(input_data_1) + out1 = paddle.frexp(input_data_1) # 计算静态图结果 exe = paddle.static.Executor(self.place) res_1 = exe.run(feed={'X': self.x_np_1}, fetch_list=[out1]) - with paddle.static.program_guard(paddle.static.Program()): - input_data_2 = paddle.fluid.data('X', self.x_np_2.shape, - self.x_np_2.dtype) - out2 = math.frexp(input_data_2) - # out2 = frexp(input_data_2) - # 计算静态图结果 - exe = paddle.static.Executor(self.place) - res_2 = exe.run(feed={'X': self.x_np_2}, fetch_list=[out2]) - out_ref_1 = np.frexp(self.x_np_1) # 对比静态图与 numpy 实现函数计算结果是否相同 for n, p in zip(out_ref_1, res_1): np.testing.assert_allclose(n, p, rtol=self.rtol, atol=self.atol) - out_ref_2 = np.frexp(self.x_np_2) - # 对比静态图与 numpy 实现函数计算结果是否相同 - for n, p in zip(out_ref_2, res_2): - np.testing.assert_allclose(n, p, rtol=self.rtol, atol=self.atol) - # 动态图单测 def test_dygraph_api(self): # 关闭静态图模式 @@ -72,34 +54,40 @@ def test_dygraph_api(self): input_num = paddle.to_tensor(self.x_np_1) # 测试动态图 tensor.frexp 和 paddle.tensor.math.frexp 计算结果 out1 = np.frexp(self.x_np_1) - out2 = math.frexp(input_num) - # out2 = frexp(input_num) + out2 = paddle.frexp(input_num) np.testing.assert_allclose(out1, out2, rtol=1e-05) out1 = np.frexp(self.x_np_1) out2 = input_num.frexp() np.testing.assert_allclose(out1, out2, rtol=1e-05) - - input_num = paddle.to_tensor(self.x_np_2) - out1 = np.frexp(self.x_np_2) - out2 = math.frexp(input_num) - # out2 = frexp(input_num) - np.testing.assert_allclose(out1, out2, rtol=1e-05) - - out1 = np.frexp(self.x_np_2) - out2 = input_num.frexp() - np.testing.assert_allclose(out1, out2, rtol=1e-05) paddle.enable_static() -class TestSplitsFloat64(TestFrexpAPI): +class TestSplitsFloat32Demo01(TestFrexpAPI): """ Test num_or_sections which is an integer and data type is float32. """ + def set_input(self): + self.x_np_1 = np.random.uniform(-1, 1, [4, 5, 2]).astype('float32') + + +class TestSplitsFloat64Demo01(TestFrexpAPI): + """ + Test num_or_sections which is an integer and data type is float64. + """ + def set_input(self): self.x_np_1 = np.random.uniform(-3, 3, [10, 12]).astype('float64') - self.x_np_2 = np.random.uniform(-1, 1, [4, 5, 2]).astype('float64') + + +class TestSplitsFloat64Demo02(TestFrexpAPI): + """ + Test num_or_sections which is an integer and data type is float64. + """ + + def set_input(self): + self.x_np_1 = np.random.uniform(-1, 1, [4, 5, 2]).astype('float64') if __name__ == "__main__": diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index 5b9a62113c0608..9b401dee8e370e 100755 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -286,7 +286,6 @@ from .einsum import einsum # noqa: F401 -# this list used in math_op_patch.py for _binary_creator_ # this list used in math_op_patch.py for _binary_creator_ tensor_method_func = [ # noqa 'matmul', 'dot', 'cov', 'corrcoef', 'norm', 'cond', 'transpose', 'lstsq', diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index de154a0786feb5..c8297a93ab3aa3 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -4916,28 +4916,28 @@ def take(x, index, mode='raise', name=None): return out -def frexp(x): +def frexp(x, name=None): """ - The function used to decompose a floating point number into mantissa and exponent. + The function used to decompose a floating point number into mantissa and exponent. - Args: - x (Tensor): The input tensor, which data type should be float32, float64. - - Returns: - mantissa(Tensor): A mantissa Tensor.The shape and data type of mantissa tensor and exponential tensor are + Args: + x (Tensor): The input tensor, which data type should be float32, float64. + name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + Returns: + mantissa (Tensor): A mantissa Tensor. The shape and data type of mantissa tensor and exponential tensor are the same as those of input. - exponent(Tensor): A exponent Tensor.The shape and data type of mantissa tensor and exponential tensor are + exponent (Tensor): A exponent Tensor. The shape and data type of mantissa tensor and exponential tensor are the same as those of input. - Examples: - .. code-block:: Python + Examples: + .. code-block:: python - import paddle + import paddle - x = paddle.to_tensor([[1,2,3,4]]) - print(paddle.tensor.math.frexp(x)) - #(Tensor(shape=[1, 4], dtype=float32, place=Place(cpu), stop_gradient=True,[[0.50000000, 0.50000000, 0.75000000, 0.50000000]]), - Tensor(shape=[1, 4], dtype=float32, place=Place(cpu), stop_gradient=True,[[1., 2., 2., 3.]])) + x = paddle.to_tensor([[1, 2, 3, 4]]) + print(paddle.tensor.math.frexp(x)) + # (Tensor(shape=[1, 4], dtype=float32, place=Place(cpu), stop_gradient=True,[[0.50000000, 0.50000000, 0.75000000, 0.50000000]]), + # Tensor(shape=[1, 4], dtype=float32, place=Place(cpu), stop_gradient=True,[[1., 2., 2., 3.]])) """ if x.dtype not in [paddle.float32, paddle.float64]: raise TypeError( From 537419b8310e39ddc4fe67207495067df4c3f4aa Mon Sep 17 00:00:00 2001 From: ZhengBicheng Date: Fri, 23 Sep 2022 00:18:57 +0800 Subject: [PATCH 06/16] =?UTF-8?q?=E6=8C=89=E7=85=A7=E8=A6=81=E6=B1=82?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fluid/tests/unittests/test_frexp_api.py | 32 +++++++++---------- python/paddle/tensor/math.py | 4 +-- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_frexp_api.py b/python/paddle/fluid/tests/unittests/test_frexp_api.py index 6c611a4f855bae..a7c4fa6e2f7d88 100644 --- a/python/paddle/fluid/tests/unittests/test_frexp_api.py +++ b/python/paddle/fluid/tests/unittests/test_frexp_api.py @@ -28,66 +28,66 @@ def setUp(self): self.set_input() def set_input(self): - self.x_np_1 = np.random.uniform(-3, 3, [10, 12]).astype('float32') + self.x_np = np.random.uniform(-3, 3, [10, 12]).astype('float32') # 静态图单测 def test_static_api(self): # 开启静态图模式 paddle.enable_static() with paddle.static.program_guard(paddle.static.Program()): - input_data_1 = paddle.fluid.data('X', self.x_np_1.shape, - self.x_np_1.dtype) - out1 = paddle.frexp(input_data_1) + input_data = paddle.fluid.data('X', self.x_np.shape, + self.x_np.dtype) + out = paddle.frexp(input_data) # 计算静态图结果 exe = paddle.static.Executor(self.place) - res_1 = exe.run(feed={'X': self.x_np_1}, fetch_list=[out1]) + res = exe.run(feed={'X': self.x_np}, fetch_list=[out]) - out_ref_1 = np.frexp(self.x_np_1) + out_ref = np.frexp(self.x_np) # 对比静态图与 numpy 实现函数计算结果是否相同 - for n, p in zip(out_ref_1, res_1): + for n, p in zip(out_ref, res): np.testing.assert_allclose(n, p, rtol=self.rtol, atol=self.atol) # 动态图单测 def test_dygraph_api(self): # 关闭静态图模式 paddle.disable_static(self.place) - input_num = paddle.to_tensor(self.x_np_1) + input_num = paddle.to_tensor(self.x_np) # 测试动态图 tensor.frexp 和 paddle.tensor.math.frexp 计算结果 - out1 = np.frexp(self.x_np_1) + out1 = np.frexp(self.x_np) out2 = paddle.frexp(input_num) np.testing.assert_allclose(out1, out2, rtol=1e-05) - out1 = np.frexp(self.x_np_1) + out1 = np.frexp(self.x_np) out2 = input_num.frexp() np.testing.assert_allclose(out1, out2, rtol=1e-05) paddle.enable_static() -class TestSplitsFloat32Demo01(TestFrexpAPI): +class TestSplitsFloat32Case1(TestFrexpAPI): """ Test num_or_sections which is an integer and data type is float32. """ def set_input(self): - self.x_np_1 = np.random.uniform(-1, 1, [4, 5, 2]).astype('float32') + self.x_np = np.random.uniform(-1, 1, [4, 5, 2]).astype('float32') -class TestSplitsFloat64Demo01(TestFrexpAPI): +class TestSplitsFloat64Case1(TestFrexpAPI): """ Test num_or_sections which is an integer and data type is float64. """ def set_input(self): - self.x_np_1 = np.random.uniform(-3, 3, [10, 12]).astype('float64') + self.x_np = np.random.uniform(-3, 3, [10, 12]).astype('float64') -class TestSplitsFloat64Demo02(TestFrexpAPI): +class TestSplitsFloat64Case2(TestFrexpAPI): """ Test num_or_sections which is an integer and data type is float64. """ def set_input(self): - self.x_np_1 = np.random.uniform(-1, 1, [4, 5, 2]).astype('float64') + self.x_np = np.random.uniform(-1, 1, [4, 5, 2]).astype('float64') if __name__ == "__main__": diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index c8297a93ab3aa3..c7b6675a0c7699 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -4921,8 +4921,8 @@ def frexp(x, name=None): The function used to decompose a floating point number into mantissa and exponent. Args: - x (Tensor): The input tensor, which data type should be float32, float64. - name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + x (Tensor): The input tensor, it's data type should be float32, float64. + name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: mantissa (Tensor): A mantissa Tensor. The shape and data type of mantissa tensor and exponential tensor are the same as those of input. From 290f82290cca3067393c09fc75cac34dc5257337 Mon Sep 17 00:00:00 2001 From: ZhengBicheng Date: Fri, 23 Sep 2022 13:14:34 +0800 Subject: [PATCH 07/16] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/paddle/tensor/math.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index c7b6675a0c7699..4b346a33fba55c 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -4934,7 +4934,7 @@ def frexp(x, name=None): import paddle - x = paddle.to_tensor([[1, 2, 3, 4]]) + x = paddle.to_tensor([[1, 2, 3, 4]], dtype="float32") print(paddle.tensor.math.frexp(x)) # (Tensor(shape=[1, 4], dtype=float32, place=Place(cpu), stop_gradient=True,[[0.50000000, 0.50000000, 0.75000000, 0.50000000]]), # Tensor(shape=[1, 4], dtype=float32, place=Place(cpu), stop_gradient=True,[[1., 2., 2., 3.]])) From dfad4688c37699c7e6712f325ee9f5d218341519 Mon Sep 17 00:00:00 2001 From: ZhengBicheng Date: Fri, 23 Sep 2022 22:48:56 +0800 Subject: [PATCH 08/16] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/paddle/__init__.py | 325 ++++++++++++++++++++++++++----- python/paddle/tensor/__init__.py | 266 +++++++++++++++++++++---- 2 files changed, 511 insertions(+), 80 deletions(-) diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index 50961a321d1928..b9801428201e6d 100755 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -388,49 +388,284 @@ disable_static() __all__ = [ # noqa - 'iinfo', 'dtype', 'uint8', 'int8', 'int16', 'int32', 'int64', 'float16', - 'float32', 'float64', 'bfloat16', 'bool', 'complex64', 'complex128', - 'addmm', 'allclose', 'isclose', 't', 'add', 'subtract', 'diag', 'diagflat', - 'isnan', 'scatter_nd_add', 'unstack', 'get_default_dtype', 'save', - 'multinomial', 'get_cuda_rng_state', 'rank', 'empty_like', 'eye', 'cumsum', - 'cumprod', 'logcumsumexp', 'logit', 'LazyGuard', 'sign', 'is_empty', - 'equal', 'equal_all', 'is_tensor', 'is_complex', 'is_integer', 'cross', - 'where', 'log1p', 'cos', 'tan', 'mean', 'mode', 'mv', 'in_dynamic_mode', - 'min', 'amin', 'any', 'slice', 'normal', 'logsumexp', 'full', 'unsqueeze', - 'unsqueeze_', 'argmax', 'Model', 'summary', 'flops', 'sort', 'searchsorted', - 'bucketize', 'split', 'vsplit', 'logical_and', 'full_like', 'less_than', - 'kron', 'clip', 'Tensor', 'crop', 'ParamAttr', 'stanh', 'randint', - 'randint_like', 'assign', 'gather', 'scale', 'zeros', 'rsqrt', 'squeeze', - 'squeeze_', 'to_tensor', 'gather_nd', 'isinf', 'uniform', 'floor_divide', - 'remainder', 'floor_mod', 'roll', 'batch', 'max', 'amax', 'logical_or', - 'bitwise_and', 'bitwise_or', 'bitwise_xor', 'bitwise_not', 'mm', 'flip', - 'rot90', 'bincount', 'histogram', 'multiplex', 'CUDAPlace', 'NPUPlace', - 'empty', 'shape', 'real', 'imag', 'is_floating_point', 'complex', - 'reciprocal', 'rand', 'less_equal', 'triu', 'sin', 'dist', 'unbind', - 'meshgrid', 'arange', 'load', 'numel', 'median', 'nanmedian', 'quantile', - 'nanquantile', 'no_grad', 'set_grad_enabled', 'is_grad_enabled', 'mod', - 'abs', 'tril', 'pow', 'zeros_like', 'maximum', 'topk', 'index_select', - 'CPUPlace', 'matmul', 'seed', 'acos', 'logical_xor', 'exp', 'expm1', - 'bernoulli', 'poisson', 'sinh', 'round', 'DataParallel', 'argmin', 'prod', - 'broadcast_shape', 'conj', 'neg', 'lgamma', 'lerp', 'erfinv', 'inner', - 'outer', 'square', 'divide', 'ceil', 'atan', 'atan2', 'rad2deg', 'deg2rad', - 'gcd', 'lcm', 'expand', 'broadcast_to', 'ones_like', 'index_sample', 'cast', - 'grad', 'all', 'ones', 'not_equal', 'sum', 'nansum', 'nanmean', - 'count_nonzero', 'tile', 'greater_equal', 'isfinite', 'create_parameter', - 'dot', 'increment', 'erf', 'bmm', 'chunk', 'tolist', 'tensordot', - 'greater_than', 'shard_index', 'argsort', 'tanh', 'tanh_', 'transpose', - 'randn', 'strided_slice', 'unique', 'unique_consecutive', - 'set_cuda_rng_state', 'set_printoptions', 'std', 'flatten', 'asin', - 'multiply', 'disable_static', 'masked_select', 'var', 'trace', - 'enable_static', 'scatter_nd', 'set_default_dtype', - 'disable_signal_handler', 'expand_as', 'stack', 'sqrt', 'randperm', - 'linspace', 'logspace', 'reshape', 'reshape_', 'reverse', 'nonzero', - 'CUDAPinnedPlace', 'logical_not', 'add_n', 'minimum', 'scatter', 'scatter_', - 'floor', 'cosh', 'log', 'log2', 'log10', 'concat', 'check_shape', 'trunc', - 'frac', 'digamma', 'standard_normal', 'diagonal', 'broadcast_tensors', - 'einsum', 'set_flags', 'get_flags', 'asinh', 'acosh', 'atanh', 'as_complex', - 'as_real', 'diff', 'angle', 'fmax', 'fmin', 'moveaxis', 'repeat_interleave', - 'clone', 'kthvalue', 'renorm', 'take_along_axis', 'put_along_axis', - 'heaviside', 'tril_indices', 'index_add', "index_add_", 'sgn', - 'triu_indices', 'take', 'frexp' + 'iinfo', + 'dtype', + 'uint8', + 'int8', + 'int16', + 'int32', + 'int64', + 'float16', + 'float32', + 'float64', + 'bfloat16', + 'bool', + 'complex64', + 'complex128', + 'addmm', + 'allclose', + 'isclose', + 't', + 'add', + 'subtract', + 'diag', + 'diagflat', + 'isnan', + 'scatter_nd_add', + 'unstack', + 'get_default_dtype', + 'save', + 'multinomial', + 'get_cuda_rng_state', + 'rank', + 'empty_like', + 'eye', + 'cumsum', + 'cumprod', + 'logcumsumexp', + 'logit', + 'LazyGuard', + 'sign', + 'is_empty', + 'equal', + 'equal_all', + 'is_tensor', + 'is_complex', + 'is_integer', + 'cross', + 'where', + 'log1p', + 'cos', + 'tan', + 'mean', + 'mode', + 'mv', + 'in_dynamic_mode', + 'min', + 'amin', + 'any', + 'slice', + 'normal', + 'logsumexp', + 'full', + 'unsqueeze', + 'unsqueeze_', + 'argmax', + 'Model', + 'summary', + 'flops', + 'sort', + 'searchsorted', + 'bucketize', + 'split', + 'vsplit', + 'logical_and', + 'full_like', + 'less_than', + 'kron', + 'clip', + 'Tensor', + 'crop', + 'ParamAttr', + 'stanh', + 'randint', + 'randint_like', + 'assign', + 'gather', + 'scale', + 'zeros', + 'rsqrt', + 'squeeze', + 'squeeze_', + 'to_tensor', + 'gather_nd', + 'isinf', + 'uniform', + 'floor_divide', + 'remainder', + 'floor_mod', + 'roll', + 'batch', + 'max', + 'amax', + 'logical_or', + 'bitwise_and', + 'bitwise_or', + 'bitwise_xor', + 'bitwise_not', + 'mm', + 'flip', + 'rot90', + 'bincount', + 'histogram', + 'multiplex', + 'CUDAPlace', + 'NPUPlace', + 'empty', + 'shape', + 'real', + 'imag', + 'is_floating_point', + 'complex', + 'reciprocal', + 'rand', + 'less_equal', + 'triu', + 'sin', + 'dist', + 'unbind', + 'meshgrid', + 'arange', + 'load', + 'numel', + 'median', + 'nanmedian', + 'quantile', + 'nanquantile', + 'no_grad', + 'set_grad_enabled', + 'is_grad_enabled', + 'mod', + 'abs', + 'tril', + 'pow', + 'zeros_like', + 'maximum', + 'topk', + 'index_select', + 'CPUPlace', + 'matmul', + 'seed', + 'acos', + 'logical_xor', + 'exp', + 'expm1', + 'bernoulli', + 'poisson', + 'sinh', + 'round', + 'DataParallel', + 'argmin', + 'prod', + 'broadcast_shape', + 'conj', + 'neg', + 'lgamma', + 'lerp', + 'erfinv', + 'inner', + 'outer', + 'square', + 'divide', + 'ceil', + 'atan', + 'atan2', + 'rad2deg', + 'deg2rad', + 'gcd', + 'lcm', + 'expand', + 'broadcast_to', + 'ones_like', + 'index_sample', + 'cast', + 'grad', + 'all', + 'ones', + 'not_equal', + 'sum', + 'nansum', + 'nanmean', + 'count_nonzero', + 'tile', + 'greater_equal', + 'isfinite', + 'create_parameter', + 'dot', + 'increment', + 'erf', + 'bmm', + 'chunk', + 'tolist', + 'tensordot', + 'greater_than', + 'shard_index', + 'argsort', + 'tanh', + 'tanh_', + 'transpose', + 'randn', + 'strided_slice', + 'unique', + 'unique_consecutive', + 'set_cuda_rng_state', + 'set_printoptions', + 'std', + 'flatten', + 'asin', + 'multiply', + 'disable_static', + 'masked_select', + 'var', + 'trace', + 'enable_static', + 'scatter_nd', + 'set_default_dtype', + 'disable_signal_handler', + 'expand_as', + 'stack', + 'sqrt', + 'randperm', + 'linspace', + 'logspace', + 'reshape', + 'reshape_', + 'reverse', + 'nonzero', + 'CUDAPinnedPlace', + 'logical_not', + 'add_n', + 'minimum', + 'scatter', + 'scatter_', + 'floor', + 'cosh', + 'log', + 'log2', + 'log10', + 'concat', + 'check_shape', + 'trunc', + 'frac', + 'digamma', + 'standard_normal', + 'diagonal', + 'broadcast_tensors', + 'einsum', + 'set_flags', + 'get_flags', + 'asinh', + 'acosh', + 'atanh', + 'as_complex', + 'as_real', + 'diff', + 'angle', + 'fmax', + 'fmin', + 'moveaxis', + 'repeat_interleave', + 'clone', + 'kthvalue', + 'renorm', + 'take_along_axis', + 'put_along_axis', + 'heaviside', + 'tril_indices', + 'index_add', + "index_add_", + 'sgn', + 'triu_indices', + 'take', + 'frexp', ] diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index 9b401dee8e370e..f08d339c049a1a 100755 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -288,41 +288,237 @@ # this list used in math_op_patch.py for _binary_creator_ tensor_method_func = [ # noqa - 'matmul', 'dot', 'cov', 'corrcoef', 'norm', 'cond', 'transpose', 'lstsq', - 'dist', 't', 'cross', 'cholesky', 'bmm', 'histogram', 'bincount', 'mv', - 'matrix_power', 'qr', 'eigvals', 'eigvalsh', 'abs', 'acos', 'all', 'any', - 'asin', 'atan', 'ceil', 'ceil_', 'cos', 'cosh', 'cumsum', 'cumprod', - 'logcumsumexp', 'logit', 'exp', 'exp_', 'floor', 'floor_', 'increment', - 'log', 'log2', 'log10', 'logsumexp', 'multiplex', 'pow', 'prod', - 'reciprocal', 'reciprocal_', 'round', 'round_', 'rsqrt', 'rsqrt_', 'scale', - 'scale_', 'sign', 'sin', 'sinh', 'sqrt', 'sqrt_', 'square', 'stanh', 'sum', - 'nansum', 'nanmean', 'count_nonzero', 'tanh', 'tanh_', 'add_n', 'max', - 'amax', 'maximum', 'min', 'amin', 'minimum', 'fmax', 'fmin', 'mm', 'inner', - 'outer', 'divide', 'floor_divide', 'remainder', 'remainder_', 'mod', - 'floor_mod', 'multiply', 'add', 'add_', 'subtract', 'subtract_', 'atan', - 'logsumexp', 'inverse', 'log1p', 'erf', 'addmm', 'clip', 'clip_', 'trace', - 'kron', 'kthvalue', 'isfinite', 'isinf', 'isnan', 'broadcast_shape', 'conj', - 'neg', 'lgamma', 'equal', 'equal_all', 'greater_equal', 'greater_than', - 'is_empty', 'less_equal', 'less_than', 'logical_and', 'logical_not', - 'logical_or', 'logical_xor', 'not_equal', 'allclose', 'isclose', - 'is_tensor', 'cast', 'concat', 'expand', 'broadcast_to', 'expand_as', - 'flatten', 'flatten_', 'gather', 'gather_nd', 'reshape', 'reshape_', - 'reverse', 'scatter', 'scatter_', 'scatter_nd_add', 'scatter_nd', - 'shard_index', 'slice', 'split', 'vsplit', 'chunk', 'tensordot', 'squeeze', - 'squeeze_', 'stack', 'strided_slice', 'transpose', 'unique', - 'unique_consecutive', 'unsqueeze', 'unsqueeze_', 'unstack', 'flip', 'rot90', - 'unbind', 'roll', 'tile', 'argmax', 'argmin', 'argsort', 'masked_select', - 'topk', 'where', 'index_select', 'nonzero', 'sort', 'index_sample', 'mean', - 'std', 'var', 'numel', 'median', 'nanmedian', 'quantile', 'nanquantile', - 'is_complex', 'is_integer', 'rank', 'shape', 'real', 'imag', - 'is_floating_point', 'digamma', 'diagonal', 'trunc', 'frac', 'bitwise_and', - 'bitwise_or', 'bitwise_xor', 'bitwise_not', 'broadcast_tensors', 'eig', - 'uniform_', 'multi_dot', 'solve', 'cholesky_solve', 'triangular_solve', - 'asinh', 'atanh', 'acosh', 'lu', 'lu_unpack', 'as_complex', 'as_real', - 'rad2deg', 'deg2rad', 'gcd', 'lcm', 'diff', "mode", 'lerp', 'lerp_', - 'erfinv', 'erfinv_', 'angle', 'moveaxis', 'repeat_interleave', - 'take_along_axis', 'put_along_axis', 'put_along_axis_', 'exponential_', - 'heaviside', 'index_add', "index_add_", 'take', 'bucketize', 'sgn', 'frexp' + 'matmul', + 'dot', + 'cov', + 'corrcoef', + 'norm', + 'cond', + 'transpose', + 'lstsq', + 'dist', + 't', + 'cross', + 'cholesky', + 'bmm', + 'histogram', + 'bincount', + 'mv', + 'matrix_power', + 'qr', + 'eigvals', + 'eigvalsh', + 'abs', + 'acos', + 'all', + 'any', + 'asin', + 'atan', + 'ceil', + 'ceil_', + 'cos', + 'cosh', + 'cumsum', + 'cumprod', + 'logcumsumexp', + 'logit', + 'exp', + 'exp_', + 'floor', + 'floor_', + 'increment', + 'log', + 'log2', + 'log10', + 'logsumexp', + 'multiplex', + 'pow', + 'prod', + 'reciprocal', + 'reciprocal_', + 'round', + 'round_', + 'rsqrt', + 'rsqrt_', + 'scale', + 'scale_', + 'sign', + 'sin', + 'sinh', + 'sqrt', + 'sqrt_', + 'square', + 'stanh', + 'sum', + 'nansum', + 'nanmean', + 'count_nonzero', + 'tanh', + 'tanh_', + 'add_n', + 'max', + 'amax', + 'maximum', + 'min', + 'amin', + 'minimum', + 'fmax', + 'fmin', + 'mm', + 'inner', + 'outer', + 'divide', + 'floor_divide', + 'remainder', + 'remainder_', + 'mod', + 'floor_mod', + 'multiply', + 'add', + 'add_', + 'subtract', + 'subtract_', + 'atan', + 'logsumexp', + 'inverse', + 'log1p', + 'erf', + 'addmm', + 'clip', + 'clip_', + 'trace', + 'kron', + 'kthvalue', + 'isfinite', + 'isinf', + 'isnan', + 'broadcast_shape', + 'conj', + 'neg', + 'lgamma', + 'equal', + 'equal_all', + 'greater_equal', + 'greater_than', + 'is_empty', + 'less_equal', + 'less_than', + 'logical_and', + 'logical_not', + 'logical_or', + 'logical_xor', + 'not_equal', + 'allclose', + 'isclose', + 'is_tensor', + 'cast', + 'concat', + 'expand', + 'broadcast_to', + 'expand_as', + 'flatten', + 'flatten_', + 'gather', + 'gather_nd', + 'reshape', + 'reshape_', + 'reverse', + 'scatter', + 'scatter_', + 'scatter_nd_add', + 'scatter_nd', + 'shard_index', + 'slice', + 'split', + 'vsplit', + 'chunk', + 'tensordot', + 'squeeze', + 'squeeze_', + 'stack', + 'strided_slice', + 'transpose', + 'unique', + 'unique_consecutive', + 'unsqueeze', + 'unsqueeze_', + 'unstack', + 'flip', + 'rot90', + 'unbind', + 'roll', + 'tile', + 'argmax', + 'argmin', + 'argsort', + 'masked_select', + 'topk', + 'where', + 'index_select', + 'nonzero', + 'sort', + 'index_sample', + 'mean', + 'std', + 'var', + 'numel', + 'median', + 'nanmedian', + 'quantile', + 'nanquantile', + 'is_complex', + 'is_integer', + 'rank', + 'shape', + 'real', + 'imag', + 'is_floating_point', + 'digamma', + 'diagonal', + 'trunc', + 'frac', + 'bitwise_and', + 'bitwise_or', + 'bitwise_xor', + 'bitwise_not', + 'broadcast_tensors', + 'eig', + 'uniform_', + 'multi_dot', + 'solve', + 'cholesky_solve', + 'triangular_solve', + 'asinh', + 'atanh', + 'acosh', + 'lu', + 'lu_unpack', + 'as_complex', + 'as_real', + 'rad2deg', + 'deg2rad', + 'gcd', + 'lcm', + 'diff', + "mode", + 'lerp', + 'lerp_', + 'erfinv', + 'erfinv_', + 'angle', + 'moveaxis', + 'repeat_interleave', + 'take_along_axis', + 'put_along_axis', + 'put_along_axis_', + 'exponential_', + 'heaviside', + 'index_add', + "index_add_", + 'take', + 'bucketize', + 'sgn', + 'frexp', ] # this list used in math_op_patch.py for magic_method bind From 81a2676be537f1a74d2638040025587c507243d6 Mon Sep 17 00:00:00 2001 From: ZhengBicheng Date: Fri, 23 Sep 2022 22:53:39 +0800 Subject: [PATCH 09/16] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/paddle/tensor/math.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index a406f3f898c437..43a3f09d80d600 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -5110,6 +5110,7 @@ def take(x, index, mode='raise', name=None): return out + def frexp(x, name=None): """ The function used to decompose a floating point number into mantissa and exponent. @@ -5139,15 +5140,26 @@ def frexp(x, name=None): .format(x.dtype)) input_x = paddle.abs(x) exponent = paddle.floor(paddle.log2(input_x)) - exponent = paddle.where(paddle.isinf(exponent), paddle.full_like(exponent, 0), exponent) + exponent = paddle.where(paddle.isinf(exponent), + paddle.full_like(exponent, 0), exponent) # 0填充 - mantissa = paddle.divide(input_x, 2 ** exponent) + mantissa = paddle.divide(input_x, 2**exponent) # 计算exponent - exponent = paddle.where((mantissa <= -1), paddle.add(exponent, paddle.ones_like(exponent)), exponent) - exponent = paddle.where((mantissa >= 1), paddle.add(exponent, paddle.ones_like(exponent)), exponent) - mantissa = paddle.where((mantissa <= -1), paddle.divide(mantissa, 2 ** paddle.ones_like(exponent)), mantissa) - mantissa = paddle.where((mantissa >= -1), paddle.divide(mantissa, 2 ** paddle.ones_like(exponent)), mantissa) + exponent = paddle.where((mantissa <= -1), + paddle.add(exponent, paddle.ones_like(exponent)), + exponent) + exponent = paddle.where((mantissa >= 1), + paddle.add(exponent, paddle.ones_like(exponent)), + exponent) + mantissa = paddle.where((mantissa <= -1), + paddle.divide(mantissa, + 2**paddle.ones_like(exponent)), + mantissa) + mantissa = paddle.where((mantissa >= -1), + paddle.divide(mantissa, + 2**paddle.ones_like(exponent)), + mantissa) mantissa = paddle.where((x < 0), mantissa * -1, mantissa) return mantissa, exponent From cdb6e48783e0139ac25690ec66dcb2ec7386b853 Mon Sep 17 00:00:00 2001 From: ZhengBicheng Date: Mon, 26 Sep 2022 11:08:11 +0800 Subject: [PATCH 10/16] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E8=8B=B1=E6=96=87?= =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/paddle/tensor/math.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 43a3f09d80d600..7148ad515c59f1 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -5119,9 +5119,10 @@ def frexp(x, name=None): x (Tensor): The input tensor, it's data type should be float32, float64. name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: - mantissa (Tensor): A mantissa Tensor. The shape and data type of mantissa tensor and exponential tensor are + - mantissa (Tensor): A mantissa Tensor. The shape and data type of mantissa tensor and exponential tensor are the same as those of input. - exponent (Tensor): A exponent Tensor. The shape and data type of mantissa tensor and exponential tensor are + + - exponent (Tensor): A exponent Tensor. The shape and data type of mantissa tensor and exponential tensor are the same as those of input. Examples: From 5ec4a9ce6a30eaf2a5edd6749524ba32efa5ff75 Mon Sep 17 00:00:00 2001 From: Ligoml <39876205+Ligoml@users.noreply.github.com> Date: Mon, 26 Sep 2022 12:14:37 +0800 Subject: [PATCH 11/16] fix docs build error --- python/paddle/tensor/math.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 7148ad515c59f1..784091467d248d 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -5119,6 +5119,7 @@ def frexp(x, name=None): x (Tensor): The input tensor, it's data type should be float32, float64. name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: + - mantissa (Tensor): A mantissa Tensor. The shape and data type of mantissa tensor and exponential tensor are the same as those of input. From e79e932ccd4e02b44e8f32a74fa947ab25bc86c7 Mon Sep 17 00:00:00 2001 From: ZhengBicheng Date: Mon, 26 Sep 2022 12:43:08 +0800 Subject: [PATCH 12/16] pre-commit --- python/paddle/tensor/math.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 784091467d248d..ed8e98c7268aa5 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -5119,7 +5119,7 @@ def frexp(x, name=None): x (Tensor): The input tensor, it's data type should be float32, float64. name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: - + - mantissa (Tensor): A mantissa Tensor. The shape and data type of mantissa tensor and exponential tensor are the same as those of input. From e4e0f943b626554ad0a8440244e50e8f3472c5b8 Mon Sep 17 00:00:00 2001 From: Ligoml <39876205+Ligoml@users.noreply.github.com> Date: Mon, 26 Sep 2022 14:15:55 +0800 Subject: [PATCH 13/16] for docs build --- python/paddle/tensor/math.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index ed8e98c7268aa5..c1d8e78761910c 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -5120,7 +5120,7 @@ def frexp(x, name=None): name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: - - mantissa (Tensor): A mantissa Tensor. The shape and data type of mantissa tensor and exponential tensor are + - mantissa (Tensor), A mantissa Tensor. The shape and data type of mantissa tensor and exponential tensor are the same as those of input. - exponent (Tensor): A exponent Tensor. The shape and data type of mantissa tensor and exponential tensor are From d1c09357842d51fadb7ad96aa8584cd327ba3fc3 Mon Sep 17 00:00:00 2001 From: Ligoml <39876205+Ligoml@users.noreply.github.com> Date: Mon, 26 Sep 2022 14:16:08 +0800 Subject: [PATCH 14/16] for docs build --- python/paddle/tensor/math.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index c1d8e78761910c..c42f1d811bcce6 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -5123,7 +5123,7 @@ def frexp(x, name=None): - mantissa (Tensor), A mantissa Tensor. The shape and data type of mantissa tensor and exponential tensor are the same as those of input. - - exponent (Tensor): A exponent Tensor. The shape and data type of mantissa tensor and exponential tensor are + - exponent (Tensor), A exponent Tensor. The shape and data type of mantissa tensor and exponential tensor are the same as those of input. Examples: From 42b9b29402c61f2475710d88bb037a6122329186 Mon Sep 17 00:00:00 2001 From: ZhengBicheng Date: Mon, 26 Sep 2022 22:09:05 +0800 Subject: [PATCH 15/16] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dmantissa=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E9=94=99=E8=AF=AF=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/paddle/tensor/math.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index ed8e98c7268aa5..35d4d3f46e7403 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -5158,7 +5158,7 @@ def frexp(x, name=None): paddle.divide(mantissa, 2**paddle.ones_like(exponent)), mantissa) - mantissa = paddle.where((mantissa >= -1), + mantissa = paddle.where((mantissa >= 1), paddle.divide(mantissa, 2**paddle.ones_like(exponent)), mantissa) From 6f9922ea0c8385839c73460a33ac53bc557c294e Mon Sep 17 00:00:00 2001 From: ZhengBicheng Date: Mon, 26 Sep 2022 22:12:52 +0800 Subject: [PATCH 16/16] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=AF=AF=E5=88=A4expon?= =?UTF-8?q?ent=E5=8F=AF=E8=83=BD=E5=AD=98=E5=9C=A8=E8=B4=9F=E6=95=B0?= =?UTF-8?q?=EF=BC=8C=E5=AF=BC=E8=87=B4=E8=AE=A1=E7=AE=97=E9=87=8F=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E7=9A=84=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/paddle/tensor/math.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index dfcf85bf8e1f14..6c10a23f962567 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -5148,16 +5148,9 @@ def frexp(x, name=None): # 0填充 mantissa = paddle.divide(input_x, 2**exponent) # 计算exponent - exponent = paddle.where((mantissa <= -1), - paddle.add(exponent, paddle.ones_like(exponent)), - exponent) exponent = paddle.where((mantissa >= 1), paddle.add(exponent, paddle.ones_like(exponent)), exponent) - mantissa = paddle.where((mantissa <= -1), - paddle.divide(mantissa, - 2**paddle.ones_like(exponent)), - mantissa) mantissa = paddle.where((mantissa >= 1), paddle.divide(mantissa, 2**paddle.ones_like(exponent)),