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-1,A-2,A-3,A-4] clean some VarType for test #61466

Merged
merged 3 commits into from
Feb 3, 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
13 changes: 6 additions & 7 deletions test/auto_parallel/amp_o2_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import paddle
from paddle.distributed.fleet import auto
from paddle.framework import core

paddle.enable_static()

Expand Down Expand Up @@ -98,11 +97,11 @@ def check_bf16(self, program):
num_fp32 = 0

for p in program.all_parameters():
if p.dtype == core.VarDesc.VarType.FP32:
if p.dtype == paddle.float32:
num_fp32 += 1
if p.dtype == core.VarDesc.VarType.FP16:
if p.dtype == paddle.float16:
num_fp16 += 1
if p.dtype == core.VarDesc.VarType.BF16:
if p.dtype == paddle.bfloat16:
num_bf16 += 1

self.assertEqual(num_bf16, 26)
Expand All @@ -115,11 +114,11 @@ def check_fp16(self, program):
num_fp32 = 0

for p in program.all_parameters():
if p.dtype == core.VarDesc.VarType.FP32:
if p.dtype == paddle.float32:
num_fp32 += 1
if p.dtype == core.VarDesc.VarType.FP16:
if p.dtype == paddle.float16:
num_fp16 += 1
if p.dtype == core.VarDesc.VarType.BF16:
if p.dtype == paddle.bfloat16:
num_bf16 += 1

self.assertEqual(num_bf16, 0)
Expand Down
19 changes: 6 additions & 13 deletions test/auto_parallel/test_pass_bf16.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import paddle
from paddle import nn
from paddle.base import core
from paddle.distributed.fleet import auto
from paddle.static import InputSpec
from paddle.static.amp.bf16.amp_utils import _valid_types
Expand Down Expand Up @@ -139,7 +138,7 @@ def check_program(self, program):
break

if op.type in bf16_op_list:
assert var.dtype == core.VarDesc.VarType.BF16
assert var.dtype == paddle.bfloat16
if "cast_bf16" in in_var_name:
if "@GRAD" in in_var_name:
tmp_in_var_name = in_var_name[
Expand All @@ -155,18 +154,15 @@ def check_program(self, program):
for in_name in prev_op.input_names:
for in_var_name in prev_op.input(in_name):
var = block.var(in_var_name)
assert (
var.dtype
== core.VarDesc.VarType.FP32
)
assert var.dtype == paddle.float32

elif op.type in fp32_op_list:
if (
op.type == "softmax_with_cross_entropy"
or op.type == "softmax_with_cross_entropy_grad"
) and in_var_name == "label0":
continue
assert var.dtype == core.VarDesc.VarType.FP32
assert var.dtype == paddle.float32
if "cast_fp32" in in_var_name:
prev_op = find_true_prev_op(
block.ops, op, tmp_in_var_name
Expand All @@ -176,10 +172,7 @@ def check_program(self, program):
for in_name in prev_op.input_names:
for in_var_name in prev_op.input(in_name):
var = block.var(in_var_name)
assert (
var.dtype
== core.VarDesc.VarType.BF16
)
assert var.dtype == paddle.bfloat16

for out_name in op.output_names:
for out_var_name in op.output(out_name):
Expand All @@ -192,9 +185,9 @@ def check_program(self, program):
if var is None or var.type not in _valid_types:
break
if op.type in bf16_op_list:
assert var.dtype == core.VarDesc.VarType.BF16
assert var.dtype == paddle.bfloat16
elif op.type in fp32_op_list:
assert var.dtype == core.VarDesc.VarType.FP32
assert var.dtype == paddle.float32

def test_bf16_pass(self):
bf16_o1_engine = self.get_engine(True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ def amp_guard_white_op(self):
with paddle.amp.amp_guard(False):
out_fp32 = conv2d(data)

self.assertTrue(data.dtype == base.core.VarDesc.VarType.FP32)
self.assertTrue(out_fp16.dtype == base.core.VarDesc.VarType.FP16)
self.assertTrue(out_fp32.dtype == base.core.VarDesc.VarType.FP32)
self.assertTrue(data.dtype == paddle.float32)
self.assertTrue(out_fp16.dtype == paddle.float16)
self.assertTrue(out_fp32.dtype == paddle.float32)

def test_amp_guard_white_op(self):
self.amp_guard_white_op()
Expand All @@ -79,8 +79,8 @@ def amp_guard_black_op(self):
with paddle.amp.amp_guard(True):
out_fp32 = paddle.mean(data)

self.assertTrue(data.dtype == base.core.VarDesc.VarType.FP32)
self.assertTrue(out_fp32.dtype == base.core.VarDesc.VarType.FP32)
self.assertTrue(data.dtype == paddle.float32)
self.assertTrue(out_fp32.dtype == paddle.float32)

def test_amp_guard_black_op(self):
self.amp_guard_black_op()
Expand Down Expand Up @@ -164,15 +164,11 @@ def amp_guard_upsupported_fp16_op(self):
out_purefp16_fp32 = paddle.expand_as(
out_purefp16_fp16, out_purefp16_fp16
) # expand_as_v2 has no fp16 kernel
self.assertTrue(data.dtype == base.core.VarDesc.VarType.FP32)
self.assertTrue(out_amp_fp16.dtype == base.core.VarDesc.VarType.FP16)
self.assertTrue(out_amp_fp32.dtype == base.core.VarDesc.VarType.FP32)
self.assertTrue(
out_purefp16_fp16.dtype == base.core.VarDesc.VarType.FP16
)
self.assertTrue(
out_purefp16_fp32.dtype == base.core.VarDesc.VarType.FP32
)
self.assertTrue(data.dtype == paddle.float32)
self.assertTrue(out_amp_fp16.dtype == paddle.float16)
self.assertTrue(out_amp_fp32.dtype == paddle.float32)
self.assertTrue(out_purefp16_fp16.dtype == paddle.float16)
self.assertTrue(out_purefp16_fp32.dtype == paddle.float32)

def test_amp_guard_upsupported_fp16_op(self):
self.amp_guard_upsupported_fp16_op()
Expand Down Expand Up @@ -1357,7 +1353,7 @@ def func_isinstance():
with paddle.amp.auto_cast(custom_white_list=['layer_norm']):
out = layer_norm(x)

self.assertTrue(out.dtype == base.core.VarDesc.VarType.FP16)
self.assertTrue(out.dtype == paddle.float16)

func_isinstance()

Expand Down
5 changes: 1 addition & 4 deletions test/custom_op/test_custom_conj.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@


def is_complex(dtype):
return (
dtype == paddle.base.core.VarDesc.VarType.COMPLEX64
or dtype == paddle.base.core.VarDesc.VarType.COMPLEX128
)
return dtype == paddle.complex64 or dtype == paddle.complex128


def to_complex(dtype):
Expand Down
8 changes: 2 additions & 6 deletions test/dygraph_to_static/test_to_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,17 +223,13 @@ def test_static(self):
if paddle.base.framework.use_pir_api():
self.assertTrue(x.dtype == paddle.base.libpaddle.DataType.INT16)
else:
self.assertTrue(
x.dtype == paddle.framework.core.VarDesc.VarType.INT16
)
self.assertTrue(x.dtype == paddle.int16)

y = paddle.to_tensor([1, 2], dtype="int16")
if paddle.base.framework.use_pir_api():
self.assertTrue(y.dtype == paddle.base.libpaddle.DataType.INT16)
else:
self.assertTrue(
y.dtype == paddle.framework.core.VarDesc.VarType.INT16
)
self.assertTrue(y.dtype == paddle.int16)


if __name__ == '__main__':
Expand Down
6 changes: 3 additions & 3 deletions test/ir/test_ir_fusion_group_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ def _feed_random_data(self, feed_vars):
raise TypeError("Feed data of non LoDTensor is not supported.")

shape = var.shape
if var.dtype == base.core.VarDesc.VarType.FP32:
if var.dtype == paddle.float32:
dtype = "float32"
elif var.dtype == base.core.VarDesc.VarType.FP64:
elif var.dtype == paddle.float64:
dtype = "float64"
elif var.dtype == base.core.VarDesc.VarType.FP16:
elif var.dtype == paddle.float16:
dtype = "float16"
else:
raise ValueError("Unsupported dtype %s" % var.dtype)
Expand Down
6 changes: 3 additions & 3 deletions test/legacy_test/gradient_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ def _product(t):


def dtype_to_np_dtype(dtype):
if dtype == core.VarDesc.VarType.FP32 or dtype == core.DataType.FLOAT32:
if dtype == paddle.float32 or dtype == core.DataType.FLOAT32:
return np.float32
elif dtype == core.VarDesc.VarType.FP64 or dtype == core.DataType.FLOAT64:
elif dtype == paddle.float64 or dtype == core.DataType.FLOAT64:
return np.float64
elif dtype == core.VarDesc.VarType.FP16 or dtype == core.DataType.FLOAT16:
elif dtype == paddle.float16 or dtype == core.DataType.FLOAT16:
return np.float16
else:
raise ValueError("Not supported data type " + str(dtype))
Expand Down
20 changes: 10 additions & 10 deletions test/legacy_test/op_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,19 +166,19 @@ def product(dim):
tensor_to_check = scope.find_var(input_to_check).get_tensor()
tensor_size = product(tensor_to_check.shape())
tensor_to_check_dtype = tensor_to_check._dtype()
if tensor_to_check_dtype == core.VarDesc.VarType.FP32:
if tensor_to_check_dtype == paddle.float32:
tensor_to_check_dtype = np.float32
elif tensor_to_check_dtype == core.VarDesc.VarType.FP64:
elif tensor_to_check_dtype == paddle.float64:
tensor_to_check_dtype = np.float64
elif tensor_to_check_dtype == core.VarDesc.VarType.FP16:
elif tensor_to_check_dtype == paddle.float16:
tensor_to_check_dtype = np.float16
# set delta as np.float16, will automatic convert to float32, float64
delta = np.array(delta).astype(np.float16)
elif tensor_to_check_dtype == core.VarDesc.VarType.BF16:
elif tensor_to_check_dtype == paddle.bfloat16:
tensor_to_check_dtype = np.float32
elif tensor_to_check_dtype == core.VarDesc.VarType.COMPLEX64:
elif tensor_to_check_dtype == paddle.complex64:
tensor_to_check_dtype = np.complex64
elif tensor_to_check_dtype == core.VarDesc.VarType.COMPLEX128:
elif tensor_to_check_dtype == paddle.complex128:
tensor_to_check_dtype = np.complex128
else:
raise ValueError(
Expand All @@ -196,7 +196,7 @@ def get_output():
# numpy.dtype does not have bfloat16, thus we use numpy.uint16 to
# store bfloat16 data, and need to be converted to float to check
# the floating precision.
if tensor_to_check._dtype() == core.VarDesc.VarType.BF16:
if tensor_to_check._dtype() == paddle.bfloat16:
output_numpy = convert_uint16_to_float(output_numpy)
sum.append(output_numpy.astype(tensor_to_check_dtype).mean())
return tensor_to_check_dtype(np.array(sum).sum() / len(output_names))
Expand All @@ -208,7 +208,7 @@ def __get_elem__(tensor, i):
numpy_tensor = np.array(tensor).astype(np.float16)
numpy_tensor = numpy_tensor.flatten()
return numpy_tensor[i]
elif tensor_to_check._dtype() == core.VarDesc.VarType.BF16:
elif tensor_to_check._dtype() == paddle.bfloat16:
numpy_tensor = np.array(tensor).astype(np.uint16)
numpy_tensor = numpy_tensor.flatten()
return struct.unpack(
Expand Down Expand Up @@ -236,7 +236,7 @@ def __set_elem__(tensor, i, e):
numpy_tensor[i] = e
numpy_tensor = numpy_tensor.reshape(shape)
tensor.set(numpy_tensor, place)
elif tensor_to_check._dtype() == core.VarDesc.VarType.BF16:
elif tensor_to_check._dtype() == paddle.bfloat16:
numpy_tensor = np.array(tensor).astype(np.uint16)
shape = numpy_tensor.shape
numpy_tensor = numpy_tensor.flatten()
Expand Down Expand Up @@ -3470,7 +3470,7 @@ def _get_dygraph_grad(
for cast_input in cast_inputs:
if isinstance(cast_input, paddle.Tensor):
cast_outputs.append(
paddle.cast(cast_input, core.VarDesc.VarType.FP32)
paddle.cast(cast_input, paddle.float32)
)
else:
raise TypeError(
Expand Down
4 changes: 2 additions & 2 deletions test/legacy_test/test_batch_norm_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ def __set_tensor__(name, data=None):
grad_tensor = scope.var(grad_var_name(name)).get_tensor()
out_dtype = out_tensor.dtype()
if data is None:
if out_dtype == core.VarDesc.VarType.FP64:
if out_dtype == paddle.float64:
data = np.ones(out_tensor.shape(), dtype=np.float64)
elif out_dtype == core.VarDesc.VarType.FP32:
elif out_dtype == paddle.float32:
data = np.ones(out_tensor.shape(), dtype=np.float32)
else:
raise ValueError("Not supported data type " + str(out_dtype))
Expand Down
3 changes: 1 addition & 2 deletions test/legacy_test/test_cummax_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import paddle
from paddle import base
from paddle.base import core
from paddle.pir_utils import test_with_pir_api


Expand Down Expand Up @@ -143,7 +142,7 @@ def run_cases(self):
z, ind = cummax_dim2(data_np, axis=-2)
np.testing.assert_array_equal(z, y.numpy())
np.testing.assert_array_equal(ind, indices.numpy())
self.assertTrue(indices.dtype == core.VarDesc.VarType.INT32)
self.assertTrue(indices.dtype == paddle.int32)

data_np = np.random.randint(0, 10, size=(100, 100)).astype(np.int32)
data = paddle.to_tensor(data_np)
Expand Down
3 changes: 1 addition & 2 deletions test/legacy_test/test_cummin_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import paddle
from paddle import base
from paddle.base import core
from paddle.pir_utils import test_with_pir_api


Expand Down Expand Up @@ -143,7 +142,7 @@ def run_cases(self):
z, ind = cummin_dim2(data_np, axis=-2)
np.testing.assert_array_equal(z, y.numpy())
np.testing.assert_array_equal(ind, indices.numpy())
self.assertTrue(indices.dtype == core.VarDesc.VarType.INT32)
self.assertTrue(indices.dtype == paddle.int32)

data_np = np.random.randint(0, 10, size=(100, 100)).astype(np.int32)
data = paddle.to_tensor(data_np)
Expand Down
4 changes: 2 additions & 2 deletions test/legacy_test/test_cumsum_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ def run_cases(self):
np.testing.assert_array_equal(z, y.numpy())

y = paddle.cumsum(data, dtype='float64')
self.assertTrue(y.dtype == core.VarDesc.VarType.FP64)
self.assertTrue(y.dtype == paddle.float64)

y = paddle.cumsum(data, dtype=np.int32)
self.assertTrue(y.dtype == core.VarDesc.VarType.INT32)
self.assertTrue(y.dtype == paddle.int32)

y = paddle.cumsum(data, axis=-2)
z = np.cumsum(data_np, axis=-2)
Expand Down
8 changes: 4 additions & 4 deletions test/legacy_test/test_elementwise_add_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ def test_static_add(self):
a = 1.5
b = paddle.full([4, 5, 6], True, dtype='bool')
c = a + b
self.assertTrue(c.dtype == core.VarDesc.VarType.FP32)
self.assertTrue(c.dtype == paddle.float32)
with paddle.pir_utils.IrGuard():
a = 1.5
b = paddle.full([4, 5, 6], True, dtype='bool')
Expand All @@ -789,7 +789,7 @@ def test_dygraph_add(self):
b = paddle.full([2], True, dtype='bool')
# special case: scalar + tensor(bool)
c = a + b
self.assertTrue(c.dtype == core.VarDesc.VarType.FP32)
self.assertTrue(c.dtype == paddle.float32)

np_a = np.random.random((2, 3, 4)).astype(np.float64)
np_b = np.random.random((2, 3, 4)).astype(np.float64)
Expand Down Expand Up @@ -843,7 +843,7 @@ def test_float32_add(self):
a = paddle.full([4, 5, 6], 1.5, dtype='float32')
b = np.array([1.5], dtype='float32')[0]
c = a + b
self.assertTrue(c.dtype == core.VarDesc.VarType.FP32)
self.assertTrue(c.dtype == paddle.float32)

def test_float16_add(self):
if not core.is_compiled_with_cuda():
Expand All @@ -852,7 +852,7 @@ def test_float16_add(self):
a = paddle.full([4, 5, 6], 1.5, dtype='float16')
b = np.array([1.5], dtype='float16')[0]
c = a + b
self.assertTrue(c.dtype == core.VarDesc.VarType.FP16)
self.assertTrue(c.dtype == paddle.float16)


class TestTensorAddAPIWarnings(unittest.TestCase):
Expand Down
3 changes: 1 addition & 2 deletions test/legacy_test/test_gather_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,7 @@ def test_out_type(self):
index = paddle.static.data(shape=[4], dtype='int64', name='index')
out = paddle.gather(data, index)
self.assertTrue(
out.dtype == core.VarDesc.VarType.INT64
or out.dtype == core.DataType.INT64
out.dtype == paddle.int64 or out.dtype == core.DataType.INT64
)

def test_pir_out_type(self):
Expand Down
2 changes: 1 addition & 1 deletion test/legacy_test/test_logcumsumexp_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def run_imperative(self):
np.testing.assert_allclose(z, y.numpy(), rtol=1e-05)

y = paddle.logcumsumexp(data, dtype='float32')
self.assertTrue(y.dtype == core.VarDesc.VarType.FP32)
self.assertTrue(y.dtype == paddle.float32)

y = paddle.logcumsumexp(data, axis=-2)
z = np_logcumsumexp(data_np, axis=-2)
Expand Down