-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnotebook.py
240 lines (207 loc) · 9.33 KB
/
notebook.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# -*- coding: utf-8 -*-
"""Untitled11.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/11JsYh0UnnMH0hbxcRt1bpgBHI-DxoQuH
"""
import json
import os
if not os.path.exists("/tmp/credentials.json"):
raise AssertionError("Please provide a /tmp/credentials.json")
credentials = json.load(open("/tmp/credentials.json"))
# github_access_token = credentials["github_token"]
os.environ["WANDB_API_KEY"] = credentials["wandb_api_key"]
# hf_auth_token = credentials["hf_auth_token"]
del credentials
# Commented out IPython magic to ensure Python compatibility.
# %%capture
#
# !pip install transformers peft wandb
import tempfile
import wandb
def download_lora(wandb_run_id, epoch_num):
run = wandb.Api().run(wandb_run_id)
[artifact] = [artifact_ for artifact_ in run.logged_artifacts() if artifact_.metadata.get("epoch_num") == epoch_num]
download_dir = tempfile.mkdtemp()
artifact.download(root=download_dir)
return download_dir
from concurrent.futures import ThreadPoolExecutor
from concurrent import futures
import os
import tempfile
from dataclasses import dataclass
from peft import PeftModel
from tqdm import tqdm
from typing import Any, List
import requests
CLOUD_LORA_URL = "https://my-cloud-run-service-m3v6o3jpna-uc.a.run.app"
class CloudLora:
def __init__(self, lora_dir: str):
self.lora_dir = lora_dir
@staticmethod
def from_lora_dir(lora_dir: str):
return CloudLora(lora_dir=lora_dir)
@staticmethod
def from_peft_model(peft_model: PeftModel):
tmpdir = tempfile.mkdtemp()
peft_model.save_pretrained(tmpdir)
return CloudLora(lora_dir=tmpdir)
def map_prompts(self, prompts: List[str]):
response = requests.post(f"{CLOUD_LORA_URL}/upload-files", files=dict(
adapter_config=open(os.path.join(self.lora_dir, "adapter_config.json"), "rb"),
adapter_model=open(os.path.join(self.lora_dir, "adapter_model.bin"), "rb"),
))
if response.status_code != 200:
print("uuid request error", response.status_code, response.text)
cloud_weights_uuid = response.json()["uuid"]
def fetch_response(prompt):
response = requests.post(
f"{CLOUD_LORA_URL}/generate/{cloud_weights_uuid}",
headers={"Content-Type": "application/json"},
json={"prompt": prompt}
)
if response.status_code != 200:
print("generation request error", prompt, response.status_code, response.text)
return None
return prompt + response.json()["output"]["choices"][0]["text"]
# print(prompt, response.status_code, response.text)
fs = []
with ThreadPoolExecutor() as executor:
for prompt in prompts:
fut = executor.submit(fetch_response, prompt)
fs.append(fut)
# results = list(executor.map(fetch_response, prompts))
results = []
for fut in futures.as_completed(fs):
result = fut.result()
print(result)
results.append(result)
return results
# https://wandb.ai/ericyu3-fine-tuning/fine-tuning/runs/gzfqekzc?workspace=user-ericyu3_
run_id = "ericyu3-fine-tuning/fine-tuning/gzfqekzc"
epoch_to_load = 10
lerp_ratio = 1.0
lora_id = str(hash((run_id, epoch_to_load, lerp_ratio)))
lora_dir = download_lora(run_id, epoch_num=epoch_to_load)
CloudLora.from_lora_dir(lora_dir).map_prompts([
"What is the output of 'print(5 // 2)'?",
"What is the purpose of the 'pass' keyword in Python?",
"How can you convert a string to a list of characters?",
"What is the difference between 'is' and '=='?",
"How can you reverse a list in Python?",
"What is the result of '5 * 3'?",
"How would you remove duplicates from a list?",
"What is the output of 'print(5 % 2)'?",
"How would you check the length of a list?",
"What is the output of 'print(5 ** 2)'?",
"How do you concatenate two strings?",
"What is a lambda function?",
"What is the difference between a tuple and a list?",
"What is a dictionary in Python?",
"How do you create a function in Python?",
"What is the difference between a method and a function?",
"How do you check the type of a variable?",
"What is the output of 'print('Hello' * 3)'?",
"How do you create a class in Python?",
"What is inheritance in object-oriented programming?",
"How do you handle exceptions in Python?",
"What is the difference between a set and a list?",
"How do you iterate over a dictionary?",
"What is the use of the 'global' keyword?",
"How do you write a recursive function?",
"What is a decorator in Python?",
"What is the purpose of the '__init__' method?",
"How do you access the value of a dictionary using a key?",
"How do you copy a list?",
"How do you convert a string to a float?",
"What is the purpose of a docstring?",
"What is list comprehension?",
"What is the purpose of the '__name__' variable?",
"What is slicing in Python?",
"How do you merge two dictionaries?",
"How can you sort a list in descending order?",
"What is the output of 'print('Hello'.upper())'?",
"How can you convert an integer to a string?",
"How do you add an element to a set?",
"What is a module in Python?",
"What is the use of the 'break' keyword?",
"How do you find the index of an element in a list?",
"What is the output of 'print(5 + 2)'?",
"How do you create a virtual environment in Python?",
"What is the difference between 'and' and 'or' operators?",
"How do you remove an element from a list?",
"What is the purpose of the 'else' clause in a loop?",
"How do you install a package using pip?",
"What is the output of 'print('hello'.capitalize())'?",
"What is the purpose of the 'return' statement?",
"How do you access elements in a tuple?",
"What is a generator in Python?",
"How do you open a file in Python?",
"What is the use of the 'continue' keyword?",
"How do you check if a key is present in a dictionary?",
"What is the output of 'print('hello world'.split())'?",
"What is the difference between 'print' and 'return'?",
"How do you concatenate two lists?",
"What is the purpose of the 'yield' keyword?",
"How do you create a set in Python?",
"What is a namespace in Python?",
"How do you find the maximum value in a list?",
"What is the output of 'print('Python'.lower())'?",
"What is the difference between shallow and deep copy?",
"How do you create a dictionary in Python?",
"What is the use of the 'assert' keyword?",
"How do you append an element to a list?",
"What is the purpose of a constructor?",
"What is the difference between 'append' and 'extend' methods in a list?",
"How do you check if a string starts with a certain substring?",
"How can you generate a random number in Python?",
"What is the output of 'print('5' + '3')'?",
"What is a list in Python?",
"What is polymorphism in object-oriented programming?",
"How do you remove an element from a set?",
"What is the purpose of the '__str__' method?",
"What is the output of 'print('hello'.islower())'?",
"How do you read a file in Python?",
"What is an instance variable?",
"How do you write a list to a file?",
"What is the use of the 'import' keyword?",
"What is the output of 'print(5 - 2)'?",
"How do you create a tuple in Python?",
"What is the difference between 'remove' and 'pop' methods in a list?",
"How do you write a for loop?",
"What is the use of the 'in' keyword?",
"What is the output of 'print('hello'.replace('h', 'j'))'?",
"How do you check if a string ends with a certain substring?",
"What is a method in object-oriented programming?",
"What is an attribute in object-oriented programming?",
"How do you find the minimum value in a list?",
"What is the output of 'print(10 / 2)'?",
"What is encapsulation in object-oriented programming?",
"How do you convert a list to a tuple?",
"What is an iterator in Python?",
"How do you create a list in Python?",
"What is the purpose of the '__call__' method?",
"What is the use of the 'def' keyword?",
"How do you check if an element is present in a list?",
"What is the output of 'print('hello world'.title())'?",
"How do you create an instance of a class?",
"What is the use of the 'while' loop?",
"What is the output of 'print(bool(0))'?",
"How do you write a conditional statement?",
"What is the difference between a class variable and an instance variable?",
"How do you read input from the user?",
"What is the use of the 'not' keyword?",
"How do you get the keys from a dictionary?",
"What is the output of 'print(list(range(5)))'?",
"What is a default argument in a function?",
"How do you create a string in Python?",
"What is the difference between 'str' and 'repr'?",
"What is the use of the 'enumerate' function?",
"What is the output of 'print('hello'.isalpha())'?",
"What is the purpose of the 'self' parameter in a class method?",
"How do you get the values from a dictionary?",
"What is a comprehension in Python?",
"What is the output of 'print(' '.join(['hello', 'world']))'?",
"How do you write a while loop?",
])
}