FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
starcoder/chat/generate.py at main · bigcode-project/starcoder · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
bigcode-project
/
starcoder
Public
Notifications
You must be signed in to change notification settings
Fork
524
Star
7.5k
Code
Issues
95
Pull requests
8
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
starcoder
/
chat
/
generate.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
139 lines (126 loc) · 5.03 KB
Breadcrumbs
starcoder
/
chat
/
generate.py
Copy path
File metadata and controls
139 lines (126 loc) · 5.03 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# coding=utf-8
# Copyright 2023 The BigCode and HuggingFace teams. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""A simple script to quickly check the model outputs of a generative model"""
import
argparse
import
torch
from
dialogues
import
DialogueTemplate
,
get_dialogue_template
from
transformers
import
(
AutoModelForCausalLM
,
AutoTokenizer
,
GenerationConfig
,
set_seed
)
def
main
():
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
"--model_id"
,
type
=
str
,
help
=
"Name of model to generate samples with"
,
)
parser
.
add_argument
(
"--revision"
,
type
=
str
,
default
=
None
,
help
=
"The model repo's revision to use"
,
)
parser
.
add_argument
(
"--system_prompt"
,
type
=
str
,
default
=
None
,
help
=
"Overrides the dialogue template's system prompt"
)
args
=
parser
.
parse_args
()
# Set seed for reproducibility
set_seed
(
42
)
prompts
=
[
[
{
"role"
:
"user"
,
"content"
:
"Develop a C++ program that reads a text file line by line and counts the number of occurrences of a specific word in the file."
,
}
],
[
{
"role"
:
"user"
,
"content"
:
"Implement a Python function to find the longest common subsequence of two input strings using dynamic programming."
,
}
],
[{
"role"
:
"user"
,
"content"
:
"Implement a regular expression in Python to validate an email address."
}],
[
{
"role"
:
"user"
,
"content"
:
"Write a program to find the nth Fibonacci number using dynamic programming."
,
}
],
[
{
"role"
:
"user"
,
"content"
:
"Implement a binary search algorithm to find a specific element in a sorted array."
,
}
],
[{
"role"
:
"user"
,
"content"
:
"Implement a queue data structure using two stacks in Python."
}],
[
{
"role"
:
"user"
,
"content"
:
"Implement a program to find the common elements in two arrays without using any extra data structures."
,
}
],
]
try
:
dialogue_template
=
DialogueTemplate
.
from_pretrained
(
args
.
model_id
,
revision
=
args
.
revision
)
except
Exception
:
print
(
"No dialogue template found in model repo. Defaulting to the `no_system` template."
)
dialogue_template
=
get_dialogue_template
(
"no_system"
)
if
args
.
system_prompt
is
not
None
:
dialogue_template
.
system
=
args
.
system_prompt
formatted_prompts
=
[]
for
prompt
in
prompts
:
dialogue_template
.
messages
=
[
prompt
]
if
isinstance
(
prompt
,
dict
)
else
prompt
formatted_prompts
.
append
(
dialogue_template
.
get_inference_prompt
())
print
(
"=== SAMPLE PROMPT ==="
)
print
(
formatted_prompts
[
0
])
print
(
"====================="
)
device
=
"cuda"
if
torch
.
cuda
.
is_available
()
else
"cpu"
tokenizer
=
AutoTokenizer
.
from_pretrained
(
args
.
model_id
,
revision
=
args
.
revision
)
print
(
f"Special tokens:
{
tokenizer
.
special_tokens_map
}
"
)
print
(
f"EOS token ID for generation:
{
tokenizer
.
convert_tokens_to_ids
(
dialogue_template
.
end_token
)
}
"
)
generation_config
=
GenerationConfig
(
temperature
=
0.2
,
top_k
=
50
,
top_p
=
0.95
,
repetition_penalty
=
1.2
,
do_sample
=
True
,
pad_token_id
=
tokenizer
.
eos_token_id
,
eos_token_id
=
tokenizer
.
convert_tokens_to_ids
(
dialogue_template
.
end_token
),
min_new_tokens
=
32
,
max_new_tokens
=
256
,
)
model
=
AutoModelForCausalLM
.
from_pretrained
(
args
.
model_id
,
revision
=
args
.
revision
,
load_in_8bit
=
True
,
device_map
=
"auto"
,
torch_dtype
=
torch
.
float16
)
outputs
=
""
for
idx
,
prompt
in
enumerate
(
formatted_prompts
):
batch
=
tokenizer
(
prompt
,
return_tensors
=
"pt"
,
return_token_type_ids
=
False
).
to
(
device
)
generated_ids
=
model
.
generate
(
**
batch
,
generation_config
=
generation_config
)
generated_text
=
tokenizer
.
decode
(
generated_ids
[
0
],
skip_special_tokens
=
False
).
lstrip
()
outputs
+=
generated_text
+
"
\n
\n
"
print
(
f"=== EXAMPLE
{
idx
}
==="
)
print
()
print
(
generated_text
)
print
()
print
(
"======================"
)
print
()
raw_model_name
=
args
.
model_id
.
split
(
"/"
)[
-
1
]
model_name
=
f"
{
raw_model_name
}
"
if
args
.
revision
is
not
None
:
model_name
+=
f"-
{
args
.
revision
}
"
with
open
(
f"data/samples-
{
model_name
}
.txt"
,
"w"
,
encoding
=
"utf-8"
)
as
f
:
f
.
write
(
outputs
)
if
__name__
==
"__main__"
:
main
()
Back
|
FazBrowse Home
|
New Git URL