ericsorides commited on
Commit
d9d9c6c
·
verified ·
1 Parent(s): 7c2fa05

Create README.md

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