[diffusion] feat: support nunchaku for Z-Image-Turbo and flux.1 (int4)#18959
Merged
[diffusion] feat: support nunchaku for Z-Image-Turbo and flux.1 (int4)#18959
Conversation
Contributor
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
ping1jing2
reviewed
Feb 19, 2026
Comment on lines
+29
to
+55
| # HF diffusers format | ||
| r"^transformer\.(\w*)\.(.*)$": r"\1.\2", | ||
| # transformer_blocks nunchaku format (raw export - before internal conversion) | ||
| r"^transformer_blocks\.(\d+)\.mlp_fc1\.(.*)$": r"transformer_blocks.\1.ff.net.0.proj.\2", | ||
| r"^transformer_blocks\.(\d+)\.mlp_fc2\.(.*)$": r"transformer_blocks.\1.ff.net.2.\2", | ||
| r"^transformer_blocks\.(\d+)\.mlp_context_fc1\.(.*)$": r"transformer_blocks.\1.ff_context.net.0.proj.\2", | ||
| r"^transformer_blocks\.(\d+)\.mlp_context_fc2\.(.*)$": r"transformer_blocks.\1.ff_context.net.2.\2", | ||
| r"^transformer_blocks\.(\d+)\.qkv_proj\.(.*)$": r"transformer_blocks.\1.attn.to_qkv.\2", | ||
| r"^transformer_blocks\.(\d+)\.qkv_proj_context\.(.*)$": r"transformer_blocks.\1.attn.to_added_qkv.\2", | ||
| r"^transformer_blocks\.(\d+)\.out_proj\.(.*)$": r"transformer_blocks.\1.attn.to_out.0.\2", | ||
| r"^transformer_blocks\.(\d+)\.out_proj_context\.(.*)$": r"transformer_blocks.\1.attn.to_add_out.\2", | ||
| r"^transformer_blocks\.(\d+)\.norm_q\.(.*)$": r"transformer_blocks.\1.attn.norm_q.\2", | ||
| r"^transformer_blocks\.(\d+)\.norm_k\.(.*)$": r"transformer_blocks.\1.attn.norm_k.\2", | ||
| r"^transformer_blocks\.(\d+)\.norm_added_q\.(.*)$": r"transformer_blocks.\1.attn.norm_added_q.\2", | ||
| r"^transformer_blocks\.(\d+)\.norm_added_k\.(.*)$": r"transformer_blocks.\1.attn.norm_added_k.\2", | ||
| # transformer_blocks nunchaku format (already converted with convert_flux_state_dict) | ||
| r"^transformer_blocks\.(\d+)\.attn\.add_qkv_proj\.(.*)$": r"transformer_blocks.\1.attn.to_added_qkv.\2", | ||
| # single_transformer_blocks nunchaku format (raw export - before internal conversion) | ||
| r"^single_transformer_blocks\.(\d+)\.qkv_proj\.(.*)$": r"single_transformer_blocks.\1.attn.to_qkv.\2", | ||
| r"^single_transformer_blocks\.(\d+)\.out_proj\.(.*)$": r"single_transformer_blocks.\1.attn.to_out.0.\2", | ||
| r"^single_transformer_blocks\.(\d+)\.norm_q\.(.*)$": r"single_transformer_blocks.\1.attn.norm_q.\2", | ||
| r"^single_transformer_blocks\.(\d+)\.norm_k\.(.*)$": r"single_transformer_blocks.\1.attn.norm_k.\2", | ||
| # nunchaku quantization parameter name conversions (apply to all blocks) | ||
| r"^(.*)\.smooth_orig$": r"\1.smooth_factor_orig", | ||
| r"^(.*)\.smooth$": r"\1.smooth_factor", | ||
| r"^(.*)\.lora_down$": r"\1.proj_down", | ||
| r"^(.*)\.lora_up$": r"\1.proj_up", |
Collaborator
There was a problem hiding this comment.
According to DRY principle, i think i might be better to change code below:
def get_param_names_mapping():
block_rules = {
# MLP related
r"mlp_fc1\.(.*)$": r"ff.net.0.proj.\1",
r"mlp_fc2\.(.*)$": r"ff.net.2.\1",
r"mlp_context_fc1\.(.*)$": r"ff_context.net.0.proj.\1",
r"mlp_context_fc2\.(.*)$": r"ff_context.net.2.\1",
# Attention related
r"qkv_proj\.(.*)$": r"attn.to_qkv.\1",
r"qkv_proj_context\.(.*)$": r"attn.to_added_qkv.\1",
r"attn\.add_qkv_proj\.(.*)$": r"attn.to_added_qkv.\1",
r"out_proj\.(.*)$": r"attn.to_out.0.\1",
r"out_proj_context\.(.*)$": r"attn.to_add_out.\1",
# Norm related
r"norm_q\.(.*)$": r"attn.norm_q.\1",
r"norm_k\.(.*)$": r"attn.norm_k.\1",
r"norm_added_q\.(.*)$": r"attn.norm_added_q.\1",
r"norm_added_k\.(.*)$": r"attn.norm_added_k.\1",
}
mapping = {
# HF diffusers format
r"^transformer\.(\w*)\.(.*)$": r"\1.\2",
}
prefixes = ["transformer_blocks", "single_transformer_blocks"]
for prefix in prefixes:
for pattern, replacement in block_rules.items():
mapping[rf"^{prefix}\.(\d+)\.{pattern}"] = rf"{prefix}.\1.{replacement}"
# nunchaku quantization parameter name conversions (apply to all blocks)
suffix_rules = {
r"smooth_orig$": r"smooth_factor_orig",
r"smooth$": r"smooth_factor",
r"lora_down$": r"proj_down",
r"lora_up$": r"proj_up",
}
for old, new in suffix_rules.items():
mapping[rf"^(.*)\.{old}"] = rf"\1.{new}"
return mapping
# nunchaku checkpoint uses different weight names; map to sglang flux layout
param_names_mapping: dict = field(default_factory=get_param_names_mapping)| try: | ||
| from nunchaku.models.attention import NunchakuFeedForward # type: ignore[import] | ||
| except Exception: | ||
| NunchakuFeedForward = None |
Collaborator
There was a problem hiding this comment.
NunchakuFeedForward = None if nunchaku is not avaiable, but i saw we have self.feed_forward = NunchakuFeedForward(ff, **nunchaku_kwargs) below, so maybe we can change code like this?
_nunchaku_enabled = is_nunchaku_available()
if _nunchaku_enabled:
from nunchaku.models.attention import NunchakuFeedForward # type: ignore[import]f2cb425 to
c12e364
Compare
Collaborator
Author
|
/tag-and-rerun-ci |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Modifications
Performance and Accuracy Tests
Flux.1-dev
original:

nunchaku:

Z-Image-Turbo
original:

nunchaku:
Benchmarking and Profiling
Checklist
Review Process
/tag-run-ci-label,/rerun-failed-ci,/tag-and-rerun-ci