Mistral AI/Mixture of Experts

Mixtral-8x7B

chatcoding
46.7B
Parameters (13B active)
32K
Context length
11
Benchmarks
4
Quantizations
780K
HF downloads
Architecture
MoE
Released
2024-01-11
Layers
32
KV Heads
8
Head Dim
128
Family
mistral

Model Card for Mixtral-8x7B

Tokenization with mistral-common

from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
from mistral_common.protocol.instruct.messages import UserMessage
from mistral_common.protocol.instruct.request import ChatCompletionRequest
 
mistral_models_path = "MISTRAL_MODELS_PATH"
 
tokenizer = MistralTokenizer.v1()
 
completion_request = ChatCompletionRequest(messages=[UserMessage(content="Explain Machine Learning to me in a nutshell.")])
 
tokens = tokenizer.encode_chat_completion(completion_request).tokens

Inference with mistral_inference

from mistral_inference.transformer import Transformer
from mistral_inference.generate import generate

model = Transformer.from_folder(mistral_models_path)
out_tokens, _ = generate([tokens], model, max_tokens=64, temperature=0.0, eos_id=tokenizer.instruct_tokenizer.tokenizer.eos_id)

result = tokenizer.decode(out_tokens[0])

print(result)

Inference with hugging face transformers

from transformers import AutoModelForCausalLM
 
model = AutoModelForCausalLM.from_pretrained("mistralai/Mixtral-8x7B-Instruct-v0.1")
model.to("cuda")
 
generated_ids = model.generate(tokens, max_new_tokens=1000, do_sample=True)

# decode with mistral tokenizer
result = tokenizer.decode(generated_ids[0].tolist())
print(result)

[!TIP] 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!


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.

For full details of this model please read our release blog post.

Warning

This repo contains weights that are compatible with vLLM serving of the model as well as Hugging Face transformers library. It is based on the original Mixtral torrent release, but the file format and parameter names are different. Please note that model cannot (yet) be instantiated with HF.

Instruction format

This format must be strictly respected, otherwise the model will generate sub-optimal outputs.

The template used to build a prompt for the Instruct model is defined as follows:

<s> [INST] Instruction [/INST] Model answer</s> [INST] Follow-up instruction [/INST]

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.

As reference, here is the pseudo-code used to tokenize instructions during fine-tuning:

def tokenize(text):
    return tok.encode(text, add_special_tokens=False)

[BOS_ID] + 
tokenize("[INST]") + tokenize(USER_MESSAGE_1) + tokenize("[/INST]") +
tokenize(BOT_MESSAGE_1) + [EOS_ID] +
…
tokenize("[INST]") + tokenize(USER_MESSAGE_N) + tokenize("[/INST]") +
tokenize(BOT_MESSAGE_N) + [EOS_ID]

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.

In the Transformers library, one can use chat templates which make sure the right format is applied.

Run the model

from transformers import AutoModelForCausalLM, AutoTokenizer

model_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
tokenizer = AutoTokenizer.from_pretrained(model_id)

model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto")

messages = [
    {"role": "user", "content": "What is your favourite condiment?"},
    {"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!"},
    {"role": "user", "content": "Do you have mayonnaise recipes?"}
]

inputs = tokenizer.apply_chat_template(messages, return_tensors="pt").to("cuda")

outputs = model.generate(inputs, max_new_tokens=20)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))

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:

In half-precision

Note float16 precision only works on GPU devices

<details> <summary> Click to expand </summary>
+ import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

model_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
tokenizer = AutoTokenizer.from_pretrained(model_id)

+ model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")

messages = [
    {"role": "user", "content": "What is your favourite condiment?"},
    {"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!"},
    {"role": "user", "content": "Do you have mayonnaise recipes?"}
]

input_ids = tokenizer.apply_chat_template(messages, return_tensors="pt").to("cuda")

outputs = model.generate(input_ids, max_new_tokens=20)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
</details>

Lower precision using (8-bit & 4-bit) using bitsandbytes

<details> <summary> Click to expand </summary>
+ import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

model_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
tokenizer = AutoTokenizer.from_pretrained(model_id)

+ model = AutoModelForCausalLM.from_pretrained(model_id, load_in_4bit=True, device_map="auto")

text = "Hello my name is"
messages = [
    {"role": "user", "content": "What is your favourite condiment?"},
    {"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!"},
    {"role": "user", "content": "Do you have mayonnaise recipes?"}
]

input_ids = tokenizer.apply_chat_template(messages, return_tensors="pt").to("cuda")

outputs = model.generate(input_ids, max_new_tokens=20)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
</details>

Load the model with Flash Attention 2

<details> <summary> Click to expand </summary>
+ import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

model_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
tokenizer = AutoTokenizer.from_pretrained(model_id)

+ model = AutoModelForCausalLM.from_pretrained(model_id, use_flash_attention_2=True, device_map="auto")

messages = [
    {"role": "user", "content": "What is your favourite condiment?"},
    {"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!"},
    {"role": "user", "content": "Do you have mayonnaise recipes?"}
]

input_ids = tokenizer.apply_chat_template(messages, return_tensors="pt").to("cuda")

outputs = model.generate(input_ids, max_new_tokens=20)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
</details>

Limitations

The Mixtral-8x7B Instruct model is a quick demonstration that the base model can be easily fine-tuned to achieve compelling performance. It does not have any moderation mechanisms. We're looking forward to engaging with the community on ways to make the model finely respect guardrails, allowing for deployment in environments requiring moderated outputs.

The Mistral AI Team

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.

Quantizations & VRAM

Q4_K_M4.5 bpw
26.8 GB
VRAM required
94%
Quality
Q6_K6.5 bpw
38.4 GB
VRAM required
97%
Quality
Q8_08 bpw
47.2 GB
VRAM required
100%
Quality
FP1616 bpw
93.9 GB
VRAM required
100%
Quality

Benchmarks (11)

IFEval59.0
BBH37.1
MATH-50029.9
MMLU-PRO29.6
GPQA Diamond29.2
MUSR16.7
MATH12.2
GPQA9.5
AA Intelligence7.7
LiveCodeBench6.6
HLE4.5

Run with Ollama

$ollama run mixtral:8x7b

GPUs that can run this model

At Q4_K_M quantization. Sorted by minimum VRAM.

AMD Radeon PRO V710
28 GB VRAM • 504 GB/s
AMD
NVIDIA RTX 5090
32 GB VRAM • 1792 GB/s
NVIDIA
$1999
Apple M1 Max (32GB)
32 GB VRAM • 400 GB/s
APPLE
$1499
Apple M2 Max (32GB)
32 GB VRAM • 400 GB/s
APPLE
$1799
NVIDIA V100 SXM2 32GB
32 GB VRAM • 900 GB/s
NVIDIA
$3500
Apple M2 Pro (32GB)
32 GB VRAM • 200 GB/s
APPLE
$1499
NVIDIA Tesla V100 DGXS 32 GB
32 GB VRAM • 897 GB/s
NVIDIA
NVIDIA Tesla V100 PCIe 32 GB
32 GB VRAM • 897 GB/s
NVIDIA
NVIDIA Tesla V100 SXM2 32 GB
32 GB VRAM • 898 GB/s
NVIDIA
NVIDIA Tesla V100 SXM3 32 GB
32 GB VRAM • 981 GB/s
NVIDIA
AMD Radeon Instinct MI60
32 GB VRAM • 1020 GB/s
AMD
NVIDIA Tesla V100S PCIe 32 GB
32 GB VRAM • 1130 GB/s
NVIDIA
AMD Radeon Instinct MI100
32 GB VRAM • 1230 GB/s
AMD
NVIDIA RTX 5000 Ada Generation
32 GB VRAM • 576 GB/s
NVIDIA
NVIDIA GeForce RTX 5090
32 GB VRAM • 1790 GB/s
NVIDIA
$1999
NVIDIA GeForce RTX 5090 D
32 GB VRAM • 1790 GB/s
NVIDIA
$1999
NVIDIA Jetson AGX Xavier 32 GB
32 GB VRAM • 136 GB/s
NVIDIA
NVIDIA Quadro GV100
32 GB VRAM • 868 GB/s
NVIDIA
NVIDIA TITAN V CEO Edition
32 GB VRAM • 868 GB/s
NVIDIA
NVIDIA Tesla PG500-216
32 GB VRAM • 1130 GB/s
NVIDIA
NVIDIA Tesla PG503-216
32 GB VRAM • 1130 GB/s
NVIDIA
AMD Radeon Pro Vega II
32 GB VRAM • 825 GB/s
AMD
AMD Radeon Pro Vega II Duo
32 GB VRAM • 1020 GB/s
AMD
AMD Radeon PRO V620
32 GB VRAM • 512 GB/s
AMD
AMD Radeon PRO W6800
32 GB VRAM • 512 GB/s
AMD
AMD Radeon Pro W6800X
32 GB VRAM • 512 GB/s
AMD
AMD Radeon Pro W6800X Duo
32 GB VRAM • 512 GB/s
AMD
AMD Radeon Pro W6900X
32 GB VRAM • 512 GB/s
AMD
NVIDIA Jetson AGX Orin 32 GB
32 GB VRAM • 205 GB/s
NVIDIA
AMD Radeon PRO W7800
32 GB VRAM • 576 GB/s
AMD

Find the best GPU for Mixtral-8x7B

Build Hardware for Mixtral-8x7B