Skip to content

MLX decision training: Clef rows and predict take images - #13127

Merged
danielhanchen merged 6 commits into
unslothai:mainfrom
Lyxot:mlx-decision-images-training
Oct 9, 2026
Merged

danielhanchen merged 6 commits into
unslothai:mainfrom
Lyxot:mlx-decision-images-training

Conversation

@Lyxot

@Lyxot Lyxot commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator

What this adds

Decision rows with images train MLX Clef models, and FastDecisionModel.predict takes images.

  • A row's images (PIL images, data URLs, a JSON string of them, or the {"bytes": ..., "path": ...} dicts of an undecoded image column) reach the MLX Clef trainer. Unsloth Studio's decision dataset reader already keeps the column.
  • FastDecisionModel.predict(..., images=) accepts the same shapes for a Clef.
  • A row whose images cannot be read, or whose model reads none, is skipped with its reason like any row that does not fit.
  • Laya reads text only and refuses images in predict. Rows without images, Laya training and the torch path are unchanged.

Depends on unslothai/unsloth-zoo#1645, which makes clef_training_item accept images. With an older unsloth-zoo, rows without images are built with the same call as before.

Validation

python -m pytest tests/test_decision_model_mlx.py -q
# 13 passed

End to end through FastDecisionModel and DecisionTrainer on bean leaf photos (AI-Lab-Makerere/beans, 1,034 train, 261 held out, 100 steps, batch 4, LoRA r16), held-out accuracy:

Model Question Before After After, with another row's photo
Clef Flash, 4-bit condition (3-way) 0.686 0.969 0.287
Clef Flash, 4-bit diseased 0.977 0.981 0.513
Plain Qwen3.5-4B as a Clef condition (3-way) 0.330 0.958 0.287
Plain Qwen3.5-4B as a Clef diseased 0.686 0.977 0.517

A plain Qwen3.5-2B trained the same way on generated pictures saves with save_pretrained_merged and reloads through the serving engine reading images with the same accuracy.

Risks

  • Laya training ignores an images column without a warning, as the torch path does.
  • Image failures are reported under the existing "does not fit" skip reason.

A decision row's `images` (PIL images, data URLs, or the bytes / path dicts of an undecoded image column) reach the MLX Clef trainer, and `FastDecisionModel.predict` takes `images` for a Clef. A row whose images cannot be read, or whose model reads none, is skipped with its reason like any row that does not fit. Laya reads text only and refuses images in `predict`. Rows without images are built as before.
@Lyxot
Lyxot requested a review from danielhanchen as a code owner October 9, 2026 07:55
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 9, 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-09T13:10:58.755530Z 05b401f 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.

@Lyxot Lyxot added the MLX label Oct 9, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

With an unsloth-zoo whose Clef items take no images, a row with images failed as a row that does not fit and was skipped, so a dataset trained without its image rows, and predict raised a bare TypeError. Both now raise an ImportError naming the upgrade. Rows without images are built with the same call as before.
@Lyxot

Lyxot commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator 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: 8992336dac

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

Comment thread unsloth/models/decision_mlx.py Resolved
Comment thread unsloth/models/decision_mlx.py Resolved
predict tested the raw value, so an empty serialized image column such as "[]" made a Laya refuse a text-only request. The value is normalized first, as it is for a Clef.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

A nullable image column holds None or an empty {bytes, path} struct where a row has no image. Those were forwarded as images, so a text-only row was treated as one with images and skipped or refused. They are dropped.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 88ebd7f9d3

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

@Lyxot

Lyxot commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator Author

Codex review converged at 88ebd7f9d3: #13127 (comment)

What changed during review:

  • With an unsloth-zoo whose Clef items take no images, a row with images or predict(..., images=...) now raises an ImportError asking for a newer unsloth-zoo. Before, every image row was skipped as one that does not fit, so a dataset trained without them.
  • An empty image cell such as "[]" is no image for a Laya prediction.
  • None entries and empty {bytes, path} structs in a row's images, as a nullable image column holds, are dropped, so such a row is built as a text-only row.

Not changed, with the reason in each thread:

  • "Preserve images in the permutation source": the images are not lost. unsloth-zoo's permuted_item re-encodes from the images kept on the item, and a run with permute_fields=True reaches 100% on held-out pictures and falls to chance with the wrong picture.
  • An image above Pillow's hard size limit (about 179 megapixels) stops build_dataset with Pillow's own error instead of being skipped as a row.
  • The unsloth-zoo version pin is not raised here, since the release carrying unsloth-zoo#1645 does not exist yet; the ImportError above covers the gap until it does.

CI at 88ebd7f9d3: 28 passed, 6 failed, none from this change. Every failing job also fails on main at cc912813:

  • Core, "Core zoo" (3 lanes): CUDA-only tests of unsloth-zoo main on a CPU runner.
  • Backend CI, "Repo tests (CPU, studio)" and "(Python 3.13, a-k)": the same 15 failing tests as on main, none of them decision tests.
  • Version Compat CI, "GRPO fake-run".

tests/test_decision_model_mlx.py needs MLX and is not run by these jobs; it passes locally on Apple Silicon (13 tests) against unsloth-zoo#1645.

@danielhanchen danielhanchen self-assigned this Oct 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 9, 2026
@unslothai unslothai deleted a comment from Lyxot Oct 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 9, 2026
@unslothai unslothai deleted a comment from Lyxot Oct 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 9, 2026
@unslothai unslothai deleted a comment from Lyxot Oct 9, 2026
@unslothai unslothai deleted a comment from Lyxot Oct 9, 2026
@unslothai unslothai deleted a comment from Lyxot Oct 9, 2026
@unslothai unslothai deleted a comment from Lyxot Oct 9, 2026
@unslothai unslothai deleted a comment from Lyxot Oct 9, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@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: 05b401f289

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

encoded = encode(state, kept)
except (TypeError, ValueError) as exc:
encoded = encode(state, kept, _row_images(row))
except (TypeError, ValueError, OSError) as exc:

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 Catch bombs decoded by the Clef encoder

When a training row supplies an accepted data:image/... string whose dimensions trip Pillow's decompression-bomb guard, _row_images leaves that string unopened and clef_training_item is where Pillow raises Image.DecompressionBombError. That exception is not a TypeError, ValueError, or OSError, so this handler lets one oversized URL abort the entire dataset instead of reporting and skipping its row as it does for the equivalent bytes/path dict; include this exception in the row-level handling for images decoded by the encoder.

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.

unsloth-zoo's _decode_images opens data URLs inside try/except Exception and raises DecisionRequestError (a ValueError), so a bomb sent as a data URL is caught here and skipped with its row; checked with a 14000x14000 PNG.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 05b401f289

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants