-
Notifications
You must be signed in to change notification settings - Fork 288
[AWQ] Generalize AWQ quantization #1961
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?
Conversation
Signed-off-by: Kyle Sayers <[email protected]>
Signed-off-by: Kyle Sayers <[email protected]>
Signed-off-by: Kyle Sayers <[email protected]>
|
👋 Hi! Thank you for contributing to llm-compressor. Please add the ready label when the PR is ready for review. Note: This is required to complete the testing suite, please only add the label once the PR is code complete and local testing has been performed. |
Signed-off-by: Brian Dellabetta <[email protected]>
Signed-off-by: Brian Dellabetta <[email protected]>
Signed-off-by: Brian Dellabetta <[email protected]>
Signed-off-by: Brian Dellabetta <[email protected]>
Signed-off-by: Brian Dellabetta <[email protected]>
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.
I think that so long as you feel confident that _compute_layer_means is going to work as expected for all the supported strategies, then I think this looks good to me!
| module=balance_layer, | ||
| ) | ||
| for balance_layer in mapping.balance_layers | ||
| if hasattr(balance_layer, "quantization_scheme") |
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.
Will this function error if balance layer doesn't have a quantization scheme?
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.
I updated this to skip balance_layers that don't have a quant schema, if that does arise. So this should be robust enough to still work when someone wants to update a mapping like input_layernorm -> q/k/v proj, but does NOT want to quantize all q/k/v proj 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.
Nice, that seems like the most robust solution.
|
|
||
| return w, scales, zeros | ||
| for layer in layers: | ||
| if not hasattr(layer, "weight"): |
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.
I feel like a lot of this algorithm assumes that layers have weights? Should we be silently skipping here?
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.
To date, all the balance_layers are linear layers. I could just change the AWQMapping type to linear and avoid this checking logic. WDYT?
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.
Anything's good with me, I prefer loud errors if assumptions are violated in this case.
| weight = layer.weight | ||
| org_shape = weight.shape | ||
|
|
||
| # If group-wise, calculate abs max based on group |
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.
Will this generalize to block or tensor quant? I think I personally need to get a better understanding of what "group normalization" is supposed to do and how it applies to other quant strategies.
Writing this function with torch native vectorized ops might help, I think it might be reducible to frobenius norm and mean/sum ops, but that doesn't have to be done now.
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.
for tensor, i don't think using channel-wise means makes sense. We could validate that if need be, i.e. throw validation error if strategy==TENSOR and duo_scaling != False.
For block, we'd have to update this logic, yeah. I'll leave as a todo for now
Signed-off-by: Brian Dellabetta <[email protected]>
Signed-off-by: Brian Dellabetta <[email protected]>
Signed-off-by: Brian Dellabetta <[email protected]>
Signed-off-by: Brian Dellabetta <[email protected]>
Signed-off-by: Brian Dellabetta <[email protected]>
Signed-off-by: Brian Dellabetta <[email protected]>
kylesayrs
left a comment
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.
Approve from my side
fynnsu
left a comment
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.
Looks good, added a couple comments below!
| if weight_total_sum is None: | ||
| weight_total_sum = weight_sum | ||
| else: | ||
| weight_total_sum += weight_sum |
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.
This seems a little strange to me. Can't we just initialize weight_total_sum = 0.0?
| history.append(loss) | ||
| if loss < best_error: | ||
| best_error = loss | ||
| best_duo_scaling = use_duo_scaling | ||
| best_ratio = ratio | ||
| best_scales = scales.clone() |
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.
It seems like history is currently just used for debugging when no best_ratio is found. I wonder if we could instead be recording saving the hyperparameter states and losses and printing them everytime (when in DEBUG logging mode.
e.g.
for grid_idx, use_duo_scaling in product(range(n_grid), duo_scalings):
ratio = grid_idx / n_grid
...
history.append({"ratio": ratio, "duo_scaling": use_duo_scaling, "error": loss})
...
logger.debug(history)This might be useful in the future as we look into improving the hyperparameter search / to get a sense of what parameters are most often selected. I think including the ratio/duo_scaling in some way is important now that we've switched from a simple linear search to a grid search, so that's easy to tell which arguments are being set.
Summary
To allow for arbitrary heterogeneous quantization schemes, this PR switches several helpers from AutoAWQ to the observer and QDQ logic. AWQ no longer constrains that the quantization config needs to have the same settings for group_size, symmetric, and num_bits for each config_group.
Resolves #1657
Prerequisites:
Test plan
llm-compressor/examples/awq/llama_example.pywith this (withduo_scaling="both") and logging the best configuration of(ratio, duo_scaling), I see a good mix of Falses and Trues. i.e. a good percentage of best_scales were found with duo_scaling=False and a good percentage were found withduo_scaling=True. Generated model output looks good.awq_one_shot.py(pasted below), Wikitext PPL is consistent for w4a16 and w4a16_asym on this branch when compared to main, and better than what was reported in a previous AWQ PR, but those might have been differently configured. For W4A16_ASYM, the results are both 13.41 for main and this branch. This is what we've been historically using to test regressions.CADENCE=weekly TEST_DATA_FILE=~/projects/llm-compressor/tests/lmeval/configs/w4a16_awq_sym.yaml pytest -s ~/projects/llm-compressor/tests/lmeval/test_lmeval.pyon this branch, which causes the test to fail. This persists even when usingpseudo_quantize_tensorinstead ofcall_observer/forward_quantize, as shown in this diff. I get the same result in this diff, so at least that means quantization logic in CT is consistent with AutoAWQOutput:
This is already a pretty high drop in recovery, should we revisit this test?
awq_oneshot.py script
```python import osos.environ["VLLM_WORKER_MULTIPROC_METHOD"] = "spawn"
from datasets import load_dataset
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
from llmcompressor import oneshot, active_session
from llmcompressor.utils import dispatch_for_generation
from llmcompressor.modifiers.awq import AWQModifier, AWQMapping
from llmcompressor.modifiers.quantization import QuantizationModifier
from compressed_tensors.quantization import (
QuantizationArgs,
QuantizationScheme,
QuantizationStrategy,
QuantizationType,
)
MODEL_ID = "meta-llama/Llama-3.2-3B-Instruct"
SAVE_DIR = MODEL_ID.split("/")[-1] + "-awq-asym"
Configure the quantization algorithm to run.
recipe = [
AWQModifier(
ignore=[
"lm_head",
"re:.*mlp.gate$",
"re:.mlp.shared_expert_gate$",
"re:visual.",
],
scheme="W4A16_ASYM",
duo_scaling="both",
targets=["Linear"],
# offload_device=torch.device("cpu"),
),
]
Select calibration dataset.
DATASET_ID = "mit-han-lab/pile-val-backup"
DATASET_SPLIT = "validation"
Select number of samples. 256 samples is a good place to start.
Increasing the number of samples can improve accuracy.
NUM_CALIBRATION_SAMPLES = 256
MAX_SEQUENCE_LENGTH = 512
def get_calib_dataset(tokenizer):
from datasets import load_dataset
if name == "main":
model = AutoModelForCausalLM.from_pretrained(
MODEL_ID, torch_dtype="auto", trust_remote_code=True
)
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)