Motit commited on
Commit
14ea3d6
·
verified ·
1 Parent(s): e97ce23

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +492 -5
README.md CHANGED
@@ -1,5 +1,492 @@
1
- ---
2
- license: other
3
- license_name: jamba-open-model-license
4
- license_link: https://www.ai21.com/jamba-open-model-license/
5
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: other
3
+ license_name: jamba-open-model-license
4
+ license_link: https://www.ai21.com/jamba-open-model-license/
5
+ ---
6
+ # Model Information
7
+
8
+ The AI21 Jamba 1.6 family of models is state-of-the-art, hybrid SSM-Transformer instruction following foundation models. The Jamba models are the most powerful & efficient long-context models on the market, which deliver up to 2.5X faster inference than leading models of comparable sizes.
9
+
10
+ The models demonstrate superior long context handling, speed, and quality. They mark the first time a non-Transformer model has been successfully scaled to the quality and strength of the market’s leading models.
11
+
12
+ [Jamba 1.6 Mini](https://huggingface.co/ai21labs/AI21-Jamba-1.6-Mini) (12B active/52B total) and [Jamba 1.6 Large](https://huggingface.co/ai21labs/AI21-Jamba-1.5-Large) (94B active/398B total) are also optimized for business use cases and capabilities such as function calling, structured output (JSON), and grounded generation.
13
+
14
+ The models are released under the [Jamba Open Model License](https://www.ai21.com/licenses/jamba-open-model-license), a permissive license allowing full research use and commercial use under the license terms. If you need to license the model for your needs, [talk to us](https://www.ai21.com/talk-to-us).
15
+
16
+ For more details of this model, see the white paper and the release [blog post](https://www.ai21.com/blog/announcing-jamba-model-family).
17
+
18
+ ## Model Details
19
+
20
+ - **Developed by:** [AI21](https://www.ai21.com)
21
+ - **Model type:** Joint Attention and Mamba (Jamba)
22
+ - **License:** [Jamba Open Model License](https://www.ai21.com/licenses/jamba-open-model-license)
23
+ - **Context length:** 256K
24
+ - **Knowledge cutoff date:** March 5, 2024
25
+ - **Supported languages:** English, Spanish, French, Portuguese, Italian, Dutch, German, Arabic and Hebrew
26
+
27
+
28
+ ## Results on common benchmarks
29
+
30
+ ### RULER Benchmark - Effective context length
31
+
32
+ |Models|Claimed Length|Effective Length|4K|8K|16K|32K|64K|128K|256K|
33
+ |:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|
34
+ Jamba 1.6 Large (94B/398B)|256K|256K|<ins>96.7</ins>|<ins>96.6</ins>|<ins>96.4</ins>|<ins>96.0</ins>|<ins>95.4</ins>|<ins>95.1</ins>|<ins>93.9</ins>|
35
+ Jamba 1.6 Mini (12B/52B)|256K|256K|<ins>95.7</ins>|<ins>95.2</ins>|<ins>94.7</ins>|<ins>93.8</ins>|<ins>92.7</ins>|<ins>89.8</ins>|<ins>86.1</ins> |
36
+ Gemini 1.5 Pro|1M|>128K|<ins>96.7</ins>|<ins>95.8</ins>|<ins>96.0</ins>|<ins>95.9</ins>|<ins>95.9</ins>|<ins>94.4</ins>| -- |
37
+ GPT-4 1106-preview |128K|64K|<ins>96.6</ins>|<ins>96.3</ins>|<ins>95.2</ins>|<ins>93.2</ins>|<ins>87.0</ins>|81.2| -- |
38
+ Llama 3.1 70B|128K|64K|<ins>96.5</ins>|<ins>95.8</ins>|<ins>95.4</ins>|<ins>94.8</ins>|<ins>88.4</ins>|66.6| -- |
39
+ Command R-plus (104B)|128K|32K|<ins>95.6</ins>|<ins>95.2</ins>|<ins>94.2</ins>|<ins>92.0</ins>|84.3|63.1| -- |
40
+ Llama 3.1 8B|128K|32K|<ins>95.5</ins>|<ins>93.8</ins>|<ins>91.6</ins>|<ins>87.4</ins>|84.7|77.0| -- |
41
+ Mistral Large 2 (123B)|128K|32K|<ins>96.2</ins>|<ins>96.1</ins>|<ins>95.1</ins>|<ins>93.0</ins>|78.8|23.7| -- |
42
+ Mixtral 8x22B (39B/141B)|64K|32K|<ins>95.6</ins>|<ins>94.9</ins>|<ins>93.4</ins>|<ins>90.9</ins>|84.7|31.7| -- |
43
+ Mixtral 8x7B (12.9B/46.7B)|32K|32K|<ins>94.9</ins>|<ins>92.1</ins>|<ins>92.5</ins>|<ins>85.9</ins>|72.4|44.5| -- |
44
+
45
+ # Usage
46
+ ## Prerequisites
47
+
48
+ In order to run optimized Mamba implementations, you first need to install `mamba-ssm` and `causal-conv1d`:
49
+ ```bash
50
+ pip install mamba-ssm causal-conv1d>=1.2.0
51
+ ```
52
+ You also have to have the model on a CUDA device.
53
+
54
+
55
+ ## Run the model with vLLM
56
+
57
+ The recommended way to perform efficient inference with Jamba 1.6 Mini is using [vLLM](https://docs.vllm.ai/en/latest/). First, make sure to install vLLM (version 0.5.4 or higher is required)
58
+ ```bash
59
+ pip install vllm>=0.5.4
60
+ ```
61
+
62
+ In the example below, `number_gpus` should match the number of GPUs you want to deploy Jamba 1.6 Mini on. A minimum of 2 80GB GPUs is required.
63
+
64
+ ```python
65
+ from vllm import LLM, SamplingParams
66
+ from transformers import AutoTokenizer
67
+
68
+ model = "ai21labs/AI21-Jamba-1.6-Mini"
69
+ number_gpus = 2
70
+
71
+ llm = LLM(model=model,
72
+ max_model_len=200*1024,
73
+ tensor_parallel_size=number_gpus)
74
+
75
+ tokenizer = AutoTokenizer.from_pretrained(model)
76
+
77
+ messages = [
78
+ {"role": "system", "content": "You are an ancient oracle who speaks in cryptic but wise phrases, always hinting at deeper meanings."},
79
+ {"role": "user", "content": "Hello!"},
80
+ ]
81
+
82
+ prompts = tokenizer.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
83
+
84
+ sampling_params = SamplingParams(temperature=0.4, top_p=0.95, max_tokens=100)
85
+ outputs = llm.generate(prompts, sampling_params)
86
+
87
+ generated_text = outputs[0].outputs[0].text
88
+ print(generated_text)
89
+ #Output: Seek and you shall find. The path is winding, but the journey is enlightening. What wisdom do you seek from the ancient echoes?
90
+ ```
91
+
92
+ With the default BF16 precision on 2 80GB A100 GPUs and default vLLM configuration, you'll be able to perform inference on prompts up to 200K tokens long. On more than 2 80GB GPUs, you can easily fit the full 256K context.
93
+
94
+ <u>Note:</u> vLLM's `main` branch has some memory utilization improvements specific to the Jamba architecture that allow using the full 256K context length on 2 80 GPUs. You can [build vLLM from source](https://docs.vllm.ai/en/latest/getting_started/installation.html#build-from-source) if you wish to make use of them.
95
+
96
+ ### ExpertsInt8 quantization
97
+ We've developed an innovative and efficient quantization technique, [ExpertsInt8](https://www.ai21.com/blog/announcing-jamba-model-family#:~:text=Like%20all%20models%20in%20its%20size%20class%2C%20Jamba%201.5%20Large%20can%E2%80%99t%20be%20loaded%20in%20full%20(FP32)%20or%20half%20(FP16/BF16)%20precision%20on%20a%20single%20node%20of%208%20GPUs.%20Dissatisfied%20with%20currently%20available%20quantization%20techniques%2C%20we%20developed%20ExpertsInt8%2C%20a%20novel%20quantization%20technique%20tailored%20for%20MoE%20models.), designed for MoE models deployed in vLLM, including Jamba models. Using it, you'll be able to deploy Jamba 1.5 Mini on a single 80GB GPU.
98
+
99
+ In order to use ExpertsInt8, you need to use vllm version 0.5.5 or higher: `pip install vllm>=0.5.5`
100
+
101
+ With default vLLM configuration, you can fit prompts up to 100K on a single 80GB A100 GPU:
102
+ ```python
103
+ import os
104
+ os.environ['VLLM_FUSED_MOE_CHUNK_SIZE']='32768' # This is a workaround a bug in vLLM's fused_moe kernel
105
+
106
+ from vllm import LLM
107
+ llm = LLM(model="ai21labs/AI21-Jamba-1.6-Mini",
108
+ max_model_len=100*1024,
109
+ quantization="experts_int8")
110
+ ```
111
+
112
+
113
+ ## Run the model with `transformers`
114
+
115
+ The following example loads Jamba 1.6 Mini to the GPU in BF16 precision, uses optimized [FlashAttention2](https://github.com/Dao-AILab/flash-attention) and Mamba kernels, and parallelizes the model across multiple GPUs using [accelerate](https://huggingface.co/docs/accelerate/index). Note that in half precision (FP16/BF16), Jamba 1.5 Mini is too large to fit on a single 80GB GPU, so you'll need at least 2 such GPUs.
116
+
117
+ ```python
118
+ import torch
119
+ from transformers import AutoModelForCausalLM, AutoTokenizer
120
+
121
+ model = AutoModelForCausalLM.from_pretrained("ai21labs/AI21-Jamba-1.6-Mini",
122
+ torch_dtype=torch.bfloat16,
123
+ attn_implementation="flash_attention_2",
124
+ device_map="auto")
125
+
126
+ tokenizer = AutoTokenizer.from_pretrained("ai21labs/AI21-Jamba-1.6-Mini")
127
+
128
+ messages = [
129
+ {"role": "system", "content": "You are an ancient oracle who speaks in cryptic but wise phrases, always hinting at deeper meanings."},
130
+ {"role": "user", "content": "Hello!"},
131
+ ]
132
+
133
+ input_ids = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors='pt').to(model.device)
134
+
135
+ outputs = model.generate(input_ids, max_new_tokens=216)
136
+
137
+ # Decode the output
138
+ conversation = tokenizer.decode(outputs[0], skip_special_tokens=True)
139
+
140
+ # Split the conversation to get only the assistant's response
141
+ assistant_response = conversation.split(messages[-1]['content'])[1].strip()
142
+ print(assistant_response)
143
+ # Output: Seek and you shall find. The path is winding, but the journey is enlightening. What wisdom do you seek from the ancient echoes?
144
+ ```
145
+
146
+ <u>Note:</u> Versions 4.44.0 and 4.44.1 of `transformers` have a bug that restricts the ability to run the Jamba architecture. Make sure you're not using these versions.
147
+
148
+ <u>Note:</u> If you're having trouble installing `mamba-ssm` and `causal-conv1d` for the optimized Mamba kernels, you can run Jamba 1.5 Mini without them, at the cost of extra latency. In order to do that, add the kwarg `use_mamba_kernels=False` when loading the model via `AutoModelForCausalLM.from_pretained()`.
149
+
150
+ <details><summary><strong>Load the model in 8-bit</strong></summary>
151
+
152
+ **Using 8-bit precision, it is possible to fit up to 140K sequence length on a single 80GB GPU.** You can easily quantize the model to 8-bit using [bitsandbytes](https://huggingface.co/docs/bitsandbytes/index). In order to not degrade model quality, we recommend to exclude the Mamba blocks from the quantization:
153
+
154
+ ```python
155
+ from transformers import AutoModelForCausalLM, BitsAndBytesConfig
156
+ quantization_config = BitsAndBytesConfig(load_in_8bit=True,
157
+ llm_int8_skip_modules=["mamba"])
158
+ model = AutoModelForCausalLM.from_pretrained("ai21labs/AI21-Jamba-1.6-Mini",
159
+ torch_dtype=torch.bfloat16,
160
+ attn_implementation="flash_attention_2",
161
+ quantization_config=quantization_config)
162
+ ```
163
+
164
+ </details>
165
+
166
+ <details><summary><strong>Load the model on CPU</strong></summary>
167
+
168
+ If you don't have access to a GPU, you can also load and run Jamba 1.6 Mini on a CPU. Note this will result in poor inference performance.
169
+
170
+ ```python
171
+ from transformers import AutoModelForCausalLM
172
+ model = AutoModelForCausalLM.from_pretrained("ai21labs/AI21-Jamba-1.6-Mini",
173
+ use_mamba_kernels=False)
174
+ ```
175
+ </details>
176
+ <br>
177
+ <br>
178
+
179
+ # Model features
180
+
181
+ ## Tool use with Jamba
182
+ Jamba 1.6 supports tool use capabilities in accordance with Huggingface's tool use API. The tools defined by the user are inserted into a dedicated section in the chat template which the model was trained to recognize.
183
+
184
+ Given a conversation that contains tools, the model can output content, tool invocations or both.
185
+
186
+ <details><summary><strong>Tool usage example</strong></summary>
187
+
188
+
189
+ ```python
190
+ from transformers import AutoTokenizer
191
+
192
+ tokenizer = AutoTokenizer.from_pretrained("ai21labs/AI21-Jamba-1.6-Mini")
193
+
194
+ messages = [
195
+ {
196
+ "role": "user",
197
+ "content": "What's the weather like right now in Jerusalem and in London?"
198
+ }
199
+ ]
200
+
201
+ tools = [
202
+ {
203
+ 'type': 'function',
204
+ 'function': {
205
+ 'name': 'get_current_weather',
206
+ 'description': 'Get the current weather',
207
+ 'parameters': {
208
+ 'type': 'object',
209
+ 'properties': {
210
+ 'location': {'type': 'string', 'description': 'The city and state, e.g. San Francisco, CA'},
211
+ 'format': {'type': 'string', 'enum': ['celsius', 'fahrenheit'], 'description': 'The temperature unit to use. Infer this from the users location.'}
212
+ },
213
+ 'required': ['location', 'format']
214
+ }
215
+ }
216
+ }
217
+ ]
218
+
219
+ prompt = tokenizer.apply_chat_template(
220
+ messages,
221
+ tools=tools,
222
+ tokenize=False,
223
+ )
224
+ ```
225
+ Output:
226
+ ```
227
+ <tool_calls>[
228
+ {"name": "get_current_weather", "arguments": {"location": "Jerusalem", "format": "celsius"}},
229
+ {"name": "get_current_weather", "arguments": {"location": "celsius", "format": "celsius"}}
230
+ ]</tool_calls>
231
+ ```
232
+
233
+ </details>
234
+
235
+
236
+ <details><summary><strong>Feeding back tool responses into the model</strong></summary>
237
+
238
+ Now that the model has called the tools, we need to feed the tool responses back to the model. In the next call, send the assistant message with the `tool_messages` field, as shown below, along with additional `tool` messages (in the corresponding order) that contain the tool outputs.
239
+
240
+ The `arguments` field for each tool call can be either a dict or a JSON string.
241
+
242
+ ```python
243
+ from transformers import AutoTokenizer
244
+
245
+ tokenizer = AutoTokenizer.from_pretrained("ai21labs/AI21-Jamba-1.6-Mini")
246
+
247
+ # Note that you must send the tool responses in the same order as the model called the tools:
248
+ messages = [
249
+ {
250
+ "role": "user",
251
+ "content": "What's the weather like right now in Jerusalem and in London?"
252
+ },
253
+ {
254
+ "role": "assistant",
255
+ "content": null,
256
+ "tool_calls": [
257
+ {
258
+ "name": "get_current_weather",
259
+ "arguments": "{\"location\": \"Jerusalem\", \"format\": \"celsius\"}"
260
+ },
261
+ {
262
+ "name": "get_current_weather",
263
+ "arguments": "{\"location\": \"London\", \"format\": \"celsius\"}"
264
+ }
265
+ ]
266
+ },
267
+ {
268
+ "role": "tool",
269
+ "content": "The weather in Jerusalem is 18 degrees celsius."
270
+ },
271
+ {
272
+ "role": "tool",
273
+ "content": "The weather in London is 8 degrees celsius."
274
+ }
275
+ ]
276
+
277
+ tool_use_prompt = tokenizer.apply_chat_template(
278
+ messages,
279
+ tools=tools,
280
+ tokenize=False,
281
+ )
282
+ ```
283
+ example output:
284
+ ```
285
+ The weather in Jerusalem is currently 18 degrees Celsius. In London, it is 8 degrees Celsius.
286
+ ```
287
+
288
+ </details>
289
+
290
+ ## Grounded Generation with Jamba:
291
+
292
+ A common use-case for LLMs is grounded generation and RAG, where the model is required to answer a question or follow an instruction based on a given set of documents or document snippets. To standardize this process, Jamba was trained with a specific "documents" section in its chat template. The model was trained to attend to this section, and grounded generation tasks show improved performance when the task is formatted in this way.
293
+
294
+ Similar to tools, which are given as an external argument to the model in addition to the conversation, documents are provided in a similar way. To support document-level metadata, a document is defined as a dictionary with key-values of your choosing. These are formatted within the chat template. Two keys that get special treatment are "title", which is formatted at the top of the document if present, and "text" which is a required field and defines the actual text of the document.
295
+
296
+ <details><summary><strong>Ataching documents to Jamba 1.6 prompt</strong></summary>
297
+
298
+ ```python
299
+ from transformers import AutoTokenizer
300
+
301
+ tokenizer = AutoTokenizer.from_pretrained("ai21labs/AI21-Jamba-1.6-Mini")
302
+
303
+ messages = [
304
+ {
305
+ "role": "user",
306
+ "content": "Who wrote Harry Potter?"
307
+ }
308
+ ]
309
+
310
+ documents = [
311
+ {
312
+ "text": "Harry Potter is a series of seven fantasy novels written by British author J. K. Rowling.",
313
+ "title": "Harry Potter"
314
+ },
315
+ {
316
+ "text": "The Great Gatsby is a novel by American writer F. Scott Fitzgerald.",
317
+ "title": "The Great Gatsby",
318
+ "country": "United States",
319
+ "genre": "Novel"
320
+
321
+ }
322
+ ]
323
+
324
+ prompt = tokenizer.apply_chat_template(
325
+ messages,
326
+ documents=documents,
327
+ tokenize=False,
328
+ )
329
+
330
+ # Output: J. K. Rowling
331
+
332
+ ```
333
+
334
+ </details>
335
+
336
+ ## JSON mode
337
+ Jamba 1.6 was trained with specific “knobs”, which help steer the model towards commonly requested behaviors. Each behavior is enabled by including specific pre-defined text in the system message. For ease of use, we've included them as flags in Jamba 1.5's chat template, so they can be toggled by passing appropriate arguments to the chat template.
338
+
339
+ Jamba 1.6 was trained to produce valid JSONs when requested to. It does so naturally, but when the JSON mode knob is activated the likelihood of a valid json increases considerably. In JSON mode, Jamba 1.5 will attempt to output a valid JSON regardless of the user request. However, it is highly recommended to specify information about the expected json schema in the user request or system message to get the best results, as shown in the example below.
340
+
341
+ <details><summary><strong>Usage of JSON knob in Jamba 1.6</strong></summary>
342
+
343
+ ```python
344
+ from transformers import AutoTokenizer
345
+
346
+ tokenizer = AutoTokenizer.from_pretrained("ai21labs/AI21-Jamba-1.6-Mini")
347
+ messages = [
348
+ {'role':'user',
349
+ 'content':'Describe the first American president. Include year of birth (number) and name (string).'}
350
+ ]
351
+ prompt = tokenizer.apply_chat_template(messages,
352
+ tokenize=False,
353
+ add_generation_prompt=False,
354
+
355
+ #Output: "{ "year of birth": 1732, "name": "George Washington." }"
356
+ ```
357
+
358
+ </details>
359
+
360
+
361
+ ## Fine-tuning examples
362
+
363
+ The examples below use the `SFTTrainer` from [huggingface/trl](https://github.com/huggingface/trl), so ensure it's installed:
364
+ ```bash
365
+ pip install trl
366
+ ```
367
+
368
+ ## Full Fine-tuning example
369
+ To train a full finetune using AWS multi nodes and FSDP configuration, follow the instructions here [hf-finetune-sagemaker](https://github.com/AI21Labs/hf-finetune-sagemaker)
370
+
371
+ ## LoRA example
372
+
373
+ Here is an example of fine-tuning with LoRA PEFT, in bfloat16 (requires ~130GB GPU RAM, so e.g. 2xA100 80GB):
374
+
375
+ ```python
376
+ import torch
377
+ from transformers import AutoTokenizer, AutoModelForCausalLM
378
+ from datasets import load_dataset
379
+ from trl import SFTTrainer, SFTConfig
380
+ from peft import LoraConfig
381
+
382
+ tokenizer = AutoTokenizer.from_pretrained("ai21labs/AI21-Jamba-1.6-Mini")
383
+ model = AutoModelForCausalLM.from_pretrained(
384
+ "ai21labs/AI21-Jamba-1.6-Mini",
385
+ device_map="auto",
386
+ torch_dtype=torch.bfloat16,
387
+ attn_implementation="flash_attention_2",
388
+ )
389
+
390
+ lora_config = LoraConfig(
391
+ r=8,
392
+ target_modules=[
393
+ "embed_tokens",
394
+ "x_proj", "in_proj", "out_proj", # mamba
395
+ "gate_proj", "up_proj", "down_proj", # mlp
396
+ "q_proj", "k_proj", "v_proj", "o_proj", # attention
397
+ ],
398
+ task_type="CAUSAL_LM",
399
+ bias="none",
400
+ )
401
+
402
+ dataset = load_dataset("philschmid/dolly-15k-oai-style", split="train")
403
+ training_args = SFTConfig(
404
+ output_dir="/dev/shm/results",
405
+ logging_dir="./logs",
406
+ num_train_epochs=2,
407
+ per_device_train_batch_size=4,
408
+ learning_rate=1e-5,
409
+ logging_steps=10,
410
+ gradient_checkpointing=True,
411
+ max_seq_length=4096,
412
+ save_steps=100,
413
+ )
414
+ trainer = SFTTrainer(
415
+ model=model,
416
+ tokenizer=tokenizer,
417
+ args=training_args,
418
+ peft_config=lora_config,
419
+ train_dataset=dataset,
420
+ )
421
+ trainer.train()
422
+ ```
423
+
424
+ Note that the dataset in the example uses conversational format (with `messages` column), so `SFTTrainer` automatically applies Jamba's chat-template as explained in [TRL docs](https://huggingface.co/docs/trl/main/en/sft_trainer#dataset-format-support).
425
+
426
+ ## QLoRA example
427
+
428
+ To fit fine-tuning on a single 80GB GPU, you can levarage [QLoRA](https://arxiv.org/abs/2305.14314) which combines LoRA with the frozen model quantized to 4-bit:
429
+
430
+ ```python
431
+ import torch
432
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
433
+ from datasets import load_dataset
434
+ from trl import SFTTrainer, SFTConfig
435
+ from peft import LoraConfig
436
+
437
+ tokenizer = AutoTokenizer.from_pretrained("ai21labs/AI21-Jamba-1.6-Mini")
438
+ quantization_config = BitsAndBytesConfig(
439
+ load_in_4bit=True,
440
+ bnb_4bit_quant_type="nf4",
441
+ bnb_4bit_compute_dtype=torch.bfloat16,
442
+ )
443
+ model = AutoModelForCausalLM.from_pretrained(
444
+ "ai21labs/AI21-Jamba-1.6-Mini",
445
+ device_map="auto",
446
+ quantization_config=quantization_config,
447
+ torch_dtype=torch.bfloat16,
448
+ attn_implementation="flash_attention_2",
449
+ )
450
+ lora_config = LoraConfig(
451
+ r=8,
452
+ target_modules=[
453
+ "embed_tokens", "x_proj", "in_proj", "out_proj", # mamba
454
+ "gate_proj", "up_proj", "down_proj", # mlp
455
+ "q_proj", "k_proj", "v_proj", "o_proj", # attention
456
+ ],
457
+ task_type="CAUSAL_LM",
458
+ bias="none",
459
+ )
460
+
461
+ dataset = load_dataset("philschmid/dolly-15k-oai-style", split="train")
462
+ training_args = SFTConfig(
463
+ output_dir="./results",
464
+ logging_dir="./logs",
465
+ num_train_epochs=2,
466
+ per_device_train_batch_size=8,
467
+ learning_rate=1e-5,
468
+ logging_steps=1,
469
+ gradient_checkpointing=True,
470
+ gradient_checkpointing_kwargs={"use_reentrant": False},
471
+ save_steps=100,
472
+ max_seq_length=4096,
473
+ )
474
+ trainer = SFTTrainer(
475
+ model=model,
476
+ tokenizer=tokenizer,
477
+ args=training_args,
478
+ peft_config=lora_config,
479
+ train_dataset=dataset,
480
+ )
481
+ trainer.train()
482
+ ```
483
+
484
+ Note: the above example reqiures the `bitsandbytes` package for the 4-bit quantization:
485
+ ```bash
486
+ pip install bitsandbytes
487
+ ```
488
+
489
+ # About AI21
490
+
491
+ AI21 builds reliable, practical, and scalable AI solutions for the enterprise. The Jamba models are available in the [AI21 Studio](https://www.ai21.com/studio) and in leading cloud partners.
492
+ To learn more about how Jamba 1.6 Mini and Jamba 1.6 Large can bring real world value to your organization, let’s talk.