Mamba
This model was released on 2023-12-01 and added to Hugging Face Transformers on 2024-03-05.
Mamba is a selective structured state space model (SSMs) designed to work around Transformers computational inefficiency when dealing with long sequences. It is a completely attention-free architecture, and comprised of a combination of H3 and gated MLP blocks (Mamba block). Mamba’s “content-based reasoning” allows it to focus on specific parts of an input depending on the current token. Mamba also uses a new hardware-aware parallel algorithm to compensate for the lack of convolutional operations. As a result, Mamba has fast inference and can scale to very long sequences.
You can find all the original Mamba checkpoints under the State Space Models organization.
The example below demonstrates how to generate text with Pipeline, AutoModel, and from the command line.
import torchfrom transformers import pipeline
pipeline = pipeline( task="text-generation", model="state-spaces/mamba-130m-hf", dtype=torch.float16, device=0)pipeline("Plants create energy through a process known as")import torchfrom transformers import AutoModelForCausalLM, AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("state-spaces/mamba-130m-hf")model = AutoModelForCausalLM.from_pretrained("state-spaces/mamba-130m-hf", dtype=torch.float16, 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)echo -e "Plants create energy through a process known as" | transformers run --task text-generation --model state-spaces/mamba-130m-hf --device 0Quantization reduces the memory burden of large models by representing the weights in a lower precision. Refer to the Quantization overview for more available quantization backends.
The example below uses torchao to only quantize the weights to 4-bit integers.
import torchfrom transformers import AutoModelForCausalLM, AutoTokenizer, TorchAoConfigfrom torchao.quantization import Int4WeightOnlyConfig
quantization_config = Int4WeightOnlyConfig(group_size=128)quantization_config = TorchAoConfig(quant_type=quant_config)tokenizer = AutoTokenizer.from_pretrained("state-spaces/mamba-2.8b-hf")model = AutoModelForCausalLM.from_pretrained("state-spaces/mamba-2.8b-hf", 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))-
The current implementation uses the original CUDA kernels. The FlashAttention equivalent implementation is hosted in the mamba-ssm and causal_conv1d repositories. Make sure to install them if your hardware supports it!
-
Mamba stacks
mixerlayers which are equivalent toAttentionlayers. You can find the main logic of Mamba in theMambaMixerclass. -
The example below demonstrates how to fine-tune Mamba with PEFT.
from datasets import load_datasetfrom trl import SFTConfig, SFTTrainerfrom peft import LoraConfigmodel_id = "state-spaces/mamba-130m-hf"dataset = load_dataset("Abirate/english_quotes", split="train")training_args = SFTConfig(dataset_text_field="quote")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()
MambaCache
Section titled “MambaCache”[[autodoc]] MambaCache - update_conv_state - update_ssm_state - reset
MambaConfig
Section titled “MambaConfig”[[autodoc]] MambaConfig
MambaModel
Section titled “MambaModel”[[autodoc]] MambaModel - forward
MambaLMHeadModel
Section titled “MambaLMHeadModel”[[autodoc]] MambaForCausalLM - forward