checkpoint_test.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # Copyright 2023 DeepMind Technologies Limited.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS-IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Check that the checkpoint serialization is reversable."""
  15. import dataclasses
  16. import io
  17. from typing import Any, Optional, Union
  18. from absl.testing import absltest
  19. from graphcast import checkpoint
  20. import numpy as np
  21. @dataclasses.dataclass
  22. class SubConfig:
  23. a: int
  24. b: str
  25. @dataclasses.dataclass
  26. class Config:
  27. bt: bool
  28. bf: bool
  29. i: int
  30. f: float
  31. o1: Optional[int]
  32. o2: Optional[int]
  33. o3: Union[int, None]
  34. o4: Union[int, None]
  35. o5: int | None
  36. o6: int | None
  37. li: list[int]
  38. ls: list[str]
  39. ldc: list[SubConfig]
  40. tf: tuple[float, ...]
  41. ts: tuple[str, ...]
  42. t: tuple[str, int, SubConfig]
  43. tdc: tuple[SubConfig, ...]
  44. dsi: dict[str, int]
  45. dss: dict[str, str]
  46. dis: dict[int, str]
  47. dsdis: dict[str, dict[int, str]]
  48. dc: SubConfig
  49. dco: Optional[SubConfig]
  50. ddc: dict[str, SubConfig]
  51. @dataclasses.dataclass
  52. class Checkpoint:
  53. params: dict[str, Any]
  54. config: Config
  55. class DataclassTest(absltest.TestCase):
  56. def test_serialize_dataclass(self):
  57. ckpt = Checkpoint(
  58. params={
  59. "layer1": {
  60. "w": np.arange(10).reshape(2, 5),
  61. "b": np.array([2, 6]),
  62. },
  63. "layer2": {
  64. "w": np.arange(8).reshape(2, 4),
  65. "b": np.array([2, 6]),
  66. },
  67. "blah": np.array([3, 9]),
  68. },
  69. config=Config(
  70. bt=True,
  71. bf=False,
  72. i=42,
  73. f=3.14,
  74. o1=1,
  75. o2=None,
  76. o3=2,
  77. o4=None,
  78. o5=3,
  79. o6=None,
  80. li=[12, 9, 7, 15, 16, 14, 1, 6, 11, 4, 10, 5, 13, 3, 8, 2],
  81. ls=list("qhjfdxtpzgemryoikwvblcaus"),
  82. ldc=[SubConfig(1, "hello"), SubConfig(2, "world")],
  83. tf=(1, 4, 2, 10, 5, 9, 13, 16, 15, 8, 12, 7, 11, 14, 3, 6),
  84. ts=("hello", "world"),
  85. t=("foo", 42, SubConfig(1, "bar")),
  86. tdc=(SubConfig(1, "hello"), SubConfig(2, "world")),
  87. dsi={"a": 1, "b": 2, "c": 3},
  88. dss={"d": "e", "f": "g"},
  89. dis={1: "a", 2: "b", 3: "c"},
  90. dsdis={"a": {1: "hello", 2: "world"}, "b": {1: "world"}},
  91. dc=SubConfig(1, "hello"),
  92. dco=None,
  93. ddc={"a": SubConfig(1, "hello"), "b": SubConfig(2, "world")},
  94. ))
  95. buffer = io.BytesIO()
  96. checkpoint.dump(buffer, ckpt)
  97. buffer.seek(0)
  98. ckpt2 = checkpoint.load(buffer, Checkpoint)
  99. np.testing.assert_array_equal(ckpt.params["layer1"]["w"],
  100. ckpt2.params["layer1"]["w"])
  101. np.testing.assert_array_equal(ckpt.params["layer1"]["b"],
  102. ckpt2.params["layer1"]["b"])
  103. np.testing.assert_array_equal(ckpt.params["layer2"]["w"],
  104. ckpt2.params["layer2"]["w"])
  105. np.testing.assert_array_equal(ckpt.params["layer2"]["b"],
  106. ckpt2.params["layer2"]["b"])
  107. np.testing.assert_array_equal(ckpt.params["blah"], ckpt2.params["blah"])
  108. self.assertEqual(ckpt.config, ckpt2.config)
  109. if __name__ == "__main__":
  110. absltest.main()