Skip to content

[rl] Add varlen attention for trainer. - #2649

Merged
zhxchen17 merged 1 commit into
mainfrom
zhxchen17/trainer_varlen
Mar 27, 2026
Merged

zhxchen17 merged 1 commit into
mainfrom
zhxchen17/trainer_varlen

Conversation

@zhxchen17

Copy link
Copy Markdown
Contributor

Summary:

We previously added varlen attention kernel for generator(vllm) by registering
a CUSTOM backend and using it in Generator Config. In this PR, we added a new
model config with attention_backend set to "varlen".

In the test_attn_numerics.py we are now testing against Qwen variant "0.6B_varlen"
instead of "0.6B" now, which means the generator and trainer should both use varlen
kernel now.

Test Plan:

torchrun --nproc_per_node=2 torchtitan/experiments/rl/tests/test_attn_numerics.py

Reviewers:

Subscribers:

Tasks:

Tags:

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Mar 22, 2026
@zhxchen17
zhxchen17 requested a review from acisseJZhong March 22, 2026 01:32

@tianyu-l tianyu-l left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would propose making varlen the default and the only supported attention backend under RL folder.

  • generator is using varlen only. In order to get batch invariance, we can't rely on sdpa.
  • although today we have one training sample per batch, eventually I think we should do packing and varlen is the only option, before flex is supported for inference. Related comment #2568 (comment)

Comment thread torchtitan/models/common/attention.py Resolved
Comment thread torchtitan/models/qwen3/__init__.py Resolved
@zhxchen17
zhxchen17 force-pushed the zhxchen17/trainer_varlen branch 2 times, most recently from 73ed0aa to ee0b3fc Compare March 24, 2026 19:44
Comment thread torchtitan/models/common/attention.py Resolved
Comment thread torchtitan/models/common/attention.py Resolved
@zhxchen17
zhxchen17 force-pushed the zhxchen17/trainer_varlen branch 5 times, most recently from 071bb5c to 6478680 Compare March 25, 2026 15:18
Comment thread torchtitan/models/qwen3/__init__.py Resolved
Comment thread torchtitan/models/qwen3/__init__.py Resolved
@zhxchen17
zhxchen17 force-pushed the zhxchen17/trainer_varlen branch from 6478680 to 1246e6d Compare March 26, 2026 15:01
@zhxchen17
zhxchen17 requested review from acisseJZhong and wwwjn March 26, 2026 15:03

@acisseJZhong acisseJZhong left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODOs: (can we create tasks for them in case we forget)

  • unify mask creation logic for RL
  • Support input batching for bz>1 #2674
  • Support flex attention backend

"""Build VarlenMetadata for all sequences in a batch."""
cu_seqs = torch.cumsum(
torch.tensor(
[0] + [token_ids.shape[0] for token_ids, _, _ in input_sequences],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious why do we add 0 in between?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is coming from varlen kernel's requirement. We always go from 0 as the index start.

Comment thread torchtitan/models/qwen3/__init__.py Resolved
@zhxchen17

Copy link
Copy Markdown
Contributor Author

token_lps = compute_token_log_probs(
model,
[
build_full_sequence(prompt_toks, gen_toks, device),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove this helper function? seems it's only used here and it's very short. The name is a little bit confusing

@acisseJZhong acisseJZhong Mar 26, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree naming is confusing, this function is used in multiple places tho. And we should probably put in some data utils.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is

  • only used in compute_token_log_probs
  • short

so agreed we can just directly expand the function content there.

full_sequence, dtype=torch.long, device=device
).unsqueeze(0)
# TODO Support input batching and expand this to batched inputs.
assert len(input_sequences) == 1, "batch size > 1 not supported yet"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, we can remove the check and remove the "batch" idea for now, you can leave a sentence in compute_token_log_probs's docstring to say this function accept one sequence for now.

@zhxchen17
zhxchen17 force-pushed the zhxchen17/trainer_varlen branch from 1246e6d to 47a4b03 Compare March 26, 2026 17:42

@tianyu-l tianyu-l left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please address remaining comments before merge

"""

# TODO Also support flex attention backend later.
assert (

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK to land first, but this should be in the VLLMGQAAttention module in #2709
cc @wwwjn

token_lps = compute_token_log_probs(
model,
[
build_full_sequence(prompt_toks, gen_toks, device),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is

  • only used in compute_token_log_probs
  • short

so agreed we can just directly expand the function content there.

Comment thread torchtitan/models/qwen3/__init__.py Resolved
Summary:

We previously added varlen attention kernel for generator(vllm) by registering
a CUSTOM backend and using it in Generator Config. In this PR, we added a new
model config with attention_backend set to "varlen".

In the test_attn_numerics.py we are now testing against Qwen variant "0.6B_varlen"
instead of "0.6B" now, which means the generator and trainer should both use varlen
kernel now.

Test Plan:

torchrun --nproc_per_node=2 torchtitan/experiments/rl/tests/test_attn_numerics.py

Reviewers:

Subscribers:

Tasks:

Tags:
@zhxchen17
zhxchen17 force-pushed the zhxchen17/trainer_varlen branch from 47a4b03 to 21b5945 Compare March 27, 2026 14:55

@Lucaskabela Lucaskabela left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @zhxchen17 this LGTM :)

@zhxchen17
zhxchen17 merged commit 7c8fc3f into main Mar 27, 2026
18 of 32 checks passed
chelsea0x3b pushed a commit to chelsea0x3b/torchtitan that referenced this pull request Mar 30, 2026
Summary:

We previously added varlen attention kernel for generator(vllm) by
registering
a CUSTOM backend and using it in Generator Config. In this PR, we added
a new
model config with attention_backend set to "varlen".

In the test_attn_numerics.py we are now testing against Qwen variant
"0.6B_varlen"
instead of "0.6B" now, which means the generator and trainer should both
use varlen
kernel now.

Test Plan:

torchrun --nproc_per_node=2
torchtitan/experiments/rl/tests/test_attn_numerics.py

Reviewers:

Subscribers:

Tasks:

Tags:
acisseJZhong pushed a commit that referenced this pull request Mar 31, 2026
Summary:

We previously added varlen attention kernel for generator(vllm) by
registering
a CUSTOM backend and using it in Generator Config. In this PR, we added
a new
model config with attention_backend set to "varlen".

In the test_attn_numerics.py we are now testing against Qwen variant
"0.6B_varlen"
instead of "0.6B" now, which means the generator and trainer should both
use varlen
kernel now.

Test Plan:

torchrun --nproc_per_node=2
torchtitan/experiments/rl/tests/test_attn_numerics.py

Reviewers:

Subscribers:

Tasks:

Tags:
TXacs pushed a commit to McmillanTAC/torchtitan that referenced this pull request Apr 13, 2026
Summary:

We previously added varlen attention kernel for generator(vllm) by
registering
a CUSTOM backend and using it in Generator Config. In this PR, we added
a new
model config with attention_backend set to "varlen".

In the test_attn_numerics.py we are now testing against Qwen variant
"0.6B_varlen"
instead of "0.6B" now, which means the generator and trainer should both
use varlen
kernel now.

Test Plan:

torchrun --nproc_per_node=2
torchtitan/experiments/rl/tests/test_attn_numerics.py

Reviewers:

Subscribers:

Tasks:

Tags:
ACharacterInASimulation pushed a commit to ACharacterInASimulation/torchtitan that referenced this pull request Apr 21, 2026
Summary:

We previously added varlen attention kernel for generator(vllm) by
registering
a CUSTOM backend and using it in Generator Config. In this PR, we added
a new
model config with attention_backend set to "varlen".

In the test_attn_numerics.py we are now testing against Qwen variant
"0.6B_varlen"
instead of "0.6B" now, which means the generator and trainer should both
use varlen
kernel now.

Test Plan:

torchrun --nproc_per_node=2
torchtitan/experiments/rl/tests/test_attn_numerics.py

Reviewers:

Subscribers:

Tasks:

Tags:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/8gpu CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants