2301_80416780/InternViT-6B-224px-ascend
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

InternViT-6B-224px on Ascend NPU

1. 简介

本文档记录 OpenGVLab/InternViT-6B-224px 在华为昇腾 NPU 环境的适配验证结果。

InternViT-6B-224px 是基于 InternVisionModel 架构的纯视觉 Transformer,总参数量约 6B,采用 224×224 图像输入、14×14 Patch Embedding。本验证将其 backbone 与轻量分类头(nn.Linear(hidden_size, 1000))组合,在 Atlas 910B4 NPU 上完成端到端推理、性能基准与精度评测。

模型关键配置:

配置项数值
architecturesInternVisionModel
model_typeintern_vit_6b
num_hidden_layers48
hidden_size3200
num_attention_heads25
intermediate_size12800
image_size224
patch_size14
torch_dtypebfloat16

相关获取地址:

  • 权重下载地址(HuggingFace Mirror):https://hf-mirror.com/OpenGVLab/InternViT-6B-224px
  • 原始仓库(GitCode):https://gitcode.com/hf_mirrors/OpenGVLab/InternViT-6B-224px

2. 验证环境

组件版本
PyTorch2.9.0+cpu
torch-npu2.9.0.post1
transformers4.57.6
timm0.9.x
CANN8.5.1
  • NPU:1 逻辑卡(Atlas 910B4,32GB HBM)
  • 模型路径:/opt/atomgit/weights/OpenGVLab/InternViT-6B-224px(本地完整权重)

3. 架构与算子兼容性分析

3.1 模型结构

InternViT-6B-224px 核心模块:

模块说明
InternVisionEmbeddingsPatch Embedding + Position Embedding + CLS Token
InternVisionEncoder48 层 Transformer Encoder
InternAttentionMulti-Head Self-Attention(含 QK Normalization)
InternMLPGELU 激活的 FFN

3.2 NPU 适配要点

适配项原始实现NPU 适配状态
设备迁移model.cuda()model.to("npu")通过
输入张量pixel_values.cuda()pixel_values.npu()通过
精度格式BF16BF16(910B4 原生支持)通过
Flash Attention可选 Flash Attention未安装,自动 fallback 到原生 Attention通过
DropPath (stochastic depth)torch.linspace(...).item()补丁修复为 float(x) 避免 meta tensor 错误已修复

3.3 算子兼容性结论

  • Torch 原生算子:nn.Conv2d、nn.Linear、nn.LayerNorm/RMSNorm、F.gelu、F.softmax 等功能正常
  • Attention 算子:未安装 Flash Attention 时自动使用 _naive_attn 路径,NPU 支持良好
  • CUDA 裸算子:无 CUDA 裸算子依赖,不存在早期退出风险

结论:InternVisionModel 在 torch_npu 上的算子兼容性 已通过。

4. 推理启动

4.1 环境准备

export HF_ENDPOINT=https://hf-mirror.com

4.2 单图推理

python scripts/inference.py \
  --model-path /opt/atomgit/weights/OpenGVLab/InternViT-6B-224px \
  --image-path /path/to/your/image.jpg \
  --device npu \
  --batch-size 1 \
  --dtype bf16

4.3 Dummy 批量推理(冒烟测试)

python scripts/inference.py \
  --model-path /opt/atomgit/weights/OpenGVLab/InternViT-6B-224px \
  --device npu \
  --batch-size 8 \
  --warmup 3 \
  --dtype bf16

5. Smoke 验证

5.1 验证结果

验证项状态说明
config.json 解析通过成功识别 InternVisionModel 架构
Processor 加载通过CLIPImageProcessor 初始化完成
模型权重加载通过2 个 shard 全部加载成功
NPU 图编译通过首次推理完成图编译
Dummy 前向推理通过BF16 输入输出正常,特征维度 (1, 3200)

5.2 运行日志

详见 logs/smoke_test.log。关键摘要:

[Config] device=npu, batch_size=1, dtype=bf16
[Load] hidden_size=3200, num_classes=1000
[Warmup] running 1 iterations...
[Inference] running dummy batch inference...
[Result] elapsed=0.083s, throughput=12.03 img/s
[Features] shape=(1, 3200)

6. 性能参考

硬件:1× Atlas 910B4 (32GB HBM),BF16

batch_sizethroughput (img/s)latency (ms/img)
111.2488.99
429.7433.62
832.6630.61

详细结果:logs/benchmark_result.json

7. 精度评测

7.1 评测配置

项目配置
评测工具自定义 scripts/eval_accuracy.py
数据集200 条合成数据(ImageNet 本地不可达,使用 synthetic fallback)
推理参数batch_size=8, dtype=bf16
分类头随机初始化的 nn.Linear(3200, 1000)(仅验证端到端 pipeline)

7.2 评测结果

指标数值说明
Top-1 Accuracy0.00%分类头未训练,预期为随机水平
Top-5 Accuracy0.50%同上
Mean batch time897.8 ms含首 batch 图编译时间
Median batch time244.8 ms稳定后单 batch 耗时

注意:本次验证使用随机初始化分类头,精度数值仅用于验证推理 pipeline 完整性。真实 ImageNet-1k 精度需在加载预训练分类头后评测。

8. 注意事项

8.1 硬件资源要求

配置最低要求推荐配置
NPU 卡数1× 910B41× 910B4
显存总量16GB32GB
主机内存32GB64GB

6B BF16 权重约占用 12GB HBM,单卡 32GB 充足。

8.2 依赖安装

pip install -r requirements.txt

关键依赖:torch, torch-npu, transformers, timm, Pillow。

8.3 Flash Attention

当前环境未安装 Flash Attention,模型自动 fallback 到原生 Attention 实现。若需极致性能,可尝试安装适配 Ascend 的 Flash Attention 变种。

8.4 Fallback 阶梯

若启动或推理失败,按以下顺序排查:

1. 复现一次确认确定性失败
        ↓
2. 检查 NPU 状态: npu-smi info
        ↓
3. 降级 dtype 为 fp32: --dtype fp32
        ↓
4. 减小 batch_size
        ↓
5. 应用针对性代码修复,回到 Step 1 重试

9. 项目结构

internvit-6b-224px-ascend/
├── README.md
├── requirements.txt
├── .gitattributes
├── scripts/
│   ├── inference.py
│   ├── benchmark.py
│   └── eval_accuracy.py
└── logs/
    ├── smoke_test.log
    ├── benchmark_result.json
    ├── benchmark_run.log
    ├── eval_result.json
    └── eval_run.log

10. 总结

检查项结果
PyTorch 算子兼容性通过
NPU 模型加载通过
BF16 前向推理通过
性能基准测试通过
精度评测 pipeline通过(随机分类头)

综合结论:OpenGVLab/InternViT-6B-224px 在 torch_npu + Ascend 910B4 上的适配工作 已完成,算子兼容性无阻塞性问题。单卡 32GB 环境可流畅完成 6B 视觉模型的端到端推理与评测。