Skip to content

Fix TP initialization#35860

Merged
Cyrilvallez merged 11 commits intomainfrom
fix-tp
Jan 28, 2025
Merged

Fix TP initialization#35860
Cyrilvallez merged 11 commits intomainfrom
fix-tp

Conversation

@Cyrilvallez
Copy link
Member

@Cyrilvallez Cyrilvallez commented Jan 23, 2025

What does this PR do?

As per the title. When TP was introduced in #34184, it used a context manager with torch.device to set the model device. However, the context manager actually does NOT put the tensor on the device explicitly (it simply changes the default device if using name without indices, such as "cuda" or .cuda()). This means that the model was still on CPU.

Then parallelize_module would automatically switch the model, however it uses the SAME gpu for all child processes. So it's fine for small models, but will OOM for larger ones (e.g. Llama 8B on 4x L4 GPU (24 GB) would OOM, even though it's supposed to take ~16 GB only).

It does not look like we can instantiate only the model shards from the CPU to the different devices to avoid loading the full model on each device at the start.
To avoid that, I believe we could instantiate model on meta device, then recursively loading and dispatching each layer, which would avoif loading the full model in RAM then dispatching which goes against parallelization idea. Will try to explore it/see with accelerate team how we can do it.

Copy link
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

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

NIce I remember needing a special version of torch for this?
Also let's add a test (the current test does not cover this case)

@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.

@Cyrilvallez Cyrilvallez merged commit f48ecd7 into main Jan 28, 2025
5 checks passed
@Cyrilvallez Cyrilvallez deleted the fix-tp branch January 28, 2025 14:07
bursteratom pushed a commit to bursteratom/transformers that referenced this pull request Jan 31, 2025
* fix tp

* Update modeling_utils.py

* style

* style

* Update test_tp.py

* Update test_tp.py

* style

* Update test_tp.py

* Update test_tp.py

* Update test_tp.py

* Update test_tp.py
elvircrn pushed a commit to elvircrn/transformers that referenced this pull request Feb 13, 2025
* fix tp

* Update modeling_utils.py

* style

* style

* Update test_tp.py

* Update test_tp.py

* style

* Update test_tp.py

* Update test_tp.py

* Update test_tp.py

* Update test_tp.py
sbucaille pushed a commit to sbucaille/transformers that referenced this pull request Feb 16, 2025
* fix tp

* Update modeling_utils.py

* style

* style

* Update test_tp.py

* Update test_tp.py

* style

* Update test_tp.py

* Update test_tp.py

* Update test_tp.py

* Update test_tp.py
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.

3 participants