-
Notifications
You must be signed in to change notification settings - Fork 217
/
Copy pathimpl.py
1684 lines (1439 loc) · 68.9 KB
/
impl.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
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2017 Google Inc. 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.
"""An implementation of tf.Transform using Beam.
The beam implementation takes a user defined preprocessing function (see
examples for how to define a preprocessing function) and implements it as a
Beam PTransform.
The AnalyzeDataset takes the user's preprocessing function and converts into
a TensorFlow function that can be applied to each row of a dataset. For
example if the user's preprocessing function describes normalizing a column by
subtracting its mean, the tensorflow function will contain the mean of the
column as a constant, and will subtract this value from each value of the
column. We refer to the result of AnalyzeDataset as a "transform function".
Since AnalyzeDataset is implemented with beam, it accepts a PCollection that
represents the dataset (see below for the exact format) and returns a singleton
PCollection containing the transform function (as a serialized TF graph).
The TransformDataset PTransform takes a dataset and a transform function, and
returns the transformed dataset where the transform function is applied to each
row of the original dataset.
There is also an AnalyzeAndTransformDataset PTransform that applies
AnalyzeDataset and TransformDataset to the same input dataset, possibly with
optimizations.
"""
import collections
import copy
import dataclasses
import datetime
import os
from absl import logging
import apache_beam as beam
from apache_beam.runners.portability import fn_api_runner
from apache_beam.typehints import Any
from apache_beam.typehints import Dict
from apache_beam.typehints import Iterable
from apache_beam.typehints import List
from apache_beam.typehints import Optional
from apache_beam.typehints import Set
from apache_beam.typehints import Tuple
from apache_beam.typehints import Union
from apache_beam.utils import shared
import numpy as np
import pyarrow as pa
import tensorflow as tf
from tensorflow_transform import annotators
from tensorflow_transform import common
from tensorflow_transform import common_types
from tensorflow_transform import graph_context
from tensorflow_transform import graph_tools
from tensorflow_transform import impl_helper
from tensorflow_transform import nodes
from tensorflow_transform import schema_inference
from tensorflow_transform.beam import analysis_graph_builder
from tensorflow_transform.beam import analyzer_cache
from tensorflow_transform.beam import beam_nodes
from tensorflow_transform.beam import common as beam_common
from tensorflow_transform.beam import context
from tensorflow_transform.beam import deep_copy
from tensorflow_transform.beam.tft_beam_io import beam_metadata_io
from tensorflow_transform.coders import example_proto_coder
from tensorflow_transform.saved import saved_transform_io
from tensorflow_transform.saved import saved_transform_io_v2
from tensorflow_transform.tf_metadata import dataset_metadata
from tensorflow_transform.tf_metadata import metadata_io
from tensorflow_transform.tf_metadata import schema_utils
from tfx_bsl import beam as tfx_bsl_beam
from tfx_bsl.coders import example_coder
from tfx_bsl.telemetry import collection as telemetry
from tfx_bsl.telemetry import util as telemetry_util
from tfx_bsl.tfxio import tensor_representation_util
from tfx_bsl.tfxio import tensor_to_arrow
from tfx_bsl.tfxio import tf_example_record
from tfx_bsl.tfxio.tensor_adapter import TensorAdapter
from tfx_bsl.tfxio.tensor_adapter import TensorAdapterConfig
from tensorflow_metadata.proto.v0 import schema_pb2
tfx_bsl_beam.fix_code_type_pickling()
_TransformFnPathType = str
Context = context.Context
_CREATE_SAVED_MODEL_COUNTER_NAME = 'saved_models_created'
# For some runners, we rely on Beam to manage concurrency, i.e. we expect it to
# run one session per CPU--so we don't want to proliferate TF threads.
# Nonetheless we provide 4 threads per session for TF ops, 2 inter-
# and 2 intra-thread. In many cases only 2 of these will be runnable
# at any given time. This approach oversubscribes a bit to make sure
# the CPUs are really saturated.
_FIXED_PARALLELISM_TF_CONFIG = tf.compat.v1.ConfigProto(
# TODO(b/36091595): use_per_session_threads is deprecated, but the
# replacement session_inter_op_thread_pool is experimental; using
# the former for now.
use_per_session_threads=True,
inter_op_parallelism_threads=2,
intra_op_parallelism_threads=2)
_DEFAULT_TENSORFLOW_CONFIG_BY_BEAM_RUNNER_TYPE = {
# TODO(katsiapis): Perhaps remove this entry once b/69922446 and b/30837990
# are resolved.
beam.runners.DataflowRunner: _FIXED_PARALLELISM_TF_CONFIG,
beam.runners.DirectRunner: _FIXED_PARALLELISM_TF_CONFIG,
fn_api_runner.FnApiRunner: _FIXED_PARALLELISM_TF_CONFIG,
}
# Batches larger than this will be sliced into smaller chunks. This size
# constraint must be at least as strict as the following constraints:
# 1. Number of elements in each individual array of the batch must be less
# than or equal to 2^31 - 1. Beam's `pa.RecordBatch` PCoder does not
# support larger sizes (even though the produced containers such as
# LargeListArray and LargeBinaryArray support them).
# 2. Serialized size of the batch must be less than 2GB. Beam's shuffle
# stage will wrap the serialized batches into a proto for materialization.
# 2GB is the proto size limit.
# We set a much stricter limit than the above to additionaly improve the outputs
# handling by making the size distributed over larger number of (still
# reasonably big) batches.
_MAX_TRANSFORMED_BATCH_BYTES_SIZE = 200 << 10 << 10 # 200MB
# TODO(b/68154497): pylint: disable=no-value-for-parameter
# TODO(b/64956765): Remove this once either the keepalive issue (b/30837990), or
# the mentioned bug above is resolved.
# TODO(zoyahav): Make this a PTransform.
def _clear_shared_state_after_barrier(pipeline, input_barrier):
"""Clears any shared state from within a pipeline context.
This will only be cleared once input_barrier becomes available.
Args:
pipeline: A `beam.Pipeline` object.
input_barrier: A `PCollection` which the pipeline should wait for.
Returns:
An empty `PCollection`.
"""
empty_pcoll = input_barrier | 'MakeCheapBarrier' >> beam.FlatMap(
lambda x: None)
return (pipeline
| 'PrepareToClearSharedKeepAlives' >> beam.Create([None])
| 'WaitAndClearSharedKeepAlives' >> beam.Map(
lambda x, empty_side_input: shared.Shared().acquire(lambda: None),
beam.pvalue.AsIter(empty_pcoll)))
# TODO(b/36223892): Verify that these type hints work and make needed fixes.
@beam.typehints.with_input_types(
Union[List[common_types.InstanceDictType], pa.RecordBatch],
_TransformFnPathType,
)
@beam.typehints.with_output_types(
Dict[str, Union[np.ndarray, tf.compat.v1.SparseTensorValue]])
class _RunMetaGraphDoFn(beam.DoFn):
"""Maps a PCollection of dicts to a PCollection of dicts via a TF graph.
The TF graph may contain more inputs than the schema provided. In that case,
a subset of the inputs will be fed, which may cause an error if the excluded
inputs are required to produce the included outputs.
"""
class _GraphStateCommon:
"""A container for a shared graph state."""
def __init__(self, saved_model_dir, input_tensor_keys, output_tensor_keys,
callable_get_outputs):
self.saved_model_dir = saved_model_dir
self.inputs_tensor_keys = input_tensor_keys
self.outputs_tensor_keys = output_tensor_keys
self.callable_get_outputs = callable_get_outputs
# Thread-safe.
class _GraphStateCompatV1(_GraphStateCommon):
"""A container for a shared TF1 graph state."""
def __init__(self, saved_model_dir, input_tensor_names, exclude_outputs,
tf_config):
with tf.compat.v1.Graph().as_default() as graph:
self._session = tf.compat.v1.Session(graph=graph, config=tf_config)
with self._session.as_default():
inputs, outputs = (
saved_transform_io.partially_apply_saved_transform_internal(
saved_model_dir, {}))
self._session.run(tf.compat.v1.global_variables_initializer())
self._session.run(tf.compat.v1.tables_initializer())
graph.finalize()
if set(input_tensor_names).difference(inputs.keys()):
raise ValueError(
'Input tensor names contained tensors not in graph: %s' %
input_tensor_names)
if set(exclude_outputs).difference(outputs.keys()):
raise ValueError('Excluded outputs contained keys not in graph: %s' %
exclude_outputs)
non_excluded_output_keys = sorted(
set(outputs.keys()).difference(exclude_outputs))
fetches = [outputs[key] for key in non_excluded_output_keys]
tensor_inputs = graph_tools.get_dependent_inputs(graph, inputs, fetches)
inputs_tensor_keys = sorted(tensor_inputs.keys())
outputs_tensor_keys = non_excluded_output_keys
tensor_inputs_list = [tensor_inputs[key] for key in inputs_tensor_keys]
callable_get_outputs = self._session.make_callable(
fetches, feed_list=tensor_inputs_list)
super().__init__(saved_model_dir, inputs_tensor_keys,
outputs_tensor_keys, callable_get_outputs)
# Thread-safe.
class _GraphStateV2(_GraphStateCommon):
"""A container for a shared TF2 graph state."""
def __init__(self, saved_model_dir, input_tensor_names, exclude_outputs):
saved_model_loader = saved_transform_io_v2.SavedModelLoader(
saved_model_dir)
callable_get_outputs = saved_model_loader.apply_transform_model
outputs_tensor_keys = set(
saved_model_loader.structured_outputs.keys()).difference(
exclude_outputs)
saved_model_loader.finalize(input_tensor_names, outputs_tensor_keys)
super().__init__(saved_model_dir, input_tensor_names, outputs_tensor_keys,
callable_get_outputs)
# Initialized in process().
_graph_state: _GraphStateCommon
# Initialized in setup().
_tensor_adapter: TensorAdapter
# i-th element in this list contains the index of the column corresponding
# to self._passthrough_keys[i].
_passthrough_column_indices: List[int]
def __init__(
self,
tf_config,
shared_graph_state_handle,
passthrough_keys,
use_tf_compat_v1,
input_tensor_adapter_config,
exclude_outputs=None,
):
"""Initialize.
Args:
tf_config: A tf.ConfigProto to use in sessions. None implies use
Tensorflow defaults.
shared_graph_state_handle: an instance of shared.Shared() that allows us
to load the graph once and share it across multiple threads in the
current process.
passthrough_keys: A set of strings that are keys to instances that should
pass through the pipeline and be hidden from the preprocessing_fn.
use_tf_compat_v1: Boolean to indicate whether TFT APIs should use TF in
compat.v1 mode.
input_tensor_adapter_config: Tensor Adapter config.
exclude_outputs: (Optional) A list of names of outputs to exclude.
"""
super().__init__()
self._use_tf_compat_v1 = use_tf_compat_v1
self._input_tensor_adapter_config = input_tensor_adapter_config
self._exclude_outputs = (
exclude_outputs if exclude_outputs is not None else [])
self._tf_config = tf_config
passthrough_keys = set(passthrough_keys)
schema_keys = self._get_input_tensor_names()
if passthrough_keys - schema_keys != passthrough_keys:
raise ValueError(
'passthrough_keys overlap with schema keys: {}, {}'.format(
passthrough_keys, schema_keys))
self._passthrough_keys = sorted(passthrough_keys)
# The shared graph state handle allows us to load the graph once and share
# it across multiple threads in the current process.
self._shared_graph_state_handle = shared_graph_state_handle
# Metrics.
self._graph_load_seconds_distribution = beam.metrics.Metrics.distribution(
beam_common.METRICS_NAMESPACE, 'graph_load_seconds')
self._batch_size_distribution = beam.metrics.Metrics.distribution(
beam_common.METRICS_NAMESPACE, 'batch_size')
self._num_instances = beam.metrics.Metrics.counter(
beam_common.METRICS_NAMESPACE, 'num_instances')
def _get_input_tensor_names(self):
return set(self._input_tensor_adapter_config.tensor_representations.keys())
def _update_metrics(self, batch):
self._batch_size_distribution.update(batch.num_rows)
self._num_instances.inc(batch.num_rows)
def _make_feed_dict(self, batch):
# If self._use_tf_compat_v1 is True, do not produce eager tensors.
produce_eager_tensors = not self._use_tf_compat_v1
return self._tensor_adapter.ToBatchTensors(
batch, produce_eager_tensors=produce_eager_tensors)
def _get_passthrough_data_from_recordbatch(
self, batch: pa.RecordBatch
) -> Dict[str, pa.Array]:
result = {}
for passthrough_key, column_index in zip(
self._passthrough_keys, self._passthrough_column_indices
):
if column_index >= 0:
# The key is present in the input batch.
passthrough_data_column = batch.column(column_index)
# The passthrough column should be of (large_)list<primitive> type with
# each sub-list being either null or of length 1.
assert (pa.types.is_list(passthrough_data_column.type) or
pa.types.is_large_list(passthrough_data_column.type))
result[passthrough_key] = passthrough_data_column
return result
def _handle_batch(self, batch):
self._update_metrics(batch)
# No need to remove (and cannot remove) the passthrough columns here:
# 1) The TensorAdapter expects the RecordBatch to be of the same schema as
# statically determined by the TFXIO implementation the yields the
# TensorAdapter.
# 2) It's not possible to leak passthrough columns through TensorAdapter
# because they are not going to be converted to Tensors.
feed_dict = self._make_feed_dict(batch)
try:
if self._use_tf_compat_v1:
# Use self._graph_state.inputs_tensor_keys and not the dictionary keys
# to maintain order of the feed list.
feed_list = [
feed_dict[name] for name in self._graph_state.inputs_tensor_keys
]
outputs_list = self._graph_state.callable_get_outputs(*feed_list)
assert len(self._graph_state.outputs_tensor_keys) == len(outputs_list)
result = {
key: value for key, value in zip(
self._graph_state.outputs_tensor_keys, outputs_list)
}
else:
result = self._graph_state.callable_get_outputs(feed_dict)
assert len(self._graph_state.outputs_tensor_keys) == len(result)
except Exception as e:
raise ValueError(
"""An error occurred while trying to apply the transformation: "{}".
Batch instances: {},
Fetching the values for the following Tensor keys: {}.""".format(
str(e), batch, self._graph_state.outputs_tensor_keys)) from e
result.update(self._get_passthrough_data_from_recordbatch(batch))
return result
def _make_graph_state(self, saved_model_dir):
start = datetime.datetime.now()
if self._use_tf_compat_v1:
result = self._GraphStateCompatV1(saved_model_dir,
self._get_input_tensor_names(),
self._exclude_outputs, self._tf_config)
else:
result = self._GraphStateV2(saved_model_dir,
self._get_input_tensor_names(),
self._exclude_outputs)
self._graph_load_seconds_distribution.update(
int((datetime.datetime.now() - start).total_seconds()))
return result
def setup(self):
assert self._input_tensor_adapter_config is not None
self._tensor_adapter = TensorAdapter(self._input_tensor_adapter_config)
arrow_schema = self._input_tensor_adapter_config.arrow_schema
self._passthrough_column_indices = [
arrow_schema.get_field_index(k) for k in self._passthrough_keys
]
def process(self, batch, saved_model_dir):
"""Runs the given graph to realize the outputs.
Runs the graph in a TF session for computing the output values of the
`Tensor`s, `SparseTensor`s, or `RaggedTensor`s, given an input row of data
(input `Tensor`s, `SparseTensor`s, or `RaggedTensor`s).
Args:
batch: the batch of elements being processed by the DoFn
saved_model_dir: Directory containing saved model.
Yields:
A representation of output features as a dict mapping keys (logical column
names) to values.
"""
if not hasattr(self, '_graph_state'):
# If available, acquire will return a cached _GraphStateCommon, since
# calling _make_graph_state is expensive.
self._graph_state = self._shared_graph_state_handle.acquire(
lambda: self._make_graph_state(saved_model_dir))
# This should remain true throughout the lifetime of this DoFn, regardless
# of whether or not self._graph_state was cached.
assert self._graph_state.saved_model_dir == saved_model_dir
yield self._handle_batch(batch)
def _warn_about_tf_compat_v1():
"""Warns about using tf.compat.v1."""
logging.warning(
'Tensorflow Transform is running in tf.compat.v1 mode. This could be '
'either because TF2 was disabled or `Context.force_tf_compat_v1=True`. '
'Features such as tf.function may not work as intended.')
def _maybe_slice_large_record_batch(
record_batch: pa.RecordBatch,
) -> Iterable[pa.RecordBatch]:
"""Slices large batches into smaller chunks."""
if record_batch.nbytes > _MAX_TRANSFORMED_BATCH_BYTES_SIZE:
if record_batch.num_rows < 2:
logging.warning(
'Transformed data row may be too large: %d bytes. '
'Consider reshaping outputs to distribute elements over a larger '
'number of rows to allow automatic slicing.',
record_batch.nbytes,
)
yield record_batch
return
# Note that slicing is a zero-copy operation, so the produced batches will
# still share memory with the original one up to the materialization
# boundary.
mid_point = record_batch.num_rows // 2
yield from _maybe_slice_large_record_batch(
record_batch.slice(offset=0, length=mid_point)
)
yield from _maybe_slice_large_record_batch(
record_batch.slice(offset=mid_point)
)
else:
yield record_batch
def _convert_to_record_batch(
batch_dict: Dict[str, Union[common_types.TensorValueType, pa.Array]],
converter: tensor_to_arrow.TensorsToRecordBatchConverter,
passthrough_keys: Set[str],
input_metadata: Union[
TensorAdapterConfig, dataset_metadata.DatasetMetadata
],
validate_varlen_sparse_values: bool = False,
) -> Iterable[Tuple[pa.RecordBatch, Dict[str, pa.Array]]]:
"""Convert batch of ndarrays to pyarrow.RecordBatches."""
# Making a copy of batch_dict because mutating PCollection elements is not
# allowed.
if passthrough_keys:
batch_dict = copy.copy(batch_dict)
passthrough_data = {
key: batch_dict.pop(key) for key in passthrough_keys if key in batch_dict
}
if validate_varlen_sparse_values:
for name, representation in converter.tensor_representations().items():
if representation.WhichOneof('kind') == 'varlen_sparse_tensor':
impl_helper.validate_varlen_sparse_value(name, batch_dict[name])
record_batch = converter.convert(batch_dict)
arrow_columns, arrow_schema = record_batch.columns, record_batch.schema
batch_size = len(arrow_columns[0])
# This dict will contain pass-through data with batch size of 1 if it doesn't
# match batch size of the transformed data.
unary_passthrough_features = {}
for key, data in passthrough_data.items():
# Only raising a ValueError in case pass-through data has more than one
# distinct value. If it has one value and batch_size>1 then it will have to
# be handled by the user.
# TODO(b/38376110): Restrict to matching batch dimensions and clean this up
# once the internal feature key is deprecated.
if len(data) not in (batch_size, 1):
# The passthrough column should be of list<primitive> type with each
# sub-list being either null or of length 1.
data_set = set(
None if elem is None else elem[0] for elem in data.to_pylist())
if len(data_set) == 1:
elem = data_set.pop()
data = pa.array([None if elem is None else [elem]], type=data.type)
else:
raise ValueError(
'Cannot pass-through data when input and output batch sizes '
'are different ({} vs. {})'.format(len(data), batch_size))
if len(data) == batch_size:
arrow_schema = arrow_schema.append(input_metadata.arrow_schema.field(key))
arrow_columns.append(data)
else:
unary_passthrough_features[key] = data
for reccord_batch in _maybe_slice_large_record_batch(
pa.RecordBatch.from_arrays(arrow_columns, schema=arrow_schema)
):
yield reccord_batch, unary_passthrough_features
def _transformed_batch_to_instance_dicts(
transformed_batch: Tuple[pa.RecordBatch, Dict[str, pa.Array]],
schema: schema_pb2.Schema,
):
"""Converts batch of transformed data to unbatched instance dicts."""
record_batch, unary_passthrough_features = transformed_batch
result = impl_helper.record_batch_to_instance_dicts(record_batch, schema)
# Convert unary passthrough data to Python primitives.
for key, value in unary_passthrough_features.items():
value = value.to_pylist()
value = None if value[0] is None else value[0]
for instance in result:
instance[key] = value
return result
@dataclasses.dataclass(frozen=True)
class _TensorBinding:
value: Any
tensor_name: str
dtype_enum: int
is_asset_filepath: bool
@beam_common.register_ptransform(beam_nodes.CreateTensorBinding)
@beam.typehints.with_input_types(common_types.InstanceValueType)
@beam.typehints.with_output_types(_TensorBinding)
class _CreateTensorBindingsImpl(beam.PTransform):
"""Maps a PCollection of data to a PCollection of `_TensorBinding`s."""
def __init__(self, operation, extra_args):
del extra_args
self._dtype_enum = operation.dtype_enum
self._tensor_name = operation.tensor_name
self._is_asset_file = operation.is_asset_filepath
def expand(self, inputs):
pcoll, = inputs
return pcoll | 'ToTensorBinding' >> beam.Map(
_TensorBinding, self._tensor_name, self._dtype_enum,
self._is_asset_file)
def _get_tensor_replacement_map(graph, *tensor_bindings):
"""Get Tensor replacement map."""
tensor_replacement_map = {}
for tensor_binding in tensor_bindings:
assert isinstance(tensor_binding, _TensorBinding), tensor_binding
replacement_tensor = tf.constant(
tensor_binding.value, tf.dtypes.as_dtype(tensor_binding.dtype_enum))
if graph is not None and tensor_binding.is_asset_filepath:
graph.add_to_collection(tf.compat.v1.GraphKeys.ASSET_FILEPATHS,
replacement_tensor)
tensor_replacement_map[tensor_binding.tensor_name] = replacement_tensor
return tensor_replacement_map
def _replace_tensors_with_constant_values(saved_model_dir, base_temp_dir,
*tensor_bindings):
"""Replaces specified `Tensor`s with constant values.
Constants are accepted as Python values; these are automatically
wrapped in `tf.constant()`.
This method creates its own temp dir, and is therefore idempotent
since any retry will use a different temp dir.
Args:
saved_model_dir: A SavedModel directory providing a transform
graph. The MetaGraphDef and signature are selected from the
SavedModel using keys defined in `../constants.py` ('transform'
and 'transform_signature', respectively).
base_temp_dir: Base temp dir for storage of new model.
*tensor_bindings: An iterable of `_TensorBinding`s.
Returns:
The directory name containing the updated SavedModel.
Raises:
RuntimeError: if there is no default graph available to which to
apply the transform.
"""
with tf.compat.v1.Graph().as_default() as graph:
tensor_replacement_map = (
_get_tensor_replacement_map(graph, *tensor_bindings))
with tf.compat.v1.Session(graph=graph) as session:
temp_dir = beam_common.get_unique_temp_path(base_temp_dir)
input_tensors, output_tensors = (
saved_transform_io.partially_apply_saved_transform_internal(
saved_model_dir, {}, tensor_replacement_map))
session.run(tf.compat.v1.global_variables_initializer())
saved_transform_io.write_saved_transform_from_session(
session, input_tensors, output_tensors, temp_dir)
return temp_dir
@beam_common.register_ptransform(
beam_nodes.CreateSavedModel,
tags={beam_common.EnvironmentTags.TF_COMPAT_V1})
@beam.typehints.with_input_types(_TensorBinding)
@beam.typehints.with_output_types(_TransformFnPathType)
class _CreateSavedModelImpl(beam.PTransform):
"""Create a SavedModel from a TF Graph."""
def __init__(self, operation, extra_args):
self._base_temp_dir = extra_args.base_temp_dir
self._graph = extra_args.graph
self._input_signature = extra_args.input_signature
self._table_initializers = operation.table_initializers
self._output_signature = operation.output_signature
def expand(self, inputs):
unbound_saved_model_dir = beam_common.get_unique_temp_path(
self._base_temp_dir)
with self._graph.as_default():
with tf.compat.v1.Session(graph=self._graph) as session:
table_initializers_ref = tf.compat.v1.get_collection_ref(
tf.compat.v1.GraphKeys.TABLE_INITIALIZERS)
original_table_initializers = list(table_initializers_ref)
del table_initializers_ref[:]
table_initializers_ref.extend(self._table_initializers)
# Initialize all variables so they can be saved.
session.run(tf.compat.v1.global_variables_initializer())
saved_transform_io.write_saved_transform_from_session(
session, self._input_signature, self._output_signature,
unbound_saved_model_dir)
del table_initializers_ref[:]
table_initializers_ref.extend(original_table_initializers)
return (inputs
| 'BindTensors' >> _BindTensors(self._base_temp_dir,
unbound_saved_model_dir)
| 'Count' >>
beam_common.IncrementCounter(_CREATE_SAVED_MODEL_COUNTER_NAME))
def _create_v2_saved_model(tensor_replacement_map, base_temp_dir,
preprocessing_fn, input_signature,
baseline_analyzers_fingerprint,
output_keys_to_name_map, save_options):
"""Writes out a SavedModelV2 with preprocessing_fn traced using tf.function.
The SavedModel written contains a method called `transform_fn` that
represents the traced `preprocessing_fn`. Additionally, if this is the final
SavedModel being written out, it will contain a method called `metadata_fn`
that provides deferred schema annotations.
Args:
tensor_replacement_map: A map from placeholder tensor names to their
evaluated replacement tensors.
base_temp_dir: Base path to write SavedModel and temporary artifacts to.
preprocessing_fn: A user defined python function to be traced.
input_signature: TypeSpecs describing the inputs to the `preprocessing_fn`.
baseline_analyzers_fingerprint: A mapping from analyzer name to a set of
paths that define its fingerprint.
output_keys_to_name_map: A map from output dictionary keys to the names of
the tensors that they represent.
save_options: The tf.saved_model.SaveOptions to save the model with.
Returns:
Path to which SavedModel was written.
"""
saved_model_dir = beam_common.get_unique_temp_path(base_temp_dir)
impl_helper.trace_and_write_v2_saved_model(saved_model_dir, preprocessing_fn,
input_signature, base_temp_dir,
baseline_analyzers_fingerprint,
tensor_replacement_map,
output_keys_to_name_map,
save_options)
return saved_model_dir
@beam_common.register_ptransform(
beam_nodes.CreateSavedModel, tags={beam_common.EnvironmentTags.TF_V2_ONLY})
@beam.typehints.with_input_types(_TensorBinding)
@beam.typehints.with_output_types(str)
class _CreateSavedModelImplV2(beam.PTransform):
"""Create a SavedModel from a TF Graph."""
def __init__(self, operation, extra_args):
self._base_temp_dir = extra_args.base_temp_dir
self._preprocessing_fn = extra_args.preprocessing_fn
self._input_signature = extra_args.input_specs
self._output_signature = operation.output_signature
self._analyzers_fingerprint = extra_args.analyzers_fingerprint
self._save_options = extra_args.save_options
def _maybe_get_output_tensor_names_dict(self):
# output_signature will contain CompositeTensors only if this is the final
# SavedModel export. In this scenario, we do not need the output_signature
# anymore as we will output everything that the preprocessing_fn returns.
if all(isinstance(v, tf.Tensor) for v in self._output_signature.values()):
return {k: v.name for k, v in self._output_signature.items()}
else:
return {}
def expand(self, inputs):
pipeline = (inputs[0] if isinstance(inputs, tuple) else inputs).pipeline
input_pcoll = pipeline | 'CreateSole' >> beam.Create([None])
if not isinstance(inputs, beam.pvalue.PBegin):
input_pcoll |= ('ReplaceWithConstants' >> beam.Map(
lambda _, *args: _get_tensor_replacement_map(None, *args),
*[beam.pvalue.AsSingleton(pcoll) for pcoll in inputs]))
return (
input_pcoll
| 'CreateSavedModel' >> beam.Map(
_create_v2_saved_model, self._base_temp_dir, self._preprocessing_fn,
self._input_signature, self._analyzers_fingerprint,
self._maybe_get_output_tensor_names_dict(), self._save_options)
| 'Count' >>
beam_common.IncrementCounter(_CREATE_SAVED_MODEL_COUNTER_NAME))
class _BindTensors(beam.PTransform):
"""PTransform to bind tensor in a SavedModel."""
def __init__(self, base_temp_dir, unbound_saved_model_dir):
self._base_temp_dir = base_temp_dir
self._unbound_saved_model_dir = unbound_saved_model_dir
def expand(self, inputs):
pipeline = (inputs[0] if isinstance(inputs, tuple) else inputs).pipeline
saved_model_dir_pcoll = pipeline | 'CreateSavedModel' >> beam.Create(
[self._unbound_saved_model_dir])
if isinstance(inputs, beam.pvalue.PBegin):
return saved_model_dir_pcoll
return saved_model_dir_pcoll | 'ReplaceWithConstants' >> beam.Map(
_replace_tensors_with_constant_values, self._base_temp_dir,
*[beam.pvalue.AsSingleton(pcoll) for pcoll in inputs])
@beam_common.register_ptransform(beam_nodes.ExtractInputForSavedModel)
class _ExtractInputForSavedModelImpl(beam.PTransform):
"""Returns a PCollection for analysis based on the specified dataset_key."""
def __init__(self, operation, extra_args):
self._dataset_key = operation.dataset_key
self._flat_pcollection = extra_args.flat_pcollection
self._pcollection_dict = extra_args.pcollection_dict
def expand(self, pbegin):
# TODO(b/151921205): we have to do an identity map for unmodified
# PCollections below because otherwise we get an error from beam.
identity_map = 'Identity' >> beam.Map(lambda x: x)
if self._dataset_key.is_flattened_dataset_key():
if self._flat_pcollection:
return self._flat_pcollection | identity_map
else:
return (
list(self._pcollection_dict.values())
| 'FlattenAnalysisInputs' >> beam.Flatten(pipeline=pbegin.pipeline))
else:
return self._pcollection_dict[self._dataset_key] | identity_map
@beam_common.register_ptransform(beam_nodes.ApplySavedModel)
class _ApplySavedModelImpl(beam.PTransform):
"""PTransform to apply a SavedModel to data."""
def __init__(self, operation, extra_args):
self._use_tf_compat_v1 = extra_args.use_tf_compat_v1
self._input_tensor_adapter_config = extra_args.input_tensor_adapter_config
self._tf_config = extra_args.tf_config
self._phase = operation.phase
def expand(self, inputs):
saved_model_dir_pcol, input_values_pcol = inputs
# We don't deep_copy pcollections used for the first phase, or when
# the user defined `Context` disables it.
if self._phase > 0 and Context.get_use_deep_copy_optimization():
# Obviates unnecessary data materialization when the input data source is
# safe to read more than once.
logging.info('Deep copying inputs for phase: %d', self._phase)
input_values_pcol = deep_copy.deep_copy(input_values_pcol)
def _convert_to_numpy(input_dict):
"""Converts eager tensors to numpy arrays."""
return {
k: np.asarray(v) if isinstance(v, tf.Tensor) else v
for k, v in input_dict.items()
}
result = (
input_values_pcol | 'ApplySavedModel' >> beam.ParDo(
_RunMetaGraphDoFn(
self._tf_config,
use_tf_compat_v1=self._use_tf_compat_v1,
input_tensor_adapter_config=self._input_tensor_adapter_config,
shared_graph_state_handle=shared.Shared(),
passthrough_keys=Context.get_passthrough_keys()),
saved_model_dir=beam.pvalue.AsSingleton(saved_model_dir_pcol)))
if not self._use_tf_compat_v1:
result |= 'ConvertToNumpy' >> beam.Map(_convert_to_numpy)
return result
@beam_common.register_ptransform(beam_nodes.ExtractFromDict)
@beam.typehints.with_input_types(Dict[str,
Union[np.ndarray,
tf.compat.v1.SparseTensorValue]])
class _ExtractFromDictImpl(beam.PTransform):
"""Implements ExtractFromDict by extracting the configured keys."""
def __init__(self, operation, extra_args):
del extra_args
self._keys = operation.keys
def expand(self, inputs):
pcoll, = inputs
def extract_keys(input_dict, keys):
return (tuple(input_dict[k] for k in keys)
if isinstance(keys, tuple) else input_dict[keys])
if isinstance(self._keys, tuple):
output_type = Tuple[(Any,) * len(self._keys)]
else:
output_type = Any
return pcoll | 'ExtractKeys' >> beam.Map(
extract_keys, keys=self._keys).with_output_types(output_type)
@beam_common.register_ptransform(beam_nodes.Flatten)
class _Flatten(beam.PTransform):
"""PTransform to flatten PCollections."""
def __init__(self, operation, extra_args):
del operation, extra_args # unused
def expand(self, inputs):
return inputs | beam.Flatten()
def _infer_metadata_from_saved_model(
saved_model_dir: str,
use_tf_compat_v1: bool) -> dataset_metadata.DatasetMetadata:
"""Infers a DatasetMetadata for outputs of a SavedModel."""
if use_tf_compat_v1:
return _infer_metadata_from_saved_model_v1(saved_model_dir)
else:
return _infer_metadata_from_saved_model_v2(saved_model_dir)
def _infer_metadata_from_saved_model_v1(
saved_model_dir: str) -> dataset_metadata.DatasetMetadata:
"""Infers a DatasetMetadata for outputs of a TF1 SavedModel."""
with tf.compat.v1.Graph().as_default() as graph:
with tf.compat.v1.Session(graph=graph) as session:
_, outputs = (
saved_transform_io.partially_apply_saved_transform_internal(
saved_model_dir, {}))
session.run(tf.compat.v1.global_variables_initializer())
session.run(tf.compat.v1.tables_initializer())
return dataset_metadata.DatasetMetadata(
schema=schema_inference.infer_feature_schema(outputs, graph, session))
def _infer_metadata_from_saved_model_v2(
saved_model_dir: str) -> dataset_metadata.DatasetMetadata:
"""Infers a DatasetMetadata for outputs of a TF2 SavedModel."""
metadata_path = os.path.join(saved_model_dir, impl_helper.METADATA_DIR_NAME)
return metadata_io.read_metadata(metadata_path)
class _InstrumentAPI(beam.PTransform):
"""PTransform that adds metrics for API usage."""
def __init__(self, tf_graph, force_tf_compat_v1, use_tf_compat_v1):
def _get_counter_from_graph_collection(collection_name):
collection = tf_graph.get_collection(collection_name)
if len(collection) > 1:
raise ValueError(
"Expected TF graph collection '{}' to contain at most one element. "
'Encountered {}.'.format(collection_name, len(collection)))
return collection[0] if collection else {}
self._analyzer_use_counter = _get_counter_from_graph_collection(
common.ANALYZER_COLLECTION)
self._mapper_use_counter = _get_counter_from_graph_collection(
common.MAPPER_COLLECTION)
self._force_tf_compat_v1 = force_tf_compat_v1
self._use_tf_compat_v1 = use_tf_compat_v1
def expand(self, pipeline):
def _make_and_increment_counters(unused_element, analyzer_counter,
mapper_counter, force_tf_compat_v1,
use_tf_compat_v1):
del unused_element
beam.metrics.Metrics.counter(beam_common.METRICS_NAMESPACE,
'requested_tf_compat_v1').inc(
int(force_tf_compat_v1))
beam.metrics.Metrics.counter(beam_common.METRICS_NAMESPACE,
'running_tf_compat_v1').inc(
int(use_tf_compat_v1))
for counter_prefix, counter in (('tft_analyzer_{}', analyzer_counter),
('tft_mapper_{}', mapper_counter)):
for name, count in counter.items():
beam.metrics.Metrics.counter(beam_common.METRICS_NAMESPACE,
counter_prefix.format(name)).inc(count)
_ = (
pipeline
| 'CreateSoleAPIUse' >> beam.Create([None])
| 'CountAPIUse' >>
beam.Map(_make_and_increment_counters, self._analyzer_use_counter,
self._mapper_use_counter, self._force_tf_compat_v1,
self._use_tf_compat_v1))
@beam.typehints.with_input_types(common_types.InstanceDictType)
@beam.typehints.with_output_types(pa.RecordBatch)
class _InstanceDictInputToTFXIOInput(beam.PTransform):
"""PTransform that turns instance dicts into RecordBatches."""
def __init__(self, schema, desired_batch_size):
self._schema = schema
self._tfxio = tf_example_record.TFExampleBeamRecord(
physical_format='inmem',
telemetry_descriptors=['StandaloneTFTransform'],
schema=schema)
self._desired_batch_size = desired_batch_size
def tensor_adapter_config(self):
return self._tfxio.TensorAdapterConfig()
def expand(self, instance_dict_pcoll):
return (
instance_dict_pcoll
| 'EncodeInstanceDictsAsTfExample' >> beam.Map(
example_proto_coder.ExampleProtoCoder(self._schema).encode)
| 'TfExampleToRecordBatch' >> self._tfxio.BeamSource(
batch_size=self._desired_batch_size))
def _make_output_cache(
cache_value_nodes: Optional[analysis_graph_builder.AnalysisCache],
traverser: nodes.Traverser,
dataset_metrics: Dict[
analyzer_cache.DatasetKey, analyzer_cache.DatasetCacheMetadata
],
) -> Optional[analyzer_cache.BeamAnalysisCache]:
"""Triggers dataset cache encoding and composes analysis cache output."""
if cache_value_nodes is None:
return None
cache_dict = collections.defaultdict(dict)
for dataset_key, dataset_cache in cache_value_nodes.items():
for cache_key, value_node in dataset_cache.items():
cache_dict[dataset_key][cache_key] = traverser.visit_value_node(
value_node
)
return {
dataset_key: analyzer_cache.DatasetCache(cache,
dataset_metrics[dataset_key])
for dataset_key, cache in cache_dict.items()
}
class _AnalyzeDatasetCommon(beam.PTransform):
"""Common implementation for AnalyzeDataset, with or without cache."""
def __init__(self, preprocessing_fn, pipeline=None):
"""Init method.
Args:
preprocessing_fn: A function that accepts and returns a dictionary from
strings to `Tensor`s, `SparseTensor`s, or `RaggedTensor`s.
pipeline: (Optional) a beam Pipeline.
"""
self._preprocessing_fn = preprocessing_fn
self.pipeline = pipeline
self._save_options = Context.get_save_options()
self._use_tf_compat_v1 = Context.get_use_tf_compat_v1()
if self._use_tf_compat_v1:
_warn_about_tf_compat_v1()
def _extract_input_pvalues(self, dataset):
# This method returns all nested pvalues to inform beam of nested pvalues.