From 53e666780067847097bdb65445020c5d8dbf3915 Mon Sep 17 00:00:00 2001 From: Nahieli Vasquez Date: Mon, 31 Mar 2025 21:57:00 +0200 Subject: [PATCH 01/13] model card for Mistral --- docs/source/en/model_doc/mistral.md | 144 ++++++++++++++++------------ 1 file changed, 83 insertions(+), 61 deletions(-) diff --git a/docs/source/en/model_doc/mistral.md b/docs/source/en/model_doc/mistral.md index 097d8888f9a5..22853a69343c 100644 --- a/docs/source/en/model_doc/mistral.md +++ b/docs/source/en/model_doc/mistral.md @@ -25,45 +25,45 @@ rendered properly in your Markdown viewer. SDPA -## Overview +## Model -Mistral was introduced in the [this blogpost](https://mistral.ai/news/announcing-mistral-7b/) by Albert Jiang, Alexandre Sablayrolles, Arthur Mensch, Chris Bamford, Devendra Singh Chaplot, Diego de las Casas, Florian Bressand, Gianna Lengyel, Guillaume Lample, Lélio Renard Lavaud, Lucile Saulnier, Marie-Anne Lachaux, Pierre Stock, Teven Le Scao, Thibaut Lavril, Thomas Wang, Timothée Lacroix, William El Sayed. +Mistral is a decoder-only Transformer model that incorporates several architectural choices to enhance its performance. It utilizes Sliding Window Attention, trained with an 8k context length and a fixed cache size, offering a theoretical attention span of up to 128K tokens. Additionally, Mistral-7B employs Grouped Query Attention (GQA), which helps to speed up inference and reduce the cache size. The model also features a Byte-fallback BPE tokenizer, ensuring that characters are never mapped to out-of-vocabulary tokens, thus improving token handling and overall efficiency. -The introduction of the blog post says: +Mistral was introduced in [this blogpost](https://mistral.ai/news/announcing-mistral-7b/) -*Mistral AI team is proud to release Mistral 7B, the most powerful language model for its size to date.* +> [!TIP] +> Click on the Mistral models in the right sidebar for more examples of how to apply Mistral to different language tasks. -Mistral-7B is the first large language model (LLM) released by [mistral.ai](https://mistral.ai/). +The example below demonstrates how to generate text with [`Pipeline`] or the [`AutoModel`], and from the command line. -### Architectural details + + -Mistral-7B is a decoder-only Transformer with the following architectural choices: - -- Sliding Window Attention - Trained with 8k context length and fixed cache size, with a theoretical attention span of 128K tokens -- GQA (Grouped Query Attention) - allowing faster inference and lower cache size. -- Byte-fallback BPE tokenizer - ensures that characters are never mapped to out of vocabulary tokens. - -For more details refer to the [release blog post](https://mistral.ai/news/announcing-mistral-7b/). - -### License +```python +>>> import torch +>>> from transformers import pipeline -`Mistral-7B` is released under the Apache 2.0 license. +>>> 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?"} +... ] -## Usage tips +chatbot = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.3") +chatbot(messages) -The Mistral team has released 3 checkpoints: +``` -- a base model, [Mistral-7B-v0.1](https://huggingface.co/mistralai/Mistral-7B-v0.1), which has been pre-trained to predict the next token on internet-scale data. -- an instruction tuned model, [Mistral-7B-Instruct-v0.1](https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.1), which is the base model optimized for chat purposes using supervised fine-tuning (SFT) and direct preference optimization (DPO). -- an improved instruction tuned model, [Mistral-7B-Instruct-v0.2](https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.2), which improves upon v1. + + The base model can be used as follows: ```python >>> from transformers import AutoModelForCausalLM, AutoTokenizer ->>> model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-v0.1", device_map="auto") ->>> tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-v0.1") +>>> model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-v0.3", device_map="auto") +>>> tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-v0.3") >>> prompt = "My favourite condiment is" @@ -78,10 +78,11 @@ The base model can be used as follows: The instruction tuned model can be used as follows: ```python +import torch >>> from transformers import AutoModelForCausalLM, AutoTokenizer ->>> model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.2", device_map="auto") ->>> tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.2") +>>> model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.3", device_map="auto") +>>> tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.3") >>> messages = [ ... {"role": "user", "content": "What is your favourite condiment?"}, @@ -95,9 +96,65 @@ The instruction tuned model can be used as follows: >>> tokenizer.batch_decode(generated_ids)[0] "Mayonnaise can be made as follows: (...)" ``` - As can be seen, the instruction-tuned model requires a [chat template](../chat_templating) to be applied to make sure the inputs are prepared in the right format. + + + +```python +echo -e "My favorite condiment is" | transformers-cli run --task text-generation --model mistralai/Mistral-7B-v0.3 +``` + +## Shrinking down Mistral using quantization + +As the Mistral model has 7 billion parameters, that would require about 14GB of GPU RAM in half precision (float16), since each parameter is stored in 2 bytes. However, one can shrink down the size of the model using [quantization](../quantization.md). If the model is quantized to 4 bits (or half a byte per parameter),that requires only about 3.5GB of RAM. + +Quantizing a model is as simple as passing a `quantization_config` to the model. Below, we'll leverage the BitsAndyBytes quantization (but refer to [this page](../quantization.md) for other quantization methods): + +```python +>>> import torch +>>> from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig + +>>> # specify how to quantize the model +>>> quantization_config = BitsAndBytesConfig( +... load_in_4bit=True, +... bnb_4bit_quant_type="nf4", +... bnb_4bit_compute_dtype="torch.float16", +... ) + +>>> model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.3", quantization_config=True, device_map="auto") +>>> tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.3") + +>>> prompt = "My favourite condiment 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?"} +... ] + +>>> model_inputs = tokenizer.apply_chat_template(messages, return_tensors="pt").to("cuda") + +>>> generated_ids = model.generate(model_inputs, max_new_tokens=100, do_sample=True) +>>> tokenizer.batch_decode(generated_ids)[0] +"The expected output" +``` + +Use the [AttentionMaskVisualizer](https://github.com/huggingface/transformers/blob/beb9b5b02246b9b7ee81ddf938f93f44cfeaad19/src/transformers/utils/attention_visualizer.py#L139) to better understand what tokens the model can and cannot attend to. + +```py +from transformers.utils.attention_visualizer import AttentionMaskVisualizer + +visualizer = AttentionMaskVisualizer("mistralai/Mistral-7B-Instruct-v0.3") +visualizer("Do you have mayonnaise recipes?") +``` + +
+ +
+ +## Notes + ## Speeding up Mistral by using Flash Attention The code snippets above showcase inference without any optimization tricks. However, one can drastically speed up the model by leveraging [Flash Attention](../perf_train_gpu_one#flash-attention-2), which is a faster implementation of the attention mechanism used inside the model. @@ -144,41 +201,6 @@ To enable sliding window attention, just make sure to have a `flash-attn` versio The Flash Attention-2 model uses also a more memory efficient cache slicing mechanism - as recommended per the official implementation of Mistral model that use rolling cache mechanism we keep the cache size fixed (`self.config.sliding_window`), support batched generation only for `padding_side="left"` and use the absolute position of the current token to compute the positional embedding. -## Shrinking down Mistral using quantization - -As the Mistral model has 7 billion parameters, that would require about 14GB of GPU RAM in half precision (float16), since each parameter is stored in 2 bytes. However, one can shrink down the size of the model using [quantization](../quantization.md). If the model is quantized to 4 bits (or half a byte per parameter),that requires only about 3.5GB of RAM. - -Quantizing a model is as simple as passing a `quantization_config` to the model. Below, we'll leverage the BitsAndyBytes quantization (but refer to [this page](../quantization.md) for other quantization methods): - -```python ->>> import torch ->>> from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig - ->>> # specify how to quantize the model ->>> quantization_config = BitsAndBytesConfig( -... load_in_4bit=True, -... bnb_4bit_quant_type="nf4", -... bnb_4bit_compute_dtype="torch.float16", -... ) - ->>> model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.2", quantization_config=True, device_map="auto") ->>> tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.2") - ->>> prompt = "My favourite condiment 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?"} -... ] - ->>> model_inputs = tokenizer.apply_chat_template(messages, return_tensors="pt").to("cuda") - ->>> generated_ids = model.generate(model_inputs, max_new_tokens=100, do_sample=True) ->>> tokenizer.batch_decode(generated_ids)[0] -"The expected output" -``` - This model was contributed by [Younes Belkada](https://huggingface.co/ybelkada) and [Arthur Zucker](https://huggingface.co/ArthurZ) . The original code can be found [here](https://github.com/mistralai/mistral-src). From 36c0646ffe1ac5cca8cc46a7f2e0c5f0d2b3e0ad Mon Sep 17 00:00:00 2001 From: Nahieli <54726691+NahieliV@users.noreply.github.com> Date: Wed, 2 Apr 2025 20:53:38 +0200 Subject: [PATCH 02/13] Update docs/source/en/model_doc/mistral.md Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> --- docs/source/en/model_doc/mistral.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/source/en/model_doc/mistral.md b/docs/source/en/model_doc/mistral.md index 22853a69343c..bf20f200ffa2 100644 --- a/docs/source/en/model_doc/mistral.md +++ b/docs/source/en/model_doc/mistral.md @@ -25,7 +25,6 @@ rendered properly in your Markdown viewer. SDPA -## Model Mistral is a decoder-only Transformer model that incorporates several architectural choices to enhance its performance. It utilizes Sliding Window Attention, trained with an 8k context length and a fixed cache size, offering a theoretical attention span of up to 128K tokens. Additionally, Mistral-7B employs Grouped Query Attention (GQA), which helps to speed up inference and reduce the cache size. The model also features a Byte-fallback BPE tokenizer, ensuring that characters are never mapped to out-of-vocabulary tokens, thus improving token handling and overall efficiency. From 33e6c40675b08deba332eab12cef17575d6b12cb Mon Sep 17 00:00:00 2001 From: Nahieli <54726691+NahieliV@users.noreply.github.com> Date: Wed, 2 Apr 2025 21:06:52 +0200 Subject: [PATCH 03/13] Apply suggestions from code review Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> --- docs/source/en/model_doc/mistral.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/docs/source/en/model_doc/mistral.md b/docs/source/en/model_doc/mistral.md index bf20f200ffa2..7a7434a2d66d 100644 --- a/docs/source/en/model_doc/mistral.md +++ b/docs/source/en/model_doc/mistral.md @@ -80,7 +80,7 @@ The instruction tuned model can be used as follows: import torch >>> from transformers import AutoModelForCausalLM, AutoTokenizer ->>> model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.3", device_map="auto") +>>> model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-v0.3", torch_dtype=torch.bfloat16, attn_implementation="sdpa", device_map="auto") >>> tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.3") >>> messages = [ @@ -101,14 +101,15 @@ As can be seen, the instruction-tuned model requires a [chat template](../chat_t ```python -echo -e "My favorite condiment is" | transformers-cli run --task text-generation --model mistralai/Mistral-7B-v0.3 +# pip install -U flash-attn --no-build-isolation +echo -e "My favorite condiment is" | transformers-cli chat --model mistralai/Mistral-7B-v0.3 --torch_dtype auto --device 0 --attn_implementation flash_attention_2 ``` ## Shrinking down Mistral using quantization -As the Mistral model has 7 billion parameters, that would require about 14GB of GPU RAM in half precision (float16), since each parameter is stored in 2 bytes. However, one can shrink down the size of the model using [quantization](../quantization.md). If the model is quantized to 4 bits (or half a byte per parameter),that requires only about 3.5GB of RAM. +Quantization reduces the memory burden of large models by representing the weights in a lower precision. Refer to the [Quantization](../quantization/overview) overview for more available quantization backends. -Quantizing a model is as simple as passing a `quantization_config` to the model. Below, we'll leverage the BitsAndyBytes quantization (but refer to [this page](../quantization.md) for other quantization methods): +The example below uses [bitsandbytes](../quantization/bitsandbytes) to only quantize the weights to 4-bits. ```python >>> import torch @@ -121,7 +122,7 @@ Quantizing a model is as simple as passing a `quantization_config` to the model. ... bnb_4bit_compute_dtype="torch.float16", ... ) ->>> model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.3", quantization_config=True, device_map="auto") +>>> model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.3", quantization_config=True, torch_dtype=torch.bfloat16, device_map="auto") >>> tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.3") >>> prompt = "My favourite condiment is" From dd94b9cf3e836502a4895b6fa586a62ac1fbb502 Mon Sep 17 00:00:00 2001 From: Nahieli <54726691+NahieliV@users.noreply.github.com> Date: Wed, 2 Apr 2025 21:08:18 +0200 Subject: [PATCH 04/13] Update docs/source/en/model_doc/mistral.md Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> --- docs/source/en/model_doc/mistral.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/source/en/model_doc/mistral.md b/docs/source/en/model_doc/mistral.md index 7a7434a2d66d..0fcd314a2269 100644 --- a/docs/source/en/model_doc/mistral.md +++ b/docs/source/en/model_doc/mistral.md @@ -26,7 +26,8 @@ rendered properly in your Markdown viewer. -Mistral is a decoder-only Transformer model that incorporates several architectural choices to enhance its performance. It utilizes Sliding Window Attention, trained with an 8k context length and a fixed cache size, offering a theoretical attention span of up to 128K tokens. Additionally, Mistral-7B employs Grouped Query Attention (GQA), which helps to speed up inference and reduce the cache size. The model also features a Byte-fallback BPE tokenizer, ensuring that characters are never mapped to out-of-vocabulary tokens, thus improving token handling and overall efficiency. +[Mistral](https://huggingface.co/papers/2310.06825) is a 7B parameter language model, available as a pretrained and instruction-tuned variant, focused on balancing +the scaling costs of large models with performance and efficient inference. This model uses sliding window attention (SWA) trained with a 8K context length and a fixed cache size to handle longer sequences more effectively. Grouped-query attention (GQA) speeds up inference and reduces memory requirements. Mistral also features a byte-fallback BPE tokenizer to improve token handling and efficiency by ensuring characters are never mapped to out-of-vocabulary tokens. Mistral was introduced in [this blogpost](https://mistral.ai/news/announcing-mistral-7b/) From da3375ec28b10af6540c8ebdfe8e7ea817818e27 Mon Sep 17 00:00:00 2001 From: Nahieli <54726691+NahieliV@users.noreply.github.com> Date: Wed, 2 Apr 2025 21:08:31 +0200 Subject: [PATCH 05/13] Update docs/source/en/model_doc/mistral.md Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> --- docs/source/en/model_doc/mistral.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/en/model_doc/mistral.md b/docs/source/en/model_doc/mistral.md index 0fcd314a2269..8c785f799cda 100644 --- a/docs/source/en/model_doc/mistral.md +++ b/docs/source/en/model_doc/mistral.md @@ -29,7 +29,7 @@ rendered properly in your Markdown viewer. [Mistral](https://huggingface.co/papers/2310.06825) is a 7B parameter language model, available as a pretrained and instruction-tuned variant, focused on balancing the scaling costs of large models with performance and efficient inference. This model uses sliding window attention (SWA) trained with a 8K context length and a fixed cache size to handle longer sequences more effectively. Grouped-query attention (GQA) speeds up inference and reduces memory requirements. Mistral also features a byte-fallback BPE tokenizer to improve token handling and efficiency by ensuring characters are never mapped to out-of-vocabulary tokens. -Mistral was introduced in [this blogpost](https://mistral.ai/news/announcing-mistral-7b/) +You can find all the original Mistral checkpoints under the [Mistral AI_](https://huggingface.co/mistralai) organization. > [!TIP] > Click on the Mistral models in the right sidebar for more examples of how to apply Mistral to different language tasks. From 4b86d80603c32ab17bddc3bde813a7cafa6c6b74 Mon Sep 17 00:00:00 2001 From: Nahieli <54726691+NahieliV@users.noreply.github.com> Date: Wed, 2 Apr 2025 21:08:41 +0200 Subject: [PATCH 06/13] Update docs/source/en/model_doc/mistral.md Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> --- docs/source/en/model_doc/mistral.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/en/model_doc/mistral.md b/docs/source/en/model_doc/mistral.md index 8c785f799cda..7a4228574294 100644 --- a/docs/source/en/model_doc/mistral.md +++ b/docs/source/en/model_doc/mistral.md @@ -34,7 +34,7 @@ You can find all the original Mistral checkpoints under the [Mistral AI_](https: > [!TIP] > Click on the Mistral models in the right sidebar for more examples of how to apply Mistral to different language tasks. -The example below demonstrates how to generate text with [`Pipeline`] or the [`AutoModel`], and from the command line. +The example below demonstrates how to chat with [`Pipeline`] or the [`AutoModel`], and from the command line. From bb9fb207d27fbbfacceb7795cc352a2d2b998d07 Mon Sep 17 00:00:00 2001 From: Nahieli <54726691+NahieliV@users.noreply.github.com> Date: Wed, 2 Apr 2025 21:08:51 +0200 Subject: [PATCH 07/13] Update docs/source/en/model_doc/mistral.md Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> --- docs/source/en/model_doc/mistral.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/en/model_doc/mistral.md b/docs/source/en/model_doc/mistral.md index 7a4228574294..4debdea3a727 100644 --- a/docs/source/en/model_doc/mistral.md +++ b/docs/source/en/model_doc/mistral.md @@ -49,7 +49,7 @@ The example below demonstrates how to chat with [`Pipeline`] or the [`AutoModel` ... {"role": "user", "content": "Do you have mayonnaise recipes?"} ... ] -chatbot = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.3") +chatbot = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.3", torch_dtype=torch.bfloat16, device=0) chatbot(messages) ``` From f143bf60d48ab59a57d603726074cc3f374d8ebe Mon Sep 17 00:00:00 2001 From: Nahieli Vasquez Date: Wed, 2 Apr 2025 21:38:21 +0200 Subject: [PATCH 08/13] apply suggestions --- docs/source/en/model_doc/mistral.md | 64 ----------------------------- 1 file changed, 64 deletions(-) diff --git a/docs/source/en/model_doc/mistral.md b/docs/source/en/model_doc/mistral.md index 4debdea3a727..f8462248695c 100644 --- a/docs/source/en/model_doc/mistral.md +++ b/docs/source/en/model_doc/mistral.md @@ -106,8 +106,6 @@ As can be seen, the instruction-tuned model requires a [chat template](../chat_t echo -e "My favorite condiment is" | transformers-cli chat --model mistralai/Mistral-7B-v0.3 --torch_dtype auto --device 0 --attn_implementation flash_attention_2 ``` -## Shrinking down Mistral using quantization - Quantization reduces the memory burden of large models by representing the weights in a lower precision. Refer to the [Quantization](../quantization/overview) overview for more available quantization backends. The example below uses [bitsandbytes](../quantization/bitsandbytes) to only quantize the weights to 4-bits. @@ -154,68 +152,6 @@ visualizer("Do you have mayonnaise recipes?") -## Notes - -## Speeding up Mistral by using Flash Attention - -The code snippets above showcase inference without any optimization tricks. However, one can drastically speed up the model by leveraging [Flash Attention](../perf_train_gpu_one#flash-attention-2), which is a faster implementation of the attention mechanism used inside the model. - -First, make sure to install the latest version of Flash Attention 2 to include the sliding window attention feature. - -```bash -pip install -U flash-attn --no-build-isolation -``` - -Make also sure that you have a hardware that is compatible with Flash-Attention 2. Read more about it in the official documentation of the [flash attention repository](https://github.com/Dao-AILab/flash-attention). Make also sure to load your model in half-precision (e.g. `torch.float16`) - -To load and run a model using Flash Attention-2, refer to the snippet below: - -```python ->>> import torch ->>> from transformers import AutoModelForCausalLM, AutoTokenizer - ->>> model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-v0.1", torch_dtype=torch.float16, attn_implementation="flash_attention_2", device_map="auto") ->>> tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-v0.1") - ->>> prompt = "My favourite condiment is" - ->>> model_inputs = tokenizer([prompt], return_tensors="pt").to("cuda") ->>> model.to(device) - ->>> generated_ids = model.generate(**model_inputs, max_new_tokens=100, do_sample=True) ->>> tokenizer.batch_decode(generated_ids)[0] -"My favourite condiment is to (...)" -``` - -### Expected speedups - -Below is a expected speedup diagram that compares pure inference time between the native implementation in transformers using `mistralai/Mistral-7B-v0.1` checkpoint and the Flash Attention 2 version of the model. - -
- -
- -### Sliding window Attention - -The current implementation supports the sliding window attention mechanism and memory efficient cache management. -To enable sliding window attention, just make sure to have a `flash-attn` version that is compatible with sliding window attention (`>=2.3.0`). - -The Flash Attention-2 model uses also a more memory efficient cache slicing mechanism - as recommended per the official implementation of Mistral model that use rolling cache mechanism we keep the cache size fixed (`self.config.sliding_window`), support batched generation only for `padding_side="left"` and use the absolute position of the current token to compute the positional embedding. - -This model was contributed by [Younes Belkada](https://huggingface.co/ybelkada) and [Arthur Zucker](https://huggingface.co/ArthurZ) . -The original code can be found [here](https://github.com/mistralai/mistral-src). - -## Resources - -A list of official Hugging Face and community (indicated by 🌎) resources to help you get started with Mistral. If you're interested in submitting a resource to be included here, please feel free to open a Pull Request and we'll review it! The resource should ideally demonstrate something new instead of duplicating an existing resource. - - - -- A demo notebook to perform supervised fine-tuning (SFT) of Mistral-7B can be found [here](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/Mistral/Supervised_fine_tuning_(SFT)_of_an_LLM_using_Hugging_Face_tooling.ipynb). 🌎 -- A [blog post](https://www.philschmid.de/fine-tune-llms-in-2024-with-trl) on how to fine-tune LLMs in 2024 using Hugging Face tooling. 🌎 -- The [Alignment Handbook](https://github.com/huggingface/alignment-handbook) by Hugging Face includes scripts and recipes to perform supervised fine-tuning (SFT) and direct preference optimization with Mistral-7B. This includes scripts for full fine-tuning, QLoRa on a single GPU as well as multi-GPU fine-tuning. -- [Causal language modeling task guide](../tasks/language_modeling) - ## MistralConfig [[autodoc]] MistralConfig From 710956e6ebe9c40bf2e0982df5ff7cdcce060db4 Mon Sep 17 00:00:00 2001 From: Nahieli Vasquez Date: Wed, 2 Apr 2025 21:52:12 +0200 Subject: [PATCH 09/13] fix typo --- docs/source/en/model_doc/mistral.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/en/model_doc/mistral.md b/docs/source/en/model_doc/mistral.md index f8462248695c..89c7a2cb1529 100644 --- a/docs/source/en/model_doc/mistral.md +++ b/docs/source/en/model_doc/mistral.md @@ -78,7 +78,7 @@ The base model can be used as follows: The instruction tuned model can be used as follows: ```python -import torch +>>> import torch >>> from transformers import AutoModelForCausalLM, AutoTokenizer >>> model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-v0.3", torch_dtype=torch.bfloat16, attn_implementation="sdpa", device_map="auto") From 2ff53ec1e33ce5da1e2b302ffb506676404eb50a Mon Sep 17 00:00:00 2001 From: Nahieli Vasquez Date: Thu, 3 Apr 2025 22:09:39 +0200 Subject: [PATCH 10/13] updated with comments --- docs/source/en/model_doc/mistral.md | 67 +++++++++++------------------ 1 file changed, 26 insertions(+), 41 deletions(-) diff --git a/docs/source/en/model_doc/mistral.md b/docs/source/en/model_doc/mistral.md index 89c7a2cb1529..540199b4b1c2 100644 --- a/docs/source/en/model_doc/mistral.md +++ b/docs/source/en/model_doc/mistral.md @@ -14,17 +14,18 @@ rendered properly in your Markdown viewer. --> -# Mistral - -
-PyTorch -TensorFlow -Flax -FlashAttention -SDPA +
+
+ PyTorch + TensorFlow + Flax + FlashAttention + SDPA +
+# Mistral [Mistral](https://huggingface.co/papers/2310.06825) is a 7B parameter language model, available as a pretrained and instruction-tuned variant, focused on balancing the scaling costs of large models with performance and efficient inference. This model uses sliding window attention (SWA) trained with a 8K context length and a fixed cache size to handle longer sequences more effectively. Grouped-query attention (GQA) speeds up inference and reduces memory requirements. Mistral also features a byte-fallback BPE tokenizer to improve token handling and efficiency by ensuring characters are never mapped to out-of-vocabulary tokens. @@ -49,39 +50,18 @@ The example below demonstrates how to chat with [`Pipeline`] or the [`AutoModel` ... {"role": "user", "content": "Do you have mayonnaise recipes?"} ... ] -chatbot = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.3", torch_dtype=torch.bfloat16, device=0) -chatbot(messages) - +>>> chatbot = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.3", torch_dtype=torch.bfloat16, device=0) +>>> chatbot(messages) ``` - + -The base model can be used as follows: - -```python ->>> from transformers import AutoModelForCausalLM, AutoTokenizer - ->>> model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-v0.3", device_map="auto") ->>> tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-v0.3") - ->>> prompt = "My favourite condiment is" - ->>> model_inputs = tokenizer([prompt], return_tensors="pt").to("cuda") ->>> model.to(device) - ->>> generated_ids = model.generate(**model_inputs, max_new_tokens=100, do_sample=True) ->>> tokenizer.batch_decode(generated_ids)[0] -"My favourite condiment is to ..." -``` - -The instruction tuned model can be used as follows: - ```python >>> import torch >>> from transformers import AutoModelForCausalLM, AutoTokenizer ->>> model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-v0.3", torch_dtype=torch.bfloat16, attn_implementation="sdpa", device_map="auto") +>>> model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.3", torch_dtype=torch.bfloat16, attn_implementation="sdpa", device_map="auto") >>> tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.3") >>> messages = [ @@ -96,16 +76,18 @@ The instruction tuned model can be used as follows: >>> tokenizer.batch_decode(generated_ids)[0] "Mayonnaise can be made as follows: (...)" ``` -As can be seen, the instruction-tuned model requires a [chat template](../chat_templating) to be applied to make sure the inputs are prepared in the right format. - + ```python -# pip install -U flash-attn --no-build-isolation -echo -e "My favorite condiment is" | transformers-cli chat --model mistralai/Mistral-7B-v0.3 --torch_dtype auto --device 0 --attn_implementation flash_attention_2 +echo -e "My favorite condiment is" | transformers-cli chat --model_name_or_path mistralai/Mistral-7B-v0.3 --torch_dtype auto --device 0 --attn_implementation flash_attention_2 ``` + + + + Quantization reduces the memory burden of large models by representing the weights in a lower precision. Refer to the [Quantization](../quantization/overview) overview for more available quantization backends. The example below uses [bitsandbytes](../quantization/bitsandbytes) to only quantize the weights to 4-bits. @@ -139,13 +121,16 @@ The example below uses [bitsandbytes](../quantization/bitsandbytes) to only quan "The expected output" ``` + + + Use the [AttentionMaskVisualizer](https://github.com/huggingface/transformers/blob/beb9b5b02246b9b7ee81ddf938f93f44cfeaad19/src/transformers/utils/attention_visualizer.py#L139) to better understand what tokens the model can and cannot attend to. ```py -from transformers.utils.attention_visualizer import AttentionMaskVisualizer +>>> from transformers.utils.attention_visualizer import AttentionMaskVisualizer -visualizer = AttentionMaskVisualizer("mistralai/Mistral-7B-Instruct-v0.3") -visualizer("Do you have mayonnaise recipes?") +>>> visualizer = AttentionMaskVisualizer("mistralai/Mistral-7B-Instruct-v0.3") +>>> visualizer("Do you have mayonnaise recipes?") ```
From 278487527a02de5e170f75d344407851f9826958 Mon Sep 17 00:00:00 2001 From: Nahieli Vasquez Date: Thu, 3 Apr 2025 22:25:41 +0200 Subject: [PATCH 11/13] updated with comments --- docs/source/en/model_doc/mistral.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/source/en/model_doc/mistral.md b/docs/source/en/model_doc/mistral.md index 540199b4b1c2..1b6a71924a85 100644 --- a/docs/source/en/model_doc/mistral.md +++ b/docs/source/en/model_doc/mistral.md @@ -126,6 +126,7 @@ The example below uses [bitsandbytes](../quantization/bitsandbytes) to only quan Use the [AttentionMaskVisualizer](https://github.com/huggingface/transformers/blob/beb9b5b02246b9b7ee81ddf938f93f44cfeaad19/src/transformers/utils/attention_visualizer.py#L139) to better understand what tokens the model can and cannot attend to. + ```py >>> from transformers.utils.attention_visualizer import AttentionMaskVisualizer From c9f2dd42bc6f35e6d35aa815a0eb685ab1edabd6 Mon Sep 17 00:00:00 2001 From: Nahieli Vasquez Date: Fri, 4 Apr 2025 08:00:47 +0200 Subject: [PATCH 12/13] updated with comments --- docs/source/en/model_doc/mistral.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/source/en/model_doc/mistral.md b/docs/source/en/model_doc/mistral.md index 1b6a71924a85..540199b4b1c2 100644 --- a/docs/source/en/model_doc/mistral.md +++ b/docs/source/en/model_doc/mistral.md @@ -126,7 +126,6 @@ The example below uses [bitsandbytes](../quantization/bitsandbytes) to only quan Use the [AttentionMaskVisualizer](https://github.com/huggingface/transformers/blob/beb9b5b02246b9b7ee81ddf938f93f44cfeaad19/src/transformers/utils/attention_visualizer.py#L139) to better understand what tokens the model can and cannot attend to. - ```py >>> from transformers.utils.attention_visualizer import AttentionMaskVisualizer From 86de7d1768a84f25b0c136c9ba4191118924c16f Mon Sep 17 00:00:00 2001 From: Steven Liu <59462357+stevhliu@users.noreply.github.com> Date: Fri, 4 Apr 2025 11:42:52 -0700 Subject: [PATCH 13/13] remove hfoption block --- docs/source/en/model_doc/mistral.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/docs/source/en/model_doc/mistral.md b/docs/source/en/model_doc/mistral.md index 540199b4b1c2..39334385e605 100644 --- a/docs/source/en/model_doc/mistral.md +++ b/docs/source/en/model_doc/mistral.md @@ -121,9 +121,6 @@ The example below uses [bitsandbytes](../quantization/bitsandbytes) to only quan "The expected output" ``` - - - Use the [AttentionMaskVisualizer](https://github.com/huggingface/transformers/blob/beb9b5b02246b9b7ee81ddf938f93f44cfeaad19/src/transformers/utils/attention_visualizer.py#L139) to better understand what tokens the model can and cannot attend to. ```py @@ -189,4 +186,4 @@ Use the [AttentionMaskVisualizer](https://github.com/huggingface/transformers/bl ## TFMistralForSequenceClassification [[autodoc]] TFMistralForSequenceClassification - - call \ No newline at end of file + - call