-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Add config option to set attrs in TE quantization recipe #6341
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
base: main
Are you sure you want to change the base?
Changes from all commits
786d17b
a4692bf
6e3e1c1
0f5f847
42ec407
3cb0458
ced16e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -145,6 +145,9 @@ class TEQuantizationRecipe: | |
| If no FP8 or FP4 quantization is configured, the recipe is execution | ||
| in high-precision (BF16). | ||
| """ | ||
|
|
||
| recipe_attrs: Optional[Dict[str, Any]] = None | ||
| """Attributes to set in the TE quantization recipe class.""" | ||
| custom_recipe_factory: Optional[str] = None | ||
| """The path to a custom recipe factory if a custom Fp4 or Fp8 recipe is configured""" | ||
| fp8_format: str = "e4m3" | ||
|
|
@@ -293,6 +296,11 @@ def _get_fp8_model_init_for_quant_recipe(qrecipe: TEQuantizationRecipe): | |
| else: | ||
| raise ValueError(f"Unhandled fp4 recipe: {qrecipe.fp4_quantization_recipe}") | ||
|
|
||
| # Set recipe attrs | ||
| if quant_recipe is not None and qrecipe.recipe_attrs is not None: | ||
| for key, val in qrecipe.recipe_attrs.items(): | ||
| setattr(quant_recipe, key, val) | ||
|
|
||
| return fp8_model_init( | ||
| enabled=enabled, | ||
| recipe=quant_recipe, | ||
|
|
@@ -355,7 +363,12 @@ def _get_fp8_autocast_for_quant_recipe(qrecipe: TEQuantizationRecipe): | |
| if qrecipe.fp4_quantization_recipe == Fp4Recipe.nvfp4: | ||
| quant_recipe = te.common.recipe.NVFP4BlockScaling() | ||
| else: | ||
| raise ValueError(f"Unhandled fp4 recipe: {qrecipe.fp8_quantization_recipe}") | ||
| raise ValueError(f"Unhandled fp4 recipe: {qrecipe.fp4_quantization_recipe}") | ||
|
|
||
| # Set recipe attrs | ||
| if quant_recipe is not None and qrecipe.recipe_attrs is not None: | ||
| for key, val in qrecipe.recipe_attrs.items(): | ||
| setattr(quant_recipe, key, val) | ||
|
|
||
| return fp8_autocast(enabled=True, fp8_recipe=quant_recipe, fp8_group=amax_group) | ||
|
|
||
|
|
@@ -3294,9 +3307,39 @@ def forward(self, hidden_states: torch.Tensor, **kwargs) -> Tuple[Tensor, Option | |
|
|
||
| # Build fused impl and cache recipe lazily on first forward pass. | ||
| # Both are created once and reused — avoids object creation every call. | ||
| # This recipe cache is a hack (TEFusedMLP does not need | ||
| # it). Keep it for now for expediency, but future | ||
| # developers are warned that this should be refactored to | ||
| # use the model's recipe config like the other TE modules. | ||
| if not hasattr(self, '_recipe'): | ||
| if os.getenv("FP4_RECIPE", "") == "nvfp4": | ||
| self._recipe = te.common.recipe.NVFP4BlockScaling() | ||
| elif os.getenv("FP4_RECIPE", "") == "nvfp4_ue5m3": | ||
|
Member
Author
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. This code path uses TE support for UE5M3 scales, which is pending in NVIDIA/TransformerEngine#3325. If you set this envvar, then you're almost certainly an expert user who's explicitly enabling UE5M3 support. Also, all of |
||
|
|
||
| def _make_nvfp4_ue5m3_quantizer( | ||
| role: te.pytorch.QuantizerRole, | ||
| ) -> te.pytorch.NVFP4Quantizer: | ||
| """Construct NVFP4 quantizer with UE5M3 scales and no per-tensor | ||
| scale for activations.""" | ||
| tensor_type = role.tensor_type if role is not None else None | ||
| if not tensor_type: | ||
| tensor_type = "input" | ||
| with_rht = tensor_type in ("input", "grad_output") | ||
| return te.pytorch.NVFP4Quantizer( | ||
| scale_dtype=te.pytorch.DType.kFloat8UE5M3, | ||
| with_rht=with_rht, | ||
| with_post_rht_amax=with_rht, | ||
| with_2d_quantization=tensor_type == "weight", | ||
| stochastic_rounding=False, | ||
| with_random_sign_mask=False, | ||
| disable_second_level_scale=tensor_type == "input", | ||
| ) | ||
|
|
||
| # Construct custom recipe for NVFP4 with UE5M3 scales | ||
| self._recipe = te.common.recipe.CustomRecipe( | ||
| qfactory=_make_nvfp4_ue5m3_quantizer | ||
| ) | ||
| self._recipe.enable_cutedsl_fused_grouped_mlp = True | ||
| else: | ||
| self._recipe = te.common.recipe.MXFP8BlockScaling() | ||
| recipe = self._recipe | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.