Repository navigation
Unsloth Studio: train video-capable VLMs on a dataset's video instead of dropping it - #16965
Conversation
…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
…swers are skipped
…t the video dataset changes
|
Codex review on the fork mirror (LeoBorcherding#668) found no major issues at |
…does not decode every image or audio cell [skip ci]
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 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".
| if video_parts(sample.get("messages")): | ||
| return { | ||
| "format": "vlm_messages", | ||
| "needs_conversion": False, |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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]
…audio dicts out of video detection [skip ci]
…ry VLM question alias for video rows
|
Codex Review: Didn't find any major issues. Keep them coming! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
# Conflicts: # studio/backend/core/training/worker.py [skip ci]
… 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.
|
Codex Review: Didn't find any major issues. Another round soon, please! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
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 sentis_dataset_image: false, the run took the text path ("Using VLM path: False") and the clip was dropped without a warning. Headless training withUnslothVisionDataCollatoron 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), ormessageswith{"type": "video", "video": ...}parts. A.webmpath counts only in a column namedvideo, since.webmis also an audio container. Video columns count as visual input (is_imagetrue, plusis_video), so the existing VLM routing picks them up.detect_video_structuregivesvlm_messagesfor video messages and a newvideo_textshape 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_formatbuilds video messages from flat rows. Video paths go through the same per-account readable-media check as image paths. Video message rows drop theNonekeys Arrow adds, since the collator treats anyimagekey 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.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 main1bf139b02b, run on a Mac (Apple M5, macOS 26). On the old basefe982adaf2the first version of the file went 5 failed / 1 passed (the passing one is the guard thatvideo_idtext 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:
1bf139b02b.93aad56787merged with that main.unsloth/Qwen2.5-VL-3B-Instruct, 4-bit, LoRA r16 on the language layers, 120 steps, batch 2. Driven through/api/datasets/check-formatand then/api/train/startwithis_dataset_imageset to check-format'sis_image, which is what the UI sends.1bf139b02b93aad56787+ mainunknown,is_image: false, mapping requiredvideo_text,is_image: true, no mappinginput_ids (2, 47)input_ids (2, 459),pixel_values_videos (3360, 1176),video_grid_thw[3, 20, 28]per clipviolin)For reference, the model with no training answers 0/8. It sees the colour but names it ("RedDot", "BlueDot") instead of the code word.
Unsloth Studio UI: the same dataset uploaded on the Train page, then "View dataset".
1bf139b02b93aad56787+ maincustom_heuristic.questionandanswerare mapped,videois mapped to None, and it says training can start.video_text. "No manual column mapping needed."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:
pixel_values_videoshas 2 clips x 3 x 20 x 28 = 3,360 rows of 3 x 2 x 14 x 14 = 1,176 values.To see how memory grows, three short runs (30 steps each, same L4, same commit) were added after the 120-step one:
video_grid_thw)[3, 20, 28][6, 20, 28][10, 20, 28][3, 40, 56]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))Not covered
datasets.Videocolumns: these are refused with a message to save the clips to paths. Training reads clips from files.conversations: video parts underconversationsinstead ofmessagesare flagged as video, then rejected with a "could not auto-detect" error rather than dropped.Before / after: Train page, View dataset
Same jsonl (4 local MP4 clips,
video+question+answer) uploaded on the Train page withunsloth/Qwen2.5-VL-3B-Instructselected. Before (merge basef863657ae): read ascustom_heuristic,videomapped to None, so the run trains on text only. After (194abe8b0): read asvideo_text, ready for training with no mapping.