-
Notifications
You must be signed in to change notification settings - Fork 757
Enable NVFP4 RHT amax for grouped SReLU MLP #3133
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
Open
sraman-rgb
wants to merge
8
commits into
NVIDIA:main
Choose a base branch
from
sraman-rgb:te-nvfp4-srelu-rht-hadamard
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+85
−18
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
79def34
Enable NVFP4 RHT amax for grouped SReLU MLP
sraman-rgb 3de3ad5
Merge branch 'main' into te-nvfp4-srelu-rht-hadamard
vthumbe1503 b8d2dd3
Update tests/pytorch/test_fusible_ops.py
sraman-rgb 3c1db0f
Fix indentation for tols assignment in test
vthumbe1503 6ce5259
Guard SReLU hadamard kernel by cuDNN frontend version
sraman-rgb fad3d47
Merge branch 'main' into te-nvfp4-srelu-rht-hadamard
sraman-rgb a04aea0
Update tolerance handling for quantization tests
vthumbe1503 46a6b32
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 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 |
|---|---|---|
|
|
@@ -83,6 +83,11 @@ def _cudnn_frontend_supports_grouped_gemm_srelu() -> bool: | |
| return _cudnn_frontend_version_at_least("1.24.0") | ||
|
|
||
|
|
||
| def _cudnn_frontend_supports_grouped_gemm_srelu_hadamard() -> bool: | ||
| """Check cuDNN frontend min version for grouped GEMM SReLU hadamard kernels.""" | ||
| return _cudnn_frontend_version_at_least("1.26.0") | ||
|
|
||
|
|
||
| def _nvidia_cudnn_frontend_supports_wgrad() -> bool: | ||
| """Check cuDNN FE min version for grouped GEMM wgrad kernel.""" | ||
| return _cudnn_frontend_version_supported() | ||
|
|
@@ -871,7 +876,7 @@ def fuser_forward( | |
| basic_op_kwargs: list[dict[str, Any]], | ||
| ) -> tuple[torch.Tensor, Iterable[Iterable[torch.Tensor]]]: | ||
| # Get basic operations | ||
| fc1_op, _, fc2_op = self.basic_ops | ||
| fc1_op, activation_op, fc2_op = self.basic_ops | ||
| fc1_ctx, _activation_ctx, fc2_ctx = basic_op_ctxs | ||
|
|
||
| # Tensor properties | ||
|
|
@@ -1151,17 +1156,23 @@ def fuser_forward( | |
| fc1_alpha_tensor = alpha_tensor | ||
|
|
||
| use_tmem_post_rht_amax = _use_tmem_post_rht_amax() | ||
| use_fc1_glu_hadamard = False | ||
| use_fc1_act_hadamard = False | ||
| use_fc1_act_hadamard_srelu = False | ||
| use_nvfp4_rht_amax = ( | ||
| use_nvfp4 | ||
| and isinstance(fc2_input_quantizer, NVFP4Quantizer) | ||
| and fc2_input_quantizer.with_rht | ||
| and fc2_input_quantizer.with_post_rht_amax | ||
| ) | ||
| if use_nvfp4_rht_amax and self._cudnn_act_func == "swiglu": | ||
| kernel_getter = getattr(self, "grouped_gemm_glu_hadamard_kernel", None) | ||
| activation_is_srelu = isinstance(activation_op, ScaledSReLU) | ||
| activation_supports_hadamard = self._cudnn_act_func == "swiglu" or ( | ||
| activation_is_srelu and _cudnn_frontend_supports_grouped_gemm_srelu_hadamard() | ||
| ) | ||
| if use_nvfp4_rht_amax and activation_supports_hadamard: | ||
| kernel_getter = getattr(self, "grouped_gemm_act_hadamard_kernel", None) | ||
| if kernel_getter is not None: | ||
| use_fc1_glu_hadamard = kernel_getter() is not None | ||
| use_fc1_act_hadamard = kernel_getter() is not None | ||
| use_fc1_act_hadamard_srelu = use_fc1_act_hadamard and activation_is_srelu | ||
|
|
||
| fc1_activation_kwargs = { | ||
| "a_tensor": fc1_x_data, | ||
|
|
@@ -1178,9 +1189,11 @@ def fuser_forward( | |
| "current_stream": current_stream, | ||
| "use_dynamic_sched": True, | ||
| } | ||
| if self._cudnn_act_func is not None: | ||
| if use_fc1_act_hadamard_srelu: | ||
| fc1_activation_kwargs["act_func"] = "srelu" | ||
| elif self._cudnn_act_func is not None: | ||
| fc1_activation_kwargs["act_func"] = self._cudnn_act_func | ||
| if use_fc1_glu_hadamard: | ||
| if use_fc1_act_hadamard: | ||
| fc1_activation_kwargs["use_tmem_post_rht_amax"] = use_tmem_post_rht_amax | ||
| else: | ||
| fc1_activation_kwargs["norm_const_tensor"] = fc1_norm_const_tensor | ||
|
|
@@ -1234,8 +1247,8 @@ def fuser_forward( | |
| fc1_activation_kwargs["b_dtype"] = data_dtype | ||
| fc1_activation_kwargs["b_major"] = "k" | ||
|
|
||
| if use_fc1_glu_hadamard: | ||
| fc1_kernel_out = self.grouped_gemm_glu_hadamard_kernel()(**fc1_activation_kwargs) | ||
| if use_fc1_act_hadamard: | ||
| fc1_kernel_out = self.grouped_gemm_act_hadamard_kernel()(**fc1_activation_kwargs) | ||
| else: | ||
| fc1_kernel_out = self.grouped_gemm_activation_kernel()(**fc1_activation_kwargs) | ||
|
|
||
|
|
@@ -1269,7 +1282,7 @@ def fuser_forward( | |
| fc2_in = fc2_in.view(in_shape[0], fc2_weight_shape[1]).contiguous() | ||
| fc2_input_quantizer.set_usage(rowwise=True, columnwise=weight_requires_grad) | ||
| fc2_input_quantizer.optimize_for_gemm = True | ||
| if use_fc1_glu_hadamard: | ||
| if use_fc1_act_hadamard: | ||
| grouped_fc2_x = _group_quantize_with_amax_for_grouped_mlp( | ||
| fc2_in, | ||
| fc2_input_quantizer, | ||
|
|
@@ -2109,8 +2122,8 @@ def grouped_gemm_activation_kernel(cls) -> Callable: | |
|
|
||
| @classmethod | ||
| @functools.lru_cache(maxsize=None) | ||
| def grouped_gemm_glu_hadamard_kernel(cls) -> Optional[Callable]: | ||
| """Fused grouped GEMM GLU kernel that also emits NVFP4 RHT amaxes.""" | ||
| def grouped_gemm_act_hadamard_kernel(cls) -> Optional[Callable]: | ||
| """Fused grouped GEMM activation kernel that also emits NVFP4 RHT amaxes.""" | ||
| try: | ||
| from cudnn import ( | ||
| grouped_gemm_glu_hadamard_wrapper_sm100, | ||
|
|
@@ -2146,6 +2159,22 @@ def grouped_gemm_activation_kernel(cls) -> Callable: | |
|
|
||
| return grouped_gemm_srelu_wrapper_sm100 | ||
|
|
||
| @classmethod | ||
| @functools.lru_cache(maxsize=None) | ||
| def grouped_gemm_act_hadamard_kernel(cls) -> Optional[Callable]: | ||
| """Fused grouped GEMM activation kernel that also emits NVFP4 RHT amaxes.""" | ||
| if not _cudnn_frontend_supports_grouped_gemm_srelu_hadamard(): | ||
| return None | ||
|
|
||
| try: | ||
| from cudnn import ( | ||
| grouped_gemm_glu_hadamard_wrapper_sm100, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need new cudnn version for supporting srelu in this kernel? If so, we should update it. |
||
| ) # pylint: disable=no-name-in-module,import-outside-toplevel | ||
| except ImportError: | ||
| return None | ||
|
|
||
| return grouped_gemm_glu_hadamard_wrapper_sm100 | ||
|
|
||
| @classmethod | ||
| @functools.lru_cache(maxsize=None) | ||
| def grouped_gemm_dactivation_kernel(cls) -> Callable: | ||
|
|
||
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.