Skip to content

Studio: load ComfyUI Krea-2 and HunyuanImage-2.1 single-file DiTs - #12885

Merged
danielhanchen merged 6 commits into
mainfrom
studio/comfy-krea2-hunyuanimage
Oct 7, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
studio/comfy-krea2-hunyuanimage

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

ComfyUI's Krea 2 and HunyuanImage 2.1 diffusion_models files did not load in Studio at all:

  • Krea 2 int8_convrot / fp8_scaled files passed the ComfyUI pre-load check, then failed with Krea2Transformer2DModel has no single-file converter; the bf16 file failed earlier, because Krea2Transformer2DModel has no from_single_file.
  • Every HunyuanImage 2.1 file (bf16, fp8_e4m3fn, distilled fp8) failed with FromOriginalModelMixin is currently only compatible with ....

diffusers ships both transformers without a single-file mapping. This PR registers one for each in the same table that already covers Qwen-Image-2.1 (_UNREGISTERED_SINGLE_FILE_CLASSES, never overriding an upstream entry), so both the plain path and the ComfyUI int8 / fp8 loader pick them up.

What changed

core/inference/diffusion_single_file_converters.py (new)

  • krea2_checkpoint_to_diffusers: renames, plus blocks.N.mod.lin [6 * hidden] viewed as scale_shift_table [6, hidden] (a 1-D, never-quantized tensor).
  • hunyuanimage_checkpoint_to_diffusers: takes both original namings, the reference repo's (img_attn_qkv, mlp.fc1, mod.linear; Comfy-Org's bf16 file) and ComfyUI's (img_attn.qkv, mlp.0, mod.lin under model.model.; its fp8 and distilled files). Fused qkv and single-block linear1 are split by rows; the final adaLN_modulation holds (shift, scale) and diffusers reads (scale, shift), so its two row halves swap.
  • Every rule is a rename, a row split or a whole-row reorder. Nothing touches columns, so the ComfyUI quant loader can trace each int8 / fp8 row back to its source and keep codes and per-row scales exact. Unknown keys raise with the key named instead of being dropped.
  • load_original_layout_transformer: a strict loader for plain files of a class with no from_single_file (Krea 2). It casts like from_pretrained (_keep_in_fp32_modules stay fp32), so the bf16 file builds the same tensors as the base repo.

diffusion_families.py, diffusion.py

  • HunyuanImage distilled files (*distilled*) get guidance_embeds + use_meanflow through transformer_config_variants, the shift 4 schedule through comfy_flow_shift_variants, and the base repo's two CFG guiders are dropped for a guidance-distilled transformer (otherwise every step would run twice).

Verification

Converted headers of the real Comfy-Org files, strict against the diffusers model (no missing, no unexpected, all shapes equal):

file tensors in int8 / fp8 layers diffusers keys missing unexpected
krea2_turbo_bf16 430 0 430 0 0
krea2_turbo_fp8_scaled 686 256 fp8 430 0 0
krea2_turbo_int8_convrot 878 224 int8 430 0 0
hunyuanimage2.1_bf16 856 0 1264 0 0
hunyuanimage2.1_fp8_e4m3fn 856 plain fp8 1264 0 0
hunyuanimage2.1_distilled_fp8_e4m3fn 864 plain fp8 1272 0 0

The distilled file against the base config leaves 8 keys unused, which is why it needs the config variant.

Bit identity, dense files vs the diffusers repos: krea2_turbo_bf16 converts to 430/430 tensors equal to krea/Krea-2-Turbo (same dtype, torch.equal), hunyuanimage2.1_bf16 to 1264/1264 equal to hunyuanvideo-community/HunyuanImage-2.1-Diffusers.

Renders, B200, 1024x1024, 4 prompts with fixed seeds. Base = this PR's merge base. LPIPS is against Studio's own bf16 render of the diffusers repo (eager, no quant). Peak is torch allocated.

file base head LPIPS vs Studio bf16 identical runtime peak GiB
krea2_turbo_bf16 (Speed off, no quant) fails renders 0.0000 4/4 bf16 36.6
krea2_turbo_int8_convrot fails renders 0.0873 0/4 224/224 int8 kept (torchao, ConvRot) 23.0
krea2_turbo_fp8_scaled fails renders 0.1671 0/4 256/256 fp8 kept (torchao) 22.7
hunyuanimage2.1_bf16 (Speed off, no quant) fails renders 0.0000 4/4 bf16 50.3
hunyuanimage2.1_fp8_e4m3fn fails renders 0.1225 0/4 plain fp8, upcast to bf16 50.6
hunyuanimage2.1_distilled_fp8_e4m3fn (8 steps) fails renders 0.1196 vs the Distilled-Diffusers bf16 0/4 plain fp8, upcast to bf16 50.7

For context, from the same harness and reference:

cell LPIPS
Krea 2, Studio's own int8 0.2642
Krea 2, Studio's own fp8 0.1127
Krea 2, Comfy fp8 file dequantized to bf16 0.1426 (most of the fp8 gap is the file's per-tensor scales, not the runtime)
HunyuanImage, Studio's own fp8 0.1569
HunyuanImage, bf16 compiled vs eager 0.0217 (floor)

Compile (Speed default): 0 graph breaks on every cell. Krea 2: 0 recompiles. HunyuanImage: 1 recompile per load (a hidden_states stride guard), the same as Studio's own HunyuanImage loads on the diffusers repo, so not new here.

Tests

tests/test_diffusion_single_file_krea2_hunyuanimage.py:

  • tiny diffusers models written out in each original layout by an inverse map kept in the test, converted back bit-identical and loaded with strict=True (Krea 2, plus HunyuanImage reference and ComfyUI namings, base and distilled);
  • row-only: every 2-D weight tagged per row and decoded with the ComfyUI loader's own decoder, each source row landing exactly once;
  • a tiny Krea 2 int8_tensorwise file in the real ComfyUI layout through load_comfy_quant_transformer with the real converter, codes and scales exact;
  • the plain Krea 2 loader (fp32 norms, strict refusal), registration, family detection of the Comfy file names, the distilled config / shift / guider wiring;
  • the real Comfy-Org headers when UNSLOTH_TEST_REAL_HF_HUB_CACHE points at a hub cache holding them.

Related suites, base vs head, compared by failing test ID:

  • the family / single-file / ComfyUI quant / flow-shift / Krea 2 suites (11 files): 307 passed on both, and the same 3 failures on both (LTX-2.3 assembler tests in test_media_family_assembler_offline.py);
  • every backend test matching diffusion, comfy, krea, hunyuan, famil or single_file (about 10.9k tests): the same 5 failures on both (test_diffusion_h3_trainer.py x3, test_diffusion_native_quant.py x1, test_diffusion_offline_source.py x1). Head adds 24 passing tests.

Not covered

  • Krea 2 nvfp4 / mxfp8 files: still refused here, handled separately.
  • Plain (unscaled) fp8 HunyuanImage files are upcast to bf16, so they load at bf16 size; they are not kept as fp8 weights.
  • The distilled file runs the family's default 50 steps unless the user picks 8; only the config, shift and guiders follow the file.
  • GGUF files of either family and HunyuanImage's refiner were not tested.
  • One GPU type (B200), Linux only; timings were on a shared card and are indicative only.

Register original-layout converters for Krea2Transformer2DModel and
HunyuanImageTransformer2DModel, which diffusers ships without a
single-file mapping. Every rule is a rename, a row split or a whole-row
reorder, so the ComfyUI int8 / fp8 loader keeps codes and scales exact.

Krea 2 has no from_single_file at all, so a plain bf16 file loads
through a small strict loader that casts like from_pretrained.
HunyuanImage takes both the reference-repo and ComfyUI namings, and the
guidance-distilled MeanFlow files get their config, shift 4 schedule
and no CFG guiders.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-07T10:04:44.090846Z 74dd117 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 7, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 74dd117369

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant