Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions python/paddle/base/dygraph/tensor_patch_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ def remove(self):


def monkey_patch_tensor():
# TODO(cleanup-legacy-ir): This method is for dy2st in legacy ir only
# and should be removed after legacy ir is removed.
@switch_to_static_graph
def _to_static_var(self, to_parameter=False, **kwargs):
"""
Expand Down Expand Up @@ -922,6 +924,8 @@ def __deepcopy__(self, memo):
new_tensor.copy_(self, True)
return new_tensor

# TODO(cleanup-legacy-ir): This method is for dy2st in legacy ir only
# and should be removed after legacy ir is removed.
@property
def block(self):
return framework.default_main_program().global_block()
Expand Down
98 changes: 98 additions & 0 deletions test/deprecated/legacy_test/test_eager_tensor_deprecated.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Copyright (c) 2024 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.

import unittest

import numpy as np

import paddle
from paddle import base
from paddle.base import core
from paddle.base.framework import paddle_type_to_proto_type


class TestEagerTensorLegacy(unittest.TestCase):
def setUp(self):
self.shape = [512, 1234]
self.dtype = np.float32
self.array = np.random.uniform(0.1, 1, self.shape).astype(self.dtype)

def test_block(self):
var = paddle.to_tensor(self.array)
self.assertEqual(var.block, base.default_main_program().global_block())

def test_to_static_var(self):
with base.dygraph.guard():
# Convert Tensor into Variable or Parameter
tensor = paddle.to_tensor(self.array)
static_var = tensor._to_static_var()
self._assert_to_static(tensor, static_var)

tensor = paddle.to_tensor(self.array)
static_param = tensor._to_static_var(to_parameter=True)
self._assert_to_static(tensor, static_param, True)

# Convert EagerParamBase into Parameter
fc = paddle.nn.Linear(
10,
20,
weight_attr=paddle.ParamAttr(
learning_rate=0.001,
do_model_average=True,
regularizer=paddle.regularizer.L1Decay(),
),
)
weight = fc.parameters()[0]
static_param = weight._to_static_var()
self._assert_to_static(weight, static_param, True)

def _assert_to_static(self, tensor, static_var, is_param=False):
if is_param:
self.assertTrue(isinstance(static_var, base.framework.Parameter))
self.assertTrue(static_var.persistable, True)
if isinstance(tensor, base.framework.EagerParamBase):
for attr in ["trainable", "is_distributed", "do_model_average"]:
self.assertEqual(
getattr(tensor, attr), getattr(static_var, attr)
)

self.assertEqual(
static_var.optimize_attr["learning_rate"], 0.001
)
self.assertTrue(
isinstance(
static_var.regularizer, paddle.regularizer.L1Decay
)
)
else:
self.assertTrue(isinstance(static_var, base.framework.Variable))

attr_keys = ["block", "dtype", "type", "name"]
for attr in attr_keys:
if isinstance(getattr(tensor, attr), core.DataType):
self.assertEqual(
paddle_type_to_proto_type[getattr(tensor, attr)],
getattr(static_var, attr),
)
else:
self.assertEqual(
getattr(tensor, attr),
getattr(static_var, attr),
)

self.assertListEqual(list(tensor.shape), list(static_var.shape))


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
# Copyright (c) 2024 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.
Expand All @@ -21,10 +21,9 @@
import paddle.nn.functional as F
from paddle import base
from paddle.base import core
from paddle.base.framework import paddle_type_to_proto_type


class TestVarBase(unittest.TestCase):
class TestEagerTensor(unittest.TestCase):
def setUp(self):
self.shape = [512, 1234]
self.dtype = np.float32
Expand Down Expand Up @@ -607,10 +606,6 @@ def test_gradient(self):
grad_var = var.gradient()
self.assertEqual(grad_var.shape, self.array.shape)

def test_block(self):
var = paddle.to_tensor(self.array)
self.assertEqual(var.block, base.default_main_program().global_block())

def _test_slice(self):
w = paddle.to_tensor(
np.random.random((784, 100, 100)).astype("float64")
Expand Down Expand Up @@ -1026,12 +1021,12 @@ def test_slice(self):
mask = np.array([1, 0, 1, 0], dtype=bool)
var[paddle.to_tensor([0, 1]), mask]

def test_var_base_to_np(self):
def test_tensor_to_np(self):
with base.dygraph.guard():
var = paddle.to_tensor(self.array)
np.testing.assert_array_equal(var.numpy(), var.numpy(False))

def test_var_base_as_np(self):
def test_tensor_as_np(self):
with base.dygraph.guard():
var = paddle.to_tensor(self.array)
np.testing.assert_array_equal(var.numpy(), np.array(var))
Expand All @@ -1058,67 +1053,6 @@ def test_if(self):
assert not bool(var1), "bool(var1) is False"
assert bool(var2), "bool(var2) is True"

def test_to_static_var(self):
with base.dygraph.guard():
# Convert Tensor into Variable or Parameter
var_base = paddle.to_tensor(self.array)
static_var = var_base._to_static_var()
self._assert_to_static(var_base, static_var)

var_base = paddle.to_tensor(self.array)
static_param = var_base._to_static_var(to_parameter=True)
self._assert_to_static(var_base, static_param, True)

# Convert EagerParamBase into Parameter
fc = paddle.nn.Linear(
10,
20,
weight_attr=paddle.ParamAttr(
learning_rate=0.001,
do_model_average=True,
regularizer=paddle.regularizer.L1Decay(),
),
)
weight = fc.parameters()[0]
static_param = weight._to_static_var()
self._assert_to_static(weight, static_param, True)

def _assert_to_static(self, var_base, static_var, is_param=False):
if is_param:
self.assertTrue(isinstance(static_var, base.framework.Parameter))
self.assertTrue(static_var.persistable, True)
if isinstance(var_base, base.framework.EagerParamBase):
for attr in ["trainable", "is_distributed", "do_model_average"]:
self.assertEqual(
getattr(var_base, attr), getattr(static_var, attr)
)

self.assertEqual(
static_var.optimize_attr["learning_rate"], 0.001
)
self.assertTrue(
isinstance(
static_var.regularizer, paddle.regularizer.L1Decay
)
)
else:
self.assertTrue(isinstance(static_var, base.framework.Variable))

attr_keys = ["block", "dtype", "type", "name"]
for attr in attr_keys:
if isinstance(getattr(var_base, attr), core.DataType):
self.assertEqual(
paddle_type_to_proto_type[getattr(var_base, attr)],
getattr(static_var, attr),
)
else:
self.assertEqual(
getattr(var_base, attr),
getattr(static_var, attr),
)

self.assertListEqual(list(var_base.shape), list(static_var.shape))

def test_tensor_str(self):
paddle.enable_static()
paddle.disable_static(paddle.CPUPlace())
Expand Down Expand Up @@ -1254,7 +1188,7 @@ def test_print_tensor_dtype(self):
self.assertEqual(a_str, expected)


class TestVarBaseSetitem(unittest.TestCase):
class TestEagerTensorSetitem(unittest.TestCase):
def func_setUp(self):
self.set_dtype()
self.tensor_x = paddle.to_tensor(np.ones((4, 2, 3)).astype(self.dtype))
Expand Down Expand Up @@ -1306,12 +1240,12 @@ def test_value_int(self):
self.func_test_value_int()


class TestVarBaseSetitemInt64(TestVarBaseSetitem):
class TestEagerTensorSetitemInt64(TestEagerTensorSetitem):
def set_dtype(self):
self.dtype = "int64"


class TestVarBaseSetitemFp32(TestVarBaseSetitem):
class TestEagerTensorSetitemFp32(TestEagerTensorSetitem):
def set_dtype(self):
self.dtype = "float32"

Expand All @@ -1324,12 +1258,12 @@ def test_value_float(self):
self.func_test_value_float()


class TestVarBaseSetitemFp64(TestVarBaseSetitem):
class TestEagerTensorSetitemFp64(TestEagerTensorSetitem):
def set_dtype(self):
self.dtype = "float64"


class TestVarBaseSetitemBoolIndex(unittest.TestCase):
class TestEagerTensorSetitemBoolIndex(unittest.TestCase):
def func_setUp(self):
paddle.disable_static()
self.set_dtype()
Expand Down Expand Up @@ -1394,7 +1328,7 @@ def test_value_int(self):
self.func_test_value_int()


class TestVarBaseSetitemBoolScalarIndex(unittest.TestCase):
class TestEagerTensorSetitemBoolScalarIndex(unittest.TestCase):
def set_input(self):
self.tensor_x = paddle.to_tensor(np.ones((1, 2, 3)).astype(self.dtype))
self.np_value = np.random.random((2, 3)).astype(self.dtype)
Expand All @@ -1419,7 +1353,7 @@ def _test(self, value):
self.assertEqual(id_origin, id(self.tensor_x))


class TestVarBaseInplaceVersion(unittest.TestCase):
class TestEagerTensorInplaceVersion(unittest.TestCase):
def test_setitem(self):
paddle.disable_static()

Expand All @@ -1444,7 +1378,7 @@ def test_bump_inplace_version(self):
self.assertEqual(var.inplace_version, 2)


class TestVarBaseSlice(unittest.TestCase):
class TestEagerTensorSlice(unittest.TestCase):
def test_slice(self):
paddle.disable_static()
np_x = np.random.random((3, 8, 8))
Expand All @@ -1454,7 +1388,7 @@ def test_slice(self):
self.assertEqual(actual_x.numpy().all(), np_x[0:1].all())


class TestVarBaseClear(unittest.TestCase):
class TestEagerTensorClear(unittest.TestCase):
def test_clear(self):
paddle.disable_static()
np_x = np.random.random((3, 8, 8))
Expand All @@ -1463,7 +1397,7 @@ def test_clear(self):
self.assertEqual(str(x), "Tensor(Not initialized)")


class TestVarBaseOffset(unittest.TestCase):
class TestEagerTensorOffset(unittest.TestCase):
def test_offset(self):
paddle.disable_static()
np_x = np.random.random((3, 8, 8))
Expand All @@ -1474,7 +1408,7 @@ def test_offset(self):
self.assertEqual(actual_x._offset(), expected_offset)


class TestVarBaseShareBufferTo(unittest.TestCase):
class TestEagerTensorShareBufferTo(unittest.TestCase):
def test_share_buffer_To(self):
paddle.disable_static()
np_src = np.random.random((3, 8, 8))
Expand All @@ -1485,7 +1419,7 @@ def test_share_buffer_To(self):
self.assertEqual(src._is_shared_buffer_with(dst), True)


class TestVarBaseTo(unittest.TestCase):
class TestEagerTensorTo(unittest.TestCase):
def func_setUp(self):
paddle.disable_static()
self.np_x = np.random.random((3, 8, 8))
Expand Down Expand Up @@ -1541,8 +1475,8 @@ def test_to_api(self):
self.func_test_to_api()


class TestVarBaseInitVarBaseFromTensorWithDevice(unittest.TestCase):
def test_varbase_init(self):
class TestEagerTensorInitEagerTensorFromTensorWithDevice(unittest.TestCase):
def test_tensor_init(self):
paddle.disable_static()
t = base.Tensor()
np_x = np.random.random((3, 8, 8))
Expand All @@ -1559,7 +1493,7 @@ def test_varbase_init(self):
self.assertEqual(tmp.numpy().all(), np_x.all())


class TestVarBaseNumel(unittest.TestCase):
class TestEagerTensorNumel(unittest.TestCase):
def test_numel_normal(self):
paddle.disable_static()
np_x = np.random.random((3, 8, 8))
Expand All @@ -1575,7 +1509,7 @@ def test_numel_without_holder(self):
self.assertEqual(x_actual_numel, 0)


class TestVarBaseCopyGradientFrom(unittest.TestCase):
class TestEagerTensorCopyGradientFrom(unittest.TestCase):
def test_copy_gradient_from(self):
paddle.disable_static()
np_x = np.random.random((2, 2))
Expand Down
4 changes: 2 additions & 2 deletions tools/parallel_UT_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,7 @@
'test_multihead_attention',
'test_grid_sampler_op',
'test_initializer_nn',
'test_var_base',
'test_eager_tensor',
'test_fuse_elewise_add_act_pass',
'test_select_input_output_op',
'test_lstm_op',
Expand Down Expand Up @@ -2851,7 +2851,7 @@
'test_trt_tuned_dynamic_shape',
'test_quantization_pass',
'test_trt_fc_fuse_pass',
'test_var_base',
'test_eager_tensor',
'trt_split_converter_test',
'test_user_defined_quantization',
'test_quantization_scale_pass',
Expand Down
2 changes: 1 addition & 1 deletion tools/windows/run_unittests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ disable_win_inference_test="^trt_quant_int8_yolov3_r50_test$|\
^test_stack_op$|\
^trt_split_converter_test$|\
^paddle_infer_api_copy_tensor_tester$|\
^test_var_base$|\
^test_eager_tensor$|\
^test_einsum_v2$|\
^test_tensor_scalar_type_promotion_static$|\
^test_matrix_power_op$|\
Expand Down