Skip to content

Zero-Offload Doubles VRAM Usage #467

Closed

Description

@anti4m

Hi,

I am testing out DeepSpeed on a single GTX 1070 GPU. Everything works fine until I try to enable "cpu_offload" in the config, which then doubles GPU memory usage from 4GB to 8GB. CPU memory usage increases significantly as expected, so it seems that the data is being copied successfully. If I also enable "overlap_comm," as recommended here, my system runs out of memory.

Here is my config file:
{ "train_batch_size": 96, "gradient_accumulation_steps": 1, "optimizer": { "type": "Adam", "params": { "lr": 0.00015 } }, "fp16": { "enabled": true }, "amp": { "enabled": false }, "gradient_clipping": 1.0, "zero_optimization": { "stage": 2, "cpu_offload": true, "contiguous_gradients": true, "overlap_comm": false } }

I am on Linux Mint 19 using PyTorch 1.7, CUDA 10.2, and latest version of DeepSpeed. Let me know if you need any more information. Thank you for your time.

Activity

  1. tjruwase commented on Oct 9, 2020

    @tjruwase
    Contributor

    @anti4m Thanks for using DeepSpeed. This is an interesting observation and I have a few questions.

    1. What is the model and number of parameters?

    2. What is the size of GPU RAM?

    3. Enabling cpu_offload is expected to reduce GPU RAM usage, so it is strange that toggling on/off doubles RAM usage. Are you using nvidia-smi to measure GPU RAM usage?

    4. Yes, overlap_comm trades off increase GPU RAM usage to lower all-reduce latency. One experiment to observe impact of overlap_comm is to reduce train_batch_size. Can you please try that?

  2. anti4m commented on Oct 9, 2020

    @anti4m
    Author
    1. I am using a custom transformer model similar to ALBERT with parameter-sharing between layers. There are only ~13 million parameters. If I disable parameter-sharing there are ~110 million parameters.

    2. 8GB

    3. Yes, I am using nvidia-smi.

    4. Even when reducing my batch size from 96 to 1, the memory used by overlap_comm exceeds the 8GB available on my GPU.

    Another interesting thing to note is that, if cpu_offload is enabled, manually instantiating the DeepSpeedCPUAdam optimizer and passing it to deepspeed.initialize() uses about 80% more CPU memory than allowing deepspeed.initialize() to automatically create the optimizer. VRAM usage is the same in either case.

  3. tjruwase commented on Oct 9, 2020

    @tjruwase
    Contributor

    Thanks for sharing these details, they are very helpful. ZeRO is targeted at multi-billion parameter models and so many of its intermediate buffers are very large (GBs) by default. The good news is that these buffers are configurable . The most relevant knobs are allgather_bucket_size and reduce_bucket_size. Both of these knobs have default values of 5e8, which translate to 1GB each (i.e., 5e8 x 2Bytes). Furthermore, for reduce_bucket_size, we double buffer when overlap_comm is enabled and currently have a hack that further scales it by 4.5 resulting in 9GB footprint by default (5e8 x 2Bytes x 2 x 4.5). So can you try reducing these two knobs in the json configuration by 2 or 3 orders of magnitude?

  4. anti4m commented on Oct 9, 2020

    @anti4m
    Author

    I reduced both buffers to 1e7 which solved my VRAM issue and allowed me to enable overlap_comm. Thank you for your help! However there is still the strange behavior that I noted at the end of my last comment. Manually instantiating DeepSpeedCPUAdam seems to give me ~20% higher samples/second while using about 7GB of additional CPU memory compared to just calling deepspeed.initialize(). The config is the same in both cases, and both have cpu_offload and overlap_comm enabled. This is the only difference in my code:

    opt = deepspeed.ops.adam.DeepSpeedCPUAdam(model.parameters(), lr=0.00015)
    model_engine, _, data_loader, _ = deepspeed.initialize(args=cmd_args, model=model, optimizer=opt, training_data=dataset)

    Versus

    model_engine, optimizer, data_loader, _ = deepspeed.initialize(args=cmd_args, model=model, model_parameters=model.parameters(), training_data=dataset)

  5. tjruwase commented on Oct 9, 2020

    @tjruwase
    Contributor

    Glad to hear that overlap_comm is now working for you.

    For the other issue, I think what is happening when you attempt to instantiating DeepSpeedCPUAdam through json is that you are actually instantiating torch.optim.Adam. You can inspect the log for this message to confirm. The reason this is happening is that in order to enable DeepspeedCPUAdam through json you need to set the type field to deepspeed_adam. We need to improve our documentation on this front, so thanks for finding this issue.

  6. anti4m commented on Oct 9, 2020

    @anti4m
    Author

    Yes, you are correct. Thank you. I am now closing the issue.

  7. szhengac commented on Dec 19, 2020

    @szhengac
    Contributor

    Hi @tjruwase, I faced the same issue that when I enable the cpu-offload, the GPU memory goes much higher and I cannot even use the same batch size I used before without cpu-offload. I have checked the log and the cpu optimizer is initialized.

      "zero_optimization": {
        "stage": 2,
        "overlap_comm": true,
        "cpu_offload": true
      },
      "zero_allow_untested_optimizer": true,
      "optimizer": {
        "type": "Adam",
        "params": {
          "lr": 1e-4,
          "weight_decay": 0.01,
          "bias_correction": true,
          "eps": 1e-6
        }
      }
    

    10.0.83.130: [2020-12-19 03:07:07,491] [INFO] [engine.py:716:_configure_zero_optimizer] Creating fp16 ZeRO stage 2 optimizer
    10.0.83.130: Checking ZeRO support for optimizer=DeepSpeedCPUAdam type=<class 'deepspeed.ops.adam.cpu_adam.DeepSpeedCPUAdam'>
    10.0.83.130: Adam Optimizer #0 is created with AVX512 arithmetic capability.
    10.0.83.130: Config: alpha=0.000100, betas=(0.900000, 0.999000), weight_decay=0.010000, adam_w=1

  8. tjruwase commented on Dec 20, 2020

    @tjruwase
    Contributor

    @szhengac, can you please the earlier discussion in this issue regarding internal buffer sizes of ZeRO. You can start from here. Please let us know whether or not the solutions there help your case. Thanks.

  9. szhengac commented on Dec 20, 2020

    @szhengac
    Contributor

    @tjruwase Thanks for your quick response. By reducing these two buffer sizes by 5x, the training can continue with zero-offload. However, I do not observe any memory reduction and the training is slower. And, why zero-offload uses more GPU buffer?

  10. tjruwase commented on Dec 21, 2020

    @tjruwase
    Contributor

    @szhengac Thanks for the update. ZeRO-Offload should not use more GPU memory, so something strange is happening. To help debug this issue, can you please provide the following?

    1. How big is your model?
    2. What is GPU RAM size?
    3. What is GPU RAM usage with and without zero-offload?
    4. How many GPUs are using?
    5. Is it possible to run with 1 GPU?
  11. szhengac commented on Dec 21, 2020

    @szhengac
    Contributor

    Hi @tjruwase,

    1. I am training a 10B model
    2. I am using A100 GPU, and its GPU RAM is 40GB
    3. They both hit on the maximum 39GB - 40GB.
    4. I am using 128 GPUs
    5. I didn't try 1 GPU.
  12. tjruwase commented on Dec 22, 2020

    @tjruwase
    Contributor

    @szhengac Thanks for providing these details.

    With zero-offload, a 10B model should easily train on one A100 GPU with 40GB. For sanity check, can you please report the memory usage for batch size 1 with 1 GPU using zero-offload?

  13. stas00 commented on Dec 22, 2020

    @stas00
    Collaborator

    This discussion was very helpful, @tjruwase

    I just have a brief suggestion:

    So can you try reducing these two knobs in the json configuration by 2 or 3 orders of magnitude?

    You must have meant by 2-3 times, and not orders of magnitude.

    We are trying to reduce 9GB to fit into 8GB, not into 0.08GB ;)

  14. stas00 commented on Dec 22, 2020

    @stas00
    Collaborator

    This is the summary I saved away from this discussion:

    ZeRO features that decrease gpu memory usage

    • CPU offload

      • zero_optimization.cpu_offload=true, requires zero_optimization.stage=2
      • cpu_offload should reduce GPU RAM usage,

    ZeRO features that increase gpu memory usage

    • zero_optimization.allgather_bucket_size and zero_optimization.reduce_bucket_size have the biggest impact on memory usage during zero_optimization.stage=2

      both default to 500000000 => 1GB buffer (5e8 x 2Bytes)

    • Overlap comm - increases gpu memory requirements

      • zero_optimization.overlap_comm=true trades off increase GPU RAM usage to lower all-reduce latency.

      • overlap_comm uses 4.5x the zero_optimization.allgather_bucket_size and zero_optimization.reduce_bucket_size resulting in 9GB footprint by default (5e8 x 2Bytes x 2 x 4.5), so for example this 2.5x change in buffer size may be a sufficient reduction:

        "allgather_bucket_size": 200000000,
        "reduce_bucket_size": 200000000,

  15. tjruwase commented on Dec 22, 2020

    @tjruwase
    Contributor

    @stas00 Thanks for the nice summary of my discussion and catching my typo as well :). @szhengac, I hope you find this summary useful as well.

    Hopefully, we can work together to enable the maximum benefits of zero-offload for your models. So my suggestion is that we start with small configuration to nail down the expected memory usage by running with batch size = 1 on 1 GPU.

  16. stas00 commented on Dec 22, 2020

    @stas00
    Collaborator

    At this point I'm almost done integrating deepspeed into transformers:
    huggingface/transformers#9211
    so my main goal at the moment is to test various configurations and ensure that they all work.

    Then we can start on sorting out which config to use when.

  17. szhengac commented on Dec 24, 2020

    @szhengac
    Contributor

    I will revisit this after I figure out another issue.

  18. self-assigned this
    on Sep 8, 2023
  19. xiexbing commented on Sep 14, 2023

    @xiexbing
    Contributor

    the software issues are solved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions