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