-
Notifications
You must be signed in to change notification settings - Fork 570
Expand file tree
/
Copy pathtest_runtime_tpu.py
More file actions
278 lines (220 loc) · 9.17 KB
/
Copy pathtest_runtime_tpu.py
File metadata and controls
278 lines (220 loc) · 9.17 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
import concurrent.futures
import os
import time
from typing import Dict
from unittest import mock
import requests
import torch
from absl.testing import absltest, parameterized
import torch_xla
import torch_xla.core.xla_env_vars as xenv
import torch_xla.core.xla_model as xm
import torch_xla.debug.metrics as met
from torch_xla import runtime as xr
from torch_xla._internal import pjrt
from torch_xla._internal import tpu
assert tpu.num_available_chips() > 0, 'Must be run on a TPU!'
def _ordinal_to_device(processes=None,
cores_per_process=None) -> Dict[int, torch.device]:
"""Returns a dict of global ordinals and their expected `torch.device` value.
Example for v4-8 multiprocessing:
{
0: torch.device('xla:0'),
1: torch.device('xla:0'),
2: torch.device('xla:0'),
3: torch.device('xla:0'),
}
Exmaple for v4-8 single-process:
{
0: torch.device('xla:0'),
1: torch.device('xla:1'),
2: torch.device('xla:2'),
3: torch.device('xla:3'),
}
Example for v3-8 multiprocessing:
{
0: torch.device('xla:0'),
1: torch.device('xla:1'),
2: torch.device('xla:0'),
3: torch.device('xla:1'),
4: torch.device('xla:0'),
5: torch.device('xla:1'),
6: torch.device('xla:0'),
7: torch.device('xla:1'),
}
"""
processes = processes or tpu.num_available_chips()
cores_per_process = cores_per_process or tpu.num_logical_cores_per_chip()
ordinal = 0
ordinal_to_device = {}
for _ in range(processes):
for core in range(cores_per_process):
ordinal_to_device[ordinal] = torch.device(f'xla:{core}')
ordinal += 1
return ordinal_to_device
class TestExperimentalPjrtTpu(parameterized.TestCase):
def setUp(self):
xr.set_device_type('TPU')
try:
tpu_env = tpu.get_tpu_env()
self.accelerator_type = tpu_env['ACCELERATOR_TYPE']
# Number of logical devices per single-host TPU
self.num_devices = tpu.num_available_devices()
except requests.HTTPError as e:
raise EnvironmentError(
'Failed to get TPU metadata. Are you running on a TPU?') from e
# TODO: assert ComputationClient is not initialized
# The main process must not initialize the ComputationClient, otherwise
# sub-processes will not be able to initialize the client witht the correct
# settings.
def tearDown(self) -> None:
os.environ.pop(xenv.TPU_VISIBLE_CHIPS, None)
os.environ.pop(xenv.TPU_PROCESS_BOUNDS, None)
def test_tensor_oom(self):
a = torch.randn((4000000000, 4000000000), device="xla")
with self.assertRaises(Exception) as cm:
# The above tensor doesn't fit in HBM. It should result in a python
# exception instead of crashing.
a.sum()
self.assertEqual(
(type(cm.exception).__name__, str(cm.exception)),
("RuntimeError", "")
)
def test_xla_devices_multiprocess(self):
expected = _ordinal_to_device()
devices_per_process = pjrt.run_multiprocess(xm.xla_device)
self.assertDictEqual(devices_per_process, expected)
@absltest.skipIf(tpu.num_available_chips() != 4, "Not implemented")
def test_xla_devices_single_process_all_chips(self):
expected = _ordinal_to_device(
processes=1, cores_per_process=tpu.num_available_devices())
os.environ[xenv.TPU_VISIBLE_CHIPS] = '0,1,2,3'
os.environ[xenv.TPU_PROCESS_BOUNDS] = '1,1,1'
devices = pjrt.run_multiprocess(xm.xla_device)
self.assertDictEqual(devices, expected)
def test_xla_devices_single_process_one_chip(self):
expected = _ordinal_to_device(processes=1)
os.environ[xenv.TPU_VISIBLE_CHIPS] = '0'
os.environ[xenv.TPU_PROCESS_BOUNDS] = '1,1,1'
devices = pjrt.run_multiprocess(xm.xla_device)
self.assertDictEqual(devices, expected)
@staticmethod
def _fail_on_nonfirst_device():
def _assert(i):
assert i == 0, f"the device index {i} must be 0 in nprocs=1"
debug_single_process = FLAGS.num_cores == 1
torch_xla.launch(
_mp_fn, args=(FLAGS,), debug_single_process=debug_single_process)
def test_xla_devices_single_process_one_chip_one_device_spawn(self):
# Avoid initializing the TPU client in the parent process
with concurrent.futures.ProcessPoolExecutor(max_workers=1) as executor:
executor.submit(self._fail_on_nonfirst_device).result()
def test_default_xla_devices(self):
with concurrent.futures.ProcessPoolExecutor(max_workers=1) as e:
f = e.submit(xm.get_xla_supported_devices)
devices = [torch.device(d) for d in f.result()]
self.assertListEqual(
devices, [torch.device(f'xla:{i}') for i in range(self.num_devices)])
def test_global_ordinal(self):
results = pjrt.run_multiprocess(xr.global_ordinal)
values = list(results.values())
self.assertListEqual(sorted(values), list(range(self.num_devices)))
def test_local_ordinal(self):
results = pjrt.run_multiprocess(xr.local_ordinal)
self.assertCountEqual(results.values(), list(range(self.num_devices)))
@staticmethod
def _local_ordinal_with_discontiguous_global_ordinal_v4():
# Actual set of global ordinals from one v4-128 host
global_ordinals = [58, 59, 62, 63]
new_global_ordinal = global_ordinals[xr.global_ordinal()]
with mock.patch.object(
xr, 'global_ordinal', return_value=new_global_ordinal):
return xr.local_ordinal()
@absltest.skipIf(tpu.num_available_devices() != 4, "Not implemented")
def test_local_ordinal_with_discontiguous_global_ordinal_v4(self):
results = pjrt.run_multiprocess(
self._local_ordinal_with_discontiguous_global_ordinal_v4)
self.assertCountEqual(results.values(), [0, 1, 2, 3])
@absltest.skipIf(tpu.num_available_devices() != 4, "Not implemented")
def test_local_ordinal_with_discontiguous_global_ordinal_v4_threaded(self):
os.environ[xenv.TPU_PROCESS_BOUNDS] = '1,1,1'
os.environ[xenv.TPU_VISIBLE_CHIPS] = '0,1,2,3'
results = pjrt.run_multiprocess(
self._local_ordinal_with_discontiguous_global_ordinal_v4)
self.assertCountEqual(results.values(), [0, 1, 2, 3])
@staticmethod
def _spawn_threads() -> Dict[int, torch.device]:
results = {}
pjrt.spawn_threads(lambda i: results.setdefault(i, torch_xla.device()))
return results
def test_spawn_threads(self):
with concurrent.futures.ProcessPoolExecutor(max_workers=1) as e:
results = e.submit(self._spawn_threads).result()
self.assertDictEqual(
results,
{i: torch.device(f'xla:{i}') for i in range(self.num_devices)})
@staticmethod
def _spawn_error():
# Initialize the client in the parent process
torch_xla.device()
torch_xla.launch(xm.xla_device)
def test_spawn_error(self):
with concurrent.futures.ProcessPoolExecutor(max_workers=1) as executor:
# Error message should at least mention that the runtime is initialized
with self.assertRaisesRegex(RuntimeError, "initialized"):
executor.submit(self._spawn_error).result()
@staticmethod
def _runtime_device_attributes():
return xr.runtime_device_attributes(str(torch_xla.device()))
def test_runtime_device_attributes(self):
result = pjrt.run_multiprocess(self._runtime_device_attributes)
for device in result.values():
self.assertCountEqual(['coords', 'core_on_chip', 'num_cores'],
list(device.keys()))
self.assertIsInstance(device['coords'], list)
self.assertIsInstance(device['core_on_chip'], int)
@staticmethod
def _global_runtime_device_attributes():
return xr.global_runtime_device_attributes()
def test_global_runtime_device_attributes(self):
results = pjrt.run_multiprocess(self._global_runtime_device_attributes)
for result in results.values():
for device in result:
self.assertCountEqual(['coords', 'core_on_chip', 'name', 'num_cores'],
list(device.keys()))
self.assertIsInstance(device['coords'], list)
self.assertIsInstance(device['core_on_chip'], int)
self.assertIsInstance(device['name'], str)
@staticmethod
def _execute_time_metric():
# Initialize the client before starting the timer.
torch_xla.device()
begin = time.perf_counter_ns()
value = (
torch.randn(10000, 10000, device='xla') *
torch.randn(10000, 10000, device='xla'))
value_mean = value.mean()
torch_xla.sync()
cpu_value = value_mean.cpu()
wall_time_ns = time.perf_counter_ns() - begin
_, execute_time_ns, _ = met.metric_data('ExecuteTime')
return execute_time_ns
def test_execute_time_metric(self):
results = pjrt.run_multiprocess(self._execute_time_metric)
for i, v in results.items():
expected_time_seconds = .1
self.assertGreater(
v, expected_time_seconds * 1e-9,
f"Expected exectue time of {i} to take more than "
f"{expected_time_seconds} seconds, got {v / 1e9} seconds")
@staticmethod
def _memory_usage():
return xm.get_memory_info()
def test_memory_usage(self):
results = pjrt.run_multiprocess(self._memory_usage)
for usage in results.values():
self.assertIn('bytes_used', usage)
self.assertIn('bytes_limit', usage)
self.assertIn('peak_bytes_used', usage)
if __name__ == '__main__':
absltest.main()