Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 37 additions & 32 deletions tensorrt_llm/_torch/pyexecutor/py_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,38 +846,8 @@ def _executor_loop_pp(self):

self._handle_canceled_requests()

# If logits were requested last PP rank has to send to first PP rank (who sends responses) the
# logits of the requests that have finished.
# NOTE: If the rank processing the logits ever becomes the same as
# the rank sending the responses, this code can be removed.
finished_reqs = [
r for r in previous_batch.sample_state.
scheduled_requests.all_requests()
if r.state == LlmRequestState.GENERATION_COMPLETE
and (r.py_return_context_logits
or r.py_return_generation_logits)
]
if self.dist.is_first_pp_rank and len(finished_reqs):
finished_reqs_py_results = [
r.py_result for r in finished_reqs
]
finished_reqs_py_results = self.dist.recv_object(
src=self.dist.prev_pp_rank,
tag=prev_microbatch_id,
)
for req, py_result in zip(finished_reqs,
finished_reqs_py_results):
req.py_result = py_result

elif self.dist.is_last_pp_rank and len(finished_reqs):
if self.send_handles[
prev_microbatch_id] is not None:
self.send_handles[prev_microbatch_id].wait()
self.send_handles[
prev_microbatch_id] = self.dist.isend_object(
[r.py_result for r in finished_reqs],
dest=self.dist.next_pp_rank,
tag=prev_microbatch_id)
self._handle_logits_communication(
previous_batch, prev_microbatch_id)

finished_requests = self._handle_responses()
previous_scheduled_batch = previous_batch.sample_state.scheduled_requests
Expand Down Expand Up @@ -1728,6 +1698,41 @@ def _terminate_ctx_finished_requests(self):
self._terminate_request(request)
self.ctx_in_transmission_requests.remove(request)

def _handle_logits_communication(self, previous_batch, prev_microbatch_id):
"""Handle logits communication between pipeline parallel ranks.

If logits were requested, the last PP rank sends to the first PP rank (who sends responses)
the logits of the requests that have finished.

Args:
previous_batch: The previous batch state
prev_microbatch_id: The microbatch ID for the previous batch
"""
# NOTE: If the rank processing the logits ever becomes the same as
# the rank sending the responses, this code can be removed.
finished_reqs = [
r for r in
previous_batch.sample_state.scheduled_requests.all_requests()
if r.state == LlmRequestState.GENERATION_COMPLETE and (
r.py_return_context_logits or r.py_return_generation_logits)
]
if self.dist.is_first_pp_rank and len(finished_reqs):
finished_reqs_py_results = [r.py_result for r in finished_reqs]
finished_reqs_py_results = self.dist.recv_object(
src=self.dist.prev_pp_rank,
tag=prev_microbatch_id,
)
for req, py_result in zip(finished_reqs, finished_reqs_py_results):
req.py_result = py_result

elif self.dist.is_last_pp_rank and len(finished_reqs):
if self.send_handles[prev_microbatch_id] is not None:
self.send_handles[prev_microbatch_id].wait()
self.send_handles[prev_microbatch_id] = self.dist.isend_object(
[r.py_result for r in finished_reqs],
dest=self.dist.next_pp_rank,
tag=prev_microbatch_id)

def _await_any_response(self,
timeout: Optional[float] = None
) -> List[LlmResponse]:
Expand Down