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