|
| 1 | +# coding=utf-8 |
| 2 | +# |
| 3 | +# Copyright 2023 Google Inc. All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +"""Tests for tft annotators.""" |
| 17 | + |
| 18 | +import tensorflow as tf |
| 19 | +import tensorflow_transform as tft |
| 20 | +from tensorflow_transform.beam import tft_unit |
| 21 | +from google.protobuf import text_format |
| 22 | +from tensorflow_metadata.proto.v0 import schema_pb2 |
| 23 | + |
| 24 | + |
| 25 | +_TF_VERSION_NAMED_PARAMETERS = [ |
| 26 | + dict(testcase_name='CompatV1', use_tf_compat_v1=True), |
| 27 | + dict(testcase_name='V2', use_tf_compat_v1=False), |
| 28 | +] |
| 29 | + |
| 30 | + |
| 31 | +class AnnotatorsTest(tft_unit.TransformTestCase): |
| 32 | + |
| 33 | + @tft_unit.named_parameters(*_TF_VERSION_NAMED_PARAMETERS) |
| 34 | + def test_annotate_sparse_outputs(self, use_tf_compat_v1): |
| 35 | + def preprocessing_fn(inputs): |
| 36 | + outputs = inputs.copy() |
| 37 | + x = tf.sparse.expand_dims(inputs['x'], -1) |
| 38 | + outputs['x'] = x |
| 39 | + tft.experimental.annotate_sparse_output_shape(x, [1, 1]) |
| 40 | + tft.experimental.annotate_sparse_output_shape(outputs['y'], [17]) |
| 41 | + tft.experimental.annotate_true_sparse_output(outputs['z']) |
| 42 | + return outputs |
| 43 | + |
| 44 | + input_data_dicts = [dict(x=[1], y=[2], z=[3], t=[4]) for x in range(10)] |
| 45 | + input_metadata = tft.DatasetMetadata.from_feature_spec({ |
| 46 | + 'x': tf.io.VarLenFeature(tf.int64), |
| 47 | + 'y': tf.io.VarLenFeature(tf.int64), |
| 48 | + 'z': tf.io.VarLenFeature(tf.int64), |
| 49 | + 't': tf.io.VarLenFeature(tf.int64), |
| 50 | + }) |
| 51 | + schema = text_format.Parse( |
| 52 | + """ |
| 53 | + feature { |
| 54 | + name: "t" |
| 55 | + type: INT |
| 56 | + } |
| 57 | + feature { |
| 58 | + name: "x$sparse_indices_0" |
| 59 | + type: INT |
| 60 | + int_domain { |
| 61 | + min: 0 |
| 62 | + max: 0 |
| 63 | + } |
| 64 | + } |
| 65 | + feature { |
| 66 | + name: "x$sparse_indices_1" |
| 67 | + type: INT |
| 68 | + int_domain { |
| 69 | + min: 0 |
| 70 | + max: 0 |
| 71 | + } |
| 72 | + } |
| 73 | + feature { |
| 74 | + name: "x$sparse_values" |
| 75 | + type: INT |
| 76 | + } |
| 77 | + feature { |
| 78 | + name: "y$sparse_indices_0" |
| 79 | + type: INT |
| 80 | + int_domain { |
| 81 | + min: 0 |
| 82 | + max: 16 |
| 83 | + } |
| 84 | + } |
| 85 | + feature { |
| 86 | + name: "y$sparse_values" |
| 87 | + type: INT |
| 88 | + } |
| 89 | + feature { |
| 90 | + name: "z$sparse_indices_0" |
| 91 | + type: INT |
| 92 | + } |
| 93 | + feature { |
| 94 | + name: "z$sparse_values" |
| 95 | + type: INT |
| 96 | + } |
| 97 | + sparse_feature { |
| 98 | + name: "x" |
| 99 | + index_feature { |
| 100 | + name: "x$sparse_indices_0" |
| 101 | + } |
| 102 | + index_feature { |
| 103 | + name: "x$sparse_indices_1" |
| 104 | + } |
| 105 | + is_sorted: true |
| 106 | + value_feature { |
| 107 | + name: "x$sparse_values" |
| 108 | + } |
| 109 | + } |
| 110 | + sparse_feature { |
| 111 | + name: "y" |
| 112 | + index_feature { |
| 113 | + name: "y$sparse_indices_0" |
| 114 | + } |
| 115 | + is_sorted: true |
| 116 | + value_feature { |
| 117 | + name: "y$sparse_values" |
| 118 | + } |
| 119 | + } |
| 120 | + sparse_feature { |
| 121 | + name: "z" |
| 122 | + index_feature { |
| 123 | + name: "z$sparse_indices_0" |
| 124 | + } |
| 125 | + is_sorted: true |
| 126 | + value_feature { |
| 127 | + name: "z$sparse_values" |
| 128 | + } |
| 129 | + } |
| 130 | + """, |
| 131 | + schema_pb2.Schema(), |
| 132 | + ) |
| 133 | + if not tft_unit.is_external_environment(): |
| 134 | + schema.generate_legacy_feature_spec = False |
| 135 | + self.assertAnalyzeAndTransformResults( |
| 136 | + input_data_dicts, |
| 137 | + input_metadata, |
| 138 | + preprocessing_fn, |
| 139 | + expected_metadata=tft.DatasetMetadata(schema), |
| 140 | + force_tf_compat_v1=use_tf_compat_v1, |
| 141 | + output_record_batches=True, |
| 142 | + ) |
| 143 | + |
| 144 | + @tft_unit.named_parameters(*_TF_VERSION_NAMED_PARAMETERS) |
| 145 | + def test_conflicting_sparse_outputs_annotations(self, use_tf_compat_v1): |
| 146 | + def preprocessing_fn(inputs): |
| 147 | + tft.experimental.annotate_sparse_output_shape(inputs['x'], [3]) |
| 148 | + tft.experimental.annotate_sparse_output_shape(inputs['x'], [17]) |
| 149 | + tft.experimental.annotate_true_sparse_output(inputs['x']) |
| 150 | + return inputs |
| 151 | + |
| 152 | + input_data_dicts = [dict(x=[1]) for x in range(10)] |
| 153 | + input_metadata = tft.DatasetMetadata.from_feature_spec( |
| 154 | + { |
| 155 | + 'x': tf.io.VarLenFeature(tf.int64), |
| 156 | + } |
| 157 | + ) |
| 158 | + schema = text_format.Parse( |
| 159 | + """ |
| 160 | + feature { |
| 161 | + name: "x$sparse_indices_0" |
| 162 | + type: INT |
| 163 | + int_domain { |
| 164 | + min: 0 |
| 165 | + max: 16 |
| 166 | + } |
| 167 | + } |
| 168 | + feature { |
| 169 | + name: "x$sparse_values" |
| 170 | + type: INT |
| 171 | + } |
| 172 | + sparse_feature { |
| 173 | + name: "x" |
| 174 | + index_feature { |
| 175 | + name: "x$sparse_indices_0" |
| 176 | + } |
| 177 | + is_sorted: true |
| 178 | + value_feature { |
| 179 | + name: "x$sparse_values" |
| 180 | + } |
| 181 | + } |
| 182 | + """, |
| 183 | + schema_pb2.Schema(), |
| 184 | + ) |
| 185 | + if not tft_unit.is_external_environment(): |
| 186 | + schema.generate_legacy_feature_spec = False |
| 187 | + self.assertAnalyzeAndTransformResults( |
| 188 | + input_data_dicts, |
| 189 | + input_metadata, |
| 190 | + preprocessing_fn, |
| 191 | + expected_metadata=tft.DatasetMetadata(schema), |
| 192 | + force_tf_compat_v1=use_tf_compat_v1, |
| 193 | + output_record_batches=True, |
| 194 | + ) |
| 195 | + |
| 196 | + @tft_unit.named_parameters(*_TF_VERSION_NAMED_PARAMETERS) |
| 197 | + def test_invalid_sparse_outputs_annotations(self, use_tf_compat_v1): |
| 198 | + def preprocessing_fn(inputs): |
| 199 | + tft.experimental.annotate_sparse_output_shape(inputs['x'], [3, 42]) |
| 200 | + return inputs |
| 201 | + |
| 202 | + input_data_dicts = [dict(x=[1]) for x in range(10)] |
| 203 | + input_metadata = tft.DatasetMetadata.from_feature_spec( |
| 204 | + { |
| 205 | + 'x': tf.io.VarLenFeature(tf.int64), |
| 206 | + } |
| 207 | + ) |
| 208 | + with self.assertRaisesRegex( # pylint: disable=g-error-prone-assert-raises |
| 209 | + ValueError, |
| 210 | + r'Annotated shape \[3, 42\] was expected to have rank 1', |
| 211 | + ): |
| 212 | + self.assertAnalyzeAndTransformResults( |
| 213 | + input_data_dicts, |
| 214 | + input_metadata, |
| 215 | + preprocessing_fn, |
| 216 | + force_tf_compat_v1=use_tf_compat_v1, |
| 217 | + ) |
| 218 | + |
| 219 | + |
| 220 | +if __name__ == '__main__': |
| 221 | + tft_unit.main() |
0 commit comments