ctranslate2-4you
commited on
Commit
•
da4de0e
1
Parent(s):
9e7abb2
Update README.md
Browse files
README.md
CHANGED
@@ -1,131 +1,230 @@
|
|
1 |
---
|
2 |
-
license: apache-2.0
|
3 |
license_link: https://huggingface.co/Qwen/Qwen2.5-7B-Instruct/blob/main/LICENSE
|
4 |
-
language:
|
5 |
-
- en
|
6 |
pipeline_tag: text-generation
|
7 |
-
base_model:
|
|
|
|
|
8 |
tags:
|
|
|
|
|
9 |
- chat
|
10 |
---
|
|
|
11 |
|
12 |
-
#
|
13 |
|
14 |
-
|
15 |
|
16 |
-
|
17 |
|
18 |
-
-
|
19 |
-
-
|
20 |
-
-
|
21 |
-
-
|
|
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
- Type: Causal Language Models
|
25 |
-
- Training Stage: Pretraining & Post-training
|
26 |
-
- Architecture: transformers with RoPE, SwiGLU, RMSNorm, and Attention QKV bias
|
27 |
-
- Number of Parameters: 7.61B
|
28 |
-
- Number of Paramaters (Non-Embedding): 6.53B
|
29 |
-
- Number of Layers: 28
|
30 |
-
- Number of Attention Heads (GQA): 28 for Q and 4 for KV
|
31 |
-
- Context Length: Full 131,072 tokens and generation 8192 tokens
|
32 |
-
- Please refer to [this section](#processing-long-texts) for detailed instructions on how to deploy Qwen2.5 for handling long texts.
|
33 |
|
34 |
-
For more details, please refer to our [blog](https://qwenlm.github.io/blog/qwen2.5/), [GitHub](https://github.com/QwenLM/Qwen2.5), and [Documentation](https://qwen.readthedocs.io/en/latest/).
|
35 |
-
|
36 |
-
## Requirements
|
37 |
-
|
38 |
-
The code of Qwen2.5 has been in the latest Hugging face `transformers` and we advise you to use the latest version of `transformers`.
|
39 |
-
|
40 |
-
With `transformers<4.37.0`, you will encounter the following error:
|
41 |
```
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
```
|
|
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
Here provides a code snippet with `apply_chat_template` to show you how to load the tokenizer and model and how to generate contents.
|
48 |
-
|
49 |
-
```python
|
50 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
51 |
-
|
52 |
-
model_name = "Qwen/Qwen2.5-7B-Instruct"
|
53 |
-
|
54 |
-
model = AutoModelForCausalLM.from_pretrained(
|
55 |
-
model_name,
|
56 |
-
torch_dtype="auto",
|
57 |
-
device_map="auto"
|
58 |
-
)
|
59 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
60 |
-
|
61 |
-
prompt = "Give me a short introduction to large language model."
|
62 |
-
messages = [
|
63 |
-
{"role": "system", "content": "You are Qwen, created by Alibaba Cloud. You are a helpful assistant."},
|
64 |
-
{"role": "user", "content": prompt}
|
65 |
-
]
|
66 |
-
text = tokenizer.apply_chat_template(
|
67 |
-
messages,
|
68 |
-
tokenize=False,
|
69 |
-
add_generation_prompt=True
|
70 |
-
)
|
71 |
-
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
72 |
-
|
73 |
-
generated_ids = model.generate(
|
74 |
-
**model_inputs,
|
75 |
-
max_new_tokens=512
|
76 |
-
)
|
77 |
-
generated_ids = [
|
78 |
-
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
|
79 |
-
]
|
80 |
-
|
81 |
-
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
82 |
-
```
|
83 |
|
84 |
-
### Processing Long Texts
|
85 |
-
|
86 |
-
The current `config.json` is set for context length up to 32,768 tokens.
|
87 |
-
To handle extensive inputs exceeding 32,768 tokens, we utilize [YaRN](https://arxiv.org/abs/2309.00071), a technique for enhancing model length extrapolation, ensuring optimal performance on lengthy texts.
|
88 |
-
|
89 |
-
For supported frameworks, you could add the following to `config.json` to enable YaRN:
|
90 |
-
```json
|
91 |
-
{
|
92 |
-
...,
|
93 |
-
"rope_scaling": {
|
94 |
-
"factor": 4.0,
|
95 |
-
"original_max_position_embeddings": 32768,
|
96 |
-
"type": "yarn"
|
97 |
-
}
|
98 |
-
}
|
99 |
```
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
```
|
117 |
-
|
118 |
-
title = {Qwen2.5: A Party of Foundation Models},
|
119 |
-
url = {https://qwenlm.github.io/blog/qwen2.5/},
|
120 |
-
author = {Qwen Team},
|
121 |
-
month = {September},
|
122 |
-
year = {2024}
|
123 |
-
}
|
124 |
-
|
125 |
-
@article{qwen2,
|
126 |
-
title={Qwen2 Technical Report},
|
127 |
-
author={An Yang and Baosong Yang and Binyuan Hui and Bo Zheng and Bowen Yu and Chang Zhou and Chengpeng Li and Chengyuan Li and Dayiheng Liu and Fei Huang and Guanting Dong and Haoran Wei and Huan Lin and Jialong Tang and Jialin Wang and Jian Yang and Jianhong Tu and Jianwei Zhang and Jianxin Ma and Jin Xu and Jingren Zhou and Jinze Bai and Jinzheng He and Junyang Lin and Kai Dang and Keming Lu and Keqin Chen and Kexin Yang and Mei Li and Mingfeng Xue and Na Ni and Pei Zhang and Peng Wang and Ru Peng and Rui Men and Ruize Gao and Runji Lin and Shijie Wang and Shuai Bai and Sinan Tan and Tianhang Zhu and Tianhao Li and Tianyu Liu and Wenbin Ge and Xiaodong Deng and Xiaohuan Zhou and Xingzhang Ren and Xinyu Zhang and Xipin Wei and Xuancheng Ren and Yang Fan and Yang Yao and Yichang Zhang and Yu Wan and Yunfei Chu and Yuqiong Liu and Zeyu Cui and Zhenru Zhang and Zhihao Fan},
|
128 |
-
journal={arXiv preprint arXiv:2407.10671},
|
129 |
-
year={2024}
|
130 |
-
}
|
131 |
-
```
|
|
|
1 |
---
|
|
|
2 |
license_link: https://huggingface.co/Qwen/Qwen2.5-7B-Instruct/blob/main/LICENSE
|
|
|
|
|
3 |
pipeline_tag: text-generation
|
4 |
+
base_model:
|
5 |
+
- Qwen/Qwen2.5-7B-Instruct
|
6 |
+
base_model_relation: quantized
|
7 |
tags:
|
8 |
+
- ctranslate2
|
9 |
+
- Qwen2.5
|
10 |
- chat
|
11 |
---
|
12 |
+
Conversion of https://huggingface.co/Qwen/Qwen2.5-7B-Instruct into the ```ctranslate2``` format using ```int8``` quantization.
|
13 |
|
14 |
+
NOTE #1: This requires a version of ```ctranslate2``` GREATER THAN 4.5.0.
|
15 |
|
16 |
+
NOTE #2: The sample scripts below require ```pip``` installing the necessary ```CUDA``` and ```CUDNN``` libraries. If you rely on a systemwide installation instead, adjust your code accordingly.
|
17 |
|
18 |
+
Requirements:
|
19 |
|
20 |
+
- torch 2.4.0+cu124
|
21 |
+
- nvidia-cublas-cu12 12.4.2.65
|
22 |
+
- nvidia-cuda-nvrtc-cu12 12.4.99
|
23 |
+
- nvidia-cuda-runtime-cu12 12.4.99
|
24 |
+
- nvidia-cudnn-cu12 9.1.0.70
|
25 |
+
- numpy==1.26.4 (YOU MUST DOWNGRADE FROM THE NUMPY VERSION THAT CTRANSLATE2 INSTALLS BY DEFAULT)
|
26 |
+
- All other traditional dependencies like ```transformers```, ```accelerate```, etc.
|
27 |
|
28 |
+
<details><summary>Sample Script #1 (non-streaming):</summary>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
```
|
31 |
+
import sys
|
32 |
+
import os
|
33 |
+
os.environ['KMP_DUPLICATE_LIB_OK']='TRUE'
|
34 |
+
from pathlib import Path
|
35 |
+
|
36 |
+
def set_cuda_paths():
|
37 |
+
venv_base = Path(sys.executable).parent.parent
|
38 |
+
nvidia_base_path = venv_base / 'Lib' / 'site-packages' / 'nvidia'
|
39 |
+
cuda_path = nvidia_base_path / 'cuda_runtime' / 'bin'
|
40 |
+
cublas_path = nvidia_base_path / 'cublas' / 'bin'
|
41 |
+
cudnn_path = nvidia_base_path / 'cudnn' / 'bin'
|
42 |
+
nvrtc_path = nvidia_base_path / 'cuda_nvrtc' / 'bin'
|
43 |
+
|
44 |
+
paths_to_add = [
|
45 |
+
str(cuda_path),
|
46 |
+
str(cublas_path),
|
47 |
+
str(cudnn_path),
|
48 |
+
str(nvrtc_path),
|
49 |
+
]
|
50 |
+
|
51 |
+
env_vars = ['CUDA_PATH', 'CUDA_PATH_V12_4', 'PATH']
|
52 |
+
|
53 |
+
for env_var in env_vars:
|
54 |
+
current_value = os.environ.get(env_var, '')
|
55 |
+
new_value = os.pathsep.join(paths_to_add + [current_value] if current_value else paths_to_add)
|
56 |
+
os.environ[env_var] = new_value
|
57 |
+
|
58 |
+
set_cuda_paths()
|
59 |
+
|
60 |
+
import ctranslate2
|
61 |
+
import gc
|
62 |
+
import torch
|
63 |
+
from transformers import AutoTokenizer
|
64 |
+
import pynvml
|
65 |
+
from constants import user_message, system_message
|
66 |
+
|
67 |
+
pynvml.nvmlInit()
|
68 |
+
handle = pynvml.nvmlDeviceGetHandleByIndex(0)
|
69 |
+
|
70 |
+
model_dir = r"[INSERT PATH TO FOLDER CONTAINING THE MODEL FILES HERE]"
|
71 |
+
|
72 |
+
def build_prompt():
|
73 |
+
prompt = f"""<|im_start|>system
|
74 |
+
{system_message}<|im_end|>
|
75 |
+
<|im_start|>user
|
76 |
+
{user_message}<|im_end|>
|
77 |
+
<|im_start|>assistant
|
78 |
+
"""
|
79 |
+
return prompt
|
80 |
+
|
81 |
+
def main():
|
82 |
+
model_name = os.path.basename(model_dir)
|
83 |
+
beam_size_value = 1
|
84 |
+
intra_threads = max(os.cpu_count() - 4, 4)
|
85 |
+
|
86 |
+
generator = ctranslate2.Generator(
|
87 |
+
model_dir,
|
88 |
+
device="cuda",
|
89 |
+
compute_type="int8",
|
90 |
+
intra_threads=intra_threads
|
91 |
+
)
|
92 |
+
|
93 |
+
tokenizer = AutoTokenizer.from_pretrained(model_dir, add_prefix_space=None)
|
94 |
+
prompt = build_prompt()
|
95 |
+
tokens = tokenizer.convert_ids_to_tokens(tokenizer.encode(prompt))
|
96 |
+
|
97 |
+
results_batch = generator.generate_batch(
|
98 |
+
[tokens],
|
99 |
+
include_prompt_in_result=False,
|
100 |
+
max_batch_size=4096,
|
101 |
+
batch_type="tokens",
|
102 |
+
beam_size=beam_size_value,
|
103 |
+
num_hypotheses=1,
|
104 |
+
max_length=512,
|
105 |
+
sampling_temperature=0.0,
|
106 |
+
)
|
107 |
+
|
108 |
+
output = tokenizer.decode(results_batch[0].sequences_ids[0])
|
109 |
+
print("\nGenerated response:\n")
|
110 |
+
print(output)
|
111 |
+
|
112 |
+
del generator
|
113 |
+
del tokenizer
|
114 |
+
torch.cuda.empty_cache()
|
115 |
+
gc.collect()
|
116 |
+
|
117 |
+
if __name__ == "__main__":
|
118 |
+
main()
|
119 |
```
|
120 |
+
</details>
|
121 |
|
122 |
+
<details><summary>Sample Script #2 (streaming)</summary>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
```
|
125 |
+
import sys
|
126 |
+
import os
|
127 |
+
os.environ['KMP_DUPLICATE_LIB_OK']='TRUE'
|
128 |
+
from pathlib import Path
|
129 |
+
|
130 |
+
def set_cuda_paths():
|
131 |
+
venv_base = Path(sys.executable).parent.parent
|
132 |
+
nvidia_base_path = venv_base / 'Lib' / 'site-packages' / 'nvidia'
|
133 |
+
cuda_path = nvidia_base_path / 'cuda_runtime' / 'bin'
|
134 |
+
cublas_path = nvidia_base_path / 'cublas' / 'bin'
|
135 |
+
cudnn_path = nvidia_base_path / 'cudnn' / 'bin'
|
136 |
+
nvrtc_path = nvidia_base_path / 'cuda_nvrtc' / 'bin'
|
137 |
+
|
138 |
+
paths_to_add = [
|
139 |
+
str(cuda_path),
|
140 |
+
str(cublas_path),
|
141 |
+
str(cudnn_path),
|
142 |
+
str(nvrtc_path),
|
143 |
+
]
|
144 |
+
|
145 |
+
env_vars = ['CUDA_PATH', 'CUDA_PATH_V12_4', 'PATH']
|
146 |
+
|
147 |
+
for env_var in env_vars:
|
148 |
+
current_value = os.environ.get(env_var, '')
|
149 |
+
new_value = os.pathsep.join(paths_to_add + [current_value] if current_value else paths_to_add)
|
150 |
+
os.environ[env_var] = new_value
|
151 |
+
|
152 |
+
set_cuda_paths()
|
153 |
+
|
154 |
+
import ctranslate2
|
155 |
+
import gc
|
156 |
+
import torch
|
157 |
+
from transformers import AutoTokenizer
|
158 |
+
import pynvml
|
159 |
+
from constants import user_message, system_message
|
160 |
+
|
161 |
+
pynvml.nvmlInit()
|
162 |
+
handle = pynvml.nvmlDeviceGetHandleByIndex(0)
|
163 |
+
|
164 |
+
model_dir = r"[PATH TO FOLDER CONTAINING THE MODEL FILES]"
|
165 |
+
|
166 |
+
|
167 |
+
def build_prompt():
|
168 |
+
prompt = f"""<|im_start|>system
|
169 |
+
{system_message}<|im_end|>
|
170 |
+
<|im_start|>user
|
171 |
+
{user_message}<|im_end|>
|
172 |
+
<|im_start|>assistant
|
173 |
+
"""
|
174 |
+
return prompt
|
175 |
+
|
176 |
+
def main():
|
177 |
+
generator = ctranslate2.Generator(
|
178 |
+
model_dir,
|
179 |
+
device="cuda",
|
180 |
+
compute_type="int8",
|
181 |
+
)
|
182 |
+
|
183 |
+
tokenizer = AutoTokenizer.from_pretrained(model_dir)
|
184 |
+
prompt = build_prompt()
|
185 |
+
tokens = tokenizer.convert_ids_to_tokens(tokenizer.encode(prompt))
|
186 |
+
|
187 |
+
# Initialize token iterator
|
188 |
+
token_iterator = generator.generate_tokens(
|
189 |
+
[tokens],
|
190 |
+
max_length=512,
|
191 |
+
sampling_temperature=0.0
|
192 |
+
)
|
193 |
+
|
194 |
+
decoded_output = ""
|
195 |
+
tokens_buffer = []
|
196 |
+
|
197 |
+
try:
|
198 |
+
for token_result in token_iterator:
|
199 |
+
token_id = token_result.token_id
|
200 |
+
token = tokenizer.convert_ids_to_tokens(token_id)
|
201 |
+
|
202 |
+
if token_id == tokenizer.eos_token_id:
|
203 |
+
break
|
204 |
+
|
205 |
+
is_new_word = token.startswith("Ġ")
|
206 |
+
if is_new_word and tokens_buffer:
|
207 |
+
word = tokenizer.decode(tokens_buffer)
|
208 |
+
print(word, end='', flush=True)
|
209 |
+
decoded_output += word
|
210 |
+
tokens_buffer = []
|
211 |
+
|
212 |
+
tokens_buffer.append(token_id)
|
213 |
+
|
214 |
+
if tokens_buffer:
|
215 |
+
word = tokenizer.decode(tokens_buffer)
|
216 |
+
print(word, end='', flush=True)
|
217 |
+
decoded_output += word
|
218 |
+
|
219 |
+
except KeyboardInterrupt:
|
220 |
+
print("\nGeneration interrupted")
|
221 |
+
|
222 |
+
del generator
|
223 |
+
del tokenizer
|
224 |
+
torch.cuda.empty_cache()
|
225 |
+
gc.collect()
|
226 |
+
|
227 |
+
if __name__ == "__main__":
|
228 |
+
main()
|
229 |
```
|
230 |
+
</details>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|