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

[Dy2St] pir dy2st unittest verification - 104 #59662

Merged
merged 7 commits into from
Dec 7, 2023
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
8 changes: 8 additions & 0 deletions python/paddle/jit/dy2static/function_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,14 @@ def _replace_value_with_input_spec(args):
input_var.shape, input_var.dtype, input_var.name
)
input_var.stop_gradient = stop_gradient
elif isinstance(input_var, paddle.pir.Value):
stop_gradient = input_var.stop_gradient
# TODO(gouzil): change input_var_name to input_var.name
input_var_name = input_var.get_defining_op().name() + "_var"
DrRyanHuang marked this conversation as resolved.
Show resolved Hide resolved
input_var = paddle.static.InputSpec(
input_var.shape, input_var.dtype, input_var_name
)
input_var.stop_gradient = stop_gradient
DrRyanHuang marked this conversation as resolved.
Show resolved Hide resolved

args_with_spec.append(input_var)
args_with_spec = paddle.utils.pack_sequence_as(args, args_with_spec)
Expand Down
7 changes: 5 additions & 2 deletions python/paddle/static/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import os

import numpy as np

import paddle
from paddle.base import Variable, core
from paddle.base.data_feeder import check_type
Expand Down Expand Up @@ -206,8 +208,9 @@ def __init__(self, shape, dtype='float32', name=None, stop_gradient=False):
self.shape = self._verify(shape)
# convert dtype into united represention
if dtype is not None:
if not isinstance(dtype, core.VarDesc.VarType):
if isinstance(dtype, (np.dtype, str)):
dtype = convert_np_dtype_to_dtype_(dtype)

self.dtype = dtype
self.name = name
self.stop_gradient = stop_gradient
Expand Down Expand Up @@ -418,7 +421,7 @@ def setitem(x, index, value):
(3) a[1,:, 3] = v -> setitem(a, (1, slice(None,None,None),3), v)
(4) a[1, ..., 2]=v -> setitem(a, (1, ..., 2), v)

3. You can always use TUPLE as index input even there is only one index.
3. You can always use TUPLE as index input, even there is only one index.
(1) a[Tensor([10,10])]=v -> setitem(a, (Tensor([10,10]),), v)
(2) a[1] = v -> setitem(a, (1,), v)
"""
Expand Down
15 changes: 12 additions & 3 deletions test/dygraph_to_static/test_function_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,24 @@
from test_declarative import foo_func

import paddle
from paddle.framework import in_pir_mode
from paddle.jit.dy2static.function_spec import FunctionSpec
from paddle.pir_utils import test_with_pir_api
from paddle.static import InputSpec

paddle.enable_static()


class TestFunctionSpec(unittest.TestCase):
@test_with_pir_api
def test_constructor(self):
foo_spec = FunctionSpec(foo_func)
args_name = foo_spec.args_name
self.assertListEqual(args_name, ['a', 'b', 'c', 'd'])
self.assertTrue(foo_spec.dygraph_function == foo_func)
self.assertIsNone(foo_spec.input_spec)

@test_with_pir_api
def test_verify_input_spec(self):
a_spec = InputSpec([None, 10], name='a')
b_spec = InputSpec([10], name='b')
Expand All @@ -42,6 +46,7 @@ def test_verify_input_spec(self):
foo_spec = FunctionSpec(foo_func, input_spec=[a_spec, b_spec])
self.assertTrue(len(foo_spec.flat_input_spec) == 2)

@test_with_pir_api
def test_unified_args_and_kwargs(self):
foo_spec = FunctionSpec(foo_func)
# case 1: foo(10, 20, c=4)
Expand Down Expand Up @@ -69,13 +74,13 @@ def test_unified_args_and_kwargs(self):
with self.assertRaises(ValueError):
foo_spec.unified_args_and_kwargs([10], {'c': 4})

@test_with_pir_api
def test_args_to_input_spec(self):
a_spec = InputSpec([None, 10], name='a', stop_gradient=True)
b_spec = InputSpec([10], name='b', stop_gradient=True)

a_tensor = paddle.static.data(name='a_var', shape=[4, 10])
b_tensor = paddle.static.data(name='b_var', shape=[4, 10])
kwargs = {'c': 1, 'd': 2}

# case 1
foo_spec = FunctionSpec(foo_func, input_spec=[a_spec, b_spec])
Expand All @@ -97,8 +102,12 @@ def test_args_to_input_spec(self):
)
self.assertTrue(len(input_with_spec) == 2)
self.assertTrue(input_with_spec[0] == a_spec) # a
self.assertTupleEqual(input_with_spec[1].shape, (4, 10)) # b.shape
self.assertEqual(input_with_spec[1].name, 'b_var') # b.name
self.assertTupleEqual(
tuple(input_with_spec[1].shape), (4, 10)
) # b.shape
DrRyanHuang marked this conversation as resolved.
Show resolved Hide resolved
if not in_pir_mode():
# TODO(gouzil): remove this if when OpResult.name is OK
self.assertEqual(input_with_spec[1].name, 'b_var') # b.name

# case 3
# assert kwargs is None if set `input_spec`
Expand Down