[PyTorch] Introduce QuantizerRole - #2620
Conversation
…ipe state Signed-off-by: Evgeny <etsykunov@nvidia.com>
Signed-off-by: Evgeny <etsykunov@nvidia.com>
for more information, see https://pre-commit.ci
Greptile SummaryThis PR introduces
Confidence Score: 3/5The PR introduces significant new machinery; several defects in the changed code mean certain configurations will crash or produce silently wrong quantizers. The asymmetric delayed-scaling crash in add_fp8_tensors_to_global_buffer is a latent runtime error that fires whenever a factory emits DelayedScalingRequest for one direction but not the other. The same has_delayed_scaling_state guard change also exposes restore_fp8_meta_tensors to a None.copy() crash. These are on top of assert-based length validations stripped by -O, the CustomRecipeState early-return that silently keeps stale quantizers when the recipe object changes, and the sequence-parallel amax check now being an assert instead of a hard error. transformer_engine/pytorch/quantization.py (add_fp8_tensors_to_global_buffer and restore_fp8_meta_tensors), transformer_engine/pytorch/module/base.py (assert-based validation and CustomRecipeState early-return guard), transformer_engine/pytorch/ops/op.py (assert-based validation and recipe-identity check) Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
subgraph ModuleForward["Module forward (set_meta_tensor)"]
A[fp8_meta_tensors_initialized?] -- No --> B[get_quantizer_roles]
A -- Yes, CustomRecipeState exists --> EarlyReturn[Early return - stale if recipe changed]
B --> C[RecipeState.create with roles]
C --> D[inherit_state_from old state - preserves scale/amax_history]
D --> E[make_quantizers]
end
subgraph CustomRecipeStateMQ["CustomRecipeState.make_quantizers"]
E --> F[qfactory role for each slot]
F --> G{Returns None?}
G -- Yes --> Err1[ValueError: None rejected]
G -- No --> H{DelayedScalingRequest?}
H -- Yes --> I[_handle_delayed_scaling_requests - allocates inner DSRS]
H -- No --> J[Use quantizer directly]
I --> K[Splice Float8Quantizer into raw list]
end
subgraph GlobalBuffer["FP8GlobalStateManager.add_fp8_tensors_to_global_buffer"]
L{_has_delayed_scaling_state?} -- True --> M[Loop fwd+bwd]
M --> N{CustomRecipeState with DS?}
N -- Yes --> O[Use inner_recipe key]
N -- No --> P[Use outer recipe key]
P --> Q[state.amax_history 0 - None if no DS - TypeError crash]
end
E --> L
|
This comment was marked as off-topic.
This comment was marked as off-topic.
Signed-off-by: Evgeny <etsykunov@nvidia.com>
Signed-off-by: Evgeny <etsykunov@nvidia.com>
Signed-off-by: Evgeny <etsykunov@nvidia.com>
Signed-off-by: Evgeny <etsykunov@nvidia.com>
timmoon10
left a comment
There was a problem hiding this comment.
Overall this design is quite clean and generalizable.
Signed-off-by: Evgeny Tsykunov <etsykunov@nvidia.com>
for more information, see https://pre-commit.ci
Signed-off-by: Evgeny <etsykunov@nvidia.com>
Signed-off-by: Evgeny <etsykunov@nvidia.com>
Signed-off-by: Evgeny <etsykunov@nvidia.com>
Signed-off-by: Evgeny <etsykunov@nvidia.com>
for more information, see https://pre-commit.ci
Signed-off-by: Evgeny <etsykunov@nvidia.com>
Signed-off-by: Evgeny <etsykunov@nvidia.com>
Signed-off-by: Evgeny <etsykunov@nvidia.com>
This new setup is fully compatible with the current one - no need to immediately update anything. Legacy users will not notice it. |
|
/te-ci pytorch L1 |
Signed-off-by: Evgeny <etsykunov@nvidia.com>
|
/te-ci pytorch L1 |
Signed-off-by: Evgeny <etsykunov@nvidia.com>
Signed-off-by: Evgeny <etsykunov@nvidia.com>
for more information, see https://pre-commit.ci
|
83c405b introduces |
|
/te-ci pytorch L1 |
|
@negvet Could you fix the issues hit in the CI runs? |
Signed-off-by: Evgeny <etsykunov@nvidia.com>
Signed-off-by: Evgeny <etsykunov@nvidia.com>
|
/te-ci pytorch L1 |
timmoon10
left a comment
There was a problem hiding this comment.
Test failures are unrelated
|
/te-ci pytorch L1 |
|
The same failures on the main |
* Enable semantic roles emitted by module/op and comsumed by custom recipe state Signed-off-by: Evgeny <etsykunov@nvidia.com> * Update quantization factories Signed-off-by: Evgeny <etsykunov@nvidia.com> * Fix tests Signed-off-by: Evgeny <etsykunov@nvidia.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Swap tensor:module Signed-off-by: Evgeny <etsykunov@nvidia.com> * Better naming Signed-off-by: Evgeny <etsykunov@nvidia.com> * Introduce QuantizerRole frozen data class instead of a string Signed-off-by: Evgeny <etsykunov@nvidia.com> * Shrink module_type vocabulary Signed-off-by: Evgeny <etsykunov@nvidia.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix numerics exact test Signed-off-by: Evgeny <etsykunov@nvidia.com> * Set defaults, make custom recipe forward compatible Signed-off-by: Evgeny <etsykunov@nvidia.com> * remove position from QuantizerRole Signed-off-by: Evgeny <etsykunov@nvidia.com> * Set good defaults Signed-off-by: Evgeny <etsykunov@nvidia.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Resolve naming: make every module/op distinguishable via name Signed-off-by: Evgeny <etsykunov@nvidia.com> * Configure output/grad_input roles, defaults to None Signed-off-by: Evgeny <etsykunov@nvidia.com> * Remove is_gemm() Signed-off-by: Evgeny <etsykunov@nvidia.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Enable base recipes via CustomRecipe and quantization factories Signed-off-by: Evgeny <etsykunov@gmail.com> * Add factory example - NVFP4 for Linear, MXFP8 for GroupedLinear Signed-off-by: Evgeny <etsykunov@gmail.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix custom recipe test Signed-off-by: Evgeny <etsykunov@gmail.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Test fine-grained quantization targets Signed-off-by: Evgeny <etsykunov@nvidia.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add quantizer roles for attention (attn is wip) Signed-off-by: Evgeny <etsykunov@nvidia.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Enable statful recipes in the Custom recipe - Delayed Scaling support Signed-off-by: Evgeny <etsykunov@nvidia.com> * Fix save_original_input for custom delayed scaling Signed-off-by: Evgeny <etsykunov@nvidia.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Enable custom recipe for attn Signed-off-by: Evgeny <etsykunov@nvidia.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Make boundary role setting more explicit in MHA Signed-off-by: Evgeny <etsykunov@nvidia.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Make dpa role setting more intuitive Signed-off-by: Evgeny <etsykunov@nvidia.com> * Docstring for get_quantizer_roles() in base module Signed-off-by: Evgeny <etsykunov@nvidia.com> * Fix lint Signed-off-by: Evgeny <etsykunov@nvidia.com> * Restrict None roles Signed-off-by: Evgeny <etsykunov@nvidia.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Linter Signed-off-by: Evgeny <etsykunov@nvidia.com> * Minor fixes Signed-off-by: Evgeny <etsykunov@nvidia.com> * Test debug tools compat Signed-off-by: Evgeny <etsykunov@nvidia.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix pylint Signed-off-by: Evgeny <etsykunov@nvidia.com> * fix test Signed-off-by: Evgeny <etsykunov@nvidia.com> * Fix lint Signed-off-by: Evgeny <etsykunov@nvidia.com> * Constructor takes roles kwarg + test fix Signed-off-by: Evgeny <etsykunov@nvidia.com> * Constructor takes roles kwarg + test fix (quantization.py) Signed-off-by: Evgeny <etsykunov@nvidia.com> * Fix attention: MXFP8, w/o CP Signed-off-by: Evgeny <etsykunov@nvidia.com> * Add test custom recipe Signed-off-by: Evgeny <etsykunov@nvidia.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Make Float8BlockScalingRecipeState and NVFP4BlockScalingRecipeState aware about QuantizerRole, dispatch on that + positional fallback if get_quantizer_roles() is not defined by the module/op Signed-off-by: Evgeny <etsykunov@nvidia.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix linter Signed-off-by: Evgeny <etsykunov@nvidia.com> * Fix CI Signed-off-by: Evgeny <etsykunov@nvidia.com> * Preserve delayed scaling state (buffers) when rebuild is triggered Signed-off-by: Evgeny <etsykunov@nvidia.com> * Fix test, minor Signed-off-by: Evgeny <etsykunov@nvidia.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix distributed tests Signed-off-by: Evgeny <etsykunov@nvidia.com> --------- Signed-off-by: Evgeny <etsykunov@nvidia.com> Signed-off-by: Evgeny Tsykunov <etsykunov@nvidia.com> Signed-off-by: Evgeny <etsykunov@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Evgeny <etsykunov@gmail.com>
Description
Introducing
QuantizerRoleThis is an API that allows to go down to "set this
LayerNormLinearin this transformer layer to be less aggressively quantized." (fine-grained, per-module/per-tensor quantization control mechanism)See
test_custom_recipe.py::test_custom_recipe_quantization_targets().Quantizer factory uses roles to dispatch according to its needs.
TE module/op emits a list of
QuantizerRole:Linear,LayerNormLinear,LayerNormMLPemitmodule_type="linear"withtensor_typein{"input", "weight", "grad_output"}.GroupedLinearemitsmodule_type="grouped_linear".CustomRecipeaccepts aqfactorycallable that receivesQuantizerRoleand returns a quantizer.Factories can be composed - e.g., dispatch (to different sub-factories as an option) based on
module_type(dpavslinear) and then refine based ontensor_type.Summary:
get_quantizer_roles()that returns a list of QuantizerRole objects.set_meta_tensor(), modules callget_quantizer_roles()and pass roles toRecipeState.create().RecipeState.create()assigns roles to the state (e.g.,CustomRecipeState.roles).CustomRecipeState.make_quantizers()callsqfactory(role)for each role to create quantizers.role.module_type,role.tensor_type, androle.nameto dispatch to different quantizers.This PR enables granular control over the recipe. Which might have some limitations and edge cases.
Type of change
Changes
Please list the changes introduced in this PR:
Checklist: