Repository navigation
[rl] add window FIFO scheduling - #3927
Conversation
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
tianyu-l
left a comment
There was a problem hiding this comment.
Let's do direction alignment before worrying about code. Having some examples / picture illustrations would be helpful.
[ghstack-poisoned]
[ghstack-poisoned]
|
added https://github.com/pytorch/torchtitan/blob/738371e1063d734cbfaaee5483f652462b24b944/torchtitan/experiments/rl/docs/windowed_fifo.md#windowed-fifo-offpolicy-bound for anyone who is interested with the derivation of |
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.15.0) (oldest at bottom): * #3927 * __->__ #3926 Rename only, no behavior change. ## RL terminology renames Clarify prompt-vs-microbatch / sample terminology across the RL experiment: * `num_groups_per_train_step` -> `num_prompts_per_train_step` * `group_size` -> `num_samples_per_prompt` * method `_take_groups_for_train_step` -> `_take_groups` * Propagate to all call sites, example configs, docstrings/diagrams, and tests. --------- Co-authored-by: JessicaZhong <[email protected]>
felipemello1
left a comment
There was a problem hiding this comment.
Great PR! Its funny how the changes in the buffer are so simple after such a long discussion.
IMO, we still have some work to do on documentation. It took us several days to align, and it will be hard for new users to digest whats going on. I think that we need to take it easy on the jargon, and make very clear WHY we are doing it.
Also, some comments on variables names.
Sorry for blocking it. Thanks for getting the PR to the finish line!
| if not 1 <= window_size <= max_active_rollout_groups: | ||
| raise ValueError( | ||
| "window_size must be between 1 and max_active_rollout_groups, got " | ||
| f"window_size={window_size}, " | ||
| f"max_active_rollout_groups={max_active_rollout_groups}" | ||
| ) |
There was a problem hiding this comment.
i havent read the rest of the code, but this is a bit confusing, since before it says that the fraction is what its set...
"The controller defaults
to strict FIFO (window_size=1) unless windowed_fifo_fraction is set."
I guess some other function calculates and passes it? Should we rename it to _window_size, so its clear that its not directly defined by the user?
There was a problem hiding this comment.
window_size is a derived property in AsyncLoopConfig class, and we pass into the buffer. The buffer stores this as _window_size.
There was a problem hiding this comment.
should we pass it as _window_size directly, so its evident that the user doesnt need to define it?
There was a problem hiding this comment.
Personally I feel it's fine? I rarely see people pass in private variables as a function argument? also users don't need to construct the work buffer themselves. Similarly, the max_active_rollout_groups is also derived.
from codex:
No. A leading underscore on a parameter implies a private or ignored argument, not a derived value.
The current separation is clear:
- User configures window_fraction.
- AsyncLoopConfig.window_size derives the size.
- RolloutGroupWorkBuffer receives window_size.
- The buffer stores it internally as _window_size.
| if ( | ||
| self.windowed_fifo_fraction is not None | ||
| and self.windowed_fifo_fraction * self.max_active_rollout_groups < 1 | ||
| ): | ||
| warnings.warn( | ||
| f"windowed_fifo_fraction={self.windowed_fifo_fraction} is too small for " | ||
| f"active_buffer_size={self.max_active_rollout_groups}; forcing " | ||
| "window_size=1 (strict FIFO)", | ||
| stacklevel=2, | ||
| ) |
There was a problem hiding this comment.
we can delete it. We can just enforce later that window_size = max(1, window_size). LEts save some lines of code.
There was a problem hiding this comment.
edit: you already do it on line 240. Lets delete it.
There was a problem hiding this comment.
I thought it would be useful for letting user know that the window_fifo_fraction they set are "useless" and they are still doing strict FIFO? wdyt
There was a problem hiding this comment.
hmmm, thats a good idea, but we would have to change this code. As it is today, windowed FIFO is only really useful if window > groups_pet_step.
what if we always logged the calculated window + extra off_policy?
There was a problem hiding this comment.
no actually if you look at the proof of max offpolicy bounds in the readme, we longer hold the assumption that window size > P. The formula should hold regardless.
For logging, I agree we need to add more informative loggings, after we do more experiments. I feel some of the logging names are confusing to me. We can do some cleanup as a follow up PR?
There was a problem hiding this comment.
for logging, what i mean here is a simple "logger.info("Window size ...., max_off_policy ...")
There was a problem hiding this comment.
added this one line logging.
[ghstack-poisoned]
| this target, up to `max_offpolicy_steps`. See | ||
| ``torchtitan/experiments/rl/docs/windowed_fifo.md`` for details.""" | ||
|
|
||
| window_fraction: float | None = 0.3 |
There was a problem hiding this comment.
window_fraction is ambiguous. Do you mean windowed_fifo_window_fraction or WindowedFifoConfig.window_fraction?
There was a problem hiding this comment.
originally we had
window_size
window_fifo_fraction
from @felipemello1 who thinks we should unify the naming of these two.
so options could be:
lookahead_window_size
lookahead_buffer_fractionor
window_fifo_size
window_fifo_buffer_fraction
I feel both options doesn't sounds right.
I am okay with what we had before. But to address Felipe's concern, I changed to window_size and window_fraction.
Let me know what you guys prefer.
There was a problem hiding this comment.
lets go with window_fifo_buffer_fraction. Its the most literal thing. It contains the algorithm name "windowed fifo", and says what its a "fraction" of.
There was a problem hiding this comment.
- it's "windowed fifo", not "window fifo"
- what is "buffer" referring to? We are not even calling the
WorkBuffer"buffer"
If you are OK, let's go with the simplest window_fraction? I have a feeling that we'll deprecate this field any way, hopefully soon.
There was a problem hiding this comment.
If you are OK, let's go with the simplest window_fraction? I have a feeling that we'll deprecate this field any way, hopefully soon.
IMO, as a new reader, its not intuitive what either "window" or "fraction" are referring to. But, if both of you think its better, then its fine.
| - `S`: target offpolicy steps (`target_offpolicy_steps`) | ||
| - `f`: fraction of the active buffer visible to the scheduler (`window_fraction`), which defaults to `0.3` following the MiniMax paper | ||
|
|
||
| Given `P` prompt groups per train step, the controller derives: | ||
|
|
||
| ```text | ||
| B = (S + 1) * P | ||
| W = max(1, floor(f * B)) | ||
| ``` |
There was a problem hiding this comment.
can we spell all variables? i dont think that we gain much by using B, S, W, f, B, P. It makes it harder for the reader.
There was a problem hiding this comment.
for example, "Let S = 1, P = 3, and f = 0.5, which gives B = 6 and W = 3. Groups 0, 1, 3, 4, and 5 finish quickly, but group 2 is slow:"
this could be read naturally, as plain english, but because of the letters, users have to go back and read what each letter means.
There was a problem hiding this comment.
the symbol is introduced for easier derivation of the math proof below. For the example illustration, I expanded using natural language.
|
|
||
| Given a window size `W`, we can calculate the worst-case extra offpoliciness it introduces. Consider a slow target group `g` that was admitted at the back of a full active buffer: | ||
|
|
||
| 1. The trainer consumes all `B - 1` older groups ahead of `g`. |
There was a problem hiding this comment.
| 1. The trainer consumes all `B - 1` older groups ahead of `g`. | |
| 1. The trainer consumes all `B - 1` older groups before `g`. |
|
|
||
| Given a window size `W`, we can calculate the worst-case extra offpoliciness it introduces. Consider a slow target group `g` that was admitted at the back of a full active buffer: | ||
|
|
||
| 1. The trainer consumes all `B - 1` older groups ahead of `g`. |
There was a problem hiding this comment.
why B-1? before you were using B for buffer size "B = (S + 1) * P", but now it seems to be batch size? we should also avoid the word batch, since we use "num_prompt_per_step"
There was a problem hiding this comment.
B always mean buffer size. updated in the notation section.
| Given a window size `W`, we can calculate the worst-case extra offpoliciness it introduces. Consider a slow target group `g` that was admitted at the back of a full active buffer: | ||
|
|
||
| 1. The trainer consumes all `B - 1` older groups ahead of `g`. | ||
| 2. Because `B - 1 = S * P + (P - 1)`, this completes `S` train steps and leaves the next batch one group short. |
There was a problem hiding this comment.
this formula is not obvious. Do we need it? I think that we could even delete 1,2,3 here and the doc would be fine.
There was a problem hiding this comment.
why B - 1 = S * P + (P - 1) is not obvious? B = (S + 1) * P
| 1. The trainer consumes all `B - 1` older groups ahead of `g`. | ||
| 2. Because `B - 1 = S * P + (P - 1)`, this completes `S` train steps and leaves the next batch one group short. | ||
| 3. Group `g`, which would complete that batch, stalls. | ||
| 4. The trainer consumes all `W - 1` younger groups that may bypass `g` before the anchored window blocks again. |
There was a problem hiding this comment.
| 4. The trainer consumes all `W - 1` younger groups that may bypass `g` before the anchored window blocks again. | |
| 4. The trainer consumes all `W - 1` younger groups ahead of `g`within the window. |
| if ( | ||
| self.windowed_fifo_fraction is not None | ||
| and self.windowed_fifo_fraction * self.max_active_rollout_groups < 1 | ||
| ): | ||
| warnings.warn( | ||
| f"windowed_fifo_fraction={self.windowed_fifo_fraction} is too small for " | ||
| f"active_buffer_size={self.max_active_rollout_groups}; forcing " | ||
| "window_size=1 (strict FIFO)", | ||
| stacklevel=2, | ||
| ) |
There was a problem hiding this comment.
for logging, what i mean here is a simple "logger.info("Window size ...., max_off_policy ...")
| Symbols: | ||
| ``P``: prompts per train step (``num_prompts_per_train_step``). | ||
| ``S``: target steady-state offpolicy steps (``target_offpolicy_steps``). | ||
| ``f``: fraction of the active buffer visible to windowed FIFO | ||
| (``window_fraction``). | ||
| ``B``: active buffer size in prompt groups, ``B = (S + 1) * P``. | ||
|
|
||
| Returns: | ||
| The FIFO look-ahead window size, ``max(1, floor(f * B))``. A value | ||
| of 1 is strict FIFO. |
There was a problem hiding this comment.
i still dont see why we need all the symbols. Window size is just buffer_size * window_fraction.
There was a problem hiding this comment.
it helps below? to understand max_offpolicy_steps
| For active buffer size ``B``, window size ``W``, and prompts per | ||
| train step ``P``, the bound is ``(B + W - 2) // P``. | ||
|
|
There was a problem hiding this comment.
| For active buffer size ``B``, window size ``W``, and prompts per | |
| train step ``P``, the bound is ``(B + W - 2) // P``. |
There was a problem hiding this comment.
why deleting? I think it provides a nice illustration/explanation.
| ```text | ||
| consumed_before = B - 1 | ||
| consumed_after = W - 1 | ||
| total_consumed = consumed_before + consumed_after = B + W - 2 | ||
| ``` | ||
|
|
||
| Each train step consumes `P` groups, so the maximum age of `g` at consumption is: | ||
|
|
||
| ```text | ||
| max_offpolicy_steps = (B + W - 2) // P | ||
| ``` | ||
|
|
||
| Substituting `B = (S + 1) * P` gives: | ||
|
|
||
| ```text | ||
| max_offpolicy_steps | ||
| = floor(((S + 1) * P + W - 2) / P) | ||
| = floor(S + (P + W - 2) / P) | ||
| = S + floor(((W - 1) + (P - 1)) / P) | ||
| = S + ceil((W - 1) / P) | ||
| ``` |
There was a problem hiding this comment.
Its funny that this works, but it is also wrong.
you are doing total_consumed = consumed_before + consumed_after = B + W - 2
Its not B. Its P. The worst case scenario is NOT when the last prompt of the buffer stalls.
The owrst case scenario is when the last prompt of a batch P stalls.
It should be:
P+W-2
Then the formula is:
max_offpolicy_steps = target_off_polocy + extra_off_policy
= S + (P + W - 2) // P
There was a problem hiding this comment.
(P+W-2)//P is extra off policy and (B + W - 2) // P is max off policy steps. I don't think it's wrong, and they don't conflict.
There was a problem hiding this comment.
S + (P + W - 2) // P = S + 1 + (W - 1) // P = S + 1 + floor((W - 1) / P)
but @acisseJZhong has S + ceil((W - 1) / P)
without reading the proof, one of them is wrong
There was a problem hiding this comment.
@tianyu-l your 2nd step is wrong. You are doing -1 instead of -2.
S + 1 + (W - 2) // P = 1 + floor((W - 2) / P) == ceil((W - 1) / P)
Example: W=4, P=3:
1 + floor(2/3) = 1
ceil(3/3) = 1
But still, the concept is a bit unnatural IMO as presented.
The worst case is when i have, for example P=8
then i consume the 7 before me
i stall on the 8th, and then consume the window ahead of me.
So the formula is:
extra_policy_steps = (consumed_before + consumed_ahead) // P
max_off_policy = target_off_policy + extra_policy_steps
but the way currently presented, it says:
the worst case is when i consume the entire buffer
stall on the last example
then consume the window ahead.
max_off_policy = B-1 + W-1
Now i see why she called it max_off_policy, instead of extra_policy_steps, but it feels unnatural to me to frame it this way. It only works because B is a multiple of P.
There was a problem hiding this comment.
wdym by it only works because B is a multiple of P? I think max_offpolicy_steps = (B + W - 2) // P = S + ceil((W - 1) / P) holds without any constraints. This is the whole point of what the last section of the readme is trying to prove?
There was a problem hiding this comment.
Its not B. Its P. The worst case scenario is NOT when the last prompt of the buffer stalls.
The owrst case scenario is when the last prompt of a batch P stalls.
I don't agree with this.

see this example, worst case is that all the eight groups generated by policy 0 are in the buffer and group 7 stalls.
There was a problem hiding this comment.
wdym by it only works because B is a multiple of P? I think max_offpolicy_steps = (B + W - 2) // P = S + ceil((W - 1) / P) holds without any constraints.
We define B = (S + 1)P, so B must be a multiple of P.
For arbitrary B = qP + r:
floor((B + W - 2) / P)
= q + floor((r + W - 2) / P)
The remainder r determines how many bypasses complete the partial batch.
To the limit: Imagine the buffer is infinite, and we just drop samples when they get too old.
The logic is not about the size of buffer.
The logic, as I see it, is about:
- We consume P-1, just shy of a new train step
- Then we consume the window ahead.
In other words: Pth_example_policy_version + How many extra train steps will happen ahead of the Pth example
I will leave it up to you and we can revisit when we follow up with the work where we drop old samples and expand the buffer to oversample.
There was a problem hiding this comment.
The logic, as I see it, is about:
We consume P-1, just shy of a new train step
Then we consume the window ahead.
I agree with this, but this(P+W-2) is how many extra off policy steps. B+W-2 is how many max off policy steps. They don't conflict.
I don't agree with the statement that the math works because of B is a multiple of P(although B is multiple of P in reality), if that's what you are trying to say?
Anyways, I believe we should be on the same page now?
There was a problem hiding this comment.
follow up with the work where we drop old samples
I didn't think too much but i feel like if we drop old samples, or if enable trainer microabtch overlap, then max offpolicy steps won't be nicely derived as a formula of B and W. It will vary depends on the actual runtime behavior. Therefore the code will become very different.
There was a problem hiding this comment.
your 2nd step is wrong. You are doing -1 instead of -2.
sorry my derivation was wrong.
| Each train step consumes `P` groups, so the maximum age of `g` at consumption is: | ||
|
|
||
| ```text | ||
| max_offpolicy_steps = (B + W - 2) // P |
There was a problem hiding this comment.
| max_offpolicy_steps = (B + W - 2) // P | |
| extra_offpolicy_steps = (B + W - 2) // P |
There was a problem hiding this comment.
we used max_offpolicy_steps for all the rest of places.
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| """Run-ahead buffer of RolloutGroupWork shared between the data-input, rollout, and batcher loops. | ||
| """Active work buffer shared between the data-input, rollout, and batcher loops. |
There was a problem hiding this comment.
| """Active work buffer shared between the data-input, rollout, and batcher loops. | |
| """Buffer of `RolloutGroupWork` shared between the data-input, rollout, and batcher loops. |
I want to avoid adding "active" -- its confusing. I added this nomenclature, but now that i read it, its not obvious what active means
|
|
||
| ## Strict FIFO | ||
|
|
||
| The simplest scheduling policy is strict FIFO: always consume the oldest prompt group before any younger group. With `P` prompt groups per train step and a target of `S` offpolicy steps, the active buffer holds `B = (S + 1) * P` groups. Strict FIFO keeps every group within the target bound because at most `B - 1` older groups can be consumed first. |
There was a problem hiding this comment.
| The simplest scheduling policy is strict FIFO: always consume the oldest prompt group before any younger group. With `P` prompt groups per train step and a target of `S` offpolicy steps, the active buffer holds `B = (S + 1) * P` groups. Strict FIFO keeps every group within the target bound because at most `B - 1` older groups can be consumed first. | |
| The simplest scheduling policy is strict FIFO: always consume the oldest prompt group before any younger group. With `P` prompt groups per train step and a target of `S` offpolicy steps, the buffer holds `B = (S + 1) * P` groups. Strict FIFO keeps every group within the target bound because at most `B - 1` older groups can be consumed first. |
| The user configures: | ||
|
|
||
| - `S`: target offpolicy steps (`target_offpolicy_steps`) | ||
| - `f`: fraction of the active buffer visible to the scheduler (`window_fraction`), which defaults to `0.3` following the MiniMax paper |
There was a problem hiding this comment.
| - `f`: fraction of the active buffer visible to the scheduler (`window_fraction`), which defaults to `0.3` following the MiniMax paper | |
| - `f`: fraction of the buffer visible to the scheduler (`window_fraction`), which defaults to `0.3` following the MiniMax paper |
| ## Reference configurations | ||
|
|
||
| Both configurations run 150 optimizer steps on one eight-GPU node. One TP=2 trainer uses two GPUs, and six independent TP=1 generators use the remaining GPUs. Each optimizer step consumes 8 prompt groups with 16 completions per group. `max_offpolicy_steps=4` bounds policy lag. | ||
| Both configurations run 150 optimizer steps on one eight-GPU node. One TP=2 trainer uses two GPUs, and six independent TP=1 generators use the remaining GPUs. Each optimizer step consumes 8 prompt groups with 16 completions per group. `target_offpolicy_steps=4` sizes the active buffer. |
There was a problem hiding this comment.
| Both configurations run 150 optimizer steps on one eight-GPU node. One TP=2 trainer uses two GPUs, and six independent TP=1 generators use the remaining GPUs. Each optimizer step consumes 8 prompt groups with 16 completions per group. `target_offpolicy_steps=4` sizes the active buffer. | |
| Both configurations run 150 optimizer steps on one eight-GPU node. One TP=2 trainer uses two GPUs, and six independent TP=1 generators use the remaining GPUs. Each optimizer step consumes 8 prompt groups with 16 completions per group. `target_offpolicy_steps=4` sizes the `RolloutGroupWorkBuffer`. |
Stack from ghstack (oldest at bottom):
Motivation for window FIFO
reference: minimax paper section 6.2.4 of https://arxiv.org/pdf/2605.26494
Strict FIFO gives the strongest freshness guarantee, but it also creates head-of-line blocking: one slow rollout group at the front of the buffer can prevent newer finalized groups from being trained, even when those groups are still within the allowed offpolicy budget.
Windowed FIFO relaxes strict ordering only within a bounded look-ahead window. This lets the trainer consume ready prompt groups sooner, improving training pipeline utilization, while still preserving an explicit hard freshness bound via
max_offpolicy_steps.Changes introduced by this PR:
target_offpolicy_stepsandmax_offpolicy_stepsthey can tolerate for the extra performance.window_size= min(buffer_size, num_prompts_per_train_step * (max_off_policy_steps - off_policy_steps) + 1).Todos:
How it works
Symbols used below:
P:num_prompts_per_train_stepS:target_offpolicy_steps, the target mean offpolicy steps used for active buffer capacityM:max_offpolicy_steps, the maximum consume-time offpolicy steps toleratedB: active buffer size in prompt groups,B = (S + 1) * PW: FIFO look-ahead window size, derivesW = P * (M - S) + 1We let user provide
offpolicy_steps(S)andwindow_fifo_fraction(f).From the window FIFO formula(see more in the attached graph), we know
Testing on DAPO math task
wait for train batch time on average from ~37s -> ~22s, first step wait for train batch time ~100s -> ~27s. Reward on par.

