Buckets:
| # Mamba 2 | |
| [Mamba 2](https://huggingface.co/papers/2405.21060) is based on the state space duality (SSD) framework which connects structured state space models (SSMs) and attention variants. It uses a more efficient SSD algorithm that is 2-8x faster than Mamba and modifies the architecture to enable tensor parallelism and a grouped-value attention (GVA) head structure. | |
| You can find all the original Mamba 2 checkpoints under the [State Space Models](https://huggingface.co/state-spaces) organization, but the examples shown below use [mistralai/Mamba-Codestral-7B-v0.1](https://huggingface.co/mistralai/Mamba-Codestral-7B-v0.1) because a Hugging Face implementation isn't supported yet for the original checkpoints. | |
| Other Mamba 2-based architectures include [Bamba](./bamba), [FalconH1](./falcon_h1), and [Zamba2](./zamba2). | |
| > [!TIP] | |
| > This model was contributed by [ArthurZ](https://huggingface.co/ArthurZ). | |
| > Click on the Mamba models in the right sidebar for more examples of how to apply Mamba to different language tasks. | |
| The example below demonstrates how to generate text with [Pipeline](/docs/transformers/pr_33962/en/main_classes/pipelines#transformers.Pipeline), [AutoModel](/docs/transformers/pr_33962/en/model_doc/auto#transformers.AutoModel), and from the command line. | |
| <hfoptions id="Usage"> | |
| <hfoption id="Pipeline"> | |
| ```python | |
| import torch | |
| from transformers import pipeline | |
| pipeline = pipeline( | |
| task="text-generation", | |
| model="mistralai/Mamba-Codestral-7B-v0.1", | |
| dtype=torch.bfloat16, | |
| device=0 | |
| ) | |
| pipeline("Plants create energy through a process known as") | |
| ``` | |
| </hfoption> | |
| <hfoption id="AutoModel"> | |
| ```python | |
| import torch | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| tokenizer = AutoTokenizer.from_pretrained("mistralai/Mamba-Codestral-7B-v0.1") | |
| model = AutoModelForCausalLM.from_pretrained("mistralai/Mamba-Codestral-7B-v0.1", dtype=torch.bfloat16, device_map="auto") | |
| input_ids = tokenizer("Plants create energy through a process known as", return_tensors="pt").to(model.device) | |
| output = model.generate(**input_ids) | |
| print(tokenizer.decode(output[0], skip_special_tokens=True)) | |
| ``` | |
| </hfoption> | |
| <hfoption id="transformers CLI"> | |
| ```bash | |
| echo -e "Plants create energy through a process known as" | transformers run --task text-generation --model mistralai/Mamba-Codestral-7B-v0.1 --device 0 | |
| ``` | |
| </hfoption> | |
| </hfoptions> | |
| 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 [torchao](../quantization/torchao) to only quantize the weights to 4-bit integers. | |
| ```py | |
| import torch | |
| from transformers import AutoModelForCausalLM, AutoTokenizer, TorchAoConfig | |
| quantization_config = TorchAoConfig("int4_weight_only", group_size=128) | |
| tokenizer = AutoTokenizer.from_pretrained("mistralai/Mamba-Codestral-7B-v0.1") | |
| model = AutoModelForCausalLM.from_pretrained("mistralai/Mamba-Codestral-7B-v0.1", dtype=torch.bfloat16, quantization_config=quantization_config, device_map="auto") | |
| input_ids = tokenizer("Plants create energy through a process known as", return_tensors="pt").to(model.device) | |
| output = model.generate(**input_ids) | |
| print(tokenizer.decode(output[0], skip_special_tokens=True)) | |
| ``` | |
| ## Notes | |
| - Codestral Mamba has `groups=8` which are similar to the number of kv heads in an attention-based model. | |
| - Codestral Mamba has two different forward passes, `torch_forward` or `cuda_kernels_forward`, and their results are expected to be slightly different. | |
| - `torch_forward` without compilation is 3-4x faster than `cuda_kernels_forward`. | |
| - `cuda_kernels_forward` uses the original CUDA kernels if they're available in your environment. It is slower during prefill because it requires a "warmup run" due to the higher CPU overhead (see [these](https://github.com/state-spaces/mamba/issues/389#issuecomment-2171755306) [comments](https://github.com/state-spaces/mamba/issues/355#issuecomment-2147597457) for more details). | |
| - There are no positional embeddings in this model, but there is an `attention_mask` and a specific logic to mask out hidden states in two places in the case of batched generation (see this [comment](https://github.com/state-spaces/mamba/issues/66#issuecomment-1863563829) for more details). This (and the addition of the reimplemented Mamba 2 kernels) results in a slight discrepancy between batched and cached generation. | |
| - The SSM algorithm heavily relies on tensor contractions, which have matmul equivalents but the order of operations is slightly different. This makes the difference greater at smaller precisions. | |
| - Hidden states that correspond to padding tokens is shutdown in 2 places and is mostly tested with left-padding. Right-padding propagates noise down the line and is not guaranteed to yield satisfactory results. `tokenizer.padding_side = "left"` ensures you are using the correct padding side. | |
| - The example below demonstrates how to fine-tune Mamba 2 with [PEFT](https://huggingface.co/docs/peft). | |
| ```python | |
| from datasets import load_dataset | |
| from peft import LoraConfig | |
| from trl import SFTConfig, SFTTrainer | |
| model_id = "mistralai/Mamba-Codestral-7B-v0.1" | |
| dataset = load_dataset("Abirate/english_quotes", split="train") | |
| training_args = SFTConfig(dataset_text_field="quote", gradient_checkpointing=True, per_device_train_batch_size=4) | |
| lora_config = LoraConfig(target_modules=["x_proj", "embeddings", "in_proj", "out_proj"]) | |
| trainer = SFTTrainer( | |
| model=model_id, | |
| args=training_args, | |
| train_dataset=dataset, | |
| peft_config=lora_config, | |
| ) | |
| trainer.train() | |
| ``` | |
| ## Mamba2Config[[transformers.Mamba2Config]] | |
| <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"> | |
| <docstring><name>class transformers.Mamba2Config</name><anchor>transformers.Mamba2Config</anchor><source>https://github.com/huggingface/transformers/blob/vr_33962/src/transformers/models/mamba2/configuration_mamba2.py#L26</source><parameters>[{"name": "num_heads", "val": " = 128"}, {"name": "head_dim", "val": " = 64"}, {"name": "vocab_size", "val": " = 32768"}, {"name": "hidden_size", "val": " = 4096"}, {"name": "state_size", "val": " = 128"}, {"name": "num_hidden_layers", "val": " = 64"}, {"name": "layer_norm_epsilon", "val": " = 1e-05"}, {"name": "pad_token_id", "val": " = 1"}, {"name": "bos_token_id", "val": " = 0"}, {"name": "eos_token_id", "val": " = 2"}, {"name": "expand", "val": " = 2"}, {"name": "conv_kernel", "val": " = 4"}, {"name": "n_groups", "val": " = 8"}, {"name": "use_bias", "val": " = False"}, {"name": "use_conv_bias", "val": " = True"}, {"name": "hidden_act", "val": " = 'silu'"}, {"name": "initializer_range", "val": " = 0.1"}, {"name": "residual_in_fp32", "val": " = True"}, {"name": "time_step_rank", "val": " = 'auto'"}, {"name": "time_step_min", "val": " = 0.001"}, {"name": "time_step_max", "val": " = 0.1"}, {"name": "time_step_floor", "val": " = 0.0001"}, {"name": "time_step_limit", "val": " = (0.0, inf)"}, {"name": "rescale_prenorm_residual", "val": " = False"}, {"name": "use_cache", "val": " = True"}, {"name": "rms_norm", "val": " = True"}, {"name": "chunk_size", "val": " = 256"}, {"name": "tie_word_embeddings", "val": " = False"}, {"name": "**kwargs", "val": ""}]</parameters><paramsdesc>- **num_heads** (`int`, *optional*, defaults to 128) -- | |
| Number of heads for the evolution matrices of mamba 2. | |
| - **head_dim** (`int`, *optional*, defaults to 64) -- | |
| Dimension of each head. | |
| - **vocab_size** (`int`, *optional*, defaults to 32768) -- | |
| Vocabulary size of the MAMBA2 model. Defines the number of different tokens that can be represented by the | |
| `inputs_ids` passed when calling [Mamba2Model](/docs/transformers/pr_33962/en/model_doc/mamba2#transformers.Mamba2Model). | |
| - **hidden_size** (`int`, *optional*, defaults to 4096) -- | |
| Dimensionality of the embeddings and hidden states. | |
| - **state_size** (`int`, *optional*, defaults to 128) -- shape of the state space latents. | |
| - **num_hidden_layers** (`int`, *optional*, defaults to 64) -- | |
| Number of hidden layers in the model. | |
| - **layer_norm_epsilon** (`float`, *optional*, defaults to 1e-05) -- | |
| The epsilon to use in the layer normalization layers. | |
| - **pad_token_id** (`int`, *optional*, defaults to 1) -- | |
| Padding token id. | |
| - **bos_token_id** (`int`, *optional*, defaults to 0) -- | |
| The id of the beginning of sentence token in the vocabulary. | |
| - **eos_token_id** (`int`, *optional*, defaults to 2) -- | |
| The id of the end of sentence token in the vocabulary. | |
| - **expand** (`int`, *optional*, defaults to 2) -- Expanding factor used to determine the intermediate size. | |
| - **conv_kernel** (`int`, *optional*, defaults to 4) -- Size of the convolution kernel. | |
| - **n_groups** (`int`, *optional*, defaults to 8) -- | |
| Number of groups for the evolution matrices of mamba 2. | |
| - **use_bias** (`bool`, *optional*, defaults to `False`) -- | |
| Whether or not to use bias in ["in_proj", "out_proj"] of the mixer block | |
| - **use_conv_bias** (`bool`, *optional*, defaults to `True`) -- | |
| Whether or not to use bias in the convolution layer of the mixer block. | |
| - **hidden_act** (`str`, *optional*, defaults to `"silu"`) -- | |
| The non-linear activation function (function or string) in the decoder. | |
| - **initializer_range** (`float`, *optional*, defaults to 0.1) -- | |
| The standard deviation of the truncated_normal_initializer for initializing all weight matrices. | |
| - **residual_in_fp32** (`bool`, *optional*, defaults to `True`) -- | |
| Whether or not residuals should be in `float32`. If set to `False` residuals will keep the same `dtype` as the rest of the model | |
| - **time_step_rank** (`Union[int,str]`, *optional*, defaults to `"auto"`) -- | |
| Rank of the discretization projection matrix. `"auto"` means that it will default to `math.ceil(self.hidden_size / 16)` | |
| - **time_step_min** (`float`, *optional*, defaults to 0.001) -- | |
| Minimum `time_step` used to bound `dt_proj.bias`. | |
| - **time_step_max** (`float`, *optional*, defaults to 0.1) -- | |
| Maximum `time_step` used to bound `dt_proj.bias`. | |
| - **time_step_floor** (`float`, *optional*, defaults to 0.0001) -- | |
| Minimum clamping value of the `dt_proj.bias` layer initialization. | |
| - **time_step_limit** (`tuple`, *optional*, defaults to `(0.0, inf)`) -- | |
| Accepted range of time step values. | |
| - **rescale_prenorm_residual** (`bool`, *optional*, defaults to `False`) -- | |
| Whether or not to rescale `out_proj` weights when initializing. | |
| - **use_cache** (`bool`, *optional*, defaults to `True`) -- | |
| Whether or not the cache should be used. | |
| - **rms_norm** (`bool`, *optional*, defaults to `True`) -- | |
| Whether to use RMS norm or not. | |
| - **chunk_size** (`int`, *optional*, defaults to 256) -- | |
| Size of the chunks that will comprise the sequence. | |
| - **tie_word_embeddings** (`bool`, *optional*, defaults to `False`) -- | |
| Whether to tie word embeddings or not.</paramsdesc><paramgroups>0</paramgroups></docstring> | |
| This is the configuration class to store the configuration of a [Mamba2Model](/docs/transformers/pr_33962/en/model_doc/mamba2#transformers.Mamba2Model). It is used to instantiate a MAMBA2 | |
| model according to the specified arguments, defining the model architecture. Instantiating a configuration with the | |
| defaults will yield a similar configuration to that of the MAMBA2 | |
| [state-spaces/mamba2-2.8b](https://huggingface.co/state-spaces/mamba2-2.8b) architecture. | |
| Configuration objects inherit from [PreTrainedConfig](/docs/transformers/pr_33962/en/main_classes/configuration#transformers.PreTrainedConfig) and can be used to control the model outputs. Read the | |
| documentation from [PreTrainedConfig](/docs/transformers/pr_33962/en/main_classes/configuration#transformers.PreTrainedConfig) for more information. | |
| <ExampleCodeBlock anchor="transformers.Mamba2Config.example"> | |
| Example: | |
| ```python | |
| >>> from transformers import Mamba2Config, Mamba2Model | |
| >>> # Initializing a Mamba2 configuration | |
| >>> configuration = Mamba2Config() | |
| >>> # Initializing a model (with random weights) from the configuration | |
| >>> model = Mamba2Model(configuration) | |
| >>> # Accessing the model configuration | |
| >>> configuration = model.config | |
| ``` | |
| </ExampleCodeBlock> | |
| </div> | |
| ## Mamba2Model[[transformers.Mamba2Model]] | |
| <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"> | |
| <docstring><name>class transformers.Mamba2Model</name><anchor>transformers.Mamba2Model</anchor><source>https://github.com/huggingface/transformers/blob/vr_33962/src/transformers/models/mamba2/modeling_mamba2.py#L820</source><parameters>[{"name": "config", "val": ""}]</parameters><paramsdesc>- **config** ([Mamba2Model](/docs/transformers/pr_33962/en/model_doc/mamba2#transformers.Mamba2Model)) -- | |
| Model configuration class with all the parameters of the model. Initializing with a config file does not | |
| load the weights associated with the model, only the configuration. Check out the | |
| [from_pretrained()](/docs/transformers/pr_33962/en/main_classes/model#transformers.PreTrainedModel.from_pretrained) method to load the model weights.</paramsdesc><paramgroups>0</paramgroups></docstring> | |
| The bare Mamba2 Model outputting raw hidden-states without any specific head on top. | |
| This model inherits from [PreTrainedModel](/docs/transformers/pr_33962/en/main_classes/model#transformers.PreTrainedModel). Check the superclass documentation for the generic methods the | |
| library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads | |
| etc.) | |
| This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. | |
| Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage | |
| and behavior. | |
| <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"> | |
| <docstring><name>forward</name><anchor>transformers.Mamba2Model.forward</anchor><source>https://github.com/huggingface/transformers/blob/vr_33962/src/transformers/models/mamba2/modeling_mamba2.py#L845</source><parameters>[{"name": "input_ids", "val": ": typing.Optional[torch.LongTensor] = None"}, {"name": "inputs_embeds", "val": ": typing.Optional[torch.LongTensor] = None"}, {"name": "cache_params", "val": ": typing.Optional[transformers.models.mamba2.modeling_mamba2.Mamba2Cache] = None"}, {"name": "use_cache", "val": ": typing.Optional[bool] = None"}, {"name": "output_hidden_states", "val": ": typing.Optional[bool] = None"}, {"name": "return_dict", "val": ": typing.Optional[bool] = None"}, {"name": "cache_position", "val": ": typing.Optional[torch.LongTensor] = None"}, {"name": "attention_mask", "val": ": typing.Optional[torch.Tensor] = None"}, {"name": "**kwargs", "val": ""}]</parameters><paramsdesc>- **input_ids** (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*) -- | |
| Indices of input sequence tokens in the vocabulary. Padding will be ignored by default. | |
| Indices can be obtained using [AutoTokenizer](/docs/transformers/pr_33962/en/model_doc/auto#transformers.AutoTokenizer). See [PreTrainedTokenizer.encode()](/docs/transformers/pr_33962/en/internal/tokenization_utils#transformers.PreTrainedTokenizerBase.encode) and | |
| [PreTrainedTokenizer.__call__()](/docs/transformers/pr_33962/en/internal/tokenization_utils#transformers.PreTrainedTokenizerBase.__call__) for details. | |
| [What are input IDs?](../glossary#input-ids) | |
| - **inputs_embeds** (`torch.LongTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*) -- | |
| Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This | |
| is useful if you want more control over how to convert `input_ids` indices into associated vectors than the | |
| model's internal embedding lookup matrix. | |
| - **cache_params** (`Mamba2Cache`, *optional*) -- | |
| If passed along, the model uses the previous state in all the blocks (which will give the output for the | |
| `input_ids` provided as if the model add `state_input_ids + input_ids` as context). | |
| - **use_cache** (`bool`, *optional*) -- | |
| If set to `True`, the `cache_params` is returned and can be used to quickly generate the next logits. | |
| - **output_hidden_states** (`bool`, *optional*) -- | |
| Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for | |
| more detail. | |
| - **return_dict** (`bool`, *optional*) -- | |
| Whether or not to return a [ModelOutput](/docs/transformers/pr_33962/en/main_classes/output#transformers.utils.ModelOutput) instead of a plain tuple. | |
| - **cache_position** (`torch.LongTensor` of shape `(batch_size,)`, *optional*) -- | |
| The position of the current input in the cache. This is used to ensure that the cache is correctly updated. | |
| If `cache_params` is passed, `cache_position` should also be passed. | |
| - **attention_mask** (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*) -- | |
| Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`: | |
| - 1 for tokens that are **not masked**, | |
| - 0 for tokens that are **masked**. | |
| [What are attention masks?](../glossary#attention-mask)</paramsdesc><paramgroups>0</paramgroups><rettype>`transformers.models.mamba2.modeling_mamba2.Mamba2Output` or `tuple(torch.FloatTensor)`</rettype><retdesc>A `transformers.models.mamba2.modeling_mamba2.Mamba2Output` or a tuple of | |
| `torch.FloatTensor` (if `return_dict=False` is passed or when `config.return_dict=False`) comprising various | |
| elements depending on the configuration ([Mamba2Config](/docs/transformers/pr_33962/en/model_doc/mamba2#transformers.Mamba2Config)) and inputs. | |
| - **last_hidden_state** (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*, defaults to `None`) -- Sequence of hidden-states at the output of the last layer of the model. | |
| - **cache_params** (`~models.mamba2.modeling_mamba2.Mamba2Cache`, *optional*, defaults to `None`) -- The state of the model at the last time step. Can be used in a forward method with the next `input_ids` to | |
| avoid providing the old `input_ids`. | |
| Includes both the State space model state matrices after the selective scan, and the Convolutional states | |
| - **hidden_states** (`tuple[torch.FloatTensor]`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`) -- Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, + | |
| one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`. | |
| Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.</retdesc></docstring> | |
| The [Mamba2Model](/docs/transformers/pr_33962/en/model_doc/mamba2#transformers.Mamba2Model) forward method, overrides the `__call__` special method. | |
| <Tip> | |
| Although the recipe for forward pass needs to be defined within this function, one should call the `Module` | |
| instance afterwards instead of this since the former takes care of running the pre and post processing steps while | |
| the latter silently ignores them. | |
| </Tip> | |
| </div></div> | |
| ## Mamba2LMHeadModel[[transformers.Mamba2ForCausalLM]] | |
| <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"> | |
| <docstring><name>class transformers.Mamba2ForCausalLM</name><anchor>transformers.Mamba2ForCausalLM</anchor><source>https://github.com/huggingface/transformers/blob/vr_33962/src/transformers/models/mamba2/modeling_mamba2.py#L935</source><parameters>[{"name": "config", "val": ""}]</parameters><paramsdesc>- **config** ([Mamba2ForCausalLM](/docs/transformers/pr_33962/en/model_doc/mamba2#transformers.Mamba2ForCausalLM)) -- | |
| Model configuration class with all the parameters of the model. Initializing with a config file does not | |
| load the weights associated with the model, only the configuration. Check out the | |
| [from_pretrained()](/docs/transformers/pr_33962/en/main_classes/model#transformers.PreTrainedModel.from_pretrained) method to load the model weights.</paramsdesc><paramgroups>0</paramgroups></docstring> | |
| The MAMBA2 Model transformer with a language modeling head on top (linear layer with weights not tied to the input | |
| embeddings). | |
| This model inherits from [PreTrainedModel](/docs/transformers/pr_33962/en/main_classes/model#transformers.PreTrainedModel). Check the superclass documentation for the generic methods the | |
| library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads | |
| etc.) | |
| This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. | |
| Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage | |
| and behavior. | |
| <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"> | |
| <docstring><name>forward</name><anchor>transformers.Mamba2ForCausalLM.forward</anchor><source>https://github.com/huggingface/transformers/blob/vr_33962/src/transformers/models/mamba2/modeling_mamba2.py#L999</source><parameters>[{"name": "input_ids", "val": ": typing.Optional[torch.LongTensor] = None"}, {"name": "inputs_embeds", "val": ": typing.Optional[torch.FloatTensor] = None"}, {"name": "cache_params", "val": ": typing.Optional[transformers.models.mamba2.modeling_mamba2.Mamba2Cache] = None"}, {"name": "labels", "val": ": typing.Optional[torch.LongTensor] = None"}, {"name": "output_hidden_states", "val": ": typing.Optional[bool] = None"}, {"name": "return_dict", "val": ": typing.Optional[bool] = None"}, {"name": "use_cache", "val": ": typing.Optional[bool] = None"}, {"name": "cache_position", "val": ": typing.Optional[torch.Tensor] = None"}, {"name": "attention_mask", "val": ": typing.Optional[torch.Tensor] = None"}, {"name": "**kwargs", "val": ""}]</parameters><paramsdesc>- **input_ids** (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*) -- | |
| Indices of input sequence tokens in the vocabulary. Padding will be ignored by default. | |
| Indices can be obtained using [AutoTokenizer](/docs/transformers/pr_33962/en/model_doc/auto#transformers.AutoTokenizer). See [PreTrainedTokenizer.encode()](/docs/transformers/pr_33962/en/internal/tokenization_utils#transformers.PreTrainedTokenizerBase.encode) and | |
| [PreTrainedTokenizer.__call__()](/docs/transformers/pr_33962/en/internal/tokenization_utils#transformers.PreTrainedTokenizerBase.__call__) for details. | |
| [What are input IDs?](../glossary#input-ids) | |
| - **inputs_embeds** (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*) -- | |
| Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This | |
| is useful if you want more control over how to convert `input_ids` indices into associated vectors than the | |
| model's internal embedding lookup matrix. | |
| - **cache_params** (`Mamba2Cache`, *optional*) -- | |
| If passed along, the model uses the previous state in all the blocks (which will give the output for the | |
| `input_ids` provided as if the model add `state_input_ids + input_ids` as context). | |
| - **labels** (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*) -- | |
| Labels for language modeling. Note that the labels **are shifted** inside the model, i.e. you can set | |
| `labels = input_ids` Indices are selected in `[-100, 0, ..., config.vocab_size]` All labels set to `-100` | |
| are ignored (masked), the loss is only computed for labels in `[0, ..., config.vocab_size]` | |
| - **output_hidden_states** (`bool`, *optional*) -- | |
| Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for | |
| more detail. | |
| - **return_dict** (`bool`, *optional*) -- | |
| Whether or not to return a [ModelOutput](/docs/transformers/pr_33962/en/main_classes/output#transformers.utils.ModelOutput) instead of a plain tuple. | |
| - **use_cache** (`bool`, *optional*) -- | |
| If set to `True`, the `cache_params` is returned and can be used to quickly generate the next logits. | |
| - **cache_position** (`torch.LongTensor` of shape `(batch_size,)`, *optional*) -- | |
| The position of the current input in the cache. This is used to ensure that the cache is correctly updated. | |
| If `cache_params` is passed, `cache_position` should also be passed. | |
| - **attention_mask** (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*) -- | |
| Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`: | |
| - 1 for tokens that are **not masked**, | |
| - 0 for tokens that are **masked**. | |
| [What are attention masks?](../glossary#attention-mask)</paramsdesc><paramgroups>0</paramgroups><rettype>`transformers.models.mamba2.modeling_mamba2.Mamba2CausalLMOutput` or `tuple(torch.FloatTensor)`</rettype><retdesc>A `transformers.models.mamba2.modeling_mamba2.Mamba2CausalLMOutput` or a tuple of | |
| `torch.FloatTensor` (if `return_dict=False` is passed or when `config.return_dict=False`) comprising various | |
| elements depending on the configuration ([Mamba2Config](/docs/transformers/pr_33962/en/model_doc/mamba2#transformers.Mamba2Config)) and inputs. | |
| - **loss** (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `labels` is provided) -- Language modeling loss (for next-token prediction). | |
| - **logits** (`torch.FloatTensor` of shape `(batch_size, sequence_length, config.vocab_size)`) -- Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax). | |
| - **cache_params** (`~models.mamba2.modeling_mamba2.Mamba2Cache`, *optional*, defaults to `None`) -- The state of the model at the last time step. Can be used in a forward method with the next `input_ids` to | |
| avoid providing the old `input_ids`. | |
| Includes both the State space model state matrices after the selective scan, and the Convolutional states | |
| - **hidden_states** (`tuple[torch.FloatTensor]`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`) -- Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, + | |
| one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`. | |
| Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.</retdesc></docstring> | |
| The [Mamba2ForCausalLM](/docs/transformers/pr_33962/en/model_doc/mamba2#transformers.Mamba2ForCausalLM) forward method, overrides the `__call__` special method. | |
| <Tip> | |
| Although the recipe for forward pass needs to be defined within this function, one should call the `Module` | |
| instance afterwards instead of this since the former takes care of running the pre and post processing steps while | |
| the latter silently ignores them. | |
| </Tip> | |
| <ExampleCodeBlock anchor="transformers.Mamba2ForCausalLM.forward.example"> | |
| Example: | |
| ```python | |
| ``` | |
| </ExampleCodeBlock> | |
| </div></div> | |
| <EditOnGithub source="https://github.com/huggingface/transformers/blob/main/docs/source/en/model_doc/mamba2.md" /> |
Xet Storage Details
- Size:
- 27 kB
- Xet hash:
- 15b58cfdd3016630531cab810fdc6e1580a4efda7a68ed9d851a9f762719c12a
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.