Skip to content

fix: copy numpy arrays from np.frombuffer to fix R3 read-only tensor warning#703

Open
NJX-njx wants to merge 1 commit intoradixark:mainfrom
NJX-njx:fix/599-r3-numpy-writable
Open

fix: copy numpy arrays from np.frombuffer to fix R3 read-only tensor warning#703
NJX-njx wants to merge 1 commit intoradixark:mainfrom
NJX-njx:fix/599-r3-numpy-writable

Conversation

@NJX-njx
Copy link
Copy Markdown
Contributor

@NJX-njx NJX-njx commented Mar 6, 2026

Fixes #599 (partial) - np.frombuffer() returns read-only arrays that cause PyTorch warnings and potential undefined behavior when converted via torch.from_numpy(). Added .copy() in both generate_endpoint_utils.py and sglang_rollout.py.

…warning

Fixes radixark#599

np.frombuffer() returns read-only arrays since they reference the
underlying buffer. When these are later converted via torch.from_numpy()
in training_utils/data.py, PyTorch warns about non-writable tensors and
any writes become undefined behavior. This can cause subtle data
corruption in R3 (Rollout Routing Replay).

Fix: Add .copy() after np.frombuffer() in both generate_endpoint_utils.py
and sglang_rollout.py to ensure the arrays are writable before being
passed through the training pipeline.
Copilot AI review requested due to automatic review settings March 6, 2026 11:36
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 resolves an issue where NumPy arrays created using np.frombuffer() were read-only, leading to PyTorch warnings and potential runtime problems when subsequently converted to torch.from_numpy(). By explicitly copying these arrays, the changes ensure data integrity and prevent unexpected behavior in downstream operations.

Highlights

  • Read-only NumPy Array Fix: Added .copy() to the result of np.frombuffer() calls in two locations to prevent issues with read-only arrays when converting them to PyTorch tensors, addressing warnings and potential undefined behavior.
Changelog
  • miles/rollout/generate_utils/generate_endpoint_utils.py
    • Ensured np.frombuffer output is copied to prevent read-only issues.
  • miles/rollout/sglang_rollout.py
    • Applied .copy() to np.frombuffer output for sample.rollout_routed_experts.
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
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a PyTorch warning (and potential undefined behavior) triggered when np.frombuffer() returns a read-only NumPy array that is later passed to torch.from_numpy(). The fix applies .copy() immediately after np.frombuffer() in both code paths that handle the routed_experts field from the SGLang inference response, ensuring the resulting array is writable before being converted to a PyTorch tensor in downstream training code.

Changes:

  • Added .copy() after np.frombuffer(...) in sglang_rollout.py for the legacy SGLang generate path.
  • Added .copy() after np.frombuffer(...) in generate_endpoint_utils.py for the newer shared utility path.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
miles/rollout/sglang_rollout.py Adds .copy() after np.frombuffer() to produce a writable array before reshape() in the legacy generate() function
miles/rollout/generate_utils/generate_endpoint_utils.py Adds .copy() after np.frombuffer() to produce a writable array before reshape() in the shared _get_rollout_topk_from_response() utility

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
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 modifies two files, miles/rollout/generate_utils/generate_endpoint_utils.py and miles/rollout/sglang_rollout.py, to address a potential issue where np.frombuffer returns a read-only array. The change involves adding a .copy() call after np.frombuffer and before .reshape when processing meta_info related to routed_experts or similar data. This ensures that the resulting NumPy array is writable, preventing warnings or undefined behavior when these arrays are subsequently used with torch.from_numpy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

R3 is buggy when --expert-model-parallel-size != --actor-num-gpus-per-node

3 participants