DeepSpeed icon indicating copy to clipboard operation
DeepSpeed copied to clipboard

OOM when turn on zero2 but not enabled

Open zengjice opened this issue 3 years ago • 0 comments

Describe the bug I have 2*2 T4-15GB GPU. When I am not using zero optimization, the task is running and the batch_size is 39 per gpu, here is the config:

{
    "fp16": {
        "enabled": "auto",
        "loss_scale": 0,
        "loss_scale_window": 1000,
        "initial_scale_power": 16,
        "hysteresis": 2,
        "min_loss_scale": 1
    },
    "optimizer": {
        "type": "AdamW",
        "params": {
            "lr": "auto",
            "betas": "auto",
            "eps": "auto",
            "weight_decay": "auto",
            "torch_adam": true
        }
    },
    "scheduler": {
        "type": "WarmupLR",
        "params": {
            "warmup_min_lr": "auto",
            "warmup_max_lr": "auto",
            "warmup_num_steps": "auto"
        }
    },
    "gradient_clipping": "auto",
    "gradient_accumulation_steps": "auto",
    "train_batch_size": 156,
    "train_micro_batch_size_per_gpu": 39,
    "wall_clock_breakdown": true
}

And then I want to turn on zero2 optimization in order to increase batchsize, but an oom error was reported when batch_size was also set to 39, here is the config:

{
    "fp16": {
        "enabled": "auto",
        "loss_scale": 0,
        "loss_scale_window": 1000,
        "initial_scale_power": 16,
        "hysteresis": 2,
        "min_loss_scale": 1
    },
    "optimizer": {
        "type": "AdamW",
        "params": {
            "lr": "auto",
            "betas": "auto",
            "eps": "auto",
            "weight_decay": "auto",
            "torch_adam": true
        }
    },
    "scheduler": {
        "type": "WarmupLR",
        "params": {
            "warmup_min_lr": "auto",
            "warmup_max_lr": "auto",
            "warmup_num_steps": "auto"
        }
    },
    "zero_optimization": {
        "stage": 2,
        "offload_optimizer": {
            "device": "cpu",
            "pin_memory": true
        },
        "contiguous_gradients": true,
        "overlap_comm": true,
        "allgather_partitions": true,
        "reduce_scatter": true,
        "allgather_bucket_size": 9e6,
        "reduce_bucket_size": 9e6
    },
    "gradient_clipping": "auto",
    "gradient_accumulation_steps": "auto",
    "train_batch_size": 156,
    "train_micro_batch_size_per_gpu": 39,
    "wall_clock_breakdown": true
}

and the error trace:

t4-50: Traceback (most recent call last):
t4-50:   File "/disk/testsys/testsys/run_mlm.py", line 613, in <module>
t4-50:     main()
t4-50:   File "/disk/testsys/testsys/run_mlm.py", line 562, in main
t4-50:     train_result = trainer.train(resume_from_checkpoint=checkpoint)
t4-50:   File "/disk/testsys/miniconda3/envs/testsys-0.1/lib/python3.10/site-packages/transformers/trainer.py", line 1543, in train
t4-50:     return inner_training_loop(
t4-50:   File "/disk/testsys/miniconda3/envs/testsys-0.1/lib/python3.10/site-packages/transformers/trainer.py", line 1791, in _inner_training_loop
t4-50:     tr_loss_step = self.training_step(model, inputs)
t4-50:   File "/disk/testsys/miniconda3/envs/testsys-0.1/lib/python3.10/site-packages/transformers/trainer.py", line 2555, in training_step
t4-50:     loss = self.deepspeed.backward(loss)
t4-50:   File "/disk/testsys/miniconda3/envs/testsys-0.1/lib/python3.10/site-packages/deepspeed/utils/nvtx.py", line 9, in wrapped_fn
t4-50:     ret_val = func(*args, **kwargs)
t4-50:   File "/disk/testsys/miniconda3/envs/testsys-0.1/lib/python3.10/site-packages/deepspeed/runtime/engine.py", line 1968, in backward
t4-50:     self.optimizer.backward(loss, retain_graph=retain_graph)
t4-50:   File "/disk/testsys/miniconda3/envs/testsys-0.1/lib/python3.10/site-packages/deepspeed/runtime/zero/stage_1_and_2.py", line 2017, in backward
t4-50:     self.loss_scaler.backward(loss.float(), retain_graph=retain_graph)
t4-50:   File "/disk/testsys/miniconda3/envs/testsys-0.1/lib/python3.10/site-packages/deepspeed/runtime/fp16/loss_scaler.py", line 51, in backward
t4-50:     scaled_loss.backward(retain_graph=retain_graph)
t4-50:   File "/disk/testsys/miniconda3/envs/testsys-0.1/lib/python3.10/site-packages/torch/_tensor.py", line 396, in backward
t4-50:     torch.autograd.backward(self, gradient, retain_graph, create_graph, inputs=inputs)
t4-50:   File "/disk/testsys/miniconda3/envs/testsys-0.1/lib/python3.10/site-packages/torch/autograd/__init__.py", line 173, in backward
t4-50:     Variable._execution_engine.run_backward(  # Calls into the C++ engine to run the backward pass
t4-50: RuntimeError: CUDA out of memory. Tried to allocate 1.14 GiB (GPU 0; 14.76 GiB total capacity; 13.00 GiB already allocated; 1.07 GiB free; 13.02 GiB reserved in total by PyTorch) If reserved memory is >> allocated memory try setting max_split_size_mb to avoid fragmentation.  See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF

And then I try to make reduce_bucket_size and allgather_bucket_size smaller, such as 1e1, and there's another error:

t4-50: Traceback (most recent call last):
t4-50:   File "/disk/testsys/testsys/run_mlm.py", line 613, in <module>
t4-50:     main()
t4-50:   File "/disk/testsys/testsys/run_mlm.py", line 562, in main
t4-50:     train_result = trainer.train(resume_from_checkpoint=checkpoint)
t4-50:   File "/disk/testsys/miniconda3/envs/testsys-0.1/lib/python3.10/site-packages/transformers/trainer.py", line 1543, in train
t4-50:     return inner_training_loop(
t4-50:   File "/disk/testsys/miniconda3/envs/testsys-0.1/lib/python3.10/site-packages/transformers/trainer.py", line 1791, in _inner_training_loop
t4-50:     tr_loss_step = self.training_step(model, inputs)
t4-50:   File "/disk/testsys/miniconda3/envs/testsys-0.1/lib/python3.10/site-packages/transformers/trainer.py", line 2555, in training_step
t4-50:     loss = self.deepspeed.backward(loss)
t4-50:   File "/disk/testsys/miniconda3/envs/testsys-0.1/lib/python3.10/site-packages/deepspeed/utils/nvtx.py", line 9, in wrapped_fn
t4-50:     ret_val = func(*args, **kwargs)
t4-50:   File "/disk/testsys/miniconda3/envs/testsys-0.1/lib/python3.10/site-packages/deepspeed/runtime/engine.py", line 1968, in backward
t4-50:     self.optimizer.backward(loss, retain_graph=retain_graph)
t4-50:   File "/disk/testsys/miniconda3/envs/testsys-0.1/lib/python3.10/site-packages/deepspeed/runtime/zero/stage_1_and_2.py", line 2017, in backward
t4-50:     self.loss_scaler.backward(loss.float(), retain_graph=retain_graph)
t4-50:   File "/disk/testsys/miniconda3/envs/testsys-0.1/lib/python3.10/site-packages/deepspeed/runtime/fp16/loss_scaler.py", line 51, in backward
t4-50:     scaled_loss.backward(retain_graph=retain_graph)
t4-50:   File "/disk/testsys/miniconda3/envs/testsys-0.1/lib/python3.10/site-packages/torch/_tensor.py", line 396, in backward
t4-50:     torch.autograd.backward(self, gradient, retain_graph, create_graph, inputs=inputs)
t4-50:   File "/disk/testsys/miniconda3/envs/testsys-0.1/lib/python3.10/site-packages/torch/autograd/__init__.py", line 173, in backward
t4-50:     Variable._execution_engine.run_backward(  # Calls into the C++ engine to run the backward pass
t4-50: RuntimeError: CUDA error: CUBLAS_STATUS_ALLOC_FAILED when calling `cublasCreate(handle)`

Shouldn't it be possible to run a larger batch_size after zero optimization?Thanks

zengjice avatar Mar 10 '23 02:03 zengjice