已合并
【PR】:完善pytorch场景下环境部署文档,安装脚本和用例实现 #1398
【PR】:完善pytorch场景下环境部署文档,安装脚本和用例实现 #1398
已合并
zzq创建于 7月21日
12 个文件变更+645-61
@@ -1,24 +0,0 @@
1-# autofuse 样例使用指导
2- 
Z
已过期

删除 examples 顶层 README 后仍有 4 个失效链接 当前仍指向已删除文件: autofuse/README.md:67 autofuse/README_en.md:81 docs/zh/quick_install.md:123 docs/en/quick_install.md:123 应分别更新到: autofuse/examples/pytorch/README.md autofuse/examples/pytorch/README_en.md

likedislike
3-## 功能描述
4- 
5-使用 autofuse 完成各种类型的算子融合
6- 
7-## 目录结构
8-```text
9-├── af_pointwise # pointwise 类型算子融合的样例
10-│ └── af_add_ge.py # 通过 autofuse 完成 add 和 ge 两个 pointwise 类型算子的融合
11-├── af_reduce # reduce 类型算子融合的样例
12-│ └── af_mul_reducesum.py # 通过 autofuse 完成 mul 和 reducesum 两个 pointwise 类型算子的融合
13-```
14-## 前置说明
15-请务必参考[《Autofuse 简介与快速上手》](../README.md)完成前置环境准备。
16- 
17-## 用例演示
18- 
19-[用例1](af_pointwise/README.md)
20-[用例2](af_reduce/README.md)
21- 
22-## 参考
23- 
24-请参考[inductor-npu-ext使用手册](https://gitcode.com/Ascend/torchair/blob/master/experimental/_inductor_npu_ext/docs/manuals.md)的相关内容。
@@ -1,27 +0,0 @@
1-# Autofuse Sample Usage Guide
2- 
3-## Function Description
4- 
5-Use autofuse to complete various types of operator fusion.
6- 
7-## Directory Structure
8-```text
9-├── af_pointwise # pointwise type operator fusion sample
10-│ └── af_add_ge.py # complete fusion of add and ge two pointwise type operators through autofuse
11-├── af_reduce # reduce type operator fusion sample
12-│ └── af_mul_reducesum.py # complete fusion of mul and reducesum two pointwise type operators through autofuse
13-```
14- 
15-## Prerequisites
16- 
17-Please refer to "[Autofuse Introduction and Quick Start](../README.md)" to complete prerequisite environment preparation.
18- 
19-## Use Case Demonstrations
20- 
21-[Use Case 1](af_pointwise/README.md)
22- 
23-[Use Case 2](af_reduce/README.md)
24- 
25-## Reference
26- 
27-Please refer to relevant content in [inductor-npu-ext User Manual](https://gitcode.com/Ascend/torchair/blob/master/experimental/_inductor_npu_ext/docs/manuals.md).
@@ -0,0 +1,91 @@
1+# PyTorch Inductor 场景用例演示
2+ 
3+## 功能描述
4+ 
5+使用 `torch.compile` 完成 PyTorch 网络下的算子融合。
6+ 
7+当前包含以下两个用例:
8+ 
9+- `add + ge`:将加法和比较算子融合为一个算子;
10+- `mul + reducesum`:将乘法和求和归约算子融合为一个算子。
11+ 
12+两个用例均开启 NPU Profiling,可通过生成的性能分析文件查看融合后的 Kernel。
13+ 
14+## 目录结构
15+ 
16+```text
17+pytorch
18+├── README.md
19+├── README_en.md
20+├── af_pointwise
21+│ ├── README.md
22+│ ├── README_en.md
23+│ └── af_add_ge.py # 融合 add + ge
24+└── af_reduce
25+ ├── README.md
26+ ├── README_en.md
27+ └── af_mul_reducesum.py # 融合 mul + reducesum
28+```
29+ 
30+## 前置说明
31+ 
32+运行本用例前,请先认真阅读[ PyTorch环境安装说明 ](../../../docs/env_install/pytorch/env_pytorch.md)。需完成以下步骤:
33+1. CANN 包版本要求为 `9.0.0` 及以上,通过 [CANN 快速安装](https://www.hiascend.com/cann/download?versionId=745&ids=d802%2Ch0501%2Ch0602%2Ch0701) 正确安装 toolkit 和 ops 包,可以参考[ 安装指导 ](../../../docs/zh/quick_install.md)。
34+2. `torch_npu` 版本要求为 `2.9.0` 及以上,可以根据 [环境快速安装脚本](../../../scripts/env_install/pytorch/setup_torch_npu_daily.sh) 快速安装python环境和 `torch_npu`
35+ 
36+## 设置环境变量
37+ 
38+每次新开终端后,执行:
39+ 
40+```bash
41+# 环境激活
42+source /mnt/workspace/env/venv/torch210_daily/bin/activate
43+ 
44+# CANN 包安装路径根据实际安装位置确定。
45+export CANN_INSTALL_PATH=/home/developer/Ascend
46+ 
47+# 加载 CANN 相关环境变量
48+source $CANN_INSTALL_PATH/cann/set_env.sh
49+ 
50+#假设跑在 device0
51+export ASCEND_DEVICE_ID=0
52+```
53+ 
54+## 执行用例
55+ 
56+### add + ge 融合
57+ 
58+```bash
59+cd af_pointwise
60+python af_add_ge.py
61+```
62+ 
63+### mul + reducesum 融合
64+ 
65+```bash
66+cd af_reduce
67+python af_mul_reducesum.py
68+```
69+ 
70+## 预期执行结果
71+ 
72+程序执行完成后,当前目录下会生成 `profiling` 目录。
73+ 
74+可在以下目录中查看算子执行详情:
75+ 
76+```text
77+profiling/PROF_时间戳/mindstudio_profiler_output
78+```
79+ 
80+打开其中的:
81+ 
82+```text
83+op_summary_时间戳.csv
84+```
85+ 
86+如果算子列表中存在名称以 `autofused_` 开头的 Kernel,表示相关算子已经成功融合为一个融合算子。
87+ 
88+## 参考
89+ 
90+- [Autofuse 简介与快速上手](../../README.md)
91+- [Profiling 性能分析工具指南](https://hiascend.com/document/redirect/CannCommunityToolProfiling)
@@ -0,0 +1,92 @@
1+# PyTorch Inductor + AscendC Example Demonstration
2+ 
3+## Description
4+ 
5+Use the AscendC backend of `torch.compile` to perform operator fusion for PyTorch networks.
6+ 
7+The following two examples are currently included:
8+ 
9+- `add + ge`: fuses the addition and comparison operators into a single operator;
10+- `mul + reducesum`: fuses the multiplication and sum-reduction operators into a single operator.
11+ 
12+NPU Profiling is enabled for both examples. The generated profiling files can be used to view the fused Kernel.
13+ 
14+## Directory Structure
15+ 
16+```text
17+pytorch
18+├── README.md
19+├── README_en.md
20+├── af_pointwise
21+│ ├── README.md
22+│ ├── README_en.md
23+│ └── af_add_ge.py # Fuse add + ge
24+└── af_reduce
25+ ├── README.md
26+ ├── README_en.md
27+ └── af_mul_reducesum.py # Fuse mul + reducesum
28+```
29+ 
30+## Prerequisites
31+ 
32+Before running the examples, carefully read the [PyTorch Environment Installation Guide](../../../docs/env_install/pytorch/env_pytorch.md) and complete the following steps:
33+ 
34+1. CANN version `9.0.0` or later is required. Install the Toolkit and OPS packages correctly through [CANN Quick Installation](https://www.hiascend.com/cann/download?versionId=745&ids=d802%2Ch0501%2Ch0602%2Ch0701). For details, see the [Installation Guide](../../../docs/zh/quick_install.md).
35+2. `torch_npu` version `2.9.0` or later is required. You can use the [Quick Environment Installation Script](../../../scripts/env_install/pytorch/setup_torch_npu_daily.sh) to quickly install the Python environment and `torch_npu`.
36+ 
37+## Setting Environment Variables
38+ 
39+Run the following commands each time you open a new terminal:
40+ 
41+```bash
42+# Activate the environment.
43+source /mnt/workspace/env/venv/torch210_daily/bin/activate
44+ 
45+# Set the CANN installation path according to the actual installation location.
46+export CANN_INSTALL_PATH=/home/developer/Ascend
47+ 
48+# Load CANN environment variables.
49+source $CANN_INSTALL_PATH/cann/set_env.sh
50+ 
51+# Assume the example runs on device 0.
52+export ASCEND_DEVICE_ID=0
53+```
54+ 
55+## Running the Examples
56+ 
57+### add + ge Fusion
58+ 
59+```bash
60+cd af_pointwise
61+python af_add_ge.py
62+```
63+ 
64+### mul + reducesum Fusion
65+ 
66+```bash
67+cd af_reduce
68+python af_mul_reducesum.py
69+```
70+ 
71+## Expected Results
72+ 
73+After the program finishes running, a `profiling` directory is generated in the current directory.
74+ 
75+Operator execution details can be viewed in the following directory:
76+ 
77+```text
78+profiling/PROF_timestamp/mindstudio_profiler_output
79+```
80+ 
81+Open the following file:
82+ 
83+```text
84+op_summary_timestamp.csv
85+```
86+ 
87+If the operator list contains a Kernel whose name starts with `autofused_`, the related operators have been successfully fused into a single fused operator.
88+ 
89+## References
90+ 
91+- [Autofuse Introduction and Quick Start](../../README.md)
92+- [Profiling Performance Analysis Tool Guide](https://hiascend.com/document/redirect/CannCommunityToolProfiling)
Rautofuse/examples/af_pointwise/README.mdautofuse/examples/pytorch/af_pointwise/README.md+0-0
文件重命名但无更改。
Rautofuse/examples/af_pointwise/README_en.mdautofuse/examples/pytorch/af_pointwise/README_en.md+0-0
文件重命名但无更改。
Rautofuse/examples/af_pointwise/af_add_ge.pyautofuse/examples/pytorch/af_pointwise/af_add_ge.py+8-5
@@ -13,8 +13,6 @@
13import torch13import torch
14import torch_npu14import torch_npu
15import torch.nn as nn15import torch.nn as nn
16-# === 核心:导入 inductor_npu_ext 后,才能走到 Autofuse 后端
17-import inductor_npu_ext
18 16 
19# ===== 1. 昇腾 NPU 配置 =====17# ===== 1. 昇腾 NPU 配置 =====
20DEVICE = "npu:0" # 假设使用0卡18DEVICE = "npu:0" # 假设使用0卡
@@ -30,9 +28,14 @@ class MyModel(nn.Module):
30 result = torch.ge(torch.add(x, y), z)28 result = torch.ge(torch.add(x, y), z)
31 return result29 return result
32 30 
33-# ===== 3. 使能 NPU + Inductor =====31+# ===== 3. inductor + 昇腾NPU自动融合后端 =====
34model = MyModel().to(DEVICE)32model = MyModel().to(DEVICE)
35-model = torch.compile(model, dynamic=False, fullgraph=True)33+model = torch.compile(
34+ model,
35+ dynamic=False,
36+ fullgraph=True,
37+ options={"npu_backend": "ascendc"},
38+)
36 39 
37# ===== 4. 创建输入 =====40# ===== 4. 创建输入 =====
38x = torch.randn(128, 50, device=DEVICE)41x = torch.randn(128, 50, device=DEVICE)
@@ -66,4 +69,4 @@ with torch_npu.profiler.profile(
66 experimental_config=experimental_config) as prof:69 experimental_config=experimental_config) as prof:
67 # 跑 100 step70 # 跑 100 step
68 for _ in range(100):71 for _ in range(100):
69- result = model(x, y, z)72+ result = model(x, y, z)
Rautofuse/examples/af_reduce/README.mdautofuse/examples/pytorch/af_reduce/README.md+0-0
文件重命名但无更改。
Rautofuse/examples/af_reduce/README_en.mdautofuse/examples/pytorch/af_reduce/README_en.md+0-0
文件重命名但无更改。
Rautofuse/examples/af_reduce/af_mul_reducesum.pyautofuse/examples/pytorch/af_reduce/af_mul_reducesum.py+8-5
@@ -13,8 +13,6 @@
13import torch13import torch
14import torch_npu14import torch_npu
15import torch.nn as nn15import torch.nn as nn
16-# === 核心:导入 inductor_npu_ext 后,才能走到 Autofuse 后端
17-import inductor_npu_ext
18 16 
19# ===== 1. 昇腾 NPU 配置 =====17# ===== 1. 昇腾 NPU 配置 =====
20DEVICE = "npu:0" # 假设使用0卡18DEVICE = "npu:0" # 假设使用0卡
@@ -30,9 +28,14 @@ class MyModel(nn.Module):
30 result = torch.sum(torch.mul(x, y))28 result = torch.sum(torch.mul(x, y))
31 return result29 return result
32 30 
33-# ===== 3. 使能 NPU + Inductor =====31+# ===== 3. inductor + 昇腾NPU自动融合后端 =====
34model = MyModel().to(DEVICE)32model = MyModel().to(DEVICE)
35-model = torch.compile(model, dynamic=False, fullgraph=True)33+model = torch.compile(
34+ model,
35+ dynamic=False,
36+ fullgraph=True,
37+ options={"npu_backend": "ascendc"},
38+)
36 39 
37# ===== 4. 创建输入 =====40# ===== 4. 创建输入 =====
38x = torch.randn(256, 100, device=DEVICE)41x = torch.randn(256, 100, device=DEVICE)
@@ -65,4 +68,4 @@ with torch_npu.profiler.profile(
65 experimental_config=experimental_config) as prof:68 experimental_config=experimental_config) as prof:
66 # 跑 100 step69 # 跑 100 step
67 for _ in range(100):70 for _ in range(100):
68- result = model(x, y)71+ result = model(x, y)
@@ -0,0 +1,262 @@
1+# PyTorch torch_npu Daily 环境部署
2+ 
3+## 前置准备
4+ 
5+`torch_npu Daily` 的安装主要依赖 Python 环境,运行 NPU 用例时依赖已经安装好的 CANN Toolkit、OPS 包和 NPU 驱动。
6+ 
7+请根据以下步骤完成前置准备:
8+ 
9+1. CANN 包版本要求为 `9.0.0` 及以上,通过 [CANN 快速安装](https://www.hiascend.com/cann/download?versionId=745&ids=d802%2Ch0501%2Ch0602%2Ch0701) 正确安装 toolkit 和 ops 包,可以参考[ 安装指导 ](../../../docs/zh/quick_install.md)。
10+ 
11+2. 通过下方步骤搭建 PyTorch 环境。
12+ 
13+> **注意**:下文中的 `/mnt/workspace` 为华为云开发环境挂载目录,可根据实际环境替换。
14+ 
15+---
16+ 
17+## 一、创建虚拟环境
18+ 
19+ 
20+### 1. 安装 Python 编译依赖
21+ 
22+`pyenv install` 会在当前机器上编译 Python,因此需要提前安装 Python 编译所需的系统依赖。如果机器已经具备完整的 Python 编译环境,可以跳过此步骤。
23+ 
24+先查看操作系统:
25+ 
26+```bash
27+cat /etc/os-release
28+```
29+ 
30+#### Ubuntu / Debian
31+ 
32+```bash
33+sudo apt-get update
34+ 
35+sudo apt-get install -y \
36+ build-essential \
37+ git \
38+ wget \
39+ curl \
40+ libssl-dev \
41+ zlib1g-dev \
42+ libbz2-dev \
43+ libreadline-dev \
44+ libsqlite3-dev \
45+ libncurses-dev \
46+ xz-utils \
47+ tk-dev \
48+ libffi-dev \
49+ liblzma-dev
50+```
51+ 
52+#### openEuler / CentOS / RHEL
53+ 
54+```bash
55+sudo yum install -y \
56+ gcc \
57+ gcc-c++ \
58+ make \
59+ git \
60+ wget \
61+ curl \
62+ openssl-devel \
63+ zlib-devel \
64+ bzip2-devel \
65+ readline-devel \
66+ sqlite-devel \
67+ ncurses-devel \
68+ xz-devel \
69+ tk-devel \
70+ libffi-devel
71+```
72+ 
73+只需要执行与当前操作系统对应的一组命令。
74+ 
75+### 2. 安装 pyenv
76+ 
77+```bash
78+mkdir -p /mnt/workspace/env
79+ 
80+cd /mnt/workspace/env
81+ 
82+git clone https://github.com/pyenv/pyenv.git
83+```
84+ 
85+配置 pyenv 环境变量:
86+ 
87+```bash
88+export PYENV_ROOT="/mnt/workspace/env/pyenv"
89+export PATH="$PYENV_ROOT/bin:$PATH"
90+```
91+ 
92+### 3. 安装指定 Python 版本
93+ 
94+`torch_npu Daily 2.10.0` 需要 Python 3.11,本文使用 Python 3.11.4,可自行更改。
95+ 
96+安装 Python 3.11.4:
97+ 
98+```bash
99+PYTHON_BUILD_MIRROR_URL="https://mirrors.huaweicloud.com/python" \
100+PYTHON_BUILD_MIRROR_URL_SKIP_CHECKSUM=1 \
101+pyenv install 3.11.4
102+```
103+ 
104+### 4. 创建并激活虚拟环境
105+ 
106+创建虚拟环境:
107+ 
108+```bash
109+mkdir -p /mnt/workspace/env/venv
110+ 
111+/mnt/workspace/env/pyenv/versions/3.11.4/bin/python \
112+ -m venv /mnt/workspace/env/venv/torch210_daily
113+```
114+ 
115+激活虚拟环境:
116+ 
117+```bash
118+source /mnt/workspace/env/venv/torch210_daily/bin/activate
119+```
120+ 
121+> **注意**:后续所有命令默认都在已激活的虚拟环境中执行,请勿退出该环境。
122+ 
123+---
124+ 
125+## 二、安装 PyTorch 环境依赖
126+ 
127+### 1. 安装 NumPy
128+ 
129+```bash
130+python -m pip install numpy
131+```
132+ 
133+---
134+ 
135+### 2. 安装 torch_npu Daily
136+ 
137+设置安装参数:
138+ 
139+```bash
140+cd /mnt/workspace/env
141+ 
142+# 版本要求为 2.9.0 及以上,这里使用 2.10.0,可自行更改。
143+TORCH_NPU_VERSION="2.10.0"
144+ 
145+DATE="$(date +%Y%m%d)"
146+ 
147+PY_TAG="$(
148+ python -c 'import sys; print(f"py{sys.version_info.major}{sys.version_info.minor}")'
149+)"
150+ 
151+ARCH="$(uname -m)"
152+ 
153+PKG_DIR="/mnt/workspace/env/torch_npu_pkg"
154+```
155+ 
156+根据版本、日期和 Python 版本生成下载地址:
157+ 
158+```bash
159+URL="https://pytorch-package.obs.cn-north-4.myhuaweicloud.com/pta/Daily/v${TORCH_NPU_VERSION}/${DATE}.1/pytorch_v${TORCH_NPU_VERSION}_${PY_TAG}.tar.gz"
160+```
161+ 
162+检查当天 Daily 包是否存在:
163+ 
164+```bash
165+wget --spider "$URL"
166+```
167+ 
168+创建下载目录:
169+ 
170+```bash
171+rm -rf "$PKG_DIR"
172+mkdir -p "$PKG_DIR"
173+```
174+ 
175+下载并解压:
176+ 
177+```bash
178+wget -O "$PKG_DIR/pytorch.tar.gz" "$URL"
179+ 
180+tar -xzf "$PKG_DIR/pytorch.tar.gz" \
181+ -C "$PKG_DIR"
182+```
183+ 
184+查找 `torch_npu` wheel:
185+ 
186+```bash
187+WHEEL="$(
188+ find "$PKG_DIR" \
189+ -type f \
190+ -name "torch_npu*${ARCH}.whl" |
191+ head -n 1
192+)"
193+```
194+ 
195+安装:
196+ 
197+```bash
198+python -m pip install --force-reinstall "$WHEEL"
199+```
200+ 
201+安装完成后,Python 包位于:
202+ 
203+```text
204+/mnt/workspace/env/venv/torch210_daily/lib/python3.11/site-packages
205+```
206+ 
207+---
208+ 
209+## 三、加载 CANN 环境
210+ 
211+运行 NPU 用例前,需要加载已经安装好的 CANN Toolkit 和 OPS:
212+ 
213+```bash
214+# CANN 包安装路径根据实际安装位置确定。
215+source /home/developer/Ascend/cann/set_env.sh
216+```
217+ 
218+设置运行设备:
219+ 
220+```bash
221+#假设跑在 device0
222+export ASCEND_DEVICE_ID=0
223+```
224+ 
225+---
226+ 
227+## 四、验证环境
228+ 
229+验证 PyTorch 和 `torch_npu`
230+ 
231+```bash
232+python - <<EOF
233+import torch
234+import torch_npu
235+ 
236+print("torch:", torch.__version__)
237+print("torch_npu:", torch_npu.__version__)
238+EOF
239+```
240+ 
241+---
242+ 
243+## 五、一键配置脚本
244+ 
245+也可使用[ 一键配置脚本 ](../../../scripts/env_install/pytorch/setup_torch_npu_daily.sh)自动完成虚拟环境创建和 PyTorch 环境依赖安装。
246+在 Graph-AutoFusion 仓库根目录执行:
247+ 
248+```bash
249+bash scripts/env_install/pytorch/setup_torch_npu_daily.sh
250+```
251+ 
252+脚本完成后激活环境:
253+ 
254+```bash
255+source /mnt/workspace/env/venv/torch210_daily/bin/activate
256+```
257+ 
258+加载 CANN:
259+ 
260+```bash
261+source /home/developer/Ascend/cann/set_env.sh
262+```
@@ -0,0 +1,184 @@
1+#!/bin/bash
Z

一键安装会失败 脚本直接安装 torch_npu wheel: python -m pip install --force-reinstall "WHEEL" 实际检查了 2026-07-27 默认 2.10.0 Daily 包: Requires-Dist: torch ==2.10.0+cpu 但默认 PyPI 只有 torch==2.10.0,没有 torch==2.10.0+cpu;后者只存在于 PyTorch CPU wheel 源。Daily 压缩包本身也不包含 torch wheel。因此普通全新虚拟环境会报依赖找不到。 建议显式增加 CPU wheel 源,例如: python -m pip install \ --extra-index-url https://download.pytorch.org/whl/cpu \ --force-reinstall "WHEEL"

likedislike
2+# ----------------------------------------------------------------------------------------------------------------------
3+# Copyright (c) 2026 Huawei Technologies Co., Ltd.
4+# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
5+# CANN Open Software License Agreement Version 2.0 (the "License").
6+# Please refer to the License for details. You may not use this file except in compliance with the License.
7+# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
8+# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
9+# See LICENSE in the root of the software repository for the full text of the License.
10+# ----------------------------------------------------------------------------------------------------------------------
11+ 
12+# 使用前置条件:
13+# 1. 已经安装 NPU 驱动、CANN Toolkit 和对应设备的 OPS 包。
14+# 2. 当前用户具有安装 Python 编译依赖的 sudo 权限。
15+# 3. torch_npu 版本要求为 2.9.0 及以上,默认安装 2.10.0,可自行修改。
16+ 
17+# 任意一条命令执行失败时,立即终止脚本。
18+set -e
19+ 
20+# 第一个参数:Python 版本。
21+# 没有指定时,默认使用 3.11.4。
22+PYTHON_VERSION="${1:-3.11.4}"
23+ 
24+# 第二个参数:torch_npu 版本。
25+# 没有指定时,默认使用 2.10.0。
26+TORCH_NPU_VERSION="${2:-2.10.0}"
27+ 
28+# 第三个参数:环境安装目录。
29+# 没有指定时,默认使用 /mnt/workspace/env。
30+ENV_DIR="${3:-/mnt/workspace/env}"
31+ 
32+PYENV_ROOT="$ENV_DIR/pyenv"
33+PYTHON_DIR="$PYENV_ROOT/versions/$PYTHON_VERSION"
Z

PYTHON_VERSION 可造成越界目录删除,涉及32行、116行 PYTHON_VERSION 直接参与删除路径: PYTHON_DIR="PYENVROOT/versions/PYENV_ROOT/versions/PYTHON_VERSION" rm -rf "PYTHONDIR"例如传入:PYTHONVERSION=../../../unrelated实际删除目标会逃逸到:/mnt/workspace/unrelated这违反目录删除边界检查红线,存在用户数据被删除的风险。建议在任何rmrf前校验版本格式:[["PYTHON_DIR" 例如传入: PYTHON_VERSION=../../../unrelated 实际删除目标会逃逸到: /mnt/workspace/unrelated 这违反目录删除边界检查红线,存在用户数据被删除的风险。建议在任何 rm -rf 前校验版本格式: [[ "PYTHON_VERSION" =~ [1]+.[0-9]+.[0-9]+$ ]] || exit 1


  1. 0-9 ↩︎

likedislike
34+BASE_PYTHON="$PYTHON_DIR/bin/python"
35+VENV_DIR="$ENV_DIR/venv/torch210_daily"
36+PKG_DIR="$ENV_DIR/torch_npu_pkg"
37+ 
38+# 一、创建虚拟环境。
39+ 
40+# 查看当前操作系统。
41+cat /etc/os-release
42+ 
43+# 安装 Python 编译依赖。
44+if command -v apt-get >/dev/null 2>&1; then
45+ sudo apt-get update
46+ 
47+ sudo apt-get install -y \
48+ build-essential \
49+ git \
50+ wget \
51+ curl \
52+ libssl-dev \
53+ zlib1g-dev \
54+ libbz2-dev \
55+ libreadline-dev \
56+ libsqlite3-dev \
57+ libncurses-dev \
58+ xz-utils \
59+ tk-dev \
60+ libffi-dev \
61+ liblzma-dev
62+elif command -v yum >/dev/null 2>&1; then
63+ sudo yum install -y \
64+ gcc \
65+ gcc-c++ \
66+ make \
67+ git \
68+ wget \
69+ curl \
70+ openssl-devel \
71+ zlib-devel \
72+ bzip2-devel \
73+ readline-devel \
74+ sqlite-devel \
75+ ncurses-devel \
76+ xz-devel \
77+ tk-devel \
78+ libffi-devel
79+else
80+ echo "未找到 apt-get 或 yum,请手动安装 Python 编译依赖。"
81+ exit 1
82+fi
83+ 
84+# 创建环境目录。
85+mkdir -p "$ENV_DIR"
86+ 
87+cd "$ENV_DIR"
88+ 
89+# 安装 pyenv,已经安装时直接复用。
90+if [ ! -x "$PYENV_ROOT/bin/pyenv" ]; then
91+ rm -rf "$PYENV_ROOT"
92+ git clone https://github.com/pyenv/pyenv.git "$PYENV_ROOT"
93+fi
94+ 
95+# 配置 pyenv 环境变量。
96+export PYENV_ROOT
97+export PATH="$PYENV_ROOT/bin:$PATH"
98+ 
99+# 检查 Python 是否存在,以及关键扩展模块是否正常。
100+if [ -x "$BASE_PYTHON" ] &&
101+ "$BASE_PYTHON" - <<'EOF' >/dev/null 2>&1
102+import ssl
103+import sqlite3
104+import readline
105+import curses
106+import bz2
107+import lzma
108+import ctypes
109+EOF
110+then
111+ echo "Python $PYTHON_VERSION 已安装且关键扩展模块正常,直接复用。"
112+else
113+ echo "Python $PYTHON_VERSION 不存在或编译不完整,重新安装。"
114+ 
115+ # 删除不完整的 Python 和基于该 Python 创建的虚拟环境。
116+ rm -rf "$PYTHON_DIR"
117+ rm -rf "$VENV_DIR"
118+ 
119+ # 安装指定 Python 版本。
120+ PYTHON_BUILD_MIRROR_URL="https://mirrors.huaweicloud.com/python" \
121+ PYTHON_BUILD_MIRROR_URL_SKIP_CHECKSUM=1 \
122+ pyenv install "$PYTHON_VERSION"
123+fi
124+ 
125+# 创建虚拟环境,已经存在时直接复用。
126+if [ ! -x "$VENV_DIR/bin/python" ]; then
127+ rm -rf "$VENV_DIR"
128+ mkdir -p "$ENV_DIR/venv"
129+ 
130+ "$BASE_PYTHON" \
131+ -m venv "$VENV_DIR"
132+fi
133+ 
134+# 激活虚拟环境。
135+source "$VENV_DIR/bin/activate"
136+ 
137+# 二、安装 PyTorch 环境依赖。
138+ 
139+# 安装 NumPy。
140+python -m pip install numpy
141+ 
142+# 进入环境目录。
143+cd "$ENV_DIR"
144+ 
145+# 获取当天日期。
146+DATE="$(date +%Y%m%d)"
147+ 
148+# 获取 Python 版本标识,例如 Python 3.11 对应 py311。
149+PY_TAG="$(
150+ python -c 'import sys; print(f"py{sys.version_info.major}{sys.version_info.minor}")'
151+)"
152+ 
153+# 获取当前机器架构,例如 aarch64 或 x86_64。
154+ARCH="$(uname -m)"
155+ 
156+# 根据版本、日期和 Python 版本生成 Daily 包下载地址。
157+URL="https://pytorch-package.obs.cn-north-4.myhuaweicloud.com/pta/Daily/v${TORCH_NPU_VERSION}/${DATE}.1/pytorch_v${TORCH_NPU_VERSION}_${PY_TAG}.tar.gz"
158+ 
159+# 判断当天的 Daily 包是否已经发布。
160+if ! wget --spider -q "$URL"; then
161+ echo "当天的 torch_npu Daily 包尚未发布,或者下载链接不可访问。"
162+ exit 1
163+fi
164+ 
165+# 清理并创建下载目录。
166+rm -rf "$PKG_DIR"
167+mkdir -p "$PKG_DIR"
168+ 
169+# 下载并解压 Daily 包。
170+wget -O "$PKG_DIR/pytorch.tar.gz" "$URL"
171+ 
172+tar -xzf "$PKG_DIR/pytorch.tar.gz" \
173+ -C "$PKG_DIR"
174+ 
175+# 查找与当前机器架构匹配的 torch_npu Wheel。
176+WHEEL="$(
177+ find "$PKG_DIR" \
178+ -type f \
179+ -name "torch_npu*${ARCH}.whl" |
180+ head -n 1
181+)"
182+ 
183+# 安装 Daily torch_npu Wheel。
184+python -m pip install --force-reinstall "$WHEEL"