Skip to content

Conversation

@yyyy2000
Copy link

@yyyy2000 yyyy2000 commented Dec 21, 2025

What does this PR do?

Add npu unit test workflow. Enable unit tests to auto-detect device (GPU/NPU)

Checklist Before Starting

  • Search for similar PRs. Paste at least one query link here: ...
  • Format the PR title as [{modules}] {type}: {description} (This will be checked by the CI)
    • {modules} include fsdp, megatron, sglang, vllm, rollout, trainer, ci, training_utils, recipe, hardware, deployment, ray, worker, single_controller, misc, perf, model, algo, env, tool, ckpt, doc, data, cfg, reward
    • If this PR involves multiple modules, separate them with , like [megatron, fsdp, doc]
    • {type} is in feat, fix, refactor, chore, test
    • If this PR breaks any API (CLI arguments, config, function signature, etc.), add [BREAKING] to the beginning of the title.
    • Example: [BREAKING][fsdp, megatron] feat: dynamic batching

Test

For changes that can not be tested by CI (e.g., algorithm implementation, new model support), validate by experiment(s) and show results like training curve plots, evaluation results, etc.

API and Usage Example

Demonstrate how the API changes if any, and provide usage example(s) if possible.

# Add code snippet or script demonstrating how to use this

Design & Code Changes

Demonstrate the high-level design if this PR is complex, and list the specific changes.

Checklist Before Submitting

Important

Please check all the following items before requesting a review, otherwise the reviewer might deprioritize this PR for review.

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 NPU support for unit tests by abstracting device-specific logic into utility functions. The changes primarily involve replacing hardcoded "cuda" references with calls to these new utilities like get_device_name. While the overall approach is correct, I've identified a few critical issues in the implementation of some new helper functions and their usage. These could lead to test failures in environments without NPU support. I've provided detailed comments and code suggestions to address these problems and improve the robustness of the device detection logic.

Comment on lines 42 to 52
def get_device_name_ray() -> str:
"""Function that gets the torch.device based on the current machine.
This currently only supports CPU, CUDA, NPU.
Returns:
device
"""
if torch.cuda.is_available():
device = "GPU"
elif torch.npu.is_available():
device = "NPU"
return device
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

This new helper function get_device_name_ray has two critical issues that will cause tests to crash:

  1. Unsafe NPU check: The call torch.npu.is_available() will raise an AttributeError if the torch.npu module is not present (e.g., in a CUDA-only environment).
  2. UnboundLocalError: If neither CUDA nor NPU is available, the device variable is never assigned, leading to an UnboundLocalError on the return statement.

Please use a safe method to check for NPU availability and ensure there's a fallback return value (e.g., "CPU") to prevent crashes.

Suggested change
def get_device_name_ray() -> str:
"""Function that gets the torch.device based on the current machine.
This currently only supports CPU, CUDA, NPU.
Returns:
device
"""
if torch.cuda.is_available():
device = "GPU"
elif torch.npu.is_available():
device = "NPU"
return device
def get_device_name_ray() -> str:
"""Function that gets the torch.device based on the current machine.
This currently only supports CPU, CUDA, NPU.
Returns:
device
"""
from verl.utils.device import is_npu_available
if torch.cuda.is_available():
return "GPU"
elif is_npu_available:
return "NPU"
return "CPU"

Copy link
Author

Choose a reason for hiding this comment

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

已修改

Comment on lines 69 to 76
if torch.cuda.is_available():
torch.cuda.set_device(cls.rank)
cls.device = torch.device(f"cuda:{cls.rank}")
elif torch.npu.is_available():
torch.npu.set_device(cls.rank)
cls.device = torch.device(f"npu:{cls.rank}")
else:
cls.device = torch.device("cpu")
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

The call torch.npu.is_available() is unsafe and will raise an AttributeError if the torch.npu module is not available (e.g., in a CUDA-only or CPU-only environment). This will cause the test to crash. Please use a safe way to check for NPU availability, for example by using get_device_name from verl.utils.device, to ensure the code runs correctly across different hardware environments.

Suggested change
if torch.cuda.is_available():
torch.cuda.set_device(cls.rank)
cls.device = torch.device(f"cuda:{cls.rank}")
elif torch.npu.is_available():
torch.npu.set_device(cls.rank)
cls.device = torch.device(f"npu:{cls.rank}")
else:
cls.device = torch.device("cpu")
from verl.utils.device import get_device_name
device_name = get_device_name()
if device_name == "cuda":
torch.cuda.set_device(cls.rank)
cls.device = torch.device(f"cuda:{cls.rank}")
elif device_name == "npu":
torch.npu.set_device(cls.rank)
cls.device = torch.device(f"npu:{cls.rank}")
else:
cls.device = torch.device("cpu")

Copy link
Author

Choose a reason for hiding this comment

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

已修改


# each rank holds tensor [rank+1]
local = torch.tensor([float(rank + 1)], device=f"cuda:{rank}")
local = torch.tensor([float(rank + 1)], device=get_device_name())
Copy link
Contributor

Choose a reason for hiding this comment

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

high

For consistency and clarity, it's better to explicitly specify the device with its index, similar to how it's done in the _worker_mask function in this same file (device=f"{get_device_name()}:{rank}"). While the current code works because set_device is called earlier, being explicit avoids reliance on implicit state and makes the code easier to understand and maintain.

Suggested change
local = torch.tensor([float(rank + 1)], device=get_device_name())
local = torch.tensor([float(rank + 1)], device=f"{get_device_name()}:{rank}")

Copy link
Author

Choose a reason for hiding this comment

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

已修改

@FightingZhen FightingZhen marked this pull request as draft December 22, 2025 12:32
@FightingZhen FightingZhen marked this pull request as ready for review December 27, 2025 02:17
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