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 1 commit
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
11 changes: 6 additions & 5 deletions test/legacy_test/test_protobuf_descs.py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个文件直接恢复吧,这个暂时不适合修改

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

收到

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import unittest

import paddle
from paddle.base import core
from paddle.base.framework import Program

Expand Down Expand Up @@ -133,8 +134,8 @@ def test_dtype(self):
block = program_desc.block(0)
var = block.var(b'my_var')
var.set_type(core.VarDesc.VarType.LOD_TENSOR)
var.set_dtype(core.VarDesc.VarType.INT32)
self.assertEqual(core.VarDesc.VarType.INT32, var.dtype())
var.set_dtype(paddle.int32)
self.assertEqual(paddle.int32, var.dtype())
self.assertEqual(core.VarDesc.VarType.LOD_TENSOR, var.type())

def test_multiple_dtype(self):
Expand All @@ -143,9 +144,9 @@ def test_multiple_dtype(self):
var = block.var(b'my_reader')
var.set_type(core.VarDesc.VarType.READER)
src_types = [
core.VarDesc.VarType.INT32,
core.VarDesc.VarType.FP64,
core.VarDesc.VarType.FP32,
paddle.int32,
paddle.float64,
paddle.float32,
]
var.set_dtypes(src_types)
self.assertEqual(src_types, var.dtypes())
Expand Down
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
16 changes: 8 additions & 8 deletions test/legacy_test/test_uniform_random_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ def test_out_dtype():
shape=[3, 4], dtype='float64'
)
if paddle.framework.in_pir_mode():
self.assertEqual(out.dtype, base.core.DataType.FLOAT64)
self.assertEqual(out.dtype, paddle.float64)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

datatype 不能改

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 @@ -593,9 +593,9 @@ def test_out_dtype():
shape=[3, 4], dtype='float64'
)
if paddle.framework.in_pir_mode():
self.assertEqual(out.dtype, base.core.DataType.FLOAT64)
self.assertEqual(out.dtype, paddle.float64)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上,datatype 不能改

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
Loading