Skip to content

Replaced int4 string with torch.int4 #11845

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 1 commit into from
Jun 23, 2025
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
2 changes: 1 addition & 1 deletion backends/qualcomm/quantizer/custom_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def annotate_matmul_input1(node: Node):
)
quantization_config_8a4w_per_channel = get_ptq_per_channel_quant_config(
act_dtype=torch.uint8,
weight_dtype="int4",
weight_dtype=torch.int4,
act_observer=MinMaxObserver,
act_symmetric=True,
)
Expand Down
38 changes: 22 additions & 16 deletions backends/qualcomm/quantizer/qconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,7 @@ def get_ptq_per_channel_quant_config(
torch.int8,
torch.int16,
}
# TODO accept "int4" temporally. Remove "int4" when torch support torch.int4 dtype
supported_weight_dtypes = {"int4", torch.int8, torch.int16}
supported_weight_dtypes = {torch.int4, torch.int8, torch.int16}
assert (
act_dtype in supported_act_types
), f"act_dtype, {act_dtype} is not one of supported types, {supported_act_types}"
Expand Down Expand Up @@ -276,9 +275,11 @@ def get_ptq_per_channel_quant_config(
)

weight_quantization_spec = QuantizationSpec(
dtype=torch.int8 if weight_dtype == "int4" else weight_dtype,
quant_min=-7 if weight_dtype == "int4" else torch.iinfo(weight_dtype).min + 1,
quant_max=7 if weight_dtype == "int4" else torch.iinfo(weight_dtype).max,
dtype=torch.int8 if weight_dtype == torch.int4 else weight_dtype,
quant_min=(
-7 if weight_dtype == torch.int4 else torch.iinfo(weight_dtype).min + 1
),
quant_max=7 if weight_dtype == torch.int4 else torch.iinfo(weight_dtype).max,
qscheme=torch.per_channel_symmetric,
ch_axis=0,
observer_or_fake_quant_ctr=PerChannelMinMaxObserver.with_args(**extra_args),
Expand Down Expand Up @@ -310,9 +311,11 @@ def get_ptq_per_block_quant_config(
act_symmetric=act_symmetric,
)
weight_quantization_spec = QuantizationSpec(
dtype=torch.int8 if weight_dtype == "int4" else weight_dtype,
quant_min=-7 if weight_dtype == "int4" else torch.iinfo(weight_dtype).min + 1,
quant_max=7 if weight_dtype == "int4" else torch.iinfo(weight_dtype).max,
dtype=torch.int8 if weight_dtype == torch.int4 else weight_dtype,
quant_min=(
-7 if weight_dtype == torch.int4 else torch.iinfo(weight_dtype).min + 1
),
quant_max=7 if weight_dtype == torch.int4 else torch.iinfo(weight_dtype).max,
qscheme=torch.per_channel_symmetric,
ch_axis=0,
observer_or_fake_quant_ctr=PerBlockParamObserver.with_args(**extra_args),
Expand Down Expand Up @@ -463,8 +466,7 @@ def get_qat_per_channel_quant_config(
torch.int8,
torch.int16,
}
# TODO accept "int4" temporally. Remove "int4" when torch support torch.int4 dtype
supported_weight_dtypes = {"int4", torch.int8, torch.int16}
supported_weight_dtypes = {torch.int4, torch.int8, torch.int16}
assert (
act_dtype in supported_act_types
), f"act_dtype, {act_dtype} is not one of supported types, {supported_act_types}"
Expand All @@ -491,17 +493,21 @@ def get_qat_per_channel_quant_config(
)

weight_fake_quant_ctr = FusedMovingAvgObsFakeQuantize.with_args(
dtype=torch.int8 if weight_dtype == "int4" else weight_dtype,
quant_min=-7 if weight_dtype == "int4" else torch.iinfo(weight_dtype).min + 1,
quant_max=7 if weight_dtype == "int4" else torch.iinfo(weight_dtype).max,
dtype=torch.int8 if weight_dtype == torch.int4 else weight_dtype,
quant_min=(
-7 if weight_dtype == torch.int4 else torch.iinfo(weight_dtype).min + 1
),
quant_max=7 if weight_dtype == torch.int4 else torch.iinfo(weight_dtype).max,
qscheme=torch.per_channel_symmetric,
ch_axis=0,
observer=MovingAveragePerChannelMinMaxObserver,
)
weight_quantization_spec = QuantizationSpec(
dtype=torch.int8 if weight_dtype == "int4" else weight_dtype,
quant_min=-7 if weight_dtype == "int4" else torch.iinfo(weight_dtype).min + 1,
quant_max=7 if weight_dtype == "int4" else torch.iinfo(weight_dtype).max,
dtype=torch.int8 if weight_dtype == torch.int4 else weight_dtype,
quant_min=(
-7 if weight_dtype == torch.int4 else torch.iinfo(weight_dtype).min + 1
),
quant_max=7 if weight_dtype == torch.int4 else torch.iinfo(weight_dtype).max,
qscheme=torch.per_channel_symmetric,
ch_axis=0,
observer_or_fake_quant_ctr=weight_fake_quant_ctr,
Expand Down
8 changes: 4 additions & 4 deletions backends/qualcomm/quantizer/quantizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class QuantDtype(IntEnum):
partial(
get_ptq_per_channel_quant_config,
act_dtype=torch.uint16,
weight_dtype="int4",
weight_dtype=torch.int4,
),
None,
),
Expand All @@ -94,12 +94,12 @@ class QuantDtype(IntEnum):
partial(
get_ptq_per_channel_quant_config,
act_dtype=torch.uint16,
weight_dtype="int4",
weight_dtype=torch.int4,
),
partial(
get_ptq_per_block_quant_config,
act_dtype=torch.uint16,
weight_dtype="int4",
weight_dtype=torch.int4,
),
),
(QuantDtype.use_8a8w, False): (
Expand All @@ -113,7 +113,7 @@ class QuantDtype(IntEnum):
partial(
get_qat_per_channel_quant_config,
act_dtype=torch.uint16,
weight_dtype="int4",
weight_dtype=torch.int4,
),
None,
),
Expand Down
Loading