This Skill provides complete steps for migrating OligoFormer (siRNA efficacy prediction model) from CUDA to Ascend NPU. Project address:https://github.com/lulab/OligoFormer.git
Prerequisites
| Item | Requirement |
|---|---|
| Hardware | Ascend910 series (at least 1 card) |
| OS | openEuler / Ubuntu / KylinOS (aarch64 or x86_64) |
| CANN | ≥ 8.0 (8.2+ or 8.3.RC1 recommended) |
| Python | 3.10 (original project required 3.8, upgraded to 3.10 because torch 2.5.1 does not support 3.8) |
| PyTorch | 2.5.1 |
| torch_npu | 2.5.1 |
| conda | Installed |
Migration Workflow
1. Environment initialization and CANN configuration
# 设置 CANN 环境(路径根据实际安装位置调整)
source /home/Ascend/ascend-toolkit/set_env.sh
# 验证 NPU 可用
npu-smi info
# 设置华为镜像源
export PIP_INDEX_URL=https://repo.huaweicloud.com/repository/pypi/simple/
2. Clone repository
cd /home/show
git clone https://github.com/lulab/OligoFormer.git
cd OligoFormer
3. Create conda environment and install dependencies
# 创建 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/
4. Install RNA-FM dependencies
OligoFormer depends on RNA-FM to generate RNA embedding features.
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
5. Verify torch_npu availability
source /home/Ascend/ascend-toolkit/set_env.sh
python -c "import torch; import torch_npu; a = torch.randn(3,4).npu(); print(a + a)"
Should output a Tensor on device='npu:0' .
6. Code adaptation
6.1 Inject transfer_to_npu into the entry file
At the very top of scripts/main.py (before all imports), add:
import torch_npu
from torch_npu.contrib import transfer_to_npu
transfer_to_npu will automatically map torch.cuda.* to torch.npu.*,
including torch.device('cuda:0') → npu:0、.cuda() → .npu() , etc.
Command-line operation:
sed -i '1i import torch_npu\nfrom torch_npu.contrib import transfer_to_npu' scripts/main.py
6.2 Add ASCEND_RT_VISIBLE_DEVICES environment variable
In the following 4 files, afterCUDA_VISIBLE_DEVICES is set, append ASCEND_RT_VISIBLE_DEVICES:
scripts/train.pyscripts/test.pyscripts/test_single.pyscripts/train_single.py
sed -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
7. Adaptation Notes
7.1 Migration characteristics
- Pure PyTorch project: no .cu files, no CUDA-specific third-party libraries such as flash_attn/xformers
- No distributed: the project does not use DataParallel or DDP
- No AMP: the project does not use mixed precision training
- Simple CUDA usage: only used at device selection with
torch.cuda.is_available()andtorch.device('cuda:0') - transfer_to_npu can cover all CUDA APIs: no need to manually modify
torch.cuda.*calls
7.2 Notes
model.pyInPositionalEncodingin__init__, a module-leveldevicevariable is used to create a tensor,transfer_to_npuwill automatically map it tonpu:0, no additional handling required- Ascend910 does not support fp64 (double); torch_npu will automatically downgrade to fp32, warnings may appear but results are unaffected
- RNA-FM's
setup.pycontains dependency versions that are too old; version constraints need to be relaxed - The
pythoncommand in the RNA-FM Shell script needs to point to the Python in the conda environment
8. Validation
8.1 Inference validation
source /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
8.2 Validation pass criteria
- Program exits normally with no NPU-related errors
result/GenerateRNA*_ranked.txt、RNA*_ranked_filtered.txtand other result files in the directory- Output contains siRNA efficacy prediction values (efficacy column is not empty)
8.3 Training validation (optional, requires generating RNA-FM features)
# 先生成训练数据的 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
9. List of modified files
| File | Modification |
|---|---|
scripts/main.py |
Inject at the top torch_npu + transfer_to_npu |
scripts/train.py |
Add ASCEND_RT_VISIBLE_DEVICES |
scripts/test.py |
Add ASCEND_RT_VISIBLE_DEVICES |
scripts/test_single.py |
Add ASCEND_RT_VISIBLE_DEVICES |
scripts/train_single.py |
Add ASCEND_RT_VISIBLE_DEVICES |
RNA-FM/redevelop/launch/predict.py |
Inject torch_npu + Add ASCEND_RT_VISIBLE_DEVICES |
RNA-FM/setup.py |
Relax dependency version constraints |
scripts/RNA-FM.sh |
source CANN environment + use conda Python path |
scripts/RNA-FM-features.sh |
source CANN environment + use conda Python path |
Companion scripts
- OligoFormer and RNA-FM precheck:
python scripts/validate_oligoformer_env.py --rna-fm-path /path/to/RNA-FM
References
- OligoFormer and RNA-FM integration notes:
references/rna-fm-integration.md