test_fp16.py 26.6 KB
Newer Older
1
2
3
4
5
6
import torch
import deepspeed
import argparse
import pytest
import json
import os
7
from deepspeed.ops.adam import FusedAdam
8
from common import distributed_test
9
from simple_model import SimpleModel, SimpleOptimizer, random_dataloader, args_from_dict, create_deepspeed_args
10

11
12
13
14
15
16
try:
    from apex import amp
    _amp_available = True
except ImportError:
    _amp_available = False
amp_available = pytest.mark.skip(_amp_available, reason="apex/amp is not installed")
17

18

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def test_lamb_fp32_grad_clip(tmpdir):
    config_dict = {
        "train_batch_size": 2,
        "steps_per_print": 1,
        "optimizer": {
            "type": "Lamb",
            "params": {
                "lr": 0.00015
            }
        },
        "gradient_clipping": 1.0
    }
    args = args_from_dict(tmpdir, config_dict)
    hidden_dim = 10

34
    model = SimpleModel(hidden_dim)
35
36
37

    @distributed_test(world_size=[1, 2])
    def _test_lamb_fp32_grad_clip(args, model, hidden_dim):
38
39
40
        model, _, _, _ = deepspeed.initialize(args=args,
                                              model=model,
                                              model_parameters=model.parameters())
41
42
43
        data_loader = random_dataloader(model=model,
                                        total_samples=50,
                                        hidden_dim=hidden_dim,
Jeff Rasley's avatar
Jeff Rasley committed
44
45
                                        device=model.device,
                                        dtype=torch.float)
46
        for n, batch in enumerate(data_loader):
Jeff Rasley's avatar
Jeff Rasley committed
47
            loss = model(batch[0], batch[1])
48
49
50
51
52
53
            model.backward(loss)
            model.step()

    _test_lamb_fp32_grad_clip(args=args, model=model, hidden_dim=hidden_dim)


54
55
56
57
58
59
60
def test_lamb_fp16_basic(tmpdir):
    config_dict = {
        "train_batch_size": 2,
        "steps_per_print": 1,
        "optimizer": {
            "type": "Lamb",
            "params": {
61
                "lr": 0.00015
62
63
            }
        },
64
        "gradient_clipping": 1.0,
65
66
67
68
        "fp16": {
            "enabled": True
        }
    }
69
    args = args_from_dict(tmpdir, config_dict)
70
71
    hidden_dim = 10

72
    model = SimpleModel(hidden_dim)
73
74
75

    @distributed_test(world_size=[1, 2])
    def _test_lamb_fp16_basic(args, model, hidden_dim):
76
77
78
        model, _, _, _ = deepspeed.initialize(args=args,
                                              model=model,
                                              model_parameters=model.parameters())
79
80
81
82
        data_loader = random_dataloader(model=model,
                                        total_samples=50,
                                        hidden_dim=hidden_dim,
                                        device=model.device)
83
84
85
86
87
88
89
90
91
92
        for n, batch in enumerate(data_loader):
            loss = model(batch[0], batch[1])
            model.backward(loss)
            model.step()

    _test_lamb_fp16_basic(args=args, model=model, hidden_dim=hidden_dim)


def test_lamb_fp16_empty_grad(tmpdir):
    config_dict = {
Jeff Rasley's avatar
Jeff Rasley committed
93
        "train_batch_size": 2,
94
95
96
97
        "steps_per_print": 1,
        "optimizer": {
            "type": "Lamb",
            "params": {
98
                "lr": 0.00015
99
100
            }
        },
101
        "gradient_clipping": 1.0,
102
103
104
105
        "fp16": {
            "enabled": True
        }
    }
106
    args = args_from_dict(tmpdir, config_dict)
107
108
    hidden_dim = 10

109
    model = SimpleModel(hidden_dim, empty_grad=True)
110

Jeff Rasley's avatar
Jeff Rasley committed
111
    @distributed_test(world_size=[2])
112
    def _test_lamb_fp16_empty_grad(args, model, hidden_dim):
113
114
115
        model, _, _, _ = deepspeed.initialize(args=args,
                                              model=model,
                                              model_parameters=model.parameters())
116
117
118
119
        data_loader = random_dataloader(model=model,
                                        total_samples=50,
                                        hidden_dim=hidden_dim,
                                        device=model.device)
120
121
122
123
124
125
126
127
        for n, batch in enumerate(data_loader):
            loss = model(batch[0], batch[1])
            model.backward(loss)
            model.step()

    _test_lamb_fp16_empty_grad(args=args, model=model, hidden_dim=hidden_dim)


Jeff Rasley's avatar
Jeff Rasley committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
def test_adam_fp32_empty_grad(tmpdir):
    config_dict = {
        "train_batch_size": 2,
        "steps_per_print": 1,
        "optimizer": {
            "type": "Adam",
            "params": {
                "lr": 0.00015
            }
        },
        "gradient_clipping": 1.0,
        "fp16": {
            "enabled": False
        }
    }
    args = args_from_dict(tmpdir, config_dict)
    hidden_dim = 10

146
    model = SimpleModel(hidden_dim, empty_grad=True)
Jeff Rasley's avatar
Jeff Rasley committed
147
148
149

    @distributed_test(world_size=[2])
    def _test_adam_fp32_empty_grad(args, model, hidden_dim):
150
151
152
        model, _, _, _ = deepspeed.initialize(args=args,
                                              model=model,
                                              model_parameters=model.parameters())
Jeff Rasley's avatar
Jeff Rasley committed
153
154
155
156
157
158
159
160
161
162
163
164
165
        data_loader = random_dataloader(model=model,
                                        total_samples=50,
                                        hidden_dim=hidden_dim,
                                        device=model.device,
                                        dtype=torch.float)
        for n, batch in enumerate(data_loader):
            loss = model(batch[0], batch[1])
            model.backward(loss)
            model.step()

    _test_adam_fp32_empty_grad(args=args, model=model, hidden_dim=hidden_dim)


166
167
168
169
170
171
172
173
def test_adamw_fp16_basic(tmpdir):
    config_dict = {
        "train_batch_size": 1,
        "steps_per_print": 1,
        "fp16": {
            "enabled": True
        }
    }
174
    args = args_from_dict(tmpdir, config_dict)
175
176
    hidden_dim = 10

177
    model = SimpleModel(hidden_dim)
178
179
180
181

    @distributed_test(world_size=[1])
    def _test_adamw_fp16_basic(args, model, hidden_dim):
        optimizer = torch.optim.AdamW(params=model.parameters())
182
183
184
        model, _, _, _ = deepspeed.initialize(args=args,
                                              model=model,
                                              optimizer=optimizer)
185
186
187
188
        data_loader = random_dataloader(model=model,
                                        total_samples=50,
                                        hidden_dim=hidden_dim,
                                        device=model.device)
189
190
191
192
193
194
195
196
        for n, batch in enumerate(data_loader):
            loss = model(batch[0], batch[1])
            model.backward(loss)
            model.step()

    _test_adamw_fp16_basic(args=args, model=model, hidden_dim=hidden_dim)


197
198
199
200
201
202
203
204
205
206
207
def test_dict_config_adamw_fp16_basic():
    config_dict = {
        "train_batch_size": 1,
        "steps_per_print": 1,
        "fp16": {
            "enabled": True
        }
    }
    args = create_deepspeed_args()
    hidden_dim = 10

208
    model = SimpleModel(hidden_dim)
209
210
211
212

    @distributed_test(world_size=[1])
    def _test_adamw_fp16_basic(args, model, hidden_dim, config_dict):
        optimizer = torch.optim.AdamW(params=model.parameters())
213
214
215
216
        model, _, _, _ = deepspeed.initialize(args=args,
                                              model=model,
                                              optimizer=optimizer,
                                              config_params=config_dict)
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
        data_loader = random_dataloader(model=model,
                                        total_samples=50,
                                        hidden_dim=hidden_dim,
                                        device=model.device)
        for n, batch in enumerate(data_loader):
            loss = model(batch[0], batch[1])
            model.backward(loss)
            model.step()

    _test_adamw_fp16_basic(args=args,
                           model=model,
                           hidden_dim=hidden_dim,
                           config_dict=config_dict)


232
233
234
235
236
237
238
239
def test_adamw_fp16_empty_grad(tmpdir):
    config_dict = {
        "train_batch_size": 1,
        "steps_per_print": 1,
        "fp16": {
            "enabled": True
        }
    }
240
    args = args_from_dict(tmpdir, config_dict)
241
242
    hidden_dim = 10

243
    model = SimpleModel(hidden_dim)
244
245
246
247

    @distributed_test(world_size=[1])
    def _test_adamw_fp16_empty_grad(args, model, hidden_dim):
        optimizer = torch.optim.AdamW(params=model.parameters())
248
249
250
        model, _, _, _ = deepspeed.initialize(args=args,
                                              model=model,
                                              optimizer=optimizer)
251
252
253
254
        data_loader = random_dataloader(model=model,
                                        total_samples=50,
                                        hidden_dim=hidden_dim,
                                        device=model.device)
255
256
257
258
259
260
        for n, batch in enumerate(data_loader):
            loss = model(batch[0], batch[1])
            model.backward(loss)
            model.step()

    _test_adamw_fp16_empty_grad(args=args, model=model, hidden_dim=hidden_dim)
261
262


Jeff Rasley's avatar
Jeff Rasley committed
263
264
265
266
267
268
269
270
271
272
@pytest.mark.parametrize('zero_stage, use_cpu_offload',
                         [
                             (1,
                              False),
                             (2,
                              False),
                             (2,
                              True),
                         ])
def test_adam_fp16_zero_onecycle_compatibility(tmpdir, zero_stage, use_cpu_offload):
273
    # if use_cpu_offload and not deepspeed.ops.__installed_ops__['cpu-adam']:
274
    #    pytest.skip("cpu-adam is not installed")
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
    config_dict = {
        "train_batch_size": 1,
        "steps_per_print": 1,
        "optimizer": {
            "type": "Adam",
            "params": {
                "lr": 0.00015
            }
        },
        "scheduler": {
            "type": "OneCycle",
            "params": {
                "cycle_first_step_size": 16000,
                "cycle_first_stair_count": 8000,
                "decay_step_size": 16000,
                "cycle_min_lr": 1e-06,
                "cycle_max_lr": 3e-05,
                "decay_lr_rate": 1e-07,
                "cycle_min_mom": 0.85,
                "cycle_max_mom": 0.99,
                "decay_mom_rate": 0.0
            }
        },
        "fp16": {
            "enabled": True
        },
Jeff Rasley's avatar
Jeff Rasley committed
301
        "zero_optimization": {
Jeff Rasley's avatar
Jeff Rasley committed
302
303
            "stage": zero_stage,
            "cpu_offload": use_cpu_offload
Jeff Rasley's avatar
Jeff Rasley committed
304
        }
305
    }
Jeff Rasley's avatar
Jeff Rasley committed
306

307
308
309
    args = args_from_dict(tmpdir, config_dict)
    hidden_dim = 10

310
    model = SimpleModel(hidden_dim)
311
312

    @distributed_test(world_size=[1])
Jeff Rasley's avatar
Jeff Rasley committed
313
    def _test_adam_fp16_zero_onecycle_compatibility(args, model, hidden_dim):
314
315
316
        model, _, _, _ = deepspeed.initialize(args=args,
                                              model=model,
                                              model_parameters=model.parameters())
317
318
319
320
321
322
323
324
325
        data_loader = random_dataloader(model=model,
                                        total_samples=50,
                                        hidden_dim=hidden_dim,
                                        device=model.device)
        for n, batch in enumerate(data_loader):
            loss = model(batch[0], batch[1])
            model.backward(loss)
            model.step()

Jeff Rasley's avatar
Jeff Rasley committed
326
327
328
    _test_adam_fp16_zero_onecycle_compatibility(args=args,
                                                model=model,
                                                hidden_dim=hidden_dim)
329
330


Jeff Rasley's avatar
Jeff Rasley committed
331
332
333
334
335
336
337
338
339
340
@pytest.mark.parametrize('zero_stage, use_cpu_offload',
                         [
                             (1,
                              False),
                             (2,
                              False),
                             (2,
                              True),
                         ])
def test_zero_static_scale(tmpdir, zero_stage, use_cpu_offload):
341
    # if use_cpu_offload and not deepspeed.ops.__installed_ops__['cpu-adam']:
342
    #    pytest.skip("cpu-adam is not installed")
343
    config_dict = {
Jeff Rasley's avatar
Jeff Rasley committed
344
        "train_batch_size": 4,
345
346
347
348
349
350
351
352
        "steps_per_print": 1,
        "optimizer": {
            "type": "Adam",
            "params": {
                "lr": 0.00015
            }
        },
        "fp16": {
Jeff Rasley's avatar
Jeff Rasley committed
353
354
            "enabled": True,
            "loss_scale": 138.
355
        },
Jeff Rasley's avatar
Jeff Rasley committed
356
        "zero_optimization": {
Jeff Rasley's avatar
Jeff Rasley committed
357
358
            "stage": zero_stage,
            "cpu_offload": use_cpu_offload
Jeff Rasley's avatar
Jeff Rasley committed
359
        }
360
361
362
    }
    args = args_from_dict(tmpdir, config_dict)

Jeff Rasley's avatar
Jeff Rasley committed
363
364
365
    @distributed_test(world_size=2)
    def _test_zero_static_scale(args):
        hidden_dim = 10
366
        model = SimpleModel(hidden_dim)
367
368
369
        model, optim, _, _ = deepspeed.initialize(args=args,
                                                  model=model,
                                                  model_parameters=model.parameters())
370

Jeff Rasley's avatar
Jeff Rasley committed
371
372
373
374
375
        # Ensure the static scaler is configured.
        assert optim.dynamic_loss_scale == False
        assert optim.loss_scaler.loss_scale == 138.

        # Now make sure things work..
376
        data_loader = random_dataloader(model=model,
Jeff Rasley's avatar
Jeff Rasley committed
377
                                        total_samples=10,
378
379
380
381
382
383
384
                                        hidden_dim=hidden_dim,
                                        device=model.device)
        for n, batch in enumerate(data_loader):
            loss = model(batch[0], batch[1])
            model.backward(loss)
            model.step()

Jeff Rasley's avatar
Jeff Rasley committed
385
    _test_zero_static_scale(args)
386
387


Jeff Rasley's avatar
Jeff Rasley committed
388
def test_zero_static_scale_deprecated_format(tmpdir):
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
    config_dict = {
        "train_batch_size": 4,
        "steps_per_print": 1,
        "optimizer": {
            "type": "Adam",
            "params": {
                "lr": 0.00015
            }
        },
        "fp16": {
            "enabled": True,
            "loss_scale": 138.
        },
        "zero_optimization": True
    }
    args = args_from_dict(tmpdir, config_dict)

    @distributed_test(world_size=2)
    def _test_zero_static_scale(args):
        hidden_dim = 10
409
        model = SimpleModel(hidden_dim)
410
411
412
        model, optim, _, _ = deepspeed.initialize(args=args,
                                                  model=model,
                                                  model_parameters=model.parameters())
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428

        # Ensure the static scaler is configured.
        assert optim.dynamic_loss_scale == False
        assert optim.loss_scaler.loss_scale == 138.

        # Now make sure things work..
        data_loader = random_dataloader(model=model,
                                        total_samples=10,
                                        hidden_dim=hidden_dim,
                                        device=model.device)
        for n, batch in enumerate(data_loader):
            loss = model(batch[0], batch[1])
            model.backward(loss)
            model.step()

    _test_zero_static_scale(args)
429
430


Jeff Rasley's avatar
Jeff Rasley committed
431
432
433
434
435
436
437
438
439
440
@pytest.mark.parametrize('zero_stage, use_cpu_offload',
                         [
                             (1,
                              False),
                             (2,
                              False),
                             (2,
                              True),
                         ])
def test_zero_allow_untested_optimizer(tmpdir, zero_stage, use_cpu_offload):
441
    # if use_cpu_offload and not deepspeed.ops.__installed_ops__['cpu-adam']:
442
    #    pytest.skip("cpu-adam is not installed")
443
444
445
446
447
448
    config_dict = {
        "train_batch_size": 4,
        "steps_per_print": 1,
        "fp16": {
            "enabled": True,
        },
Jeff Rasley's avatar
Jeff Rasley committed
449
        "zero_optimization": {
Jeff Rasley's avatar
Jeff Rasley committed
450
451
            "stage": zero_stage,
            "cpu_offload": use_cpu_offload
Jeff Rasley's avatar
Jeff Rasley committed
452
        },
453
454
455
456
457
458
459
        "zero_allow_untested_optimizer": False
    }
    args = args_from_dict(tmpdir, config_dict)

    @distributed_test(world_size=[1])
    def _test_zero_allow_untested_optimizer(args):
        hidden_dim = 10
460
        model = SimpleModel(hidden_dim)
461
462
        optimizer = SimpleOptimizer(model.parameters())
        with pytest.raises(AssertionError):
463
464
465
466
            model, optim, _, _ = deepspeed.initialize(args=args,
                                                      model=model,
                                                      optimizer=optimizer,
                                                      model_parameters=model.parameters())
467
468

    _test_zero_allow_untested_optimizer(args)
469
470


Jeff Rasley's avatar
Jeff Rasley committed
471
472
473
474
475
476
477
478
479
480
@pytest.mark.parametrize('zero_stage, use_cpu_offload',
                         [
                             (1,
                              False),
                             (2,
                              False),
                             (2,
                              True),
                         ])
def test_zero_empty_partition(tmpdir, zero_stage, use_cpu_offload):
481
    # if use_cpu_offload and not deepspeed.ops.__installed_ops__['cpu-adam']:
482
    #    pytest.skip("cpu-adam is not installed")
483
484
485
486
487
488
489
490
491
492
493
494
495
496
    config_dict = {
        "train_micro_batch_size_per_gpu": 1,
        "gradient_accumulation_steps": 1,
        "fp16": {
            "enabled": True,
            "initial_scale_power": 8
        },
        "optimizer": {
            "type": "Adam",
            "params": {
                "lr": 0.00015
            }
        },
        "zero_optimization": {
Jeff Rasley's avatar
Jeff Rasley committed
497
            "stage": zero_stage,
Jeff Rasley's avatar
Jeff Rasley committed
498
499
500
            "cpu_offload": use_cpu_offload,
            "reduce_bucket_size": 100,
            "allgather_bucket_size": 100
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
        }
    }
    args = args_from_dict(tmpdir, config_dict)

    @distributed_test(world_size=[3])
    def _test_zero_empty_partition(args):
        hidden_dim = 1
        model = SimpleModel(hidden_dim)
        # Ensure model has 2 parameters, to cause empty partition with DP=3
        assert len(list(model.parameters())) == 2
        model, _, _, _ = deepspeed.initialize(args=args,
                                              model=model,
                                              model_parameters=model.parameters())

        # Now make sure things work..
        data_loader = random_dataloader(model=model,
                                        total_samples=1,
                                        hidden_dim=hidden_dim,
                                        device=model.device)
        for n, batch in enumerate(data_loader):
            loss = model(batch[0], batch[1])
            model.backward(loss)
            model.step()

    _test_zero_empty_partition(args)
526
527


528
@amp_available
529
530
531
532
533
def test_adam_amp_basic(tmpdir):
    config_dict = {"train_batch_size": 1, "steps_per_print": 1, "amp": {"enabled": True}}
    args = args_from_dict(tmpdir, config_dict)
    hidden_dim = 10

534
    model = SimpleModel(hidden_dim)
535
536
537
538

    @distributed_test(world_size=[1])
    def _test_adam_amp_basic(args, model, hidden_dim):
        optimizer = torch.optim.Adam(params=model.parameters())
539
540
541
        model, _, _, _ = deepspeed.initialize(args=args,
                                              model=model,
                                              optimizer=optimizer)
542
543
544
545
546
547
548
549
550
551
552
553
        data_loader = random_dataloader(model=model,
                                        total_samples=50,
                                        hidden_dim=hidden_dim,
                                        device=model.device)
        for n, batch in enumerate(data_loader):
            loss = model(batch[0], batch[1])
            model.backward(loss)
            model.step()

    _test_adam_amp_basic(args=args, model=model, hidden_dim=hidden_dim)


554
@amp_available
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
def test_lamb_amp_basic(tmpdir):
    config_dict = {
        "train_batch_size": 2,
        "steps_per_print": 1,
        "optimizer": {
            "type": "Lamb",
            "params": {
                "lr": 0.00015
            }
        },
        "gradient_clipping": 1.0,
        "amp": {
            "enabled": True,
        }
    }
    args = args_from_dict(tmpdir, config_dict)
    hidden_dim = 10

573
    model = SimpleModel(hidden_dim)
574
575
576

    @distributed_test(world_size=[1, 2])
    def _test_lamb_amp_basic(args, model, hidden_dim):
577
578
579
        model, _, _, _ = deepspeed.initialize(args=args,
                                              model=model,
                                              model_parameters=model.parameters())
580
581
582
583
584
585
586
587
588
589
590
591
        data_loader = random_dataloader(model=model,
                                        total_samples=50,
                                        hidden_dim=hidden_dim,
                                        device=model.device)
        for n, batch in enumerate(data_loader):
            loss = model(batch[0], batch[1])
            model.backward(loss)
            model.step()

    _test_lamb_amp_basic(args=args, model=model, hidden_dim=hidden_dim)


592
@amp_available
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
def test_adam_amp_o2(tmpdir):
    config_dict = {
        "train_batch_size": 2,
        "steps_per_print": 1,
        "optimizer": {
            "type": "Adam",
            "params": {
                "lr": 0.00015
            }
        },
        "gradient_clipping": 1.0,
        "amp": {
            "enabled": True,
            "opt_level": "O2"
        }
    }
    args = args_from_dict(tmpdir, config_dict)
    hidden_dim = 10

612
    model = SimpleModel(hidden_dim)
613
614
615

    @distributed_test(world_size=[1, 2])
    def _test_adam_amp_o2(args, model, hidden_dim):
616
617
618
        model, _, _, _ = deepspeed.initialize(args=args,
                                              model=model,
                                              model_parameters=model.parameters())
619
620
621
622
623
624
625
626
627
628
        data_loader = random_dataloader(model=model,
                                        total_samples=50,
                                        hidden_dim=hidden_dim,
                                        device=model.device)
        for n, batch in enumerate(data_loader):
            loss = model(batch[0], batch[1])
            model.backward(loss)
            model.step()

    _test_adam_amp_o2(args=args, model=model, hidden_dim=hidden_dim)
Jeff Rasley's avatar
Jeff Rasley committed
629
630


631
@amp_available
Jeff Rasley's avatar
Jeff Rasley committed
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
def test_adam_amp_o2_empty_grad(tmpdir):
    config_dict = {
        "train_batch_size": 2,
        "steps_per_print": 1,
        "optimizer": {
            "type": "Adam",
            "params": {
                "lr": 0.00015
            }
        },
        "gradient_clipping": 1.0,
        "amp": {
            "enabled": True,
            "opt_level": "O2"
        }
    }
    args = args_from_dict(tmpdir, config_dict)
    hidden_dim = 10

651
    model = SimpleModel(hidden_dim)
Jeff Rasley's avatar
Jeff Rasley committed
652
653
654

    @distributed_test(world_size=[2])
    def _test_adam_amp_o2_empty_grad(args, model, hidden_dim):
655
656
657
        model, _, _, _ = deepspeed.initialize(args=args,
                                              model=model,
                                              model_parameters=model.parameters())
Jeff Rasley's avatar
Jeff Rasley committed
658
659
660
661
662
663
664
665
666
667
        data_loader = random_dataloader(model=model,
                                        total_samples=50,
                                        hidden_dim=hidden_dim,
                                        device=model.device)
        for n, batch in enumerate(data_loader):
            loss = model(batch[0], batch[1])
            model.backward(loss)
            model.step()

    _test_adam_amp_o2_empty_grad(args=args, model=model, hidden_dim=hidden_dim)
Jeff Rasley's avatar
Jeff Rasley committed
668
669
670
671


@pytest.mark.parametrize('zero_stage, optimizer_constructor',
                         [(1,
672
                           FusedAdam),
Jeff Rasley's avatar
Jeff Rasley committed
673
674
675
                          (2,
                           torch.optim.Adam),
                          (2,
676
                           FusedAdam)])
Jeff Rasley's avatar
Jeff Rasley committed
677
678
679
680
681
682
683
684
685
686
687
688
689
690
def test_zero_supported_client_optimizer(tmpdir, zero_stage, optimizer_constructor):
    config_dict = {
        "train_batch_size": 2,
        "steps_per_print": 1,
        "fp16": {
            "enabled": True
        },
        "zero_optimization": {
            "stage": zero_stage
        }
    }
    args = args_from_dict(tmpdir, config_dict)
    hidden_dim = 10

691
    model = SimpleModel(hidden_dim)
Jeff Rasley's avatar
Jeff Rasley committed
692
693
694
695
696

    @distributed_test(world_size=[1])
    def _test_zero_supported_client_optimizer(args, model, optimizer_constructor):
        client_optimizer = optimizer_constructor(params=model.parameters())
        model, _, _, _ = deepspeed.initialize(args=args,
697
698
                                              model=model,
                                              optimizer=client_optimizer)
Jeff Rasley's avatar
Jeff Rasley committed
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730

    _test_zero_supported_client_optimizer(args=args,
                                          model=model,
                                          optimizer_constructor=optimizer_constructor)


def test_zero2_reduce_scatter_off(tmpdir):
    config_dict = {
        "train_batch_size": 2,
        "steps_per_print": 1,
        "optimizer": {
            "type": "Adam",
            "params": {
                "lr": 0.00015
            }
        },
        "gradient_clipping": 1.0,
        "zero_optimization": {
            "stage": 2,
            "contiguous_gradients": True,
            "allgather_bucket_size": 2000000000,
            "reduce_bucket_size": 200000000,
            "overlap_comm": False,
            "reduce_scatter": False
        },
        "fp16": {
            "enabled": True
        }
    }
    args = args_from_dict(tmpdir, config_dict)
    hidden_dim = 10

731
    model = SimpleModel(hidden_dim)
Jeff Rasley's avatar
Jeff Rasley committed
732
733
734

    @distributed_test(world_size=[2])
    def _helper(args, model, hidden_dim):
735
736
737
        model, _, _, _ = deepspeed.initialize(args=args,
                                              model=model,
                                              model_parameters=model.parameters())
Jeff Rasley's avatar
Jeff Rasley committed
738
739
740
741
742
743
744
745
746
747
        data_loader = random_dataloader(model=model,
                                        total_samples=50,
                                        hidden_dim=hidden_dim,
                                        device=model.device)
        for n, batch in enumerate(data_loader):
            loss = model(batch[0], batch[1])
            model.backward(loss)
            model.step()

    _helper(args=args, model=model, hidden_dim=hidden_dim)
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777


@pytest.mark.parametrize('adam_type, torch_impl',
                         [('Adam',
                           True),
                          ('Adam',
                           False),
                          ('AdamW',
                           True),
                          ('AdamW',
                           False)])
def test_fp16_adam_types(tmpdir, adam_type, torch_impl):
    config_dict = {
        "train_batch_size": 1,
        "steps_per_print": 1,
        "fp16": {
            "enabled": True,
            "initial_scale_power": 10
        },
        "optimizer": {
            "type": adam_type,
            "torch_adam": torch_impl,
            "params": {
                "lr": 0.00015
            }
        }
    }
    args = args_from_dict(tmpdir, config_dict)
    hidden_dim = 10

778
    model = SimpleModel(hidden_dim)
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797

    @distributed_test(world_size=[1])
    def _test_fp16_adam_types(args, model, hidden_dim):

        model, _, _, _ = deepspeed.initialize(args=args,
                                              model=model,
                                              model_parameters=model.parameters())

        data_loader = random_dataloader(model=model,
                                        total_samples=10,
                                        hidden_dim=hidden_dim,
                                        device=model.device)

        for _, batch in enumerate(data_loader):
            loss = model(batch[0], batch[1])
            model.backward(loss)
            model.step()

    _test_fp16_adam_types(args=args, model=model, hidden_dim=hidden_dim)