Skip to content

[PyTorch] Deferred Initialization via device='meta' option - #596

Merged
denera merged 10 commits into
NVIDIA:mainfrom
denera:databricks/fp8-fsdp-oom-fix
Jan 17, 2024
Merged

denera merged 10 commits into
NVIDIA:mainfrom
denera:databricks/fp8-fsdp-oom-fix

Conversation

@denera

@denera denera commented Jan 8, 2024 •

Copy link
Copy Markdown
Collaborator

This is primarily meant to support training very large models in torch.distributed.fsdp.FullyShardedDataParallel mode.

FSDP execution flow requires models to be initialized in serial mode before being passed into an FSDP wrapper that shards their parameters. Without device='meta' support, this materializes a copy of every module in memory for every host/device and causes OOM errors for large models. Deferring initialization with device='meta' materializes the memory only when the FSDP wrapper recursively executes module.reset_parameters() on already sharded modules, thus avoiding unsharded duplicates.

Databricks has requested us to accelerate upstreaming this feature for te.Linear in support of their immediate work instead of waiting to upstream it until the same feature is implemented for other TransformerEngine modules. I will be filing additional PRs to extend this feature to other modules in the coming days/weeks.

The PR also includes an example for demonstrating device='meta' use with FSDP. I am planning to convert this into a distributed/FSDP regression test once the meta device feature is implemented for other TE modules.

Edit: Extending this to all TE modules turned out to be easy enough to roll into this PR instead postponing to a follow-up.

@denera denera added the enhancement New feature or request label Jan 8, 2024
@denera denera self-assigned this Jan 8, 2024
@denera
denera force-pushed the databricks/fp8-fsdp-oom-fix branch from 908f1a5 to 70a90a3 Compare January 8, 2024 17:55
@denera

denera commented Jan 8, 2024

Copy link
Copy Markdown
Collaborator Author

/te-ci pytorch

@denera
denera removed the request for review from cyanguwa January 8, 2024 17:58
@denera
denera force-pushed the databricks/fp8-fsdp-oom-fix branch from 0ed0550 to a12bb2b Compare January 9, 2024 20:01
@denera

denera commented Jan 9, 2024

Copy link
Copy Markdown
Collaborator Author

/te-ci pytorch

Comment thread examples/pytorch/fsdp/README.md Outdated
Comment thread transformer_engine/pytorch/module/linear.py Outdated
Comment thread examples/pytorch/fsdp/fsdp.py Outdated
@denera

denera commented Jan 10, 2024

Copy link
Copy Markdown
Collaborator Author

/te-ci pytorch

Comment thread transformer_engine/pytorch/module/linear.py Outdated
Comment thread transformer_engine/pytorch/module/linear.py Outdated
Comment thread examples/pytorch/fsdp/README.md Outdated
@denera
denera force-pushed the databricks/fp8-fsdp-oom-fix branch from cb055e5 to 05b0e12 Compare January 12, 2024 18:17
@denera

denera commented Jan 12, 2024

Copy link
Copy Markdown
Collaborator Author

/te-ci pytorch

@denera
denera requested review from ksivaman and ptrendx January 12, 2024 18:22
….Linear and added new PyTorch example to demonstrate its use with FullyShardedDataParallel execution.

Signed-off-by: Alp Dener <adener@nvidia.com>
Signed-off-by: Alp Dener <adener@nvidia.com>
Signed-off-by: Alp Dener <adener@nvidia.com>
Signed-off-by: Alp Dener <adener@nvidia.com>
Signed-off-by: Alp Dener <adener@nvidia.com>
…ally

Signed-off-by: Alp Dener <adener@nvidia.com>
Signed-off-by: Alp Dener <adener@nvidia.com>
@denera
denera force-pushed the databricks/fp8-fsdp-oom-fix branch from 05b0e12 to 1cc1be6 Compare January 12, 2024 22:43
@denera

denera commented Jan 12, 2024

Copy link
Copy Markdown
Collaborator Author

/te-ci pytorch

@denera denera changed the title [PyTorch] Deferred Initialization for te.Linear via device='meta' option [PyTorch] Deferred Initialization via device='meta' option Jan 12, 2024
@denera

denera commented Jan 12, 2024

Copy link
Copy Markdown
Collaborator Author

@ksivaman @ptrendx Extending this to all TE modules turned out to be easy enough to do in this PR instead of splitting it into multiple. Pushed the update now, monitoring the CI for failures. Please let me know what you think.

Signed-off-by: Alp Dener <adener@nvidia.com>
@denera

denera commented Jan 16, 2024

Copy link
Copy Markdown
Collaborator Author

/te-ci pytorch

@ptrendx ptrendx added the 1.3.0 label Jan 16, 2024
Comment thread transformer_engine/pytorch/module/_common.py Outdated
Comment thread transformer_engine/pytorch/module/base.py

@ksivaman ksivaman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

…d clarifying comments in parameter reset

Signed-off-by: Alp Dener <adener@nvidia.com>
@denera
denera merged commit 434d58f into NVIDIA:main Jan 17, 2024
Marks101 added a commit to Marks101/TransformerEngine that referenced this pull request Jan 22, 2024
Signed-off-by: Markus Schnoes <markus.schnoes@gmx.de>
ptrendx pushed a commit that referenced this pull request Jan 22, 2024
* Implemented deferred initialization via `device='meta'` option for te.Linear and added new PyTorch example to demonstrate its use with FullyShardedDataParallel execution.

Signed-off-by: Alp Dener <adener@nvidia.com>

* correcting Float8Tensor initialization and fixing linting errors

Signed-off-by: Alp Dener <adener@nvidia.com>

* removed duplicate code from upstream rebase, local tests passing

Signed-off-by: Alp Dener <adener@nvidia.com>

* improved comments/documentation for FSDP example

Signed-off-by: Alp Dener <adener@nvidia.com>

* converted reset_parameters() into a base module function

Signed-off-by: Alp Dener <adener@nvidia.com>

* fixed Float8Tensor creation with deferred init, all tests passing locally

Signed-off-by: Alp Dener <adener@nvidia.com>

* extended deferred initialization to all TE modules

Signed-off-by: Alp Dener <adener@nvidia.com>

* fixed linting errors

Signed-off-by: Alp Dener <adener@nvidia.com>

* removed unnecessary reference to the parent module of parameter, added clarifying comments in parameter reset

Signed-off-by: Alp Dener <adener@nvidia.com>

---------

Signed-off-by: Alp Dener <adener@nvidia.com>
ksivaman pushed a commit that referenced this pull request Jan 22, 2024
Signed-off-by: Markus Schnoes <markus.schnoes@gmx.de>
ptrendx pushed a commit that referenced this pull request Jan 22, 2024
Signed-off-by: Markus Schnoes <markus.schnoes@gmx.de>
@denera denera mentioned this pull request Jan 24, 2024
xiaoteng-cui-mt pushed a commit to MooreThreads/MT-TransformerEngine that referenced this pull request Oct 9, 2026
)

* Implemented deferred initialization via `device='meta'` option for te.Linear and added new PyTorch example to demonstrate its use with FullyShardedDataParallel execution.

Signed-off-by: Alp Dener <adener@nvidia.com>

* correcting Float8Tensor initialization and fixing linting errors

Signed-off-by: Alp Dener <adener@nvidia.com>

* removed duplicate code from upstream rebase, local tests passing

Signed-off-by: Alp Dener <adener@nvidia.com>

* improved comments/documentation for FSDP example

Signed-off-by: Alp Dener <adener@nvidia.com>

* converted reset_parameters() into a base module function

Signed-off-by: Alp Dener <adener@nvidia.com>

* fixed Float8Tensor creation with deferred init, all tests passing locally

Signed-off-by: Alp Dener <adener@nvidia.com>

* extended deferred initialization to all TE modules

Signed-off-by: Alp Dener <adener@nvidia.com>

* fixed linting errors

Signed-off-by: Alp Dener <adener@nvidia.com>

* removed unnecessary reference to the parent module of parameter, added clarifying comments in parameter reset

Signed-off-by: Alp Dener <adener@nvidia.com>

---------

Signed-off-by: Alp Dener <adener@nvidia.com>
xiaoteng-cui-mt pushed a commit to MooreThreads/MT-TransformerEngine that referenced this pull request Oct 9, 2026
Signed-off-by: Markus Schnoes <markus.schnoes@gmx.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

1.3.0 enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants