ericsorides
commited on
Commit
•
a4c13ff
1
Parent(s):
c6f344d
Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
tags:
|
3 |
+
- text-generation-inference
|
4 |
+
- gemma
|
5 |
+
- 4-bit precision
|
6 |
+
- AWQ
|
7 |
+
base_model:
|
8 |
+
- google/gemma-2b-it
|
9 |
+
---
|
10 |
+
|
11 |
+
|
12 |
+
# Gemma 2B instruct with Key-Value-Cache enabled in ONNX AWQ (4-bit) format
|
13 |
+
- Model creator: [Google](https://huggingface.co/google)
|
14 |
+
- Original model: [Gemma 2B instruct](https://huggingface.co/google/gemma-2b-it)
|
15 |
+
|
16 |
+
<!-- description start -->
|
17 |
+
## Description
|
18 |
+
|
19 |
+
This repo contains the ONNX files of the ONNX conversion of Gemma 2B instruct done by Esperanto Technologies.
|
20 |
+
The model is in the 4-bit format quantized with AWQ and has the KVC enabled.
|
21 |
+
|
22 |
+
### About AWQ
|
23 |
+
|
24 |
+
AWQ is an efficient, accurate and blazing-fast low-bit weight quantization method, currently supporting 4-bit quantization. Compared to GPTQ, it offers faster Transformers-based inference with equivalent or better quality compared to the most commonly used GPTQ settings.
|
25 |
+
More here: [AutoAWQ](https://github.com/casper-hansen/AutoAWQ)
|
26 |
+
|
27 |
+
<!-- description end -->
|
28 |
+
|
29 |
+
## How to download ONNX model and weight files
|
30 |
+
|
31 |
+
The easiest way to obtain the model is to clone this whole repo.
|
32 |
+
Alternatively you can download the files is using the `huggingface-hub` Python library.
|
33 |
+
|
34 |
+
```shell
|
35 |
+
pip3 install huggingface-hub>=0.17.1
|
36 |
+
```
|
37 |
+
|
38 |
+
Then you can download any individual model file to the current directory, at high speed, with a command like this:
|
39 |
+
|
40 |
+
```shell
|
41 |
+
huggingface-cli download Esperanto/gemma-2b-it-kvc-AWQ-int4-onnx --local-dir gemma-2b-it-kvc-AWQ-int4-onnx --local-dir-use-symlinks False
|
42 |
+
```
|
43 |
+
|
44 |
+
For more documentation on downloading with `huggingface-cli`, please see: [HF -> Hub Python Library -> Download files -> Download from the CLI](https://huggingface.co/docs/huggingface_hub/guides/download#download-from-the-cli).
|
45 |
+
|
46 |
+
## How to run from Python code using ONNXRuntime
|
47 |
+
|
48 |
+
This model can easily be ran in a CPU using [ONNXRuntime](https://onnxruntime.ai/).
|
49 |
+
|
50 |
+
#### First install the packages
|
51 |
+
|
52 |
+
```bash
|
53 |
+
pip3 install onnx==1.16.1
|
54 |
+
pip3 install onnxruntime==1.17.1
|
55 |
+
```
|
56 |
+
|
57 |
+
#### Example code: generate text with this model
|
58 |
+
|
59 |
+
We define the loop with greedy decoding:
|
60 |
+
```python
|
61 |
+
import numpy as np
|
62 |
+
import onnxruntime
|
63 |
+
import onnx
|
64 |
+
from transformers import AutoTokenizer
|
65 |
+
|
66 |
+
def generate_text(model_path, prompt, tokenizer, max_gen_tokens, total_sequence, window, context):
|
67 |
+
model = onnx.load(model_path)
|
68 |
+
|
69 |
+
#we create the inputs for the first iteration
|
70 |
+
input_tensor = tokenizer(prompt, return_tensors="pt")
|
71 |
+
prompt_size = len(input_tensor['input_ids'][0])
|
72 |
+
actual_input = input_tensor['input_ids']
|
73 |
+
if prompt_size < window:
|
74 |
+
actual_input = np.concatenate((tokenizer.bos_token_id*np.ones([1, window - prompt_size], dtype = 'int64'),
|
75 |
+
actual_input), axis=1)
|
76 |
+
if prompt_size + max_gen_tokens > total_sequence:
|
77 |
+
print("ERROR: Longer total sequence is needed!")
|
78 |
+
return
|
79 |
+
first_attention = np.concatenate((np.zeros([1, total_sequence - window], dtype = 'int64'),
|
80 |
+
np.ones((1, window), dtype = 'int64')), axis=1)
|
81 |
+
max_gen_tokens += prompt_size #we need to generate on top of parsing the prompt
|
82 |
+
inputs_names =[node.name for node in model.graph.input]
|
83 |
+
output_names =[node.name for node in model.graph.output]
|
84 |
+
n_heads = 1 #gqa-heads of the kvc
|
85 |
+
inputs_dict = {}
|
86 |
+
inputs_dict['input_ids'] = actual_input[:, :window].reshape(1, window).numpy()
|
87 |
+
inputs_dict['attention_mask'] = first_attention
|
88 |
+
for name in inputs_names:
|
89 |
+
if name == 'input_ids' or name == 'attention_mask': continue
|
90 |
+
inputs_dict[name] = np.zeros([1, n_heads, context-window, 256], dtype="float16")
|
91 |
+
index = 0
|
92 |
+
new_token = np.array([10])
|
93 |
+
next_index = window
|
94 |
+
old_j = 0
|
95 |
+
total_input = actual_input.numpy()
|
96 |
+
|
97 |
+
rt_session = onnxruntime.InferenceSession(model_path)
|
98 |
+
## We run the inferences
|
99 |
+
while next_index < max_gen_tokens:
|
100 |
+
if new_token.any() == tokenizer.eos_token_id:
|
101 |
+
break
|
102 |
+
#inference
|
103 |
+
output = rt_session.run(output_names, inputs_dict)
|
104 |
+
outs_dictionary = {name: content for (name, content) in zip (output_names, output)}
|
105 |
+
#we prepare the inputs for the next inference
|
106 |
+
for name in inputs_names:
|
107 |
+
if name == 'input_ids':
|
108 |
+
old_j = next_index
|
109 |
+
if next_index < prompt_size:
|
110 |
+
if prompt_size - next_index >= window: next_index += window
|
111 |
+
else: next_index = prompt_size
|
112 |
+
j = next_index - window
|
113 |
+
else:
|
114 |
+
next_index +=1
|
115 |
+
j = next_index - window
|
116 |
+
new_token = outs_dictionary['logits'].argmax(-1).reshape(1, window)
|
117 |
+
total_input = np.concatenate((total_input, new_token[: , -1:]), axis = 1)
|
118 |
+
inputs_dict['input_ids']= total_input[:, j:next_index].reshape(1, window)
|
119 |
+
elif name == 'attention_mask':
|
120 |
+
inputs_dict['attention_mask'] = np.concatenate((np.zeros((1, total_sequence-next_index), dtype = 'int64'), np.ones((1, next_index), dtype = 'int64')), axis=1)
|
121 |
+
else:
|
122 |
+
old_name = name.replace("past_key_values", "present")
|
123 |
+
inputs_dict[name] = outs_dictionary[old_name][:, :, next_index-old_j:context-window+(next_index - old_j), :]
|
124 |
+
|
125 |
+
answer = tokenizer.decode(total_input[0], skip_special_tokens=True, clean_up_tokenization_spaces=False)
|
126 |
+
return answer
|
127 |
+
```
|
128 |
+
We now run the inferences:
|
129 |
+
|
130 |
+
```python
|
131 |
+
tokenizer = AutoTokenizer.from_pretrained("Esperanto/gemma-2b-it-kvc-AWQ-int4-onnx")
|
132 |
+
model_path = "gemma-2b-it-kvc-AWQ-int4-onnx/model.onnx"
|
133 |
+
|
134 |
+
max_gen_tokens = 20 #number of tokens we want tog eneral
|
135 |
+
total_sequence = 128 #total sequence_length
|
136 |
+
context = 1024 #the context to extend the kvc
|
137 |
+
window = 16 #number of tokens we want to parse at the time
|
138 |
+
messages = [
|
139 |
+
{"role": "system", "content": "You are a pirate chatbot who always responds in pirate speak!"},
|
140 |
+
{"role": "user", "content": "Who are you?"},
|
141 |
+
]
|
142 |
+
|
143 |
+
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
144 |
+
|
145 |
+
generated = generate_text(model_path, prompt, tokenizer, max_gen_tokens, total_sequence, window, context)
|
146 |
+
print(generated)
|
147 |
+
```
|