jester6136 commited on
Commit
888ea19
·
verified ·
1 Parent(s): cbef04e

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +131 -3
README.md CHANGED
@@ -1,3 +1,131 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ language:
4
+ - en
5
+ tags:
6
+ - onnxruntime_genai
7
+ - llm
8
+ - llama3
9
+ pipeline_tag: text-generation
10
+ ---
11
+
12
+ #### This is an optimized version of the Llama 3 8B Instruct model.
13
+
14
+ # Llama-3-8B-Instruct for ONNX Runtime
15
+
16
+ ## Introduction
17
+
18
+ This repository hosts the optimized versions of **Llama-3** to accelerate inference with ONNX Runtime CUDA execution provider.
19
+
20
+ ## Usage Example
21
+ To make running of the Llama-3-8B-Instruct models across a range of devices and platforms across various execution provider backends possible, we introduce a new API to wrap several aspects of generative AI inferencing. This API make it easy to drag and drop LLMs straight into your app.
22
+
23
+ Example steps:
24
+
25
+ 1. Install required dependencies.
26
+ ```shell
27
+ pip install numpy
28
+ pip install --pre onnxruntime-genai
29
+ ```
30
+
31
+ 2. Inference using manual model API:
32
+ ```python
33
+ import onnxruntime_genai as og
34
+ import argparse
35
+ import time
36
+
37
+ def main(args):
38
+ if args.verbose: print("Loading model...")
39
+ if args.timings:
40
+ started_timestamp = 0
41
+ first_token_timestamp = 0
42
+
43
+ model = og.Model(f'{args.model}')
44
+ if args.verbose: print("Model loaded")
45
+ tokenizer = og.Tokenizer(model)
46
+ tokenizer_stream = tokenizer.create_stream()
47
+ if args.verbose: print("Tokenizer created")
48
+ if args.verbose: print()
49
+ search_options = {name:getattr(args, name) for name in ['do_sample', 'max_length', 'min_length', 'top_p', 'top_k', 'temperature', 'repetition_penalty'] if name in args}
50
+
51
+ # Set the max length to something sensible by default, unless it is specified by the user,
52
+ # since otherwise it will be set to the entire context length
53
+ if 'max_length' not in search_options:
54
+ search_options['max_length'] = 2048
55
+
56
+ chat_template = '<|start_header_id|>user<|end_header_id|>\n{input}<|eot_id|><|start_header_id|>assistant<|end_header_id|>'
57
+
58
+ # Keep asking for input prompts in a loop
59
+ while True:
60
+ text = input("Input: ")
61
+ if not text:
62
+ print("Error, input cannot be empty")
63
+ continue
64
+
65
+ if args.timings: started_timestamp = time.time()
66
+
67
+ # If there is a chat template, use it
68
+ prompt = f'{chat_template.format(input=text)}'
69
+
70
+ input_tokens = tokenizer.encode(prompt)
71
+
72
+ params = og.GeneratorParams(model)
73
+ params.set_search_options(**search_options)
74
+ params.input_ids = input_tokens
75
+ generator = og.Generator(model, params)
76
+ if args.verbose: print("Generator created")
77
+
78
+ if args.verbose: print("Running generation loop ...")
79
+ if args.timings:
80
+ first = True
81
+ new_tokens = []
82
+
83
+ print()
84
+ print("Output: ", end='', flush=True)
85
+
86
+ try:
87
+ while not generator.is_done():
88
+ generator.compute_logits()
89
+ generator.generate_next_token()
90
+ if args.timings:
91
+ if first:
92
+ first_token_timestamp = time.time()
93
+ first = False
94
+
95
+ new_token = generator.get_next_tokens()[0]
96
+ print(tokenizer_stream.decode(new_token), end='', flush=True)
97
+ if args.timings: new_tokens.append(new_token)
98
+ except KeyboardInterrupt:
99
+ print(" --control+c pressed, aborting generation--")
100
+ print()
101
+ print()
102
+
103
+ # Delete the generator to free the captured graph for the next generator, if graph capture is enabled
104
+ del generator
105
+
106
+ if args.timings:
107
+ prompt_time = first_token_timestamp - started_timestamp
108
+ run_time = time.time() - first_token_timestamp
109
+ print(f"Prompt length: {len(input_tokens)}, New tokens: {len(new_tokens)}, Time to first: {(prompt_time):.2f}s, Prompt tokens per second: {len(input_tokens)/prompt_time:.2f} tps, New tokens per second: {len(new_tokens)/run_time:.2f} tps")
110
+
111
+
112
+ if __name__ == "__main__":
113
+ parser = argparse.ArgumentParser(argument_default=argparse.SUPPRESS, description="End-to-end AI Question/Answer example for gen-ai")
114
+ parser.add_argument('-m', '--model', type=str, required=True, help='Onnx model folder path (must contain config.json and model.onnx)')
115
+ parser.add_argument('-i', '--min_length', type=int, help='Min number of tokens to generate including the prompt')
116
+ parser.add_argument('-l', '--max_length', type=int, help='Max number of tokens to generate including the prompt')
117
+ parser.add_argument('-ds', '--do_sample', action='store_true', default=False, help='Do random sampling. When false, greedy or beam search are used to generate the output. Defaults to false')
118
+ parser.add_argument('-p', '--top_p', type=float, help='Top p probability to sample with')
119
+ parser.add_argument('-k', '--top_k', type=int, help='Top k tokens to sample from')
120
+ parser.add_argument('-t', '--temperature', type=float, help='Temperature to sample with')
121
+ parser.add_argument('-r', '--repetition_penalty', type=float, help='Repetition penalty to sample with')
122
+ parser.add_argument('-v', '--verbose', action='store_true', default=False, help='Print verbose output and timing information. Defaults to false')
123
+ parser.add_argument('-g', '--timings', action='store_true', default=False, help='Print timing information for each generation step. Defaults to false')
124
+ args = parser.parse_args()
125
+ main(args)
126
+ ```
127
+
128
+ 3. Run API:
129
+ ```python
130
+ python model-qa.py -m /*{YourModelPath}*/onnx/cpu_and_mobile/phi-3-mini-4k-instruct-int4-cpu -k 40 -p 0.95 -t 0.8 -r 1.0
131
+ ```