Skip to content

Conversation

@SunMarc
Copy link
Member

@SunMarc SunMarc commented Mar 24, 2025

What does this PR do?

This PR fixes cache allocator function when querying the max-memory available on cuda devices. Currently, it doesn't work for torch <= 2.4.1 due to torch.cuda.mem_get_info raising an issue if we pass a device without an index.

Traceback returned for a loading a model like gpt2 with device_map="cuda"

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/marc/transformers/src/transformers/models/auto/auto_factory.py", line 573, in from_pretrained
    return model_class.from_pretrained(
  File "/home/marc/transformers/src/transformers/modeling_utils.py", line 272, in _wrapper
    return func(*args, **kwargs)
  File "/home/marc/transformers/src/transformers/modeling_utils.py", line 4474, in from_pretrained
    ) = cls._load_pretrained_model(
  File "/home/marc/transformers/src/transformers/modeling_utils.py", line 4859, in _load_pretrained_model
    caching_allocator_warmup(model_to_load, expanded_device_map)
  File "/home/marc/transformers/src/transformers/modeling_utils.py", line 5870, in caching_allocator_warmup
    index = device.index if device.index is not None else 0
  File "/home/marc/anaconda3/envs/llama-export/lib/python3.10/site-packages/torch/cuda/memory.py", line 684, in mem_get_info
    device = _get_device_index(device)
  File "/home/marc/anaconda3/envs/llama-export/lib/python3.10/site-packages/torch/cuda/_utils.py", line 38, in _get_device_index
    return _torch_get_device_index(device, optional, allow_cpu)
  File "/home/marc/anaconda3/envs/llama-export/lib/python3.10/site-packages/torch/_utils.py", line 803, in _get_device_index
    raise ValueError(
ValueError: Expected a torch.device with a specified index or an integer, but got:cuda

cc @winglian

@github-actions
Copy link
Contributor

Hi 👋, thank you for opening this pull request! The pull request is converted to draft by default. The CI will be paused while the PR is in draft mode. When it is ready for review, please click the Ready for review button (at the bottom of the PR page). This will assign reviewers.

@github-actions github-actions bot marked this pull request as draft March 24, 2025 16:27
@SunMarc SunMarc requested a review from Cyrilvallez March 24, 2025 16:27
@SunMarc SunMarc marked this pull request as ready for review March 24, 2025 16:27
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Member

@Cyrilvallez Cyrilvallez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for fixing!

@SunMarc SunMarc merged commit 80b4c5d into main Mar 25, 2025
24 checks passed
@SunMarc SunMarc deleted the fix-index branch March 25, 2025 10:51
zucchini-nlp pushed a commit to zucchini-nlp/transformers that referenced this pull request May 14, 2025
soghomon-b pushed a commit to soghomon-b/transformers that referenced this pull request Aug 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants