Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chatglm4v support #11327

Merged
merged 3 commits into from
Jun 17, 2024
Merged

chatglm4v support #11327

merged 3 commits into from
Jun 17, 2024

Conversation

qiuxin2012
Copy link
Contributor

Description

glm-4v-9b support

@qiuxin2012
Copy link
Contributor Author

Example code:

from PIL import Image
from ipex_llm.transformers import AutoModelForCausalLM
from transformers import AutoTokenizer
import torch

device = "xpu"

tokenizer = AutoTokenizer.from_pretrained("/mnt/disk1/models/glm-4v-9b", trust_remote_code=True)

query = '描述这张图片'
image = Image.open("./a.jpg").convert('RGB')
inputs = tokenizer.apply_chat_template([{"role": "user", "image": image, "content": query}],
                                       add_generation_prompt=True, tokenize=True, return_tensors="pt",
                                       return_dict=True)  # chat mode
inputs = inputs.to(device)
model = AutoModelForCausalLM.from_pretrained(
    "/mnt/disk1/models/glm-4v-9b",
    cpu_embedding=True,
    load_in_4bit=True,
    low_cpu_mem_usage=True,
    trust_remote_code=True
).half().to(device).eval()

gen_kwargs = {"max_length": 2500, "do_sample": True, "top_k": 1}
with torch.no_grad():
    outputs = model.generate(**inputs, **gen_kwargs)
    outputs = outputs[:, inputs['input_ids'].shape[1]:]
    print(tokenizer.decode(outputs[0]))

Output:

图片展示了一个日落时分的湖泊景色。太阳正处于地平线附近,将天空染成了橙红色。湖面平静如镜,反射出太阳的光芒和周围的景色。远处可以看到轮廓分明的山脉,山脚下似乎有一些建筑或房屋。整个场景显得宁静而美丽。 <|endoftext|>

Copy link
Contributor

@lalalapotter lalalapotter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@qiuxin2012 qiuxin2012 merged commit 183e0c6 into intel-analytics:main Jun 17, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants