-
Notifications
You must be signed in to change notification settings - Fork 34
Support torchao MPS 4-bit quantization #197
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
Changes from all commits
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 |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ def quantize_model_( | |
| if not (qlinear_config or qembedding_config): | ||
| return | ||
|
|
||
| from torchao.experimental.quant_api import UIntxWeightOnlyConfig | ||
| from torchao.quantization.granularity import PerAxis, PerGroup | ||
| from torchao.quantization.quant_api import ( | ||
| Int4WeightOnlyConfig, | ||
|
|
@@ -42,9 +43,9 @@ def quantize_model_( | |
|
|
||
| if qembedding_config: | ||
| if qlinear_config == "8w": | ||
| assert ( | ||
| qembedding_group_size == 0 | ||
| ), "8-bit embedding quantization only supports per-token at the moment, please use qembedding_group_size = 0." | ||
| assert qembedding_group_size == 0, ( | ||
| "8-bit embedding quantization only supports per-token at the moment, please use qembedding_group_size = 0." | ||
| ) | ||
| if qembedding_group_size == 0: | ||
| embedding_weight_granularity = PerAxis(0) | ||
| else: | ||
|
|
@@ -101,6 +102,13 @@ def build_linear_config(quant_config_key: str, granularity: str, packing_format: | |
| weight_dtype=torch.int8, | ||
| weight_granularity=PerAxis(0), | ||
| ) | ||
| if quant_config_key == "fpa4w": | ||
| # Need to import to load the ops | ||
| import torchao.experimental.ops.mps # noqa: F401 | ||
|
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. nit should we import this in torchao.experimental.quant_api so that
Contributor
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.
Contributor
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. that's also why I import |
||
| return UIntxWeightOnlyConfig( | ||
| group_size=qlinear_group_size, | ||
| bitwidth=4, | ||
| ) | ||
| raise ValueError(f"Unsupported linear quantization config '{quant_config_key}'.") | ||
|
|
||
| qlinear_configs = [cfg.strip() for cfg in qlinear_config.split(",")] | ||
|
|
@@ -120,9 +128,9 @@ def build_linear_config(quant_config_key: str, granularity: str, packing_format: | |
| ) | ||
| fallback_linear_config_key = None | ||
| else: | ||
| assert ( | ||
| qlinear_group_size % 2 == 0 | ||
| ), f"Linear quantization group size must be a multiple of 2, got {qlinear_group_size}." | ||
| assert qlinear_group_size % 2 == 0, ( | ||
| f"Linear quantization group size must be a multiple of 2, got {qlinear_group_size}." | ||
| ) | ||
| linear_weight_granularity = PerGroup(qlinear_group_size) | ||
|
|
||
| logging.info("Quantizing linear layers.") | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does
fpa4wwork on backends other than metal?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it only works with Metal
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a check then, if a user pass
--qlinear fpa4wand--device mpsat the same timeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done!