Repository navigation
Add DP fan-in mechanism to generator and hardcode DP routing to the highest DP rank - #3765
Conversation
ed383be to
7c5f387
Compare
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). |
There was a problem hiding this comment.
what if other parallelism is on (e.g. PP, CP, ...), should ranks_per_dp also include them?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
ranks_per_dp didn't consider EP right now? maybe worth adding a TODO
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
_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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
I will change to self._tp_rank==0 (i.e. what @tianyu-l suggested)
@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. |
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] = {} |
There was a problem hiding this comment.
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). |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 | |||
There was a problem hiding this comment.
since we only support DP and TP, let's just be straightforward
| 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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
I'm not a fan of
- having
metrics_prefixin bothGenerationRequestandGenerationFuture - having extra
_request_metrics_prefixfor bookkeepingmetrics_prefixbecause the above two are not enough - filling partial
completion.metricswhen a completion is created and later fill more
But I don't have a clean way to resolve this right now. @felipemello1 any advice?
There was a problem hiding this comment.
comments in the other functions
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Added RequestDispatcher as suggested, and group relevant logic inside it.
| dist.broadcast_object_list( | ||
| container, src=0, group=self._broadcast_group, device=torch.device("cpu") | ||
| ) |
There was a problem hiding this comment.
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]
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
| def _resolve_completions(self, completions: list[tuple[str, Completion]]) -> None: | |
| def _rank0_resolve_futures(self, completions: list[tuple[str, Completion]]) -> None: |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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:
- a small diagram so its quick to grasp a mental model of whats going on (or perhaps update teh docstring of the class?
- Consider putting the DP logic in another class, for separation of concerns. Idk if this makes sense.
- 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
| completions = self._build_completions(request_outputs) | ||
| if self._rank == 0: | ||
| self._resolve_completions(completions) | ||
| elif completions: | ||
| self._result_port.send(completions) |
There was a problem hiding this comment.
is rank_0 supposed to resolve completions from other ranks as well? if so, it doesn't seem to happen
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
not immediately clear the difference (or meaning) between _build_completions and _resolve_completions for a new reader
| # 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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
| 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), | ||
| ) | ||
| ) |
There was a problem hiding this comment.
i think we can delete/move this metric to _build_completions?
| ) | ||
| return completions | ||
|
|
||
| def _resolve_completions(self, completions: list[tuple[str, Completion]]) -> None: |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
comments in the other functions
c8ba2f7 to
04c3931
Compare
04c3931 to
740ee2a
Compare
|
@tianyu-l @felipemello1 I made a big update based on the comments. Please take another look. |
@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:
|
tianyu-l
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
| def ail_generation_futures(self, exc: BaseException) -> None: | |
| def fail_generation_futures(self, exc: BaseException) -> None: |
| rank 0 _process_finished_requests -> prompt_1 done? gen_future_1.set_result(Completion) | ||
| rank 1 _process_finished_requests -> no-op (holds no futures) |
There was a problem hiding this comment.
literal _process_finished_requests is gone, so need update
…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.
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.
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,
In summary, this PR does 2 changes: