-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplacement_test.py
294 lines (241 loc) · 10.3 KB
/
placement_test.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
# Copyright 2020 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.
# ==============================================================================
"""Tests for tf.data placement within tf.functions."""
from absl.testing import parameterized
from tensorflow.python.data.experimental.ops import prefetching_ops
from tensorflow.python.data.kernel_tests import test_base
from tensorflow.python.data.ops import dataset_ops
from tensorflow.python.eager import def_function
from tensorflow.python.framework import combinations
from tensorflow.python.framework import config
from tensorflow.python.framework import constant_op
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import ops
from tensorflow.python.framework import test_ops
from tensorflow.python.framework import test_util
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import cond
from tensorflow.python.ops import gen_dataset_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.platform import test
class PlacementTest(test_base.DatasetTestBase, parameterized.TestCase):
"""Tests for tf.data placement within tf.functions.
Specifically, tf.data dataset tensors cannot be copied between devices. These
tests verify the ops are placed in a way that avoids this.
"""
def setUp(self):
super(PlacementTest, self).setUp()
# Grappler optimizations can affect whether the placement issues occur,
# since they may inadvertently rewrite nodes and edges in a way that removes
# cross-device copies.
config.set_optimizer_experimental_options({"disable_meta_optimizer": True})
@combinations.generate(test_base.eager_only_combinations())
def testWhileWithCapturedDataset(self):
dataset = dataset_ops.Dataset.range(10)
@def_function.function
def f():
total = constant_op.constant(0, dtypes.int64)
for _ in math_ops.range(1):
for elem in dataset:
total += elem
return total
self.assertEqual(f().numpy(), 45)
@combinations.generate(test_base.eager_only_combinations())
def testWhile(self):
@def_function.function
def f():
dataset = dataset_ops.Dataset.range(10)
total = constant_op.constant(0, dtypes.int64)
for _ in math_ops.range(1):
for elem in dataset:
total += elem
return total
self.assertEqual(f().numpy(), 45)
@combinations.generate(test_base.eager_only_combinations())
def testCondWithPlacement(self):
# When the cond op is explicitly placed, there shouldn't be cross-device
# copies.
@def_function.function
def f():
dataset = dataset_ops.Dataset.range(10)
def fn():
return dataset.map(lambda x: x+1)
c = constant_op.constant(2)
with ops.device("/cpu:0"):
a = cond.cond(math_ops.equal(c, 2), fn, fn)
iterator = iter(a)
nxt = next(iterator)
return nxt
self.assertEqual(f().numpy(), 1)
@combinations.generate(test_base.eager_only_combinations())
def testCondWithColocation(self):
# When the cond op is colocated with the dataset, there shouldn't be
# cross-device copies.
@def_function.function
def f():
dataset = dataset_ops.Dataset.range(8)
def fn():
return dataset.map(lambda x: x+1)
c = constant_op.constant(2)
with ops.colocate_with(dataset._variant_tensor): # pylint:disable=protected-access
a = cond.cond(math_ops.equal(c, 2), fn, fn)
iterator = iter(a)
nxt = next(iterator)
return nxt
self.assertEqual(f().numpy(), 1)
@combinations.generate(test_base.eager_only_combinations())
def testCond(self):
@def_function.function
def f():
dataset = dataset_ops.Dataset.range(8)
c = constant_op.constant(2)
a = cond.cond(
math_ops.equal(c, 2),
lambda: dataset.map(lambda x: x + 1),
lambda: dataset.map(lambda x: x + 2),
)
return next(iter(a))
self.assertEqual(f().numpy(), 1)
@combinations.generate(test_base.eager_only_combinations())
def testId(self):
# Ideally, placer should know that Identity(dataset) should be on the same
# device as the dataset.
@def_function.function
def f():
dataset = dataset_ops.Dataset.range(10)
dataset = array_ops.identity(dataset)
return dataset
f()
@combinations.generate(test_base.default_test_combinations())
@test_util.run_gpu_only
def testFunctionCall(self):
# Ideally, placer should know that Call(dataset) should be on the same
# device as the dataset. Create a funciton that could be place don the GPU,
# but a Dataset that cannot.
@def_function.function
def test_call(dataset):
return dataset.reduce(0, lambda s, _: s + 1)
@def_function.function
def f():
dataset = dataset_ops.Dataset.range(10)
return test_call(dataset)
self.assertEqual(self.evaluate(f()), 10)
@combinations.generate(test_base.eager_only_combinations())
@test_util.run_gpu_only
def testIteratorOnDeviceEagerMode(self):
dataset = dataset_ops.Dataset.range(10)
dataset = dataset.apply(prefetching_ops.prefetch_to_device("/gpu:0"))
iterator = iter(dataset)
data = next(iterator)
optional_data = iterator.get_next_as_optional()
self.assertIn("gpu:0", dataset._variant_tensor.device.lower())
self.assertIn("gpu:0", iterator._iterator_resource.device.lower())
self.assertIn("gpu:0", data.device.lower())
self.assertIn("gpu:0", optional_data.get_value().device.lower())
self.assertIn("gpu:0", optional_data.has_value().device.lower())
# There are HostMemory constraints on AnonymousIteratorV2 and
# DeleteIterator kernels on TPU but not on GPU. This is intentional because
# when running AnonymousIteratorV2 in a function
#
# - If the op is placed on GPU, the variant _Retval is placed on GPU.
# - However, if the op is placed on TPU, the variant _Retval is placed on
# CPU.
#
# So if were to add HostMemory constraints to the GPU kernels it would lead
# to variant device copy errors.
#
# TODO(b/204231062): Unify behavior across GPU and TPU.
@combinations.generate(test_base.eager_only_combinations())
@test_util.run_gpu_only
def testCreateIteratorInFuncOnGpu(self):
@def_function.function
def create_iter():
return gen_dataset_ops.anonymous_iterator_v2(
output_types=[dtypes.float32], output_shapes=[[]])
create_iter()
@combinations.generate(test_base.graph_only_combinations())
@test_util.run_gpu_only()
def testIteratorOnDeviceGraphModeOneShotIterator(self):
self.skipTest("TODO(b/169429285): tf.data.Dataset.make_one_shot_iterator "
"does not support GPU placement.")
dataset = dataset_ops.Dataset.range(10)
dataset = dataset.apply(prefetching_ops.prefetch_to_device("/gpu:0"))
iterator = dataset_ops.make_one_shot_iterator(dataset)
data = iterator.get_next()
optional_data = iterator.get_next_as_optional()
with ops.colocate_with(dataset._variant_tensor):
dataset_device = test_ops.device_placement_op()
self.assertIn(b"GPU:0", self.evaluate(dataset_device))
with ops.colocate_with(iterator._iterator_resource):
iterator_device = test_ops.device_placement_op()
self.assertIn(b"GPU:0", self.evaluate(iterator_device))
with ops.colocate_with(data):
data_device = test_ops.device_placement_op()
self.assertIn(b"GPU:0", self.evaluate(data_device))
with ops.colocate_with(optional_data.get_value()):
get_value_device = test_ops.device_placement_op()
self.assertIn(b"GPU:0", self.evaluate(get_value_device))
with ops.colocate_with(optional_data.has_value()):
has_value_device = test_ops.device_placement_op()
self.assertIn(b"GPU:0", self.evaluate(has_value_device))
@combinations.generate(test_base.graph_only_combinations())
@test_util.run_gpu_only()
def testIteratorOnDeviceGraphModeInitializableIterator(self):
dataset = dataset_ops.Dataset.range(10)
dataset = dataset.apply(prefetching_ops.prefetch_to_device("/gpu:0"))
iterator = dataset_ops.make_initializable_iterator(dataset)
data = iterator.get_next()
optional_data = iterator.get_next_as_optional()
with ops.colocate_with(dataset._variant_tensor):
dataset_device = test_ops.device_placement_op()
self.assertIn(b"GPU:0", self.evaluate(dataset_device))
with ops.colocate_with(iterator._iterator_resource):
iterator_device = test_ops.device_placement_op()
self.assertIn(b"GPU:0", self.evaluate(iterator_device))
with ops.colocate_with(data):
data_device = test_ops.device_placement_op()
self.assertIn(b"GPU:0", self.evaluate(data_device))
with ops.colocate_with(optional_data.get_value()):
get_value_device = test_ops.device_placement_op()
self.assertIn(b"GPU:0", self.evaluate(get_value_device))
with ops.colocate_with(optional_data.has_value()):
has_value_device = test_ops.device_placement_op()
self.assertIn(b"GPU:0", self.evaluate(has_value_device))
@combinations.generate(test_base.eager_only_combinations())
@test_util.run_gpu_only()
def testIterDatasetEagerModeWithExplicitDevice(self):
@def_function.function
def comp():
value = constant_op.constant(0, dtype=dtypes.int64)
for d in iter(dataset_ops.Dataset.range(10)):
value += d
return value
with ops.device("/gpu:0"):
result = comp()
self.assertEqual(result.numpy(), 45)
@combinations.generate(test_base.eager_only_combinations())
@test_util.run_gpu_only()
def testFunctionInliningColocation(self):
@def_function.function
def f(ds):
return next(iter(ds))
@def_function.function
def g():
dataset = dataset_ops.Dataset.range(10)
return f(dataset)
with ops.device("/gpu:0"):
self.assertEqual(self.evaluate(g()), 0)
if __name__ == "__main__":
test.main()