-
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 13, 2024
1 parent
0a9f5e6
commit 14ef2fc
Showing
10 changed files
with
515 additions
and
209 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
|
||
from huggingface_hub import snapshot_download | ||
from safetensors import safe_open | ||
import numpy as np | ||
import torch.nn as nn | ||
import torch.nn.functional as F | ||
from mistral_inference.transformer import Transformer | ||
from mistral_inference.generate import generate | ||
from mistral_common.tokens.tokenizers.mistral import MistralTokenizer | ||
|
||
from vision_qna import * | ||
|
||
# mistralai/Pixtral-12B-2409 | ||
|
||
class VisionQnA(VisionQnABase): | ||
model_name: str = "pixtral" | ||
format: str = "pixtral" | ||
visual_layers: List[str] = ["vision_encoder", 'vision_language_adapter'] | ||
|
||
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) | ||
|
||
mistral_models_path = snapshot_download(repo_id=model_id, allow_patterns=["params.json", "consolidated.safetensors", "tekken.json"]) | ||
|
||
self.tokenizer = MistralTokenizer.from_file(f"{mistral_models_path}/tekken.json") | ||
self.model = Transformer.from_folder(mistral_models_path, device=self.device, dtype=self.dtype) | ||
|
||
# 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 chat_with_images(self, request: ImageChatRequest) -> str: | ||
prompt = await pixtral_messages(request.messages) | ||
|
||
# tokenize image urls and text | ||
tokenized = self.tokenizer.encode_chat_completion(prompt) | ||
|
||
generation_kwargs = dict( | ||
eos_id = self.tokenizer.instruct_tokenizer.tokenizer.eos_id, | ||
max_tokens = request.max_tokens, | ||
temperature= 0.35 if request.temperature is None else request.temperature, | ||
) | ||
|
||
out_tokens, _ = generate([tokenized.tokens], self.model, images=[tokenized.images], **generation_kwargs) | ||
|
||
return self.tokenizer.decode(out_tokens[0]) |
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.