diff --git a/paddle/phi/api/yaml/legacy_ops.yaml b/paddle/phi/api/yaml/legacy_ops.yaml index cb0dfa23c99ae69defd113667bb39c1a9e63e5c5..11f7449f4507c99420d7496330134305d53bc40d 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 cc1b02696144f052e6b4836eba4ab7b7b325c3ed..8b168de57507a120042b7440bcb4c60409f45eef 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 c0abeb222cbbafb59318380fc372ee8366559cad..87ed5529046a638a195cf776d7975a907e72f6d1 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 dee098dd5f34dc1c0d7b6669984475ea785d4adb..19461fc938f200300260db261fa79caaf114fe42 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 07f33b328c183f882afee33f0a8b7d6888dfd6f5..83379ff69c8a4085fa943a473cdebb6b77ab723d 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())