[Dy2stat] Support InputSpec and Return callable class instance in @declarative (#25960)
* add InputSpec * add unittest for tensorSpec and SimpleNetrevert-26856-strategy_example2
parent
89d7d86684
commit
f05613683f
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,116 @@
|
||||
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import paddle
|
||||
from paddle.static import InputSpec
|
||||
from paddle.fluid.dygraph.dygraph_to_static.function_spec import FunctionSpec
|
||||
|
||||
from test_declarative import foo_func
|
||||
|
||||
import unittest
|
||||
|
||||
|
||||
class TestFunctionSpec(unittest.TestCase):
|
||||
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.assertTrue(foo_spec.input_spec is None)
|
||||
|
||||
def test_verify_input_spec(self):
|
||||
a_spec = InputSpec([None, 10], name='a')
|
||||
b_spec = InputSpec([10], name='b')
|
||||
|
||||
# type(input_spec) should be list or tuple
|
||||
with self.assertRaises(TypeError):
|
||||
foo_spec = FunctionSpec(foo_func, input_spec=a_spec)
|
||||
|
||||
# each element of input_spec should be `InputSpec`
|
||||
with self.assertRaises(ValueError):
|
||||
foo_spec = FunctionSpec(foo_func, input_spec=[a_spec, 10])
|
||||
|
||||
foo_spec = FunctionSpec(foo_func, input_spec=[a_spec, b_spec])
|
||||
self.assertTrue(len(foo_spec.flat_input_spec) == 2)
|
||||
|
||||
def test_unified_args_and_kwargs(self):
|
||||
foo_spec = FunctionSpec(foo_func)
|
||||
# case 1: foo(10, 20, c=4)
|
||||
args, kwargs = foo_spec.unified_args_and_kwargs([10, 20], {'c': 4})
|
||||
self.assertTupleEqual(args, (10, 20, 4, 2))
|
||||
self.assertTrue(len(kwargs) == 0)
|
||||
|
||||
# case 2: foo(a=10, b=20, d=4)
|
||||
args, kwargs = foo_spec.unified_args_and_kwargs(
|
||||
[], {'a': 10,
|
||||
'b': 20,
|
||||
'd': 4})
|
||||
self.assertTupleEqual(args, (10, 20, 1, 4))
|
||||
self.assertTrue(len(kwargs) == 0)
|
||||
|
||||
# case 3: foo(10, b=20)
|
||||
args, kwargs = foo_spec.unified_args_and_kwargs([10], {'b': 20})
|
||||
self.assertTupleEqual(args, (10, 20, 1, 2))
|
||||
self.assertTrue(len(kwargs) == 0)
|
||||
|
||||
# assert len(self._arg_names) >= len(args)
|
||||
with self.assertRaises(ValueError):
|
||||
foo_spec.unified_args_and_kwargs([10, 20, 30, 40, 50], {'c': 4})
|
||||
|
||||
# assert arg_name should be in kwargs
|
||||
with self.assertRaises(ValueError):
|
||||
foo_spec.unified_args_and_kwargs([10], {'c': 4})
|
||||
|
||||
def test_args_to_input_spec(self):
|
||||
a_spec = InputSpec([None, 10], name='a')
|
||||
b_spec = InputSpec([10], name='b')
|
||||
|
||||
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])
|
||||
input_with_spec = foo_spec.args_to_input_spec(
|
||||
(a_tensor, b_tensor, 1, 2), {})
|
||||
self.assertTrue(len(input_with_spec) == 4)
|
||||
self.assertTrue(input_with_spec[0] == a_spec) # a
|
||||
self.assertTrue(input_with_spec[1] == b_spec) # b
|
||||
self.assertTrue(input_with_spec[2] == 1) # c
|
||||
self.assertTrue(input_with_spec[3] == 2) # d
|
||||
|
||||
# case 2
|
||||
foo_spec = FunctionSpec(foo_func, input_spec=[a_spec])
|
||||
input_with_spec = foo_spec.args_to_input_spec((a_tensor, b_tensor), {})
|
||||
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
|
||||
|
||||
# case 3
|
||||
# assert kwargs is None if set `input_spec`
|
||||
foo_spec = FunctionSpec(foo_func, input_spec=[a_spec])
|
||||
with self.assertRaises(ValueError):
|
||||
input_with_spec = foo_spec.args_to_input_spec((a_tensor, b_tensor),
|
||||
{'c': 4})
|
||||
|
||||
# case 4
|
||||
# assert len(args) >= len(self._input_spec)
|
||||
foo_spec = FunctionSpec(foo_func, input_spec=[a_spec, b_spec])
|
||||
with self.assertRaises(ValueError):
|
||||
input_with_spec = foo_spec.args_to_input_spec((a_tensor, ), {})
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
@ -0,0 +1,113 @@
|
||||
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import unittest
|
||||
import numpy as np
|
||||
import paddle.fluid as fluid
|
||||
from paddle.static import InputSpec
|
||||
from paddle.fluid.framework import core, convert_np_dtype_to_dtype_
|
||||
|
||||
|
||||
class TestInputSpec(unittest.TestCase):
|
||||
def test_default(self):
|
||||
tensor_spec = InputSpec([3, 4])
|
||||
self.assertEqual(tensor_spec.dtype,
|
||||
convert_np_dtype_to_dtype_('float32'))
|
||||
self.assertEqual(tensor_spec.name, None)
|
||||
|
||||
def test_from_tensor(self):
|
||||
x_bool = fluid.layers.fill_constant(shape=[1], dtype='bool', value=True)
|
||||
bool_spec = InputSpec.from_tensor(x_bool)
|
||||
self.assertEqual(bool_spec.dtype, x_bool.dtype)
|
||||
self.assertEqual(bool_spec.shape, x_bool.shape)
|
||||
self.assertEqual(bool_spec.name, x_bool.name)
|
||||
|
||||
bool_spec2 = InputSpec.from_tensor(x_bool, name='bool_spec')
|
||||
self.assertEqual(bool_spec2.name, bool_spec2.name)
|
||||
|
||||
def test_from_numpy(self):
|
||||
x_numpy = np.ones([10, 12])
|
||||
x_np_spec = InputSpec.from_numpy(x_numpy)
|
||||
self.assertEqual(x_np_spec.dtype,
|
||||
convert_np_dtype_to_dtype_(x_numpy.dtype))
|
||||
self.assertEqual(x_np_spec.shape, x_numpy.shape)
|
||||
self.assertEqual(x_np_spec.name, None)
|
||||
|
||||
x_numpy2 = np.array([1, 2, 3, 4]).astype('int64')
|
||||
x_np_spec2 = InputSpec.from_numpy(x_numpy2, name='x_np_int64')
|
||||
self.assertEqual(x_np_spec2.dtype,
|
||||
convert_np_dtype_to_dtype_(x_numpy2.dtype))
|
||||
self.assertEqual(x_np_spec2.shape, x_numpy2.shape)
|
||||
self.assertEqual(x_np_spec2.name, 'x_np_int64')
|
||||
|
||||
def test_shape_with_none(self):
|
||||
tensor_spec = InputSpec([None, 4, None], dtype='int8', name='x_spec')
|
||||
self.assertEqual(tensor_spec.dtype, convert_np_dtype_to_dtype_('int8'))
|
||||
self.assertEqual(tensor_spec.name, 'x_spec')
|
||||
self.assertEqual(tensor_spec.shape, (-1, 4, -1))
|
||||
|
||||
def test_shape_raise_error(self):
|
||||
# 1. shape should only contain int and None.
|
||||
with self.assertRaises(ValueError):
|
||||
tensor_spec = InputSpec(['None', 4, None], dtype='int8')
|
||||
|
||||
# 2. shape should be type `list` or `tuple`
|
||||
with self.assertRaises(TypeError):
|
||||
tensor_spec = InputSpec(4, dtype='int8')
|
||||
|
||||
# 3. len(shape) should be greater than 0.
|
||||
with self.assertRaises(ValueError):
|
||||
tensor_spec = InputSpec([], dtype='int8')
|
||||
|
||||
def test_batch_and_unbatch(self):
|
||||
tensor_spec = InputSpec([10])
|
||||
# insert batch_size
|
||||
batch_tensor_spec = tensor_spec.batch(16)
|
||||
self.assertEqual(batch_tensor_spec.shape, (16, 10))
|
||||
|
||||
# unbatch
|
||||
unbatch_spec = batch_tensor_spec.unbatch()
|
||||
self.assertEqual(unbatch_spec.shape, (10, ))
|
||||
|
||||
# 1. `unbatch` requires len(shape) > 1
|
||||
with self.assertRaises(ValueError):
|
||||
unbatch_spec.unbatch()
|
||||
|
||||
# 2. `batch` requires len(batch_size) == 1
|
||||
with self.assertRaises(ValueError):
|
||||
tensor_spec.batch([16, 12])
|
||||
|
||||
# 3. `batch` requires type(batch_size) == int
|
||||
with self.assertRaises(TypeError):
|
||||
tensor_spec.batch('16')
|
||||
|
||||
def test_eq_and_hash(self):
|
||||
tensor_spec_1 = InputSpec([10, 16], dtype='float32')
|
||||
tensor_spec_2 = InputSpec([10, 16], dtype='float32')
|
||||
tensor_spec_3 = InputSpec([10, 16], dtype='float32', name='x')
|
||||
tensor_spec_4 = InputSpec([16], dtype='float32', name='x')
|
||||
|
||||
# override ``__eq__`` according to [shape, dtype, name]
|
||||
self.assertTrue(tensor_spec_1 == tensor_spec_2)
|
||||
self.assertTrue(tensor_spec_1 != tensor_spec_3) # different name
|
||||
self.assertTrue(tensor_spec_3 != tensor_spec_4) # different shape
|
||||
|
||||
# override ``__hash__`` according to [shape, dtype]
|
||||
self.assertTrue(hash(tensor_spec_1) == hash(tensor_spec_2))
|
||||
self.assertTrue(hash(tensor_spec_1) == hash(tensor_spec_3))
|
||||
self.assertTrue(hash(tensor_spec_3) != hash(tensor_spec_4))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue