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-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 ##revision="80a3f3d" ##AutoGPTQ format
20).to(DEVICE).eval()
21
22image_url = "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg"
23content = "Describe this image."
24
25text_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:"
26query = text_only_template.format(content)
27
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}
45
46with torch.no_grad():
47 outputs = model.generate(**inputs, **gen_kwargs)
48 outputs = outputs[:, inputs['input_ids'].shape[1]:]
49 response = tokenizer.decode(outputs[0])
50 response = response.split("<|end_of_text|>")[0]
51 print(response)
52##INT4:
53## The image depicts a serene beach scene during what appears to be the golden hour, just before sunset. A woman is seated on the sandy shore, facing the vast expanse of the ocean. The waves are gently crashing on the beach, and the sky is painted with hues of orange and blue. The woman seems to be in a relaxed posture, possibly enjoying the tranquility of the moment. Beside her is a large, golden-colored dog, possibly a Labrador, wearing a colorful harness. The dog is sitting upright, looking at the woman, and they seem to share a bond of trust and affection. The overall mood of the image is peaceful, reflective, and heartwarming.
54
55##BF16:
56## 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.
57
58image_url = "http://images.cocodataset.org/train2017/000000411975.jpg"
59content = "图片中的棒球场上有多少人?"
60##INT4:
61## In the image provided, there are four individuals on the baseball field. There are two children in the foreground, presumably engaged in some playful activity or perhaps participating in a game. One child is bending over, possibly picking something up off the ground, while the other stands beside them. Additionally, there is an adult standing further back, holding a microphone, suggesting they might be an announcer or commentator for the game. Lastly, there is another adult, seen from the back, sitting in the stands. So, in total, there are four people on the baseball field.
62
63##BF16:
64## In the image provided, there are five people visible on the baseball field.
65
66image_url = "https://intelcorp.scene7.com/is/image/intelcorp/processor-overview-framed-badge:1920-1080?wid=480&hei=270"
67content = "这张图片代表哪家公司?"
68##INT4:
69## The image represents the company Intel. The logo in the image is the Intel Inside logo, which is commonly used by Intel to signify the presence of their processors in various electronic devices.
70
71##BF16:
72## The image represents the company Intel.auto-round-mllm --lmms --model OPEA/cogvlm2-llama3-chat-19B-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 | |
| MMBench_DEV_EN_V11 | 75.86 | 74.57 |
| TextVQA_VAL | 77.77 | 78.91 |
| POPE | 87.37 | 87.64 |
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 2048 \
10--format 'auto_gptq,auto_round' \
11--output_dir "./tmp_autoround"