THUDM / cogvlm-chat-hf

huggingface.co
Total runs: 6.5K
24-hour runs: 0
7-day runs: 0
30-day runs: 0
Model's Last Updated: December 19 2023
text-generation

Introduction of cogvlm-chat-hf

Model Details of cogvlm-chat-hf

CogVLM

CogVLM 是一个强大的开源视觉语言模型(VLM)。CogVLM-17B 拥有 100 亿视觉参数和 70 亿语言参数,在 10 个经典跨模态基准测试上取得了 SOTA 性能,包括 NoCaps、Flicker30k captioning、RefCOCO、RefCOCO+、RefCOCOg、Visual7W、GQA、ScienceQA、VizWiz VQA 和 TDIUC,而在 VQAv2、OKVQA、TextVQA、COCO captioning 等方面则排名第二,超越或与 PaLI-X 55B 持平。您可以通过线上 demo 体验 CogVLM 多模态对话。

CogVLM is a powerful open-source visual language model ( VLM ). CogVLM-17B has 10 billion vision parameters and 7 billion language parameters. CogVLM-17B achieves state-of-the-art performance on 10 classic cross-modal benchmarks, including NoCaps, Flicker30k captioning, RefCOCO, RefCOCO+, RefCOCOg, Visual7W, GQA, ScienceQA, VizWiz VQA and TDIUC, and rank the 2nd on VQAv2, OKVQA, TextVQA, COCO captioning, etc., surpassing or matching PaLI-X 55B . CogVLM can also chat with you about images.

img

以上权重对学术研究完全开放,在填写 问卷 进行登记后亦允许免费商业使用。

快速开始(Qiuckstart)

硬件需求(hardware requirement)

需要近 40GB GPU 显存用于模型推理。如果没有一整块GPU显存超过40GB,则需要使用accelerate的将模型切分到多个有较小显存的GPU设备上。

40GB VRAM for inference. If there is no single GPU with more than 40GB of VRAM, you will need to use the "accelerate" library to dispatch the model into multiple GPUs with smaller VRAM.

安装依赖(dependencies)

pip install torch==2.1.0 transformers==4.35.0 accelerate==0.24.1 sentencepiece==0.1.99 einops==0.7.0 xformers==0.0.22.post7 triton==2.1.0

代码示例(example)

import torch
import requests
from PIL import Image
from transformers import AutoModelForCausalLM, LlamaTokenizer

tokenizer = LlamaTokenizer.from_pretrained('lmsys/vicuna-7b-v1.5')
model = AutoModelForCausalLM.from_pretrained(
    'THUDM/cogvlm-chat-hf',
    torch_dtype=torch.bfloat16,
    low_cpu_mem_usage=True,
    trust_remote_code=True
).to('cuda').eval()


# chat example
query = 'Describe this image'
image = Image.open(requests.get('https://github.com/THUDM/CogVLM/blob/main/examples/1.png?raw=true', stream=True).raw).convert('RGB')
inputs = model.build_conversation_input_ids(tokenizer, query=query, history=[], images=[image])  # chat mode
inputs = {
    'input_ids': inputs['input_ids'].unsqueeze(0).to('cuda'),
    'token_type_ids': inputs['token_type_ids'].unsqueeze(0).to('cuda'),
    'attention_mask': inputs['attention_mask'].unsqueeze(0).to('cuda'),
    'images': [[inputs['images'][0].to('cuda').to(torch.bfloat16)]],
}
gen_kwargs = {"max_length": 2048, "do_sample": False}

with torch.no_grad():
    outputs = model.generate(**inputs, **gen_kwargs)
    outputs = outputs[:, inputs['input_ids'].shape[1]:]
    print(tokenizer.decode(outputs[0]))

# This image captures a moment from a basketball game. Two players are prominently featured: one wearing a yellow jersey with the number
# 24 and the word 'Lakers' written on it, and the other wearing a navy blue jersey with the word 'Washington' and the number 34. The player
# in yellow is holding a basketball and appears to be dribbling it, while the player in navy blue is reaching out with his arm, possibly
# trying to block or defend. The background shows a filled stadium with spectators, indicating that this is a professional game.</s>



# vqa example
query = 'How many houses are there in this cartoon?'
image = Image.open(requests.get('https://github.com/THUDM/CogVLM/blob/main/examples/3.jpg?raw=true', stream=True).raw).convert('RGB')
inputs = model.build_conversation_input_ids(tokenizer, query=query, history=[], images=[image], template_version='vqa')   # vqa mode
inputs = {
    'input_ids': inputs['input_ids'].unsqueeze(0).to('cuda'),
    'token_type_ids': inputs['token_type_ids'].unsqueeze(0).to('cuda'),
    'attention_mask': inputs['attention_mask'].unsqueeze(0).to('cuda'),
    'images': [[inputs['images'][0].to('cuda').to(torch.bfloat16)]],
}
gen_kwargs = {"max_length": 2048, "do_sample": False}

with torch.no_grad():
    outputs = model.generate(**inputs, **gen_kwargs)
    outputs = outputs[:, inputs['input_ids'].shape[1]:]
    print(tokenizer.decode(outputs[0]))

# 4</s>

当单卡显存不足时,可以将模型切分到多个小显存GPU上。以下是个当你有两张24GB的GPU,16GBCPU内存的例子。 你可以将 infer_auto_device_map 的参数改成你的配置。注意这里将GPU显存少写了一点,这是为推理时中间状态预留出一部分显存。

dispatch the model into multiple GPUs with smaller VRAM. This is an example for you have two 24GB GPU and 16GB CPU memory. you can change the arguments of infer_auto_device_map with your own setting.

import torch
import requests
from PIL import Image
from transformers import AutoModelForCausalLM, LlamaTokenizer
from accelerate import init_empty_weights, infer_auto_device_map, load_checkpoint_and_dispatch

tokenizer = LlamaTokenizer.from_pretrained('lmsys/vicuna-7b-v1.5')
with init_empty_weights():
    model = AutoModelForCausalLM.from_pretrained(
        'THUDM/cogvlm-chat-hf',
        torch_dtype=torch.bfloat16,
        low_cpu_mem_usage=True,
        trust_remote_code=True,
    )
device_map = infer_auto_device_map(model, max_memory={0:'20GiB',1:'20GiB','cpu':'16GiB'}, no_split_module_classes=['CogVLMDecoderLayer', 'TransformerLayer'])
model = load_checkpoint_and_dispatch(
    model,
    'local/path/to/hf/version/chat/model',   # typical, '~/.cache/huggingface/hub/models--THUDM--cogvlm-chat-hf/snapshots/balabala'
    device_map=device_map,
)
model = model.eval()

# check device for weights if u want to
for n, p in model.named_parameters():
    print(f"{n}: {p.device}")

# chat example
query = 'Describe this image'
image = Image.open(requests.get('https://github.com/THUDM/CogVLM/blob/main/examples/1.png?raw=true', stream=True).raw).convert('RGB')
inputs = model.build_conversation_input_ids(tokenizer, query=query, history=[], images=[image])  # chat mode
inputs = {
    'input_ids': inputs['input_ids'].unsqueeze(0).to('cuda'),
    'token_type_ids': inputs['token_type_ids'].unsqueeze(0).to('cuda'),
    'attention_mask': inputs['attention_mask'].unsqueeze(0).to('cuda'),
    'images': [[inputs['images'][0].to('cuda').to(torch.bfloat16)]],
}
gen_kwargs = {"max_length": 2048, "do_sample": False}

with torch.no_grad():
    outputs = model.generate(**inputs, **gen_kwargs)
    outputs = outputs[:, inputs['input_ids'].shape[1]:]
    print(tokenizer.decode(outputs[0]))

方法(Method)

CogVLM 模型包括四个基本组件:视觉变换器(ViT)编码器、MLP适配器、预训练的大型语言模型(GPT)和一个 视觉专家模块 。更多细节请参见 Paper

CogVLM model comprises four fundamental components: a vision transformer (ViT) encoder, an MLP adapter, a pretrained large language model (GPT), and a visual expert module . See Paper for more details.

许可(License)

此存储库中的代码是根据 Apache-2.0 许可 开放源码,而使用 CogVLM 模型权重必须遵循 模型许可

The code in this repository is open source under the Apache-2.0 license , while the use of the CogVLM model weights must comply with the Model License .

引用(Citation)

If you find our work helpful, please consider citing the following papers

@article{wang2023cogvlm,
      title={CogVLM: Visual Expert for Pretrained Language Models}, 
      author={Weihan Wang and Qingsong Lv and Wenmeng Yu and Wenyi Hong and Ji Qi and Yan Wang and Junhui Ji and Zhuoyi Yang and Lei Zhao and Xixuan Song and Jiazheng Xu and Bin Xu and Juanzi Li and Yuxiao Dong and Ming Ding and Jie Tang},
      year={2023},
      eprint={2311.03079},
      archivePrefix={arXiv},
      primaryClass={cs.CV}
}

Runs of THUDM cogvlm-chat-hf on huggingface.co

6.5K
Total runs
0
24-hour runs
0
3-day runs
0
7-day runs
0
30-day runs

More Information About cogvlm-chat-hf huggingface.co Model

More cogvlm-chat-hf license Visit here:

https://choosealicense.com/licenses/apache-2.0

cogvlm-chat-hf huggingface.co

cogvlm-chat-hf huggingface.co is an AI model on huggingface.co that provides cogvlm-chat-hf's model effect (), which can be used instantly with this THUDM cogvlm-chat-hf model. huggingface.co supports a free trial of the cogvlm-chat-hf model, and also provides paid use of the cogvlm-chat-hf. Support call cogvlm-chat-hf model through api, including Node.js, Python, http.

cogvlm-chat-hf huggingface.co Url

https://huggingface.co/THUDM/cogvlm-chat-hf

THUDM cogvlm-chat-hf online free

cogvlm-chat-hf huggingface.co is an online trial and call api platform, which integrates cogvlm-chat-hf's modeling effects, including api services, and provides a free online trial of cogvlm-chat-hf, you can try cogvlm-chat-hf online for free by clicking the link below.

THUDM cogvlm-chat-hf online free url in huggingface.co:

https://huggingface.co/THUDM/cogvlm-chat-hf

cogvlm-chat-hf install

cogvlm-chat-hf is an open source model from GitHub that offers a free installation service, and any user can find cogvlm-chat-hf on GitHub to install. At the same time, huggingface.co provides the effect of cogvlm-chat-hf install, users can directly use cogvlm-chat-hf installed effect in huggingface.co for debugging and trial. It also supports api for free installation.

cogvlm-chat-hf install url in huggingface.co:

https://huggingface.co/THUDM/cogvlm-chat-hf

Url of cogvlm-chat-hf

cogvlm-chat-hf huggingface.co Url

Provider of cogvlm-chat-hf huggingface.co

THUDM
ORGANIZATIONS

Other API from THUDM

huggingface.co

Total runs: 659.2K
Run Growth: 0
Growth Rate: 0.00%
Updated:December 05 2024
huggingface.co

Total runs: 611.5K
Run Growth: 0
Growth Rate: 0.00%
Updated:August 04 2024
huggingface.co

Total runs: 243.1K
Run Growth: 0
Growth Rate: 0.00%
Updated:March 11 2025
huggingface.co

Total runs: 95.0K
Run Growth: 0
Growth Rate: 0.00%
Updated:March 13 2025
huggingface.co

Total runs: 69.6K
Run Growth: 0
Growth Rate: 0.00%
Updated:November 23 2024
huggingface.co

Total runs: 28.0K
Run Growth: 0
Growth Rate: 0.00%
Updated:March 03 2025
huggingface.co

Total runs: 23.6K
Run Growth: 0
Growth Rate: 0.00%
Updated:November 23 2024
huggingface.co

Total runs: 15.8K
Run Growth: 0
Growth Rate: 0.00%
Updated:April 14 2025
huggingface.co

Total runs: 6.0K
Run Growth: 0
Growth Rate: 0.00%
Updated:October 25 2024
huggingface.co

Total runs: 4.8K
Run Growth: 0
Growth Rate: 0.00%
Updated:April 22 2025
huggingface.co

Total runs: 3.3K
Run Growth: 0
Growth Rate: 0.00%
Updated:January 02 2025
huggingface.co

Total runs: 2.8K
Run Growth: 0
Growth Rate: 0.00%
Updated:August 04 2024
huggingface.co

Total runs: 993
Run Growth: 0
Growth Rate: 0.00%
Updated:October 20 2023
huggingface.co

Total runs: 928
Run Growth: 0
Growth Rate: 0.00%
Updated:October 20 2023
huggingface.co

Total runs: 853
Run Growth: 0
Growth Rate: 0.00%
Updated:January 27 2025
huggingface.co

Total runs: 607
Run Growth: 0
Growth Rate: 0.00%
Updated:January 02 2025
huggingface.co

Total runs: 299
Run Growth: 0
Growth Rate: 0.00%
Updated:March 02 2023
huggingface.co

Total runs: 188
Run Growth: 0
Growth Rate: 0.00%
Updated:August 04 2024
huggingface.co

Total runs: 167
Run Growth: 0
Growth Rate: 0.00%
Updated:October 20 2023
huggingface.co

Total runs: 119
Run Growth: 0
Growth Rate: 0.00%
Updated:December 10 2024
huggingface.co

Total runs: 75
Run Growth: 0
Growth Rate: 0.00%
Updated:November 20 2023
huggingface.co

Total runs: 62
Run Growth: 0
Growth Rate: 0.00%
Updated:July 09 2025
huggingface.co

Total runs: 43
Run Growth: 0
Growth Rate: 0.00%
Updated:July 09 2025
huggingface.co

Total runs: 43
Run Growth: 0
Growth Rate: 0.00%
Updated:July 24 2023
huggingface.co

Total runs: 28
Run Growth: 0
Growth Rate: 0.00%
Updated:July 09 2025
huggingface.co

Total runs: 25
Run Growth: 0
Growth Rate: 0.00%
Updated:March 02 2023
huggingface.co

Total runs: 14
Run Growth: 0
Growth Rate: 0.00%
Updated:July 24 2023
huggingface.co

Total runs: 12
Run Growth: 0
Growth Rate: 0.00%
Updated:July 22 2024