From c284d42abc1c5f949855090d091c2bec2d53517e Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Thu, 16 Feb 2023 15:50:58 +0800 Subject: [PATCH] Add logspace yaml (#49194) * add logspace yaml * update by comments * resolve test framework conflicct --- paddle/phi/api/yaml/legacy_ops.yaml | 12 +++++ paddle/phi/infermeta/multiary.cc | 3 +- paddle/phi/infermeta/multiary.h | 1 + .../fluid/tests/unittests/test_logspace.py | 44 +++++-------------- python/paddle/tensor/creation.py | 11 +++-- 5 files changed, 34 insertions(+), 37 deletions(-) diff --git a/paddle/phi/api/yaml/legacy_ops.yaml b/paddle/phi/api/yaml/legacy_ops.yaml index cb0dfa23c99..11f7449f450 100755 --- a/paddle/phi/api/yaml/legacy_ops.yaml +++ b/paddle/phi/api/yaml/legacy_ops.yaml @@ -1031,6 +1031,18 @@ kernel : func : logical_xor +- op : logspace + args : (Tensor start, Tensor stop, Tensor num, Tensor base, DataType dtype, Place place={}) + output : Tensor(out) + infer_meta: + func : LogspaceInferMeta + param : [start, stop, num, base, dtype] + kernel : + func : logspace + param : [start, stop, num, base, dtype] + data_type : dtype + backend : place + - op : logsumexp args : (Tensor x, int64_t[] axis, bool keepdim, bool reduce_all) output : Tensor(out) diff --git a/paddle/phi/infermeta/multiary.cc b/paddle/phi/infermeta/multiary.cc index cc1b0269614..8b168de5750 100644 --- a/paddle/phi/infermeta/multiary.cc +++ b/paddle/phi/infermeta/multiary.cc @@ -1972,6 +1972,7 @@ void LogspaceInferMeta(const MetaTensor& start, const MetaTensor& stop, const MetaTensor& number, const MetaTensor& base, + DataType dtype, MetaTensor* out) { auto s_dims = start.dims(); PADDLE_ENFORCE_EQ( @@ -2002,7 +2003,7 @@ void LogspaceInferMeta(const MetaTensor& start, "but received input shape is [%s].", b_dims)); out->set_dims(phi::make_ddim({-1})); - out->set_dtype(start.dtype()); + out->set_dtype(dtype); } void MergedAdamInferMeta( diff --git a/paddle/phi/infermeta/multiary.h b/paddle/phi/infermeta/multiary.h index c0abeb222cb..87ed5529046 100644 --- a/paddle/phi/infermeta/multiary.h +++ b/paddle/phi/infermeta/multiary.h @@ -341,6 +341,7 @@ void LogspaceInferMeta(const MetaTensor& start, const MetaTensor& stop, const MetaTensor& number, const MetaTensor& base, + DataType dtype, MetaTensor* out); void MergedAdamInferMeta( diff --git a/python/paddle/fluid/tests/unittests/test_logspace.py b/python/paddle/fluid/tests/unittests/test_logspace.py index dee098dd5f3..19461fc938f 100644 --- a/python/paddle/fluid/tests/unittests/test_logspace.py +++ b/python/paddle/fluid/tests/unittests/test_logspace.py @@ -24,6 +24,9 @@ class TestLogspaceOpCommonCase(OpTest): def setUp(self): self.op_type = "logspace" self.python_api = paddle.logspace + self.init_data() + + def init_data(self): dtype = 'float32' self.inputs = { 'Start': np.array([0]).astype(dtype), @@ -32,17 +35,14 @@ class TestLogspaceOpCommonCase(OpTest): 'Base': np.array([2]).astype(dtype), } self.attrs = {'dtype': int(paddle.float32)} - self.outputs = {'Out': np.power(2, np.arange(0, 11)).astype(dtype)} def test_check_output(self): self.check_output() -class TestLogspaceOpReverseCase(OpTest): - def setUp(self): - self.op_type = "logspace" - self.python_api = paddle.logspace +class TestLogspaceOpReverseCase(TestLogspaceOpCommonCase): + def init_data(self): dtype = 'float32' self.inputs = { 'Start': np.array([10]).astype(dtype), @@ -51,17 +51,11 @@ class TestLogspaceOpReverseCase(OpTest): 'Base': np.array([2]).astype(dtype), } self.attrs = {'dtype': int(paddle.float32)} - self.outputs = {'Out': np.power(2, np.arange(10, -1, -1)).astype(dtype)} - def test_check_output(self): - self.check_output() - -class TestLogspaceOpNumOneCase(OpTest): - def setUp(self): - self.op_type = "logspace" - self.python_api = paddle.logspace +class TestLogspaceOpNumOneCase(TestLogspaceOpCommonCase): + def init_data(self): dtype = 'float32' self.inputs = { 'Start': np.array([10]).astype(dtype), @@ -70,17 +64,11 @@ class TestLogspaceOpNumOneCase(OpTest): 'Base': np.array([2]).astype(dtype), } self.attrs = {'dtype': int(paddle.float32)} - self.outputs = {'Out': np.power(2, np.array(10)).astype(dtype)} - def test_check_output(self): - self.check_output() - -class TestLogspaceOpMinusBaseCase(OpTest): - def setUp(self): - self.op_type = "logspace" - self.python_api = paddle.logspace +class TestLogspaceOpMinusBaseCase(TestLogspaceOpCommonCase): + def init_data(self): dtype = 'float32' self.inputs = { 'Start': np.array([0]).astype(dtype), @@ -89,17 +77,11 @@ class TestLogspaceOpMinusBaseCase(OpTest): 'Base': np.array([-2]).astype(dtype), } self.attrs = {'dtype': int(paddle.float32)} - self.outputs = {'Out': np.power(-2, np.arange(0, 11)).astype(dtype)} - def test_check_output(self): - self.check_output() - -class TestLogspaceOpZeroBaseCase(OpTest): - def setUp(self): - self.op_type = "logspace" - self.python_api = paddle.logspace +class TestLogspaceOpZeroBaseCase(TestLogspaceOpCommonCase): + def init_data(self): dtype = 'float32' self.inputs = { 'Start': np.array([0]).astype(dtype), @@ -108,12 +90,8 @@ class TestLogspaceOpZeroBaseCase(OpTest): 'Base': np.array([0]).astype(dtype), } self.attrs = {'dtype': int(paddle.float32)} - self.outputs = {'Out': np.power(0, np.arange(0, 11)).astype(dtype)} - def test_check_output(self): - self.check_output() - class TestLogspaceAPI(unittest.TestCase): def test_variable_input1(self): diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 07f33b328c1..83379ff69c8 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -21,7 +21,7 @@ import warnings import numpy as np import paddle -from paddle import _C_ops, _legacy_C_ops +from paddle import _C_ops from paddle.common_ops_import import fill_constant from ..fluid.data_feeder import ( @@ -447,8 +447,13 @@ def logspace(start, stop, num, base=10.0, dtype=None, name=None): with device_guard("cpu"): tensor_base = fill_constant([1], dtype, base) if in_dygraph_mode(): - return _legacy_C_ops.logspace( - tensor_start, tensor_stop, tensor_num, tensor_base, 'dtype', dtype + return _C_ops.logspace( + tensor_start, + tensor_stop, + tensor_num, + tensor_base, + dtype, + _current_expected_place(), ) else: helper = LayerHelper("logspace", **locals()) -- GitLab