Skip to content

Unsloth Studio: train video-capable VLMs on a dataset's video instead of dropping it - #16965

Merged
danielhanchen merged 29 commits into
unslothai:mainfrom
LeoBorcherding:night-shift-tester/studio-video-train
Nov 8, 2026
Merged

danielhanchen merged 29 commits into
unslothai:mainfrom
LeoBorcherding:night-shift-tester/studio-video-train

Conversation

@LeoBorcherding

@LeoBorcherding LeoBorcherding commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Training a vision model in Unsloth Studio on a dataset with a video column trained on text only. Dataset detection never recognised video, so it answered is_image: false, the UI sent is_dataset_image: false, the run took the text path ("Using VLM path: False") and the clip was dropped without a warning. Headless training with UnslothVisionDataCollator on the same rows works, so the gap is only in Unsloth Studio's dataset detection and formatting.

What changed

  • format_detection.py: video detection. A column whose values are video files (.mp4, .mov, .mkv, .avi, .m4v, .mpeg, .mpg), or messages with {"type": "video", "video": ...} parts. A .webm path counts only in a column named video, since .webm is also an audio container. Video columns count as visual input (is_image true, plus is_video), so the existing VLM routing picks them up. detect_video_structure gives vlm_messages for video messages and a new video_text shape for flat video + answer (+ optional question) rows.
  • hub/utils/dataset_format.py (the check-format route's copy) calls the same helpers.
  • format_conversion.py: convert_video_to_vlm_format builds video messages from flat rows. Video paths go through the same per-account readable-media check as image paths. Video message rows drop the None keys Arrow adds, since the collator treats any image key as an image.
  • trainer.py: only passes the dataset revision through to formatting, so video rows from a pinned Hub revision resolve against the same revision.
  • MLX (worker.py, _adapt_for_mlx_vlm): it has no video training, so a video part now raises a clear error instead of being passed through.

Proof

Unit tests: studio/backend/tests/test_video_dataset_training.py. CPU, no network, no model. All 32 pass on this branch merged with main 1bf139b02b, run on a Mac (Apple M5, macOS 26). On the old base fe982adaf2 the first version of the file went 5 failed / 1 passed (the passing one is the guard that video_id text columns stay text).

GPU run. Before and after were run back to back on one Colab L4 (22.5 GB, Linux), with Unsloth Studio booted from each ref:

  • Before: upstream main 1bf139b02b.
  • After: this PR's head 93aad56787 merged with that main.
  • Settings: unsloth/Qwen2.5-VL-3B-Instruct, 4-bit, LoRA r16 on the language layers, 120 steps, batch 2. Driven through /api/datasets/check-format and then /api/train/start with is_dataset_image set to check-format's is_image, which is what the UI sends.
  • Toy data: 32 train / 8 held-out clips, each 3 s at 392x280, of a coloured ball moving over noise. The answer is a code word per colour (red=kiwi, green=anchor, blue=lamp, yellow=violin), so it can only be learned from the video.
Colab L4 before, main 1bf139b02b after, 93aad56787 + main
check-format unknown, is_image: false, mapping required video_text, is_image: true, no mapping
Unsloth Studio log "Using VLM path: False", loads a text model "Using VLM path: True", "Vision data collator configured"
tokens per step (2 rows) 94 918, of which 840 are video
tokens seen in 120 steps 11,222 110,102
collator batch input_ids (2, 47) input_ids (2, 459), pixel_values_videos (3360, 1176), video_grid_thw [3, 20, 28] per clip
final training loss 0.54 0.00002
peak VRAM, torch allocated 4.17 GB 4.37 GB (+206 MB)
peak VRAM, nvidia-smi (sampled each second) 4,556 MiB 4,758 MiB
median time per step 0.43 s 1.05 s
held-out answers 1/8 (mostly violin) 8/8

For reference, the model with no training answers 0/8. It sees the colour but names it ("RedDot", "BlueDot") instead of the code word.

before vs after

loss curve

Unsloth Studio UI: the same dataset uploaded on the Train page, then "View dataset".

before, main 1bf139b02b after, 93aad56787 + main
before dataset check after dataset check
Read as custom_heuristic. question and answer are mapped, video is mapped to None, and it says training can start. Read as video_text. "No manual column mapping needed."
before training run after training run
The 120-step run: 11,222 tokens, loss flat near 0.5 The 120-step run: 110,102 tokens, loss to about 0

VRAM: what a video clip costs

The extra memory is the video tokens. Qwen2.5-VL cuts each frame into 14x14 patches, pairs frames in time (temporal patch 2), and merges 2x2 patches into one token. Unsloth's video reader samples at 2 frames per second, so:

tokens per clip = (frames / 2) x (H / 14) x (W / 14) / 4 = seconds x (H / 28) x (W / 28)

Here H and W are the frame size after it is rounded to multiples of 28 and kept between 100,352 and 602,112 pixels. For a 3 s, 392x280 clip that's 3 x 10 x 14 = 420 tokens. It checks against the logged shapes:

  • Patches: pixel_values_videos has 2 clips x 3 x 20 x 28 = 3,360 rows of 3 x 2 x 14 x 14 = 1,176 values.
  • Tokens: each row is 420 video tokens plus 39 text tokens = 459, and a batch of 2 is 918, matching the 918 tokens per step measured.

To see how memory grows, three short runs (30 steps each, same L4, same commit) were added after the 120-step one:

clip video tokens per clip (formula / logged video_grid_thw) tokens per step peak VRAM time per step
text only (before) 0 94 4.17 GB 0.43 s
3 s, 392x280 420 / [3, 20, 28] 918 4.37 GB 1.05 s
6 s, 392x280 840 / [6, 20, 28] 1,758 4.58 GB 1.91 s
10 s, 392x280 1,400 / [10, 20, 28] 2,878 4.89 GB 3.21 s
3 s, 784x560 1,680 / [3, 40, 56] 3,438 4.99 GB 3.85 s

Peak VRAM is a straight line in tokens per step: 4.15 GB + 0.25 MB per token. Every run lands within 30 MB of that line. Fitting only the 392x280 runs and predicting the 784x560 one gives 5.03 GB against 4.99 GB measured, 43 MB off. Step time follows the same shape, about 1.0 ms per token.

Where the 0.25 MB per token goes is an estimate, not separately measured. The model keeps each decoder layer's input for the backward pass: 36 layers x 2,048 values x 2 bytes = 0.15 MB per token. One layer is recomputed at a time during backward: the MLP's 3 x 11,008 values plus about 9,000 for attention, x 2 bytes, is about 0.08 MB. Together that's about 0.23 MB, close to the measured 0.25. The frozen vision tower runs without keeping activations, so it adds only a few tens of MB and doesn't show above the noise.

As a rule of thumb for this model with 4-bit LoRA r16:

peak VRAM ~ 4.15 GB + 0.25 MB x batch x (text tokens + seconds x (H/28) x (W/28))

  • Per clip-second: about 35 MB at 392x280, 140 MB at 784x560, and at most about 190 MB at the largest frame size (768 tokens per second).
  • Model: the 4.15 GB base is the weights, LoRA and optimizer state of this 3B model. A larger model moves the base and raises the per-token cost with its hidden size and layer count.

clip sweep

VRAM vs tokens

Not covered

  • Byte-only datasets.Video columns: these are refused with a message to save the clips to paths. Training reads clips from files.
  • Video under conversations: video parts under conversations instead of messages are flagged as video, then rejected with a "could not auto-detect" error rather than dropped.
  • Long clips: clips longer than the context length allows (default 2048 tokens, about 14 s at 392x280 or about 2.5 s at the largest frame size) are not tested.
  • LFM3-Omni-2B: not run through Unsloth Studio's trainer here.
  • MLX: refuses video. Not run on a Mac, only the CPU unit test.
  • Other GPUs: NVIDIA L4 on Linux only. The VRAM figures are for Qwen2.5-VL-3B 4-bit on that card.

Before / after: Train page, View dataset

Same jsonl (4 local MP4 clips, video + question + answer) uploaded on the Train page with unsloth/Qwen2.5-VL-3B-Instruct selected. Before (merge base f863657ae): read as custom_heuristic, video mapped to None, so the run trains on text only. After (194abe8b0): read as video_text, ready for training with no mapping.

Dataset Preview before vs after

…ping it

check-format never recognised a video column, so a video dataset reported
is_image=false, the run took the text path and the clip was silently dropped.
Video columns and {type: video} message parts now count as visual input, flat
video + text rows become video messages, and the vision collator gets the clip.
The MLX path, which has no video training, refuses instead of dropping it.
… answer column, and a video with no answer column is not reported as formatted
@LeoBorcherding

Copy link
Copy Markdown
Collaborator Author

Codex review on the fork mirror (LeoBorcherding#668) found no major issues at 93aad56787, this PR's head: result.

…does not decode every image or audio cell

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-07T17:19:24.571715Z c8d4b56 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.

@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: d1c3ac5d4b

ℹ️ 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 on lines +881 to +884
if video_parts(sample.get("messages")):
return {
"format": "vlm_messages",
"needs_conversion": False,

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 Preserve image conversion for video message rows

If a messages row contains a video part and LLaVA image placeholders ({"type":"image","index":0} with a separate images column), this early return declares the row fully converted before the existing image detection runs. format_and_template_dataset consequently skips convert_llava_to_vlm_format, and own_message_videos never inlines those images, so the output still has image placeholders without payloads when it reaches the vision collator. Preserve image conversion and validation for mixed message rows, or reject this shape instead of reporting it ready.

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.

Rows that mix video parts with LLaVA image placeholders from a separate images column are a two-format shape outside this PR's scope; single-format video and image datasets both route correctly.

…ched Hub clips offline, and drop the column-subset fill that fingerprinted the dataset in the server

[skip ci]
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…audio dicts out of video detection

[skip ci]
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

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

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 4ff20347e1

ℹ️ 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 added a commit that referenced this pull request Nov 7, 2026
… lint passes on main (#17041)

## Summary

`Lint CI / Source lint` fails on main at its "No new risky loader call
sites" step, so every PR based on main fails it too. For example, a
staging run of #16965, which does not touch this file:

```
1 risky loader call site(s) not in the baseline:

  studio/backend/core/inference/video_cosmos3.py:159  [dynamic-import] import_module
```

#16742 (the Cosmos3 guardrail refusal message) added
`guardrail_hub_token`. It wraps the guardrail package's
`snapshot_download` so each load sends its own token. It imports that
module with
`importlib.import_module(f"{GUARDRAIL_PACKAGE}.{GUARDRAIL_PACKAGE}")`.
`scripts/lint_risky_loaders.py` flags any `import_module` whose name it
cannot read as fixed, and an f-string reads as dynamic even though
`GUARDRAIL_PACKAGE` is the constant `"cosmos_guardrail"`. The import
itself is safe and the PR's behaviour is correct. Only the form trips
the guard.

## Change

`studio/backend/core/inference/video_cosmos3.py`: the import names the
module outright,
`importlib.import_module("cosmos_guardrail.cosmos_guardrail")`. That is
the same module, and this is the lint's first suggested remedy ("load
from a written-out name"). It keeps the baseline free of an entry that
would have to be justified. No behaviour changes.

## Testing

| | `python scripts/lint_risky_loaders.py` |
|---|---|
| main | exit 1, the site above |
| this branch | exit 0, `117 risky loader call site(s), all recorded` |

`studio/backend/tests/test_video_cosmos3.py`, which covers
`guardrail_hub_token` through a `cosmos_guardrail.cosmos_guardrail`
module stub: 195 passed.
@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: c8d4b566d1

ℹ️ 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 9882ccb into unslothai:main Nov 8, 2026
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.

2 participants