Skip to content
New issue

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

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

Already on GitHub? # to your account

rm unittests eager guard tests part6 eager_run2expand_v2 #48817

Merged
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
111 changes: 48 additions & 63 deletions python/paddle/fluid/tests/unittests/test_eager_run_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@
_is_dy2st_enable_standalone_executor,
_is_enable_standalone_executor,
)
from paddle.fluid.framework import (
Variable,
_in_legacy_dygraph,
_test_eager_guard,
)
from paddle.fluid.framework import Variable, _in_legacy_dygraph
from paddle.fluid.layers.utils import _hash_with_id


Expand Down Expand Up @@ -124,68 +120,57 @@ def test_eager(self):

paddle.disable_static('cpu')
# step 2: call run_program in eager mode
with _test_eager_guard():
x_t = paddle.ones([2, 4])
x_t.name = "x"
x_t.stop_gradient = False
y_t = paddle.ones([4, 2])
y_t.name = "y"
y_t.stop_gradient = False

fake_var = paddle.zeros([1])
fake_var.name = 'Fake_var'

out_t = _create_out(out)

scope = core.Scope()
attrs = [
'global_block',
program.desc.block(0),
'start_op_index',
0,
'end_op_index',
main_program.desc.block(0).op_size(),
'is_test',
False,
'program_id',
_hash_with_id(program),
]

use_interpretorcore = (
_is_enable_standalone_executor()
and _is_dy2st_enable_standalone_executor()
)
attrs.extend(('use_interpretorcore', use_interpretorcore))
if use_interpretorcore:
attrs.extend(
(
'forward_global_block',
forward_program.desc.block(0),
'backward_global_block',
backward_program.desc.block(0),
)
)
x_t = paddle.ones([2, 4])
x_t.name = "x"
x_t.stop_gradient = False
y_t = paddle.ones([4, 2])
y_t.name = "y"
y_t.stop_gradient = False

fake_var = paddle.zeros([1])
fake_var.name = 'Fake_var'

out_t = _create_out(out)

scope = core.Scope()
attrs = [
'global_block',
program.desc.block(0),
'start_op_index',
0,
'end_op_index',
main_program.desc.block(0).op_size(),
'is_test',
False,
'program_id',
_hash_with_id(program),
]

_legacy_C_ops.run_program(
[x_t, y_t],
[fake_var],
[out_t],
[scope],
[fake_var],
None,
*attrs
use_interpretorcore = (
_is_enable_standalone_executor()
and _is_dy2st_enable_standalone_executor()
)
attrs.extend(('use_interpretorcore', use_interpretorcore))
if use_interpretorcore:
attrs.extend(
(
'forward_global_block',
forward_program.desc.block(0),
'backward_global_block',
backward_program.desc.block(0),
)
)

loss = paddle.mean(out_t)
loss.backward()
_legacy_C_ops.run_program(
[x_t, y_t], [fake_var], [out_t], [scope], [fake_var], None, *attrs
)

np.testing.assert_array_equal(np.ones([2, 2]) * 4, out_t.numpy())
np.testing.assert_array_equal(
np.ones([2, 4]) * 0.5, x_t.grad.numpy()
)
np.testing.assert_array_equal(
np.ones([4, 2]) * 0.5, y_t.grad.numpy()
)
loss = paddle.mean(out_t)
loss.backward()

np.testing.assert_array_equal(np.ones([2, 2]) * 4, out_t.numpy())
np.testing.assert_array_equal(np.ones([2, 4]) * 0.5, x_t.grad.numpy())
np.testing.assert_array_equal(np.ones([4, 2]) * 0.5, y_t.grad.numpy())


if __name__ == '__main__':
Expand Down
36 changes: 17 additions & 19 deletions python/paddle/fluid/tests/unittests/test_eager_trace_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,27 @@
import numpy as np

import paddle
from paddle.fluid.framework import _test_eager_guard


class TestEagerTraceOp(unittest.TestCase):
def test_branches(self):
with _test_eager_guard():
data = np.random.random([1, 1]).astype(np.float32)
x = paddle.to_tensor(data)

paddle.fluid.framework._dygraph_tracer().trace_op(
'broadcast_tensors',
{'X': [x, x], 'Out': [x, x]},
{'Out': [x, x]},
{},
)
paddle.fluid.framework._dygraph_tracer().trace_op(
'scale', {'X': x}, {'Out': x}, {'scale': 0.5}
)

scale = paddle.to_tensor(np.random.random([1]).astype(np.float32))
paddle.fluid.framework._dygraph_tracer().trace_op(
'instance_norm', {'Scale': [scale], 'X': [x]}, {'Y': [x]}, {}
)
data = np.random.random([1, 1]).astype(np.float32)
x = paddle.to_tensor(data)

paddle.fluid.framework._dygraph_tracer().trace_op(
'broadcast_tensors',
{'X': [x, x], 'Out': [x, x]},
{'Out': [x, x]},
{},
)
paddle.fluid.framework._dygraph_tracer().trace_op(
'scale', {'X': x}, {'Out': x}, {'scale': 0.5}
)

scale = paddle.to_tensor(np.random.random([1]).astype(np.float32))
paddle.fluid.framework._dygraph_tracer().trace_op(
'instance_norm', {'Scale': [scale], 'X': [x]}, {'Y': [x]}, {}
)


if __name__ == "__main__":
Expand Down
71 changes: 33 additions & 38 deletions python/paddle/fluid/tests/unittests/test_egr_code_generate_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,55 +17,50 @@
import numpy as np

import paddle
from paddle.fluid.framework import _test_eager_guard


class EagerOpAPIGenerateTestCase(unittest.TestCase):
def test_elementwise_add(self):
with _test_eager_guard():
paddle.set_device("cpu")
np_x = np.ones([4, 16, 16, 32]).astype('float32')
np_y = np.ones([4, 16, 16, 32]).astype('float32')
x = paddle.to_tensor(np_x)
y = paddle.to_tensor(np_y)
out = paddle.add(x, y)
out_arr = out.numpy()
paddle.set_device("cpu")
np_x = np.ones([4, 16, 16, 32]).astype('float32')
np_y = np.ones([4, 16, 16, 32]).astype('float32')
x = paddle.to_tensor(np_x)
y = paddle.to_tensor(np_y)
out = paddle.add(x, y)
out_arr = out.numpy()

out_arr_expected = np.add(np_x, np_y)
np.testing.assert_array_equal(out_arr, out_arr_expected)
out_arr_expected = np.add(np_x, np_y)
np.testing.assert_array_equal(out_arr, out_arr_expected)

def test_sum(self):
with _test_eager_guard():
x_data = np.array(
[[0.2, 0.3, 0.5, 0.9], [0.1, 0.2, 0.6, 0.7]]
).astype('float32')
x = paddle.to_tensor(x_data, 'float32')
out = paddle.sum(x, axis=0)
out_arr = out.numpy()
out_arr_expected = np.sum(x_data, axis=0)
np.testing.assert_array_equal(out_arr, out_arr_expected)
x_data = np.array([[0.2, 0.3, 0.5, 0.9], [0.1, 0.2, 0.6, 0.7]]).astype(
'float32'
)
x = paddle.to_tensor(x_data, 'float32')
out = paddle.sum(x, axis=0)
out_arr = out.numpy()
out_arr_expected = np.sum(x_data, axis=0)
np.testing.assert_array_equal(out_arr, out_arr_expected)

def test_mm(self):
with _test_eager_guard():
np_input = np.random.random([16, 32]).astype('float32')
np_mat2 = np.random.random([32, 32]).astype('float32')
input = paddle.to_tensor(np_input)
mat2 = paddle.to_tensor(np_mat2)
out = paddle.mm(input, mat2)
out_arr = out.numpy()
out_arr_expected = np.matmul(np_input, np_mat2)
np.testing.assert_allclose(out_arr, out_arr_expected, rtol=1e-05)
np_input = np.random.random([16, 32]).astype('float32')
np_mat2 = np.random.random([32, 32]).astype('float32')
input = paddle.to_tensor(np_input)
mat2 = paddle.to_tensor(np_mat2)
out = paddle.mm(input, mat2)
out_arr = out.numpy()
out_arr_expected = np.matmul(np_input, np_mat2)
np.testing.assert_allclose(out_arr, out_arr_expected, rtol=1e-05)

def test_sigmoid(self):
with _test_eager_guard():
np_x = np.array([-0.4, -0.2, 0.1, 0.3]).astype('float32')
x = paddle.to_tensor(np_x)
out = paddle.nn.functional.sigmoid(x)
out_arr = out.numpy()
out_arr_expected = np.array(
[0.40131234, 0.450166, 0.52497919, 0.57444252]
).astype('float32')
np.testing.assert_allclose(out_arr, out_arr_expected, rtol=1e-05)
np_x = np.array([-0.4, -0.2, 0.1, 0.3]).astype('float32')
x = paddle.to_tensor(np_x)
out = paddle.nn.functional.sigmoid(x)
out_arr = out.numpy()
out_arr_expected = np.array(
[0.40131234, 0.450166, 0.52497919, 0.57444252]
).astype('float32')
np.testing.assert_allclose(out_arr, out_arr_expected, rtol=1e-05)


if __name__ == "__main__":
Expand Down
Loading