Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ONNX example #1

Closed
lutzroeder opened this issue May 27, 2023 · 0 comments
Closed

ONNX example #1

lutzroeder opened this issue May 27, 2023 · 0 comments

Comments

@lutzroeder
Copy link
Owner

lutzroeder commented May 27, 2023

Export

torch.onnx.export(model, context, 'gpt2.onnx',
    input_names=['input', 'temperature', 'top_k'],
    output_names=['output'],
    dynamic_axes={'input': {0: 'batch', 1: 'tokens'}, 'output': {0: 'batch'}})

Inference

import sys
import onnxruntime
import tiktoken
prompt = 'The Eiffel tower is in' if len(sys.argv) <= 1 else sys.argv[1]
encoding = tiktoken.get_encoding('gpt2')
context = encoding.encode(prompt, allowed_special={'<|endoftext|>'})
session = onnxruntime.InferenceSession('gpt2.onnx')
print(prompt, end='', flush=True)
for _ in range(50): # max_tokens
    outputs = session.run(['output'], {'input': [context], 'temperature': [0.7], 'top_k': [40]})
    y = outputs[0][0]
    print(encoding.decode(y), end='', flush=True)
    context.extend(y)
print('')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant