-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Expand file tree
/
Copy pathtest_cpu_adam.py
More file actions
503 lines (396 loc) · 21.6 KB
/
Copy pathtest_cpu_adam.py
File metadata and controls
503 lines (396 loc) · 21.6 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team
import os
import sys
import torch
import numpy as np
import pytest
from cpuinfo import get_cpu_info
import deepspeed
from deepspeed.accelerator import get_accelerator
from deepspeed.ops.adam import FusedAdam
from deepspeed.ops.op_builder import CPUAdamBuilder, FusedAdamBuilder
from unit.common import DistributedTest
if not deepspeed.ops.__compatible_ops__[CPUAdamBuilder.NAME]:
pytest.skip("cpu-adam is not compatible", allow_module_level=True)
pytest.cpu_vendor = get_cpu_info().get("vendor_id_raw", "").lower()
def check_equal(first, second, atol=1e-2, verbose=False):
x = first.detach().float().numpy()
y = second.detach().float().numpy()
print("ATOL", atol)
if verbose:
print("x = {}".format(x.flatten()))
print("y = {}".format(y.flatten()))
print('-' * 80)
np.testing.assert_allclose(x, y, err_msg="param-update mismatch!", atol=atol)
def _compare_optimizers(model_size, param1, optimizer1, param2, optimizer2):
for i in range(10):
param1.grad = torch.randn(model_size, device=param1.device).to(param1.dtype)
param2.grad = param1.grad.clone().detach().to(device=param2.device, dtype=param2.dtype)
optimizer1.step()
optimizer2.step()
tolerance = param1.float().norm().detach().numpy() * 1e-2
check_equal(param1.float().norm(), param2.float().cpu().norm(), atol=tolerance, verbose=True)
@pytest.mark.parametrize('dtype', [torch.half, torch.bfloat16, torch.float], ids=["fp16", "bf16", "fp32"])
@pytest.mark.parametrize('model_size',
[
(64),
(22),
#(55),
(128),
(1024),
(1048576),
]) # yapf: disable
class TestCPUAdam(DistributedTest):
world_size = 1
reuse_dist_env = True
requires_cuda_env = False
if not get_accelerator().is_available():
init_distributed = False
set_dist_env = False
@pytest.mark.skipif(not get_accelerator().is_available(), reason="only supported in CUDA environments.")
@pytest.mark.skipif(not deepspeed.ops.__compatible_ops__[FusedAdamBuilder.NAME],
reason="FusedAdam is not compatible")
def test_fused_adam_equal(self, dtype, model_size):
if dtype not in get_accelerator().supported_dtypes():
pytest.skip(f"dtype {dtype} not supported in current accelerator")
if ("amd" in pytest.cpu_vendor) and (dtype == torch.half):
pytest.skip("cpu-adam with half precision not supported on AMD CPUs")
from deepspeed.ops.adam import DeepSpeedCPUAdam
cpu_data = torch.randn(model_size, device='cpu').to(dtype)
cpu_param = torch.nn.Parameter(cpu_data)
cuda_param = torch.nn.Parameter(cpu_data.to(get_accelerator().device_name()))
# tolerance = cpu_param.float().norm().detach().numpy() * 1e-2
# check_equal(cpu_param.float().norm(),
# cuda_param.float().cpu().norm(),
# atol=tolerance,
# verbose=True)
cpu_optimizer = DeepSpeedCPUAdam([cpu_param])
cuda_optimizer = FusedAdam([cuda_param])
_compare_optimizers(model_size=model_size,
param1=cpu_param,
optimizer1=cpu_optimizer,
param2=cuda_param,
optimizer2=cuda_optimizer)
def test_torch_adamw_equal(self, dtype, model_size):
if get_accelerator().is_available():
if dtype == torch.half:
pytest.skip("torch.optim.AdamW with half precision inf/nan output.")
if ("amd" in pytest.cpu_vendor) and (dtype == torch.half):
pytest.skip("cpu-adam with half precision not supported on AMD CPUs")
ref_param_device = get_accelerator().device_name()
else:
if dtype == torch.half:
pytest.skip("torch.optim.AdamW with half precision only supported in CUDA environments.")
ref_param_device = 'cpu'
from deepspeed.ops.adam import DeepSpeedCPUAdam
cpu_data = torch.randn(model_size, device='cpu').to(dtype)
cpu_param = torch.nn.Parameter(cpu_data)
ref_param = torch.nn.Parameter(cpu_data.to(ref_param_device))
cpu_optimizer = DeepSpeedCPUAdam([cpu_param])
ref_optimizer = torch.optim.AdamW([ref_param])
_compare_optimizers(model_size=model_size,
param1=cpu_param,
optimizer1=cpu_optimizer,
param2=ref_param,
optimizer2=ref_optimizer)
class TestCPUAdamBf16OptimizerStates(DistributedTest):
world_size = 1
reuse_dist_env = True
requires_cuda_env = False
if not get_accelerator().is_available():
init_distributed = False
set_dist_env = False
@pytest.mark.parametrize('model_size', [64, 1024])
def test_bf16_optimizer_states_dtype(self, model_size):
"""fp32_optimizer_states=False keeps the Adam moments in the bf16 parameter precision."""
from deepspeed.ops.adam import DeepSpeedCPUAdam
param = torch.nn.Parameter(torch.randn(model_size, device='cpu', dtype=torch.bfloat16))
optimizer = DeepSpeedCPUAdam([param], fp32_optimizer_states=False)
param.grad = torch.randn(model_size, device='cpu', dtype=torch.bfloat16)
optimizer.step()
state = optimizer.state[param]
assert state['exp_avg'].dtype == torch.bfloat16
assert state['exp_avg_sq'].dtype == torch.bfloat16
assert state['exp_avg'].device == torch.device('cpu')
assert state['exp_avg_sq'].device == torch.device('cpu')
@pytest.mark.parametrize('model_size', [64, 1024])
def test_bf16_optimizer_states_match_fp32(self, model_size):
"""bf16 moments should track fp32 moments within bf16 tolerance over several steps."""
from deepspeed.ops.adam import DeepSpeedCPUAdam
torch.manual_seed(0)
base = torch.randn(model_size, device='cpu', dtype=torch.float32).to(torch.bfloat16)
param_fp32_states = torch.nn.Parameter(base.clone())
param_bf16_states = torch.nn.Parameter(base.clone())
opt_fp32_states = DeepSpeedCPUAdam([param_fp32_states], fp32_optimizer_states=True)
opt_bf16_states = DeepSpeedCPUAdam([param_bf16_states], fp32_optimizer_states=False)
for _ in range(10):
grad = torch.randn(model_size, device='cpu', dtype=torch.bfloat16)
param_fp32_states.grad = grad.clone()
param_bf16_states.grad = grad.clone()
opt_fp32_states.step()
opt_bf16_states.step()
assert opt_fp32_states.state[param_fp32_states]['exp_avg'].dtype == torch.float32
assert opt_bf16_states.state[param_bf16_states]['exp_avg'].dtype == torch.bfloat16
# bf16 moments round every Adam update to an 8-bit mantissa, so over 10 steps they
# diverge from fp32 moments more than the same-precision comparison in _compare_optimizers
# (1e-2). A wider 5% band keeps this stable while still catching gross errors; the dtype
# assertions above guard the precision itself. Norm comparison follows _compare_optimizers.
tolerance = param_fp32_states.float().norm().detach().numpy() * 5e-2
check_equal(param_fp32_states.float().norm(), param_bf16_states.float().norm(), atol=tolerance)
def _zenflow_adam_proc_worker(param, g0, g1, ea0, ea1, eq0, eq1, stale, ctrl, ready, affinity):
op = CPUAdamBuilder().load()
op.create_adam(0, 1e-3, 0.9, 0.999, 1e-8, 0.0, True, False)
handle = op.zenflow_adam_create(0, affinity)
op.zenflow_adam_register_group(handle, param, g0, g1, ea0, ea1, eq0, eq1, stale)
ready.set()
op.zenflow_adam_run_worker(handle, ctrl.data_ptr()) # blocks until the exit command
op.zenflow_adam_destroy(handle)
op.destroy_adam(0)
@pytest.mark.skipif(not sys.platform.startswith("linux"), reason="cross-process ZenFlowAdam is Linux-only")
def test_zenflow_adam_cross_process():
"""The optimizer-process driver (shared-memory semaphore control + native worker, the
production path for ZenFlow stage 1/2 overlap) must match a per-parameter adam_update
reference bit-for-bit with alternating double buffers. Run as a plain test, not
DistributedTest, so the pytest process (non-daemonic) can spawn the optimizer process."""
import torch.multiprocessing as mp
op = CPUAdamBuilder().load()
if not hasattr(op, "zenflow_adam_ctrl_size"):
pytest.skip("cross-process ZenFlowAdam not available in this build")
lr, beta1, beta2, eps, wd = 1e-3, 0.9, 0.999, 1e-8, 0.0
n = 100003 # non-SIMD-aligned, exercises the scalar tail
affinity = list(range(min(4, os.cpu_count() or 1)))
ctrl = torch.zeros(op.zenflow_adam_ctrl_size(), dtype=torch.uint8).share_memory_()
op.zenflow_adam_ctrl_init(ctrl.data_ptr(), 1)
torch.manual_seed(0)
param = torch.randn(n).share_memory_()
g = [torch.zeros(n).share_memory_(), torch.zeros(n).share_memory_()]
ea = [torch.zeros(n).share_memory_(), torch.zeros(n).share_memory_()]
eq = [torch.zeros(n).share_memory_(), torch.zeros(n).share_memory_()]
stale = torch.zeros(n).share_memory_()
op.create_adam(1, lr, beta1, beta2, eps, wd, True, False)
p_ref = param.clone()
ea_ref = [ea[0].clone(), ea[1].clone()]
eq_ref = [eq[0].clone(), eq[1].clone()]
st_ref = stale.clone()
ctx = mp.get_context("spawn")
ready = ctx.Event()
proc = ctx.Process(target=_zenflow_adam_proc_worker,
args=(param, g[0], g[1], ea[0], ea[1], eq[0], eq[1], stale, ctrl, ready, affinity))
proc.start()
try:
assert ready.wait(timeout=60), "optimizer process did not start"
# With no step submitted yet, a bounded wait must time out (return False) rather than
# block -- this is what lets the training side notice a dead optimizer process.
assert op.zenflow_adam_wait(ctrl.data_ptr(), 0.05) is False, "wait should time out when no step is pending"
for step in range(1, 6):
now = step & 1
grad = torch.randn(n)
g[now].copy_(grad)
op.zenflow_adam_submit(ctrl.data_ptr(), now, step, [lr], [beta1], [beta2], [eps], [wd], [1])
assert op.zenflow_adam_wait(ctrl.data_ptr(), 60.0), f"wait timed out step {step}"
# Reference: single-tensor adam_update on the mirror, then snapshot the updated
# param into the stale buffer -- exactly what the native worker does per group.
op.adam_update(1, step, lr, beta1, beta2, eps, wd, True, p_ref, grad.clone(), ea_ref[now], eq_ref[now])
st_ref.copy_(p_ref)
assert torch.equal(param, p_ref), f"param mismatch step {step}"
assert torch.equal(ea[now], ea_ref[now]), f"exp_avg mismatch step {step}"
assert torch.equal(eq[now], eq_ref[now]), f"exp_avg_sq mismatch step {step}"
assert torch.equal(stale, st_ref), f"stale mismatch step {step}"
op.zenflow_adam_ctrl_exit(ctrl.data_ptr())
proc.join(timeout=10)
finally:
if proc.is_alive():
proc.terminate()
proc.join(timeout=5)
op.destroy_adam(1)
class TestCPUAdamGPUError(DistributedTest):
def test_cpu_adam_gpu_error(self):
model_size = 64
from deepspeed.ops.adam import DeepSpeedCPUAdam
device = get_accelerator().device_name(0) # 'cuda:0' or 'xpu:0'
param = torch.nn.Parameter(torch.randn(model_size, device=device))
optimizer = DeepSpeedCPUAdam([param])
param.grad = torch.randn(model_size, device=device)
with pytest.raises(AssertionError):
optimizer.step()
class TestCPUAdamSubgroup(DistributedTest):
world_size = 1
reuse_dist_env = True
requires_cuda_env = False
if not get_accelerator().is_available():
init_distributed = False
set_dist_env = False
@pytest.mark.parametrize('dtype', [torch.half, torch.bfloat16], ids=["fp16", "bf16"])
@pytest.mark.parametrize('model_size', [64, 128, 1024])
def test_step_subgroup_basic(self, dtype, model_size):
"""Test basic functionality of step_subgroup method."""
if ("amd" in pytest.cpu_vendor) and (dtype == torch.half):
pytest.skip("cpu-adam with half precision not supported on AMD CPUs")
from deepspeed.ops.adam import DeepSpeedCPUAdam
# Create parameters
cpu_data = torch.randn(model_size, device='cpu').to(dtype)
param = torch.nn.Parameter(cpu_data)
optimizer = DeepSpeedCPUAdam([param])
# Set gradient
param.grad = torch.randn(model_size, device='cpu').to(dtype)
# Store initial parameter values
initial_param = param.data.clone()
# Test step_subgroup with subgroup_id=0
subgroup_id = 0
optimizer.step_subgroup(subgroup_id)
# Verify parameter was updated
assert not torch.equal(param.data, initial_param), "Parameters should be updated after step_subgroup"
# Verify optimizer state was created for subgroup
assert subgroup_id in optimizer.state, "Optimizer state should be created for subgroup"
assert optimizer.state[subgroup_id]['step'] == 1, "Step count should be 1"
assert 'exp_avg' in optimizer.state[subgroup_id], "exp_avg should be in state"
assert 'exp_avg_sq' in optimizer.state[subgroup_id], "exp_avg_sq should be in state"
@pytest.mark.parametrize('dtype', [torch.half, torch.bfloat16], ids=["fp16", "bf16"])
def test_step_subgroup_multiple_calls(self, dtype):
"""Test multiple calls to step_subgroup increment step count correctly."""
if ("amd" in pytest.cpu_vendor) and (dtype == torch.half):
pytest.skip("cpu-adam with half precision not supported on AMD CPUs")
from deepspeed.ops.adam import DeepSpeedCPUAdam
model_size = 64
cpu_data = torch.randn(model_size, device='cpu').to(dtype)
param = torch.nn.Parameter(cpu_data)
optimizer = DeepSpeedCPUAdam([param])
subgroup_id = 0
# Perform multiple steps
for step in range(1, 4):
param.grad = torch.randn(model_size, device='cpu').to(dtype)
optimizer.step_subgroup(subgroup_id)
# Verify step count increments
assert optimizer.state[subgroup_id]['step'] == step, f"Step count should be {step}"
@pytest.mark.parametrize('dtype', [torch.half, torch.bfloat16], ids=["fp16", "bf16"])
def test_rollback_subgroup_basic(self, dtype):
"""Test basic functionality of rollback_subgroup method."""
if ("amd" in pytest.cpu_vendor) and (dtype == torch.half):
pytest.skip("cpu-adam with half precision not supported on AMD CPUs")
from deepspeed.ops.adam import DeepSpeedCPUAdam
model_size = 64
cpu_data = torch.randn(model_size, device='cpu').to(dtype)
param = torch.nn.Parameter(cpu_data)
optimizer = DeepSpeedCPUAdam([param])
subgroup_id = 0
param.grad = torch.randn(model_size, device='cpu').to(dtype)
# First, perform a step to initialize state
optimizer.step_subgroup(subgroup_id)
assert optimizer.state[subgroup_id]['step'] == 1
# Store parameter state after step
param_after_step = param.data.clone()
exp_avg_after_step = optimizer.state[subgroup_id]['exp_avg'].clone()
exp_avg_sq_after_step = optimizer.state[subgroup_id]['exp_avg_sq'].clone()
# Now rollback
optimizer.rollback_subgroup(subgroup_id)
# Verify step count decremented
assert optimizer.state[subgroup_id]['step'] == 0, "Step count should be decremented after rollback"
def test_rollback_subgroup_uninitialized_error(self):
"""Test that rollback_subgroup raises error for uninitialized subgroup."""
from deepspeed.ops.adam import DeepSpeedCPUAdam
model_size = 64
param = torch.nn.Parameter(torch.randn(model_size, device='cpu'))
optimizer = DeepSpeedCPUAdam([param])
# Try to rollback uninitialized subgroup
with pytest.raises(RuntimeError, match="Cannot rollback optimizer state for sub_group_id 0"):
optimizer.rollback_subgroup(0)
def test_rollback_subgroup_zero_step_error(self):
"""Test that rollback_subgroup raises error when step count is already 0."""
from deepspeed.ops.adam import DeepSpeedCPUAdam
model_size = 64
param = torch.nn.Parameter(torch.randn(model_size, device='cpu'))
optimizer = DeepSpeedCPUAdam([param])
subgroup_id = 0
param.grad = torch.randn(model_size, device='cpu')
# Initialize state by doing one step
optimizer.step_subgroup(subgroup_id)
# Rollback once (step should become 0)
optimizer.rollback_subgroup(subgroup_id)
assert optimizer.state[subgroup_id]['step'] == 0
# Try to rollback again - should raise error
with pytest.raises(RuntimeError, match="Cannot rollback sub_group_id 0: step count is 0"):
optimizer.rollback_subgroup(subgroup_id)
@pytest.mark.parametrize('dtype', [torch.half, torch.bfloat16], ids=["fp16", "bf16"])
def test_step_rollback_sequence(self, dtype):
"""Test sequence of step_subgroup and rollback_subgroup operations."""
if ("amd" in pytest.cpu_vendor) and (dtype == torch.half):
pytest.skip("cpu-adam with half precision not supported on AMD CPUs")
from deepspeed.ops.adam import DeepSpeedCPUAdam
model_size = 64
cpu_data = torch.randn(model_size, device='cpu').to(dtype)
param = torch.nn.Parameter(cpu_data)
optimizer = DeepSpeedCPUAdam([param])
subgroup_id = 0
param.grad = torch.randn(model_size, device='cpu').to(dtype)
# Perform multiple steps
for step in range(1, 4):
optimizer.step_subgroup(subgroup_id)
assert optimizer.state[subgroup_id]['step'] == step
# Rollback steps one by one
for step in range(2, -1, -1):
optimizer.rollback_subgroup(subgroup_id)
assert optimizer.state[subgroup_id]['step'] == step
def test_multiple_subgroups(self):
"""Test that different subgroups maintain independent state."""
from deepspeed.ops.adam import DeepSpeedCPUAdam
model_size = 64
param = torch.nn.Parameter(torch.randn(model_size, device='cpu'))
optimizer = DeepSpeedCPUAdam([param])
param.grad = torch.randn(model_size, device='cpu')
# Step different subgroups
optimizer.step_subgroup(0)
optimizer.step_subgroup(1)
optimizer.step_subgroup(0) # Step subgroup 0 again
# Verify independent step counts
assert optimizer.state[0]['step'] == 2, "Subgroup 0 should have step count 2"
assert optimizer.state[1]['step'] == 1, "Subgroup 1 should have step count 1"
# Rollback subgroup 0 only
optimizer.rollback_subgroup(0)
assert optimizer.state[0]['step'] == 1, "Subgroup 0 step count should be decremented"
assert optimizer.state[1]['step'] == 1, "Subgroup 1 step count should be unchanged"
def test_step_subgroup_same_step_idempotent_across_subgroups(self):
"""Repeated same-step subgroup updates should remain bit-identical."""
from deepspeed.ops.adam import DeepSpeedCPUAdam
model_size = 128
steps = 4
base = torch.randn(model_size, device='cpu', dtype=torch.float32)
param_a = torch.nn.Parameter(base.clone())
param_b = torch.nn.Parameter(base.clone())
optimizer = DeepSpeedCPUAdam([param_a])
for logical_step in range(1, steps + 1):
grad = torch.randn(model_size, device='cpu', dtype=torch.float32)
optimizer.param_groups[0]['params'] = [param_a]
param_a.grad = grad.clone()
optimizer.step_subgroup(0)
optimizer.param_groups[0]['params'] = [param_b]
param_b.grad = grad.clone()
optimizer.step_subgroup(1)
assert optimizer.state[0]['step'] == logical_step
assert optimizer.state[1]['step'] == logical_step
assert torch.equal(param_a.data, param_b.data)
assert torch.equal(optimizer.state[0]['exp_avg'], optimizer.state[1]['exp_avg'])
assert torch.equal(optimizer.state[0]['exp_avg_sq'], optimizer.state[1]['exp_avg_sq'])
def test_step_same_step_idempotent_across_param_keys(self):
"""Repeated optimizer.step() with swapped param keys should be deterministic."""
from deepspeed.ops.adam import DeepSpeedCPUAdam
model_size = 128
steps = 4
base = torch.randn(model_size, device='cpu', dtype=torch.float32)
param_a = torch.nn.Parameter(base.clone())
param_b = torch.nn.Parameter(base.clone())
optimizer = DeepSpeedCPUAdam([param_a])
for logical_step in range(1, steps + 1):
grad = torch.randn(model_size, device='cpu', dtype=torch.float32)
optimizer.param_groups[0]['params'] = [param_a]
param_a.grad = grad.clone()
optimizer.step()
optimizer.param_groups[0]['params'] = [param_b]
param_b.grad = grad.clone()
optimizer.step()
assert optimizer.state[param_a]['step'] == logical_step
assert optimizer.state[param_b]['step'] == logical_step
assert torch.equal(param_a.data, param_b.data)
assert torch.equal(optimizer.state[param_a]['exp_avg'], optimizer.state[param_b]['exp_avg'])
assert torch.equal(optimizer.state[param_a]['exp_avg_sq'], optimizer.state[param_b]['exp_avg_sq'])