|
26 | 26 | import tempfile |
27 | 27 |
|
28 | 28 | from tensorflow.python.data.ops.dataset_ops import Dataset |
| 29 | +try: |
| 30 | + from tensorflow.python.data.ops.dataset_ops import AUTOTUNE |
| 31 | +except ImportError: |
| 32 | + from tensorflow.python.data.experimental.ops.optimization import AUTOTUNE |
29 | 33 | from tensorflow.python.framework import dtypes |
30 | 34 | from tensorflow.python.framework import errors |
31 | 35 | from tensorflow.python.framework import ops |
@@ -129,6 +133,56 @@ def gen_filenames(): |
129 | 133 | with self.assertRaises(errors.OutOfRangeError): |
130 | 134 | sess.run(batch) |
131 | 135 |
|
| 136 | + def test_read_from_generator_parallel(self): |
| 137 | + num_epochs = 2 |
| 138 | + batch_size = 100 |
| 139 | + with ops.Graph().as_default() as graph: |
| 140 | + def gen_filenames(): |
| 141 | + for i in range(num_epochs + 1): |
| 142 | + if i == num_epochs: |
| 143 | + return # raise StopIteration |
| 144 | + yield self._filename |
| 145 | + filenames = Dataset.from_generator( |
| 146 | + gen_filenames, dtypes.string, tensor_shape.TensorShape([])) |
| 147 | + fields = [ |
| 148 | + DataFrame.Field('A', dtypes.int64), |
| 149 | + DataFrame.Field('C', dtypes.int64)] |
| 150 | + ds = filenames.apply( |
| 151 | + read_parquet(batch_size, fields=fields, num_parallel_reads=3)) |
| 152 | + ds = ds.prefetch(4) |
| 153 | + batch = make_one_shot_iterator(ds).get_next() |
| 154 | + |
| 155 | + with self.test_session(use_gpu=False, graph=graph) as sess: |
| 156 | + for _ in range(len(self._df) * num_epochs // batch_size): |
| 157 | + sess.run(batch) |
| 158 | + with self.assertRaises(errors.OutOfRangeError): |
| 159 | + sess.run(batch) |
| 160 | + |
| 161 | + def test_read_from_generator_parallel_auto(self): |
| 162 | + num_epochs = 2 |
| 163 | + batch_size = 100 |
| 164 | + with ops.Graph().as_default() as graph: |
| 165 | + def gen_filenames(): |
| 166 | + for i in range(num_epochs + 1): |
| 167 | + if i == num_epochs: |
| 168 | + return # raise StopIteration |
| 169 | + yield self._filename |
| 170 | + filenames = Dataset.from_generator( |
| 171 | + gen_filenames, dtypes.string, tensor_shape.TensorShape([])) |
| 172 | + fields = [ |
| 173 | + DataFrame.Field('A', dtypes.int64), |
| 174 | + DataFrame.Field('C', dtypes.int64)] |
| 175 | + ds = filenames.apply( |
| 176 | + read_parquet(batch_size, fields=fields, num_parallel_reads=AUTOTUNE)) |
| 177 | + ds = ds.prefetch(4) |
| 178 | + batch = make_one_shot_iterator(ds).get_next() |
| 179 | + |
| 180 | + with self.test_session(use_gpu=False, graph=graph) as sess: |
| 181 | + for _ in range(len(self._df) * num_epochs // batch_size): |
| 182 | + sess.run(batch) |
| 183 | + with self.assertRaises(errors.OutOfRangeError): |
| 184 | + sess.run(batch) |
| 185 | + |
132 | 186 |
|
133 | 187 | if __name__ == '__main__': |
134 | 188 | os.environ['CUDA_VISIBLE_DEVICES'] = '' |
|
0 commit comments