Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cleanup][A-7] Replace VarType #61548

Merged
merged 3 commits into from
Feb 4, 2024
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
6 changes: 3 additions & 3 deletions test/legacy_test/test_rand_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,17 @@ def test_default_dtype(self):
def test_default_fp16():
paddle.framework.set_default_dtype('float16')
out = paddle.tensor.random.rand([2, 3])
self.assertEqual(out.dtype, base.core.VarDesc.VarType.FP16)
self.assertEqual(out.dtype, paddle.float16)

def test_default_fp32():
paddle.framework.set_default_dtype('float32')
out = paddle.tensor.random.rand([2, 3])
self.assertEqual(out.dtype, base.core.VarDesc.VarType.FP32)
self.assertEqual(out.dtype, paddle.float32)

def test_default_fp64():
paddle.framework.set_default_dtype('float64')
out = paddle.tensor.random.rand([2, 3])
self.assertEqual(out.dtype, base.core.VarDesc.VarType.FP64)
self.assertEqual(out.dtype, paddle.float64)

if paddle.is_compiled_with_cuda():
paddle.set_device('gpu')
Expand Down
27 changes: 14 additions & 13 deletions test/legacy_test/test_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import numpy as np

import paddle
from paddle import base
from paddle.base import core

Expand Down Expand Up @@ -344,7 +345,7 @@ def test_tensor_poiter(self):
var = scope.var("test_tensor")
place = core.CPUPlace()
tensor = var.get_tensor()
dtype = core.VarDesc.VarType.FP32
dtype = paddle.float32
self.assertTrue(
isinstance(tensor._mutable_data(place, dtype), numbers.Integral)
)
Expand All @@ -370,37 +371,37 @@ def test_tensor_set_fp16(self):
tensor = base.Tensor()
place = core.CPUPlace()
tensor.set(array, place)
self.assertEqual(tensor._dtype(), core.VarDesc.VarType.FP16)
self.assertEqual(tensor._dtype(), paddle.float16)
np.testing.assert_array_equal(np.array(tensor), array)

if core.is_compiled_with_cuda():
place = core.CUDAPlace(0)
tensor.set(array, place)
self.assertEqual(tensor._dtype(), core.VarDesc.VarType.FP16)
self.assertEqual(tensor._dtype(), paddle.float16)
np.testing.assert_array_equal(np.array(tensor), array)

place = core.CUDAPinnedPlace()
tensor.set(array, place)
self.assertEqual(tensor._dtype(), core.VarDesc.VarType.FP16)
self.assertEqual(tensor._dtype(), paddle.float16)
np.testing.assert_array_equal(np.array(tensor), array)

def test_tensor_set_int16(self):
array = np.random.randint(100, size=(300, 500)).astype("int16")
tensor = base.Tensor()
place = core.CPUPlace()
tensor.set(array, place)
self.assertEqual(tensor._dtype(), core.VarDesc.VarType.INT16)
self.assertEqual(tensor._dtype(), paddle.int16)
np.testing.assert_array_equal(np.array(tensor), array)

if core.is_compiled_with_cuda():
place = core.CUDAPlace(0)
tensor.set(array, place)
self.assertEqual(tensor._dtype(), core.VarDesc.VarType.INT16)
self.assertEqual(tensor._dtype(), paddle.int16)
np.testing.assert_array_equal(np.array(tensor), array)

place = core.CUDAPinnedPlace()
tensor.set(array, place)
self.assertEqual(tensor._dtype(), core.VarDesc.VarType.INT16)
self.assertEqual(tensor._dtype(), paddle.int16)
np.testing.assert_array_equal(np.array(tensor), array)

def test_tensor_set_from_array_list(self):
Expand Down Expand Up @@ -447,7 +448,7 @@ def test_tensor_set_item_complex128(self):
place = core.CPUPlace()
tensor.set(array, place)

self.assertEqual(tensor._dtype(), core.VarDesc.VarType.COMPLEX128)
self.assertEqual(tensor._dtype(), paddle.complex128)
tensor._set_complex128_element(0, 42.1 + 42.1j)
np.testing.assert_allclose(
tensor._get_complex128_element(0), 42.1 + 42.1j
Expand All @@ -456,15 +457,15 @@ def test_tensor_set_item_complex128(self):
if core.is_compiled_with_cuda():
place = core.CUDAPlace(0)
tensor.set(array, place)
self.assertEqual(tensor._dtype(), core.VarDesc.VarType.COMPLEX128)
self.assertEqual(tensor._dtype(), paddle.complex128)
tensor._set_complex128_element(0, 42.1 + 42.1j)
np.testing.assert_allclose(
tensor._get_complex128_element(0), 42.1 + 42.1j
)

place = core.CUDAPinnedPlace()
tensor.set(array, place)
self.assertEqual(tensor._dtype(), core.VarDesc.VarType.COMPLEX128)
self.assertEqual(tensor._dtype(), paddle.complex128)
tensor._set_complex128_element(0, 42.1 + 42.1j)
np.testing.assert_allclose(
tensor._get_complex128_element(0), 42.1 + 42.1j
Expand All @@ -478,7 +479,7 @@ def test_tensor_set_item_complex64(self):
place = core.CPUPlace()
tensor.set(array, place)

self.assertEqual(tensor._dtype(), core.VarDesc.VarType.COMPLEX64)
self.assertEqual(tensor._dtype(), paddle.complex64)
tensor._set_complex64_element(0, 42.1 + 42.1j)
np.testing.assert_allclose(
np.complex64(tensor._get_complex64_element(0)),
Expand All @@ -488,7 +489,7 @@ def test_tensor_set_item_complex64(self):
if core.is_compiled_with_cuda():
place = core.CUDAPlace(0)
tensor.set(array, place)
self.assertEqual(tensor._dtype(), core.VarDesc.VarType.COMPLEX64)
self.assertEqual(tensor._dtype(), paddle.complex64)
tensor._set_complex64_element(0, 42.1 + 42.1j)
np.testing.assert_allclose(
np.complex64(tensor._get_complex64_element(0)),
Expand All @@ -497,7 +498,7 @@ def test_tensor_set_item_complex64(self):

place = core.CUDAPinnedPlace()
tensor.set(array, place)
self.assertEqual(tensor._dtype(), core.VarDesc.VarType.COMPLEX64)
self.assertEqual(tensor._dtype(), paddle.complex64)
tensor._set_complex64_element(0, 42.1 + 42.1j)
np.testing.assert_allclose(
np.complex64(tensor._get_complex64_element(0)),
Expand Down
12 changes: 6 additions & 6 deletions test/legacy_test/test_uniform_random_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def test_out_dtype():
if paddle.framework.in_pir_mode():
self.assertEqual(out.dtype, base.core.DataType.FLOAT64)
else:
self.assertEqual(out.dtype, base.core.VarDesc.VarType.FP64)
self.assertEqual(out.dtype, paddle.float64)

test_out_dtype()
paddle.disable_static()
Expand Down Expand Up @@ -595,7 +595,7 @@ def test_out_dtype():
if paddle.framework.in_pir_mode():
self.assertEqual(out.dtype, base.core.DataType.FLOAT64)
else:
self.assertEqual(out.dtype, base.core.VarDesc.VarType.FP64)
self.assertEqual(out.dtype, paddle.float64)

test_out_dtype()
paddle.disable_static()
Expand All @@ -619,25 +619,25 @@ def test_default_dtype(self):
def test_default_fp16():
paddle.framework.set_default_dtype('float16')
out = paddle.tensor.random.uniform([2, 3])
self.assertEqual(out.dtype, base.core.VarDesc.VarType.FP16)
self.assertEqual(out.dtype, paddle.float16)

def test_default_fp32():
paddle.framework.set_default_dtype('float32')
out = paddle.tensor.random.uniform([2, 3])
self.assertEqual(out.dtype, base.core.VarDesc.VarType.FP32)
self.assertEqual(out.dtype, paddle.float32)

def test_default_fp64():
paddle.framework.set_default_dtype('float64')
out = paddle.tensor.random.uniform([2, 3])
self.assertEqual(out.dtype, base.core.VarDesc.VarType.FP64)
self.assertEqual(out.dtype, paddle.float64)

def test_dygraph_fp16():
if not paddle.is_compiled_with_cuda():
paddle.enable_static()
return
paddle.set_device('gpu')
out = paddle.uniform([2, 3], dtype=paddle.float16)
self.assertEqual(out.dtype, base.core.VarDesc.VarType.FP16)
self.assertEqual(out.dtype, paddle.float16)

if paddle.is_compiled_with_cuda():
paddle.set_device('gpu')
Expand Down
58 changes: 27 additions & 31 deletions test/legacy_test/test_var_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def check_with_place(place):
# set_default_dtype should not take effect on int
x = paddle.to_tensor(1, place=place, stop_gradient=False)
np.testing.assert_array_equal(x.numpy(), [1])
self.assertNotEqual(x.dtype, core.VarDesc.VarType.FP32)
self.assertNotEqual(x.dtype, paddle.float32)

y = paddle.to_tensor(2, place=x.place)
self.assertEqual(str(x.place), str(y.place))
Expand All @@ -50,23 +50,23 @@ def check_with_place(place):
np.testing.assert_array_equal(
x.numpy(), np.array([1.2], 'float16')
)
self.assertEqual(x.dtype, core.VarDesc.VarType.FP16)
self.assertEqual(x.dtype, paddle.float16)

# set_default_dtype take effect on int
x = paddle.to_tensor(1, place=place)
self.assertTrue(x.dtype, core.VarDesc.VarType.INT64)
self.assertTrue(x.dtype, paddle.int64)

# set_default_dtype take effect on float
x = paddle.to_tensor(1.2, place=place, stop_gradient=False)
np.testing.assert_array_equal(
x.numpy(), np.array([1.2]).astype('float32')
)
self.assertEqual(x.dtype, core.VarDesc.VarType.FP32)
self.assertEqual(x.dtype, paddle.float32)
clone_x = x.clone()
np.testing.assert_array_equal(
clone_x.numpy(), np.array([1.2]).astype('float32')
)
self.assertEqual(clone_x.dtype, core.VarDesc.VarType.FP32)
self.assertEqual(clone_x.dtype, paddle.float32)
y = clone_x**2
y.backward()
np.testing.assert_array_equal(
Expand Down Expand Up @@ -97,27 +97,27 @@ def check_with_place(place):
# support 'dtype' is core.VarType
x = paddle.rand((2, 2))
y = paddle.to_tensor([2, 2], dtype=x.dtype)
self.assertEqual(y.dtype, core.VarDesc.VarType.FP32)
self.assertEqual(y.dtype, paddle.float32)

# set_default_dtype take effect on complex
x = paddle.to_tensor(1 + 2j, place=place, stop_gradient=False)
np.testing.assert_array_equal(x.numpy(), [1 + 2j])
self.assertEqual(x.dtype, core.VarDesc.VarType.COMPLEX64)
self.assertEqual(x.dtype, paddle.complex64)

paddle.set_default_dtype('float64')
x = paddle.to_tensor(1.2, place=place, stop_gradient=False)
np.testing.assert_array_equal(x.numpy(), [1.2])
self.assertEqual(x.dtype, core.VarDesc.VarType.FP64)
self.assertEqual(x.dtype, paddle.float64)

x = paddle.to_tensor(1 + 2j, place=place, stop_gradient=False)
np.testing.assert_array_equal(x.numpy(), [1 + 2j])
self.assertEqual(x.dtype, core.VarDesc.VarType.COMPLEX128)
self.assertEqual(x.dtype, paddle.complex128)

x = paddle.to_tensor(
1, dtype='float32', place=place, stop_gradient=False
)
np.testing.assert_array_equal(x.numpy(), [1.0])
self.assertEqual(x.dtype, core.VarDesc.VarType.FP32)
self.assertEqual(x.dtype, paddle.float32)
self.assertEqual(x.shape, [])
self.assertEqual(x.stop_gradient, False)
self.assertEqual(x.type, core.VarDesc.VarType.LOD_TENSOR)
Expand All @@ -129,7 +129,7 @@ def check_with_place(place):
[1, 2], dtype='float32', place=place, stop_gradient=False
)
np.testing.assert_array_equal(x.numpy(), [1.0, 2.0])
self.assertEqual(x.dtype, core.VarDesc.VarType.FP32)
self.assertEqual(x.dtype, paddle.float32)
self.assertIsNone(x.grad)
self.assertEqual(x.shape, [2])
self.assertEqual(x.stop_gradient, False)
Expand All @@ -142,15 +142,15 @@ def check_with_place(place):
stop_gradient=False,
)
np.testing.assert_array_equal(x.numpy(), self.array)
self.assertEqual(x.dtype, core.VarDesc.VarType.FP32)
self.assertEqual(x.dtype, paddle.float32)
self.assertEqual(x.shape, self.shape)
self.assertEqual(x.stop_gradient, False)
self.assertEqual(x.type, core.VarDesc.VarType.LOD_TENSOR)

y = paddle.to_tensor(x)
y = paddle.to_tensor(y, dtype='float64', place=place)
np.testing.assert_array_equal(y.numpy(), self.array)
self.assertEqual(y.dtype, core.VarDesc.VarType.FP64)
self.assertEqual(y.dtype, paddle.float64)
self.assertEqual(y.shape, self.shape)
self.assertEqual(y.stop_gradient, True)
self.assertEqual(y.type, core.VarDesc.VarType.LOD_TENSOR)
Expand All @@ -162,7 +162,7 @@ def check_with_place(place):
)
y = paddle.to_tensor(x)
np.testing.assert_array_equal(x.numpy(), [1 + 2j, 1 - 2j])
self.assertEqual(y.dtype, core.VarDesc.VarType.COMPLEX64)
self.assertEqual(y.dtype, paddle.complex64)
self.assertEqual(y.shape, [2])

paddle.set_default_dtype('float32')
Expand Down Expand Up @@ -252,21 +252,21 @@ def check_with_place(place):

# test dtype=bfloat16
x = paddle.to_tensor(-1e6, dtype=paddle.bfloat16)
self.assertEqual(x.dtype, core.VarDesc.VarType.BF16)
self.assertEqual(x.dtype, paddle.bfloat16)
self.assertTrue(x == -999424.0)
self.assertTrue(x.item() == -999424.0)
self.assertTrue(isinstance(x.item(), float))

x = paddle.to_tensor([-1e6, -1e6, -1e6], dtype='bfloat16')
self.assertEqual(x.dtype, core.VarDesc.VarType.BF16)
self.assertEqual(x.dtype, paddle.bfloat16)
self.assertTrue(x[0] == -999424.0)
self.assertTrue(x[1] == -999424.0)
self.assertTrue(x[2] == -999424.0)

x = paddle.to_tensor(
-1e6, dtype=paddle.bfloat16, stop_gradient=False
)
self.assertEqual(x.dtype, core.VarDesc.VarType.BF16)
self.assertEqual(x.dtype, paddle.bfloat16)
self.assertTrue(x == -999424.0)
y = x * x
y.backward()
Expand All @@ -275,19 +275,19 @@ def check_with_place(place):
# test default_type=bfloat16
paddle.set_default_dtype('bfloat16')
x = paddle.to_tensor(-1e6)
self.assertEqual(x.dtype, core.VarDesc.VarType.BF16)
self.assertEqual(x.dtype, paddle.bfloat16)
self.assertTrue(x == -999424.0)
self.assertTrue(x.item() == -999424.0)
self.assertTrue(isinstance(x.item(), float))

x = paddle.to_tensor([-1e6, -1e6, -1e6])
self.assertEqual(x.dtype, core.VarDesc.VarType.BF16)
self.assertEqual(x.dtype, paddle.bfloat16)
self.assertTrue(x[0] == -999424.0)
self.assertTrue(x[1] == -999424.0)
self.assertTrue(x[2] == -999424.0)

x = paddle.to_tensor(-1e6, stop_gradient=False)
self.assertEqual(x.dtype, core.VarDesc.VarType.BF16)
self.assertEqual(x.dtype, paddle.bfloat16)
self.assertTrue(x == -999424.0)
y = x * x
y.backward()
Expand Down Expand Up @@ -1224,7 +1224,7 @@ def test_tensor_str_linewidth2(self):
def test_tensor_str_bf16(self):
paddle.disable_static(paddle.CPUPlace())
a = paddle.to_tensor([[1.5, 1.0], [0, 0]])
a = paddle.cast(a, dtype=core.VarDesc.VarType.BF16)
a = paddle.cast(a, dtype=paddle.bfloat16)
paddle.set_printoptions(precision=4)
a_str = str(a)

Expand Down Expand Up @@ -1483,11 +1483,11 @@ def func_setUp(self):

def func_test_to_api(self):
x_double = self.x._to(dtype='double')
self.assertEqual(x_double.dtype, paddle.base.core.VarDesc.VarType.FP64)
self.assertEqual(x_double.dtype, paddle.float64)
np.testing.assert_allclose(self.np_x, x_double, rtol=1e-05)

x_ = self.x._to()
self.assertEqual(self.x.dtype, paddle.base.core.VarDesc.VarType.FP64)
self.assertEqual(self.x.dtype, paddle.float64)
np.testing.assert_allclose(self.np_x, x_, rtol=1e-05)

if paddle.base.is_compiled_with_cuda():
Expand All @@ -1502,16 +1502,12 @@ def func_test_to_api(self):
x_gpu1 = self.x._to(device='gpu:0', dtype="float64")
self.assertTrue(x_gpu1.place.is_gpu_place())
self.assertEqual(x_gpu1.place.gpu_device_id(), 0)
self.assertEqual(
x_gpu1.dtype, paddle.base.core.VarDesc.VarType.FP64
)
self.assertEqual(x_gpu1.dtype, paddle.float64)

x_gpu2 = self.x._to(device='gpu:0', dtype="float16")
self.assertTrue(x_gpu2.place.is_gpu_place())
self.assertEqual(x_gpu2.place.gpu_device_id(), 0)
self.assertEqual(
x_gpu2.dtype, paddle.base.core.VarDesc.VarType.FP16
)
self.assertEqual(x_gpu2.dtype, paddle.float16)

x_cpu = self.x._to(device=paddle.CPUPlace())
self.assertTrue(x_cpu.place.is_cpu_place())
Expand All @@ -1521,11 +1517,11 @@ def func_test_to_api(self):

x_cpu1 = self.x._to(device=paddle.CPUPlace(), dtype="float64")
self.assertTrue(x_cpu1.place.is_cpu_place())
self.assertEqual(x_cpu1.dtype, paddle.base.core.VarDesc.VarType.FP64)
self.assertEqual(x_cpu1.dtype, paddle.float64)

x_cpu2 = self.x._to(device='cpu', dtype="float16")
self.assertTrue(x_cpu2.place.is_cpu_place())
self.assertEqual(x_cpu2.dtype, paddle.base.core.VarDesc.VarType.FP16)
self.assertEqual(x_cpu2.dtype, paddle.float16)

self.assertRaises(ValueError, self.x._to, device=1)
self.assertRaises(AssertionError, self.x._to, blocking=1)
Expand Down