Skip to content

studio: offer the undistilled z-image base for lora training - #8291

Merged
danielhanchen merged 6 commits into
unslothai:mainfrom
mahiatlinux:feat/studio-z-image-base
Aug 10, 2026
Merged

danielhanchen merged 6 commits into
unslothai:mainfrom
mahiatlinux:feat/studio-z-image-base

Conversation

@mahiatlinux

Copy link
Copy Markdown
Contributor

Summary

Z-Image LoRA training offered only the distilled Tongyi-MAI/Z-Image-Turbo, while
examples/dreambooth/README_z_image.md and train_dreambooth_lora_z_image.py in diffusers train on
Tongyi-MAI/Z-Image. That repo was absent from _TRUSTED_NON_GGUF_REPOS, so _assert_trusted_base_model
refused it before anything was fetched and no custom-base entry could reach it.

Fixes #8270.

What changed

  • Add tongyi-mai/z-image to _TRUSTED_NON_GGUF_REPOS and Tongyi-MAI/Z-Image to the z-image
    train_base_repos.
  • Share the Qwen3-4B pre-cast text encoder between the base and Turbo through _TE_EQUIVALENT_BASES.
  • Price the base's download at its real bf16 size via a new per-base hub_download_factor.
  • Carry the base into the Train panel's offline family preset and correct that preset's label.

The allowlist gap also broke an inference pick

unsloth/Z-Image-GGUF carries base_model: Tongyi-MAI/Z-Image. _resolve_base_repo drops a card tag
that fails _is_trusted_diffusion_repo, so that pick fell back to the curated family default and loaded
the Turbo companions. Their scheduler is shift: 3.0 against the base's shift: 6.0, so the base GGUF
denoised on the wrong sigma schedule. The same allowlist entry fixes both paths. This is the defect the
existing klein-9B comment in that table already records for FLUX.2.

Correction to the issue's sizing claim

#8270 states that Z-Image ships fp32 and that _FAMILY_HUB_DOWNLOAD_FACTOR["z-image"] = 2.0 therefore
already covers the base. It is the other way round. Read from the Hub:

repo transformer shards dtype
Tongyi-MAI/Z-Image 12,309,817,472 bytes BF16
Tongyi-MAI/Z-Image-Turbo 24,619,634,944 bytes F32

Both hold 521 tensors and identical configs, so the 12.3 GB bf16-resident figure in _FAMILY_BF16_GB
stays correct for both. The family factor of 2.0 exists because the distilled Turbo publishes fp32, and
charging it to the base made the free-disk gate demand 24.6 GB for a 12.3 GB download.
_BASE_REPO_HUB_DOWNLOAD_FACTOR pins the base at 1.0, mirroring the shape of the existing
_BASE_REPO_BF16_GB override.

The rest of the issue's analysis holds. _ZIMAGE_TARGETS already matches the upstream script's
["to_k", "to_q", "to_v", "to_out.0"], z-image is not in AUTO_FLOW_SHIFT_FAMILIES so flow_shift
defaults to 1.0 and the scheduler's static shift is baked in at init, and _GENERATION_DEFAULTS already
resolves the base to 20 steps at guidance 4 behind the more specific z-image-turbo key.

Text encoder, VAE and tokenizer are byte identical

Compared through the Hub paths-info API:

file sha256
text_encoder/model-00001-of-00003.safetensors 328a91d3122359d5547f9d79521205bc0a46e1f79a792dfe650e99fc2d651223
text_encoder/model-00002-of-00003.safetensors 6cd087b316306a68c562436b5492edbcf6e16c6dba3a1308279caa5a58e21ca5
text_encoder/model-00003-of-00003.safetensors 7ca841ee75b9c61267c0c6148fd8d096d3d21b6d3e161256a9b878154f91fc52
vae/diffusion_pytorch_model.safetensors f5b59a26851551b67ae1fe58d32e76486e1e812def4696a4bea97f16604d40a3

Only the transformers differ. Without the _TE_EQUIVALENT_BASES entry the validator refuses the
Turbo-baked artifact for the base and every base run re-pulls the encoder dense, 8.04 GB instead of 4.41 GB.

What this PR deliberately leaves out

No deploy pairing. deploy_base_repo is family-wide and deployBaseFor rewrites any repo listed in
base_repos, so pairing z-image to Turbo would also rewrite unsloth/Z-Image-Turbo-unsloth-bnb-4bit and
send a QLoRA run's preview to a dense fp32 load. Per-variant deploy_base_repos is #8267's work and is not
on main. An adapter previews on whichever checkpoint it trained on, which is what the upstream recipe does.

No model-picker row for the base. unsloth/Z-Image does not exist on the Hub, and
test_every_third_party_bf16_pipeline_the_catalog_offers_is_mirrored requires an unsloth mirror for every
vendor bf16 pipeline the catalog offers. A mirror row for a repo that is not published would send
prefer_ungated_mirror to a 404. Deploy does not need the row: handleDeployAdapter calls
handleLoad(baseRepo, { kind: "pipeline" }) with the id directly, and the frontend MODEL_DEFAULTS table
already resolves Tongyi-MAI/Z-Image to 20 steps at guidance 4. The catalog entry should land together
with the mirror once one is published.

Base is not the default. The nf4 Turbo repo stays first in train_base_repos, so the picker default,
download size and VRAM floor are unchanged for existing users. #8267 earned its base-first flip with a
measured comparison; nothing equivalent exists for Z-Image yet.

Testing

Z-Image LoRA training offered only the distilled Tongyi-MAI/Z-Image-Turbo, while the
upstream DreamBooth recipe in diffusers trains on Tongyi-MAI/Z-Image. That repo was
absent from _TRUSTED_NON_GGUF_REPOS, so _assert_trusted_base_model refused it before
anything was fetched.

The same gap broke an inference pick. unsloth/Z-Image-GGUF carries
base_model: Tongyi-MAI/Z-Image, and _resolve_base_repo drops a card tag that fails
_is_trusted_diffusion_repo, so it fell back to the Turbo companions and denoised on
their shift 3.0 scheduler instead of the base's 6.0.

Add the base to the allowlist and to the z-image train_base_repos, keeping the nf4
Turbo repo first so the picker default is unchanged. Share the Qwen3-4B pre-cast text
encoder through _TE_EQUIVALENT_BASES: the shards are sha256 identical across both
checkpoints, so the base reuses the 4.41 GB artifact instead of pulling 8.04 GB dense.

Price the base's download correctly. The family factor of 2.0 exists because Turbo
publishes fp32 (24,619,634,944 bytes); the base ships bf16 (12,309,817,472 bytes) and
downloads what it occupies, so hub_download_factor pins it at 1.0 through a per-base
table. Without it the free-disk gate demanded twice the real size.
@mahiatlinux

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1d814a88b2

ℹ️ 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".

mahiatlinux and others added 2 commits August 10, 2026 15:10
Both hosted z-image checkpoints are baked from the distilled Turbo transformer, but
family_prequant_repo fell back to them for any base without its own variant row. The
base_model_id check refuses the artifact at load time, long after planning acted on it:
auto declined the dense path whenever the Turbo artifact was uncached, and an explicit
int8/fp8 request downloaded it, hit the refusal, and found no dense shards staged.

Add prequant_excluded_bases so a base whose weights genuinely differ resolves to None
and plans from its own dense transformer, and list Tongyi-MAI/Z-Image there.

Key both per-base tables in diffusion_auto_policy on the lowercased canonical id.
canonical_base preserves the caller's casing for a non-mirror, so a base typed as
tongyi-mai/z-image cleared the case-insensitive trust gate and then missed its download
override. _BASE_REPO_BF16_GB had the same defect: a lowercase FLUX.2-klein-base-9B was
sized as the 4B.
@mahiatlinux

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 6b625062bc

ℹ️ 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".

@danielhanchen

Copy link
Copy Markdown
Member

Checked the four factual claims against the Hub and they all hold:

  • Tongyi-MAI/Z-Image publishes 521 transformer tensors in F32 where Turbo publishes them in BF16, so the download factor override is right.
  • The Qwen3-4B text encoder is 3 shards / 8,044,982,000 bytes with an identical sha256 set on both repos, so adding the base to the _TE_EQUIVALENT_BASES group is safe and the pre-cast fp8 encoder genuinely is shared.
  • Both hosted prequant checkpoints are baked from the distilled Turbo transformer, so prequant_excluded_bases is the correct call for the undistilled base.
  • The base's recipe really is 20 steps / guidance 4 against Turbo's 8 / 1, and shift 6.0 vs 3.0.

One blocker though.

family_prequant_repo now raises for every video family

diffusion_families.py:

    base = canonical_base(base_repo).lower()
    if base:
        if base in fam.prequant_excluded_bases:
            return None

prequant_excluded_bases is a new DiffusionFamily field, but family_prequant_repo is also called with a VideoFamily, which does not have it. diffusion_prequant.resolve_prequant_source wraps the call in except Exception: repo_id = None ("a bad family object must not break the load"), so the AttributeError is swallowed and the video load silently drops to dense quantisation instead of the hosted checkpoint.

It only triggers when base_repo is passed, because an empty base short-circuits before the attribute is touched. That is exactly what the H3 modular load path does:

>>> family_prequant_repo(h3, "fp8", base_repo = "MiniMaxAI/MiniMax-H3")
AttributeError: 'VideoFamily' object has no attribute 'prequant_excluded_bases'
>>> resolve_prequant_source(h3, "fp8", base_repo = "MiniMaxAI/MiniMax-H3")
None

Two tests catch it:

FAILED tests/test_video_backend.py::test_download_plan_stages_the_prequant_denoiser_it_drops_the_dense_shards_for
FAILED tests/test_video_backend.py::test_h3_modular_load_pins_a_hosted_prequant_denoiser_out_of_the_offload_rotation

    status = load("fp8")
>   assert status["transformer_quant"] == "fp8"
E   AssertionError: assert None == 'fp8'

with video.loaded: MiniMaxAI/MiniMax-H3 (minimax-h3, modular diffusers, transformer_quant=off).

Both pass at this branch's merge base and fail on the head, and reverting diffusion_families.py alone is enough to make them pass again, so it is this hunk and nothing else in the PR.

The fix is the same defensive read the surrounding code already uses (getattr(fam, "name", None) in hub_download_factor, getattr(fam, "prequant_repos", ()) in video_families):

        if base in (getattr(fam, "prequant_excluded_bases", ()) or ()):
            return None

With that applied, both video tests pass and the PR's own new tests stay green: 146 passed across test_diffusion_auto_policy.py, test_diffusion_te_prequant.py, test_diffusion_more_families.py, test_diffusion_dit_trainer.py and the two video tests. The two failures left over (test_cast_fp8_is_idempotent_on_precast_encoder, test_create_causal_mask_patch_is_self_disabling_and_idempotent) also fail at the merge base, so they are not yours.

Worth considering separately: the swallowing except Exception in resolve_prequant_source is what turned a typo-level mistake into a silent quality and VRAM regression rather than a crash. Nothing to change here, but it is why this needed two failing tests to surface.

Happy to push the one-liner myself if you would rather not round-trip.

@danielhanchen

Copy link
Copy Markdown
Member

Correction to my first bullet above: I had the two dtypes the wrong way round. Your table is the correct one.

Read back off the safetensors headers just now:

repo transformer shards dtype bytes
Tongyi-MAI/Z-Image 2 BF16 12,309,873,872
Tongyi-MAI/Z-Image-Turbo 3 F32 24,619,690,888

So the base is the bf16 one and Turbo is the fp32 one, exactly as the PR body says, and it is the distilled Turbo that earns the family factor of 2.0. The conclusion is unchanged: pinning the base at 1.0 is right, and charging it the family factor would make the free-disk gate demand 24.6 GB for a 12.3 GB download. The other three claims stand as checked.

… hosted checkpoint

family_prequant_repo is shared with the video loader, which calls it with a VideoFamily. That
class has no prequant_excluded_bases, so the new plain attribute read raises AttributeError, and
resolve_prequant_source catches it in the bare except that exists to survive a bad family object
and returns None. The result is that every video family silently loses its hosted prequant
denoiser and falls back to dense whenever a base_repo is passed, which the H3 modular load always
does.

Two existing tests caught it, both passing at the merge base 9959212 and failing on the head:

  tests/test_video_backend.py::test_download_plan_stages_the_prequant_denoiser_it_drops_the_dense_shards_for
  tests/test_video_backend.py::test_h3_modular_load_pins_a_hosted_prequant_denoiser_out_of_the_offload_rotation

Same defensive read the surrounding code already uses for cross-family fields, plus a regression
test that pins family_prequant_repo and resolve_prequant_source on a VideoFamily directly.
@danielhanchen

Copy link
Copy Markdown
Member

No round-trip needed, I have pushed the one-liner to the branch as 086b031 since maintainer edits are on. Nothing else in the PR changed.

        # getattr, because the video loader calls this with a VideoFamily, which has no such
        # field. A plain attribute read raises AttributeError, resolve_prequant_source swallows it
        # in its bare except and hands back None, and every video family silently loses its hosted
        # prequant checkpoint to the dense path whenever a base_repo is passed.
        if base in (getattr(fam, "prequant_excluded_bases", ()) or ()):
            return None

Plus a regression test in test_diffusion_more_families.py that pins family_prequant_repo and resolve_prequant_source on a VideoFamily directly, rather than leaving the coverage to two H3 tests that would only catch it by side effect. It fails on 6b62506 and passes on 086b031.

Full studio/backend/tests/ at the merge base 9959212 and at 6b62506, same CUDA_VISIBLE_DEVICES mask on both, FAILED and ERROR line sets diffed rather than counts:

run passed FAILED+ERROR lines
merge base 9959212 20276 30
head 6b62506 20280 31

The set difference is exactly the two video tests:

> FAILED tests/test_video_backend.py::test_download_plan_stages_the_prequant_denoiser_it_drops_the_dense_shards_for
> FAILED tests/test_video_backend.py::test_h3_modular_load_pins_a_hosted_prequant_denoiser_out_of_the_offload_rotation

(one more line, test_stt_sidecar.py::test_pending_load_can_be_cancelled_without_waiting_for_model_lock, moves in the other direction and is a timing flake: it also appears and disappears between the two 8293 runs on the same host.)

After 086b031, 396 passed across test_video_backend.py, test_diffusion_more_families.py, test_diffusion_auto_policy.py, test_diffusion_te_prequant.py, test_diffusion_dit_trainer.py, test_diffusion_prequant.py and test_video_prequant.py, and hub/tests/ is 429 passed at both ends.

On the CI: the four Backend CI Python jobs are killed at the 15 minute timeout-minutes on the "Backend tests" step, each 15 minutes after its own start while "Repo tests (CPU)" in the same run succeeds. That is the backend suite having outgrown the step budget, it happens on every branch right now, and it is not yours. The Mac Studio Playwright failure is exercise_permission_mode_controls not finding the "Permission level for tool calls" pill in the chat composer, which nothing in this PR touches either. I will re-run both once the queue drains.

Thanks for the careful writeup, the sizing correction in particular was the right call and I had it backwards.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 086b031361

ℹ️ 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".

train_base_repos = (
"unsloth/Z-Image-Turbo-unsloth-bnb-4bit",
"Tongyi-MAI/Z-Image-Turbo",
"Tongyi-MAI/Z-Image",

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Make the generation hints match the undistilled base

When a user selects this newly offered base, trains an adapter, and clicks Deploy, handleDeployAdapter correctly initializes the Create page to 20 steps and guidance 4 via defaultsFor; however, the visible Steps and Guidance hints in studio/frontend/src/features/images/images-page.tsx still explicitly recommend Z-Image-Turbo's 9-step, guidance-0 recipe. Users following those instructions will switch the undistilled model to its distilled sibling's settings and get poor output, so the hints need to become model-aware or neutral before advertising this deployment path.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not this PR. Those two hints are static strings on ImagesPage, the single Create page every diffusion family renders through, and they have read that way since #6763:

studio/frontend/src/features/images/images-page.tsx:3615  hint="9 is the recommended setting for Z-Image-Turbo. More steps rarely help."
studio/frontend/src/features/images/images-page.tsx:3625  hint="Keep this at 0 for Z-Image-Turbo. Higher values make its output worse. Other models use guidance."

Byte identical at this branch's merge base 9959212 and at the head, and the PR does not touch that file at all. They already mis-advise FLUX.1, Qwen-Image, Lumina, HiDream and the Z-Image base itself, which has been loadable for inference through unsloth/Z-Image-GGUF the whole time, so this is not a path the PR opens.

The machine-readable side is right and stays right, which you note yourself: _GENERATION_DEFAULTS resolves Tongyi-MAI/Z-Image to 20 steps at guidance 4 behind the more specific z-image-turbo key, and Deploy seeds the Create page from defaultsFor. Making the hint text model-aware is worth doing, but it is a pre-existing copy fix for the whole page and belongs in its own change rather than bolted onto a training-base allowlist entry.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 086b031361

ℹ️ 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".

danielhanchen and others added 2 commits August 10, 2026 08:42
Resolves the diffusion_families.py DiffusionFamily field conflict by keeping both
new fields: prequant_excluded_bases from this branch and prequant_filenames from main.
@danielhanchen
danielhanchen merged commit 26154d5 into unslothai:main Aug 10, 2026
37 of 43 checks passed
@mahiatlinux
mahiatlinux deleted the feat/studio-z-image-base branch August 13, 2026 22:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Studio: Z-Image LoRA training only offers the distilled Turbo checkpoint, while the upstream recipe trains on Tongyi-MAI/Z-Image

2 participants