verl icon indicating copy to clipboard operation
verl copied to clipboard

How can I freeze the VIT of VLM in the current version of the project?

Open ccl4wsh opened this issue 1 year ago • 4 comments

ccl4wsh avatar Jul 14 '25 14:07 ccl4wsh

+1

huaiyizhao avatar Aug 13 '25 07:08 huaiyizhao

Does the Megatron backend training framework, support freezing the VL model of Qwen-VL 2.5 or 3?

Ryan-osss avatar Nov 29 '25 13:11 Ryan-osss

Proposal: Standardized Partial Parameter Freeze for FSDP & Megatron

Motivation

Freezing specific model components during RL training — especially the vision encoder in VLMs — is a common requirement that verl currently has no standardized mechanism for. Users resort to custom training scripts or maintain their own forks.

Related: #2526 (July 2025, no resolution), #6104 (Megatron-specific query), PR #3178 (merged but incomplete).


Current landscape: three incomplete solutions

A. PR #3178 — legacy FSDP path only

Adds freeze_vision_tower to the old ActorRolloutRefWorker code path. On the current main branch (which uses verl/workers/engine/fsdp/), this config has no effect.

Limitations:

  • Wrong code path — changes verl/workers/fsdp_workers.py (legacy), not the current FSDP engine at verl/workers/engine/fsdp/transformer_impl.py.
  • VL-only & hardcoded — only finds model.visual; doesn't work for non-VL models or different module structures.
  • Silent config override — sets use_orig_params = True without the user's knowledge, with surprising memory/performance side effects.
  • No optimizer guard — frozen parameters are still passed to the optimizer, wasting memory and risking weight-decay side effects.

B. #6104 / community fork — Megatron-Bridge only

Adds a post_model_creation_callback in make_megatron_module() that calls model.freeze(…) via Megatron-Bridge's hook system. Only works for models that implement a freeze() method and only on the Megatron engine.

Limitations:

  • Megatron-Bridge only — uses provider.register_pre_wrap_hook(). No equivalent for FSDP users.
  • Model-specific freeze() — calls model.freeze() which only exists on a few VL models (e.g., qwen2_5_vl). Crashes at runtime for others.
  • Hardcoded three flags — freeze_vision_model, freeze_vision_projection, freeze_language_model — can't express arbitrary module patterns.
  • No optimizer isolation — relies on Megatron's distributed optimizer to handle frozen parameters internally.

C. User custom scripts — current workaround

Most users call model-specific freeze() or set requires_grad=False in their own launch scripts, outside verl's config system. Fragile across model versions and hard to reproduce.


Design

Config layer

A single new field in the model config (hf_model.yaml):

freeze_module_pattern: null   # null = no freeze (default)

When set to a regex string, any parameter whose fully-qualified name matches the pattern has requires_grad set to False before the model is wrapped by FSDP or DDP.

Examples of patterns:

  • model\\.visual\\. — freeze the entire vision tower
  • model\\.visual\\.merger — freeze only the vision-to-LLM projection
  • model\\.visual|model\\.embed — freeze multiple subtrees

Why regex (not a list of exact paths or boolean flags)

  • Generality — one pattern can cover multiple modules across different model architectures without enumerating every sub-module path.
  • Model-agnostic — each model's internal structure differs (e.g., Qwen3.5-VL uses model.visual, while another model may use vision_encoder or vit). Regex lets users match whatever path their model uses, without verl needing to know model internals.
  • Flexibility — users can express "freeze everything matching X except Y" with standard regex negation.
  • Simplicity — a single string field, no nested config structures.

Matching semantics: re.search

Using re.search (partial match), not re.fullmatch. A pattern like model\\.visual\\. matches all parameters whose name contains model.visual. as a substring — covering all its sub-modules. Users who need exact-parameter matching can add ^ / $ anchors within the regex itself; no separate config option needed.

Dedicated unfreeze pattern is unnecessary

Regex already supports negation (e.g., ^(?!model\\.visual\\.).*) and subtractive patterns. A separate unfreeze_module_pattern field would be functionally redundant.

Engine-agnostic insertion point

The freeze runs after model loading (and after LoRA adapter application, if enabled) but before the parallelism wrapper (FSDP or DDP). This ensures requires_grad is set on raw parameters before PyTorch's distributed training layers take over.

Both engine paths (FSDP and Megatron) apply freeze at the same logical stage: between model construction and optimizer creation.

Safety: FSDP use_orig_params

PyTorch FSDP with use_orig_params=False (verl's default) flattens all parameters within each FSDP unit into a single FlatParameter. This forbids mixing frozen and non-frozen parameters in the same FSDP unit.

If freeze_module_pattern is set and use_orig_params=False, raise a hard error instructing the user to set use_orig_params=True in their config. We do not auto-enable it — silently changing distributed training configurations masks problems and may cause unexpected behavior.

Megatron: no equivalent constraint

Megatron's DDP wrapping and distributed optimizer natively respect requires_grad=False on individual parameters, so no additional safety checks are needed for the Megatron engine.

Optimizer isolation

Frozen parameters are excluded from the optimizer's parameter groups, so they consume no optimizer state and are not affected by weight decay. This applies to both FSDP (where the optimizer receives only requires_grad=True params) and Megatron (where the distributed optimizer respects requires_grad).

Scope: what this does NOT cover

  • Selective unfreeze during training — freeze is static, applied once at initialization.
  • The legacy freeze_vision_tower field — this existing field in actor.yaml (from PR #3178) is unused on the current engine and model-dependent (hardcoded to model.visual). It should not be mapped to freeze_module_pattern, as that defeats the model-agnostic design goal. Removing it in a separate cleanup is preferred.
  • LoRA path — VLMLoRA already has its own freeze_vision_model / freeze_vision_projection configs. This proposal targets full-parameter fine-tuning (non-LoRA path).

Comparison

Requirement PR #3178 Community fork This proposal
FSDP (current engine) ❌ legacy only ❌ ✅
Megatron (current engine) ❌ ✅ ✅
Arbitrary module patterns ❌ VL hardcoded ❌ 3 bool flags ✅ regex
Model-agnostic ❌ VL only ❌ needs model.freeze() ✅ requires_grad
use_orig_params safety ⚠️ silent override N/A ✅ hard error
Optimizer filters frozen params ❌ ❌ ✅
Config-driven ✅ ✅ ✅
Backward compatible ✅ ✅ ✅ (default null)

wzhgba avatar May 12 '26 09:05 wzhgba

中文:目前不建议把这个问题视为已经完全解决。当前 main 上可以看到两类相关配置:

  1. 对 VLMLoRA 路径,model.lora.freeze_vision_model、model.lora.freeze_vision_projection、model.lora.freeze_language_model 已在配置/文档中列出,可用于 LoRA 场景。
  2. actor_rollout_ref.actor.freeze_vision_tower 这个字段已经存在,但它不是一个通用的、跨当前 FSDP/Megatron 引擎的冻结方案。

如果你要的是非 LoRA full-parameter 训练,或者 Megatron backend 下冻结 Qwen-VL/Qwen2.5-VL/Qwen3-VL 的 vision module,目前更应该跟进 #6320。该 PR 提出的 freeze_module_pattern 会用参数名 regex 做通用冻结,但截至目前它仍未合入。因此在当前 release/main 上,Megatron/full-parameter 场景可能仍需要自定义代码或等待 #6320 类似方案合入。

English: I would not treat this as fully resolved yet. On current main there are two related configuration paths:

  1. For the VLMLoRA path, model.lora.freeze_vision_model, model.lora.freeze_vision_projection, and model.lora.freeze_language_model are listed in config/docs and can be used for LoRA cases.
  2. actor_rollout_ref.actor.freeze_vision_tower exists, but it is not the general engine-agnostic solution for the current FSDP/Megatron paths.

If the request is about non-LoRA full-parameter training, or freezing the vision module under the Megatron backend for Qwen-VL/Qwen2.5-VL/Qwen3-VL, the relevant follow-up is #6320. That PR proposes freeze_module_pattern as a regex-based general freezing mechanism, but it is still open and not merged. So on current release/main, the Megatron/full-parameter case may still require custom code or waiting for a #6320-style solution to land.

This response was generated by a scheduled Codex GPT5.5 issue processing task.

Kirrito-k423 avatar Jul 10 '26 01:07 Kirrito-k423