Spaces:
Runtime error
Runtime error
Create llm.py
Browse files
llm.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
2 |
+
import torch
|
3 |
+
import time
|
4 |
+
|
5 |
+
|
6 |
+
class Gemma2B:
|
7 |
+
def __init__(self):
|
8 |
+
self.model_name = "google/gemma-2b-it"
|
9 |
+
self.tokenizer = AutoTokenizer.from_pretrained(self.model_name)
|
10 |
+
self.model = AutoModelForCausalLM.from_pretrained(self.model_name, torch_dtype=torch.bfloat16, )
|
11 |
+
|
12 |
+
def inference_cpu(self, chat_template, max_new_tokens=200):
|
13 |
+
chat = self.tokenizer.apply_chat_template(chat_template, tokenize=False,
|
14 |
+
add_generation_prompt=True)
|
15 |
+
|
16 |
+
input_ids = self.tokenizer(chat, return_tensors="pt")
|
17 |
+
outputs = self.model.generate(**input_ids, max_length=300, max_new_tokens=300)
|
18 |
+
return self.tokenizer.decode(outputs[0])
|
19 |
+
|
20 |
+
|
21 |
+
if __name__ == "__main__":
|
22 |
+
llm = Gemma2B()
|
23 |
+
start_time_cpu = time.time()
|
24 |
+
print(llm.inference_cpu(
|
25 |
+
[
|
26 |
+
{"role": "user", "content": f"hello"}]
|
27 |
+
))
|
28 |
+
end_time_cpu = time.time()
|
29 |
+
print(f"CPU Inference Time: {end_time_cpu - start_time_cpu}")
|