Skip to content
Closed
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
48 changes: 28 additions & 20 deletions src/python/py/models/builders/qwen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1013,29 +1013,37 @@ def __init__(self, config, io_dtype, onnx_dtype, ep, cache_dir, extra_options):
# Disable fused RoPE in attention op - we apply mRoPE manually
self.attention_attrs["use_rope_in_attn"] = False

# Mixed-precision quantization for linear attention layers.
# Baseline: whole model INT4. Override linear attention layer nodes
# to INT8 for better accuracy with modest size increase.
# Optional mixed-precision quantization for linear attention layers.
# When enabled via extra_options linear_attention_int8=true, promotes
# linear attention layer nodes from INT4 to INT8 for better accuracy.
#
# Linear attention recurrence accumulates errors across the full sequence,
# unlike softmax attention which normalizes per-step.
int8_nodes = {}
for i, lt in enumerate(self.layer_types):
if lt == "linear_attention":
# All linear attention projections: INT8
for proj in ("in_proj_a", "in_proj_b", "in_proj_qkv", "in_proj_z", "out_proj"):
int8_nodes[f"/model/layers.{i}/linear_attn/{proj}/MatMul"] = {"bits": 8}
# MLP projections in linear attention layers: INT8
for proj in ("gate_proj", "up_proj", "down_proj"):
int8_nodes[f"/model/layers.{i}/mlp/{proj}/MatMul"] = {"bits": 8}

if int8_nodes:
algo_config = self.quant_attrs["int4"].get("algo_config")
if algo_config is not None and hasattr(algo_config, "customized_weight_config"):
algo_config.customized_weight_config.update(int8_nodes)
else:
algo_config = RTNWeightOnlyQuantConfig(customized_weight_config=int8_nodes)
self.quant_attrs["int4"]["algo_config"] = algo_config
linear_attention_int8 = extra_options.get("linear_attention_int8", False)

@apsonawane Akshay Sonawane (apsonawane) Apr 24, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add linear_attention_int8 to the bools list in check_extra_options in builder.py, then just simplify this to
if extra_options.get("linear_attention_int8", False)

if isinstance(linear_attention_int8, bool):
enable_linear_attention_int8 = linear_attention_int8
elif isinstance(linear_attention_int8, str):
linear_attention_int8 = linear_attention_int8.lower()
enable_linear_attention_int8 = linear_attention_int8 in {"true", "1"}
else:
enable_linear_attention_int8 = False

if enable_linear_attention_int8:
int8_nodes = {}
for i, lt in enumerate(self.layer_types):
if lt == "linear_attention":
for proj in ("in_proj_a", "in_proj_b", "in_proj_qkv", "in_proj_z", "out_proj"):
int8_nodes[f"/model/layers.{i}/linear_attn/{proj}/MatMul"] = {"bits": 8}
for proj in ("gate_proj", "up_proj", "down_proj"):
int8_nodes[f"/model/layers.{i}/mlp/{proj}/MatMul"] = {"bits": 8}

if int8_nodes:
algo_config = self.quant_attrs["int4"].get("algo_config")
if algo_config is not None and hasattr(algo_config, "customized_weight_config"):
algo_config.customized_weight_config.update(int8_nodes)
else:
algo_config = RTNWeightOnlyQuantConfig(customized_weight_config=int8_nodes)
self.quant_attrs["int4"]["algo_config"] = algo_config

# Replace standard KV cache I/O with hybrid cache I/O
self._setup_hybrid_cache_io()
Expand Down
Loading