Add transformers snippet
#36
by merve HF Staff - opened
README.md
CHANGED
|
@@ -428,4 +428,32 @@ print(outputs[0].outputs[0].text)
|
|
| 428 |
|
| 429 |
Transformers-compatible model weights are also uploaded (thanks a lot @cyrilvallez).
|
| 430 |
However the transformers implementation was **not throughly tested**, but only on "vibe-checks".
|
| 431 |
-
Hence, we can only ensure 100% correct behavior when using the original weight format with vllm (see above).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 428 |
|
| 429 |
Transformers-compatible model weights are also uploaded (thanks a lot @cyrilvallez).
|
| 430 |
However the transformers implementation was **not throughly tested**, but only on "vibe-checks".
|
| 431 |
+
Hence, we can only ensure 100% correct behavior when using the original weight format with vllm (see above).
|
| 432 |
+
|
| 433 |
+
You can use Mistral-Small-3.1-24B-Instruct-2503 with transformers as follows.
|
| 434 |
+
|
| 435 |
+
```python
|
| 436 |
+
from transformers import AutoProcessor, AutoModelForImageTextToText
|
| 437 |
+
import torch
|
| 438 |
+
|
| 439 |
+
device = "cuda"
|
| 440 |
+
|
| 441 |
+
model_checkpoint = "mistralai/Mistral-Small-3.1-24B-Instruct-2503"
|
| 442 |
+
processor = AutoProcessor.from_pretrained(model_checkpoint)
|
| 443 |
+
model = AutoModelForImageTextToText.from_pretrained(model_checkpoint, device_map=torch_device, torch_dtype=torch.bfloat16)
|
| 444 |
+
|
| 445 |
+
messages = [
|
| 446 |
+
... {
|
| 447 |
+
... "role": "user",
|
| 448 |
+
... "content": [
|
| 449 |
+
... {"type": "image", "url": "http://images.cocodataset.org/val2017/000000039769.jpg"},
|
| 450 |
+
... {"type": "text", "text": "Describe this image"},
|
| 451 |
+
... ],
|
| 452 |
+
... }
|
| 453 |
+
... ]
|
| 454 |
+
|
| 455 |
+
inputs = processor.apply_chat_template(messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt").to(model.device, dtype=torch.bfloat16)
|
| 456 |
+
|
| 457 |
+
generate_ids = model.generate(**inputs, max_new_tokens=20)
|
| 458 |
+
decoded_output = processor.decode(generate_ids[0, inputs["input_ids"].shape[1] :], skip_special_tokens=True)
|
| 459 |
+
```
|