-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patholmo_inference.py
66 lines (53 loc) · 2.09 KB
/
olmo_inference.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# system imports
import time
import json
# external imports
from transformers import GPTNeoXForCausalLM, AutoModelForCausalLM, AutoTokenizer, OlmoForCausalLM
import torch
from tqdm import tqdm
from datasets import load_dataset
# local imports
# enivornment setup
torch.manual_seed(42)
torch.cuda.manual_seed(42)
torch.mps.manual_seed(42)
# -------------------------Start of Script------------------------- #
# attempt to auto recognize the device!
device = "cpu"
if torch.cuda.is_available():
device = "cuda"
elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
device = "mps"
print(f"using device {device}")
model_id_olmo_1b_base = "allenai/OLMo-1B-0724-hf"
model_id_olmo_1b_sft = "hamishivi/OLMo-1B-0724-SFT-hf"
model_id_olmo_1b_inst = "hamishivi/OLMo-1B-0724-Instruct-hf"
model_id_olmo_7b_base = "allenai/OLMo-7B-0724-hf"
model_id_olmo_7b_sft = "allenai/OLMo-7B-0724-SFT-hf"
model_id_olmo_7b_inst = "allenai/OLMo-7B-0724-Instruct-hf"
model_id_olmo = model_id_olmo_7b_inst
cache_dir_olmo = "./models/" + model_id_olmo
print(f"loading {model_id_olmo}")
model = AutoModelForCausalLM.from_pretrained(
pretrained_model_name_or_path=model_id_olmo,
# revision="step3000",
cache_dir=cache_dir_olmo,
device_map=device,
)
tokenizer = AutoTokenizer.from_pretrained(
pretrained_model_name_or_path=model_id_olmo,
cache_dir=cache_dir_olmo,
)
for i in range(5):
message = ""
equation = input()
message += equation
# inputs = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt")
inputs = tokenizer(message, return_tensors="pt").to(model.device)
response = model.generate(**inputs,
max_new_tokens=100,
do_sample=False,
)
print(tokenizer.batch_decode(response, skip_special_tokens=True)[0])
print("-"*100)
# Mor Geva et al. have shown that the MLP acts as an associative key-value memory (where we make a critical distinction against the words "key" and "value" from the attention operation). Furthermore,