Views
No views yet
1from transformers import AutoModelForCausalLM, AutoProcessor, GenerationConfig
2from PIL import Image
3import requests
4from auto_round import AutoRoundConfig ##must import for auto-round format if transformers <= 4.51.3
5quantized_model_path = "OPEA/Molmo-7B-D-0924-int4-sym-inc"
6
7# load the processor
8processor = AutoProcessor.from_pretrained(
9 quantized_model_path,
10 trust_remote_code=True,
11 torch_dtype='auto',
12 device_map='auto',
13 use_fast=True
14)
15
16# load the model
17model = AutoModelForCausalLM.from_pretrained(
18 quantized_model_path,
19 trust_remote_code=True,
20 torch_dtype='auto',
21 device_map='auto',
22 ##revision="e64d453" ##AutoGPTQ format
23)
24
25image_url = "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg"
26text = "Describe this image."
27# process the image and text
28inputs = processor.process(
29 images=[Image.open(requests.get(image_url, stream=True).raw)],
30 text=text
31)
32
33# move inputs to the correct device and make a batch of size 1
34inputs = {k: v.to(model.device).unsqueeze(0) for k, v in inputs.items()}
35inputs["images"] = inputs["images"].to(model.dtype)
36
37# generate output; maximum 200 new tokens; stop generation when <|endoftext|> is generated
38output = model.generate_from_batch(
39 inputs,
40 GenerationConfig(max_new_tokens=200, stop_strings="<|endoftext|>"),
41 tokenizer=processor.tokenizer
42)
43
44# only get generated tokens; decode them to text
45generated_tokens = output[0,inputs['input_ids'].size(1):]
46generated_text = processor.tokenizer.decode(generated_tokens, skip_special_tokens=True)
47
48# print the generated text
49print(generated_text)
50##INT4:
51## In this serene beach scene, a woman with long, dark hair sits on the sandy shore, facing left. She is dressed in a plaid shirt with rolled-up sleeves, black pants, and sandals. Her eyes are closed, and she is smiling warmly as she reaches out to high-five a large, light brown dog. The dog, possibly a Labrador or a similar breed, sits on its hind legs with its front paws raised, eagerly engaging in the friendly gesture. The dog is adorned with a blue harness featuring a pattern of pink and blue flowers, and a red leash lies on the sand beside it. The beach is calm, with gentle waves lapping at the shore, and the sky above is a clear, light blue. The sun is setting, casting a soft, warm glow over the scene, enhancing the tranquil and joyful atmosphere.
52
53##FP32:
54## In this serene beach scene, a woman and her dog share a tender moment. The woman, with long dark hair, is seated on the sandy shore, her legs crossed as she faces the ocean. She is wearing a plaid shirt with rolled-up sleeves, black pants, and sandals. Her eyes are closed, and she is smiling warmly at her canine companion. The dog, a light brown Labrador, sits beside her with its front paws raised, eagerly reaching out to touch her hand. The dog is adorned with a blue harness decorated with pink and green flowers, and a red leash lies on the sand nearby. The beach is calm, with gentle waves lapping at the shore, and the sky above is a clear, light blue. The sun is setting, casting a soft, golden glow over the scene, enhancing the peaceful and joyful atmosphere.
55
56
57image_url = "http://images.cocodataset.org/train2017/000000411975.jpg"
58text = "How many people are there on the baseball field in the picture??"
59##INT4:
60## Counting the <points x1="46.5" y1="37.1" x2="58.6" y2="48.3" x3="76.5" y3="33.0" alt="people on the baseball field">people on the baseball field</points> shows a total of 3.
61
62##FP32:
63## Counting the <points x1="46.5" y1="37.6" x2="58.5" y2="49.0" x3="76.0" y3="33.1" alt="people on the baseball field">people on the baseball field</points> shows a total of 3.
64
65
66image_url = "https://intelcorp.scene7.com/is/image/intelcorp/processor-overview-framed-badge:1920-1080?wid=480&hei=270"
67text = "Which company does this image represent?"
68##INT4:
69## The image represents Intel, a well-known technology company. The logo features the text "Intel" in white lowercase letters, followed by "INSIDE" in uppercase letters. This iconic logo design is instantly recognizable and has been a symbol of Intel's brand for many years.
70
71##FP32:
72## The image represents Intel, a well-known technology company. The logo features the text "Intel" in white lowercase letters, with "INSIDE" in uppercase letters below it. This iconic logo design is instantly recognizable and associated with Intel's brand in the computer industry.
73auto-round-mllm --eval --model OPEA/Molmo-7B-D-0924-int4-sym-inc --tasks MMBench_DEV_EN_V11,ScienceQA_VAL,TextVQA_VAL,POPE --output_dir "./eval_result"| Metric | 16bits | INT4 |
|---|---|---|
| avg | 69.22 | 68.90 |
| MMBench_DEV_EN_V11 | 70.89 | 69.20 |
| ScienceQA_VAL | 35.81 | 36.34 |
| TextVQA_VAL | 81.16 | 80.64 |
| POPE | 89.02 | 89.40 |
1pip install auto-round
2auto-round-mllm
3--model allenai/Molmo-7B-D-0924 \
4--device 0 \
5--group_size 128 \
6--bits 4 \
7--iters 1000 \
8--nsamples 512 \
9--seqlen 2048 \
10--format 'auto_gptq,auto_round' \
11--output_dir "./tmp_autoround"