File size: 6,810 Bytes
00fd396
 
 
 
8746c7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
library_name: keras-hub
pipeline_tag: text-generation
---
### Model Overview
# Model Summary

Qwen2.5 is the latest series of Qwen large language models. For Qwen2.5, the Qwen team released a number of base language models and instruction-tuned language models ranging from 0.5 to 72 billion parameters.

## Qwen2.5 brings the following improvements upon Qwen2:

* Significantly more knowledge and has greatly improved capabilities in coding and mathematics, thanks to our specialized expert models in these domains.
* Significant improvements in instruction following, generating long texts (over 8K tokens), understanding structured data (e.g, tables), and generating structured outputs especially JSON. More resilient to the diversity of system prompts, enhancing role-play implementation and condition-setting for chatbots.
* Long-context Support up to 128K tokens and can generate up to 8K tokens.
* Multilingual support for over 29 languages, including Chinese, English, French, Spanish, Portuguese, German, Italian, Russian, Japanese, Korean, Vietnamese, Thai, Arabic, and more.


For more details, please refer to Qwen [Blog](https://qwenlm.github.io/blog/qwen2.5/), [GitHub](https://github.com/keras-team/keras-hub/tree/master/keras_hub/src/models/qwen), and [Documentation](https://qwen.readthedocs.io/en/latest/).

Weights are released under the [Apache 2 License](https://github.com/keras-team/keras-hub/blob/master/LICENSE) . Keras model code is released under the [Apache 2 License](https://github.com/keras-team/keras-hub/blob/master/LICENSE).

## Links

* [Qwen 2.5 Quickstart Notebook](https://www.kaggle.com/code/laxmareddypatlolla/qwen-quickstart-notebook)
* [Qwen 2.5 API Documentation](https://keras.io/keras_hub/api/models/qwen/)
* [Qwen 2.5 Model Card](https://qwenlm.github.io/blog/qwen2.5/)
* [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
```

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                                                                                                  |
|---------------------------------------|------------|--------------------------------------------------------------------------------------------------------------|
| qwen2.5_0.5b_en        | 0.5B      | 24-layer Qwen model with 0.5 billion parameters. |
| qwen2.5_3b_en     | 3.1B      | 36-layer Qwen model with 3.1 billion parameters. |
| qwen2.5_7b_en    | 7B      | 48-layer Qwen model with 7 billion parameters. |
| qwen2.5_instruct_0.5b_en   | 0.5B      | Instruction fine-tuned 24-layer Qwen model with 0.5 billion parameters. |
| qwen2.5_instruct_32b_en    | 32B      | Instruction fine-tuned 64-layer Qwen model with 32 billion parameters. |
| qwen2.5_instruct_72b_en   | 72B      | Instruction fine-tuned 80-layer Qwen model with 72 billion parameters. |

## Example Usage
```Python

import keras
import keras_hub
import numpy as np

# Use generate() to do text generation.
qwen_lm = keras_hub.models.Qwen2CausalLM.from_preset("qwen2.5_3b_en")
qwen_lm.generate("I want to say", max_length=30)

# Generate with batched prompts.
qwen_lm.generate(["This is a", "Where are you"], max_length=30)

# Compile the generate() function with a custom sampler.
qwen_lm = keras_hub.models.Qwen2CausalLM.from_preset("qwen2.5_3b_en")
qwen_lm.compile(sampler="greedy")
qwen_lm.generate("I want to say", max_length=30)
qwen_lm.compile(sampler=keras_hub.samplers.BeamSampler(num_beams=2))
qwen_lm.generate("I want to say", max_length=30)

# Use generate() without preprocessing.
# Prompt the model with `15191, 374` (the token ids for `"Who is"`).
# Use `"padding_mask"` to indicate values that should not be overridden.
prompt = {
    "token_ids": np.array([[15191, 374, 0, 0, 0]] * 2),
    "padding_mask": np.array([[1, 1, 0, 0, 0]] * 2),
}

qwen_lm = keras_hub.models.Qwen2CausalLM.from_preset(
    "qwen2.5_3b_en",
    preprocessor=None,
)
qwen_lm.generate(prompt)

# Call fit() on a single batch.
features = ["The quick brown fox jumped.", "I forgot my homework."]
qwen_lm = keras_hub.models.Qwen2CausalLM.from_preset("qwen2.5_3b_en")
qwen_lm.fit(x=features, batch_size=2)

# Call fit() without preprocessing.
x = {
    "token_ids": np.array([[1, 2, 3, 4, 5]] * 2),
    "padding_mask": np.array([[1, 1, 1, 1, 1]] * 2),
}
y = np.array([[2, 3, 4, 5, 0]] * 2)
sw = np.array([[1, 1, 1, 1, 1]] * 2)

qwen_lm = keras_hub.models.Qwen2CausalLM.from_preset(
    "qwen2.5_3b_en",
    preprocessor=None,
)
qwen_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.
qwen_lm = keras_hub.models.Qwen2CausalLM.from_preset("hf://keras/qwen2.5_3b_en")
qwen_lm.generate("I want to say", max_length=30)

# Generate with batched prompts.
qwen_lm.generate(["This is a", "Where are you"], max_length=30)

# Compile the generate() function with a custom sampler.
qwen_lm = keras_hub.models.Qwen2CausalLM.from_preset("hf://keras/qwen2.5_3b_en")
qwen_lm.compile(sampler="greedy")
qwen_lm.generate("I want to say", max_length=30)
qwen_lm.compile(sampler=keras_hub.samplers.BeamSampler(num_beams=2))
qwen_lm.generate("I want to say", max_length=30)

# Use generate() without preprocessing.
# Prompt the model with `15191, 374` (the token ids for `"Who is"`).
# Use `"padding_mask"` to indicate values that should not be overridden.
prompt = {
    "token_ids": np.array([[15191, 374, 0, 0, 0]] * 2),
    "padding_mask": np.array([[1, 1, 0, 0, 0]] * 2),
}

qwen_lm = keras_hub.models.Qwen2CausalLM.from_preset(
    "hf://keras/qwen2.5_3b_en",
    preprocessor=None,
)
qwen_lm.generate(prompt)

# Call fit() on a single batch.
features = ["The quick brown fox jumped.", "I forgot my homework."]
qwen_lm = keras_hub.models.Qwen2CausalLM.from_preset("hf://keras/qwen2.5_3b_en")
qwen_lm.fit(x=features, batch_size=2)

# Call fit() without preprocessing.
x = {
    "token_ids": np.array([[1, 2, 3, 4, 5]] * 2),
    "padding_mask": np.array([[1, 1, 1, 1, 1]] * 2),
}
y = np.array([[2, 3, 4, 5, 0]] * 2)
sw = np.array([[1, 1, 1, 1, 1]] * 2)

qwen_lm = keras_hub.models.Qwen2CausalLM.from_preset(
    "hf://keras/qwen2.5_3b_en",
    preprocessor=None,
)
qwen_lm.fit(x=x, y=y, sample_weight=sw, batch_size=2)

```