Skip to content

Commit de0fa92

Browse files
Deploy preview for PR #229 7e7f3cc
1 parent 6125cbc commit de0fa92

317 files changed

Lines changed: 91404 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎pr-preview/pr-229/.buildinfo‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Sphinx build info version 1
2+
# This file records the configuration used when building these files. When it is not found, a full rebuild will be done.
3+
config: eb161ed5563adaa7f604eddce66c347c
4+
tags: 645f666f9bcd5a90fca523b33c5a78b7
590 KB
Loading
3.67 KB
Loading
96.4 KB
Loading
872 KB
Loading

‎pr-preview/pr-229/_sources/index.rst.txt‎

Lines changed: 671 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.. include:: quick_start.md
2+
:parser: myst_parser.sphinx_
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
LLaMA-Factory
2+
=============
3+
4+
NPU 相关文档由 LLaMA-Factory 官方维护,请访问官方文档站:
5+
6+
- GitHub:`LLaMA-Factory <https://github.com/hiyouga/LLaMA-Factory>`_
7+
- 文档中心:`LLaMA-Factory Docs <https://llamafactory.readthedocs.io/zh-cn/latest/>`_
8+
- NPU 安装:`NPU Installation <https://llamafactory.readthedocs.io/zh-cn/latest/multibackend/npu/npu_installation.html>`_
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.. include:: quick_start.md
2+
:parser: myst_parser.sphinx_
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
# Ray
2+
3+
使用 Ray 将 Task 和 Actor 分别调度到两张昇腾 NPU,并在各自的进程中
4+
运行 `torch_npu` 张量计算。通过本示例,可以了解 Ray 如何发现 NPU、
5+
分配设备以及隔离任务。更多调度方式参见 Ray 的
6+
[Accelerator Support](https://docs.ray.io/en/latest/ray-core/scheduling/accelerators.html) 文档。
7+
8+
## 前置条件
9+
10+
### 硬件
11+
12+
Atlas 900 A2 / A3 训练系列产品,至少有两张可用的 Ascend NPU,并已完成
13+
物理机或容器中的设备与驱动配置。
14+
15+
### 基础软件
16+
17+
在运行本文档之前,机器上需要已经安装并可用:
18+
19+
- Linux aarch64 操作系统;
20+
- 可用的 Python 环境;
21+
- 可用的 CANN toolkit 和驱动;
22+
- 与 CANN 匹配的 `torch` 和 `torch_npu`,且
23+
`torch.npu.is_available() == True`;
24+
- `npu-smi` 能正常显示 NPU 设备。
25+
26+
CANN 与 PyTorch-NPU 的安装和版本匹配可参考
27+
[Ascend PyTorch 安装文档](https://gitcode.com/Ascend/pytorch)。
28+
29+
### 本文档示例使用的版本
30+
31+
**配套机器**:
32+
33+
- **机器类型**:Atlas 900 A2 PODc(Ascend 910B4,64 GB × 2);
34+
- **操作系统**:Ubuntu 22.04。
35+
36+
**配套镜像**:
37+
38+
swr.cn-south-1.myhuaweicloud.com/ascendhub/cann:9.1.0-910b-ubuntu22.04-py3.12
39+
40+
**软件版本**:
41+
42+
| 组件 | 版本 |
43+
| --- | --- |
44+
| Python | 3.12.13 |
45+
| CANN | 9.1.0 |
46+
| torch | 2.9.0+cpu |
47+
| torch_npu | 2.9.0.post2 |
48+
| Ray | xxx |
49+
| NPU | Ascend 910B4 × 2 |
50+
51+
### 检查前置是否满足
52+
53+
检查 Python 版本:
54+
55+
```shell #test id="check-py"
56+
python --version
57+
```
58+
59+
输出结果如下:
60+
61+
```text
62+
Python 3.12.13
63+
```
64+
65+
<!--
66+
```text #test-result id="check-py" fuzzy="xxx"
67+
Python 3.12.xxx
68+
```
69+
-->
70+
71+
检查 Torch、Torch-NPU 和 NPU 设备:
72+
73+
以下代码用 Python 执行:
74+
75+
```python #test id="check-torch"
76+
import torch
77+
import torch_npu
78+
79+
print("torch=", torch.__version__)
80+
print("torch_npu=", torch_npu.__version__)
81+
print("is_available:", torch.npu.is_available())
82+
print("count:", torch.npu.device_count())
83+
```
84+
85+
输出结果如下:
86+
87+
```text #test-result id="check-torch"
88+
torch= 2.9.0+cpu
89+
torch_npu= 2.9.0.post2
90+
is_available: True
91+
count: 2
92+
```
93+
94+
确认 `npu-smi` 可以看到两张 NPU:
95+
96+
```shell
97+
npu-smi info
98+
```
99+
100+
如果 `npu-smi` 不存在或 `import torch_npu` 失败,请先修复驱动、CANN、
101+
Torch 与 Torch-NPU 的版本匹配问题。
102+
103+
## 安装 Ray
104+
105+
安装当前最新 Ray release,并检查安装结果:
106+
107+
```shell #test id="ray-install"
108+
python -m pip install -q -U "ray[default]"
109+
python -c "import ray; print('ray', ray.__version__)"
110+
```
111+
112+
输出结果如下:
113+
114+
```text #test-result id="ray-install" fuzzy="xxx"
115+
ray xxx
116+
```
117+
118+
```{admonition} Note
119+
:class: note
120+
xxx 表示最新的版本号。
121+
```
122+
123+
## 查看 Ray 检测到的 NPU
124+
125+
启动 Ray 后,查看集群中注册的 `NPU` 资源数量。下面的环境应显示两张 NPU。
126+
127+
以下代码用 Python 执行:
128+
129+
```python #test id="ray-detects-npus"
130+
import ray
131+
132+
ray.init(include_dashboard=False, log_to_driver=False)
133+
resources = ray.cluster_resources()
134+
count = int(resources.get("NPU", 0))
135+
assert count == 2, resources
136+
print("Ray NPU resources:", count)
137+
ray.shutdown()
138+
```
139+
140+
输出结果如下:
141+
142+
```text #test-result id="ray-detects-npus" fuzzy="xxx"
143+
Ray NPU resources: 2
144+
```
145+
146+
## 在 Task 和 Actor 中使用 NPU
147+
148+
下面分别启动一个 Actor 和一个 Task,各请求一张 NPU。Ray 会将分配的设备
149+
ID 写入 `ASCEND_RT_VISIBLE_DEVICES`;两个进程各自在可见的 NPU 上计算。
150+
151+
以下代码用 Python 执行:
152+
153+
```python #test id="ray-isolates-npus"
154+
import os
155+
import ray
156+
157+
ray.init(include_dashboard=False, log_to_driver=False)
158+
159+
def assigned_npu():
160+
import torch
161+
import torch_npu
162+
163+
ids = [str(value) for value in ray.get_runtime_context().get_accelerator_ids()["NPU"]]
164+
assert len(ids) == 1, ids
165+
visible = os.environ["ASCEND_RT_VISIBLE_DEVICES"]
166+
assert visible == ids[0], (visible, ids)
167+
value = torch.ones(4, device="npu:0").sum().cpu().item()
168+
return ids[0], value
169+
170+
@ray.remote(resources={"NPU": 1})
171+
class NPUActor:
172+
def ping(self):
173+
return assigned_npu()
174+
175+
@ray.remote(resources={"NPU": 1})
176+
def npu_task():
177+
return assigned_npu()
178+
179+
actor = NPUActor.remote()
180+
actor_result = ray.get(actor.ping.remote())
181+
task_result = ray.get(npu_task.remote())
182+
ids = sorted([actor_result[0], task_result[0]])
183+
values = sorted([actor_result[1], task_result[1]])
184+
assert ids == ["0", "1"], ids
185+
assert values == [4.0, 4.0], values
186+
print("assigned NPU IDs:", ",".join(ids))
187+
print("tensor sums:", ",".join(str(value) for value in values))
188+
ray.kill(actor)
189+
ray.shutdown()
190+
```
191+
192+
输出结果如下:
193+
194+
```text #test-result id="ray-isolates-npus"
195+
assigned NPU IDs: 0,1
196+
tensor sums: 4.0,4.0
197+
```
198+
199+
Ray 的 NPU 是逻辑资源。`resources={"NPU": 0.25}` 这样的请求允许多个
200+
任务共享一张 NPU,但不会提供显存或算力隔离,应用仍需自行保证共享安全。

0 commit comments

Comments
 (0)