[NPU] [Quant] Support HunyuanImage3 offline quantization with vLLM-Ascend on diffusion path (#2979)
Signed-off-by: betta18 <jiangmengyu1@huawei.com> Signed-off-by: jiangmengyu18 <56633611+jiangmengyu18@users.noreply.github.com> Co-authored-by: betta18 <jiangmengyu1@huawei.com>
This commit is contained in:
1 parent
e375b12680
commit
c174c95ab0
11 files changed
+147
-18
No files matched your search
@@ -1484,7 +1484,7 @@ class HunYuanSparseMoeBlock(nn.Module):
|
||||
config.hidden_size,
|
||||
config.num_experts,
|
||||
bias=False,
|
||||
quant_config=None,
|
||||
quant_config=quant_config,
|
||||
prefix=f"{prefix}.gate",
|
||||
)
|
||||
if config.use_mixed_mlp_moe > 0:
|
||||
@@ -1658,8 +1658,10 @@ class HunYuanAttention(nn.Module):
|
||||
custom_pos_emb: tuple[torch.FloatTensor] | None = None,
|
||||
**kwargs,
|
||||
) -> torch.Tensor:
|
||||
bsz, q_len, _ = hidden_states.size()
|
||||
bsz, q_len, hidden_size = hidden_states.size()
|
||||
hidden_states = hidden_states.reshape(-1, hidden_size)
|
||||
qkv, _ = self.qkv_proj(hidden_states)
|
||||
qkv = qkv.reshape(bsz, q_len, -1)
|
||||
q, k, v = qkv.split([self.q_size, self.kv_size, self.kv_size], dim=-1)
|
||||
|
||||
past_key_value: Cache | None = kwargs.get("past_key_value", None)
|
||||
@@ -1723,7 +1725,7 @@ class HunyuanImage3DecoderLayer(nn.Module):
|
||||
rope_theta=rope_theta,
|
||||
rope_scaling=rope_scaling,
|
||||
max_position_embeddings=max_position_embeddings,
|
||||
quant_config=None,
|
||||
quant_config=quant_config,
|
||||
bias=attention_bias,
|
||||
cache_config=None,
|
||||
prefix=f"{prefix}.self_attn",
|
||||
@@ -1933,7 +1935,7 @@ class HunyuanImage3Model(nn.Module):
|
||||
layer_idx=int(prefix.split(".")[-1]),
|
||||
prefix=prefix,
|
||||
),
|
||||
prefix=f"{prefix}.layers",
|
||||
prefix=f"{prefix}.layers" if prefix else "layers",
|
||||
)
|
||||
if get_pp_group().is_last_rank:
|
||||
self.norm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
||||
@@ -1948,7 +1950,7 @@ class HunyuanImage3Model(nn.Module):
|
||||
num_attention_heads = self.config.num_attention_heads
|
||||
num_kv_heads = getattr(self.config, "num_key_value_heads", self.config.num_attention_heads)
|
||||
num_key_value_groups = num_attention_heads // num_kv_heads
|
||||
hidden_size = self.config.hidden_size
|
||||
hidden_size = qkv.shape[1]
|
||||
|
||||
if hasattr(self.config, "head_dim"):
|
||||
attention_head_dim = self.config.head_dim
|
||||
@@ -2001,8 +2003,15 @@ class HunyuanImage3Model(nn.Module):
|
||||
split_params_mapping = [
|
||||
(".gate_up_proj", ".gate_and_up_proj", 2, [(1, 1), (0, 1)], None),
|
||||
(
|
||||
".qkv_proj",
|
||||
".qkv_proj",
|
||||
".qkv_proj.weight",
|
||||
".qkv_proj.weight",
|
||||
num_attention_heads + num_kv_heads * 2,
|
||||
[("q", num_attention_heads), ("k", num_kv_heads), ("v", num_kv_heads)],
|
||||
self._split_qkv_weight,
|
||||
),
|
||||
(
|
||||
".qkv_proj.weight_scale",
|
||||
".qkv_proj.weight_scale",
|
||||
num_attention_heads + num_kv_heads * 2,
|
||||
[("q", num_attention_heads), ("k", num_kv_heads), ("v", num_kv_heads)],
|
||||
self._split_qkv_weight,
|
||||
@@ -2101,6 +2110,8 @@ class HunyuanImage3Model(nn.Module):
|
||||
continue
|
||||
if "mlp.experts" in name:
|
||||
continue
|
||||
if ".qkv_proj" in name and not name.endswith(weight_name):
|
||||
continue
|
||||
name = name.replace(weight_name, param_name)
|
||||
# Skip loading extra bias for GPTQ models.
|
||||
if name.endswith(".bias") and name not in params_dict:
|
||||
|
||||
@@ -14,7 +14,7 @@ from transformers.generation.utils import ALL_CACHE_NAMES, GenerationMixin
|
||||
from transformers.models.siglip2 import Siglip2VisionConfig, Siglip2VisionModel
|
||||
from transformers.utils.generic import ModelOutput
|
||||
from vllm.config.vllm import get_current_vllm_config
|
||||
from vllm.model_executor.models.utils import AutoWeightsLoader
|
||||
from vllm.model_executor.models.utils import AutoWeightsLoader, WeightsMapper
|
||||
from vllm.transformers_utils.config import get_config
|
||||
|
||||
from vllm_omni.diffusion.data import DiffusionOutput, OmniDiffusionConfig
|
||||
@@ -64,6 +64,15 @@ def to_device(data, device):
|
||||
|
||||
|
||||
class HunyuanImage3Pipeline(HunyuanImage3PreTrainedModel, GenerationMixin, DiffusionPipelineProfilerMixin):
|
||||
hf_to_vllm_mapper = WeightsMapper(
|
||||
orig_to_new_prefix={
|
||||
"model.": "",
|
||||
},
|
||||
orig_to_new_substr={
|
||||
"mlp.gate.wg.": "mlp.gate.",
|
||||
"gate_and_up_proj.": "gate_up_proj.",
|
||||
},
|
||||
)
|
||||
_PROFILER_TARGETS = [
|
||||
"model.forward",
|
||||
"model.layers[0].forward",
|
||||
|
||||
Reference in new issue
Block a user