未验证 提交 11d7dae9 编写于 作者: W wanghuancoder 提交者: GitHub

Del old dygraph optest1 (#51417)

* delete old dygraph op test
上级 da551b2e
......@@ -15,7 +15,7 @@
import time
import numpy as np
from op_test import OpTest
from eager_op_test import OpTest
from paddle.fluid.op import Operator
......
......@@ -15,7 +15,7 @@
import unittest
import numpy as np
from op_test import OpTest
from eager_op_test import OpTest
from paddle.framework import core
......
......@@ -50,6 +50,17 @@ from white_list import (
op_threshold_white_list,
)
from paddle.fluid.wrapped_decorator import signature_safe_contextmanager
@signature_safe_contextmanager
def paddle_static_guard():
try:
paddle.enable_static()
yield
finally:
paddle.disable_static()
def check_out_dtype(api_fn, in_specs, expect_dtypes, target_index=0, **configs):
"""
......@@ -66,37 +77,39 @@ def check_out_dtype(api_fn, in_specs, expect_dtypes, target_index=0, **configs):
check_out_dtype(fluid.layers.pad_constant_like, [([2,3,2,3], 'float64'), ([1, 3, 1,3], )], ['float32', 'float64', 'int64'], target_index=1, pad_value=0.)
"""
paddle.enable_static()
for i, expect_dtype in enumerate(expect_dtypes):
with paddle.static.program_guard(paddle.static.Program()):
input_t = []
for index, spec in enumerate(in_specs):
if len(spec) == 1:
shape = spec[0]
dtype = expect_dtype if target_index == index else 'float32'
elif len(spec) == 2:
shape, dtype = spec
else:
raise ValueError(
"Value of in_specs[{}] should contains two elements: [shape, dtype]".format(
index
with paddle_static_guard():
for i, expect_dtype in enumerate(expect_dtypes):
with paddle.static.program_guard(paddle.static.Program()):
input_t = []
for index, spec in enumerate(in_specs):
if len(spec) == 1:
shape = spec[0]
dtype = (
expect_dtype if target_index == index else 'float32'
)
elif len(spec) == 2:
shape, dtype = spec
else:
raise ValueError(
"Value of in_specs[{}] should contains two elements: [shape, dtype]".format(
index
)
)
input_t.append(
paddle.static.data(
name='data_%s' % index, shape=shape, dtype=dtype
)
)
input_t.append(
paddle.static.data(
name='data_%s' % index, shape=shape, dtype=dtype
)
)
out = api_fn(*input_t, **configs)
out_dtype = fluid.data_feeder.convert_dtype(out.dtype)
out = api_fn(*input_t, **configs)
out_dtype = fluid.data_feeder.convert_dtype(out.dtype)
if out_dtype != expect_dtype:
raise ValueError(
"Expected out.dtype is {}, but got {} from {}.".format(
expect_dtype, out_dtype, api_fn.__name__
if out_dtype != expect_dtype:
raise ValueError(
"Expected out.dtype is {}, but got {} from {}.".format(
expect_dtype, out_dtype, api_fn.__name__
)
)
)
def _set_use_system_allocator(value=None):
......@@ -723,11 +736,9 @@ class OpTest(unittest.TestCase):
if is_input:
v = self._create_var_from_numpy(np_value_temp)
if if_return_inputs_grad_dict:
v.stop_gradient = False
if hasattr(v, "retain_grads"):
v.retain_grads()
v.retain_grads()
if has_lod:
v.value().get_tensor().set_recursive_sequence_lengths(
......@@ -887,6 +898,8 @@ class OpTest(unittest.TestCase):
)
if not kernel_sig:
return None
if not hasattr(self, "python_api"):
print(kernel_sig)
assert hasattr(self, "python_api"), (
"Detect there is KernelSignature for `%s` op, please set the `self.python_api` if you set check_dygraph = True"
% self.op_type
......@@ -2256,6 +2269,10 @@ class OpTest(unittest.TestCase):
dygraph_outputs = self._calc_python_api_output(
place, inputs, outputs
)
if dygraph_outputs is None:
# missing KernelSignature, fall back to eager middle output.
dygraph_outs = self._calc_dygraph_output(place)
# if outputs is None, kernel sig is empty or other error is happens.
if not check_dygraph or dygraph_outputs is None:
block.append_op(
......
......@@ -15,7 +15,7 @@
import unittest
import numpy as np
from op_test import check_out_dtype
from eager_op_test import check_out_dtype, paddle_static_guard
import paddle
import paddle.fluid as fluid
......@@ -93,22 +93,29 @@ class TestPool1D_API(unittest.TestCase):
np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05)
def check_adaptive_max_static_results(self, place):
with fluid.program_guard(fluid.Program(), fluid.Program()):
input = fluid.data(name="input", shape=[2, 3, 32], dtype="float32")
result = F.adaptive_max_pool1d(input, output_size=16)
input_np = np.random.random([2, 3, 32]).astype("float32")
result_np = max_pool1D_forward_naive(
input_np, ksize=[16], strides=[2], paddings=[0], adaptive=True
)
exe = fluid.Executor(place)
fetches = exe.run(
fluid.default_main_program(),
feed={"input": input_np},
fetch_list=[result],
)
np.testing.assert_allclose(fetches[0], result_np, rtol=1e-05)
with paddle_static_guard():
with fluid.program_guard(fluid.Program(), fluid.Program()):
input = fluid.data(
name="input", shape=[2, 3, 32], dtype="float32"
)
result = F.adaptive_max_pool1d(input, output_size=16)
input_np = np.random.random([2, 3, 32]).astype("float32")
result_np = max_pool1D_forward_naive(
input_np,
ksize=[16],
strides=[2],
paddings=[0],
adaptive=True,
)
exe = fluid.Executor(place)
fetches = exe.run(
fluid.default_main_program(),
feed={"input": input_np},
fetch_list=[result],
)
np.testing.assert_allclose(fetches[0], result_np, rtol=1e-05)
def test_adaptive_max_pool1d(self):
for place in self.places:
......
......@@ -15,7 +15,7 @@
import unittest
import numpy as np
from op_test import check_out_dtype
from eager_op_test import check_out_dtype
import paddle
import paddle.fluid as fluid
......
......@@ -15,7 +15,7 @@
import unittest
import numpy as np
from op_test import check_out_dtype
from eager_op_test import check_out_dtype
import paddle
import paddle.fluid as fluid
......
......@@ -15,7 +15,7 @@ import math
import unittest
import numpy as np
from op_test import OpTest
from eager_op_test import OpTest
def add_position_encoding(input, alpha=1.0, beta=1.0):
......
......@@ -18,7 +18,7 @@ Unit testing for affine_channel_op
import unittest
import numpy as np
from op_test import OpTest
from eager_op_test import OpTest
def affine_channel(x, scale, bias, layout):
......
......@@ -15,7 +15,7 @@
import unittest
import numpy as np
from op_test import OpTest
from eager_op_test import OpTest
import paddle
......@@ -133,12 +133,10 @@ class TestAffineGridOp(OpTest):
}
def test_check_output(self):
self.check_output(check_eager=True)
self.check_output()
def test_check_grad_normal(self):
self.check_grad(
['Theta'], 'Output', no_grad_set=['OutputShape'], check_eager=True
)
self.check_grad(['Theta'], 'Output', no_grad_set=['OutputShape'])
def initTestCase(self):
self.theta_shape = (17, 2, 3)
......
......@@ -15,7 +15,7 @@
import unittest
import numpy as np
from op_test import OpTest
from eager_op_test import OpTest
import paddle
import paddle.fluid.core as core
......@@ -55,7 +55,7 @@ class TestAllcloseOp(OpTest):
}
def test_check_output(self):
self.check_output(check_eager=True)
self.check_output()
class TestAllcloseOpException(TestAllcloseOp):
......@@ -63,28 +63,28 @@ class TestAllcloseOpException(TestAllcloseOp):
def test_rtol_num():
self.inputs['Rtol'] = np.array([1e-05, 1e-05]).astype("float64")
self.inputs['Atol'] = np.array([1e-08]).astype("float64")
self.check_output(check_eager=True)
self.check_output()
self.assertRaises(ValueError, test_rtol_num)
def test_rtol_type():
self.inputs['Rtol'] = np.array([5]).astype("int32")
self.inputs['Atol'] = np.array([1e-08]).astype("float64")
self.check_output(check_eager=True)
self.check_output()
self.assertRaises(ValueError, test_rtol_type)
def test_atol_num():
self.inputs['Rtol'] = np.array([1e-05]).astype("float64")
self.inputs['Atol'] = np.array([1e-08, 1e-08]).astype("float64")
self.check_output(check_eager=True)
self.check_output()
self.assertRaises(ValueError, test_atol_num)
def test_atol_type():
self.inputs['Rtol'] = np.array([1e-05]).astype("float64")
self.inputs['Atol'] = np.array([8]).astype("int32")
self.check_output(check_eager=True)
self.check_output()
self.assertRaises(ValueError, test_atol_type)
......@@ -198,7 +198,7 @@ class TestAllcloseOpFloat16(TestAllcloseOp):
if core.is_compiled_with_cuda():
place = core.CUDAPlace(0)
if core.is_float16_supported(place):
self.check_output_with_place(place, check_eager=True)
self.check_output_with_place(place)
class TestAllcloseOpFloat32(TestAllcloseOp):
......
......@@ -15,7 +15,7 @@
import unittest
import numpy as np
from op_test import OpTest
from eager_op_test import OpTest
import paddle.static.amp.amp_nn as amp_nn
......@@ -44,7 +44,7 @@ class TestCheckFiniteAndUnscaleOp(OpTest):
self.dtype = np.float32
def test_check_output(self):
self.check_output(check_eager=True)
self.check_output()
class TestCheckFiniteAndUnscaleOpWithNan(OpTest):
......@@ -69,7 +69,7 @@ class TestCheckFiniteAndUnscaleOpWithNan(OpTest):
def test_check_output(self):
# When input contains nan, do not check the output,
# since the output may be nondeterministic and will be discarded.
self.check_output(no_check_set=['Out'], check_eager=True)
self.check_output(no_check_set=['Out'])
class TestCheckFiniteAndUnscaleOpWithInf(OpTest):
......@@ -94,7 +94,7 @@ class TestCheckFiniteAndUnscaleOpWithInf(OpTest):
def test_check_output(self):
# When input contains inf, do not check the output,
# since the output may be nondeterministic and will be discarded.
self.check_output(no_check_set=['Out'], check_eager=True)
self.check_output(no_check_set=['Out'])
if __name__ == '__main__':
......
......@@ -15,7 +15,7 @@
import unittest
import numpy as np
from op_test import OpTest
from eager_op_test import OpTest
def anchor_generator_in_python(
......
......@@ -15,7 +15,7 @@
import unittest
import numpy as np
from op_test import OpTest
from eager_op_test import OpTest
import paddle
from paddle import static
......@@ -49,7 +49,7 @@ class TestAngleOpFloat(OpTest):
self.outputs = {'Out': out_ref}
def test_check_output(self):
self.check_output(check_eager=True)
self.check_output()
def test_check_grad(self):
self.check_grad(
......@@ -58,7 +58,6 @@ class TestAngleOpFloat(OpTest):
user_defined_grads=[
angle_grad(self.x, np.ones_like(self.x) / self.x.size)
],
check_eager=True,
)
......@@ -75,7 +74,7 @@ class TestAngleOpComplex(OpTest):
self.outputs = {'Out': out_ref}
def test_check_output(self):
self.check_output(check_eager=True)
self.check_output()
def test_check_grad(self):
self.check_grad(
......@@ -84,7 +83,6 @@ class TestAngleOpComplex(OpTest):
user_defined_grads=[
angle_grad(self.x, np.ones_like(self.x) / self.x.size)
],
check_eager=True,
)
......
......@@ -16,7 +16,7 @@ import platform
import unittest
import numpy as np
from eager_op_test import OpTest
from eager_op_test import OpTest, paddle_static_guard
import paddle
import paddle.fluid as fluid
......@@ -401,7 +401,7 @@ class TestResizeLinearOpUint8(OpTest):
class TestLinearInterpOpError(unittest.TestCase):
def test_error(self):
with paddle.fluid.framework._static_guard():
with paddle_static_guard():
with program_guard(Program(), Program()):
def input_shape_error():
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册