Skip to content

Studio: realign the image precision contracts with the fail-closed refusal - #8257

Merged
danielhanchen merged 1 commit into
unslothai:mainfrom
danielhanchen:fix/studio-precision-contract-vs-refusal
Aug 9, 2026
Merged

danielhanchen merged 1 commit into
unslothai:mainfrom
danielhanchen:fix/studio-precision-contract-vs-refusal

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Four tests are red on a clean checkout of main, so every branch cut from it inherits them:

FAILED studio/backend/tests/test_saved_image_metadata_precision_contract.py::test_a_declined_quant_request_is_not_reported_as_engaged
FAILED studio/backend/tests/test_saved_image_metadata_precision_contract.py::test_the_persisted_recipe_records_the_engaged_build_not_the_load_request
FAILED studio/backend/tests/test_saved_image_metadata_precision_contract.py::test_the_openai_route_persists_the_same_build
FAILED studio/backend/tests/test_diffusion_backend.py::test_load_refines_component_placement_after_text_encoder_quantization

Root cause

#8165 ("Report the precision actually in use, and refuse an explicit one we cannot honor", merged 01:15) changed two things: a declined EXPLICIT precision now refuses the load instead of falling back silently, and quantize_text_encoders returns a TEQuantOutcome the loader reads .mode off rather than a bare mode string.

Two squash merges that landed after it carried branches cut before it, so main got combinations neither PR ever ran:

Test side or product side?

Test side, and the two symptoms were checked separately.

AttributeError: 'NoneType' object has no attribute 'mode' at diffusion.py:3113 looks like a live crash users could hit, so it is the one worth being sure about. quantize_text_encoders is annotated -> TEQuantOutcome and every one of its seven return statements constructs one; there is no bare return and no path that falls off the end. Nothing in production wraps or rebinds it, and video.py:1562 reads .mode off the same call with no guard for the same reason. The only object that ever reaches line 3113 as None is the stub a test monkeypatches in. A if te_outcome is None guard would be unreachable code that also weakens the return contract the outcome type exists to state.

AttributeError: '_EngagedBackend' object has no attribute 'assert_precision_available' is the same shape. The route only makes that call under pending_name == ENGINE_DIFFUSERS, and engine_for(ENGINE_DIFFUSERS) returns get_diffusion_backend(), i.e. DiffusionBackend, which defines the method at diffusion.py:917. Reaching for getattr(backend, "assert_precision_available", None) there would be strictly worse than the status quo: it would turn a loud failure into a silently skipped fail-closed gate.

So no production code changes and no regression test, because there is no product behaviour change to regress. #8165's refusal is the newer intent and the tests simply encode the older one.

Changes

  • opt into UNSLOTH_DIFFUSION_ALLOW_PRECISION_FALLBACK in the declined-request contract. That test is about what the recipe records once a fallback is allowed, so it has to ask for the build it is describing, the same way test_diffusion_routes.py, test_diffusion_backend.py, test_video_routes.py and test_video_backend.py were already adapted for Report the precision actually in use, and refuse an explicit one we cannot honor #8165
  • assert the decline reason Report the precision actually in use, and refuse an explicit one we cannot honor #8165 records rather than the old wording naming what loaded instead. The substantive pair is untouched: the resolved value is still off from an explicit source, and generate() still must not echo the declined request
  • give _EngagedBackend the assert_precision_available gate the route now calls, matching the fake in test_diffusion_routes.py
  • return a TEQuantOutcome(None) from the stubbed text-encoder pass, which preserves the stub's meaning (the encoders stayed dense) in the shape the function now returns

Verification

Before, on a clean detached checkout of c7ffee7bc: 4 failed, 233 passed.

After: test_saved_image_metadata_precision_contract.py, test_diffusion_backend.py, test_diffusion_routes.py and test_diffusion_precision.py together are 347 passed, 0 failed. Both runs with python3 -B on a cleared __pycache__. Ruff linter and the pinned kwargs formatter are clean.

Relationship to #8254

#8254 proposed this same Studio fix first, and the two Studio files here are its approach, reviewed on their merits rather than copied. This PR exists only because #8254's head branch lives on the org repo, so I cannot push to it, and because #8254 additionally carries a change to tests/test_python39_compatibility.py that is now obsolete: that floor red was fixed by the merged #8252, and the change conflicts with main as it stands. #8254 is left open for its author to close.

…fusal

Four tests are red on a clean checkout of main. unslothai#8165 made a declined EXPLICIT
precision refuse the load instead of falling back silently, and made the
text-encoder pass return a TEQuantOutcome the loader reads .mode off. Two later
squash merges carried branches cut before it: unslothai#8162 added the saved-recipe
precision contracts, and unslothai#8141 added a placement test whose stub returns a bare
None. Neither combination was ever run.

Fixed on the test side. quantize_text_encoders is annotated -> TEQuantOutcome
and every one of its seven returns constructs one, so the None at
diffusion.py:3113 only exists because a test monkeypatches the function; and the
route only calls assert_precision_available on the diffusers engine, which is
DiffusionBackend, which defines it. Neither AttributeError is reachable in a
real run, so there is no product guard to add.

- opt into UNSLOTH_DIFFUSION_ALLOW_PRECISION_FALLBACK where the contract is
  about what the recipe records once a fallback is allowed, as the routes,
  backend and video suites already do
- assert the decline reason unslothai#8165 records instead of the old wording naming what
  loaded instead. The substantive pair is untouched: off, from explicit
- give _EngagedBackend the assert_precision_available gate the route now calls,
  and return a TEQuantOutcome from the stubbed text-encoder pass
@danielhanchen
danielhanchen merged commit d9c15f6 into unslothai:main Aug 9, 2026
32 of 37 checks passed
danielhanchen added a commit that referenced this pull request Aug 9, 2026
…fusal (#8257)

Four tests are red on a clean checkout of main. #8165 made a declined EXPLICIT
precision refuse the load instead of falling back silently, and made the
text-encoder pass return a TEQuantOutcome the loader reads .mode off. Two later
squash merges carried branches cut before it: #8162 added the saved-recipe
precision contracts, and #8141 added a placement test whose stub returns a bare
None. Neither combination was ever run.

Fixed on the test side. quantize_text_encoders is annotated -> TEQuantOutcome
and every one of its seven returns constructs one, so the None at
diffusion.py:3113 only exists because a test monkeypatches the function; and the
route only calls assert_precision_available on the diffusers engine, which is
DiffusionBackend, which defines it. Neither AttributeError is reachable in a
real run, so there is no product guard to add.

- opt into UNSLOTH_DIFFUSION_ALLOW_PRECISION_FALLBACK where the contract is
  about what the recipe records once a fallback is allowed, as the routes,
  backend and video suites already do
- assert the decline reason #8165 records instead of the old wording naming what
  loaded instead. The substantive pair is untouched: off, from explicit
- give _EngagedBackend the assert_precision_available gate the route now calls,
  and return a TEQuantOutcome from the stubbed text-encoder pass
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.

1 participant