[9b26b7]: / third_party / nucleus / io / sharded_file_utils.py

Download this file

317 lines (249 with data), 11.5 kB

  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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# Copyright 2018 Google LLC.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
"""Utility functions for working with sharded files.
A sharded file is a single conceptual file that is broken into a collection
of files to make parallelization easier. A sharded file spec is like a
filename for a sharded file; the file spec "/some/path/prefix@200.txt"
says that the sharded file consists of 200 actual files that have names like
"/some/path/prefix-00000-of-00200.txt", "/some/path/prefix-00001-of-00200.txt",
etc. This module contains functions for parsing, generating, detecting and
resolving sharded file specs.
"""
# Important: Please keep this module free of TensorFlow c++ extensions.
# This makes it easy to build pure python packages for training that work with
# CMLE.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import math
import os
import re
from typing import List, Tuple
from etils import epath
import six
SHARD_SPEC_PATTERN = re.compile(R'((.*)\@(\d*[1-9]\d*)(?:\.(.+))?)')
SHARD_FILE_PATTERN = re.compile(R'(.*)-(\d+)-of-(\d*[1-9]\d*)([^/]+)?$')
class ShardError(Exception):
"""An I/O error."""
def parse_sharded_file_spec(spec):
"""Parse a sharded file specification.
Args:
spec: str. The sharded file specification. A sharded file spec is one like
'gs://some/file@200.txt'. Here, '@200' specifies the number of shards.
Returns:
basename: str. The basename for the files.
num_shards: int >= 0. The number of shards.
suffix: str. The suffix if there is one, or '' if not.
Raises:
ShardError: If the spec is not a valid sharded specification.
"""
m = SHARD_SPEC_PATTERN.match(spec)
if not m:
raise ShardError(('The file specification {0} is not a sharded file '
'specification because it did not match the regex '
'{1}').format(spec, SHARD_SPEC_PATTERN.pattern))
# If there's a non-empty suffix, we need to prepend '.' so we get files like
# foo@20.ext instead of foo@ext. The original C++ parser version has:
# string ext = StrCat(suff.empty() ? "" : ".", suff);
suffix = '.' + m.group(4) if m.group(4) else ''
return m.group(2), int(m.group(3)), suffix
def _shard_width(num_shards):
"""Return the width of the shard matcher based on the number of shards."""
return max(5, int(math.floor(math.log10(num_shards)) + 1))
def generate_sharded_filenames(spec: str) -> List[str]:
"""Generate the list of filenames corresponding to the sharding path.
Args:
spec: str. Represents a filename with a sharding specification.
e.g., 'gs://some/file@200.txt' represents a file sharded 200 ways.
Returns:
List of filenames.
Raises:
ShardError: If spec is not a valid sharded file specification.
"""
basename, num_shards, suffix = parse_sharded_file_spec(spec)
files = []
width = _shard_width(num_shards)
format_str = '{{0}}-{{1:0{0}}}-of-{{2:0{0}}}{{3}}'.format(width)
for i in range(num_shards):
files.append(format_str.format(basename, i, num_shards, suffix))
return files
def glob_list_sharded_file_patterns(
comma_separated_patterns: str, sep: str = ','
) -> List[str]:
"""Generate list of filenames corresponding to `comma_separated_patterns`.
Args:
comma_separated_patterns: str. A pattern or a comma-separated list of
patterns that represent file names.
sep: char. Separator character.
Returns:
List of filenames, sorted and dedupped.
"""
return sorted(set([
os.fspath(f)
for pattern in comma_separated_patterns.split(sep)
# Note: epath.Path().glob(normalize_to_sharded_file_pattern(pattern))
# does not work with gs://.
for f in epath.Path(
os.path.dirname(normalize_to_sharded_file_pattern(pattern))).glob(
os.path.basename(normalize_to_sharded_file_pattern(pattern)))
]))
def generate_sharded_file_pattern(basename, num_shards, suffix):
"""Generate a sharded file pattern.
Args:
basename: str. The basename for the files.
num_shards: int. The number of shards.
suffix: str. The suffix if there is one or ''.
Returns:
pattern:
"""
width = _shard_width(num_shards)
specifier = '?' * width
format_str = '{{0}}-{{1}}-of-{{2:0{0}}}{{3}}'.format(width)
return format_str.format(basename, specifier, num_shards, suffix)
def normalize_to_sharded_file_pattern(spec_or_pattern):
"""Take a sharding spec or sharding file pattern and return a sharded pattern.
The input can be a sharding spec(e.g '/some/file@10') or a sharded file
pattern (e.g. '/some/file-?????-of-00010)
Args:
spec_or_pattern: str. A sharded file specification or sharded file pattern.
Returns:
A sharded file pattern.
"""
try:
basename, num_shards, suffix = parse_sharded_file_spec(spec_or_pattern)
except ShardError:
return spec_or_pattern
return generate_sharded_file_pattern(basename, num_shards, suffix)
def is_sharded_filename(filename: str) -> bool:
"""Returns True if filename is a sharded filename."""
m = SHARD_FILE_PATTERN.match(filename)
return m is not None
def is_sharded_file_spec(spec: str) -> bool:
"""Returns True if spec is a sharded file specification."""
m = SHARD_SPEC_PATTERN.match(spec)
return m is not None
# TODO: retire when GenerateShardedFilename is added to library.
def sharded_filename(spec: str, i: int) -> str:
"""Gets a path appropriate for writing the ith file of a sharded spec."""
return generate_sharded_filenames(spec)[i]
# TODO: Improve the return value (instead of using tuple). It hurts
# readability when there are multiple input filespecs.
def resolve_filespecs(
shard: int, *filespecs: str
):
"""Transforms potentially sharded filespecs into their paths for single shard.
This function takes a shard number and a varargs of potentially-sharded
filespecs, and returns a list where the filespecs have been resolved into
concrete file paths for a single shard.
This function has a concept of a master filespec, which is used to constrain
and check the validity of other filespecs. The first filespec is considered
the master, and it cannot be None. For example, if master is not sharded, none
of the other specs can be sharded, and vice versa. They must all also have a
consistent sharding (e.g., master is @10, then all others must be @10).
Note that filespecs (except the master) may be None or any other False value,
which are returned as-is in the output list.
Args:
shard: int >= 0. Our shard number.
*filespecs: list[str]. Contains all of the filespecs we want to resolve into
shard-specific file paths.
Returns:
A list. The first element is the number of shards, which is an int >= 1 when
filespecs contains sharded paths and 0 if none do. All subsequent
returned values follow the shard-specific paths for each filespec, in order.
Raises:
ValueError: if any filespecs are inconsistent.
"""
if not filespecs:
raise ValueError('filespecs must have at least one element.')
master = filespecs[0]
master_is_sharded = is_sharded_file_spec(master)
master_num_shards = 0
if master_is_sharded:
_, master_num_shards, _ = parse_sharded_file_spec(master)
if shard >= master_num_shards or shard < 0:
raise ValueError('Invalid shard={} value with master={} sharding'.format(
shard, master))
elif shard > 0:
raise ValueError('Output is not sharded but shard > 0: {}'.format(shard))
def resolve_one(filespec):
"""Resolves a single filespec into a concrete filepath."""
if not filespec:
return filespec
is_sharded = is_sharded_file_spec(filespec)
if master_is_sharded != is_sharded:
raise ValueError('Master={} and {} have inconsistent sharding'.format(
master, filespec))
if not is_sharded: # Not sharded => filespec is the actual filename.
return filespec
_, filespec_num_shards, _ = parse_sharded_file_spec(filespec)
if filespec_num_shards != master_num_shards:
raise ValueError('Master={} and {} have inconsistent sharding'.format(
master, filespec))
return sharded_filename(filespec, shard)
return [master_num_shards] + [resolve_one(spec) for spec in filespecs]
def maybe_generate_sharded_filenames(filespec):
"""Potentially expands sharded filespec into a list of paths.
This function takes in a potentially sharded filespec and expands it into a
list containing the full set of corresponding concrete sharded file paths. If
the input filespec is not sharded then a list containing just that file path
is returned. This function is useful, for example, when the input to a binary
can either be sharded or not.
Args:
filespec: String. A potentially sharded filespec to expand.
Returns:
A list of file paths.
Raises:
TypeError: if filespec is not in valid string_types.
"""
if not isinstance(filespec, six.string_types):
raise TypeError('Invalid filespec: %s' % filespec)
if is_sharded_file_spec(filespec):
return generate_sharded_filenames(filespec)
else:
return [filespec]
def parse_sharded_filename(filename):
"""Parse a sharded name specification.
Args:
filename: str. The sharded filename. A sharded filename is one like
'gs://some/file-00000-of-00200.txt'.
Returns:
basename: str. The first part before the -ddddd-of-ddddd part.
shard_index: str: The first part of -ddddd-of-ddddd
num_shards: str: The number of shards. (Second part after "of")
suffix: str. Any string after the -ddddd-of-ddddd part.
Raises:
ShardError: If the spec is not a valid sharded specification.
"""
m = SHARD_FILE_PATTERN.match(filename)
if not m:
raise ShardError(('The file specification {0} is not a sharded file '
'specification because it did not match the regex '
'{1}').format(filename, SHARD_FILE_PATTERN.pattern))
suffix = '' if m.group(4) is None else m.group(4)
return m.group(1), m.group(2), m.group(3), suffix