该系列模型可执行视觉-语言及纯文本任务,包括光学字符识别、多模态推理、定位、常识推理、世界知识运用和代码生成。
本模型已准备就绪,可供非商业用途使用。
管辖条款:协议文本 - 知识共享署名-非商业性使用4.0国际许可协议。
补充信息:Qwen2-72B-Instruct 遵循 LICENSE · Qwen/Qwen2-72B-Instruct at main,InternViT-6B-448px-V1-2 遵循 MIT 许可协议 – 开放源代码促进会。
今日(2024年9月17日),我们正式推出 NVLM 1.0,这是一系列前沿级多模态大型语言模型(LLMs)。该系列模型在视觉-语言任务上取得了最先进的成果,可与领先的专有模型(如 GPT-4o)及开源模型(如 Llama 3-V 405B 和 InternVL 2)相媲美。值得注意的是,经过多模态训练后,NVLM 1.0 在纯文本性能上相较于其基础语言模型有显著提升。
在本代码库中,我们开源了 NVLM-1.0-D-72B(纯解码器架构),即面向社区开放纯解码器模型的权重与代码。
我们使用 legacy 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
纯文本 LLM 基础模型: Qwen2-72B-Instruct
视觉编码器: InternViT-6B
在该数据集上训练的模型无法再生其训练数据:
该模型不具备图像生成能力,因其输出仅为文本。因此,它无法再生训练期间可能见过的任何图像。
该模型无法再生训练文本数据:在训练过程中,模型将文本和图像作为输入,且模型输出(文本)同时以这两种输入为条件。在推理过程中,若没有训练图像作为输入,模型将无法重现训练文本数据的任何部分。
输入类型: 文本、图像
输入格式: 字符串、Pillow 库支持的格式
输入维度: 一维(1D)、二维(2D)
与输入相关的其他属性: 最大令牌长度 = 128K 令牌
输出类型: 文本
输出格式: 字符串
模型输出: 1D
与输出相关的其他属性: 无
将 Megatron checkpoint 转换为 Huggingface 格式时,我们适配了 InternVL 代码库,以支持在 HF 中进行模型加载和多 GPU 推理。
在将分词器适配到 Huggingface 时,我们还使用了来自 Qwen2.5-72B-Instruct 的分词器,因为它包含用于视觉任务的额外特殊令牌,例如 <|vision_pad|>。
我们基于 Qwen2-72B-Instruct 纯文本模型和 InternViT-6B-448px-V1-5 ViT 模型,并使用我们的大规模高质量多模态数据集训练了 NVLM-1.0-D-72B。
关于训练代码,请参考 Megatron-Core。
我们在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}')要在基准数据集上测试我们的 NVLM-1.0 模型,您可以使用以下代码:
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 指定要评估的任务。运行时引擎
支持的硬件微架构兼容性:
[首选/支持的] 操作系统:
引擎: PyTorch
测试硬件:
链接
按数据集的数据收集方法
按数据集的标注方法
特性
链接
按数据集的数据收集方法
按数据集的标注方法
特性
链接
按数据集的数据收集方法
按数据集的标注方法
特性
戴文亮*(wdai@nvidia.com)、李奈妍*(nayeonl@nvidia.com)、王博欣*(boxinw@nvidia.com)、杨卓林*(zhuoliny@nvidia.com)、平伟*(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 相关问题。