Skip to content

Commit 2967543

Browse files
authored
[megatron] fix: VLMs using fused kernels (verl-project#3849)
### What does this PR do? > Add **concise** overview of what this PR aims to achieve or accomplish. Reference related GitHub issues and PRs that help with the review. Currently, we will have error regarding to unexpected keyword argument 'visual_pos_masks', this is because mbridge did some customization of the `GPTModel` forward as well for Qwen3VL to support deepstack: https://github.com/ISEEKYAN/mbridge/blob/ecbdfbdfdc8027004702149d6dc87fbad7417708/mbridge/models/qwen3_vl/gpt_model.py#L84 Since mcore v0.13.0 introduced `_postprocess` and `_preprocess`, and our patch focuses on `_postprocess`, I also cleaned up the function for better maintainability and to fix this extra deepstack argument issue. We can't simply patch `_postprocess` as we will need to pass `temperature` argument as well: ```logs output = self.forward_backward_batch( /verl_megatron/verl/workers/actor/megatron_actor.py", line 598, in forward_backward_batch losses_reduced = forward_backward_func( miniconda/envs/qwenvl/lib/python3.10/site-packages/megatron/core/pipeline_parallel/schedules.py", line 500, in forward_backward_no_pipelining output_tensor, num_tokens = forward_step( ....... verl_megatron/verl/models/mcore/model_forward_fused.py", line 136, in fused_forward_qwen2_5_vl output_orig: CausalLMOutputForPPO = model( ...... mbridge-main/mbridge/models/qwen3_vl/model.py", line 323, in forward output = self.language_model( envs/qwenvl/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1773, in _wrapped_call_impl return self._call_impl(*args, **kwargs) /envs/qwenvl/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1784, in _call_impl return forward_call(*args, **kwargs) TypeError: _fused_GPTModel_forward() got an unexpected keyword argument 'visual_pos_masks' ``` In addition, there will be shape mismatch error when calculating `mrope`, if we pass `position_ids` in `fused_forward_qwen2_5_vl`, I tried to debug but the shape passed here doesn't make sense, and since according to https://github.com/volcengine/verl/blob/981d781db932ff53a0c584fd501dcd73ce2a8077/verl/models/mcore/model_forward.py#L117 it says model will calculate position_ids, I just follow the code there to not pass the position ids, and it works both for Qwen2.5VL and Qwen3VL without throwing further errors. ### Checklist Before Starting - [X] Search for similar PRs. Paste at least one query link here: ... - [X] Format the PR title as `[{modules}] {type}: {description}` (This will be checked by the CI) - `{modules}` include `fsdp`, `megatron`, `sglang`, `vllm`, `rollout`, `trainer`, `ci`, `training_utils`, `recipe`, `hardware`, `deployment`, `ray`, `worker`, `single_controller`, `misc`, `perf`, `model`, `algo`, `env`, `tool`, `ckpt`, `doc`, `data` - If this PR involves multiple modules, separate them with `,` like `[megatron, fsdp, doc]` - `{type}` is in `feat`, `fix`, `refactor`, `chore`, `test` - If this PR breaks any API (CLI arguments, config, function signature, etc.), add `[BREAKING]` to the beginning of the title. - Example: `[BREAKING][fsdp, megatron] feat: dynamic batching` ### Test > For changes that can not be tested by CI (e.g., algorithm implementation, new model support), validate by experiment(s) and show results like training curve plots, evaluation results, etc. ### API and Usage Example > Demonstrate how the API changes if any, and provide usage example(s) if possible. ```python # Add code snippet or script demonstrating how to use this ``` ### Design & Code Changes > Demonstrate the high-level design if this PR is complex, and list the specific changes. ### Checklist Before Submitting > [!IMPORTANT] > Please check all the following items before requesting a review, otherwise the reviewer might deprioritize this PR for review. - [X] Read the [Contribute Guide](https://github.com/volcengine/verl/blob/main/CONTRIBUTING.md). - [X] Apply [pre-commit checks](https://github.com/volcengine/verl/blob/main/CONTRIBUTING.md#code-linting-and-formatting): `pre-commit install && pre-commit run --all-files --show-diff-on-failure --color=always` - [X] Add / Update [the documentation](https://github.com/volcengine/verl/tree/main/docs). - [X] Add unit or end-to-end test(s) to [the CI workflow](https://github.com/volcengine/verl/tree/main/.github/workflows) to cover all the code. If not feasible, explain why: ... - [X] Once your PR is ready for CI, send a message in [the `ci-request` channel](https://verl-project.slack.com/archives/C091TCESWB1) in [the `verl` Slack workspace](https://join.slack.com/t/verl-project/shared_invite/zt-3855yhg8g-CTkqXu~hKojPCmo7k_yXTQ). (If not accessible, please try [the Feishu group (飞书群)](https://applink.larkoffice.com/client/chat/chatter/add_by_link?link_token=772jd4f1-cd91-441e-a820-498c6614126a).) Signed-off-by: Hollow Man <hollowman@opensuse.org>
1 parent 81ee664 commit 2967543

File tree

12 files changed

+181
-375
lines changed

12 files changed

+181
-375
lines changed

.github/workflows/.deprecate/e2e_prime.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747
HF_ENDPOINT: "https://hf-mirror.com"
4848
HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable
4949
container:
50-
image: whatcanyousee/verl:ngc-cu124-vllm0.8.5-sglang0.4.6.post5-mcore0.12.0-te2.3
50+
image: "verl-ci-cn-beijing.cr.volces.com/verlai/verl:app-verl0.6-transformers4.56.1-sglang0.5.2-mcore0.13.0-te2.2"
5151
options: --gpus all --shm-size=10g
5252
steps:
5353
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

.github/workflows/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ permissions:
3131
contents: read
3232

3333
env:
34-
IMAGE: "your vemlp image" # e.g. "verl-ci-cn-beijing.cr.volces.com/verlai/verl:app-verl0.4-vllm0.8.5-mcore0.12.2"
34+
IMAGE: "your vemlp image" # e.g. "verl-ci-cn-beijing.cr.volces.com/verlai/verl:app-verl0.6-transformers4.56.1-sglang0.5.2-mcore0.13.0-te2.2"
3535
DYNAMIC_RUNNER_URL: "https://sd10g3clalm04ug7alq90.apigateway-cn-beijing.volceapi.com/runner" # public veFaas api
3636

3737
jobs:

docker/Dockerfile.extention.awsefa

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Base Image support aws EFA
22
# Build Image with frameworks based on this
3-
FROM verlai/verl:app-verl0.5-sglang0.4.6.post5-mcore0.12.2
3+
FROM verlai/verl:app-verl0.6-transformers4.56.1-sglang0.5.2-mcore0.13.0-te2.2
44

55
# For aws instances with EFA net interface (Sagemaker AI Pod)
66
# install EFA driver:

docs/start/multinode.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ Once the fleet is created, define a Ray cluster task, e.g. in ``ray-cluster.dsta
334334
- PYTHONUNBUFFERED=1
335335
- CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
336336
337-
image: whatcanyousee/verl:ngc-cu124-vllm0.8.5-sglang0.4.6-mcore0.12.0-te2.2
337+
image: verlai/verl:app-verl0.6-transformers4.56.1-sglang0.5.2-mcore0.13.0-te2.2
338338
commands:
339339
- git clone https://github.com/volcengine/verl
340340
- cd verl

verl/models/mcore/model_forward.py

Lines changed: 59 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -21,105 +21,42 @@
2121
postprocess_packed_seqs_no_padding,
2222
preprocess_packed_seqs,
2323
preprocess_packed_seqs_no_padding,
24-
recover_left_padding,
25-
remove_left_padding,
2624
)
2725

2826

29-
def gptmodel_forward(
30-
model,
31-
input_ids,
32-
attention_mask,
33-
position_ids,
34-
sequence_parallel,
35-
value_model=False,
36-
pack_seqs=True,
37-
logits_processor=None,
38-
logits_processor_args: dict = None,
39-
**kwargs,
40-
):
41-
"""Default forward pass for GPT models with optional sequence packing."""
42-
pre_process = unwrap_model(model).pre_process
43-
post_process = unwrap_model(model).post_process
44-
if pack_seqs:
45-
batch_size, seq_len = attention_mask.shape[:2]
46-
input_ids_rmpad, packed_seq_params = preprocess_packed_seqs(input_ids, attention_mask, pre_process=pre_process)
47-
input_ids_rmpad = input_ids_rmpad.contiguous()
48-
output_orig = model(
49-
input_ids=input_ids_rmpad,
50-
attention_mask=None,
51-
position_ids=position_ids,
52-
packed_seq_params=packed_seq_params,
53-
)
54-
if post_process and logits_processor is not None:
55-
args = {
56-
k: preprocess_packed_seqs(v, attention_mask, pre_process=True)[0]
57-
for k, v in logits_processor_args.items()
58-
}
59-
output_dict = logits_processor(output_orig, **args)
60-
output = {
61-
k: postprocess_packed_seqs(
62-
v, packed_seq_params, attention_mask, batch_size, seq_len, post_process=post_process
63-
)
64-
for k, v in output_dict.items()
65-
}
66-
else:
67-
output = postprocess_packed_seqs(
68-
output_orig, packed_seq_params, attention_mask, batch_size, seq_len, post_process=post_process
69-
)
70-
else:
71-
batch_size, sequence_length = attention_mask.shape
72-
new_input_ids, new_attention_mask, new_position_ids = remove_left_padding(
73-
input_ids, attention_mask, position_ids, sequence_parallel, pre_process=pre_process
74-
)
75-
output = model(input_ids=new_input_ids, attention_mask=new_attention_mask, position_ids=new_position_ids)
76-
if post_process:
77-
output = logits_processor(output, **logits_processor_args)
78-
output = recover_left_padding(
79-
output, new_attention_mask, attention_mask, sequence_length, post_process=post_process
80-
)
81-
if value_model and post_process:
82-
output = output[..., 0]
83-
return output
27+
def model_forward_gen(vision_model: bool = False):
28+
def model_forward(
29+
model,
30+
input_ids,
31+
attention_mask,
32+
position_ids,
33+
multi_modal_inputs: dict,
34+
logits_processor=None,
35+
logits_processor_args: dict = None,
36+
value_model=False,
37+
):
38+
"""Forward pass for models with sequence packing."""
39+
pre_process = (
40+
unwrap_model(model).pre_process if not vision_model else True
41+
) # vision model always needs pre_process
42+
post_process = unwrap_model(model).post_process
8443

44+
model_kwargs = {}
45+
if "pixel_values" in multi_modal_inputs:
46+
model_kwargs["pixel_values"] = multi_modal_inputs["pixel_values"].to(input_ids.device)
47+
if "image_grid_thw" in multi_modal_inputs:
48+
model_kwargs["image_grid_thw"] = multi_modal_inputs["image_grid_thw"].to(input_ids.device)
8549

86-
def gptmodel_forward_qwen2_5_vl(
87-
model,
88-
input_ids,
89-
attention_mask,
90-
position_ids,
91-
sequence_parallel,
92-
value_model=False,
93-
pack_seqs=True,
94-
multi_modal_inputs=None,
95-
logits_processor=None,
96-
logits_processor_args: dict = None,
97-
**kwargs,
98-
):
99-
from megatron.core import parallel_state as mpu
100-
101-
assert mpu.get_context_parallel_world_size() == 1, "qwen2_5_vl's context parallel is not accurate yet"
102-
pre_process = unwrap_model(model).pre_process
103-
post_process = unwrap_model(model).post_process
104-
pixel_values = (
105-
multi_modal_inputs["pixel_values"].to(input_ids.device) if "pixel_values" in multi_modal_inputs else None
106-
)
107-
image_grid_thw = (
108-
multi_modal_inputs["image_grid_thw"].to(input_ids.device) if "image_grid_thw" in multi_modal_inputs else None
109-
)
110-
if pack_seqs:
11150
batch_size, seq_len = attention_mask.shape[:2]
112-
input_ids_rmpad, packed_seq_params = preprocess_packed_seqs(input_ids, attention_mask, pre_process=True)
51+
input_ids_rmpad, packed_seq_params = preprocess_packed_seqs(input_ids, attention_mask, pre_process=pre_process)
11352
input_ids_rmpad = input_ids_rmpad.contiguous()
11453
output_orig = model(
11554
input_ids=input_ids_rmpad,
11655
attention_mask=None,
117-
position_ids=None, # model will calculate position_ids
56+
position_ids=position_ids if not vision_model else None, # vision models will calculate position_ids
11857
packed_seq_params=packed_seq_params,
119-
pixel_values=pixel_values,
120-
image_grid_thw=image_grid_thw,
58+
**model_kwargs,
12159
)
122-
12360
if post_process and logits_processor is not None:
12461
args = {
12562
k: preprocess_packed_seqs(v, attention_mask, pre_process=True)[0]
@@ -136,66 +73,55 @@ def gptmodel_forward_qwen2_5_vl(
13673
output = postprocess_packed_seqs(
13774
output_orig, packed_seq_params, attention_mask, batch_size, seq_len, post_process=post_process
13875
)
139-
else:
140-
batch_size, sequence_length = attention_mask.shape
141-
new_input_ids, new_attention_mask, new_position_ids = remove_left_padding(
142-
input_ids, attention_mask, position_ids, sequence_parallel, pre_process=pre_process
143-
)
144-
output = model(
145-
input_ids=new_input_ids,
146-
position_ids=new_position_ids,
147-
attention_mask=new_attention_mask,
148-
pixel_values=pixel_values,
149-
image_grid_thw=image_grid_thw,
150-
)
151-
output = recover_left_padding(
152-
output, new_attention_mask, attention_mask, sequence_length, post_process=post_process
153-
)
154-
if value_model and post_process:
155-
output = output[..., 0]
156-
return output
76+
if value_model and post_process:
77+
output = output[..., 0]
78+
return output
79+
80+
return model_forward
15781

15882

15983
def gptmodel_forward_no_padding(
16084
model,
16185
input_ids,
162-
value_model=False,
163-
pack_seqs=True,
86+
multi_modal_inputs: dict,
16487
logits_processor=None,
16588
logits_processor_args: dict = None,
166-
**kwargs,
89+
value_model=False,
16790
):
16891
"""Default forward pass for GPT models with optional sequence packing."""
16992
pre_process = unwrap_model(model).pre_process
17093
post_process = unwrap_model(model).post_process
171-
if pack_seqs:
172-
batch_size = input_ids.shape[0]
173-
input_ids_rmpad, packed_seq_params = preprocess_packed_seqs_no_padding(input_ids, pre_process=pre_process)
174-
input_ids_rmpad = input_ids_rmpad.contiguous()
175-
output_orig = model(
176-
input_ids=input_ids_rmpad,
177-
attention_mask=None,
178-
position_ids=None,
179-
packed_seq_params=packed_seq_params,
180-
)
18194

182-
if post_process and logits_processor is not None:
183-
args = {
184-
k: preprocess_packed_seqs_no_padding(v, pre_process=True)[0] for k, v in logits_processor_args.items()
185-
}
186-
output_dict = logits_processor(output_orig, **args)
187-
output = {
188-
k: postprocess_packed_seqs_no_padding(
189-
v, packed_seq_params, input_ids, batch_size, post_process=post_process
190-
)
191-
for k, v in output_dict.items()
192-
}
193-
else:
194-
output = postprocess_packed_seqs_no_padding(
195-
output_orig, packed_seq_params, input_ids, batch_size, post_process=post_process
95+
model_kwargs = {}
96+
if "pixel_values" in multi_modal_inputs:
97+
model_kwargs["pixel_values"] = multi_modal_inputs["pixel_values"].to(input_ids.device)
98+
if "image_grid_thw" in multi_modal_inputs:
99+
model_kwargs["image_grid_thw"] = multi_modal_inputs["image_grid_thw"].to(input_ids.device)
100+
101+
batch_size = input_ids.shape[0]
102+
input_ids_rmpad, packed_seq_params = preprocess_packed_seqs_no_padding(input_ids, pre_process=pre_process)
103+
input_ids_rmpad = input_ids_rmpad.contiguous()
104+
output_orig = model(
105+
input_ids=input_ids_rmpad,
106+
attention_mask=None,
107+
position_ids=None,
108+
packed_seq_params=packed_seq_params,
109+
**model_kwargs,
110+
)
111+
112+
if post_process and logits_processor is not None:
113+
args = {k: preprocess_packed_seqs_no_padding(v, pre_process=True)[0] for k, v in logits_processor_args.items()}
114+
output_dict = logits_processor(output_orig, **args)
115+
output = {
116+
k: postprocess_packed_seqs_no_padding(
117+
v, packed_seq_params, input_ids, batch_size, post_process=post_process
196118
)
119+
for k, v in output_dict.items()
120+
}
197121
else:
198-
raise NotImplementedError("gptmodel_forward_no_padding only supports packed sequences")
122+
output = postprocess_packed_seqs_no_padding(
123+
output_orig, packed_seq_params, input_ids, batch_size, post_process=post_process
124+
)
199125

200126
if value_model and post_process:
201127
# output = output[..., 0]

0 commit comments

Comments
 (0)