LlamaFinetuneBase commited on
Commit
5f8c549
1 Parent(s): 18fbdb8

Upload 5 files

Browse files
Files changed (5) hide show
  1. README.md +221 -0
  2. special_tokens_map.json +23 -0
  3. tokenizer.json +0 -0
  4. tokenizer.model +3 -0
  5. tokenizer_config.json +44 -0
README.md ADDED
@@ -0,0 +1,221 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - fr
4
+ - it
5
+ - de
6
+ - es
7
+ - en
8
+ license: apache-2.0
9
+ base_model: mistralai/Mixtral-8x7B-v0.1
10
+ inference:
11
+ parameters:
12
+ temperature: 0.5
13
+ widget:
14
+ - messages:
15
+ - role: user
16
+ content: What is your favorite condiment?
17
+
18
+ extra_gated_description: If you want to learn more about how we process your personal data, please read our <a href="https://mistral.ai/terms/">Privacy Policy</a>.
19
+ ---
20
+ # Model Card for Mixtral-8x7B
21
+
22
+ ### Tokenization with `mistral-common`
23
+
24
+ ```py
25
+ from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
26
+ from mistral_common.protocol.instruct.messages import UserMessage
27
+ from mistral_common.protocol.instruct.request import ChatCompletionRequest
28
+
29
+ mistral_models_path = "MISTRAL_MODELS_PATH"
30
+
31
+ tokenizer = MistralTokenizer.v1()
32
+
33
+ completion_request = ChatCompletionRequest(messages=[UserMessage(content="Explain Machine Learning to me in a nutshell.")])
34
+
35
+ tokens = tokenizer.encode_chat_completion(completion_request).tokens
36
+ ```
37
+
38
+ ## Inference with `mistral_inference`
39
+
40
+ ```py
41
+ from mistral_inference.transformer import Transformer
42
+ from mistral_inference.generate import generate
43
+
44
+ model = Transformer.from_folder(mistral_models_path)
45
+ out_tokens, _ = generate([tokens], model, max_tokens=64, temperature=0.0, eos_id=tokenizer.instruct_tokenizer.tokenizer.eos_id)
46
+
47
+ result = tokenizer.decode(out_tokens[0])
48
+
49
+ print(result)
50
+ ```
51
+
52
+ ## Inference with hugging face `transformers`
53
+
54
+ ```py
55
+ from transformers import AutoModelForCausalLM
56
+
57
+ model = AutoModelForCausalLM.from_pretrained("mistralai/Mixtral-8x7B-Instruct-v0.1")
58
+ model.to("cuda")
59
+
60
+ generated_ids = model.generate(tokens, max_new_tokens=1000, do_sample=True)
61
+
62
+ # decode with mistral tokenizer
63
+ result = tokenizer.decode(generated_ids[0].tolist())
64
+ print(result)
65
+ ```
66
+
67
+ > [!TIP]
68
+ > PRs to correct the transformers tokenizer so that it gives 1-to-1 the same results as the mistral-common reference implementation are very welcome!
69
+
70
+
71
+ ---
72
+ The Mixtral-8x7B Large Language Model (LLM) is a pretrained generative Sparse Mixture of Experts. The Mixtral-8x7B outperforms Llama 2 70B on most benchmarks we tested.
73
+
74
+ For full details of this model please read our [release blog post](https://mistral.ai/news/mixtral-of-experts/).
75
+
76
+ ## Warning
77
+ This repo contains weights that are compatible with [vLLM](https://github.com/vllm-project/vllm) serving of the model as well as Hugging Face [transformers](https://github.com/huggingface/transformers) library. It is based on the original Mixtral [torrent release](magnet:?xt=urn:btih:5546272da9065eddeb6fcd7ffddeef5b75be79a7&dn=mixtral-8x7b-32kseqlen&tr=udp%3A%2F%http://2Fopentracker.i2p.rocks%3A6969%2Fannounce&tr=http%3A%2F%http://2Ftracker.openbittorrent.com%3A80%2Fannounce), but the file format and parameter names are different. Please note that model cannot (yet) be instantiated with HF.
78
+
79
+ ## Instruction format
80
+
81
+ This format must be strictly respected, otherwise the model will generate sub-optimal outputs.
82
+
83
+ The template used to build a prompt for the Instruct model is defined as follows:
84
+ ```
85
+ <s> [INST] Instruction [/INST] Model answer</s> [INST] Follow-up instruction [/INST]
86
+ ```
87
+ Note that `<s>` and `</s>` are special tokens for beginning of string (BOS) and end of string (EOS) while [INST] and [/INST] are regular strings.
88
+
89
+ As reference, here is the pseudo-code used to tokenize instructions during fine-tuning:
90
+ ```python
91
+ def tokenize(text):
92
+ return tok.encode(text, add_special_tokens=False)
93
+
94
+ [BOS_ID] +
95
+ tokenize("[INST]") + tokenize(USER_MESSAGE_1) + tokenize("[/INST]") +
96
+ tokenize(BOT_MESSAGE_1) + [EOS_ID] +
97
+
98
+ tokenize("[INST]") + tokenize(USER_MESSAGE_N) + tokenize("[/INST]") +
99
+ tokenize(BOT_MESSAGE_N) + [EOS_ID]
100
+ ```
101
+
102
+ In the pseudo-code above, note that the `tokenize` method should not add a BOS or EOS token automatically, but should add a prefix space.
103
+
104
+ In the Transformers library, one can use [chat templates](https://huggingface.co/docs/transformers/main/en/chat_templating) which make sure the right format is applied.
105
+
106
+ ## Run the model
107
+
108
+ ```python
109
+ from transformers import AutoModelForCausalLM, AutoTokenizer
110
+
111
+ model_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
112
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
113
+
114
+ model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto")
115
+
116
+ messages = [
117
+ {"role": "user", "content": "What is your favourite condiment?"},
118
+ {"role": "assistant", "content": "Well, I'm quite partial to a good squeeze of fresh lemon juice. It adds just the right amount of zesty flavour to whatever I'm cooking up in the kitchen!"},
119
+ {"role": "user", "content": "Do you have mayonnaise recipes?"}
120
+ ]
121
+
122
+ inputs = tokenizer.apply_chat_template(messages, return_tensors="pt").to("cuda")
123
+
124
+ outputs = model.generate(inputs, max_new_tokens=20)
125
+ print(tokenizer.decode(outputs[0], skip_special_tokens=True))
126
+ ```
127
+
128
+ By default, transformers will load the model in full precision. Therefore you might be interested to further reduce down the memory requirements to run the model through the optimizations we offer in HF ecosystem:
129
+
130
+ ### In half-precision
131
+
132
+ Note `float16` precision only works on GPU devices
133
+
134
+ <details>
135
+ <summary> Click to expand </summary>
136
+
137
+ ```diff
138
+ + import torch
139
+ from transformers import AutoModelForCausalLM, AutoTokenizer
140
+
141
+ model_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
142
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
143
+
144
+ + model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")
145
+
146
+ messages = [
147
+ {"role": "user", "content": "What is your favourite condiment?"},
148
+ {"role": "assistant", "content": "Well, I'm quite partial to a good squeeze of fresh lemon juice. It adds just the right amount of zesty flavour to whatever I'm cooking up in the kitchen!"},
149
+ {"role": "user", "content": "Do you have mayonnaise recipes?"}
150
+ ]
151
+
152
+ input_ids = tokenizer.apply_chat_template(messages, return_tensors="pt").to("cuda")
153
+
154
+ outputs = model.generate(input_ids, max_new_tokens=20)
155
+ print(tokenizer.decode(outputs[0], skip_special_tokens=True))
156
+ ```
157
+ </details>
158
+
159
+ ### Lower precision using (8-bit & 4-bit) using `bitsandbytes`
160
+
161
+ <details>
162
+ <summary> Click to expand </summary>
163
+
164
+ ```diff
165
+ + import torch
166
+ from transformers import AutoModelForCausalLM, AutoTokenizer
167
+
168
+ model_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
169
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
170
+
171
+ + model = AutoModelForCausalLM.from_pretrained(model_id, load_in_4bit=True, device_map="auto")
172
+
173
+ text = "Hello my name is"
174
+ messages = [
175
+ {"role": "user", "content": "What is your favourite condiment?"},
176
+ {"role": "assistant", "content": "Well, I'm quite partial to a good squeeze of fresh lemon juice. It adds just the right amount of zesty flavour to whatever I'm cooking up in the kitchen!"},
177
+ {"role": "user", "content": "Do you have mayonnaise recipes?"}
178
+ ]
179
+
180
+ input_ids = tokenizer.apply_chat_template(messages, return_tensors="pt").to("cuda")
181
+
182
+ outputs = model.generate(input_ids, max_new_tokens=20)
183
+ print(tokenizer.decode(outputs[0], skip_special_tokens=True))
184
+ ```
185
+ </details>
186
+
187
+ ### Load the model with Flash Attention 2
188
+
189
+ <details>
190
+ <summary> Click to expand </summary>
191
+
192
+ ```diff
193
+ + import torch
194
+ from transformers import AutoModelForCausalLM, AutoTokenizer
195
+
196
+ model_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
197
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
198
+
199
+ + model = AutoModelForCausalLM.from_pretrained(model_id, use_flash_attention_2=True, device_map="auto")
200
+
201
+ messages = [
202
+ {"role": "user", "content": "What is your favourite condiment?"},
203
+ {"role": "assistant", "content": "Well, I'm quite partial to a good squeeze of fresh lemon juice. It adds just the right amount of zesty flavour to whatever I'm cooking up in the kitchen!"},
204
+ {"role": "user", "content": "Do you have mayonnaise recipes?"}
205
+ ]
206
+
207
+ input_ids = tokenizer.apply_chat_template(messages, return_tensors="pt").to("cuda")
208
+
209
+ outputs = model.generate(input_ids, max_new_tokens=20)
210
+ print(tokenizer.decode(outputs[0], skip_special_tokens=True))
211
+ ```
212
+ </details>
213
+
214
+ ## Limitations
215
+
216
+ The Mixtral-8x7B Instruct model is a quick demonstration that the base model can be easily fine-tuned to achieve compelling performance.
217
+ It does not have any moderation mechanisms. We're looking forward to engaging with the community on ways to
218
+ make the model finely respect guardrails, allowing for deployment in environments requiring moderated outputs.
219
+
220
+ # The Mistral AI Team
221
+ Albert Jiang, Alexandre Sablayrolles, Arthur Mensch, Blanche Savary, Chris Bamford, Devendra Singh Chaplot, Diego de las Casas, Emma Bou Hanna, Florian Bressand, Gianna Lengyel, Guillaume Bour, Guillaume Lample, Lélio Renard Lavaud, Louis Ternon, Lucile Saulnier, Marie-Anne Lachaux, Pierre Stock, Teven Le Scao, Théophile Gervet, Thibaut Lavril, Thomas Wang, Timothée Lacroix, William El Sayed.
special_tokens_map.json ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": {
3
+ "content": "<s>",
4
+ "lstrip": false,
5
+ "normalized": false,
6
+ "rstrip": false,
7
+ "single_word": false
8
+ },
9
+ "eos_token": {
10
+ "content": "</s>",
11
+ "lstrip": false,
12
+ "normalized": false,
13
+ "rstrip": false,
14
+ "single_word": false
15
+ },
16
+ "unk_token": {
17
+ "content": "<unk>",
18
+ "lstrip": false,
19
+ "normalized": false,
20
+ "rstrip": false,
21
+ "single_word": false
22
+ }
23
+ }
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:dadfd56d766715c61d2ef780a525ab43b8e6da4de6865bda3d95fdef5e134055
3
+ size 493443
tokenizer_config.json ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_bos_token": true,
3
+ "add_eos_token": false,
4
+ "add_prefix_space": null,
5
+ "added_tokens_decoder": {
6
+ "0": {
7
+ "content": "<unk>",
8
+ "lstrip": false,
9
+ "normalized": false,
10
+ "rstrip": false,
11
+ "single_word": false,
12
+ "special": true
13
+ },
14
+ "1": {
15
+ "content": "<s>",
16
+ "lstrip": false,
17
+ "normalized": false,
18
+ "rstrip": false,
19
+ "single_word": false,
20
+ "special": true
21
+ },
22
+ "2": {
23
+ "content": "</s>",
24
+ "lstrip": false,
25
+ "normalized": false,
26
+ "rstrip": false,
27
+ "single_word": false,
28
+ "special": true
29
+ }
30
+ },
31
+ "additional_special_tokens": [],
32
+ "bos_token": "<s>",
33
+ "chat_template": "{%- if messages[0]['role'] == 'system' %}\n {%- set system_message = messages[0]['content'] %}\n {%- set loop_messages = messages[1:] %}\n{%- else %}\n {%- set loop_messages = messages %}\n{%- endif %}\n\n{{- bos_token }}\n{%- for message in loop_messages %}\n {%- if (message['role'] == 'user') != (loop.index0 % 2 == 0) %}\n {{- raise_exception('After the optional system message, conversation roles must alternate user/assistant/user/assistant/...') }}\n {%- endif %}\n {%- if message['role'] == 'user' %}\n {%- if loop.first and system_message is defined %}\n {{- ' [INST] ' + system_message + '\\n\\n' + message['content'] + ' [/INST]' }}\n {%- else %}\n {{- ' [INST] ' + message['content'] + ' [/INST]' }}\n {%- endif %}\n {%- elif message['role'] == 'assistant' %}\n {{- ' ' + message['content'] + eos_token}}\n {%- else %}\n {{- raise_exception('Only user and assistant roles are supported, with the exception of an initial optional system message!') }}\n {%- endif %}\n{%- endfor %}\n",
34
+ "clean_up_tokenization_spaces": false,
35
+ "eos_token": "</s>",
36
+ "legacy": false,
37
+ "model_max_length": 1000000000000000019884624838656,
38
+ "pad_token": null,
39
+ "sp_model_kwargs": {},
40
+ "spaces_between_special_tokens": false,
41
+ "tokenizer_class": "LlamaTokenizer",
42
+ "unk_token": "<unk>",
43
+ "use_default_system_prompt": false
44
+ }