Skip to content

Add DP fan-in mechanism to generator and hardcode DP routing to the highest DP rank - #3765

Merged
pzhan9 merged 4 commits into
pytorch:mainfrom
pzhan9:generator-dp-fan-in
Jun 25, 2026
Merged

pzhan9 merged 4 commits into
pytorch:mainfrom
pzhan9:generator-dp-fan-in

Conversation

@pzhan9

@pzhan9 pzhan9 commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

In #3743, generator was changed so its rank 0 starts to route requests among its DP ranks. In that PR, the routing was hardcoded to DP0.

In this PR, we change the hardcoded routing to the highest DP rank. The main difference between the highest DP rank and DP0 is,

  • rank 0 is on DP0. So when requests are processed by DP0, the results are accessed by rank 0 locally.
  • But if requests are processed by other DPs, rank 0 no long has local access. Instead, the peer DPs need to send their results back to rank 0, aka. "fan-in", so rank 0 can further process them, and return the final responses back to controller.

In summary, this PR does 2 changes:

  1. add the fan-in mechanism;
  2. change the hardcoding to the highest DP rank, so we can verify the fan-in works.

@pzhan9
pzhan9 requested a review from tianyu-l June 24, 2026 13:54
@pzhan9
pzhan9 requested a review from felipemello1 June 24, 2026 13:54
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jun 24, 2026
@pzhan9
pzhan9 force-pushed the generator-dp-fan-in branch from ed383be to 7c5f387 Compare June 24, 2026 16:14
@acisseJZhong

Copy link
Copy Markdown
Contributor

rank 0 is on DP0. So when requests are processed by DP0, the results are accessed by rank 0 locally.

maybe I'm missing the context, wondering why this is necessarily bad(since we just choose another rank to process all the requests)?

ranks_per_dp = config.parallelism.tensor_parallel_degree
# Which DP replica this rank belongs to (== vLLM's data_parallel_rank).
self._dp_rank = self._rank // ranks_per_dp
# Whether this rank is its replica's TP-leader (the lowest rank in the replica).

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.

what if other parallelism is on (e.g. PP, CP, ...), should ranks_per_dp also include them?

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.

This calculation is based on what VLLMGenerator's current implementation allows, i.e DP, TP and EP. If in the future we add, e.g. PP, this calculation, will be wrong and needs to be updated.

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.

ranks_per_dp didn't consider EP right now? maybe worth adding a TODO

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.

EP is technically considered. IIUC, EP does not change the mapping between global rank -> DP/TP rank.

For example, DP=2, TP=2, no EP and DP=2, TP=2, EP=4 would have the same mapping:

Global rank DP rank TP rank
0 0 0
1 0 1
2 1 0
3 1 1

# Which DP replica this rank belongs to (== vLLM's data_parallel_rank).
self._dp_rank = self._rank // ranks_per_dp
# Whether this rank is its replica's TP-leader (the lowest rank in the replica).
self._is_dp_master = self._rank % ranks_per_dp == 0

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.

_is_dp_master here means the leader rank among ranks within the same dp_rank?
is this an appropriate name? Originally I thought dp_master is dp_rank=0.

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.

iiuc, dp_rank by convention means the DP replica ID. e.g., in DP=2, TP=2:

| Global rank | DP rank | dp master |
|-------------|---------|-----------|
| 0           | 0       | yes       |
| 1           | 0       | no        |
| 2           | 1       | yes       |
| 3           | 1       | no        |

@tianyu-l can you confirm? (or maybe I understood your previous comment wrong)

For DP master, I am okay with using a different name. Anyway, DP master is just the lowest rank of each DP replica. Technically we could use the highest rank, and it would also work. I pick the lowest rank is mainly because VllmGenerator already lets rank 0 coordinate the whole generator. It makes sense to make rank 0 to be the DP master of DP0 too.

@acisseJZhong acisseJZhong Jun 24, 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.

We are on the same page. I am just thinking dp_master name is a little bit confusing as it is really the master rank for other parallelism group(TP group for now).

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 will change to self._tp_rank==0 (i.e. what @tianyu-l suggested)

@pzhan9

pzhan9 commented Jun 24, 2026

Copy link
Copy Markdown
Contributor Author

rank 0 is on DP0. So when requests are processed by DP0, the results are accessed by rank 0 locally.

maybe I'm missing the context, wondering why this is necessarily bad(since we just choose another rank to process all the requests)?

@acisseJZhong no, this is not bad. I mentioned it in the description is mainly to explain why we need to add the fan-in in this PR, and why #3743 could work without it.

@acisseJZhong

Copy link
Copy Markdown
Contributor

I mentioned it in the description is mainly to explain why we need to add the fan-in in this PR

yeah my question is more about why this fan-in thing is necessary?

# Used by DP-master to temporarily store the metrics prefix for a request
# it processes. The DP-master builds completions for its own replica's
# requests, so it needs to know the caller's metrics prefix.
self._request_metrics_prefix: dict[str, str] = {}

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.

n00bq: what is a metrics prefix for a vllm request? and why this not needed before?

ranks_per_dp = config.parallelism.tensor_parallel_degree
# Which DP replica this rank belongs to (== vLLM's data_parallel_rank).
self._dp_rank = self._rank // ranks_per_dp
# Whether this rank is its replica's TP-leader (the lowest rank in the replica).

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.

ranks_per_dp didn't consider EP right now? maybe worth adding a TODO

# Which DP replica this rank belongs to (== vLLM's data_parallel_rank).
self._dp_rank = self._rank // ranks_per_dp
# Whether this rank is its replica's TP-leader (the lowest rank in the replica).
self._is_dp_master = self._rank % ranks_per_dp == 0

@acisseJZhong acisseJZhong Jun 24, 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.

We are on the same page. I am just thinking dp_master name is a little bit confusing as it is really the master rank for other parallelism group(TP group for now).

@@ -466,6 +467,20 @@ def __init__(
ranks_per_dp = config.parallelism.tensor_parallel_degree

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.

since we only support DP and TP, let's just be straightforward

Suggested change
ranks_per_dp = config.parallelism.tensor_parallel_degree
tp_degree = config.parallelism.tensor_parallel_degree

# Which DP replica this rank belongs to (== vLLM's data_parallel_rank).
self._dp_rank = self._rank // ranks_per_dp
# Whether this rank is its replica's TP-leader (the lowest rank in the replica).
self._is_dp_master = self._rank % ranks_per_dp == 0

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 agree this name is ambiguous. Let's not have this bool. Instead, just use self._tp_rank everywhere, e.g. if self._tp_rank==0

request_id: str
prompt_token_ids: list[int] # [prompt_tokens]
sampling: SamplingConfig
metrics_prefix: str

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'm not a fan of

  • having metrics_prefix in both GenerationRequest and GenerationFuture
  • having extra _request_metrics_prefix for bookkeeping metrics_prefix because the above two are not enough
  • filling partial completion.metrics when a completion is created and later fill more

But I don't have a clean way to resolve this right now. @felipemello1 any advice?

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.

comments in the other functions

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.

Claude comes up a better solution by using an intermediate class _RequestMetricsInputs, so metrics_prefix does not need to be passed to other DPs. See the latest commit.

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.

One overall suggestion:
Starting from here, these variables and related methods should be grouped into a class e.g. named RequestDispatcher, which is responsible for dispatcher requests, collect completions, set result to generation futures. It'd help hides implementation details related to DP / TP handling with a clean interface.

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.

Added RequestDispatcher as suggested, and group relevant logic inside it.

Comment on lines +779 to +781
dist.broadcast_object_list(
container, src=0, group=self._broadcast_group, device=torch.device("cpu")
)

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.

It seems only DP masters need to send/recv, but you are broadcasting this port everywhere via _broadcast_group. Can we restrict the scope of this broadcast to the DP group with TP rank 0? Say there are 16 gpus with DP4 TP4, this group would be [0, 4, 8, 12]

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.

Do you suggest to create a new gloo group? That feels unnecessary complicated, since tp_rank!=0 could simply discard the port after receiving them?

)
return completions

def _resolve_completions(self, completions: list[tuple[str, Completion]]) -> None:

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.

Suggested change
def _resolve_completions(self, completions: list[tuple[str, Completion]]) -> None:
def _rank0_resolve_futures(self, completions: list[tuple[str, Completion]]) -> None:

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.

Sorry, its late, but why can different ranks build_completions but not solve them? Is there a reason?

I see that we refactored what was one function into multiple new functions. If there is no reason, can we reduce the complexity, delete/merge functions?

@felipemello1 felipemello1 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 for the PR! I did a quick pass. I am just worried that the logic is becoming too convoluted. I know we are in a time crunch, and i would be ok with landing something like this for now and refactor later, but i would encourage the following:

  1. a small diagram so its quick to grasp a mental model of whats going on (or perhaps update teh docstring of the class?
  2. Consider putting the DP logic in another class, for separation of concerns. Idk if this makes sense.
  3. See if the extra functions focused on rank 0 are as simple as possible, or if we can merge/delete some and be more elegant about it

Comment on lines +797 to +801
completions = self._build_completions(request_outputs)
if self._rank == 0:
self._resolve_completions(completions)
elif completions:
self._result_port.send(completions)

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.

is rank_0 supposed to resolve completions from other ranks as well? if so, it doesn't seem to happen

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.

Yes, rank 0 has a background task which listens to the port, and resolve them. See the logic around await self._result_rx.recv().

elif completions:
self._result_port.send(completions)

def _build_completions(

@felipemello1 felipemello1 Jun 25, 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.

not immediately clear the difference (or meaning) between _build_completions and _resolve_completions for a new reader

Comment on lines -751 to -753
# in flight when this one finished (includes itself; counted before the pop)
inflight_requests_at_completion = float(len(self._generation_futures))
generation_future = self._generation_futures.pop(request_output.request_id)

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.

why dont we keep this, but instead of popping, we just retrieve the generation_future.metrics_prefix, like we had before, keeping one source of truth, instead of self._request_metrics_prefix + adding it to GenerationRequest? would that work?

cc @tianyu-l

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.

Only rank 0 has self._generation_futures. Other DPs do not have it. Other DPs can only get the metric_prefix from the request rank 0 forwards to them.

Comment on lines 860 to 867
metrics_prefix = generation_future.metrics_prefix
for metric_type in [m.Max, m.Mean]:
metrics.append(
completion.metrics.append(
m.Metric(
f"{metrics_prefix}/inflight_requests_at_completion",
metric_type(inflight_requests_at_completion),
)
)

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 think we can delete/move this metric to _build_completions?

)
return completions

def _resolve_completions(self, completions: list[tuple[str, Completion]]) -> None:

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.

Sorry, its late, but why can different ranks build_completions but not solve them? Is there a reason?

I see that we refactored what was one function into multiple new functions. If there is no reason, can we reduce the complexity, delete/merge functions?

request_id: str
prompt_token_ids: list[int] # [prompt_tokens]
sampling: SamplingConfig
metrics_prefix: str

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.

comments in the other functions

@pzhan9
pzhan9 force-pushed the generator-dp-fan-in branch 2 times, most recently from c8ba2f7 to 04c3931 Compare June 25, 2026 20:19
@pzhan9
pzhan9 force-pushed the generator-dp-fan-in branch from 04c3931 to 740ee2a Compare June 25, 2026 20:25
@pzhan9

pzhan9 commented Jun 25, 2026

Copy link
Copy Markdown
Contributor Author

@tianyu-l @felipemello1 I made a big update based on the comments. Please take another look.

@pzhan9

pzhan9 commented Jun 25, 2026

Copy link
Copy Markdown
Contributor Author

I mentioned it in the description is mainly to explain why we need to add the fan-in in this PR

yeah my question is more about why this fan-in thing is necessary?

@acisseJZhong this is because only rank 0 returns response to controller. Other ranks do not do that.

The reason rank 0 has that responsibility is because we want it to be the coordinator of all ranks, so we have 1 decision point to:

  • make sure requests are processed in the same order among all ranks;
  • drive all DPs to run step, even if that DP is idle (e.g. there is only 1 request, and it was sent to DP4. Then DP0/1/3 will be idle).

@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.

Thanks for the awesome work (as always)! I left some final comments.

# locally) and on tp_rank!=0 (no outputs). All None for DP=1 since there
# is no peer DP to send from.
self._result_port: Port | None = None
self._rank0_result_rx: PortReceiver | None = None

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.

what is "rx"? please rename to something full

completions = await self._rank0_result_rx.recv()
self._rank0_resolve_futures(completions)

def ail_generation_futures(self, exc: BaseException) -> None:

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.

Suggested change
def ail_generation_futures(self, exc: BaseException) -> None:
def fail_generation_futures(self, exc: BaseException) -> None:

Comment on lines 547 to 548
rank 0 _process_finished_requests -> prompt_1 done? gen_future_1.set_result(Completion)
rank 1 _process_finished_requests -> no-op (holds no futures)

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.

literal _process_finished_requests is gone, so need update

@pzhan9
pzhan9 merged commit fcde6db into pytorch:main Jun 25, 2026
9 of 12 checks passed
@pzhan9
pzhan9 deleted the generator-dp-fan-in branch June 25, 2026 23:44
felipemello1 pushed a commit to felipemello1/torchtitan that referenced this pull request Jun 26, 2026
…orch#3794 fuse-qkv fix)

Reconcile our async-loop generator (min/max policy version) onto main's
RequestDispatcher (pytorch#3765: routes to highest DP rank + result fan-in):
- rank 0 stamps min_policy_version on every future in the STEP decision (it owns
  all futures regardless of which DP rank serves the request).
- _build_completions sets min == max (finish version) as a placeholder; rank 0
  replaces min with the true admitted version in _rank0_resolve_futures.
- pytorch#3794 fused-qkv HF-load fix arrives via the merge; our workaround was removed.
pzhan9 added a commit that referenced this pull request Jun 26, 2026
Note: the first 2 comments are submitted in it own PR in
#3765. Review that first. For
this PR, only review the commits after that.

This PR adds a `DPRequestRouter`, and use it in generator DP routing. It
does a refactoring on `generator_router.py`, so the routing strategy
classes, such as `StickySessionRoutingStrategy` can be shared.

TODO: Currently `StickySessionRoutingStrategy` will not re-route a
sticky session even if the assigned DP or generator is overloaded. We
should add a fail-over policy to address that. But that is not in the
scope of this PR.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/rl 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.

4 participants