-
Notifications
You must be signed in to change notification settings - Fork 75.3k
Expand file tree
/
Copy pathcache_test.py
More file actions
330 lines (276 loc) · 11.2 KB
/
cache_test.py
File metadata and controls
330 lines (276 loc) · 11.2 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
# Copyright 2023 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 DTensor device cache for compiled function computation."""
import gc
import numpy as np
# pylint: disable=g-direct-tensorflow-import
from tensorflow.dtensor.python import api
from tensorflow.dtensor.python import d_variable
from tensorflow.dtensor.python import layout as layout_lib
from tensorflow.dtensor.python.tests import test_util
from tensorflow.python.eager.polymorphic_function import polymorphic_function
from tensorflow.python.framework import combinations
from tensorflow.python.framework import constant_op
from tensorflow.python.framework import dtypes
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import gen_stateless_random_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.platform import test
# Convenient constants to use for tests.
_BATCH_DIM = "batch"
_MESH_DIM_X = "x"
# Shorter notation.
Layout = layout_lib.Layout
Mesh = layout_lib.Mesh
def diff_dicts(dict1, dict2):
keys = set(dict1.keys()) | set(dict2.keys())
return {key: dict1.get(key, 0) - dict2.get(key, 0) for key in keys}
class DTensorDeviceCacheTest(test_util.DTensorBaseTest):
def setUp(self):
super(DTensorDeviceCacheTest, self).setUp()
device_ids = test_util.create_device_ids_array((2,))
local_device_ids = np.ravel(device_ids).tolist()
mesh_dict = {
device: Mesh(
[_BATCH_DIM],
device_ids,
local_device_ids,
test_util.create_device_list((2,), device),
)
for device in ("CPU", "GPU", "TPU")
}
self.mesh = self.configTestMesh(mesh_dict)
def testBasic(self):
@polymorphic_function.function
def func0(a):
return a + 1
@polymorphic_function.function
def func1(a):
return a + 2
c0 = api.copy_to_mesh(
constant_op.constant(1.0), Layout.replicated(self.mesh, rank=0)
)
c1 = api.copy_to_mesh(
constant_op.constant([2.0, 3.0]), Layout.replicated(self.mesh, rank=1)
)
c2 = api.copy_to_mesh(
constant_op.constant([4.0]), Layout.replicated(self.mesh, rank=1)
)
c3 = api.copy_to_mesh(
constant_op.constant(1, dtype=dtypes.int32),
Layout.replicated(self.mesh, rank=0),
)
# c0 and c1 have different layouts. c1 and c2 have different shapes.
# c0 and c3 have different dtypes.
self.assertAllEqual(func0(c0), 2.0)
self.assertAllEqual(func0(c1), [3.0, 4.0])
self.assertAllEqual(func0(c2), [5.0])
self.assertAllEqual(func0(c3), 2)
# func0 and func1 have different names.
self.assertAllEqual(func1(c0), 3.0)
def testFunctionInputConstantFoldingCacheHits(self):
@polymorphic_function.function
def add(a, b):
return a + b
c0 = api.copy_to_mesh(
constant_op.constant(17.0), Layout.replicated(self.mesh, rank=0)
)
c1 = api.copy_to_mesh(
constant_op.constant(21.0), Layout.replicated(self.mesh, rank=0)
)
stats1 = api._dtensor_device()._get_stats()
self.assertAllEqual(add(c0, c1), 38.0)
self.assertAllEqual(add(c0, c1), 38.0)
# First call should miss and second should hit.
stats2 = api._dtensor_device()._get_stats()
diff = {key: stats2[key] - stats1[key] for key in stats1.keys()}
self.assertEqual(diff["function_manager.miss"], 1)
self.assertEqual(diff["function_manager.hit"], 1)
def testFunctionInputConstantFoldingCacheMiss(self):
@polymorphic_function.function
def add(a, b):
return a + b
c0 = api.copy_to_mesh(
constant_op.constant(17.0), Layout.replicated(self.mesh, rank=0)
)
c1 = api.copy_to_mesh(
constant_op.constant(21.0), Layout.replicated(self.mesh, rank=0)
)
c2 = api.copy_to_mesh(
constant_op.constant(0.0), Layout.replicated(self.mesh, rank=0)
)
stats1 = api._dtensor_device()._get_stats()
# First call should log a cache miss.
self.assertAllEqual(add(c0, c1), 38.0)
# Second call should also log a cache miss since second constant changed.
self.assertAllEqual(add(c0, c2), 17.0)
# Third call should not log a cache miss since the same input as the prev.
self.assertAllEqual(add(c0, c2), 17.0)
# Fourth call should log a cache miss since first input changed.
self.assertAllEqual(add(c1, c2), 21.0)
stats2 = api._dtensor_device()._get_stats()
diff = {key: stats2[key] - stats1[key] for key in stats1.keys()}
self.assertEqual(diff["function_manager.miss"], 3)
self.assertEqual(diff["function_manager.hit"], 1)
def testCacheWithRNG(self):
with api._dtensor_device()._default_layout(
Layout.replicated(self.mesh, rank=1)):
v0 = gen_stateless_random_ops.stateless_random_normal(
shape=[1], seed=[1, 2]
)
with api._dtensor_device()._default_layout(
Layout.replicated(self.mesh, rank=1)):
v1 = gen_stateless_random_ops.stateless_random_normal(
shape=[1], seed=[1, 2]
)
v2 = gen_stateless_random_ops.stateless_random_normal(
shape=[2], seed=[1, 2]
)
v3 = gen_stateless_random_ops.stateless_random_normal(
shape=[1], seed=[3, 4]
)
# v0 and v1 have same layouts.
self.assertAllEqual(v0, v1)
api.check_layout(v0, Layout.replicated(self.mesh, rank=1))
api.check_layout(v1, Layout.replicated(self.mesh, rank=1))
# v1 and v2 have different shapes.
self.assertNotEqual(v1.shape, v2.shape)
# v1 and v3 have different seeds.
self.assertNotEqual(v1.numpy(), v3.numpy())
def testCacheWithVariable(self):
c0 = api.copy_to_mesh(
constant_op.constant(1.0), Layout.replicated(self.mesh, rank=0)
)
c1 = api.copy_to_mesh(
constant_op.constant([2.0, 3.0]), Layout.replicated(self.mesh, rank=1)
)
a = constant_op.constant([4.0])
b = constant_op.constant([5.0])
c2 = api.pack(
[a, b], layout=Layout.batch_sharded(self.mesh, _BATCH_DIM, rank=1)
)
v0 = d_variable.DVariable(c0)
v1 = d_variable.DVariable(c1)
v2 = d_variable.DVariable(c2)
self.assertAllEqual(v0.read_value(), 1.0)
self.assertAllEqual(v1.read_value(), [2.0, 3.0])
unpacked_tensor = api.unpack(v2.read_value())
self.assertAllClose([4.0], unpacked_tensor[0])
self.assertAllClose([5.0], unpacked_tensor[1])
@combinations.generate(
combinations.combine(size=[16, 40], same_value=[True, False])
)
def testManyFunctions(self, size, same_value):
r = range(100)
values = [np.reshape(r[i : i + size], (4, size // 4)) for i in range(10)]
c_layout = Layout.replicated(self.mesh, rank=2)
values = [constant_op.constant(v, dtype=dtypes.float32) for v in values]
c0 = [api.copy_to_mesh(v, c_layout) for v in values]
c0 = [c0[0 if same_value else i] for i in range(10)]
e0 = [values[0 if same_value else i] for i in range(10)]
stats1 = api._dtensor_device()._get_stats()
for i in range(10):
# Use a special to ensure no conflicts with otherwise used names.
@polymorphic_function.function
def fn_31415926(c):
return math_ops.reduce_sum(c)
self.assertAllEqual(fn_31415926(c0[i]).numpy(), np.sum(e0[i]))
del fn_31415926
gc.collect()
stats2 = api._dtensor_device()._get_stats()
diff = diff_dicts(stats2, stats1)
self.assertEqual(diff["function_manager.size"], 0)
self.assertEqual(diff["kernel_cache.size"], 0)
self.assertEqual(diff["device_cache.size"], 0)
@combinations.generate(
combinations.combine(size=[16, 40], same_value=[True, False])
)
def testManyEagerOps(self, size, same_value):
if self.mesh.device_type() != "TPU":
# For the CPU/GPU mesh, we have a shortcut that doesn't go through the
# MLIR, but run the eager op locally and broadcast to all the devices.
expected_cache_diff = 0
expected_kernel_cache = 0
expected_device_cache = 0
expected_eager_pure_hit = 10
else:
# TODO(b/287529295): Remove this branch after the TPU issue is fixed.
expected_device_cache = 0
expected_eager_pure_hit = 0
if same_value:
expected_cache_diff = 1
expected_kernel_cache = 2
else:
if size >= 20:
expected_cache_diff = 1
expected_kernel_cache = 2
else:
expected_cache_diff = 2
expected_kernel_cache = 4
r = range(100)
c_layout = Layout.replicated(self.mesh, rank=2)
values = [np.reshape(r[i : i + size], (4, size // 4)) for i in range(10)]
values = [constant_op.constant(v, dtype=dtypes.float32) for v in values]
c0 = [api.copy_to_mesh(v, c_layout) for v in values]
c0 = [c0[0 if same_value else i] for i in range(10)]
e0 = [values[0 if same_value else i] for i in range(10)]
stats1 = api._dtensor_device()._get_stats()
for i in range(10):
self.assertAllEqual(array_ops.identity(c0[i]).numpy(), e0[i])
gc.collect()
stats2 = api._dtensor_device()._get_stats()
diff = diff_dicts(stats2, stats1)
if same_value:
self.assertEqual(diff["function_manager.size"], expected_cache_diff)
self.assertEqual(
diff["eager_pure_optimization.hit"], expected_eager_pure_hit
)
# TFRT doesn't use eager cache.
if not test_util.is_tfrt_enabled():
self.assertEqual(diff["kernel_cache.size"], expected_kernel_cache)
self.assertEqual(diff["device_cache.size"], expected_device_cache)
else:
# FIXME(feyu): Update these when the leaks are fixed.
if size >= 20:
self.assertEqual(diff["function_manager.size"], expected_cache_diff)
self.assertEqual(
diff["eager_pure_optimization.hit"], expected_eager_pure_hit
)
# TFRT doesn't use eager cache.
if not test_util.is_tfrt_enabled():
self.assertEqual(diff["kernel_cache.size"], expected_kernel_cache)
self.assertEqual(diff["device_cache.size"], expected_device_cache)
else:
self.assertEqual(diff["function_manager.size"], expected_cache_diff)
self.assertEqual(
diff["eager_pure_optimization.hit"], expected_eager_pure_hit
)
# TFRT doesn't use eager cache.
if not test_util.is_tfrt_enabled():
self.assertEqual(diff["kernel_cache.size"], expected_kernel_cache)
self.assertEqual(diff["device_cache.size"], expected_device_cache)
def testManyEagerOpsVaryInput(self):
c_layout = Layout.replicated(self.mesh, rank=10)
c0 = constant_op.constant(
[[[[[[[[[[0, 1, 2, 3], [4, 5, 6, 7]]]]]]]]]], dtype=dtypes.float32
)
e0 = c0.numpy()
c0 = api.copy_to_mesh(c0, c_layout)
for ax in range(10):
self.assertAllEqual(
math_ops.reduce_sum(c0, axis=ax).numpy(), np.sum(e0, axis=ax)
)
if __name__ == "__main__":
test.main()