Skip to content

Fix image inputs in Clef training and prediction - #13471

Merged
danielhanchen merged 5 commits into
unslothai:mainfrom
Imagineer99:fix/clef-image-inputs
Oct 13, 2026
Merged

danielhanchen merged 5 commits into
unslothai:mainfrom
Imagineer99:fix/clef-image-inputs

Conversation

@Imagineer99

Copy link
Copy Markdown
Member

Clef training rows currently lose their images field, and prediction bypasses the vision tower. Preserve ordered images through encoding, question permutation and batching, forward the resulting tensors through the multimodal backbone, and accept images= in FastDecisionModel.predict().

Reuses Clef's existing media encoder/collator. Image rows without a processor are rejected in the dataset report. Text-only requests retain the language-only path, and LoRA keeps the existing frozen vision tower policy.

Validation:

  • Focused repo tests: 9 passed, including image ordering, mixed 0/1/3-image collation, vision gradients, prediction/evaluation, and existing Clef regressions.
  • Negative control on unmodified main: three image tests fail; the text-only control passes.
  • L4 experiment with tiny random Qwen3.5 modules: 9/9 patched checks versus 0/9 original, a successful DecisionTrainer LoRA step, and matching predictions after processor/checkpoint reload.
  • Pretrained Clef-Flash 9B (8b2e5fd17c09fd49bc2880b805d5323ec7ff4ff3), A100 80GB, BF16 without quantization: original 8/32 versus patched 32/32 on a fixed balanced synthetic set covering colours, counts, digits and ordered frames. The vendored Cloudflare encoder/wrapper/head, sharing the same loaded backbone, also scores 32/32; maximum probability difference is 0.000512. Public prediction agrees on all 32 cases; reversing the frames gives the correct opposite direction in 8/8 cases. The tested source hash matches this commit.
  • Repository formatter, Ruff and git diff whitespace checks pass.

The pretrained evaluation is a small synthetic correctness check, not a general accuracy benchmark or an independent backbone-loader comparison. No Studio UI or serving API changes are included.

Fixes #13461

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 12, 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-13T08:12:23.167633Z 350058c 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.

@carlitogwapito

Copy link
Copy Markdown
Contributor

I checked _clef_truncated at 745968c with five CPU-only helper-contract cases: shorter input, exact fit, one-token overflow, an empty image list, and text-only defaults. All five passed. The exact-fit and overflow cases also check that the same ordered image list and processor reach the spare-token encoding call.

The existing test_a_clef_prompt_that_fits_exactly_is_not_truncated accepts the new processor argument, but its mock does not assert that media is forwarded. A small extension could retain that boundary coverage for images too:

@pytest.mark.parametrize("natural, expected", [(16, False), (17, True)])
def test_clef_image_truncation_keeps_media(monkeypatch, natural, expected):
    from unsloth.models import clef

    tokenizer, processor = object(), object()
    images = [object(), object(), object()]
    original = images.copy()

    def encode(tok, record, max_length, processor=None):
        assert tok is tokenizer
        assert record["images"] is images
        assert record["images"] == original
        assert processor is expected_processor
        assert max_length == 17
        return types.SimpleNamespace(input_ids=[0] * min(natural, max_length))

    expected_processor = processor
    monkeypatch.setattr(clef, "encode_record", encode)
    encoded = types.SimpleNamespace(input_ids=[0] * 16)
    assert decision._clef_truncated(
        tokenizer, "synthetic frames", {}, encoded, 16,
        images=images, processor=processor,
    ) is expected
    assert images == original

My executed probe AST-extracted the real helper and mocked only encode_record under CPython 3.14.4. It did not import Unsloth/PyTorch, load a model, or exercise a real processor/GPU. The snippet above is a suggested extension to the repository test, not a claim that I ran the full suite or independently reproduced the multimodal accuracy experiment. No defect was observed in the scoped helper checks.

Share MLX's row image reader so bare images, data URLs and undecoded
image-column structs load the same on both paths. Training items no
longer hold pixel tensors (24 MiB per 1024x1024 image); the collator
makes them again per batch, as the MLX path does.
@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. What shall we delve into next?

Reviewed commit: 350058c04e

ℹ️ 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
danielhanchen merged commit a00ef5f into unslothai:main Oct 13, 2026
SpatariuRares added a commit to SpatariuRares/unsloth that referenced this pull request Oct 18, 2026
…reads them

POST /v1/systemone validated images and then refused outright when the
configured model was a saved connection, and _connection_decide had no images
parameter to pass on. A multimodal System One server reached through a custom
connection could only be asked text questions, even though unslothai#13126 and unslothai#13471
gave the native Clef path images.

Images are forwarded now, but never on a guess: Studio cannot see what a
remote System One server accepts, so the owner declares it per connection
(input_modalities: ["text", "image"], stored for the configured connection
only, with a switch in Settings > API) and an undeclared connection keeps
refusing images with a message naming the setting. Declared or not,
_validate_images runs first, so a malformed, oversized, over-pixel or
non-PNG/JPEG image is refused before any provider is called, and remote URLs
are still never fetched. A provider that cannot read what it was sent fails
through _upstream_error as itself, with no silent text-only retry.

laya_runtime.input_modalities answers for a connection too, so
GET /api/settings/systemone, /v1/models and the try dialog agree with what
gets forwarded.

Try a decision gains an image example, "Read a screenshot", offered only when
the configured model reads images. Its PNG sits in its own module behind
import(), so the startup bundle never carries it.
SpatariuRares added a commit to SpatariuRares/unsloth that referenced this pull request Oct 18, 2026
…reads them

POST /v1/systemone validated images and then refused outright when the
configured model was a saved connection, and _connection_decide had no images
parameter to pass on. A multimodal System One server reached through a custom
connection could only be asked text questions, even though unslothai#13126 and unslothai#13471
gave the native Clef path images.

Images are forwarded now, but never on a guess: Studio cannot see what a
remote System One server accepts, so the owner declares it per connection
(input_modalities: ["text", "image"], stored for the configured connection
only, with a switch in Settings > API) and an undeclared connection keeps
refusing images with a message naming the setting. Declared or not,
_validate_images runs first, so a malformed, oversized, over-pixel or
non-PNG/JPEG image is refused before any provider is called, and remote URLs
are still never fetched. A provider that cannot read what it was sent fails
through _upstream_error as itself, with no silent text-only retry.

laya_runtime.input_modalities answers for a connection too, so
GET /api/settings/systemone, /v1/models and the try dialog agree with what
gets forwarded.

Try a decision gains an image example, "Read a screenshot", offered only when
the configured model reads images. Its PNG sits in its own module behind
import(), so the startup bundle never carries it.
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.

[Feature] Multi-image Jev/Clef decision model training and prediction on PyTorch/CUDA

4 participants