Skip to content

feat: use post4 libtorch_npu with early Python init and native ProcessGroupHCCL.#2006

Draft
yingxudeng wants to merge 2 commits into
xLLM-AI:mainfrom
yingxudeng:feat/npu-python-graph-tp-post4
Draft

feat: use post4 libtorch_npu with early Python init and native ProcessGroupHCCL.#2006
yingxudeng wants to merge 2 commits into
xLLM-AI:mainfrom
yingxudeng:feat/npu-python-graph-tp-post4

Conversation

@yingxudeng

Copy link
Copy Markdown
Collaborator

Description

Related Issues

Change Type

  • Bug fix
  • New feature
  • Performance improvement
  • Refactor
  • Documentation
  • Test
  • Build or CI

Pull Request Checklist

Thank you for contributing to xLLM. Before requesting review, please make sure the following items are complete.

PR Title and Commit Messages

  • The PR title and each commit message follow the xLLM commit format: <type>: <subject>.

Allowed types: feat, bugfix, docs, test, refactor, chore, style, revert, perf, model, build, release.
The subject should use clear English, start with a verb, include at least 4 words, and end with ..

Pre-commit Checks

  • I have installed pre-commit by running pip install pre-commit or an equivalent command.
  • I have installed the hooks with pre-commit install.
  • I have run pre-commit run --all-files and fixed any reported issues.

If you are unsure how to set up pre-commit, see the pre-commit documentation.

Self Review

  • I have self-reviewed the code according to .agents/skills/code-review/references/custom-code-style.md, especially code written or assisted by AI.
  • I have rebased this PR onto the latest main branch.

Build and Test Coverage

  • Tests have been added or updated as needed.
  • CUDA: python setup.py build test has passed on a CUDA machine.
  • NPU: python setup.py build test has passed on an NPU machine.
  • MLU: python setup.py build test has passed on an MLU machine.

Reviewer Notes

…sGroupHCCL.

Replace the C++ custom-op TP workaround with a native Python ProcessGroup
approach that mirrors the CUDA path:

- xllm.cpp: early Py_Initialize + import torch_npu before workers start,
  so post4 libtorch_npu's PyGILState_Ensure in empty_with_format() has a
  valid interpreter. Dynamic device index, exception-safe monkeypatch,
  narrow RuntimeError catch, guarded PyEval_SaveThread, CHECK aclInit.
- collectives.py: full Python implementation using ProcessGroupHCCL for
  NPU and ProcessGroupNCCL for CUDA, registered via @custom_op.
- py_causal_lm.cpp: call Python init_tp_group() like the CUDA path
  instead of the C++ set_tp_process_group() hack.
- npu_ops_library.cpp: remove all_reduce_/all_gather C++ ops (now Python),
  add update_decode_graph_metadata NPU impl (tensor copy).
- npu_paged_attention.py: use PyTorch index_put for KV cache write
  (thread-safe vs ATB ReshapeAndCache singleton). Causal mask grows on
  demand for seq_len > 2048.
- device.cpp: conditionalize init_device_context — Python path uses
  SetDevice only, C++ path retains full torch_npu::init_npu.
- master.cpp: force npu_kernel_backend=TORCH for python model_impl
  (ATB path skips tp_group creation). Restore in-process multi-device
  TP for Python executor (new multi-process arch takes single device
  per process, but Python model needs all visible devices).
- executor.py: treat "eager" as graph-off to avoid inductor fallthrough.

Verified: Qwen3-4B TP2 --model_impl=python --python_graph_backend=eager
on dyx-xllm-cann9-main with post4 libtorch_npu.so (71MB). Inference
output correct across Chinese/English/code tests.
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@yingxudeng
yingxudeng marked this pull request as draft July 22, 2026 12:50
Add scripts/deps/torch_npu_install.py mirroring the TileLang pattern:
detect installed torch_npu version via importlib.metadata, download and
install the post4 wheel from JD storage if missing or mismatched.

Handles pip 26+ strict PEP 440 filename validation by downloading to a
temp dir with a compliant filename before pip install.

Integrated into setup.py as _ensure_torch_npu_ready(), called before
_ensure_tilelang_ascend_ready() in the NPU build path.
TORCH_NPU_WHEELS: dict[tuple[str, str], str] = {
# (cann_version, arch) → wheel URL
("9.0", "arm"): (
"http://storage.jd.local/bmp-packages/NPU_CICD/compile-base/"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

这个链接可以吗

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.

1 participant