-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
QAT #2590
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
Merged
QAT #2590
Changes from all commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
14538c9
Initial QAT implementation
SalmanMohammadi 646ff68
adding config
SalmanMohammadi 0e47cde
running E2E training
SalmanMohammadi 574e3d6
updating config
SalmanMohammadi 72d4f77
adding callback for fake quant delay
SalmanMohammadi e45ed55
Merge branch 'main' into qat
SalmanMohammadi 00d46ae
updating conf
SalmanMohammadi 99a7aee
correctly delaying quantization
SalmanMohammadi 38e5b75
adding post-train quantize, bugfixes
SalmanMohammadi f1eed54
revamping quantization apis
SalmanMohammadi 86291c5
WIP PTQ support
SalmanMohammadi 98cb0ee
Merge branch 'main' into qat
SalmanMohammadi 9d6c066
adding tests
SalmanMohammadi 16d1ca7
pushing for diff
SalmanMohammadi 4c3cfd1
WIP tearing everything apart and putting it back together
SalmanMohammadi d8361b7
more tests
SalmanMohammadi ba52f1a
more testing, rounding out quantize cli
SalmanMohammadi 983316e
things looking good
SalmanMohammadi a102c45
working state
SalmanMohammadi 92512ac
fixing docs
SalmanMohammadi 47cbc27
more docs
SalmanMohammadi e73f8d5
tidying
SalmanMohammadi 8452510
reverting change
SalmanMohammadi f16f99b
more docs
SalmanMohammadi adb3aed
more comments
SalmanMohammadi 3e8f6f3
more comments
SalmanMohammadi dcc82a3
updating cli
SalmanMohammadi 0433b0c
updating conf
SalmanMohammadi 002a637
merging
SalmanMohammadi 2ed234b
linting
SalmanMohammadi 2ca43c6
fixing docs
SalmanMohammadi 4cd9dd5
Merge branch 'main' into qat
SalmanMohammadi d41d459
fixing import
SalmanMohammadi 9cb48ac
silly billy me
SalmanMohammadi 2f13f42
fixing import
SalmanMohammadi df2fd43
dan suggest good
SalmanMohammadi bcc72d9
fixing test
SalmanMohammadi e0bf629
Update examples/llama-3/3b-qat.yaml
SalmanMohammadi 7f548b1
Update docs/quantize.qmd
SalmanMohammadi 2079592
comments
SalmanMohammadi 86f564c
CI
SalmanMohammadi 529cdb8
Merge branch 'main' into qat
SalmanMohammadi 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
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,32 @@ | ||
| --- | ||
| title: "Quantization Aware Training (QAT)" | ||
| back-to-top-navigation: true | ||
| toc: true | ||
| toc-expand: 2 | ||
| toc-depth: 4 | ||
| --- | ||
|
|
||
| ## Overview | ||
|
|
||
| [Quantization Aware Training](https://pytorch.org/blog/introduction-to-quantization-on-pytorch/#quantization-aware-training) (QAT) is a technique for improving the accuracy of models which are quantized | ||
| by applying "fake" quantizations to the model's weights (and optionally, activations) during training. This fake | ||
| quantization allows for the model to adjust for noise introduced by the quantization, so when the model is eventually | ||
| quantized, the accuracy loss is minimized. We use the quantization techniques implemented in [torchao](https://github.com/pytorch/ao) to provide | ||
| support for QAT and post-training quantization (PTQ) in axolotl. | ||
|
|
||
| We recommend reviewing the excellent QAT tutorial in the [torchtune library](https://pytorch.org/torchtune/main/tutorials/qat_finetune.html#quantizing-the-qat-model), | ||
| and the QAT documentation in the [torchao library](https://github.com/pytorch/ao/tree/main/torchao/quantization/qat), for more details. | ||
|
|
||
| ## Configuring QAT in Axolotl | ||
|
|
||
| To enable QAT in axolotl, add the following to your configuration file: | ||
|
|
||
| ```yaml | ||
| qat: | ||
| activation_dtype: # Optional[str] = "int8". Fake quantization layout to use for activation quantization. Valid options are "int4" and "int8" | ||
| weight_dtype: # Optional[str] = "int8". Fake quantization layout to use for weight quantization. Valid options are "int4" and "int8" | ||
| group_size: # Optional[int] = 32. The number of elements in each group for per-group fake quantization | ||
| fake_quant_after_n_steps: # Optional[int] = None. The number of steps to apply fake quantization after | ||
| ``` | ||
|
|
||
| Once you have finished training, you must quantize your model by using the same quantization configuration which you used to train the model with. You can use the [`quantize` command](./quantize.md) to do this. |
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,53 @@ | ||
| --- | ||
| title: "Quantization with torchao" | ||
| back-to-top-navigation: true | ||
| toc: true | ||
| toc-expand: 2 | ||
| toc-depth: 4 | ||
| --- | ||
|
|
||
| Quantization is a technique to lower the memory footprint of your model, potentially at the cost of accuracy or model performance. We support quantizing your model using the [torchao](https://github.com/pytorch/ao) library. Quantization is supported for both post-training quantization (PTQ) and quantization-aware training (QAT). | ||
|
|
||
|
|
||
| ::: {.callout-note} | ||
|
|
||
| We do not currently support quantization techniques such as GGUF/GPTQ,EXL2 at the moment. | ||
|
|
||
| ::: | ||
|
|
||
| ## Configuring Quantization in Axolotl | ||
|
|
||
| Quantization is configured using the `quantization` key in your configuration file. | ||
|
|
||
| ```yaml | ||
| base_model: # The path to the model to quantize. | ||
| quantization: | ||
| weight_dtype: # Optional[str] = "int8". Fake quantization layout to use for weight quantization. Valid options are uintX for X in [1, 2, 3, 4, 5, 6, 7], or int4, or int8 | ||
| activation_dtype: # Optional[str] = "int8". Fake quantization layout to use for activation quantization. Valid options are "int4" and "int8" | ||
| group_size: # Optional[int] = 32. The number of elements in each group for per-group fake quantization | ||
| quantize_embedding: # Optional[bool] = False. Whether to quantize the embedding layer. | ||
|
|
||
| output_dir: # The path to the output directory. | ||
| ``` | ||
|
|
||
| Once quantization is complete, your quantized model will be saved in the `{output_dir}/quantized` directory. | ||
|
|
||
| You may also use the `quantize` command to quantize a model which has been trained with [QAT](./qat.md) - you can do this by using the existing QAT configuration file which | ||
| you used to train the model: | ||
|
|
||
| ```yaml | ||
| # qat.yml | ||
| qat: | ||
| activation_dtype: int8 | ||
| weight_dtype: int8 | ||
| group_size: 256 | ||
| quantize_embedding: true | ||
|
|
||
| output_dir: # The path to the output directory used during training where the final checkpoint has been saved. | ||
| ``` | ||
|
|
||
| ```bash | ||
| axolotl quantize qat.yml | ||
| ``` | ||
|
|
||
| This ensures that an identical quantization configuration is used to quantize the model as was used to train it. | ||
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,79 @@ | ||
| base_model: meta-llama/Llama-3.2-3B | ||
| # Automatically upload checkpoint and final model to HF | ||
| # hub_model_id: username/custom_model_name | ||
|
|
||
| load_in_8bit: false | ||
| load_in_4bit: false | ||
| strict: false | ||
|
|
||
| plugins: | ||
| - axolotl.integrations.liger.LigerPlugin | ||
|
|
||
| liger_rope: true | ||
| liger_rms_norm: true | ||
| liger_glu_activation: true | ||
| liger_layer_norm: true | ||
| liger_fused_linear_cross_entropy: true | ||
|
|
||
| datasets: | ||
| - path: yahma/alpaca-cleaned | ||
| type: alpaca | ||
|
|
||
| output_dir: ./outputs/qat_out/ | ||
|
|
||
| sample_packing: true | ||
| pad_to_sequence_len: true | ||
| sequence_len: 512 | ||
|
|
||
| flex_attention: true | ||
| flex_attn_compile_kwargs: | ||
| dynamic: false | ||
| mode: max-autotune-no-cudagraphs | ||
|
|
||
| qat: | ||
| activation_dtype: int8 | ||
| weight_dtype: int4 | ||
| group_size: 32 | ||
|
|
||
| wandb_project: | ||
| wandb_entity: | ||
| wandb_watch: | ||
| wandb_name: | ||
| wandb_log_model: | ||
|
|
||
| gradient_accumulation_steps: 1 | ||
| micro_batch_size: 16 | ||
| num_epochs: 1 | ||
| optimizer: adamw_torch_fused | ||
|
|
||
| cosine_constant_lr_ratio: 0 | ||
| cosine_min_lr_ratio: 1.0 | ||
| learning_rate: 2e-5 | ||
| save_only_model: true | ||
| bf16: true | ||
|
|
||
| resume_from_checkpoint: | ||
| logging_steps: 1 | ||
SalmanMohammadi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| evals_per_epoch: 1 | ||
| saves_per_epoch: 1 | ||
|
|
||
| warmup_steps: 10 | ||
| weight_decay: 0.0 | ||
| fsdp: | ||
| - full_shard | ||
| - auto_wrap | ||
|
|
||
| fsdp_config: | ||
| fsdp_version: 2 | ||
| fsdp_offload_params: false | ||
| fsdp_cpu_ram_efficient_loading: true | ||
| fsdp_auto_wrap_policy: TRANSFORMER_BASED_WRAP | ||
| fsdp_transformer_layer_cls_to_wrap: LlamaDecoderLayer | ||
| fsdp_state_dict_type: FULL_STATE_DICT | ||
| fsdp_sharding_strategy: FULL_SHARD | ||
| fsdp_reshard_after_forward: true | ||
| fsdp_activation_checkpointing: true | ||
|
|
||
| special_tokens: | ||
| pad_token: <|end_of_text|> | ||
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,78 @@ | ||
| base_model: Qwen/Qwen3-8B | ||
| # Automatically upload checkpoint and final model to HF | ||
| # hub_model_id: username/custom_model_name | ||
|
|
||
| load_in_8bit: false | ||
| load_in_4bit: false | ||
| strict: false | ||
|
|
||
| plugins: | ||
| - axolotl.integrations.liger.LigerPlugin | ||
|
|
||
| liger_rope: true | ||
| liger_rms_norm: true | ||
| liger_glu_activation: true | ||
| liger_layer_norm: true | ||
| liger_fused_linear_cross_entropy: true | ||
|
|
||
| datasets: | ||
| - path: tatsu-lab/alpaca | ||
| type: alpaca | ||
|
|
||
| output_dir: ./outputs/qat_out/ | ||
|
|
||
| sequence_len: 2048 | ||
| sample_packing: true | ||
| flex_attention: true | ||
| pad_to_sequence_len: true | ||
|
|
||
| flex_attn_compile_kwargs: | ||
| dynamic: false | ||
| mode: max-autotune-no-cudagraphs | ||
|
|
||
| qat: | ||
| activation_dtype: int8 | ||
| weight_dtype: int4 | ||
| group_size: 256 | ||
| fake_quant_after_n_steps: 1000 | ||
|
|
||
| wandb_project: | ||
| wandb_entity: | ||
| wandb_watch: | ||
| wandb_name: | ||
| wandb_log_model: | ||
|
|
||
| gradient_accumulation_steps: 1 | ||
| micro_batch_size: 2 | ||
| max_steps: 2000 | ||
| optimizer: adamw_torch_fused | ||
| lr_scheduler: cosine | ||
| learning_rate: 2e-5 | ||
|
|
||
| bf16: true | ||
| tf32: true | ||
|
|
||
| resume_from_checkpoint: | ||
| logging_steps: 1 | ||
SalmanMohammadi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| evals_per_epoch: 1 | ||
| saves_per_epoch: 1 | ||
|
|
||
| warmup_steps: 10 | ||
| weight_decay: 0.0 | ||
| fsdp: | ||
| - full_shard | ||
| - auto_wrap | ||
|
|
||
| fsdp_config: | ||
| fsdp_version: 2 | ||
| fsdp_offload_params: false | ||
| fsdp_cpu_ram_efficient_loading: true | ||
| fsdp_auto_wrap_policy: TRANSFORMER_BASED_WRAP | ||
| fsdp_transformer_layer_cls_to_wrap: Qwen3DecoderLayer | ||
| fsdp_state_dict_type: FULL_STATE_DICT | ||
| fsdp_sharding_strategy: FULL_SHARD | ||
| fsdp_reshard_after_forward: true | ||
| fsdp_activation_checkpointing: true | ||
|
|
||
| special_tokens: | ||
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 |
|---|---|---|
|
|
@@ -90,6 +90,18 @@ class VllmServeCliArgs: | |
| ) | ||
|
|
||
|
|
||
| @dataclass | ||
| class QuantizeCliArgs: | ||
|
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. Since these are part of the pydantic model config schema, I think we don't need to duplicate here. As an aside, we can probably get rid of these CLI arg classes in favor of using all pydantic model fields? |
||
| """Dataclass with CLI arguments for `axolotl quantize` command.""" | ||
|
|
||
| base_model: Optional[str] = field(default=None) | ||
| weight_dtype: Optional[str] = field(default=None) | ||
| activation_dtype: Optional[str] = field(default=None) | ||
| quantize_embedding: Optional[bool] = field(default=None) | ||
| group_size: Optional[int] = field(default=None) | ||
| output_dir: Optional[str] = field(default=None) | ||
|
|
||
|
|
||
| @dataclass | ||
| class EvaluateCliArgs: | ||
| """Dataclass with CLI arguments for `axolotl evaluate` command.""" | ||
|
|
||
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.