Skip to content

[quantization] Refactor quantization API - #1763

Merged
tianyu-l merged 12 commits into
pytorch:mainfrom
danielvegamyhre:validate
Sep 30, 2025
Merged

tianyu-l merged 12 commits into
pytorch:mainfrom
danielvegamyhre:validate

Conversation

@danielvegamyhre

@danielvegamyhre danielvegamyhre commented Sep 26, 2025 •

Copy link
Copy Markdown
Contributor

Summary

  • Float8 training and mx training cannot be combined. Add validation to ensure either one or the other is used.
  • Fix tomls which still use old config naming (addresses ValueError: Invalid field names in <class 'torchtitan.config.job_config.JobConfig'> data: {'mx'}. #1764)
  • Refactor quantization config naming so (1) job_config class names are consistent with converter names, and (2) naming is more explicit and aligned with semantics of what conversions actually happen
    • Old:
      • Converter classes:
        • Float8Converter
        • Float8MoEConverter
        • MXConverter
        • MXMoEConverter
      • User-facing args:
        • quantize.dense.float8
        • quantize.moe.float8
        • quantize.dense.mx
        • quantize.moe.mx
    • New:
      • Converter classes:
        • Float8LinearConverter
        • Float8GroupedGEMMConverter
        • MXLinearConverter
        • MXGroupedGEMMConverter
      • User-facing args:
        • quantize.linear.float8
        • quantize.grouped_mm.float8
        • quantize.linear.mx
        • quantize.grouped_mm.mx

Tests: mx on B200

  • Test new config/args with valid job:
    • NGPU=4 CONFIG_FILE="./torchtitan/experiments/llama4/train_configs/debug_model.toml" ./run_train.sh --parallelism.data_parallel_shard_degree=4 --parallelism.expert_parallel_degree=1 --parallelism.tensor_parallel_degree=1 --model.print-after-conversion --metrics.log_freq=10 --training.steps=30 --model.converters="quantize.linear.mx,quantize.grouped_mm.mx" --quantize.linear.mx.recipe_name="mxfp8_cublas" --quantize.groupe d_mm.mx.fqns="experts" --compile.enable
    • Result: training as expected
  • Test mixing converters for different quantization methods, validate error:
    • NGPU=4 CONFIG_FILE="./torchtitan/experiments/llama4/train_configs/debug_model.toml" ./run_train.sh --parallelism.data_parallel_shard_degree=4 --parallelism.expert_parallel_degree=1 --parallelism.tensor_parallel_degree=1 --model.print-after-conversion --metrics.log_freq=10 --training.steps=30 --model.converters="quantize.linear.fp8,quantize.grouped_mm.mx" --quantize.linear.mx.recipe_name="mxfp8_cublas" --quantize.grouped_mm.mx.fqns="experts" --compile.enable
      • Result AssertionError: Cannot combine model converters with different quantization types: 'mx' and 'fp8'

Tests: FP8 rowwise on H100

  • Test new config/args with valid job:
    • NGPU=4 CONFIG_FILE="./torchtitan/experiments/llama4/train_configs/debug_model.toml" ./run_train.sh --parallelism.data_parallel_shard_degree=4 --parallelism.expert_parallel_degree=1 --parallelism.tensor_parallel_degree=1 --model.print-after-conversion --metrics.log_freq=10 --training.steps=30 --model.converters="quantize.linear.float8,quantize.grouped_mm.float8" --quantize.linear.float8.recipe_name="rowwise" --quantize.grouped_mm.float8.fqns="experts" --compile.enable
    • Result: training as expected

@danielvegamyhre
danielvegamyhre force-pushed the validate branch 6 times, most recently from 87bbaf8 to 82b74e7 Compare September 26, 2025 21:30
Comment thread torchtitan/protocols/model_converter.py Resolved
Comment thread torchtitan/protocols/model_converter.py Resolved
)


def is_quantization_converter(converter: str):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to have this function, as it's only used in one line

@danielvegamyhre danielvegamyhre changed the title [quantization] validate only one quantization method is used [quantization] refactor quantization config naming; validate only one quantization method is used Sep 30, 2025
Comment thread torchtitan/config/job_config.py Outdated
expert_parallel_degree: int = 1
"""
Expert parallelism degree. 1 means disabled. No effect for non-MoE models.
Expert parallelism degree. 1 means disabled. No effect for non-GroupedMMConfig models.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't change this?

Comment thread torchtitan/config/job_config.py Outdated
expert_tensor_parallel_degree: int = 1
"""
Expert tensor parallelism degree. 1 means disabled. No effect for non-MoE models, or when ep = 1.
Expert tensor parallelism degree. 1 means disabled. No effect for non-GroupedMMConfig models, or when ep = 1.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

--quantize.dense.float8.enable_fsdp_float8_all_gather \
--quantize.dense.float8.precompute_float8_dynamic_scale_for_fsdp \
--quantize.dense.float8.force_recompute_fp8_weight_in_bwd \
--model.converters="quantize.linear.fp8" \

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we should change this, as user won't be able to fetch the old package and still run it with new commands.

I think it's OK to change, but let's add a warning for the breakage.

Comment thread torchtitan/config/job_config.py Outdated

@dataclass
class Float8Dense:
class FP8LinearConfig:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this still be Float8Linear to be consistent with torchao? The same for all other fp8, should they still be float8? Anyway I saw that you didn't change the file name float8.py.

@danielvegamyhre danielvegamyhre Sep 30, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered this as well, I actually would prefer to change the name in torchao, since it is inconsistent to call every recipe by the abbreviated names (mxfp8/mxfp4/nvfp4/etc) but only call float8 by the fully spelled out name.

Secondarily, I'm also a bit concerned about this API getting too verbose, e.g. --quantize.grouped_mm.float8.recipe_name so trying to make things more concise (i.e., GroupedGEMM -> GroupedMM, Float8 -> FP8)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on second thought, there's already multiple blogs and tutorials etc out there all using "float8" ... I think it would be worse to potentially confuse or add friction to all future readers of those blogs by changing torchao feature/API to use "fp8" ... I'll just keep it Float8* here

Comment thread torchtitan/config/job_config.py Outdated
class Dense:
float8: Float8Dense = field(default_factory=Float8Dense)
"""Float8 training config for dense layers"""
class LinearConfig:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In terms of style, we shouldn't add the Config suffix to the configs. This is only trying to be consistent -- I can imagine a repo where all configs have the config suffix.

Also to make them meaningful, we probably should use QuantizeLinear instead of just Linear.

@danielvegamyhre danielvegamyhre Sep 30, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about this too but "XYZLinear" is the common naming convention for actual nn.Linear subclass using XYZ customization - not just in torchao, but in other codebases as well.

So it sounds like it's a linear layer, and I wanted the difference between Linear configs and the Linear layer to be clear from the naming.

I don't have strong opinions on this though, just sharing context. I am fine with making it QuantizedLinear and removing Config suffixes.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file needs update

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, accidentally had ~/torchtitan/torchtitan open instead of ~torchtitan/, so replace all missed these other dirs.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Comment thread docs/float8.md

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

class Dense:
float8: Float8Dense = field(default_factory=Float8Dense)
"""Float8 training config for dense layers"""
class QuantizedLinear:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about this too but "XYZLinear" is the common naming convention for actual nn.Linear subclass using XYZ customization - not just in torchao, but in other codebases as well.
So it sounds like it's a linear layer, and I wanted the difference between Linear configs and the Linear layer to be clear from the naming.
I don't have strong opinions on this though, just sharing context. I am fine with making it QuantizedLinear and remove Config suffixes.

Good point. Given they are not supposed to be actually used by users, I think it's fine for now to do it this way today. Let's see how things goes.

@danielvegamyhre danielvegamyhre changed the title [quantization] refactor quantization config naming; validate only one quantization method is used [quantization] refactor quantization API; validate only one quantization method is used Sep 30, 2025
@danielvegamyhre danielvegamyhre changed the title [quantization] refactor quantization API; validate only one quantization method is used [quantization] Refactor quantization API Sep 30, 2025

@tianyu-l tianyu-l left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks neat, thank you!

@tianyu-l
tianyu-l merged commit 8a2686c into pytorch:main Sep 30, 2025
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 13, 2026
## Summary
- Float8 training and mx training cannot be combined. Add validation to
ensure either one or the other is used.
- Fix tomls which still use old config naming (addresses pytorch#1764)
- Refactor quantization config naming so (1) job_config class names are
consistent with converter names, and (2) naming is more explicit and
aligned with semantics of what conversions actually happen
   - Old: 
       - Converter classes: 
           - `Float8Converter`
           - `Float8MoEConverter` 
          - `MXConverter`
           - `MXMoEConverter`
       - User-facing args: 
            - `quantize.dense.float8`
            - `quantize.moe.float8`
            - `quantize.dense.mx`
            - `quantize.moe.mx`
   - New:
       - Converter classes: 
           - `Float8LinearConverter`
           - `Float8GroupedGEMMConverter`
           - `MXLinearConverter`
           - `MXGroupedGEMMConverter`
       - User-facing args: 
            - `quantize.linear.float8`
            - `quantize.grouped_mm.float8`
            - `quantize.linear.mx`
            - `quantize.grouped_mm.mx`

## Tests: mx on B200
- Test new config/args with valid job:
- `NGPU=4
CONFIG_FILE="./torchtitan/experiments/llama4/train_configs/debug_model.toml"
./run_train.sh --parallelism.data_parallel_shard_degree=4
--parallelism.expert_parallel_degree=1
--parallelism.tensor_parallel_degree=1 --model.print-after-conversion
--metrics.log_freq=10 --training.steps=30
--model.converters="quantize.linear.mx,quantize.grouped_mm.mx"
--quantize.linear.mx.recipe_name="mxfp8_cublas" --quantize.groupe
d_mm.mx.fqns="experts" --compile.enable`
    - Result: training as expected 
- Test mixing converters for different quantization methods, validate
error:
- `NGPU=4
CONFIG_FILE="./torchtitan/experiments/llama4/train_configs/debug_model.toml"
./run_train.sh --parallelism.data_parallel_shard_degree=4
--parallelism.expert_parallel_degree=1
--parallelism.tensor_parallel_degree=1 --model.print-after-conversion
--metrics.log_freq=10 --training.steps=30
--model.converters="quantize.linear.fp8,quantize.grouped_mm.mx"
--quantize.linear.mx.recipe_name="mxfp8_cublas"
--quantize.grouped_mm.mx.fqns="experts" --compile.enable`
- Result `AssertionError: Cannot combine model converters with different
quantization types: 'mx' and 'fp8'`

## Tests: FP8 rowwise on H100
- Test new config/args with valid job: 
- `NGPU=4
CONFIG_FILE="./torchtitan/experiments/llama4/train_configs/debug_model.toml"
./run_train.sh --parallelism.data_parallel_shard_degree=4
--parallelism.expert_parallel_degree=1
--parallelism.tensor_parallel_degree=1 --model.print-after-conversion
--metrics.log_freq=10 --training.steps=30
--model.converters="quantize.linear.float8,quantize.grouped_mm.float8"
--quantize.linear.float8.recipe_name="rowwise"
--quantize.grouped_mm.float8.fqns="experts" --compile.enable`
    - Result: training as expected
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 25, 2026
## Summary
- Float8 training and mx training cannot be combined. Add validation to
ensure either one or the other is used.
- Fix tomls which still use old config naming (addresses pytorch#1764)
- Refactor quantization config naming so (1) job_config class names are
consistent with converter names, and (2) naming is more explicit and
aligned with semantics of what conversions actually happen
   - Old: 
       - Converter classes: 
           - `Float8Converter`
           - `Float8MoEConverter` 
          - `MXConverter`
           - `MXMoEConverter`
       - User-facing args: 
            - `quantize.dense.float8`
            - `quantize.moe.float8`
            - `quantize.dense.mx`
            - `quantize.moe.mx`
   - New:
       - Converter classes: 
           - `Float8LinearConverter`
           - `Float8GroupedGEMMConverter`
           - `MXLinearConverter`
           - `MXGroupedGEMMConverter`
       - User-facing args: 
            - `quantize.linear.float8`
            - `quantize.grouped_mm.float8`
            - `quantize.linear.mx`
            - `quantize.grouped_mm.mx`

## Tests: mx on B200
- Test new config/args with valid job:
- `NGPU=4
CONFIG_FILE="./torchtitan/experiments/llama4/train_configs/debug_model.toml"
./run_train.sh --parallelism.data_parallel_shard_degree=4
--parallelism.expert_parallel_degree=1
--parallelism.tensor_parallel_degree=1 --model.print-after-conversion
--metrics.log_freq=10 --training.steps=30
--model.converters="quantize.linear.mx,quantize.grouped_mm.mx"
--quantize.linear.mx.recipe_name="mxfp8_cublas" --quantize.groupe
d_mm.mx.fqns="experts" --compile.enable`
    - Result: training as expected 
- Test mixing converters for different quantization methods, validate
error:
- `NGPU=4
CONFIG_FILE="./torchtitan/experiments/llama4/train_configs/debug_model.toml"
./run_train.sh --parallelism.data_parallel_shard_degree=4
--parallelism.expert_parallel_degree=1
--parallelism.tensor_parallel_degree=1 --model.print-after-conversion
--metrics.log_freq=10 --training.steps=30
--model.converters="quantize.linear.fp8,quantize.grouped_mm.mx"
--quantize.linear.mx.recipe_name="mxfp8_cublas"
--quantize.grouped_mm.mx.fqns="experts" --compile.enable`
- Result `AssertionError: Cannot combine model converters with different
quantization types: 'mx' and 'fp8'`

## Tests: FP8 rowwise on H100
- Test new config/args with valid job: 
- `NGPU=4
CONFIG_FILE="./torchtitan/experiments/llama4/train_configs/debug_model.toml"
./run_train.sh --parallelism.data_parallel_shard_degree=4
--parallelism.expert_parallel_degree=1
--parallelism.tensor_parallel_degree=1 --model.print-after-conversion
--metrics.log_freq=10 --training.steps=30
--model.converters="quantize.linear.float8,quantize.grouped_mm.float8"
--quantize.linear.float8.recipe_name="rowwise"
--quantize.grouped_mm.float8.fqns="experts" --compile.enable`
    - Result: training as expected
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants