Divyasreepat
commited on
Commit
•
7f38a17
1
Parent(s):
be78497
Update README.md with new model card content
Browse files
README.md
CHANGED
@@ -1,19 +1,180 @@
|
|
1 |
---
|
2 |
library_name: keras-hub
|
3 |
---
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
*
|
12 |
-
*
|
13 |
-
*
|
14 |
-
*
|
15 |
-
*
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
library_name: keras-hub
|
3 |
---
|
4 |
+
### Model Overview
|
5 |
+
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.
|
6 |
+
|
7 |
+
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).
|
8 |
+
|
9 |
+
## Links
|
10 |
+
|
11 |
+
* [Llama 2 Quickstart Notebook](https://www.kaggle.com/code/matthewdwatson/llama2-quickstart)
|
12 |
+
* [Llama 2 API Documentation](https://keras.io/api/keras_hub/models/llama2/)
|
13 |
+
* [Llama 2 Model Card & Prompt Formats](https://llama.meta.com/docs/model-cards-and-prompt-formats/other-models/)
|
14 |
+
* [KerasHub Beginner Guide](https://keras.io/guides/keras_hub/getting_started/)
|
15 |
+
* [KerasHub Model Publishing Guide](https://keras.io/guides/keras_hub/upload/)
|
16 |
+
|
17 |
+
## Installation
|
18 |
+
|
19 |
+
Keras and KerasHub can be installed with:
|
20 |
+
|
21 |
+
```
|
22 |
+
pip install -U -q keras-hub
|
23 |
+
pip install -U -q keras>=3
|
24 |
+
```
|
25 |
+
|
26 |
+
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.
|
27 |
+
|
28 |
+
## Presets
|
29 |
+
|
30 |
+
The following model checkpoints are provided by the Keras team. Full code examples for each are available below.
|
31 |
+
|
32 |
+
| Preset name | Parameters | Description |
|
33 |
+
|-----------------------|------------|---------------|
|
34 |
+
|` llama2_7b_en` | 6.74B | 7 billion parameter, 32-layer, base LLaMA 2 model. |
|
35 |
+
|` llama2_7b_en_int8` | 6.74B | 7 billion parameter, 32-layer, base LLaMA 2 model with activation and weights quantized to int8. |
|
36 |
+
| `llama2_instruct_7b_en` | 6.74B | 7 billion parameter, 32-layer, instruction tuned LLaMA 2 model. |
|
37 |
+
| `llama2_instruct_7b_en_int8` | 6.74B | 7 billion parameter, 32-layer, instruction tuned LLaMA 2 model with activation and weights quantized to int8. |
|
38 |
+
|
39 |
+
|
40 |
+
## Prompts
|
41 |
+
|
42 |
+
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:
|
43 |
+
|
44 |
+
```python
|
45 |
+
prompt = """<s>[INST] <
|
46 |
+
|
47 |
+
### Example Usage
|
48 |
+
```python
|
49 |
+
import keras
|
50 |
+
import keras_hub
|
51 |
+
import numpy as np
|
52 |
+
```
|
53 |
+
|
54 |
+
Use `generate()` to do text generation.
|
55 |
+
```python
|
56 |
+
llama_lm = keras_hub.models.LlamaCausalLM.from_preset("llama2_7b_en", dtype="bfloat16")
|
57 |
+
llama_lm.generate("What is Keras?", max_length=500)
|
58 |
+
|
59 |
+
# Generate with batched prompts.
|
60 |
+
llama_lm.generate(["What is Keras?", "Give me your best brownie recipe."], max_length=500)
|
61 |
+
```
|
62 |
+
|
63 |
+
Compile the `generate()` function with a custom sampler.
|
64 |
+
```python
|
65 |
+
llama_lm = keras_hub.models.LlamaCausalLM.from_preset("llama2_7b_en", dtype="bfloat16")
|
66 |
+
llama_lm.compile(sampler="greedy")
|
67 |
+
llama_lm.generate("I want to say", max_length=30)
|
68 |
+
|
69 |
+
llama_lm.compile(sampler=keras_hub.samplers.BeamSampler(num_beams=2))
|
70 |
+
llama_lm.generate("I want to say", max_length=30)
|
71 |
+
```
|
72 |
+
|
73 |
+
Use `generate()` without preprocessing.
|
74 |
+
```python
|
75 |
+
prompt = {
|
76 |
+
# `1` maps to the start token followed by "I want to say".
|
77 |
+
"token_ids": np.array([[1, 306, 864, 304, 1827, 0, 0, 0, 0, 0]] * 2),
|
78 |
+
# Use `"padding_mask"` to indicate values that should not be overridden.
|
79 |
+
"padding_mask": np.array([[1, 1, 1, 1, 1, 0, 0, 0, 0, 0]] * 2),
|
80 |
+
}
|
81 |
+
|
82 |
+
llama_lm = keras_hub.models.LlamaCausalLM.from_preset(
|
83 |
+
"llama2_7b_en",
|
84 |
+
preprocessor=None,
|
85 |
+
dtype="bfloat16"
|
86 |
+
)
|
87 |
+
llama_lm.generate(prompt)
|
88 |
+
```
|
89 |
+
|
90 |
+
Call `fit()` on a single batch.
|
91 |
+
```python
|
92 |
+
features = ["The quick brown fox jumped.", "I forgot my homework."]
|
93 |
+
llama_lm = keras_hub.models.LlamaCausalLM.from_preset("llama2_7b_en", dtype="bfloat16")
|
94 |
+
llama_lm.fit(x=features, batch_size=2)
|
95 |
+
```
|
96 |
+
|
97 |
+
Call `fit()` without preprocessing.
|
98 |
+
```python
|
99 |
+
x = {
|
100 |
+
"token_ids": np.array([[1, 450, 4996, 17354, 1701, 29916, 12500, 287, 29889, 0]] * 2),
|
101 |
+
"padding_mask": np.array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 0]] * 2),
|
102 |
+
}
|
103 |
+
y = np.array([[450, 4996, 17354, 1701, 29916, 12500, 287, 29889, 0, 0]] * 2)
|
104 |
+
sw = np.array([[1, 1, 1, 1, 1, 1, 1, 1, 0, 0]] * 2)
|
105 |
+
|
106 |
+
llama_lm = keras_hub.models.LlamaCausalLM.from_preset(
|
107 |
+
"llama2_7b_en",
|
108 |
+
preprocessor=None,
|
109 |
+
dtype="bfloat16"
|
110 |
+
)
|
111 |
+
llama_lm.fit(x=x, y=y, sample_weight=sw, batch_size=2)
|
112 |
+
```
|
113 |
+
|
114 |
+
## Example Usage with Hugging Face URI
|
115 |
+
|
116 |
+
```python
|
117 |
+
import keras
|
118 |
+
import keras_hub
|
119 |
+
import numpy as np
|
120 |
+
```
|
121 |
+
|
122 |
+
Use `generate()` to do text generation.
|
123 |
+
```python
|
124 |
+
llama_lm = keras_hub.models.LlamaCausalLM.from_preset("hf://keras/llama2_7b_en", dtype="bfloat16")
|
125 |
+
llama_lm.generate("What is Keras?", max_length=500)
|
126 |
+
|
127 |
+
# Generate with batched prompts.
|
128 |
+
llama_lm.generate(["What is Keras?", "Give me your best brownie recipe."], max_length=500)
|
129 |
+
```
|
130 |
+
|
131 |
+
Compile the `generate()` function with a custom sampler.
|
132 |
+
```python
|
133 |
+
llama_lm = keras_hub.models.LlamaCausalLM.from_preset("hf://keras/llama2_7b_en", dtype="bfloat16")
|
134 |
+
llama_lm.compile(sampler="greedy")
|
135 |
+
llama_lm.generate("I want to say", max_length=30)
|
136 |
+
|
137 |
+
llama_lm.compile(sampler=keras_hub.samplers.BeamSampler(num_beams=2))
|
138 |
+
llama_lm.generate("I want to say", max_length=30)
|
139 |
+
```
|
140 |
+
|
141 |
+
Use `generate()` without preprocessing.
|
142 |
+
```python
|
143 |
+
prompt = {
|
144 |
+
# `1` maps to the start token followed by "I want to say".
|
145 |
+
"token_ids": np.array([[1, 306, 864, 304, 1827, 0, 0, 0, 0, 0]] * 2),
|
146 |
+
# Use `"padding_mask"` to indicate values that should not be overridden.
|
147 |
+
"padding_mask": np.array([[1, 1, 1, 1, 1, 0, 0, 0, 0, 0]] * 2),
|
148 |
+
}
|
149 |
+
|
150 |
+
llama_lm = keras_hub.models.LlamaCausalLM.from_preset(
|
151 |
+
"hf://keras/llama2_7b_en",
|
152 |
+
preprocessor=None,
|
153 |
+
dtype="bfloat16"
|
154 |
+
)
|
155 |
+
llama_lm.generate(prompt)
|
156 |
+
```
|
157 |
+
|
158 |
+
Call `fit()` on a single batch.
|
159 |
+
```python
|
160 |
+
features = ["The quick brown fox jumped.", "I forgot my homework."]
|
161 |
+
llama_lm = keras_hub.models.LlamaCausalLM.from_preset("hf://keras/llama2_7b_en", dtype="bfloat16")
|
162 |
+
llama_lm.fit(x=features, batch_size=2)
|
163 |
+
```
|
164 |
+
|
165 |
+
Call `fit()` without preprocessing.
|
166 |
+
```python
|
167 |
+
x = {
|
168 |
+
"token_ids": np.array([[1, 450, 4996, 17354, 1701, 29916, 12500, 287, 29889, 0]] * 2),
|
169 |
+
"padding_mask": np.array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 0]] * 2),
|
170 |
+
}
|
171 |
+
y = np.array([[450, 4996, 17354, 1701, 29916, 12500, 287, 29889, 0, 0]] * 2)
|
172 |
+
sw = np.array([[1, 1, 1, 1, 1, 1, 1, 1, 0, 0]] * 2)
|
173 |
+
|
174 |
+
llama_lm = keras_hub.models.LlamaCausalLM.from_preset(
|
175 |
+
"hf://keras/llama2_7b_en",
|
176 |
+
preprocessor=None,
|
177 |
+
dtype="bfloat16"
|
178 |
+
)
|
179 |
+
llama_lm.fit(x=x, y=y, sample_weight=sw, batch_size=2)
|
180 |
+
```
|