-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
matatonic
committed
Sep 29, 2024
1 parent
6c45b53
commit 5d2252d
Showing
10 changed files
with
175 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
from transformers import AutoTokenizer, AutoModel, AutoImageProcessor, AutoModelForCausalLM | ||
from transformers.generation.configuration_utils import GenerationConfig | ||
|
||
from vision_qna import * | ||
|
||
# WIP | ||
|
||
# BAAI/Emu3-Gen | ||
# BAAI/Emu3-Chat | ||
|
||
VQ_HUB = "BAAI/Emu3-VisionTokenizer" | ||
|
||
class VisionQnA(VisionQnABase): | ||
model_name: str = "emu3" | ||
format: str = "emu3" | ||
visual_layers: List[str] = [] | ||
|
||
def __init__(self, model_id: str, device: str, device_map: str = 'auto', extra_params = {}, format = None): | ||
super().__init__(model_id, device, device_map, extra_params, format) | ||
|
||
|
||
self.processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=self.params.get('trust_remote_code', False)) | ||
self.model = AutoModel.from_pretrained(**self.params).eval() | ||
|
||
# bitsandbytes already moves the model to the device, so we don't need to do it again. | ||
if not (extra_params.get('load_in_4bit', False) or extra_params.get('load_in_8bit', False)): | ||
self.model = self.model.to(self.device) | ||
|
||
self.loaded_banner() | ||
|
||
async def stream_chat_with_images(self, request: ImageChatRequest) -> AsyncGenerator[str, None]: | ||
images, prompt = await prompt_from_messages(request.messages, self.format) | ||
|
||
inputs = self.processor(images=images, text=prompt, return_tensors="pt").to(self.model.device) | ||
|
||
default_params = { | ||
'do_sample': False, | ||
# 'eos_token_id': self.processor.tokenizer.eos_token_id, | ||
# 'pad_token_id': self.processor.tokenizer.eos_token_id, | ||
} | ||
|
||
params = self.get_generation_params(request, default_params=default_params) | ||
|
||
generation_kwargs = dict( | ||
**inputs, | ||
**params, | ||
) | ||
|
||
for new_text in threaded_streaming_generator(generate=self.model.generate, tokenizer=self.processor.tokenizer, generation_kwargs=generation_kwargs): | ||
end = new_text.find(self.processor.tokenizer.eos_token) | ||
if end == -1: | ||
yield new_text | ||
else: | ||
yield new_text[:end] | ||
break | ||
|
||
async def chat_with_images(self, request: ImageChatRequest) -> str: | ||
images, prompt = await prompt_from_messages(request.messages, self.format) | ||
|
||
inputs = self.processor(images=images, text=prompt, return_tensors="pt").to(self.model.device) | ||
|
||
default_params = { | ||
'do_sample': False, | ||
# 'eos_token_id': self.processor.tokenizer.eos_token_id, | ||
# 'pad_token_id': self.processor.tokenizer.eos_token_id, | ||
} | ||
|
||
params = self.get_generation_params(request, default_params=default_params) | ||
|
||
output = self.model.generate(**inputs, **params) | ||
response = self.processor.tokenizer.decode(output[0][inputs.input_ids.size(1):].cpu(), skip_special_tokens=True) | ||
|
||
return response | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.