Files changed (1) hide show
  1. README.md +412 -5
README.md CHANGED
@@ -1,5 +1,412 @@
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
+ Built with hybrid SSM-Transformer architecture, the Jamba 1.6 family of models outperform other open, instruction-following foundation models on quality, speed, and long context performance, and rival leading closed models on quality. As open models, Jamba Mini 1.6 (12B active/52B total) and Jamba Large 1.6 (94B active/398B total) are available for private deployment, either in VPC or on-premise, and demonstrate superior performance on the kind of long context tasks that matter most to enterprises, such as RAG workflows and grounded question answering across lengthy documents.
9
+
10
+ The models are released under the Jamba Open Model License, a permissive license allowing full research use and commercial use under the license terms.
11
+
12
+ If you need to license the model for your needs, talk to us.
13
+
14
+ For more details of this model, see the release [blog post](https://www.ai21.com/blog/introducing-jamba-1-6).
15
+ ## Model Details
16
+
17
+ - **Developed by:** [AI21](https://www.ai21.com)
18
+ - **Model type:** Joint Attention and Mamba (Jamba)
19
+ - **License:** [Jamba Open Model License](https://www.ai21.com/licenses/jamba-open-model-license)
20
+ - **Context length:** 256K
21
+ - **Knowledge cutoff date:** March 5, 2024
22
+ - **Supported languages:** English, Spanish, French, Portuguese, Italian, Dutch, German, Arabic and Hebrew
23
+
24
+ ## Results on common benchmarks
25
+
26
+ | Benchmark | Jamba Mini 1.6 | Ministral 8B | Llama 3.1 8B | Command R7B |
27
+ |--------------|:-----:|:-----:|:-----:|:-----:|
28
+ | Arena Hard | 51.2| 41.35| 28.17| 27.95|
29
+ | CRAG | 76.2| 52| 60| 23.1|
30
+ | FinanceBench (FullDoc) | 45.4 | 19.2 | 28.4 | 2.8|
31
+ | HELMET LongQA | 46.9 | 33 | 29.2| 9.6|
32
+ | LongBench | 32 | 17.5 | 17.7 | 2|
33
+
34
+ LongBench and Arena Hard scores are from official leaderboards for applicable models. Examples that couldn't fit models' context windows were scored accordingly. Due to a 32K context limit in its vLLM deployment, Ministral 8B was evaluated through its official API.
35
+ # Usage
36
+ ## Prerequisites
37
+
38
+ In order to run optimized Mamba implementations, you first need to install `mamba-ssm` and `causal-conv1d`:
39
+ ```bash
40
+ pip install mamba-ssm causal-conv1d>=1.2.0
41
+ ```
42
+ You also have to have the model on a CUDA device.
43
+
44
+
45
+ ## Run the model with vLLM
46
+
47
+ The recommended way to perform efficient inference with Jamba Mini 1.6 is using [vLLM](https://docs.vllm.ai/en/latest/). First, make sure to install vLLM (version 0.5.4 or higher is required)
48
+ ```bash
49
+ pip install vllm>=0.5.4
50
+ ```
51
+
52
+ In the example below, `number_gpus` should match the number of GPUs you want to deploy Jamba Mini 1.6 on. A minimum of 2 80GB GPUs is required.
53
+
54
+ ```python
55
+ from vllm import LLM, SamplingParams
56
+ from transformers import AutoTokenizer
57
+
58
+ model = "ai21labs/AI21-Jamba-Mini-1.6"
59
+ number_gpus = 2
60
+
61
+ llm = LLM(model=model,
62
+ max_model_len=200*1024,
63
+ tensor_parallel_size=number_gpus)
64
+
65
+ tokenizer = AutoTokenizer.from_pretrained(model)
66
+
67
+ messages = [
68
+ {"role": "system", "content": "You are an ancient oracle who speaks in cryptic but wise phrases, always hinting at deeper meanings."},
69
+ {"role": "user", "content": "Hello!"},
70
+ ]
71
+
72
+ prompts = tokenizer.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
73
+
74
+ sampling_params = SamplingParams(temperature=0.4, top_p=0.95, max_tokens=100)
75
+ outputs = llm.generate(prompts, sampling_params)
76
+
77
+ generated_text = outputs[0].outputs[0].text
78
+ print(generated_text)
79
+ #Output: Seek and you shall find. The path is winding, but the journey is enlightening. What wisdom do you seek from the ancient echoes?
80
+ ```
81
+
82
+ 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.
83
+
84
+ <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.
85
+
86
+ ### ExpertsInt8 quantization
87
+ 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.6%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 Mini 1.6 on a single 80GB GPU.
88
+
89
+ In order to use ExpertsInt8, you need to use vllm version 0.5.5 or higher: `pip install vllm>=0.5.5`
90
+
91
+ With default vLLM configuration, you can fit prompts up to 100K on a single 80GB A100 GPU:
92
+ ```python
93
+ import os
94
+ os.environ['VLLM_FUSED_MOE_CHUNK_SIZE']='32768' # This is a workaround a bug in vLLM's fused_moe kernel
95
+
96
+ from vllm import LLM
97
+ llm = LLM(model="ai21labs/AI21-Jamba-Mini-1.6",
98
+ max_model_len=100*1024,
99
+ quantization="experts_int8")
100
+ ```
101
+
102
+
103
+ ## Run the model with `transformers`
104
+
105
+ The following example loads Jamba Mini 1.6 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 Mini 1.6 is too large to fit on a single 80GB GPU, so you'll need at least 2 such GPUs.
106
+
107
+ ```python
108
+ import torch
109
+ from transformers import AutoModelForCausalLM, AutoTokenizer
110
+
111
+ model = AutoModelForCausalLM.from_pretrained("ai21labs/AI21-Jamba-Mini-1.6",
112
+ torch_dtype=torch.bfloat16,
113
+ attn_implementation="flash_attention_2",
114
+ device_map="auto")
115
+
116
+ tokenizer = AutoTokenizer.from_pretrained("ai21labs/AI21-Jamba-Mini-1.6")
117
+
118
+ messages = [
119
+ {"role": "system", "content": "You are an ancient oracle who speaks in cryptic but wise phrases, always hinting at deeper meanings."},
120
+ {"role": "user", "content": "Hello!"},
121
+ ]
122
+
123
+ input_ids = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors='pt').to(model.device)
124
+
125
+ outputs = model.generate(input_ids, max_new_tokens=216)
126
+
127
+ # Decode the output
128
+ conversation = tokenizer.decode(outputs[0], skip_special_tokens=True)
129
+
130
+ # Split the conversation to get only the assistant's response
131
+ assistant_response = conversation.split(messages[-1]['content'])[1].strip()
132
+ print(assistant_response)
133
+ # Output: Seek and you shall find. The path is winding, but the journey is enlightening. What wisdom do you seek from the ancient echoes?
134
+ ```
135
+
136
+ <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.
137
+
138
+ <u>Note:</u> If you're having trouble installing `mamba-ssm` and `causal-conv1d` for the optimized Mamba kernels, you can run Jamba Mini 1.6 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()`.
139
+
140
+ <details><summary><strong>Load the model in 8-bit</strong></summary>
141
+
142
+ **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:
143
+
144
+ ```python
145
+ from transformers import AutoModelForCausalLM, BitsAndBytesConfig
146
+ quantization_config = BitsAndBytesConfig(load_in_8bit=True,
147
+ llm_int8_skip_modules=["mamba"])
148
+ model = AutoModelForCausalLM.from_pretrained("ai21labs/AI21-Jamba-Mini-1.6",
149
+ torch_dtype=torch.bfloat16,
150
+ attn_implementation="flash_attention_2",
151
+ quantization_config=quantization_config)
152
+ ```
153
+
154
+ </details>
155
+
156
+ <details><summary><strong>Load the model on CPU</strong></summary>
157
+
158
+ If you don't have access to a GPU, you can also load and run Jamba Mini 1.6 on a CPU. Note this will result in poor inference performance.
159
+
160
+ ```python
161
+ from transformers import AutoModelForCausalLM
162
+ model = AutoModelForCausalLM.from_pretrained("ai21labs/AI21-Jamba-Mini-1.6",
163
+ use_mamba_kernels=False)
164
+ ```
165
+ </details>
166
+ <br>
167
+ <br>
168
+
169
+ # Model features
170
+
171
+ ## Tool use with Jamba
172
+ Jamba Mini 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.
173
+
174
+ Given a conversation that contains tools, the model can output content, tool invocations or both.
175
+
176
+ <details><summary><strong>Tool usage example</strong></summary>
177
+
178
+
179
+ ```python
180
+ from transformers import AutoTokenizer
181
+
182
+ tokenizer = AutoTokenizer.from_pretrained("ai21labs/AI21-Jamba-Mini-1.6")
183
+
184
+ messages = [
185
+ {
186
+ "role": "user",
187
+ "content": "What's the weather like right now in Jerusalem and in London?"
188
+ }
189
+ ]
190
+
191
+ tools = [
192
+ {
193
+ 'type': 'function',
194
+ 'function': {
195
+ 'name': 'get_current_weather',
196
+ 'description': 'Get the current weather',
197
+ 'parameters': {
198
+ 'type': 'object',
199
+ 'properties': {
200
+ 'location': {'type': 'string', 'description': 'The city and state, e.g. San Francisco, CA'},
201
+ 'format': {'type': 'string', 'enum': ['celsius', 'fahrenheit'], 'description': 'The temperature unit to use. Infer this from the users location.'}
202
+ },
203
+ 'required': ['location', 'format']
204
+ }
205
+ }
206
+ }
207
+ ]
208
+
209
+ prompt = tokenizer.apply_chat_template(
210
+ messages,
211
+ tools=tools,
212
+ tokenize=False,
213
+ )
214
+ ```
215
+ Output:
216
+ ```
217
+ <tool_calls>[
218
+ {"name": "get_current_weather", "arguments": {"location": "Jerusalem", "format": "celsius"}},
219
+ {"name": "get_current_weather", "arguments": {"location": "celsius", "format": "celsius"}}
220
+ ]</tool_calls>
221
+ ```
222
+
223
+ </details>
224
+
225
+
226
+ <details><summary><strong>Feeding back tool responses into the model</strong></summary>
227
+
228
+ 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.
229
+
230
+ The `arguments` field for each tool call can be either a dict or a JSON string.
231
+
232
+ ```python
233
+ from transformers import AutoTokenizer
234
+
235
+ tokenizer = AutoTokenizer.from_pretrained("ai21labs/AI21-Jamba-Mini-1.6")
236
+
237
+ # Note that you must send the tool responses in the same order as the model called the tools:
238
+ messages = [
239
+ {
240
+ "role": "user",
241
+ "content": "What's the weather like right now in Jerusalem and in London?"
242
+ },
243
+ {
244
+ "role": "assistant",
245
+ "content": null,
246
+ "tool_calls": [
247
+ {
248
+ "name": "get_current_weather",
249
+ "arguments": "{\"location\": \"Jerusalem\", \"format\": \"celsius\"}"
250
+ },
251
+ {
252
+ "name": "get_current_weather",
253
+ "arguments": "{\"location\": \"London\", \"format\": \"celsius\"}"
254
+ }
255
+ ]
256
+ },
257
+ {
258
+ "role": "tool",
259
+ "content": "The weather in Jerusalem is 18 degrees celsius."
260
+ },
261
+ {
262
+ "role": "tool",
263
+ "content": "The weather in London is 8 degrees celsius."
264
+ }
265
+ ]
266
+
267
+ tool_use_prompt = tokenizer.apply_chat_template(
268
+ messages,
269
+ tools=tools,
270
+ tokenize=False,
271
+ )
272
+ ```
273
+ example output:
274
+ ```
275
+ The weather in Jerusalem is currently 18 degrees Celsius. In London, it is 8 degrees Celsius.
276
+ ```
277
+
278
+ </details>
279
+
280
+
281
+ ## Fine-tuning examples
282
+
283
+ The examples below use the `SFTTrainer` from [huggingface/trl](https://github.com/huggingface/trl), so ensure it's installed:
284
+ ```bash
285
+ pip install trl
286
+ ```
287
+
288
+ ## Full Fine-tuning example
289
+ 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)
290
+
291
+ ## LoRA example
292
+
293
+ Here is an example of fine-tuning with LoRA PEFT, in bfloat16 (requires ~130GB GPU RAM, so e.g. 2xA100 80GB):
294
+
295
+ ```python
296
+ import torch
297
+ from transformers import AutoTokenizer, AutoModelForCausalLM
298
+ from datasets import load_dataset
299
+ from trl import SFTTrainer, SFTConfig
300
+ from peft import LoraConfig
301
+
302
+ tokenizer = AutoTokenizer.from_pretrained("ai21labs/AI21-Jamba-Mini-1.6")
303
+ model = AutoModelForCausalLM.from_pretrained(
304
+ "ai21labs/AI21-Jamba-Mini-1.6",
305
+ device_map="auto",
306
+ torch_dtype=torch.bfloat16,
307
+ attn_implementation="flash_attention_2",
308
+ )
309
+
310
+ lora_config = LoraConfig(
311
+ r=8,
312
+ target_modules=[
313
+ "embed_tokens",
314
+ "x_proj", "in_proj", "out_proj", # mamba
315
+ "gate_proj", "up_proj", "down_proj", # mlp
316
+ "q_proj", "k_proj", "v_proj", "o_proj", # attention
317
+ ],
318
+ task_type="CAUSAL_LM",
319
+ bias="none",
320
+ )
321
+
322
+ dataset = load_dataset("philschmid/dolly-15k-oai-style", split="train")
323
+ training_args = SFTConfig(
324
+ output_dir="/dev/shm/results",
325
+ logging_dir="./logs",
326
+ num_train_epochs=2,
327
+ per_device_train_batch_size=4,
328
+ learning_rate=1e-5,
329
+ logging_steps=10,
330
+ gradient_checkpointing=True,
331
+ max_seq_length=4096,
332
+ save_steps=100,
333
+ )
334
+ trainer = SFTTrainer(
335
+ model=model,
336
+ tokenizer=tokenizer,
337
+ args=training_args,
338
+ peft_config=lora_config,
339
+ train_dataset=dataset,
340
+ )
341
+ trainer.train()
342
+ ```
343
+
344
+ 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).
345
+
346
+ ## QLoRA example
347
+
348
+ 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:
349
+
350
+ ```python
351
+ import torch
352
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
353
+ from datasets import load_dataset
354
+ from trl import SFTTrainer, SFTConfig
355
+ from peft import LoraConfig
356
+
357
+ tokenizer = AutoTokenizer.from_pretrained("ai21labs/AI21-Jamba-Mini-1.6")
358
+ quantization_config = BitsAndBytesConfig(
359
+ load_in_4bit=True,
360
+ bnb_4bit_quant_type="nf4",
361
+ bnb_4bit_compute_dtype=torch.bfloat16,
362
+ )
363
+ model = AutoModelForCausalLM.from_pretrained(
364
+ "ai21labs/AI21-Jamba-Mini-1.6",
365
+ device_map="auto",
366
+ quantization_config=quantization_config,
367
+ torch_dtype=torch.bfloat16,
368
+ attn_implementation="flash_attention_2",
369
+ )
370
+ lora_config = LoraConfig(
371
+ r=8,
372
+ target_modules=[
373
+ "embed_tokens", "x_proj", "in_proj", "out_proj", # mamba
374
+ "gate_proj", "up_proj", "down_proj", # mlp
375
+ "q_proj", "k_proj", "v_proj", "o_proj", # attention
376
+ ],
377
+ task_type="CAUSAL_LM",
378
+ bias="none",
379
+ )
380
+
381
+ dataset = load_dataset("philschmid/dolly-15k-oai-style", split="train")
382
+ training_args = SFTConfig(
383
+ output_dir="./results",
384
+ logging_dir="./logs",
385
+ num_train_epochs=2,
386
+ per_device_train_batch_size=8,
387
+ learning_rate=1e-5,
388
+ logging_steps=1,
389
+ gradient_checkpointing=True,
390
+ gradient_checkpointing_kwargs={"use_reentrant": False},
391
+ save_steps=100,
392
+ max_seq_length=4096,
393
+ )
394
+ trainer = SFTTrainer(
395
+ model=model,
396
+ tokenizer=tokenizer,
397
+ args=training_args,
398
+ peft_config=lora_config,
399
+ train_dataset=dataset,
400
+ )
401
+ trainer.train()
402
+ ```
403
+
404
+ Note: the above example reqiures the `bitsandbytes` package for the 4-bit quantization:
405
+ ```bash
406
+ pip install bitsandbytes
407
+ ```
408
+
409
+ # About AI21
410
+
411
+ 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.
412
+ To learn more about how Jamba Mini 1.6 and Jamba Large 1.6 can bring real world value to your organization, let’s talk.