-
Notifications
You must be signed in to change notification settings - Fork 4
/
tiny_starcoder.py
44 lines (37 loc) · 1.58 KB
/
tiny_starcoder.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
# pip install -q transformers==4.29.2
from transformers import AutoModelForCausalLM, AutoTokenizer
checkpoint = "bigcode/tiny_starcoder_py"
device = "cuda" # for GPU usage or "cpu" for CPU usage
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
model = AutoModelForCausalLM.from_pretrained(checkpoint).to(device)
# Sane hyper-parameters
params = {
'max_new_tokens': 128,
'temperature': 0.2,
'top_k': 50,
'top_p': 0.1,
'repetition_penalty': 1.17
}
# Prompt Style 1: Function Signature
inputs = tokenizer.encode("def print_hello_world():", return_tensors="pt").to(device)
outputs = model.generate(inputs, pad_token_id=tokenizer.eos_token_id, **params)
print()
print(tokenizer.decode(outputs[0]))
print()
# Prompt Style 2: A comment
inputs = tokenizer.encode("# a python function that says hello\n", return_tensors="pt").to(device)
outputs = model.generate(inputs, pad_token_id=tokenizer.eos_token_id, **params)
print()
print(tokenizer.decode(outputs[0]))
print()
# Prompt Style 3: A docstring
inputs = tokenizer.encode("\"\"\" a python function that says hello \"\"\"\n", return_tensors="pt").to(device)
outputs = model.generate(inputs, pad_token_id=tokenizer.eos_token_id, **params)
print()
print(tokenizer.decode(outputs[0]))
print()
# Prompt Style 4: [ADVANCED] Fill in the middle
input_text = "<fim_prefix>def print_one_two_three():\n print('one')\n <fim_suffix>\n print('three')<fim_middle>"
inputs = tokenizer.encode(input_text, return_tensors="pt").to(device)
outputs = model.generate(inputs, pad_token_id=tokenizer.eos_token_id, **params)
print(tokenizer.decode(outputs[0]))