xref: /aosp_15_r20/external/tensorflow/tensorflow/python/distribute/checkpointing_test.py (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
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# ==============================================================================
15
16import os
17
18from absl.testing import parameterized
19from tensorflow.python.checkpoint import checkpoint as trackable_utils
20from tensorflow.python.distribute import combinations
21from tensorflow.python.distribute import strategy_combinations
22from tensorflow.python.eager import test
23from tensorflow.python.ops import array_ops
24from tensorflow.python.ops import variables as variables_lib
25
26
27class TrainingCheckpointTests(test.TestCase, parameterized.TestCase):
28
29  @combinations.generate(
30      combinations.combine(
31          distribution=[
32              strategy_combinations.mirrored_strategy_with_one_cpu,
33              strategy_combinations.mirrored_strategy_with_gpu_and_cpu,
34              strategy_combinations.tpu_strategy,
35              strategy_combinations.tpu_strategy_packed_var,
36              strategy_combinations.central_storage_strategy_with_two_gpus,
37          ],
38          mode=["eager"]))
39  def testInitializeFromCheckpoint(self, distribution):
40    variable_shape = [5]
41    save_checkpoint = trackable_utils.Checkpoint(v=variables_lib.Variable(
42        array_ops.ones(variable_shape)))
43    save_path = save_checkpoint.save(
44        os.path.join(self.get_temp_dir(), "checkpoint"))
45    with distribution.scope():
46      restore_checkpoint = trackable_utils.Checkpoint()
47      restore_checkpoint.restore(save_path)
48      initial_value = restore_checkpoint._preload_simple_restoration(
49          "v")
50      v = variables_lib.Variable(initial_value)
51      # Check that the variable is now tagged as restored. `Checkpoint` then
52      # knows it doesn't have to restore `v`'s value when it's assigned to an
53      # object.
54      self.assertGreater(v._update_uid, 0)
55      self.assertAllClose(array_ops.ones(variable_shape), v)
56      v.assign(array_ops.zeros(variable_shape))
57      # Assignment to an object should not trigger restoration, since we already
58      # restored the object through an initializer. This wouldn't be a
59      # correctness issue, but it would mean that models would use twice as much
60      # memory when loading (the buffer already assigned to the variable, and
61      # the new restoration).
62      restore_checkpoint.v = v
63      self.assertAllClose(array_ops.zeros(variable_shape), v)
64
65
66if __name__ == "__main__":
67  test.main()
68