/
file_program_state_manager.py
281 lines (235 loc) · 10.2 KB
/
file_program_state_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# Copyright 2019, The TensorFlow Federated Authors.
#
# 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.
"""Utilities for saving and loading program state to a file system.
Note: This library uses `tf.io.gfile` to perform file system operations, this
means that this library:
* supports all the file systems supported by `tf.io.gfile`
* encodes files in the same way as `tf.io.gfile`
"""
import asyncio
import os
import os.path
from typing import Optional, Union
from absl import logging
import numpy as np
import tensorflow as tf
from tensorflow_federated.python.common_libs import py_typecheck
from tensorflow_federated.python.common_libs import serializable
from tensorflow_federated.python.program import file_utils
from tensorflow_federated.python.program import program_state_manager
from tensorflow_federated.python.program import structure_utils
from tensorflow_federated.python.program import value_reference
class FileProgramStateManager(
program_state_manager.ProgramStateManager[
program_state_manager.ProgramStateStructure
]
):
"""A `tff.program.ProgramStateManager` that is backed by a file system.
A `tff.program.FileProgramStateManager` is a utility for saving and loading
program state to a file system in a federated program and is used to implement
fault tolerance. In particular, it is intended to only restart the same
simulation and run with the same version of TensorFlow Federated.
Program state is saved to the file system using the SavedModel (see
`tf.saved_model`) format. When the program state is saved, if the program
state is a value reference or a structure containing value references, each
value reference is materialized. The program state is then flattened and saved
using the SavedModel format. The structure of the program state is discarded,
but is required to load the program state.
Note: The SavedModel format can only contain values that can be converted to a
`tf.Tensor` (see `tf.convert_to_tensor`), releasing any other values will
result in an error.
See https://www.tensorflow.org/guide/saved_model for more information about
the SavedModel format.
"""
def __init__(
self,
root_dir: Union[str, os.PathLike[str]],
prefix: str = 'program_state_',
keep_total: int = 5,
keep_first: bool = True,
):
"""Returns an initialized `tff.program.ProgramStateManager`.
Args:
root_dir: A path on the file system to save program state. If this path
does not exist it will be created.
prefix: A string to use as the prefix for filenames.
keep_total: An integer representing the total number of program states to
keep. If the value is zero or smaller, all program states will be kept.
keep_first: A boolean indicating if the first program state should be
kept, irrespective of whether it is the oldest program state or not.
This is desirable in settings where you would like to ensure full
reproducibility of the simulation, especially in settings where model
weights or optimizer states are initialized randomly. By loading from
the initial program state, one can avoid re-initializing and obtaining
different results.
Raises:
ValueError: If `root_dir` is an empty string.
"""
py_typecheck.check_type(root_dir, (str, os.PathLike))
if not root_dir:
raise ValueError('Expected `root_dir` to not be an empty string.')
py_typecheck.check_type(prefix, str)
py_typecheck.check_type(keep_total, int)
py_typecheck.check_type(keep_first, bool)
if not tf.io.gfile.exists(root_dir):
tf.io.gfile.makedirs(root_dir)
self._root_dir = root_dir
self._prefix = prefix
self._keep_total = keep_total
self._keep_first = keep_first
async def get_versions(self) -> Optional[list[int]]:
"""Returns a list of saved versions or `None`.
Returns:
A list of saved versions or `None` if there is no saved program state.
"""
if not await file_utils.exists(self._root_dir):
return None
versions = []
# Due to tensorflow/issues/19378, we cannot use `tf.io.gfile.glob` here
# because it returns directory contents recursively on Windows.
entries = await file_utils.listdir(self._root_dir)
for entry in entries:
if entry.startswith(self._prefix):
version = self._get_version_for_path(entry)
if version is not None:
versions.append(version)
if not versions:
return None
return sorted(versions)
def _get_version_for_path(
self, path: Union[str, os.PathLike[str]]
) -> Optional[int]:
"""Returns the version for the given `path` or `None`.
This method does not assert that the given `path` or the returned version
represent saved program state.
Args:
path: The path to extract the version from.
"""
py_typecheck.check_type(path, (str, os.PathLike))
basename = os.path.basename(path)
if basename.startswith(self._prefix):
version = basename[len(self._prefix) :]
else:
version = None
try:
return int(version)
except (TypeError, ValueError):
return None
def _get_path_for_version(self, version: int) -> str:
"""Returns the path for the given `version`.
This method does not assert that the given `version` or the returned path
represent saved program state.
Args:
version: The version used to construct the path.
"""
py_typecheck.check_type(version, (int, np.integer))
basename = f'{self._prefix}{version}'
return os.path.join(self._root_dir, basename)
async def load(
self, version: int, structure: program_state_manager.ProgramStateStructure
) -> program_state_manager.ProgramStateStructure:
"""Returns the program state for the given `version`.
Args:
version: A integer representing the version of a saved program state.
structure: The structure of the saved program state for the given
`version` used to support serialization and deserialization of
user-defined classes in the structure.
Raises:
ProgramStateNotFoundError: If there is no program state for the given
`version`.
"""
py_typecheck.check_type(version, int)
path = self._get_path_for_version(version)
if not await file_utils.exists(path):
raise program_state_manager.ProgramStateNotFoundError(version)
flattened_state = await file_utils.read_saved_model(path)
program_state = structure_utils.unflatten_as(structure, flattened_state)
def _normalize(
value: program_state_manager.ProgramStateValue,
) -> program_state_manager.ProgramStateValue:
"""Returns a normalized value.
The `tff.program.FileProgramStateManager` saves and loads program state to
the file system using the SavedModel format. When the program state is
loaded, the values will be TF-native types. This function normalizes those
values as numpy values so that when program state is returned, those
values can be used more naturally.
Args:
value: The value to normalize.
"""
if isinstance(value, tf.Tensor):
return value.numpy()
else:
return value
normalized_state = structure_utils.map_structure(_normalize, program_state)
def _deserialize_as(structure, value):
if isinstance(structure, serializable.Serializable):
serializable_cls = type(structure)
value = serializable_cls.from_bytes(value)
return value
deserialized_state = structure_utils.map_structure(
_deserialize_as, structure, normalized_state
)
logging.info('Program state loaded: %s', path)
return deserialized_state
async def _remove(self, version: int) -> None:
"""Removes program state for the given `version`."""
py_typecheck.check_type(version, (int, np.integer))
path = self._get_path_for_version(version)
if await file_utils.exists(path):
await file_utils.rmtree(path)
logging.info('Program state removed: %s', path)
async def _remove_old_program_state(self) -> None:
"""Removes old program state."""
if self._keep_total <= 0:
return
versions = await self.get_versions()
if versions is not None and len(versions) > self._keep_total:
start = 1 if self._keep_first else 0
stop = len(versions) - (self._keep_total - start)
await asyncio.gather(*[self._remove(v) for v in versions[start:stop]])
async def remove_all(self) -> None:
"""Removes all program states."""
versions = await self.get_versions()
if versions is not None:
await asyncio.gather(*[self._remove(v) for v in versions])
async def save(
self,
program_state: program_state_manager.ProgramStateStructure,
version: int,
) -> None:
"""Saves `program_state` for the given `version`.
Args:
program_state: A `tff.program.ProgramStateStructure` to save.
version: A strictly increasing integer representing the version of a saved
`program_state`.
Raises:
ProgramStateExistsError: If there is already program state for the given
`version`.
"""
py_typecheck.check_type(version, (int, np.integer))
path = self._get_path_for_version(version)
if await file_utils.exists(path):
raise program_state_manager.ProgramStateExistsError(version)
materialized_state = await value_reference.materialize_value(program_state)
def _serialize(value):
if isinstance(value, serializable.Serializable):
value = value.to_bytes()
return value
serialized_state = structure_utils.map_structure(
_serialize, materialized_state
)
flattened_state = structure_utils.flatten(serialized_state)
await file_utils.write_saved_model(flattened_state, path)
logging.info('Program state saved: %s', path)
await self._remove_old_program_state()