Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import onnxruntime_genai as og
|
2 |
+
import argparse
|
3 |
+
import time
|
4 |
+
|
5 |
+
def main(args):
|
6 |
+
if args.verbose: print("Loading model...")
|
7 |
+
if args.timings:
|
8 |
+
started_timestamp = 0
|
9 |
+
first_token_timestamp = 0
|
10 |
+
|
11 |
+
model = og.Model(f'{args.model}')
|
12 |
+
if args.verbose: print("Model loaded")
|
13 |
+
tokenizer = og.Tokenizer(model)
|
14 |
+
tokenizer_stream = tokenizer.create_stream()
|
15 |
+
if args.verbose: print("Tokenizer created")
|
16 |
+
if args.verbose: print()
|
17 |
+
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}
|
18 |
+
|
19 |
+
# Keep asking for input prompts in a loop
|
20 |
+
while True:
|
21 |
+
text = input("Input: ")
|
22 |
+
if not text:
|
23 |
+
print("Error, input cannot be empty")
|
24 |
+
continue
|
25 |
+
|
26 |
+
if args.timings: started_timestamp = time.time()
|
27 |
+
|
28 |
+
input_tokens = tokenizer.encode(args.system_prompt + text)
|
29 |
+
|
30 |
+
params = og.GeneratorParams(model)
|
31 |
+
params.try_use_cuda_graph_with_max_batch_size(1)
|
32 |
+
params.set_search_options(**search_options)
|
33 |
+
params.input_ids = input_tokens
|
34 |
+
generator = og.Generator(model, params)
|
35 |
+
if args.verbose: print("Generator created")
|
36 |
+
|
37 |
+
if args.verbose: print("Running generation loop ...")
|
38 |
+
if args.timings:
|
39 |
+
first = True
|
40 |
+
new_tokens = []
|
41 |
+
|
42 |
+
print()
|
43 |
+
print("Output: ", end='', flush=True)
|
44 |
+
|
45 |
+
try:
|
46 |
+
while not generator.is_done():
|
47 |
+
generator.compute_logits()
|
48 |
+
generator.generate_next_token()
|
49 |
+
if args.timings:
|
50 |
+
if first:
|
51 |
+
first_token_timestamp = time.time()
|
52 |
+
first = False
|
53 |
+
|
54 |
+
new_token = generator.get_next_tokens()[0]
|
55 |
+
print(tokenizer_stream.decode(new_token), end='', flush=True)
|
56 |
+
if args.timings: new_tokens.append(new_token)
|
57 |
+
except KeyboardInterrupt:
|
58 |
+
print(" --control+c pressed, aborting generation--")
|
59 |
+
print()
|
60 |
+
print()
|
61 |
+
|
62 |
+
if args.timings:
|
63 |
+
prompt_time = first_token_timestamp - started_timestamp
|
64 |
+
run_time = time.time() - first_token_timestamp
|
65 |
+
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")
|
66 |
+
|
67 |
+
|
68 |
+
if __name__ == "__main__":
|
69 |
+
parser = argparse.ArgumentParser(argument_default=argparse.SUPPRESS, description="End-to-end AI Question/Answer example for gen-ai")
|
70 |
+
parser.add_argument('-m', '--model', type=str, required=True, help='Onnx model folder path (must contain config.json and model.onnx)')
|
71 |
+
parser.add_argument('-i', '--min_length', type=int, help='Min number of tokens to generate including the prompt')
|
72 |
+
parser.add_argument('-l', '--max_length', type=int, help='Max number of tokens to generate including the prompt')
|
73 |
+
parser.add_argument('-ds', '--do_random_sampling', action='store_true', help='Do random sampling. When false, greedy or beam search are used to generate the output. Defaults to false')
|
74 |
+
parser.add_argument('-p', '--top_p', type=float, help='Top p probability to sample with')
|
75 |
+
parser.add_argument('-k', '--top_k', type=int, help='Top k tokens to sample from')
|
76 |
+
parser.add_argument('-t', '--temperature', type=float, help='Temperature to sample with')
|
77 |
+
parser.add_argument('-r', '--repetition_penalty', type=float, help='Repetition penalty to sample with')
|
78 |
+
parser.add_argument('-v', '--verbose', action='store_true', default=False, help='Print verbose output and timing information. Defaults to false')
|
79 |
+
parser.add_argument('-s', '--system_prompt', type=str, default='', help='Prepend a system prompt to the user input prompt. Defaults to empty')
|
80 |
+
parser.add_argument('-g', '--timings', action='store_true', default=False, help='Print timing information for each generation step. Defaults to false')
|
81 |
+
args = parser.parse_args()
|
82 |
+
main(args)
|