Skip to content
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

Implement apply_clip argument to quantize() #427

Merged
merged 3 commits into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions awq/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ def quantize(
"This argument avoids real quantization by only applying the scales without quantizing down to FP16."
),
] = False,
apply_clip: Annotated[
bool,
Doc(
"Whether to apply clipping to the model during quantization. Some models may perform better with this set to False."
),
] = True,
):
"""
The main quantization function that you can use to quantize your model.
Expand Down Expand Up @@ -173,6 +179,7 @@ def quantize(
duo_scaling,
modules_to_not_convert=self.quant_config.modules_to_not_convert,
export_compatible=export_compatible,
apply_clip=apply_clip,
)
self.quantizer.quantize()

Expand Down
17 changes: 10 additions & 7 deletions awq/quantize/quantizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def __init__(
duo_scaling,
modules_to_not_convert=None,
export_compatible=False,
apply_clip=True,
) -> None:
self.awq_model = awq_model
self.model = model
Expand All @@ -53,6 +54,7 @@ def __init__(
self.text_column = text_column
self.duo_scaling = duo_scaling
self.export_compatible = export_compatible
self.apply_clip = apply_clip
self.modules_to_not_convert = (
modules_to_not_convert if modules_to_not_convert is not None else []
)
Expand Down Expand Up @@ -161,13 +163,14 @@ def quantize(self):
)

# [STEP 3]: Compute and apply clipping list
clip_list = self._search_best_clip(
self.modules[i], named_linears, input_feat
)
apply_clip(self.modules[i], clip_list)
clip_list = append_str_prefix(
clip_list, get_op_name(self.model, self.modules[i]) + "."
)
if self.apply_clip:
clip_list = self._search_best_clip(
self.modules[i], named_linears, input_feat
)
apply_clip(self.modules[i], clip_list)
clip_list = append_str_prefix(
clip_list, get_op_name(self.model, self.modules[i]) + "."
)

# [STEP 4]: Quantize weights
if not self.export_compatible:
Expand Down