Repository navigation
[Core][PyTorch] NVFP4 recipe #2177
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
+15,060
−753
Merged
Changes from 10 commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
6298a76
Add NVFP4 recipe
ksivaman d5919e1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 49e939b
Add MathDx dependency to GitHub builds
timmoon10 689c66e
Suggestions from GitHub Copilot
timmoon10 afd411d
Merge branch 'main' into nvfp4_recipe
timmoon10 f8b2a2d
Move 2x shape logic from core to PyTorch
ksivaman 6427c09
Fix compilation errors with CUDA 12.1
timmoon10 d8ae3b0
Merge branch 'main' into nvfp4_recipe
timmoon10 279b013
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1465744
SM 70 is not supported in CUDA 13
timmoon10 00dd510
Typo
timmoon10 6430896
Revert "Move 2x shape logic from core to PyTorch"
timmoon10 788ee72
Merge branch 'main' into nvfp4_recipe
timmoon10 fe9a347
Added dequantize kernel for FP4
ptrendx 838c5fc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6eb2a7f
Fix linter warning
timmoon10 7d8bce6
Merge branch 'main' into nvfp4_recipe
timmoon10 9981058
Add NVFP4 support with fusible ops
timmoon10 3ad35d8
Merge branch 'main' into nvfp4_recipe
ksivaman 9dc682b
Fix logic for 2x shapes and move to PyTorch
ksivaman 14c82e0
Fix CG test model config
ksivaman 1af7dd8
Debug NVFP4 tensor size function
timmoon10 8ce8140
Proper handling of the RNG state
ptrendx e2c7be2
Test SR properly
ptrendx 3b20ca1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 628a83d
Fix workspace size for GEMM heuristic.
ksivaman c7b4851
Merge branch 'main' into nvfp4_recipe
ksivaman 5e29005
Fix compile error in C++ NVFP4 test
timmoon10 c3dd5d6
fix distrbuted test problem shape
zhongbozhu f25c55c
proper assert dim for low precision AG TP
zhongbozhu 589c399
clean up duplicated code in nvfp4_utils.cuh
zhongbozhu f55c267
lint
zhongbozhu d478c68
pylint: disable=unused-argument
zhongbozhu f956028
`nvte_cublas_gemm_v2` to take alpha pointer (#12)
phu0ngng 4573a16
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 38055db
Support fused amax kernels with NVFP4 quantization
timmoon10 6688e35
Disable fused amax with cuDNN LayerNorm kernel
timmoon10 911335d
Add NVFP4 cases to distributed tests for TE ops
timmoon10 8550cb7
Change assert to NVTE_CHECK in the hadamard cast fusion
ptrendx d01a658
Fix compile error
timmoon10 e519ea4
Use global thread IDs for Philox subsequences
timmoon10 8be0a74
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6d2a106
Merge branch 'main' into nvfp4_recipe
timmoon10 a0cb86d
Add shape checks for NVFP4 cast kernels
timmoon10 b396119
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f549d78
Merge branch 'main' into nvfp4_recipe
timmoon10 eaa5c46
Do not fuse amax if cuDNN normalization is forced by envvar
ptrendx 115dbfc
Merge branch 'main' into nvfp4_recipe
ksivaman 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
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,152 @@ | ||
| # Copyright (c) 2022-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # | ||
| # See LICENSE for license information. | ||
|
|
||
| import argparse | ||
| import torch | ||
| import pandas as pd | ||
| import torch.utils.benchmark as benchmark | ||
|
|
||
| import transformer_engine.pytorch as te | ||
| import transformer_engine_torch as tex | ||
| import transformer_engine.pytorch.cpp_extensions as ext | ||
|
|
||
| from transformer_engine.pytorch.tensor.nvfp4_tensor import NVFP4Quantizer | ||
|
|
||
| scale_padding_to = 1 | ||
| permute_scale = False | ||
|
|
||
| TORCH_TO_TE_FLOAT_MAP = { | ||
| torch.bfloat16: tex.DType.kBFloat16, | ||
| } | ||
|
|
||
|
|
||
| def run_kernel(shape, stochastic_rounding: bool, input_dtype=torch.bfloat16): | ||
| # Generate random input data | ||
| M, K = shape | ||
| x = torch.randn([M, K], dtype=input_dtype, device="cuda") | ||
|
|
||
| assert shape[0] % 16 == 0, "Shape must be divisible by 16" | ||
| assert shape[1] % 16 == 0, "Shape must be divisible by 16" | ||
|
|
||
| # Quantize | ||
| nvfp4_quantizer = NVFP4Quantizer( | ||
| fp4_dtype=tex.DType.kFloat4E2M1, | ||
| rowwise=True, | ||
| columnwise=True, | ||
| with_amax_reduction=False, | ||
| amax_reduction_group=None, | ||
| with_rht=True, | ||
| with_post_rht_amax=True, | ||
| with_random_sign_mask=True, | ||
| stochastic_rounding=stochastic_rounding, | ||
| ) | ||
| x_nvfp4_sut = nvfp4_quantizer.make_empty( | ||
| (M, K), dtype=x.dtype, device=x.device, requires_grad=False | ||
| ) | ||
| x_nvfp4_sut = nvfp4_quantizer.update_quantized(x, x_nvfp4_sut) | ||
|
|
||
| with torch.no_grad(): | ||
| stmt = "kernel_func(input, output)" | ||
| globals_dict = { | ||
| "kernel_func": nvfp4_quantizer.update_quantized, | ||
| "input": x, | ||
| "output": x_nvfp4_sut, | ||
| } | ||
|
|
||
| timing = benchmark.Timer( | ||
| stmt=stmt, | ||
| globals=globals_dict, | ||
| num_threads=1, | ||
| ).blocked_autorange(min_run_time=5) | ||
| print(timing) | ||
| timing_us = timing.median * 1e6 | ||
|
|
||
| input_nbytes = shape[0] * shape[1] * 2 # bf16 | ||
| output_nbytes = shape[0] * shape[1] // 2 # //2 for fp4 | ||
| sf_nbytes = shape[0] * shape[1] // 16 # //16 for 1 byte per 16 elems | ||
|
|
||
| total_nbytes = ( | ||
| 0 | ||
| + input_nbytes | ||
| * 3 # Reading input for Amax(x)&Amax(RHT(x.T)), Reading input for Cast(x), Reaindg input for Cast(RHT(x.T)) | ||
| + 2 * 4 # Output 2 * float for scale & amax | ||
| + 2 * 4 # Input 2 * float | ||
| + output_nbytes * 2 # Output from Cast(x) and Cast(RHT(x.T)) | ||
| + sf_nbytes * 2 # Scale factor | ||
| ) | ||
|
|
||
| throughput_GBps = total_nbytes / (1024 * 1024 * 1024) / (timing_us / 1e6) | ||
|
|
||
| print( | ||
| f"Stochastic rounding: {stochastic_rounding}, Total: {total_nbytes} bytes, Throughput:" | ||
| f" {throughput_GBps} GB/s" | ||
| ) | ||
| return timing_us, throughput_GBps | ||
|
|
||
|
|
||
| # Nsight Compute Profiling Command: | ||
| # ncu -f -o block_scaled_1d_cast_transpose_kernel --set=full --kernel-name "block_scaled_1d_cast_transpose_kernel" -s 5 -c 5 python benchmark_cast_transpose_1d_block.py --profile | ||
|
|
||
| if __name__ == "__main__": | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("--profile", action="store_true", help="Enable profiling mode") | ||
| args = parser.parse_args() | ||
|
|
||
| if args.profile: | ||
| print("Profiling is enabled.") | ||
| else: | ||
| print("Profiling is disabled.") | ||
|
|
||
| shapes = [ | ||
| (8192, 5120), | ||
| (8192, 10240), | ||
| (8192, 2560), | ||
| (8192, 11328), | ||
| (8192, 512), | ||
| (8192, 3584), | ||
| (5120, 8192), | ||
| (10240, 8192), | ||
| (2560, 8192), | ||
| (11328, 8192), | ||
| (512, 8192), | ||
| (3584, 8192), | ||
| (4096, 16384), | ||
| (14336, 16384), | ||
| ] | ||
|
|
||
| if args.profile: | ||
| shapes = [ | ||
| (16384, 6144), | ||
| ] | ||
|
|
||
| data = [] | ||
| for stochastic_rounding in [True]: # , False]: | ||
| for shape in shapes: | ||
| print( | ||
| f"Running benchmark_func with shape {shape} and stochastic_rounding" | ||
| f" {stochastic_rounding}" | ||
| ) | ||
| timing_us, throughput_GBps = run_kernel(shape, stochastic_rounding) | ||
| data.append( | ||
| [ | ||
| "benchmark_func", | ||
| shape, | ||
| stochastic_rounding, | ||
| timing_us, | ||
| throughput_GBps, | ||
| ] | ||
| ) | ||
|
|
||
| df = pd.DataFrame( | ||
| data=data, | ||
| columns=[ | ||
| "kernel", | ||
| "shape", | ||
| "stochastic_rounding", | ||
| "timing_us", | ||
| "throughput(GB/s)", | ||
| ], | ||
| ) | ||
| print(df) | ||
| df.to_csv("benchmark_cast_nvfp4.csv", index=False) |
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
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
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
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
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
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
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
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.