Userb1az commited on
Commit
4a57156
·
verified ·
1 Parent(s): e38e70b

Upload README.md

Browse files
Files changed (1) hide show
  1. README.md +194 -0
README.md ADDED
@@ -0,0 +1,194 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: vllm
3
+ language:
4
+ - code
5
+ license: other
6
+ tags:
7
+ - code
8
+ - mistral-common
9
+ inference: false
10
+ license_name: mnpl
11
+ license_link: https://mistral.ai/licences/MNPL-0.1.md
12
+
13
+ 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>.
14
+ ---
15
+
16
+ # Model Card for Codestral-22B-v0.1
17
+
18
+
19
+ ## Encode and Decode with `mistral_common`
20
+
21
+ ```py
22
+ from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
23
+ from mistral_common.protocol.instruct.messages import UserMessage
24
+ from mistral_common.protocol.instruct.request import ChatCompletionRequest
25
+
26
+ mistral_models_path = "MISTRAL_MODELS_PATH"
27
+
28
+ tokenizer = MistralTokenizer.v3()
29
+
30
+ completion_request = ChatCompletionRequest(messages=[UserMessage(content="Explain Machine Learning to me in a nutshell.")])
31
+
32
+ tokens = tokenizer.encode_chat_completion(completion_request).tokens
33
+ ```
34
+
35
+ ## Inference with `mistral_inference`
36
+
37
+ ```py
38
+ from mistral_inference.transformer import Transformer
39
+ from mistral_inference.generate import generate
40
+
41
+ model = Transformer.from_folder(mistral_models_path)
42
+ out_tokens, _ = generate([tokens], model, max_tokens=64, temperature=0.0, eos_id=tokenizer.instruct_tokenizer.tokenizer.eos_id)
43
+
44
+ result = tokenizer.decode(out_tokens[0])
45
+
46
+ print(result)
47
+ ```
48
+
49
+ ## Inference with hugging face `transformers`
50
+
51
+ ```py
52
+ from transformers import AutoModelForCausalLM
53
+
54
+ model = AutoModelForCausalLM.from_pretrained("mistralai/Codestral-22B-v0.1")
55
+ model.to("cuda")
56
+
57
+ generated_ids = model.generate(tokens, max_new_tokens=1000, do_sample=True)
58
+
59
+ # decode with mistral tokenizer
60
+ result = tokenizer.decode(generated_ids[0].tolist())
61
+ print(result)
62
+ ```
63
+
64
+ > [!TIP]
65
+ > 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!
66
+
67
+ ---
68
+
69
+ Codestral-22B-v0.1 is trained on a diverse dataset of 80+ programming languages, including the most popular ones, such as Python, Java, C, C++, JavaScript, and Bash (more details in the [Blogpost](https://mistral.ai/news/codestral/)). The model can be queried:
70
+ - As instruct, for instance to answer any questions about a code snippet (write documentation, explain, factorize) or to generate code following specific indications
71
+ - As Fill in the Middle (FIM), to predict the middle tokens between a prefix and a suffix (very useful for software development add-ons like in VS Code)
72
+
73
+
74
+ ## Installation
75
+
76
+ It is recommended to use `mistralai/Codestral-22B-v0.1` with [mistral-inference](https://github.com/mistralai/mistral-inference).
77
+
78
+ ```
79
+ pip install mistral_inference
80
+ ```
81
+
82
+ ## Download
83
+
84
+ ```py
85
+ from huggingface_hub import snapshot_download
86
+ from pathlib import Path
87
+
88
+ mistral_models_path = Path.home().joinpath('mistral_models', 'Codestral-22B-v0.1')
89
+ mistral_models_path.mkdir(parents=True, exist_ok=True)
90
+
91
+ snapshot_download(repo_id="mistralai/Codestral-22B-v0.1", allow_patterns=["params.json", "consolidated.safetensors", "tokenizer.model.v3"], local_dir=mistral_models_path)
92
+ ```
93
+
94
+ ### Chat
95
+
96
+ After installing `mistral_inference`, a `mistral-chat` CLI command should be available in your environment.
97
+
98
+ ```
99
+ mistral-chat $HOME/mistral_models/Codestral-22B-v0.1 --instruct --max_tokens 256
100
+ ```
101
+
102
+ Will generate an answer to "Write me a function that computes fibonacci in Rust" and should give something along the following lines:
103
+
104
+ ```
105
+ Sure, here's a simple implementation of a function that computes the Fibonacci sequence in Rust. This function takes an integer `n` as an argument and returns the `n`th Fibonacci number.
106
+
107
+ fn fibonacci(n: u32) -> u32 {
108
+ match n {
109
+ 0 => 0,
110
+ 1 => 1,
111
+ _ => fibonacci(n - 1) + fibonacci(n - 2),
112
+ }
113
+ }
114
+
115
+ fn main() {
116
+ let n = 10;
117
+ println!("The {}th Fibonacci number is: {}", n, fibonacci(n));
118
+ }
119
+
120
+ This function uses recursion to calculate the Fibonacci number. However, it's not the most efficient solution because it performs a lot of redundant calculations. A more efficient solution would use a loop to iteratively calculate the Fibonacci numbers.
121
+ ```
122
+
123
+
124
+ ### Fill-in-the-middle (FIM)
125
+
126
+ After installing `mistral_inference` and running `pip install --upgrade mistral_common` to make sure to have mistral_common>=1.2 installed:
127
+
128
+ ```py
129
+ from mistral_inference.transformer import Transformer
130
+ from mistral_inference.generate import generate
131
+ from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
132
+ from mistral_common.tokens.instruct.request import FIMRequest
133
+
134
+ tokenizer = MistralTokenizer.v3()
135
+ model = Transformer.from_folder("~/codestral-22B-240529")
136
+
137
+ prefix = """def add("""
138
+ suffix = """ return sum"""
139
+
140
+ request = FIMRequest(prompt=prefix, suffix=suffix)
141
+
142
+ tokens = tokenizer.encode_fim(request).tokens
143
+
144
+ out_tokens, _ = generate([tokens], model, max_tokens=256, temperature=0.0, eos_id=tokenizer.instruct_tokenizer.tokenizer.eos_id)
145
+ result = tokenizer.decode(out_tokens[0])
146
+
147
+ middle = result.split(suffix)[0].strip()
148
+ print(middle)
149
+ ```
150
+
151
+ Should give something along the following lines:
152
+
153
+ ```
154
+ num1, num2):
155
+
156
+ # Add two numbers
157
+ sum = num1 + num2
158
+
159
+ # return the sum
160
+ ```
161
+
162
+ ## Usage with transformers library
163
+
164
+ This model is also compatible with `transformers` library, first run `pip install -U transformers` then use the snippet below to quickly get started:
165
+
166
+ ```python
167
+ from transformers import AutoModelForCausalLM, AutoTokenizer
168
+
169
+ model_id = "mistralai/Codestral-22B-v0.1"
170
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
171
+
172
+ model = AutoModelForCausalLM.from_pretrained(model_id)
173
+
174
+ text = "Hello my name is"
175
+ inputs = tokenizer(text, return_tensors="pt")
176
+
177
+ outputs = model.generate(**inputs, max_new_tokens=20)
178
+ print(tokenizer.decode(outputs[0], skip_special_tokens=True))
179
+ ```
180
+
181
+ 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.
182
+
183
+ ## Limitations
184
+
185
+ The Codestral-22B-v0.1 does not have any moderation mechanisms. We're looking forward to engaging with the community on ways to
186
+ make the model finely respect guardrails, allowing for deployment in environments requiring moderated outputs.
187
+
188
+ ## License
189
+
190
+ Codestral-22B-v0.1 is released under the `MNLP-0.1` license.
191
+
192
+ ## The Mistral AI Team
193
+
194
+ Albert Jiang, Alexandre Sablayrolles, Alexis Tacnet, Antoine Roux, Arthur Mensch, Audrey Herblin-Stoop, Baptiste Bout, Baudouin de Monicault, Blanche Savary, Bam4d, Caroline Feldman, Devendra Singh Chaplot, Diego de las Casas, Eleonore Arcelin, Emma Bou Hanna, Etienne Metzger, Gianna Lengyel, Guillaume Bour, Guillaume Lample, Harizo Rajaona, Henri Roussez, Jean-Malo Delignon, Jia Li, Justus Murke, Kartik Khandelwal, Lawrence Stewart, Louis Martin, Louis Ternon, Lucile Saulnier, Lélio Renard Lavaud, Margaret Jennings, Marie Pellat, Marie Torelli, Marie-Anne Lachaux, Marjorie Janiewicz, Mickael Seznec, Nicolas Schuhl, Patrick von Platen, Romain Sauvestre, Pierre Stock, Sandeep Subramanian, Saurabh Garg, Sophia Yang, Szymon Antoniak, Teven Le Scao, Thibaut Lavril, Thibault Schueller, Timothée Lacroix, Théophile Gervet, Thomas Wang, Valera Nemychnikova, Wendy Shang, William El Sayed, William Marshall