|
15 | 15 | """Tests for tf.framework.tensor_struct.""" |
16 | 16 |
|
17 | 17 | import contextlib |
| 18 | +import tempfile |
18 | 19 | import typing |
19 | 20 |
|
20 | 21 | from absl.testing import parameterized |
|
29 | 30 | from tensorflow.python.framework import tensor_spec |
30 | 31 | from tensorflow.python.framework import tensor_struct |
31 | 32 | from tensorflow.python.framework import test_util |
| 33 | +from tensorflow.python.module import module |
32 | 34 | from tensorflow.python.ops import array_ops |
33 | 35 | from tensorflow.python.ops import control_flow_ops |
34 | 36 | from tensorflow.python.ops import math_ops |
35 | 37 | from tensorflow.python.ops.ragged import ragged_factory_ops |
36 | 38 | from tensorflow.python.ops.ragged import ragged_tensor |
37 | 39 | from tensorflow.python.platform import googletest |
| 40 | +from tensorflow.python.platform import test |
| 41 | +from tensorflow.python.saved_model import load |
| 42 | +from tensorflow.python.saved_model import save |
38 | 43 | from tensorflow.python.util import dispatch |
39 | 44 | from tensorflow.python.util import nest |
40 | 45 | from tensorflow.python.util import tf_inspect |
@@ -694,6 +699,33 @@ def testStructBaseConstructorRaisesException(self): |
694 | 699 | 'Struct is an abstract base class.'): |
695 | 700 | tensor_struct.Struct() |
696 | 701 |
|
| 702 | + class StructWithName(tensor_struct.Struct): |
| 703 | + __name__ = 'tf.__test__.StructWithName' # For SavedModel serialization |
| 704 | + x: typing.Tuple[ops.Tensor, int] |
| 705 | + y: ops.Tensor |
| 706 | + |
| 707 | + def testSavedModelSupport(self): |
| 708 | + |
| 709 | + class TestModule(module.Module): |
| 710 | + |
| 711 | + @def_function.function |
| 712 | + def f(self, s): |
| 713 | + return s.x[0] + s.x[1] + s.y |
| 714 | + |
| 715 | + s1 = self.StructWithName((1, 2), 3) |
| 716 | + s2 = self.StructWithName((1.0, 2), [3.0, 4.0]) |
| 717 | + |
| 718 | + m = TestModule() |
| 719 | + m.f.get_concrete_function(s1) |
| 720 | + m.f.get_concrete_function(s2) |
| 721 | + |
| 722 | + path = tempfile.mkdtemp(prefix=test.get_temp_dir()) |
| 723 | + save.save(m, path) |
| 724 | + loaded = load.load(path) |
| 725 | + |
| 726 | + self.assertAllEqual(loaded.f(s1), 6) |
| 727 | + self.assertAllEqual(loaded.f(s2), [6.0, 7.0]) |
| 728 | + |
697 | 729 |
|
698 | 730 | @test_util.run_all_in_graph_and_eager_modes |
699 | 731 | class StructSpecTest(test_util.TensorFlowTestCase, parameterized.TestCase): |
@@ -768,20 +800,19 @@ class Zoo(tensor_struct.Struct): |
768 | 800 | }) |
769 | 801 |
|
770 | 802 | serialized = zoo_spec._serialize() |
771 | | - self.assertEqual( |
772 | | - serialized, |
773 | | - dict( |
774 | | - zookeepers=('Zoey', 'Zack'), |
775 | | - animals={ |
776 | | - 'tiger': featurespec, |
777 | | - 'elephant': featurespec |
778 | | - })) |
| 803 | + self.assertEqual(serialized, |
| 804 | + (('zookeepers', ('Zoey', 'Zack')), ('animals', { |
| 805 | + 'tiger': featurespec, |
| 806 | + 'elephant': featurespec |
| 807 | + }))) |
779 | 808 | restored = Zoo.Spec._deserialize(serialized) |
780 | 809 | self.assertEqual(zoo_spec, restored) |
781 | 810 |
|
782 | 811 | # ImmutableDict is used for the field, but dict for the serialization: |
783 | 812 | self.assertIsInstance(zoo_spec.animals, immutable_dict.ImmutableDict) |
784 | | - self.assertIsInstance(serialized['animals'], dict) |
| 813 | + serialized_field_name, serialized_field_value = serialized[1] |
| 814 | + self.assertEqual(serialized_field_name, 'animals') |
| 815 | + self.assertIsInstance(serialized_field_value, dict) |
785 | 816 |
|
786 | 817 | def testSpecComponents(self): |
787 | 818 |
|
|
0 commit comments