From external-gitcode-ascend-skills
Migrates OligoFormer (siRNA efficacy prediction) from CUDA to Huawei Ascend NPU, covering environment setup, RNA-FM installation, code adaptation, inference verification, and optional training.
How this skill is triggered — by the user, by Claude, or both
Slash command
/external-gitcode-ascend-skills:oligoformerThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
本 Skill 提供将 OligoFormer(siRNA 效能预测模型)从 CUDA 迁移到昇腾 NPU 的完整步骤。
本 Skill 提供将 OligoFormer(siRNA 效能预测模型)从 CUDA 迁移到昇腾 NPU 的完整步骤。 项目地址:https://github.com/lulab/OligoFormer.git
| 项目 | 要求 |
|---|---|
| 硬件 | Ascend910 系列(至少 1 卡) |
| OS | openEuler / Ubuntu / KylinOS(aarch64 或 x86_64) |
| CANN | ≥ 8.0(推荐 8.2+ 或 8.3.RC1) |
| Python | 3.10(原项目要求 3.8,因 torch 2.5.1 不支持 3.8,升级至 3.10) |
| PyTorch | 2.5.1 |
| torch_npu | 2.5.1 |
| conda | 已安装 |
# 设置 CANN 环境(路径根据实际安装位置调整)
source /home/Ascend/ascend-toolkit/set_env.sh
# 验证 NPU 可用
npu-smi info
# 设置华为镜像源
export PIP_INDEX_URL=https://repo.huaweicloud.com/repository/pypi/simple/
cd /home/show
git clone https://github.com/lulab/OligoFormer.git
cd OligoFormer
# 创建 Python 3.10 环境
conda create -n oligoformer-conda python=3.10 -y
conda activate oligoformer-conda
# 安装 PyTorch + torch_npu(版本需与 CANN 匹配)
pip install torch==2.5.1 torchvision==0.20.1 torch_npu==2.5.1 \
-i https://repo.huaweicloud.com/repository/pypi/simple/
# 安装基础运行时依赖
pip install numpy==1.26.4 decorator attrs psutil absl-py cloudpickle \
ml-dtypes scipy tornado \
-i https://repo.huaweicloud.com/repository/pypi/simple/
# 安装项目依赖(不安装 requirements.txt 中的 torch/torchvision,已单独安装)
pip install bio matplotlib pandas prefetch-generator ptflops \
pytorch-ignite scikit-learn tqdm yacs \
-i https://repo.huaweicloud.com/repository/pypi/simple/
OligoFormer 依赖 RNA-FM 生成 RNA 嵌入特征。
cd /home/show/OligoFormer
# 下载打包好的 RNA-FM(含预训练权重)
wget 'https://cloud.tsinghua.edu.cn/f/46d71884ee8848b3a958/?dl=1' -O RNA-FM.tar.gz
tar -zxf RNA-FM.tar.gz
source /home/Ascend/ascend-toolkit/set_env.sh
python -c "import torch; import torch_npu; a = torch.randn(3,4).npu(); print(a + a)"
应输出 device='npu:0' 的 Tensor。
在 scripts/main.py 最顶部(所有 import 之前)添加:
import torch_npu
from torch_npu.contrib import transfer_to_npu
transfer_to_npu 会自动将 torch.cuda.* 映射到 torch.npu.*,
包括 torch.device('cuda:0') → npu:0、.cuda() → .npu() 等。
命令行操作:
sed -i '1i import torch_npu\nfrom torch_npu.contrib import transfer_to_npu' scripts/main.py
在以下 4 个文件中,CUDA_VISIBLE_DEVICES 设置后追加 ASCEND_RT_VISIBLE_DEVICES:
scripts/train.pyscripts/test.pyscripts/test_single.pyscripts/train_single.pysed -i 's/os.environ\["CUDA_VISIBLE_DEVICES"\] = str(Args.cuda)/os.environ["CUDA_VISIBLE_DEVICES"] = str(Args.cuda)\n\tos.environ["ASCEND_RT_VISIBLE_DEVICES"] = str(Args.cuda)/' \
scripts/train.py scripts/test.py scripts/test_single.py scripts/train_single.py
# 添加 CANN 环境 source
sed -i '1a source /home/Ascend/ascend-toolkit/set_env.sh' \
scripts/RNA-FM.sh scripts/RNA-FM-features.sh
# 替换 python 为 conda 环境完整路径
sed -i 's|^python |/root/anaconda3/envs/oligoformer-conda/bin/python |g' \
scripts/RNA-FM.sh scripts/RNA-FM-features.sh
torch.cuda.is_available() 和 torch.device('cuda:0')torch.cuda.* 调用model.py 中 PositionalEncoding 在 __init__ 中使用模块级 device 变量创建 tensor,
transfer_to_npu 会自动将其映射到 npu:0,无需额外处理setup.py 中依赖版本过旧,需放宽版本约束python 命令需要指向 conda 环境中的 Pythonsource /home/Ascend/ascend-toolkit/set_env.sh
export ASCEND_RT_VISIBLE_DEVICES=0
cd /home/show/OligoFormer
python scripts/main.py --infer 1 -i1 data/example.fa
result/ 目录下生成 RNA*_ranked.txt、RNA*_ranked_filtered.txt 等结果文件# 先生成训练数据的 RNA-FM 特征
bash scripts/RNA-FM-features.sh
# 训练
python scripts/main.py --datasets Hu Mix --cuda 0 --learning_rate 0.0001 \
--batch_size 16 --epoch 200 --early_stopping 30
| 文件 | 修改内容 |
|---|---|
scripts/main.py | 顶部注入 torch_npu + transfer_to_npu |
scripts/train.py | 添加 ASCEND_RT_VISIBLE_DEVICES |
scripts/test.py | 添加 ASCEND_RT_VISIBLE_DEVICES |
scripts/test_single.py | 添加 ASCEND_RT_VISIBLE_DEVICES |
scripts/train_single.py | 添加 ASCEND_RT_VISIBLE_DEVICES |
RNA-FM/redevelop/launch/predict.py | 注入 torch_npu + 添加 ASCEND_RT_VISIBLE_DEVICES |
RNA-FM/setup.py | 放宽依赖版本约束 |
scripts/RNA-FM.sh | source CANN 环境 + 使用 conda Python 路径 |
scripts/RNA-FM-features.sh | source CANN 环境 + 使用 conda Python 路径 |
python scripts/validate_oligoformer_env.py --rna-fm-path /path/to/RNA-FMreferences/rna-fm-integration.mdnpx claudepluginhub ascend-ai-coding/awesome-ascend-skills --plugin npu-torchair-inferMigrates CUDA-based AI models (PyTorch, TensorFlow, vLLM) to Huawei Ascend NPU. Covers environment setup, code analysis, automatic and manual adaptation, distributed training, and verification.
Creates, edits, and optimizes skills for Claude Code, including drafting, evaluating with test prompts, iterating on performance, and improving skill descriptions for better triggering accuracy.