File size: 7,393 Bytes
e6cd8d0
 
c35430f
e6cd8d0
 
 
 
c35430f
e6cd8d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c35430f
e6cd8d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c35430f
e6cd8d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
---
base_model:
- mistralai/Magistral-Small-2509
---

## Model Details

This model is an int4 model with group_size 128 and symmetric quantization of [mistralai/Magistral-Small-2509](https://huggingface.co/mistralai/Magistral-Small-2509) generated by [intel/auto-round](https://github.com/intel/auto-round) algorithm.  

Please follow the license of the original model.

## How To Use


### INT4 Inference
```python
import os
import torch
from datetime import datetime, timedelta
from transformers import  Mistral3ForConditionalGeneration
from mistral_common.protocol.instruct.request import ChatCompletionRequest
from mistral_common.tokens.tokenizers.mistral import MistralTokenizer


def load_system_prompt(repo_id_or_path: str, filename: str) -> str:
    from huggingface_hub import hf_hub_download

    if os.path.isdir(repo_id_or_path):
        file_path = os.path.join(repo_id_or_path, filename)
    else:
        file_path = hf_hub_download(repo_id=repo_id_or_path, filename=filename)
    with open(file_path, "r") as file:
        system_prompt = file.read()
    today = datetime.today().strftime("%Y-%m-%d")
    yesterday = (datetime.today() - timedelta(days=1)).strftime("%Y-%m-%d")
    model_name = repo_id_or_path.split("/")[-1]
    return system_prompt.format(name=model_name, today=today, yesterday=yesterday)

model_id = "Intel/Magistral-Small-2509-int4-AutoRound"
model = Mistral3ForConditionalGeneration.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map="auto")
if os.path.isdir(model_id):
    tokenizer = MistralTokenizer.from_file(os.path.join(model_id, "tekken.json"))
else:
    tokenizer = MistralTokenizer.from_hf_hub(model_id)

SYSTEM_PROMPT = load_system_prompt(model.name_or_path, "SYSTEM_PROMPT.txt")

content = "Which of the depicted countries has the best food? Which the second and third and fourth? Name the country, its color on the map and one its city that is visible on the map, but is not the capital. Make absolutely sure to only name a city that can be seen on the map."
image_url = "https://huggingface.co/datasets/patrickvonplaten/random_img/resolve/main/europe.png"
conversation = [{"role": "system", "content": SYSTEM_PROMPT}]
conversation.append({
    "role": "user",
    "content": [{
        "type": "text",
        "text": content
    }, {
        "type": "image_url",
        "image_url": {
            "url": image_url
        }
    }],
},)
tokenized = tokenizer.encode_chat_completion(ChatCompletionRequest(messages=conversation, continue_final_message=False))
breakpoint()
input_ids = torch.tensor([tokenized.tokens])
attention_mask = torch.ones_like(input_ids)
pixel_values = torch.tensor(tokenized.images[0], dtype=torch.bfloat16).unsqueeze(0)
image_sizes = torch.tensor([pixel_values.shape[-2:]])
ret = {
    "input_ids": input_ids,
    "attention_mask": attention_mask,
    "pixel_values": pixel_values,
    "image_sizes": image_sizes,
}
for key in ret:
    if isinstance(ret[key], torch.Tensor):
        ret[key] = ret[key].to(model.device)
with torch.inference_mode():
    generation = model.generate(**ret, max_new_tokens=512, do_sample=True)

decoded = tokenizer.decode(generation)
print(decoded)


"""
Alright, the question is about identifying the best, second best, third best, and fourth best countries in terms of food from the depicted map. The user also wants the country's color on the map and a non-capital city visible on the map for each.

First, let's list out the countries visible on the map and their colors:

1. Iceland - light purple
2. Ireland - dark green
3. United Kingdom - yellow
4. France - brown
5. Spain - yellow
6. Portugal - yellow
7. Italy - orange
8. Greece - light blue
9. Germany - orange
10. Switzerland - light green
11. Austria - light green
12. Netherlands - orange
13. Belgium - light blue
14. Luxembourg - light blue
15. Denmark - light blue
16. Poland - light green
17. Czech Republic - purple
18. Slovakia - purple
19. Hungary - purple
20. Romania - light green
21. Bulgaria - dark brown
22. Russia - red
23. Finland - light green
24. Sweden - dark purple
25. Norway - dark green
26. Denmark - light blue
27. Estonia - light blue
28. Latvia - light blue
29. Lithuania - light blue
30. Belarus - dark purple
31. Ukraine - yellow
32. Moldova - yellow
33. Turkey - light blue (partially visible)

Now, let's consider the countries known for their food. This is subjective, but generally, Italy, France, Spain, and Greece are often praised for their cuisine.

1. Italy (orange) - Known for pasta, pizza, risotto, etc.
2. France (brown) - Known for wine, cheese, pastries, etc.
3. Spain (yellow) - Known for paella, tapas, jamón, etc.
4. Greece (light blue) - Known for gyros, souvlaki, feta, etc.

Now, we need to pick one non-capital city for each that is visible on the map.

1. Italy: Milan (not the capital, Rome is the capital)
2. France: Lyon (not the capital, Paris is the capital)
3. Spain: Barcelona (not the capital, Madrid is the capital)
4. Greece: Thessaloniki (not the capital, Athens is the capital)

Now, let's format the response:

1. First: Italy, color: orange, city: Milan
2. Second: France, color: brown, city: Lyon
3. Third: Spain, color: yellow, city: Barcelona
4. Fourth: Greece, color: light blue, city: Thessaloniki

But wait, the user asked for the best, second best, third best, and fourth best. We need to rank them. Let's assume the following ranking based on general popularity and global influence of cuisine:

1. Italy - often considered the best
2. France - often considered the second best
3. Spain - often considered the third best
4. Greece - often considered the fourth best

Now, let's prepare the response.1. **Italy** - Color: orange, City: Milan
2. **France** - Color: brown, City: Lyon
3. **Spain** - Color: yellow, City: Barcelona
4. **Greece** - Color: light blue, City: Thessaloniki

"""
```

### Generate the model

https://github.com/intel/auto-round/pull/713 is required.

```bash
auto_round --model /models/Magistral-Small-2509/ --iters 200 --seqlen 1024 --output_dir tmp_autoround
```


## Ethical Considerations and Limitations

The model can produce factually incorrect output, and should not be relied on to produce factually accurate information. Because of the limitations of the pretrained model and the finetuning datasets, it is possible that this model could generate lewd, biased or otherwise offensive outputs.

Therefore, before deploying any applications of the model, developers should perform safety testing.

## Caveats and Recommendations

Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model.

Here are a couple of useful links to learn more about Intel's AI software:

- Intel Neural Compressor [link](https://github.com/intel/neural-compressor)

## Disclaimer

The license on this model does not constitute legal advice. We are not responsible for the actions of third parties who use this model. Please consult an attorney before using this model for commercial purposes.

## Cite

@article{cheng2023optimize, title={Optimize weight rounding via signed gradient descent for the quantization of llms}, author={Cheng, Wenhua and Zhang, Weiwei and Shen, Haihao and Cai, Yiyang and He, Xin and Lv, Kaokao and Liu, Yi}, journal={arXiv preprint arXiv:2309.05516}, year={2023} }

[arxiv](https://arxiv.org/abs/2309.05516) [github](https://github.com/intel/auto-round)