HuggingFace 模型 Addaoud/DeepPlant-EAP
(植物组学 EAP 任务,bioinformatics)在华为昇腾 NPU 上的适配工程(torch_npu)。
该模型通过
PyTorchModelHubMixin发布,不是 transformers 模型。HF 仓库内没有模型代码, 仅有config.json、model.safetensors、README.md。因此本工程从model.safetensors的 143 个 tensor 反推出网络结构,并用load_state_dict(strict=True)严格加载全部权重。
attention_pool.to_attn_logits [2048, 2048] 原始 Parameter(注意力池化 logits 矩阵)
backbone.conv_features.0.conv.weight/bias Conv1d(4 -> 512, k=11)
backbone.conv_features.1.* BatchNorm1d(512)
backbone.conv_network.0.{layer,project}.* Conv 残差块 512 -> 2048
backbone.conv_network.2.{layer,project}.* Conv 残差块 2048 -> 2048
transformer.pos_encoder.pe [1, 100, 2048] 学习式位置编码
transformer.encoder.encoder_layers.{0..5}.* 6 x TransformerEncoderLayer(d_model=2048, nhead=8, ff=2048)
fc.linear.0 Linear(2048 -> 4096)
fc.linear.3 Linear(4096 -> 4096)
fc.prediction_head.0 Linear(4096 -> 2) <- 输出(n_features=2)前向流程:
(batch, 4, L)(L ≤ 100,4 通道 x 序列长度,如 one-hot 基因组窗口)(batch, 2048, L)(batch, L, 2048),加位置编码 peto_attn_logits 计算每位置权重,softmax 后加权求和)→ (batch, 2048)(batch, 2) EAP 预测向量(多输出回归,2 个目标)模型输入为 4 通道 x 100 长度的特征图((batch, 4, 100),flatten 后即 400 个浮点数)。
gene_features_sample.txt 内置 400 个标准正态随机浮点数,对应 (4, 100),可直接用于
--input。
注意:
n_features = 2是输出维度(预测 2 个目标),不是输入特征数。
python3 /opt/atomgit/adapt/_shared/download_weights.py Addaoud/DeepPlant-EAP
# 缓存到 ./model_weights/Addaoud/DeepPlant-EAP/(已存在则跳过)NPU 环境(本机 Ascend910):
source /usr/local/Ascend/ascend-toolkit/set_env.sh
export ASCEND_RT_VISIBLE_DEVICES=0 # 指定 NPU 卡(本机 2 卡)
export ASCEND_GLOBAL_LOG_LEVEL=3
# 1) CLI 单输入推理
python inference.py --input gene_features_sample.txt
# 2) FastAPI 服务(默认 0.0.0.0:8000)
python inference.py --serve --port 8000FastAPI:
GET / → HTML 页面(上传 .txt 特征文件,或粘贴 400 个浮点数)POST /predict → 接受上传文件 / form 字段 input / JSON {"values":[...]},返回 JSON:
{model, device, input_shape, prediction, prediction_scalar, prediction_std}inference.py 中 get_device() 优先 npu:0,失败回退 cpu,try/except 捕获。grep -rin cuda 为空)。nn.TransformerEncoderLayer 会派发到融合算子 _transformer_encoder_layer_fwd,
NPU 后端不支持并回退 CPU;本工程用等价的手工 post-norm 编码层(EncoderLayer,拆成
MHA + Linear + LayerNorm 等基础算子)替代,全部在 NPU 上执行。safetensors.torch.load_file + load_state_dict(strict=True)(0 missing / 0 unexpected)。