Views
No views yet
1import torch
2from PIL import Image
3from auto_round import AutoRoundConfig ##must import for auto-round format
4from transformers import AutoModelForCausalLM, AutoTokenizer
5import requests
6
7MODEL_PATH = "OPEA/cogvlm2-llama3-chat-19B-qvision-int4-sym-inc"
8DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
9
10tokenizer = AutoTokenizer.from_pretrained(
11 MODEL_PATH,
12 trust_remote_code=True
13)
14model = AutoModelForCausalLM.from_pretrained(
15 MODEL_PATH,
16 torch_dtype="auto",
17 trust_remote_code=True,
18 device_map=DEVICE,
19).to(DEVICE).eval()
20
21text_only_template = "A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions. USER: {} ASSISTANT:"
22
23image_url = "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg"
24content = "Describe this image."
25
26# Preparation for inference
27query = text_only_template.format(content)
28image = Image.open(requests.get(image_url, stream=True).raw)
29input_by_model = model.build_conversation_input_ids(
30 tokenizer,
31 query=query,
32 images=[image],
33 template_version='chat'
34)
35inputs = {
36 'input_ids': input_by_model['input_ids'].unsqueeze(0).to(DEVICE),
37 'token_type_ids': input_by_model['token_type_ids'].unsqueeze(0).to(DEVICE),
38 'attention_mask': input_by_model['attention_mask'].unsqueeze(0).to(DEVICE),
39 'images': [[input_by_model['images'][0].to(DEVICE).to(model.dtype)]] if image is not None else None,
40}
41gen_kwargs = {
42 "max_new_tokens": 2048,
43 "pad_token_id": 128002,
44 "do_sample": False,
45}
46
47with torch.no_grad():
48 outputs = model.generate(**inputs, **gen_kwargs)
49 outputs = outputs[:, inputs['input_ids'].shape[1]:]
50 response = tokenizer.decode(outputs[0])
51 response = response.split("<|end_of_text|>")[0]
52 print(response)
53
54
55##INT4:
56## The image captures a serene moment at a beach during what appears to be sunset or sunrise. The sun casts a warm, golden hue over the scene. In the foreground, a woman sits on the sandy shore, facing a large, golden-colored dog. The dog, wearing a colorful harness, places one paw on the woman's hand, suggesting a bond or a playful gesture. The woman seems to be smiling, indicating a moment of joy or connection with the dog. The ocean waves gently crash in the background, and the horizon is visible, suggesting the vastness of the sea. The overall mood of the image is peaceful and heartwarming.
57
58##BF16:
59## The image showcases a serene beach setting during what appears to be either sunrise or sunset. In the foreground, a woman sits on the sandy beach, dressed in casual attire, including a checkered shirt and jeans. She is engaged in a moment of connection with a golden retriever dog, which is seated beside her. The dog wears a colorful harness and is looking up at the woman, possibly in anticipation of a treat or a playful gesture. The vast expanse of the ocean can be seen in the background, with gentle waves crashing onto the shore. The sky is clear, and the warm hues of the setting or rising sun cast a soft glow over the scene, creating a tranquil and heartwarming atmosphere.
60
61image_url = "http://images.cocodataset.org/train2017/000000411975.jpg"
62content = "图片中的棒球场上有多少人?"
63##INT4:
64## In the image provided, there are four individuals visible on the baseball field.
65
66##BF16:
67## In the image provided, there are five people visible on the baseball field.
68
69image_url = "https://intelcorp.scene7.com/is/image/intelcorp/processor-overview-framed-badge:1920-1080?wid=480&hei=270"
70content = "这张图片代表哪家公司?"
71##INT4:
72## The image represents Intel, a well-known multinational corporation that specializes in computer chips and other technologies.
73
74##BF16:
75## The image represents the company Intel.auto-round-mllm --lmms --model OPEA/cogvlm2-llama3-chat-19B-qvision-int4-sym-inc --tasks pope,textvqa_val,scienceqa,mmbench_en --output_dir "./eval_result" --device cuda:0 | Metric | 16bits | Llava Calib INT4 |
|---|---|---|
| avg | 80.38 | 80.21 |
| MMBench_DEV_EN_V11 | 75.86 | 75.77 |
| TextVQA_VAL | 77.77 | 77.15 |
| POPE | 87.37 | 87.70 |
1pip install auto-round
2auto-round-mllm \
3--model THUDM/cogvlm2-llama3-chat-19B \
4--device 0 \
5--group_size 128 \
6--bits 4 \
7--iters 1000 \
8--nsample 512 \
9--seqlen 512 \
10--quant_nontext_module \
11--format 'auto_round' \
12--output_dir "./tmp_autoround"