Skip to content

Gp/fix api parallel#567

Merged
helloyongyang merged 2 commits intomainfrom
gp/fix_api_parallel
Dec 4, 2025
Merged

Gp/fix api parallel#567
helloyongyang merged 2 commits intomainfrom
gp/fix_api_parallel

Conversation

@GACLove
Copy link
Contributor

@GACLove GACLove commented Dec 4, 2025

No description provided.

@GACLove GACLove requested a review from helloyongyang December 4, 2025 07:24
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @GACLove, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request focuses on refining the distributed API for parallel processing by establishing a dedicated communication channel for task distribution and bolstering error recovery mechanisms. The changes aim to enhance the stability and reliability of the distributed inference system, particularly in handling task broadcasting and ensuring proper synchronization across processes, even in the face of failures.

Highlights

  • Dedicated Process Group for Task Distribution: A new gloo process group, self.task_pg, has been introduced in the DistributedManager to specifically handle the broadcasting of task data. This group is initialized with a 30-day timeout, ensuring robust and isolated communication for task distribution.
  • Enhanced Distributed Error Handling: Error handling in process_request and worker_loop within inference/worker.py has been significantly improved. The system now ensures that dist_manager.barrier() is consistently called and error states are correctly propagated by rank 0, even when exceptions occur, which helps prevent deadlocks in distributed environments.
  • Streamlined Broadcast Mechanism: The _broadcast_byte_chunks and _receive_byte_chunks methods in distributed_utils.py have been refactored. Explicit device handling has been removed, and all broadcast operations now consistently utilize the newly introduced self.task_pg, simplifying the communication logic.
  • Improved Parallel Configuration Management: The inference/worker.py now includes a call to set_parallel_config when parallel execution is enabled. This change suggests a more structured approach to managing configurations for distributed inference setups.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces significant improvements to the distributed inference capabilities by creating a dedicated 'gloo' process group for task communication, which should enhance stability and prevent deadlocks. The error handling in the worker process is also made more robust. I've identified a potential crash in an error handling path and suggested several performance optimizations for data serialization. Overall, these are great changes for improving the reliability of the parallel execution.

if self.rank == 0:
if has_error:
return {
"task_id": task_data.get("task_id", "unknown"),
Copy link
Contributor

Choose a reason for hiding this comment

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

high

If an exception occurs early in process_request (e.g., if it's called with task_data=None), task_data could be None at this point. Calling .get() on None would cause a crash within the error handling logic, which can mask the original error. This change adds a check to handle this case gracefully.

Suggested change
"task_id": task_data.get("task_id", "unknown"),
"task_id": task_data.get("task_id", "unknown") if task_data else "unknown",

chunk = data_bytes[start_idx:end_idx]
task_tensor = torch.tensor(list(chunk), dtype=torch.uint8).to(device)
dist.broadcast(task_tensor, src=0)
task_tensor = torch.tensor(list(chunk), dtype=torch.uint8)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Using torch.tensor(list(chunk)) is inefficient for converting bytes to a tensor, especially for large chunks (1MB). It creates a large intermediate list of integers. A more performant approach is to use numpy.frombuffer which creates a view on the byte buffer without copying data, and then convert that to a tensor. You will need to add import numpy at the top of the file.

Suggested change
task_tensor = torch.tensor(list(chunk), dtype=torch.uint8)
task_tensor = torch.from_numpy(numpy.frombuffer(chunk, dtype=numpy.uint8))

chunk = data_bytes[-remaining:]
task_tensor = torch.tensor(list(chunk), dtype=torch.uint8).to(device)
dist.broadcast(task_tensor, src=0)
task_tensor = torch.tensor(list(chunk), dtype=torch.uint8)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Similar to the loop above, this conversion from bytes to tensor is inefficient. Using numpy.frombuffer is recommended for better performance. You will need to add import numpy at the top of the file if you haven't already.

Suggested change
task_tensor = torch.tensor(list(chunk), dtype=torch.uint8)
task_tensor = torch.from_numpy(numpy.frombuffer(chunk, dtype=numpy.uint8))

received.extend(task_tensor.cpu().numpy())
task_tensor = torch.empty(chunk_length, dtype=torch.uint8)
dist.broadcast(task_tensor, src=0, group=self.task_pg)
received.extend(task_tensor.numpy())
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

bytearray.extend() with a NumPy array iterates through the array and appends each number individually, which is inefficient. Using tobytes() and += provides a more direct and performant way to append the tensor's data.

Suggested change
received.extend(task_tensor.numpy())
received += task_tensor.numpy().tobytes()

@helloyongyang helloyongyang merged commit 14034d8 into main Dec 4, 2025
2 checks passed
@GACLove GACLove deleted the gp/fix_api_parallel branch December 4, 2025 09:05
helloyongyang pushed a commit that referenced this pull request Mar 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants