trl icon indicating copy to clipboard operation
trl copied to clipboard

[GOLD] VLM support for GOLDTrainer

Open Strongich opened this issue 5 months ago • 6 comments

Clear PR, based on https://github.com/huggingface/trl/pull/5461 with changes introduced in 2ac060eeec4b0f7afaf2395431cbff652aa18c00

Adds VLM support to GOLDTrainer:

  • Supports same-family VLM distillation with JSD loss when student and teacher share compatible tokenization/image-token semantics.
  • Supports cross-family VLM distillation with ULD loss by processing images separately through the student and teacher processors.
  • Preserves raw PIL images through the dataloader with an identity collator, then materializes VLM batches only for the current accumulation slice.
  • Adds VLM on-policy generation paths for both local generation and vLLM-backed generation where the vLLM backend supports the selected model.
  • Adds teacher-side VLM input construction for ULD, including a fix for a silent alignment bug: teacher completions are now rendered with the teacher chat template instead of approximating them from generated text plus a manually appended EOS.
  • Adds examples/scripts/gold_vlm.py with documented same-family JSD and cross-family ULD examples.
  • Adds tests covering VLM collation, label masking, raw image preservation, cross-architecture validation, teacher processor setup, vLLM behavior, lazy slice materialization, and ULD alignment regressions.

Motivation

The GOLD algorithm has no theoretical constraints against VLM-to-VLM distillation -- the barriers were purely engineering (incompatible image token formats, different tokenizers, raw image handling through the dataloader).

Key changes

  • GOLDTrainer detects VLM datasets and uses an identity collator to preserve raw PIL images through the dataloader
  • For cross-architecture pairs, a _teacher_processor is stored and used in compute_loss to build teacher-compatible vision tensors from raw images
  • Auto-resolves teacher_tokenizer_name_or_path
  • Added examples/scripts/gold_vlm.py with two documented usage examples (same-family JSD + vLLM, cross-family ULD)
  • Added tests for VLM collator (label masking, completion preservation), cross-architecture detection (rejects JSD, stores teacher processor for different archs, skips it for same arch), VLM + vLLM init (copied from the LLM example), rejects LLM teacher with vision dataset
  • VLM handling (identity collator, raw image storage, vLLM multimodal path) is borrowed (where it was possible) from SFTTrainer and GRPOTrainer

Note

  • I didn't add VLM usage examples to docs/source/gold_trainer.md -- will add if that's desirable, just let me know.

Before submitting

  • [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • [X] Did you read the contributor guideline, Pull Request section?
  • [ ] Was this discussed/approved via a GitHub issue? Please add a link to it if that's the case.
  • [X] Did you make sure to update the documentation with your changes?
  • [X] Did you write any new necessary tests?

AI writing disclosure

We welcome the use of AI tools to help with contributions. For transparency and to help us improve our review process, please indicate the level of AI involvement in this PR.

  • [ ] No AI usage: the PR was written entirely by a human.
  • [X] AI-assisted: some parts were suggested or improved by AI, but the PR was written and reviewed by a human.
  • [ ] AI-generated: the PR was mostly or fully generated by an AI tool.

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag members/contributors who may be interested in your PR.

@qgallouedec @kashif


[!NOTE] Medium Risk Touches core GOLD training, generation, and loss paths with new modality-specific buffering and teacher input construction; mistakes could misalign images, truncate visual tokens, or break on-policy/eval, but behavior is heavily tested including smoke runs.

Overview Adds end-to-end vision-language model (VLM) distillation to GOLDTrainer, including docs, a launch script, a dedicated collator, and broad test coverage.

For VLM datasets, training now uses an identity dataloader collator so raw PIL images survive until batches are built per gradient-accumulation slice via DataCollatorForVisionLanguageChatML. Same-family pairs can use JSD with shared processor kwargs; cross-architecture pairs require use_uld_loss=True, with a separate _teacher_processor and _build_teacher_vlm_inputs so the teacher sees images through its own template while ULD byte alignment stays on untemplated completion text. On-policy paths cover local generate and vLLM (without double-applying RepeatSampler duplication). Eval collates raw examples in prediction_step / _prepare_inputs.

Guardrails: reject text-only models on vision data, non-VLM teachers, truncation_mode='keep_end' for VLMs, cross-arch without ULD, and Liger + ULD together. Liger JSD forwards multimodal kwargs and uses a VLM-aware backbone. New trl/experimental/gold/gold_vlm.py and a gold_trainer.md VLM section document usage.

Reviewed by Cursor Bugbot for commit cf90b7113f661c200e782080939b1af159fb8d5a. Bugbot is set up for automated code reviews on this repo. Configure here.

Strongich avatar Jun 07 '26 14:06 Strongich

New training runs from examples/scripts/gold_vlm.py:

  1. Qwen 8B -> Qwen 2B, JSD loss, vLLM: image
  2. Qwen 8B -> Qwen 2B, ULD loss, vLLM: image
  3. Qwen 8B -> LFM 1.6B, ULD loss, no-vllm: image

Results are consistent with https://github.com/huggingface/trl/pull/5461#issuecomment-4525368153 and https://github.com/kashif/trl/pull/6#issuecomment-4591997765

Strongich avatar Jun 07 '26 14:06 Strongich

nice! the scripts for experimental belong in the experimental trainer's folder for now

kashif avatar Jun 07 '26 16:06 kashif

To avoid opening a separate discussion or issue, I think I can address this idea here (but if you'd prefer otherwise, I'll create one):

liger_kernel is not yet supported with VLMs. I plan to work on it in 2 stages:

  1. add VLM support to GKDTrainer
  2. add liger kernel VLM support to both GOLDTrainer and GKDTrainer (in a single PR)

Since GKD functionality is fundamentally a case of GOLD, when use_uld_loss=False and we use same-family models, adding duplicative VLM support to the GKDTrainer is excessive imho.

I think we could later combine everything under a single GOLDTrainer (basically dropping a separate GKDTrainer) to keep the whole distillation logic in one place and avoid having to change the JSD path in two separate places. This way, I wouldn't need to add separate VLM support to GKDTrainer and then update liger_kernel -- I'd only need to implement the second part.

10.06.2026 Update

Implementation of the liger kernel support for VLMs was easier than I anticipated, so I added it to the GOLDTrainer in e9d9836 and also added tests regarding it. To check whether it works, I ran the training from the example above (Qwen -> Qwen, JSD, vLLM, use_liger_kernel = True). The results were almost identical to those of the same training with use_liger_kernel=False.

image

Strongich avatar Jun 08 '26 09:06 Strongich

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

bot-ci-comment[bot] avatar Jun 21 '26 10:06 bot-ci-comment[bot]

Will be home in a few days & will test it

Strongich avatar Jun 21 '26 10:06 Strongich

@kashif

Results are consistent with before, but ahead of sharing them I ran into a bug, fully described in https://github.com/huggingface/trl/pull/6156 (the PR description is fully AI-generated -- I was too lazy to write it up myself 😄). Since it touches the general Chunked NLL loss in SFTTrainer, I've split it out into its own PR. My suggestion is to merge the PR above first, and then this one, since without it ULD distillation for different families models with no vLLM, and chunked NLL Loss fails.

  1. Qwen 8B -> Qwen 2B, JSD, vLLM: image
  2. Qwen 8B -> Qwen 2B, ULD, vLLM: image
  3. Qwen 8B -> LFM 1.6B, ULD, no vLLM: image

Strongich avatar Jun 23 '26 19:06 Strongich

@kashif since the results in #5969 (comment), the branch picked up a few more commits. None of them touch the training path exercised by those runs -- they're either cosmetic or guard against edge cases that don't occur in a normal run:

  • 69faf72c — doc typo fix (docs/source/gold_trainer.md)
  • 124adf33 — torch_dtype → dtype rename in docs/example, no behavior change
  • 92110ffc — adds a fail-fast ValueError when the generation batch size isn't divisible by gradient_accumulation_steps; only triggers on an invalid config, no-op otherwise
  • a4c6f4cb — rejects a custom data_collator for VLM training with a ValueError; only triggers if one is passed, no-op otherwise
  • 03770c07 — fixes DataCollatorForVisionLanguageChatML dropping the image field when it's None (e.g. a text-only example in a mixed dataset); a no-op when every example actually has an image, as in the runs above
  • ba9da964 — replaces silent truncation of over-budget VLM prompts with a ValueError; only triggers when a prompt exceeds max_length - max_completion_length, which none of the runs hit
  • 2a3e1c82 — version-gated workaround for a transformers 5.3.x processor bug (already fixed in 5.4.0); no-op outside that version range
  • ca413bab — test-only change

So the core used to produce those results is unchanged, and the results reported there still stand

Strongich avatar Jul 05 '26 18:07 Strongich