Repository navigation
Expand file tree
/
Copy pathstructure.py
More file actions
573 lines (459 loc) · 20.8 KB
/
Copy pathstructure.py
File metadata and controls
573 lines (459 loc) · 20.8 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
#
# 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 describing the structure of a `tf.data` type."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import abc
import six
from tensorflow.python.data.util import nest
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import ops
from tensorflow.python.framework import sparse_tensor as sparse_tensor_lib
from tensorflow.python.framework import tensor_shape
from tensorflow.python.framework import tensor_util
from tensorflow.python.ops import sparse_ops
from tensorflow.python.util.tf_export import tf_export
_STRUCTURE_CONVERSION_FUNCTION_REGISTRY = {}
@tf_export("data.experimental.Structure")
@six.add_metaclass(abc.ABCMeta)
class Structure(object):
"""Represents structural information, such as type and shape, about a value.
A `Structure` generalizes the `tf.Tensor.dtype` and `tf.Tensor.shape`
properties, so that we can define generic containers of objects including:
* `tf.Tensor`
* `tf.SparseTensor`
* Nested structures of the above.
TODO(b/110122868): In the future, a single `Structure` will replace the
`tf.data.Dataset.output_types`, `tf.data.Dataset.output_shapes`,
and `tf.data.Dataset.output_classes`, and similar properties and arguments in
the `tf.data.Iterator` and `Optional` classes.
"""
@abc.abstractproperty
def _flat_shapes(self):
"""A list of shapes matching the shapes of `self._to_tensor_list()`.
Returns:
A list of `tf.TensorShape` objects.
"""
raise NotImplementedError("Structure._flat_shapes")
@abc.abstractproperty
def _flat_types(self):
"""A list of types matching the types of `self._to_tensor_list()`.
Returns:
A list of `tf.DType` objects.
"""
raise NotImplementedError("Structure._flat_shapes")
@abc.abstractmethod
def is_compatible_with(self, other):
"""Returns `True` if `other` is compatible with this structure.
A structure `t` is a "subtype" of `s` if:
* `s` and `t` are instances of the same `Structure` subclass.
* The nested structures (if any) of `s` and `t` are the same, according to
`tf.contrib.framework.nest.assert_same_structure`, and each nested
structure of `t` is a "subtype" of the corresponding nested structure of
`s`.
* Any `tf.DType` components of `t` are the same as the corresponding
components in `s`.
* Any `tf.TensorShape` components of `t` are compatible with the
corresponding components in `s`, according to
`tf.TensorShape.is_compatible_with`.
Args:
other: A `Structure`.
Returns:
`True` if `other` is a subtype of this structure, otherwise `False`.
"""
raise NotImplementedError("Structure.is_compatible_with()")
@abc.abstractmethod
def _to_tensor_list(self, value):
"""Returns a flat list of `tf.Tensor` representing `value`.
This method can be used, along with `self._flat_shapes` and
`self._flat_types` to represent structured values in lower level APIs
(such as plain TensorFlow operations) that do not understand structure.
Requires: `self.is_compatible_with(Structure.from_value(value))`.
Args:
value: A value with compatible structure.
Returns:
A flat list of `tf.Tensor` representing `value`.
"""
raise NotImplementedError("Structure._to_tensor_list()")
@abc.abstractmethod
def _to_batched_tensor_list(self, value):
"""Returns a flat list of rank >= 1 `tf.Tensor` representing `value`.
This method can be used, along with `self._flat_shapes` and
`self._flat_types` to represent structured values in lower level APIs
(such as plain TensorFlow operations) that do not understand structure,
*and* that require that the plain tensors have a rank of at least one
(e.g. for the purpose of slicing the tensors).
Requires: `self.is_compatible_with(Structure.from_value(value))`.
Args:
value: A value with compatible structure.
Returns:
A flat list of `tf.Tensor` representing `value`.
"""
raise NotImplementedError("Structure._to_batched_tensor_list()")
@abc.abstractmethod
def _from_tensor_list(self, flat_value):
"""Builds a flat list of `tf.Tensor` into a value matching this structure.
Args:
flat_value: A list of `tf.Tensor` with compatible flat structure.
Returns:
A structured object matching this structure.
Raises:
ValueError: If the shapes and types of the tensors in `flat_value` are not
compatible with `self._flat_shapes` and `self._flat_types` respectively.
"""
raise NotImplementedError("Structure._from_tensor_list()")
def _from_compatible_tensor_list(self, flat_value):
"""A version of `_from_tensor_list()` that may avoid performing checks.
NOTE: This method should be used to avoid checks for performance reasons,
when the validity of `flat_value` has been validated by other means.
The shapes and types of the tensors in `flat_value` must be compatible with
`self._flat_shapes` and `self._flat_types` respectively. The behavior is
undefined if this requirement is not met.
Args:
flat_value: A list of `tf.Tensor` with compatible flat structure.
Returns:
A structured object matching this structure.
"""
return self._from_tensor_list(flat_value)
@abc.abstractmethod
def _batch(self, batch_size):
"""Returns a structure representing a batch of objects with this structure.
Args:
batch_size: An `int` representing the number of elements in a batch,
or `None` if the batch size may vary.
Returns:
A `Structure` representing a batch of objects with this structure.
"""
raise NotImplementedError("Structure._batch()")
@abc.abstractmethod
def _unbatch(self):
raise NotImplementedError("Structure._unbatch()")
@staticmethod
def from_value(value):
"""Returns a `Structure` that represents the given `value`.
Args:
value: A potentially structured value.
Returns:
A `Structure` that is compatible with `value`.
Raises:
TypeError: If a structure cannot be built for `value`, because its type
or one of its component types is not supported.
"""
# TODO(b/110122868): Add support for custom types and Dataset to this
# method.
if isinstance(
value,
(sparse_tensor_lib.SparseTensor, sparse_tensor_lib.SparseTensorValue)):
return SparseTensorStructure.from_value(value)
elif isinstance(value, (tuple, dict)):
return NestedStructure.from_value(value)
else:
for converter_type, converter_fn in (
_STRUCTURE_CONVERSION_FUNCTION_REGISTRY.items()):
if isinstance(value, converter_type):
return converter_fn(value)
try:
tensor = ops.convert_to_tensor(value)
except (ValueError, TypeError):
raise TypeError("Could not build a structure for %r" % value)
return TensorStructure.from_value(tensor)
@staticmethod
def _register_custom_converter(type_object, converter_fn):
"""Registers `converter_fn` for converting values of the given type.
Args:
type_object: A Python `type` object representing the type of values
accepted by `converter_fn`.
converter_fn: A function that takes one argument (an instance of the
type represented by `type_object`) and returns a `Structure`.
"""
_STRUCTURE_CONVERSION_FUNCTION_REGISTRY[type_object] = converter_fn
@abc.abstractmethod
def _to_legacy_output_types(self):
raise NotImplementedError("Structure._to_legacy_output_types()")
@abc.abstractmethod
def _to_legacy_output_shapes(self):
raise NotImplementedError("Structure._to_legacy_output_shapes()")
@abc.abstractmethod
def _to_legacy_output_classes(self):
raise NotImplementedError("Structure._to_legacy_output_classes()")
def convert_legacy_structure(output_types, output_shapes, output_classes):
"""Returns a `Structure` that represents the given legacy structure.
This method provides a way to convert from the existing `Dataset` and
`Iterator` structure-related properties to a `Structure` object. A "legacy"
structure is represented by the `tf.data.Dataset.output_types`,
`tf.data.Dataset.output_shapes`, and `tf.data.Dataset.output_classes`
properties.
TODO(b/110122868): Remove this function once `Structure` is used throughout
`tf.data`.
Args:
output_types: A nested structure of `tf.DType` objects corresponding to
each component of a structured value.
output_shapes: A nested structure of `tf.TensorShape` objects
corresponding to each component a structured value.
output_classes: A nested structure of Python `type` objects corresponding
to each component of a structured value.
Returns:
A `Structure`.
Raises:
TypeError: If a structure cannot be built from the arguments, because one of
the component classes in `output_classes` is not supported.
"""
flat_types = nest.flatten(output_types)
flat_shapes = nest.flatten(output_shapes)
flat_classes = nest.flatten(output_classes)
flat_ret = []
for flat_type, flat_shape, flat_class in zip(flat_types, flat_shapes,
flat_classes):
if isinstance(flat_class, Structure):
flat_ret.append(flat_class)
elif issubclass(flat_class, sparse_tensor_lib.SparseTensor):
flat_ret.append(SparseTensorStructure(flat_type, flat_shape))
elif issubclass(flat_class, ops.Tensor):
flat_ret.append(TensorStructure(flat_type, flat_shape))
else:
# NOTE(mrry): Since legacy structures produced by iterators only
# comprise Tensors, SparseTensors, and nests, we do not need to
# support all structure types here.
raise TypeError(
"Could not build a structure for output class %r" % flat_type)
ret = nest.pack_sequence_as(output_classes, flat_ret)
if isinstance(ret, Structure):
return ret
else:
return NestedStructure(ret)
# NOTE(mrry): The following classes make extensive use of non-public methods of
# their base class, so we disable the protected-access lint warning once here.
# pylint: disable=protected-access
@tf_export("data.experimental.NestedStructure")
class NestedStructure(Structure):
"""Represents a nested structure in which each leaf is a `Structure`."""
def __init__(self, nested_structure):
self._nested_structure = nested_structure
self._flat_nested_structure = nest.flatten(nested_structure)
self._flat_shapes_list = []
self._flat_types_list = []
for s in nest.flatten(nested_structure):
if not isinstance(s, Structure):
raise TypeError("nested_structure must be a (potentially nested) tuple "
"or dictionary of Structure objects.")
self._flat_shapes_list.extend(s._flat_shapes)
self._flat_types_list.extend(s._flat_types)
@property
def _flat_shapes(self):
return self._flat_shapes_list
@property
def _flat_types(self):
return self._flat_types_list
def is_compatible_with(self, other):
if not isinstance(other, NestedStructure):
return False
try:
# pylint: disable=protected-access
nest.assert_same_structure(self._nested_structure,
other._nested_structure)
except (ValueError, TypeError):
return False
return all(
substructure.is_compatible_with(other_substructure)
for substructure, other_substructure in zip(
nest.flatten(self._nested_structure),
nest.flatten(other._nested_structure)))
def _to_tensor_list(self, value):
ret = []
try:
flat_value = nest.flatten_up_to(self._nested_structure, value)
except (ValueError, TypeError):
raise ValueError("The value %r is not compatible with the nested "
"structure %r." % (value, self._nested_structure))
for sub_value, structure in zip(flat_value, self._flat_nested_structure):
if not structure.is_compatible_with(Structure.from_value(sub_value)):
raise ValueError("Component value %r is not compatible with the nested "
"structure %r." % (sub_value, structure))
ret.extend(structure._to_tensor_list(sub_value))
return ret
def _to_batched_tensor_list(self, value):
ret = []
try:
flat_value = nest.flatten_up_to(self._nested_structure, value)
except (ValueError, TypeError):
raise ValueError("The value %r is not compatible with the nested "
"structure %r." % (value, self._nested_structure))
for sub_value, structure in zip(flat_value, self._flat_nested_structure):
if not structure.is_compatible_with(Structure.from_value(sub_value)):
raise ValueError("Component value %r is not compatible with the nested "
"structure %r." % (sub_value, structure))
ret.extend(structure._to_batched_tensor_list(sub_value))
return ret
def _from_tensor_list(self, flat_value):
if len(flat_value) != len(self._flat_types):
raise ValueError("Expected %d flat values in NestedStructure but got %d."
% (len(self._flat_types), len(flat_value)))
flat_ret = []
i = 0
for structure in self._flat_nested_structure:
num_flat_values = len(structure._flat_types)
sub_value = flat_value[i:i + num_flat_values]
flat_ret.append(structure._from_tensor_list(sub_value))
i += num_flat_values
return nest.pack_sequence_as(self._nested_structure, flat_ret)
def _from_compatible_tensor_list(self, flat_value):
flat_ret = []
i = 0
for structure in self._flat_nested_structure:
num_flat_values = len(structure._flat_types)
sub_value = flat_value[i:i + num_flat_values]
flat_ret.append(structure._from_compatible_tensor_list(sub_value))
i += num_flat_values
return nest.pack_sequence_as(self._nested_structure, flat_ret)
@staticmethod
def from_value(value):
flat_nested_structure = [
Structure.from_value(sub_value) for sub_value in nest.flatten(value)
]
return NestedStructure(nest.pack_sequence_as(value, flat_nested_structure))
def _to_legacy_output_types(self):
return nest.map_structure(
lambda s: s._to_legacy_output_types(), self._nested_structure)
def _to_legacy_output_shapes(self):
return nest.map_structure(
lambda s: s._to_legacy_output_shapes(), self._nested_structure)
def _to_legacy_output_classes(self):
return nest.map_structure(
lambda s: s._to_legacy_output_classes(), self._nested_structure)
def _batch(self, batch_size):
return NestedStructure(nest.map_structure(
lambda s: s._batch(batch_size), self._nested_structure))
def _unbatch(self):
return NestedStructure(nest.map_structure(
lambda s: s._unbatch(), self._nested_structure))
@tf_export("data.experimental.TensorStructure")
class TensorStructure(Structure):
"""Represents structural information about a `tf.Tensor`."""
def __init__(self, dtype, shape):
self._dtype = dtypes.as_dtype(dtype)
self._shape = tensor_shape.as_shape(shape)
@property
def _flat_shapes(self):
return [self._shape]
@property
def _flat_types(self):
return [self._dtype]
def is_compatible_with(self, other):
return (isinstance(other, TensorStructure) and
self._dtype.is_compatible_with(other._dtype) and
self._shape.is_compatible_with(other._shape))
def _to_tensor_list(self, value):
if not self.is_compatible_with(Structure.from_value(value)):
raise ValueError("Value %r is not convertible to a tensor with dtype %s "
"and shape %s." % (value, self._dtype, self._shape))
return [value]
def _to_batched_tensor_list(self, value):
if self._shape.merge_with(value.shape).ndims == 0:
raise ValueError("Unbatching a tensor is only supported for rank >= 1")
return [value]
def _from_tensor_list(self, flat_value):
if len(flat_value) != 1:
raise ValueError("TensorStructure corresponds to a single tf.Tensor.")
if not self.is_compatible_with(Structure.from_value(flat_value[0])):
raise ValueError("Cannot convert %r to a tensor with dtype %s and shape "
"%s." % (flat_value[0], self._dtype, self._shape))
return self._from_compatible_tensor_list(flat_value)
def _from_compatible_tensor_list(self, flat_value):
# TODO(b/112266545): It would be cleaner to create a new `ensure_shape()`
# op here and return that, instead of mutating the input's shape using
# `Tensor.set_shape()`. However, that would add extra ops on the arguments
# of each `tf.data` function, which could impact performance. When this
# bug is resolved, we should be able to add the `ensure_shape()` ops and
# optimize them away using contextual shape information.
flat_value[0].set_shape(self._shape)
return flat_value[0]
@staticmethod
def from_value(value):
return TensorStructure(value.dtype, value.shape)
def _to_legacy_output_types(self):
return self._dtype
def _to_legacy_output_shapes(self):
return self._shape
def _to_legacy_output_classes(self):
return ops.Tensor
def _batch(self, batch_size):
return TensorStructure(
self._dtype,
tensor_shape.TensorShape([batch_size]).concatenate(self._shape))
def _unbatch(self):
if self._shape.ndims == 0:
raise ValueError("Unbatching a tensor is only supported for rank >= 1")
return TensorStructure(self._dtype, self._shape[1:])
@tf_export("data.experimental.SparseTensorStructure")
class SparseTensorStructure(Structure):
"""Represents structural information about a `tf.SparseTensor`."""
def __init__(self, dtype, dense_shape):
self._dtype = dtypes.as_dtype(dtype)
self._dense_shape = tensor_shape.as_shape(dense_shape)
@property
def _flat_shapes(self):
# NOTE(mrry): The default flat shape of a boxed `SparseTensor` is `(3,)`,
# but a `SparseTensorStructure` can also represent a batch of boxed
# `SparseTensor` objects with shape `(?, 3)` (and batches of batches, etc.),
# so the flat shape must be unknown.
return [tensor_shape.unknown_shape(None)]
@property
def _flat_types(self):
return [dtypes.variant]
def is_compatible_with(self, other):
return (isinstance(other, SparseTensorStructure) and
self._dtype.is_compatible_with(other._dtype) and
self._dense_shape.is_compatible_with(other._dense_shape))
def _to_tensor_list(self, value):
return [sparse_ops.serialize_sparse(value, out_type=dtypes.variant)]
def _to_batched_tensor_list(self, value):
if self._dense_shape.merge_with(
tensor_util.constant_value_as_shape(value.dense_shape)).ndims == 0:
raise ValueError(
"Unbatching a sparse tensor is only supported for rank >= 1")
return [sparse_ops.serialize_many_sparse(value, out_type=dtypes.variant)]
def _from_tensor_list(self, flat_value):
if (len(flat_value) != 1 or flat_value[0].dtype != dtypes.variant or
not flat_value[0].shape.is_compatible_with(tensor_shape.vector(3))):
raise ValueError("SparseTensorStructure corresponds to a single "
"tf.variant vector of length 3.")
return self._from_compatible_tensor_list(flat_value)
def _from_compatible_tensor_list(self, flat_value):
ret = sparse_ops.deserialize_sparse(
flat_value[0], dtype=self._dtype, rank=self._dense_shape.ndims)
ret.indices.set_shape([None, self._dense_shape.ndims])
ret.dense_shape.set_shape([self._dense_shape.ndims])
return ret
@staticmethod
def from_value(value):
sparse_tensor = sparse_tensor_lib.SparseTensor.from_value(value)
return SparseTensorStructure(
sparse_tensor.dtype,
tensor_util.constant_value_as_shape(sparse_tensor.dense_shape))
def _to_legacy_output_types(self):
return self._dtype
def _to_legacy_output_shapes(self):
return self._dense_shape
def _to_legacy_output_classes(self):
return sparse_tensor_lib.SparseTensor
def _batch(self, batch_size):
return SparseTensorStructure(
self._dtype,
tensor_shape.TensorShape([batch_size]).concatenate(self._dense_shape))
def _unbatch(self):
if self._dense_shape.ndims == 0:
raise ValueError("Unbatching a tensor is only supported for rank >= 1")
return SparseTensorStructure(self._dtype, self._dense_shape[1:])