本文档记录 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 上完成端到端推理、性能基准与精度评测。
模型关键配置:
| 配置项 | 数值 |
|---|---|
architectures | InternVisionModel |
model_type | intern_vit_6b |
num_hidden_layers | 48 |
hidden_size | 3200 |
num_attention_heads | 25 |
intermediate_size | 12800 |
image_size | 224 |
patch_size | 14 |
torch_dtype | bfloat16 |
相关获取地址:
| 组件 | 版本 |
|---|---|
PyTorch | 2.9.0+cpu |
torch-npu | 2.9.0.post1 |
transformers | 4.57.6 |
timm | 0.9.x |
| CANN | 8.5.1 |
1 逻辑卡(Atlas 910B4,32GB HBM)/opt/atomgit/weights/OpenGVLab/InternViT-6B-224px(本地完整权重)InternViT-6B-224px 核心模块:
| 模块 | 说明 |
|---|---|
InternVisionEmbeddings | Patch Embedding + Position Embedding + CLS Token |
InternVisionEncoder | 48 层 Transformer Encoder |
InternAttention | Multi-Head Self-Attention(含 QK Normalization) |
InternMLP | GELU 激活的 FFN |
| 适配项 | 原始实现 | NPU 适配 | 状态 |
|---|---|---|---|
| 设备迁移 | model.cuda() | model.to("npu") | 通过 |
| 输入张量 | pixel_values.cuda() | pixel_values.npu() | 通过 |
| 精度格式 | BF16 | BF16(910B4 原生支持) | 通过 |
| Flash Attention | 可选 Flash Attention | 未安装,自动 fallback 到原生 Attention | 通过 |
| DropPath (stochastic depth) | torch.linspace(...).item() | 补丁修复为 float(x) 避免 meta tensor 错误 | 已修复 |
nn.Conv2d、nn.Linear、nn.LayerNorm/RMSNorm、F.gelu、F.softmax 等功能正常_naive_attn 路径,NPU 支持良好结论:InternVisionModel 在 torch_npu 上的算子兼容性 已通过。
export HF_ENDPOINT=https://hf-mirror.compython 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 bf16python scripts/inference.py \
--model-path /opt/atomgit/weights/OpenGVLab/InternViT-6B-224px \
--device npu \
--batch-size 8 \
--warmup 3 \
--dtype bf16| 验证项 | 状态 | 说明 |
|---|---|---|
config.json 解析 | 通过 | 成功识别 InternVisionModel 架构 |
| Processor 加载 | 通过 | CLIPImageProcessor 初始化完成 |
| 模型权重加载 | 通过 | 2 个 shard 全部加载成功 |
| NPU 图编译 | 通过 | 首次推理完成图编译 |
| Dummy 前向推理 | 通过 | BF16 输入输出正常,特征维度 (1, 3200) |
详见 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)硬件:1× Atlas 910B4 (32GB HBM),BF16
| batch_size | throughput (img/s) | latency (ms/img) |
|---|---|---|
| 1 | 11.24 | 88.99 |
| 4 | 29.74 | 33.62 |
| 8 | 32.66 | 30.61 |
详细结果:logs/benchmark_result.json
| 项目 | 配置 |
|---|---|
| 评测工具 | 自定义 scripts/eval_accuracy.py |
| 数据集 | 200 条合成数据(ImageNet 本地不可达,使用 synthetic fallback) |
| 推理参数 | batch_size=8, dtype=bf16 |
| 分类头 | 随机初始化的 nn.Linear(3200, 1000)(仅验证端到端 pipeline) |
| 指标 | 数值 | 说明 |
|---|---|---|
| Top-1 Accuracy | 0.00% | 分类头未训练,预期为随机水平 |
| Top-5 Accuracy | 0.50% | 同上 |
| Mean batch time | 897.8 ms | 含首 batch 图编译时间 |
| Median batch time | 244.8 ms | 稳定后单 batch 耗时 |
注意:本次验证使用随机初始化分类头,精度数值仅用于验证推理 pipeline 完整性。真实 ImageNet-1k 精度需在加载预训练分类头后评测。
| 配置 | 最低要求 | 推荐配置 |
|---|---|---|
| NPU 卡数 | 1× 910B4 | 1× 910B4 |
| 显存总量 | 16GB | 32GB |
| 主机内存 | 32GB | 64GB |
6B BF16 权重约占用 12GB HBM,单卡 32GB 充足。
pip install -r requirements.txt关键依赖:torch, torch-npu, transformers, timm, Pillow。
当前环境未安装 Flash Attention,模型自动 fallback 到原生 Attention 实现。若需极致性能,可尝试安装适配 Ascend 的 Flash Attention 变种。
若启动或推理失败,按以下顺序排查:
1. 复现一次确认确定性失败
↓
2. 检查 NPU 状态: npu-smi info
↓
3. 降级 dtype 为 fp32: --dtype fp32
↓
4. 减小 batch_size
↓
5. 应用针对性代码修复,回到 Step 1 重试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| 检查项 | 结果 |
|---|---|
| PyTorch 算子兼容性 | 通过 |
| NPU 模型加载 | 通过 |
| BF16 前向推理 | 通过 |
| 性能基准测试 | 通过 |
| 精度评测 pipeline | 通过(随机分类头) |
综合结论:OpenGVLab/InternViT-6B-224px 在 torch_npu + Ascend 910B4 上的适配工作 已完成,算子兼容性无阻塞性问题。单卡 32GB 环境可流畅完成 6B 视觉模型的端到端推理与评测。