Views
No views yet
| Model Size | FP | 4-bit |
|---|---|---|
| 22.39G | 12.71G |
1pip uninstall transformers
2pip install git+https://github.com/huggingface/transformers@3a1ead0aabed473eafe527915eea8c197d424356
3pip install accelerate
4pip install qwen-omni-utils[decord]1import os
2import json
3import torch
4import torch.nn.functional as F
5import numpy as np
6from PIL import Image
7from typing import Any, Dict, List, Optional, Tuple, Union
8
9from transformers import (
10 Qwen2_5OmniModel,
11 Qwen2_5OmniProcessor,
12 AutoModelForVision2Seq,
13 AutoProcessor,
14 AutoTokenizer
15)
16from transformers.utils.hub import cached_file
17from transformers.generation.utils import GenerateOutput
18
19from gptqmodel import GPTQModel, QuantizeConfig, BACKEND
20from gptqmodel.models.base import BaseGPTQModel
21from gptqmodel.models.auto import MODEL_MAP, SUPPORTED_MODELS
22from gptqmodel.models._const import CPU
23
24from datasets import load_dataset
25from qwen_omni_utils import process_mm_info
26
27class Qwen25OmniThiknerGPTQ(BaseGPTQModel):
28 loader = Qwen2_5OmniModel
29 base_modules = [
30 "thinker.model.embed_tokens",
31 "thinker.model.norm",
32 "token2wav",
33 "thinker.audio_tower",
34 "thinker.model.rotary_emb",
35 "thinker.visual",
36 "talker"
37 ]
38 pre_lm_head_norm_module = "thinker.model.norm"
39 require_monkeypatch = False
40 layers_node = "thinker.model.layers"
41 layer_type = "Qwen2_5OmniDecoderLayer"
42 layer_modules = [
43 ["self_attn.k_proj", "self_attn.v_proj", "self_attn.q_proj"],
44 ["self_attn.o_proj"],
45 ["mlp.up_proj", "mlp.gate_proj"],
46 ["mlp.down_proj"],
47 ]
48
49 def pre_quantize_generate_hook_start(self):
50 self.thinker.visual = move_to(self.thinker.visual, device=self.quantize_config.device)
51 self.thinker.audio_tower = move_to(self.thinker.audio_tower, device=self.quantize_config.device)
52
53 def pre_quantize_generate_hook_end(self):
54 self.thinker.visual = move_to(self.thinker.visual, device=CPU)
55 self.thinker.audio_tower = move_to(self.thinker.audio_tower, device=CPU)
56
57 def preprocess_dataset(self, sample: Dict) -> Dict:
58 return sample
59
60MODEL_MAP["qwen2_5_omni"] = Qwen25OmniThiknerGPTQ
61SUPPORTED_MODELS.append("qwen2_5_omni")
62
63model_path = "/home/chentianqi/model/Qwen/Qwen2.5-Omni-7B-GPTQ-4bit"
64
65from types import MethodType
66
67@classmethod
68def patched_from_config(cls, config, *args, **kwargs):
69 kwargs.pop("trust_remote_code", None)
70
71
72 model = cls._from_config(config, **kwargs)
73 spk_path = cached_file(
74 model_path,
75 "spk_dict.pt",
76 subfolder=kwargs.pop("subfolder", None),
77 cache_dir=kwargs.pop("cache_dir", None),
78 force_download=kwargs.pop("force_download", False),
79 proxies=kwargs.pop("proxies", None),
80 resume_download=kwargs.pop("resume_download", None),
81 local_files_only=kwargs.pop("local_files_only", False),
82 token=kwargs.pop("use_auth_token", None),
83 revision=kwargs.pop("revision", None),
84 )
85 if spk_path is None:
86 raise ValueError(f"Speaker dictionary not found at {spk_path}")
87
88 model.load_speakers(spk_path)
89 return model
90
91Qwen2_5OmniModel.from_config = patched_from_config
92
93# FP Model
94# model = Qwen2_5OmniModel.from_pretrained(
95# model_path,
96# torch_dtype=torch.bfloat16,
97# device_map="auto",
98# attn_implementation="flash_attention_2",
99# )
100
101# GPTQ MODEL
102model = GPTQModel.load(
103 model_path,
104 device_map="cuda",
105 torch_dtype=torch.float16,
106 attn_implementation="flash_attention_2"
107)
1081
2from qwen_omni_utils import process_mm_info
3processor = Qwen2_5OmniProcessor.from_pretrained(model_path)
4# @title inference function
5def inference(video_path, prompt, sys_prompt):
6 messages = [
7 {"role": "system", "content": sys_prompt},
8 {"role": "user", "content": [
9 {"type": "text", "text": prompt},
10 {"type": "video", "video": video_path},
11 ]
12 },
13 ]
14 text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
15 # image_inputs, video_inputs = process_vision_info([messages])
16 audios, images, videos = process_mm_info(messages, use_audio_in_video=False)
17 inputs = processor(text=text, audios=audios, images=images, videos=videos, return_tensors="pt", padding=True)
18 inputs = inputs.to(model.device).to(model.dtype)
19
20 output = model.generate(**inputs, use_audio_in_video=False, return_audio=False)
21
22 text = processor.batch_decode(output, skip_special_tokens=True, clean_up_tokenization_spaces=False)
23 return text
24
25video_path = "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen2.5-Omni/screen.mp4"
26prompt = "Please trranslate the abstract of paper into Chinese."
27
28# display(Video(video_path, width=640, height=360))
29
30## Use a local HuggingFace model to inference.
31response = inference(video_path, prompt=prompt, sys_prompt="You are a helpful assistant.")
32print(response[0])