Skip to content

[Bug] Excessive Memory Usage for Phi-3-Mini #559

@riedgar-ms

Description

@riedgar-ms

I'm attempting to use deepspeed-mii to run parallel inferencing with Phi-3-Mini, based on the doc sample. My code is:

    pipe = pipeline("microsoft/Phi-3-mini-4k-instruct")
    output = pipe("Tell me about yourself", max_new_tokens=2048)
    print(output)

I can run this on 4xA100 GPUs, but each has almost its entire memory used. If I try using just 2xA100s, then I end up with a CUDA OoM:

4c0c211e77f3413c9323303eaadddde4000000:451:1290 [1] NCCL INFO TUNER/Plugin: Failed to find ncclTunerPlugin_v2, using internal tuner instead.
4c0c211e77f3413c9323303eaadddde4000000:451:1290 [1] NCCL INFO ncclCommInitRank comm 0x27667a40 rank 1 nranks 2 cudaDev 1 nvmlDev 1 busId 200000 commId 0x6c0124234d02ef09 - Init COMPLETE
4c0c211e77f3413c9323303eaadddde4000000:450:1288 [0] NCCL INFO TUNER/Plugin: Failed to find ncclTunerPlugin_v2, using internal tuner instead.
4c0c211e77f3413c9323303eaadddde4000000:450:1288 [0] NCCL INFO ncclCommInitRank comm 0xe079d10 rank 0 nranks 2 cudaDev 0 nvmlDev 0 busId 100000 commId 0x6c0124234d02ef09 - Init COMPLETE
[2025-03-13 18:29:53,201] [INFO] [kv_cache.py:135:__init__] Allocating KV-cache 0 with shape: (32, 6352, 64, 2, 16, 96) consisting of 6352 blocks.
[2025-03-13 18:29:53,201] [INFO] [kv_cache.py:135:__init__] Allocating KV-cache 0 with shape: (32, 6352, 64, 2, 16, 96) consisting of 6352 blocks.
[rank0]: Traceback (most recent call last):
[rank0]:   File "/mnt/azureml/cr/j/2d4bdb9e62604894b76a4726f8f056c1/exe/wd/./speed_test.py", line 68, in <module>
[rank0]:     main()
[rank0]:   File "/mnt/azureml/cr/j/2d4bdb9e62604894b76a4726f8f056c1/exe/wd/./speed_test.py", line 38, in main
[rank0]:     pipe = pipeline("microsoft/Phi-3-mini-4k-instruct")
[rank0]:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[rank0]:   File "/azureml-envs/azureml_af2302dc5f1b8b98e07fefa1789fbe91/lib/python3.12/site-packages/mii/api.py", line 231, in pipeline
[rank0]:     inference_engine = load_model(model_config)
[rank0]:                        ^^^^^^^^^^^^^^^^^^^^^^^^
[rank0]:   File "/azureml-envs/azureml_af2302dc5f1b8b98e07fefa1789fbe91/lib/python3.12/site-packages/mii/modeling/models.py", line 17, in load_model
[rank0]:     inference_engine = build_hf_engine(
[rank0]:                        ^^^^^^^^^^^^^^^^
[rank0]:   File "/azureml-envs/azureml_af2302dc5f1b8b98e07fefa1789fbe91/lib/python3.12/site-packages/deepspeed/inference/v2/engine_factory.py", line 135, in build_hf_engine
[rank0]:     return InferenceEngineV2(policy, engine_config)
[rank0]:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[rank0]:   File "/azureml-envs/azureml_af2302dc5f1b8b98e07fefa1789fbe91/lib/python3.12/site-packages/deepspeed/inference/v2/engine_v2.py", line 88, in __init__
[rank0]:     self._state_manager = DSStateManager(self._config.state_manager,
[rank0]:                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[rank0]:   File "/azureml-envs/azureml_af2302dc5f1b8b98e07fefa1789fbe91/lib/python3.12/site-packages/deepspeed/inference/v2/ragged/ragged_manager.py", line 95, in __init__
[rank0]:     self._kv_cache = BlockedKVCache(self._kv_configs,
[rank0]:                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[rank0]:   File "/azureml-envs/azureml_af2302dc5f1b8b98e07fefa1789fbe91/lib/python3.12/site-packages/deepspeed/inference/v2/ragged/kv_cache.py", line 137, in __init__
[rank0]:     caches.append(torch.empty(alloc_shape, dtype=config.cache_dtype,
[rank0]:                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[rank0]: torch.OutOfMemoryError: CUDA out of memory. Tried to allocate 74.44 GiB. GPU 0 has a total capacity of 79.15 GiB of which 74.01 GiB is free. Process 17190 has 5.13 GiB memory in use. Of the allocated memory 3.78 GiB is allocated by PyTorch, and 10.27 MiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True to avoid fragmentation.  See documentation for Memory Management  (https://pytorch.org/docs/stable/notes/cuda.html#environment-variables)
[rank1]:

This memory consumption is beyond excessive - Phi3-Mini can run on a single GPU, taking about 5 GiB of RAM.

Output of ds_report :

[2025-03-13 18:26:51,736] [INFO] [real_accelerator.py:222:get_accelerator] Setting ds_accelerator to cuda (auto detect)
df: /root/.triton/autotune: No such file or directory
/azureml-envs/azureml_af2302dc5f1b8b98e07fefa1789fbe91/compiler_compat/ld: warning: libm.so.6, needed by /usr/local/cuda/lib64/libcufile.so, not found (try using -rpath or -rpath-link)
/azureml-envs/azureml_af2302dc5f1b8b98e07fefa1789fbe91/compiler_compat/ld: /usr/local/cuda/lib64/libcufile.so: undefined reference to `log2f@GLIBC_2.2.5'
/azureml-envs/azureml_af2302dc5f1b8b98e07fefa1789fbe91/compiler_compat/ld: /azureml-envs/azureml_af2302dc5f1b8b98e07fefa1789fbe91/lib/libstdc++.so.6: undefined reference to `fesetround@GLIBC_2.2.5'
/azureml-envs/azureml_af2302dc5f1b8b98e07fefa1789fbe91/compiler_compat/ld: /usr/local/cuda/lib64/libcufile.so: undefined reference to `dlopen'
/azureml-envs/azureml_af2302dc5f1b8b98e07fefa1789fbe91/compiler_compat/ld: /azureml-envs/azureml_af2302dc5f1b8b98e07fefa1789fbe91/lib/libstdc++.so.6: undefined reference to `fegetround@GLIBC_2.2.5'
/azureml-envs/azureml_af2302dc5f1b8b98e07fefa1789fbe91/compiler_compat/ld: /usr/local/cuda/lib64/libcufile.so: undefined reference to `dlclose'
/azureml-envs/azureml_af2302dc5f1b8b98e07fefa1789fbe91/compiler_compat/ld: /usr/local/cuda/lib64/libcufile.so: undefined reference to `dlerror'
/azureml-envs/azureml_af2302dc5f1b8b98e07fefa1789fbe91/compiler_compat/ld: /usr/local/cuda/lib64/libcufile.so: undefined reference to `dlsym'
collect2: error: ld returned 1 exit status
--------------------------------------------------
DeepSpeed C++/CUDA extension op report
--------------------------------------------------
NOTE: Ops not installed will be just-in-time (JIT) compiled at
      runtime if needed. Op compatibility means that your system
      meet the required dependencies to JIT install the op.
--------------------------------------------------
JIT compiled ops requires ninja
ninja .................. �[92m[OKAY]�[0m
--------------------------------------------------
op name ................ installed .. compatible
--------------------------------------------------
async_io ............... �[93m[NO]�[0m ....... �[92m[OKAY]�[0m
fused_adam ............. �[93m[NO]�[0m ....... �[92m[OKAY]�[0m
cpu_adam ............... �[93m[NO]�[0m ....... �[92m[OKAY]�[0m
cpu_adagrad ............ �[93m[NO]�[0m ....... �[92m[OKAY]�[0m
cpu_lion ............... �[93m[NO]�[0m ....... �[92m[OKAY]�[0m
�[93m [WARNING] �[0m Please specify the CUTLASS repo directory as environment variable $CUTLASS_PATH
evoformer_attn ......... �[93m[NO]�[0m ....... �[93m[NO]�[0m
�[93m [WARNING] �[0m FP Quantizer is using an untested triton version (3.2.0), only 2.3.(0, 1) and 3.0.0 are known to be compatible with these kernels
fp_quantizer ........... �[93m[NO]�[0m ....... �[93m[NO]�[0m
fused_lamb ............. �[93m[NO]�[0m ....... �[92m[OKAY]�[0m
fused_lion ............. �[93m[NO]�[0m ....... �[92m[OKAY]�[0m
gds .................... �[93m[NO]�[0m ....... �[93m[NO]�[0m
transformer_inference .. �[93m[NO]�[0m ....... �[92m[OKAY]�[0m
inference_core_ops ..... �[93m[NO]�[0m ....... �[92m[OKAY]�[0m
cutlass_ops ............ �[93m[NO]�[0m ....... �[92m[OKAY]�[0m
quantizer .............. �[93m[NO]�[0m ....... �[92m[OKAY]�[0m
ragged_device_ops ...... �[93m[NO]�[0m ....... �[92m[OKAY]�[0m
ragged_ops ............. �[93m[NO]�[0m ....... �[92m[OKAY]�[0m
random_ltd ............. �[93m[NO]�[0m ....... �[92m[OKAY]�[0m
�[93m [WARNING] �[0m sparse_attn requires a torch version >= 1.5 and < 2.0 but detected 2.6
�[93m [WARNING] �[0m using untested triton version (3.2.0), only 1.0.0 is known to be compatible
sparse_attn ............ �[93m[NO]�[0m ....... �[93m[NO]�[0m
spatial_inference ...... �[93m[NO]�[0m ....... �[92m[OKAY]�[0m
transformer ............ �[93m[NO]�[0m ....... �[92m[OKAY]�[0m
stochastic_transformer . �[93m[NO]�[0m ....... �[92m[OKAY]�[0m
--------------------------------------------------
DeepSpeed general environment info:
torch install path ............... ['/azureml-envs/azureml_af2302dc5f1b8b98e07fefa1789fbe91/lib/python3.12/site-packages/torch']
torch version .................... 2.6.0+cu124
deepspeed install path ........... ['/azureml-envs/azureml_af2302dc5f1b8b98e07fefa1789fbe91/lib/python3.12/site-packages/deepspeed']
deepspeed info ................... 0.16.4, unknown, unknown
torch cuda version ............... 12.4
torch hip version ................ None
nvcc version ..................... 12.1
deepspeed wheel compiled w. ...... torch 0.0, cuda 0.0
shared memory (/dev/shm) size .... 2.00 GB

Metadata

Metadata

Assignees

No one assigned

    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