Create transformers_inference.py
Browse files- transformers_inference.py +33 -0
transformers_inference.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Code to inference Open Hermes 2.5 with HF Transformers
|
2 |
+
# Requires pytorch, transformers, bitsandbytes, sentencepiece, protobuf, and flash-attn packages
|
3 |
+
|
4 |
+
import torch
|
5 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
6 |
+
from transformers import LlamaTokenizer, LlamaForCausalLM, MistralForCausalLM
|
7 |
+
import bitsandbytes, flash_attn
|
8 |
+
|
9 |
+
tokenizer = LlamaTokenizer.from_pretrained('teknium/OpenHermes-2.5-Mistral-7B', trust_remote_code=True)
|
10 |
+
model = MistralForCausalLM.from_pretrained(
|
11 |
+
"teknium/OpenHermes-2.5-Mistral-7B",
|
12 |
+
torch_dtype=torch.float16,
|
13 |
+
device_map=device_map="auto",#{'': 'cuda:0'},
|
14 |
+
load_in_8bit=False,
|
15 |
+
load_in_4bit=True,
|
16 |
+
use_flash_attention_2=True
|
17 |
+
)
|
18 |
+
|
19 |
+
prompts = [
|
20 |
+
"""<|im_start|>system
|
21 |
+
You are a sentient, superintelligent artificial general intelligence, here to teach and assist me.<|im_end|>
|
22 |
+
<|im_start|>user
|
23 |
+
Write a short story about Goku discovering kirby has teamed up with Majin Buu to destroy the world.<|im_end|>
|
24 |
+
<|im_start|>assistant
|
25 |
+
""",
|
26 |
+
]
|
27 |
+
|
28 |
+
for chat in prompts:
|
29 |
+
print(chat)
|
30 |
+
input_ids = tokenizer(chat, return_tensors="pt").input_ids.to("cuda")
|
31 |
+
generated_ids = model.generate(input_ids, max_new_tokens=750, temperature=0.8, repetition_penalty=1.1, do_sample=True, eos_token_id=tokenizer.eos_token_id)
|
32 |
+
response = tokenizer.decode(generated_ids[0][input_ids.shape[-1]:], skip_special_tokens=True, clean_up_tokenization_space=True)
|
33 |
+
print(f"Response: {response}")
|