模型简介
模型特点
模型能力
使用案例
🚀 NVLM 1.0
NVLM 1.0是一系列前沿的多模态大语言模型,在视觉语言任务上取得了领先成果,可与GPT - 4o等专有模型以及Llama 3 - V 405B等开源模型相媲美。
🚀 快速开始
今天(2024年9月17日),我们推出了NVLM 1.0,这是一系列前沿级别的多模态大语言模型(LLMs),在视觉语言任务上取得了最先进的成果,可与领先的专有模型(如GPT - 4o)和开放访问模型(如Llama 3 - V 405B和InternVL 2)相媲美。值得注意的是,NVLM 1.0在多模态训练后,其纯文本性能相较于其大语言模型骨干有所提升。
在这个仓库中,我们将NVLM - 1.0 - D - 72B(仅解码器架构)的模型权重和代码开源给社区。
✨ 主要特性
- 该系列模型可执行视觉语言和纯文本任务,包括光学字符识别、多模态推理、定位、常识推理、世界知识利用和编码。
- 模型经过多模态训练后,纯文本性能相较于骨干大语言模型有所提升。
📦 安装指南
准备环境
我们在Dockerfile中提供了一个Docker构建文件,用于复现实验。
Docker镜像基于nvcr.io/nvidia/pytorch:23.09 - py3
。
⚠️ 重要提示
我们观察到不同的Transformer版本、CUDA版本和Docker版本可能会导致基准测试结果出现细微差异。建议使用上述Dockerfile进行精确复现。
💻 使用示例
基础用法
import torch
from transformers import AutoModel
path = "nvidia/NVLM-D-72B"
model = AutoModel.from_pretrained(
path,
torch_dtype=torch.bfloat16,
low_cpu_mem_usage=True,
use_flash_attn=False,
trust_remote_code=True).eval()
高级用法
多GPU加载模型
import torch
import math
from transformers import AutoModel
def split_model():
device_map = {}
world_size = torch.cuda.device_count()
num_layers = 80
# Since the first GPU will be used for ViT, treat it as half a GPU.
num_layers_per_gpu = math.ceil(num_layers / (world_size - 0.5))
num_layers_per_gpu = [num_layers_per_gpu] * world_size
num_layers_per_gpu[0] = math.ceil(num_layers_per_gpu[0] * 0.5)
layer_cnt = 0
for i, num_layer in enumerate(num_layers_per_gpu):
for j in range(num_layer):
device_map[f'language_model.model.layers.{layer_cnt}'] = i
layer_cnt += 1
device_map['vision_model'] = 0
device_map['mlp1'] = 0
device_map['language_model.model.tok_embeddings'] = 0
device_map['language_model.model.embed_tokens'] = 0
device_map['language_model.output'] = 0
device_map['language_model.model.norm'] = 0
device_map['language_model.lm_head'] = 0
device_map['language_model.model.rotary_emb'] = 0
device_map[f'language_model.model.layers.{num_layers - 1}'] = 0
return device_map
path = "nvidia/NVLM-D-72B"
device_map = split_model()
model = AutoModel.from_pretrained(
path,
torch_dtype=torch.bfloat16,
low_cpu_mem_usage=True,
use_flash_attn=False,
trust_remote_code=True,
device_map=device_map).eval()
推理示例
import torch
from transformers import AutoTokenizer, AutoModel
import math
from PIL import Image
import torchvision.transforms as T
from torchvision.transforms.functional import InterpolationMode
def split_model():
device_map = {}
world_size = torch.cuda.device_count()
num_layers = 80
# Since the first GPU will be used for ViT, treat it as half a GPU.
num_layers_per_gpu = math.ceil(num_layers / (world_size - 0.5))
num_layers_per_gpu = [num_layers_per_gpu] * world_size
num_layers_per_gpu[0] = math.ceil(num_layers_per_gpu[0] * 0.5)
layer_cnt = 0
for i, num_layer in enumerate(num_layers_per_gpu):
for j in range(num_layer):
device_map[f'language_model.model.layers.{layer_cnt}'] = i
layer_cnt += 1
device_map['vision_model'] = 0
device_map['mlp1'] = 0
device_map['language_model.model.tok_embeddings'] = 0
device_map['language_model.model.embed_tokens'] = 0
device_map['language_model.output'] = 0
device_map['language_model.model.norm'] = 0
device_map['language_model.lm_head'] = 0
device_map['language_model.model.rotary_emb'] = 0
device_map[f'language_model.model.layers.{num_layers - 1}'] = 0
return device_map
IMAGENET_MEAN = (0.485, 0.456, 0.406)
IMAGENET_STD = (0.229, 0.224, 0.225)
def build_transform(input_size):
MEAN, STD = IMAGENET_MEAN, IMAGENET_STD
transform = T.Compose([
T.Lambda(lambda img: img.convert('RGB') if img.mode != 'RGB' else img),
T.Resize((input_size, input_size), interpolation=InterpolationMode.BICUBIC),
T.ToTensor(),
T.Normalize(mean=MEAN, std=STD)
])
return transform
def find_closest_aspect_ratio(aspect_ratio, target_ratios, width, height, image_size):
best_ratio_diff = float('inf')
best_ratio = (1, 1)
area = width * height
for ratio in target_ratios:
target_aspect_ratio = ratio[0] / ratio[1]
ratio_diff = abs(aspect_ratio - target_aspect_ratio)
if ratio_diff < best_ratio_diff:
best_ratio_diff = ratio_diff
best_ratio = ratio
elif ratio_diff == best_ratio_diff:
if area > 0.5 * image_size * image_size * ratio[0] * ratio[1]:
best_ratio = ratio
return best_ratio
def dynamic_preprocess(image, min_num=1, max_num=12, image_size=448, use_thumbnail=False):
orig_width, orig_height = image.size
aspect_ratio = orig_width / orig_height
# calculate the existing image aspect ratio
target_ratios = set(
(i, j) for n in range(min_num, max_num + 1) for i in range(1, n + 1) for j in range(1, n + 1) if
i * j <= max_num and i * j >= min_num)
target_ratios = sorted(target_ratios, key=lambda x: x[0] * x[1])
# find the closest aspect ratio to the target
target_aspect_ratio = find_closest_aspect_ratio(
aspect_ratio, target_ratios, orig_width, orig_height, image_size)
# calculate the target width and height
target_width = image_size * target_aspect_ratio[0]
target_height = image_size * target_aspect_ratio[1]
blocks = target_aspect_ratio[0] * target_aspect_ratio[1]
# resize the image
resized_img = image.resize((target_width, target_height))
processed_images = []
for i in range(blocks):
box = (
(i % (target_width // image_size)) * image_size,
(i // (target_width // image_size)) * image_size,
((i % (target_width // image_size)) + 1) * image_size,
((i // (target_width // image_size)) + 1) * image_size
)
# split the image
split_img = resized_img.crop(box)
processed_images.append(split_img)
assert len(processed_images) == blocks
if use_thumbnail and len(processed_images) != 1:
thumbnail_img = image.resize((image_size, image_size))
processed_images.append(thumbnail_img)
return processed_images
def load_image(image_file, input_size=448, max_num=12):
image = Image.open(image_file).convert('RGB')
transform = build_transform(input_size=input_size)
images = dynamic_preprocess(image, image_size=input_size, use_thumbnail=True, max_num=max_num)
pixel_values = [transform(image) for image in images]
pixel_values = torch.stack(pixel_values)
return pixel_values
path = "nvidia/NVLM-D-72B"
device_map = split_model()
model = AutoModel.from_pretrained(
path,
torch_dtype=torch.bfloat16,
low_cpu_mem_usage=True,
use_flash_attn=False,
trust_remote_code=True,
device_map=device_map).eval()
print(model)
tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True, use_fast=False)
generation_config = dict(max_new_tokens=1024, do_sample=False)
# pure-text conversation
question = 'Hello, who are you?'
response, history = model.chat(tokenizer, None, question, generation_config, history=None, return_history=True)
print(f'User: {question}\nAssistant: {response}')
# single-image single-round conversation
pixel_values = load_image('path/to/your/example/image.jpg', max_num=6).to(
torch.bfloat16)
question = '<image>\nPlease describe the image shortly.'
response = model.chat(tokenizer, pixel_values, question, generation_config)
print(f'User: {question}\nAssistant: {response}')
基准测试评估
python run_eval.py --config-path eval/full_eval.yaml \
--result-save-path path/to/eval_results/ \
--zero-shot-eval-tasks chartqa coco_caption flickr30k_caption vqav2 mmmu textvqa mathvista mmbench chartqa docvqa realworldqa ocrbench ai2diagram ai2diagram_nomask mmmu_pro docvqa_test
具体来说:
--config-path eval/full_eval.yaml
文件包含评估配置,包括评估提示、评估数据集路径和生成超参数。--result-save-path path/to/eval_results/
指定保存评估结果的路径。--zero-shot-eval-tasks
指定要评估的任务。
📚 详细文档
参考资料
基准测试结果
我们使用旧版Megatron - LM训练模型,并将代码库适配到Huggingface以进行模型托管、复现和推理。
我们观察到Megatron和Huggingface代码库之间存在数值差异,但这些差异在预期的变化范围内。
为了复现和与其他模型进行比较,我们提供了Huggingface代码库和Megatron代码库的结果。
截至2024年9月17日,多模态基准测试的结果如下:
视觉语言基准测试
基准测试 | MMMU (验证集 / 测试集) | MathVista | OCRBench | AI2D | ChartQA | DocVQA | TextVQA | RealWorldQA | VQAv2 |
---|---|---|---|---|---|---|---|---|---|
NVLM - D 1.0 72B (Huggingface) | 58.7 / 54.9 | 65.2 | 852 | 94.2 | 86.0 | 92.6 | 82.6 | 69.5 | 85.4 |
NVLM - D 1.0 72B (Megatron) | 59.7 / 54.6 | 65.2 | 853 | 94.2 | 86.0 | 92.6 | 82.1 | 69.7 | 85.4 |
Llama 3.2 90B | 60.3 / - | 57.3 | - | 92.3 | 85.5 | 90.1 | - | - | 78.1 |
Llama 3 - V 70B | 60.6 / - | - | - | 93.0 | 83.2 | 92.2 | 83.4 | - | 79.1 |
Llama 3 - V 405B | 64.5 / - | - | - | 94.1 | 85.8 | 92.6 | 84.8 | - | 80.2 |
InternVL2 - Llama3 - 76B | 55.2 / - | 65.5 | 839 | 94.8 | 88.4 | 94.1 | 84.4 | 72.2 | - |
GPT - 4V | 56.8 / 55.7 | 49.9 | 645 | 78.2 | 78.5 | 88.4 | 78.0 | 61.4 | 77.2 |
GPT - 4o | 69.1 / - | 63.8 | 736 | 94.2 | 85.7 | 92.8 | - | - | - |
Claude 3.5 Sonnet | 68.3 / - | 67.7 | 788 | 94.7 | 90.8 | 95.2 | - | - | - |
Gemini 1.5 Pro (2024年8月) | 62.2 / - | 63.9 | 754 | 94.4 | 87.2 | 93.1 | 78.7 | 70.4 | 80.2 |
纯文本基准测试
任务 | 骨干大语言模型 | MMLU | GSM8K | MATH | HumanEval | 平均准确率 |
---|---|---|---|---|---|---|
专有模型 | ||||||
GPT - 4.0 | N/A | 88.7 | - | 76.6 | 90.2 | - |
Gemini Pro 1.5 (2024年8月) | N/A | 85.9 | 90.8 | 67.7 | 84.1 | 82.1 |
Claude 3.5 Sonnet | N/A | 88.7 | 96.4 | 71.1 | 92.0 | 87.0 |
开源大语言模型 | ||||||
(a) Nous - Hermes - 2 - Yi - 34B | N/A | 75.5 | 78.6 | 21.8 | 43.3 | 54.8 |
(b) Qwen - 72B - Instruct | N/A | 82.3 | 91.1 | 59.7 | 86.0 | 79.8 |
(c) Llama - 3 - 70B - Instruct | N/A | 82.0 | 93.0 | 51.0 | 81.7 | 76.6 |
(d) Llama - 3.1 - 70B - Instruct | N/A | 83.6 | 95.1 | 68.0 | 80.5 | 81.8 |
(e) Llama - 3.1 - 405B - Instruct | N/A | 87.3 | 96.8 | 73.8 | 89.0 | 86.7 |
开源多模态大语言模型 | ||||||
VILA - 1.5 40B | (a) | 73.3 | 67.5 | 16.8 | 34.1 | 🥶 47.9 (-6.9) |
LLaVA - OneVision 72B | (b) | 80.6 | 89.9 | 49.2 | 74.4 | 🥶 73.5 (-6.3) |
InternVL - 2 - Llama3 - 76B | (c) | 78.5 | 87.1 | 42.5 | 71.3 | 🥶 69.9 (-6.7) |
*Llama 3 - V 70B | (d) | 83.6 | 95.1 | 68.0 | 80.5 | 🙂 81.8 (0) |
*Llama 3 - V 405B | (e) | 87.3 | 96.8 | 73.8 | 89.0 | 🙂 86.7 (0) |
NVLM - D 1.0 72B (Megatron) | (b) | 82.0 | 92.9 | 73.1 | 88.4 | 🥳 84.1 (+4.3) |
NVLM - D 1.0 72B (Huggingface) | (b) | 81.7 | 93.2 | 73.1 | 89.0 | 🥳 84.3 (+4.5) |
模型架构
属性 | 详情 |
---|---|
网络架构 | 仅解码器Transformer |
纯文本大语言模型骨干 | Qwen2 - 72B - Instruct |
视觉编码器 | InternViT - 6B |
鲁棒性
在该数据集上训练的模型无法再生其训练数据:
- 由于模型的输出仅为文本,因此它没有图像生成能力,无法再生训练期间看到的任何图像。
- 模型无法再生训练文本数据:在训练期间,模型将文本和图像作为输入,模型输出(文本)取决于这两个输入。在推理期间,如果没有训练图像作为输入,模型将无法再现训练文本数据的任何部分。
输入
- 输入类型:文本、图像
- 输入格式:字符串、Pillow库支持的格式
- 输入维度:一维(1D)、二维(2D)
- 其他输入相关属性:最大令牌长度 = 128K 令牌
输出
- 输出类型:文本
- 输出格式:字符串
- 模型输出维度:1D
- 其他输出相关属性:无
软件集成
属性 | 详情 |
---|---|
运行时引擎 | PyTorch |
支持的硬件微架构兼容性 | NVIDIA Hopper |
首选/支持的操作系统 | Linux |
推理
属性 | 详情 |
---|---|
引擎 | PyTorch |
测试硬件 | H100 |
模型版本
- v1.0 - D (NVLM - D)
训练、测试和评估数据集
预训练数据集
- 链接:见表4
- 数据收集方法:混合:自动、人工、合成、未知
- 标注方法:混合:自动、人工、合成、未知
- 属性:在图像标题、图像 - 文本对、自然图像、图表、文档、场景描述和数学推理上进行训练。
监督微调数据集
- 链接:见表6
- 数据收集方法:混合:自动、人工、合成、未知
- 标注方法:混合:自动、人工、合成、未知
- 属性:在图像标题、常识知识、图像 - 文本对、自然图像、图表、图表、文档、场景描述、科学图表、课程、教科书数据和问答对、视觉指令调整和数学推理上进行训练。
评估数据集
- 链接:见第6.1节“基准测试”
- 数据收集方法:人工
- 标注方法:人工
- 属性:在常识知识、视觉问答、图表理解、表格、光学字符识别和数学推理上进行评估。
联系方式
Wenliang Dai* (wdai@nvidia.com)、Nayeon Lee* (nayeonl@nvidia.com)、Boxin Wang* (boxinw@nvidia.com)、Zhuolin Yang* (zhuoliny@nvidia.com)、Wei Ping* (wping@nvidia.com)
*同等贡献
引用
@article{nvlm2024,
title={NVLM: Open Frontier-Class Multimodal LLMs},
author={Dai, Wenliang and Lee, Nayeon and Wang, Boxin and Yang, Zhuolin and Liu, Zihan and Barker, Jon and Rintamaki, Tuomas and Shoeybi, Mohammad and Catanzaro, Bryan and Ping, Wei},
journal={arXiv preprint},
year={2024}
}
伦理考量
NVIDIA认为可信AI是一项共同责任,我们已经制定了政策和实践,以支持广泛的AI应用开发。当按照我们的服务条款下载或使用时,开发者应与支持的模型团队合作,确保该模型满足相关行业和用例的要求,并解决不可预见的产品滥用问题。
请在此报告安全漏洞或NVIDIA AI相关问题。
📄 许可证
适用条款:许可协议 - 署名 - 非商业性使用 4.0 国际 - 知识共享。
附加信息:Qwen2 - 72B - Instruct的许可证 以及 InternViT - 6B - 448px - V1 - 2的MIT许可证。








