-
Notifications
You must be signed in to change notification settings - Fork 797
Optimize FSDP2 Pytest Timings (12 -> 2 mins) #2787
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
vthumbe1503
merged 22 commits into
NVIDIA:main
from
vthumbe1503:fsdp_pytest_infra_change
Mar 24, 2026
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
eb604b0
Update cudnnFE to v1.20.0 (#2774)
ksivaman 0608bde
fix merge conflicts, now things working
vthumbe1503 93e8b9a
[PyTorch] torch.compile support for permutation functions (#2686)
pggPL 6da802e
[PyTorch] Add an API restore from function context to ensure tensors …
kainzhong 56366bb
[PyT] Install pytest in onnx L1 test as Pyt container no longer packa…
KshitijLakhani f943147
[Core] Fix MXFP8 grouped quantize for zero-sized groups in update_tma…
jberchtold-nvidia 4f0f7f9
Revert "fix merge conflicts, now things working"
vthumbe1503 6b95e60
change distributed tests infra for fsdp2
vthumbe1503 a91d17b
verbose flag for reporting
vthumbe1503 2e26b05
add back coments
vthumbe1503 5ca65c9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9e91caf
another minor fix
vthumbe1503 a88eac4
Merge branch 'main' into fsdp_pytest_infra_change
vthumbe1503 47f8513
not needed for this PR
vthumbe1503 d669748
Merge branch 'fsdp_pytest_infra_change' of github.com:vthumbe1503/Tra…
vthumbe1503 7d9785f
address review comments
vthumbe1503 3ac0ccd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 075b6aa
unecessary comments
vthumbe1503 43dc77b
Merge branch 'fsdp_pytest_infra_change' of github.com:vthumbe1503/Tra…
vthumbe1503 14c1c48
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 947bb39
address revire comments
vthumbe1503 c385be1
Merge branch 'fsdp_pytest_infra_change' of github.com:vthumbe1503/Tra…
vthumbe1503 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # Copyright (c) 2022-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # | ||
| # See LICENSE for license information. | ||
|
|
||
| """Shared pytest fixtures for FSDP2 distributed tests. | ||
|
|
||
| Fixtures defined here (dist_init, _cleanup, recipe_name) are auto-discovered | ||
| by pytest for every test module in this directory. | ||
| """ | ||
|
|
||
| import gc | ||
| import os | ||
| import pytest | ||
| import torch | ||
| import torch.distributed as dist | ||
| from transformer_engine.pytorch import fp8 | ||
|
|
||
| # Ensure the correct CUDA device is active before _parametrize_recipes() | ||
| # runs at collection time, since the session-scoped dist_init fixture | ||
| # has not executed yet. | ||
| _local_rank = int(os.environ.get("LOCAL_RANK", "0")) | ||
| torch.cuda.set_device(_local_rank) | ||
|
|
||
|
|
||
| # ── FP8 recipe parametrization ────────────────────────────────────── | ||
| def _check_nvfp4_support(): | ||
| supported, reason = fp8.check_nvfp4_support() | ||
| if supported and torch.cuda.get_device_capability()[0] == 12: | ||
| return ( | ||
| False, | ||
| ( | ||
| "NVFP4BlockScaling is failing on SM120 with " | ||
| "hadamard_transform/hadamard_transform_cast_fusion.cu:672 in function " | ||
| "rht_gemm_ntt_w_sfc: CUDA Error: invalid argument" | ||
| ), | ||
| ) | ||
| return supported, reason | ||
|
|
||
|
|
||
| _FP8_RECIPE_CONFIGS = [ | ||
| ("DelayedScaling", fp8.check_fp8_support), | ||
| ("Float8CurrentScaling", fp8.check_fp8_support), | ||
| ("Float8BlockScaling", fp8.check_fp8_block_scaling_support), | ||
| ("MXFP8BlockScaling", fp8.check_mxfp8_support), | ||
| ("NVFP4BlockScaling", _check_nvfp4_support), | ||
| ] | ||
|
|
||
|
|
||
| def _parametrize_recipes(): | ||
| params = [] | ||
| for name, check_fn in _FP8_RECIPE_CONFIGS: | ||
| supported, reason = check_fn() | ||
| params.append( | ||
| pytest.param(name, id=name, marks=pytest.mark.skipif(not supported, reason=reason)) | ||
| ) | ||
| return params | ||
|
|
||
|
|
||
| # ── Session / per-test fixtures ────────────────────────────────────── | ||
| @pytest.fixture(scope="session", autouse=True) | ||
| def dist_init(): | ||
| """Initialize the distributed process group once for the entire pytest session.""" | ||
| local_rank = int(os.environ["LOCAL_RANK"]) | ||
| torch.cuda.set_device(local_rank) | ||
| dist.init_process_group(backend="cpu:gloo,cuda:nccl") | ||
| torch.manual_seed(42) | ||
| torch.cuda.manual_seed(42) | ||
| yield | ||
| if dist.is_initialized(): | ||
| dist.destroy_process_group() | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def _cleanup(): | ||
| """Release GPU memory and stale NCCL state between tests.""" | ||
| yield | ||
| if dist.is_initialized(): | ||
| dist.barrier() | ||
| gc.collect() | ||
| torch.cuda.empty_cache() | ||
|
|
||
|
|
||
| @pytest.fixture(params=_parametrize_recipes()) | ||
| def recipe_name(request): | ||
| return request.param | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # Copyright (c) 2022-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # | ||
| # See LICENSE for license information. | ||
|
|
||
| """Shared utility functions for FSDP2 distributed tests.""" | ||
|
|
||
| import transformer_engine.common.recipe | ||
| from transformer_engine.pytorch import QuantizedTensor | ||
|
|
||
|
|
||
| def get_recipe_from_string(recipe): | ||
| return getattr(transformer_engine.common.recipe, recipe)() | ||
|
|
||
|
|
||
| def save_custom_attrs(module): | ||
| custom_attrs = {} | ||
| for name, param in module.named_parameters(): | ||
| if isinstance(param, QuantizedTensor): | ||
| ignore_keys = [key for key in param.__dict__.keys() if key.startswith("_")] | ||
| else: | ||
| ignore_keys = [] | ||
| attrs = vars(param) | ||
| custom_attrs[name] = {k: v for k, v in attrs.items() if k not in ignore_keys} | ||
| return custom_attrs | ||
|
|
||
|
|
||
| def restore_custom_attrs(module, custom_attrs): | ||
| for name, param in module.named_parameters(): | ||
| if name in custom_attrs: | ||
| for attr_name, attr_value in custom_attrs[name].items(): | ||
| setattr(param, attr_name, attr_value) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.