-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
65 lines (47 loc) · 1.45 KB
/
main.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
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
import concurrent.futures
import time
# variables
debug = True
def read_file(filename):
with open(filename, "r") as f:
return f.read()
def generate_text(model, tokenizer, inputs):
inputs = tokenizer(inputs, return_tensors="pt").to(model.device)
model.config.pad_token_id = 0
with torch.no_grad():
tokens = model.generate(
**inputs,
max_new_tokens=48,
temperature=0.2,
do_sample=True,
)
return tokenizer.decode(tokens[0], skip_special_tokens=True, return_token_type_ids=False)
def main():
# Start the timer
if debug:
# Record the start time
start_time = time.time()
tokenizer = AutoTokenizer.from_pretrained(
"stabilityai/stablecode-instruct-alpha-3b",
use_auth_token=True
)
model = AutoModelForCausalLM.from_pretrained(
"stabilityai/stablecode-instruct-alpha-3b",
trust_remote_code=True,
torch_dtype="auto",
use_auth_token=True
).to("cuda:0")
inputs = read_file("instruction.txt")
generated_text = generate_text(model, tokenizer, inputs)
print(generated_text)
if debug:
# Record the end time
end_time = time.time()
# Calculate and print the execution time
execution_time = end_time - start_time
print("")
print(f"Total Execution Time: {execution_time:.2f} seconds")
if __name__ == "__main__":
main()