File size: 6,503 Bytes
be78497 7a4724c 7e50176 be78497 ad6c309 7f38a17 2e11e09 7f38a17 f4202dc 7f38a17 c4052a4 7f38a17 f212b73 7f38a17 f212b73 7f38a17 f212b73 7f38a17 f212b73 7f38a17 f212b73 7f38a17 f212b73 7f38a17 ad6c309 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
---
library_name: keras-hub
license: llama2
language:
- en
tags:
- text-generation
- keras
pipeline_tag: text-generation
---
### Model Overview
Llama 2 is a set of large language models published by Meta. Both pretrained and instruction tuned models are available, and range in size from 7 billion to 70 billion parameters. See the model card below for benchmarks, data sources, and intended use cases.
Weights are released under the [Llama 2 Community License](https://ai.meta.com/llama/license/). Keras model code is released under the [Apache 2 License](https://github.com/keras-team/keras-hub/blob/master/LICENSE).
## Links
* [Llama 2 Quickstart Notebook](https://www.kaggle.com/code/matthewdwatson/llama2-quickstart)
* [Llama 2 API Documentation](https://keras.io/api/keras_hub/models/llama2/)
* [Llama 2 Model Card & Prompt Formats](https://llama.meta.com/docs/model-cards-and-prompt-formats/other-models/)
* [KerasHub Beginner Guide](https://keras.io/guides/keras_hub/getting_started/)
* [KerasHub Model Publishing Guide](https://keras.io/guides/keras_hub/upload/)
## Installation
Keras and KerasHub can be installed with:
```
pip install -U -q keras-hub
pip install -U -q keras>=3
```
Jax, TensorFlow, and Torch come preinstalled in Kaggle Notebooks. For instructions on installing them in another environment see the [Keras Getting Started](https://keras.io/getting_started/) page.
## Presets
The following model checkpoints are provided by the Keras team. Full code examples for each are available below.
| Preset name | Parameters | Description |
|-----------------------|------------|---------------|
|` llama2_7b_en` | 6.74B | 7 billion parameter, 32-layer, base LLaMA 2 model. |
|` llama2_7b_en_int8` | 6.74B | 7 billion parameter, 32-layer, base LLaMA 2 model with activation and weights quantized to int8. |
| `llama2_instruct_7b_en` | 6.74B | 7 billion parameter, 32-layer, instruction tuned LLaMA 2 model. |
| `llama2_instruct_7b_en_int8` | 6.74B | 7 billion parameter, 32-layer, instruction tuned LLaMA 2 model with activation and weights quantized to int8. |
## Prompts
Llama-2 "instruct" models are instruction tuned on turn by turn conversations and should be prompted with examples that precisely match the training data. Specifically, you must alternate user and assistant turns that begin and end with special tokens. New lines do matter. See the following for an example:
```python
prompt = """<s>[INST] <
```
## Example Usage
```python
import keras
import keras_hub
import numpy as np
```
Use `generate()` to do text generation.
```python
llama_lm = keras_hub.models.LlamaCausalLM.from_preset("llama2_7b_en")
llama_lm.generate("What is Keras?", max_length=500)
# Generate with batched prompts.
llama_lm.generate(["What is Keras?", "Give me your best brownie recipe."], max_length=500)
```
Compile the `generate()` function with a custom sampler.
```python
llama_lm = keras_hub.models.LlamaCausalLM.from_preset("llama2_7b_en")
llama_lm.compile(sampler="greedy")
llama_lm.generate("I want to say", max_length=30)
llama_lm.compile(sampler=keras_hub.samplers.BeamSampler(num_beams=2))
llama_lm.generate("I want to say", max_length=30)
```
Use `generate()` without preprocessing.
```python
prompt = {
# `1` maps to the start token followed by "I want to say".
"token_ids": np.array([[1, 306, 864, 304, 1827, 0, 0, 0, 0, 0]] * 2),
# Use `"padding_mask"` to indicate values that should not be overridden.
"padding_mask": np.array([[1, 1, 1, 1, 1, 0, 0, 0, 0, 0]] * 2),
}
llama_lm = keras_hub.models.LlamaCausalLM.from_preset(
"llama2_7b_en",
preprocessor=None,
dtype="bfloat16"
)
llama_lm.generate(prompt)
```
Call `fit()` on a single batch.
```python
features = ["The quick brown fox jumped.", "I forgot my homework."]
llama_lm = keras_hub.models.LlamaCausalLM.from_preset("llama2_7b_en")
llama_lm.fit(x=features, batch_size=2)
```
Call `fit()` without preprocessing.
```python
x = {
"token_ids": np.array([[1, 450, 4996, 17354, 1701, 29916, 12500, 287, 29889, 0]] * 2),
"padding_mask": np.array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 0]] * 2),
}
y = np.array([[450, 4996, 17354, 1701, 29916, 12500, 287, 29889, 0, 0]] * 2)
sw = np.array([[1, 1, 1, 1, 1, 1, 1, 1, 0, 0]] * 2)
llama_lm = keras_hub.models.LlamaCausalLM.from_preset(
"llama2_7b_en",
preprocessor=None,
dtype="bfloat16"
)
llama_lm.fit(x=x, y=y, sample_weight=sw, batch_size=2)
```
## Example Usage with Hugging Face URI
```python
import keras
import keras_hub
import numpy as np
```
Use `generate()` to do text generation.
```python
llama_lm = keras_hub.models.LlamaCausalLM.from_preset("hf://keras/llama2_7b_en")
llama_lm.generate("What is Keras?", max_length=500)
# Generate with batched prompts.
llama_lm.generate(["What is Keras?", "Give me your best brownie recipe."], max_length=500)
```
Compile the `generate()` function with a custom sampler.
```python
llama_lm = keras_hub.models.LlamaCausalLM.from_preset("hf://keras/llama2_7b_en")
llama_lm.compile(sampler="greedy")
llama_lm.generate("I want to say", max_length=30)
llama_lm.compile(sampler=keras_hub.samplers.BeamSampler(num_beams=2))
llama_lm.generate("I want to say", max_length=30)
```
Use `generate()` without preprocessing.
```python
prompt = {
# `1` maps to the start token followed by "I want to say".
"token_ids": np.array([[1, 306, 864, 304, 1827, 0, 0, 0, 0, 0]] * 2),
# Use `"padding_mask"` to indicate values that should not be overridden.
"padding_mask": np.array([[1, 1, 1, 1, 1, 0, 0, 0, 0, 0]] * 2),
}
llama_lm = keras_hub.models.LlamaCausalLM.from_preset(
"hf://keras/llama2_7b_en",
preprocessor=None,
dtype="bfloat16"
)
llama_lm.generate(prompt)
```
Call `fit()` on a single batch.
```python
features = ["The quick brown fox jumped.", "I forgot my homework."]
llama_lm = keras_hub.models.LlamaCausalLM.from_preset("hf://keras/llama2_7b_en")
llama_lm.fit(x=features, batch_size=2)
```
Call `fit()` without preprocessing.
```python
x = {
"token_ids": np.array([[1, 450, 4996, 17354, 1701, 29916, 12500, 287, 29889, 0]] * 2),
"padding_mask": np.array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 0]] * 2),
}
y = np.array([[450, 4996, 17354, 1701, 29916, 12500, 287, 29889, 0, 0]] * 2)
sw = np.array([[1, 1, 1, 1, 1, 1, 1, 1, 0, 0]] * 2)
llama_lm = keras_hub.models.LlamaCausalLM.from_preset(
"hf://keras/llama2_7b_en",
preprocessor=None,
dtype="bfloat16"
)
llama_lm.fit(x=x, y=y, sample_weight=sw, batch_size=2)
```
|