forked from cvlab-columbia/viper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_batch.py
252 lines (206 loc) · 10.2 KB
/
main_batch.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
241
242
243
244
245
246
247
248
249
250
251
252
import datetime
import math
import os
import pathlib
from functools import partial
import warnings
import traceback
import pandas as pd
import torch.multiprocessing as mp
from joblib import Memory
from num2words import num2words
import numpy as np
from omegaconf import OmegaConf
from rich.console import Console
from torch.utils.data import DataLoader
from tqdm import tqdm
from configs import config
from utils import seed_everything
import datasets
# See https://github.com/pytorch/pytorch/issues/11201, https://github.com/pytorch/pytorch/issues/973
# Not for dataloader, but for multiprocessing batches
mp.set_sharing_strategy('file_system')
queue_results = None
cache = Memory('cache/' if config.use_cache else None, verbose=0)
runs_dict = {}
seed_everything()
console = Console(highlight=False)
def my_collate(batch):
# Avoid stacking images (different size). Return everything as a list
to_return = {k: [d[k] for d in batch] for k in batch[0].keys()}
return to_return
def run_program(parameters, queues_in_, input_type_, retrying=False):
from image_patch import ImagePatch, llm_query, best_image_match, distance, bool_to_yesno
from video_segment import VideoSegment
global queue_results
code, sample_id, image, possible_answers, query = parameters
code_header = f'def execute_command_{sample_id}(' \
f'{input_type_}, possible_answers, query, ' \
f'ImagePatch, VideoSegment, ' \
'llm_query, bool_to_yesno, distance, best_image_match):\n' \
f' # Answer is:'
code = code_header + code
try:
exec(compile(code, 'Codex', 'exec'), globals())
except Exception as e:
print(f'Sample {sample_id} failed at compilation time with error: {e}')
try:
with open(config.fixed_code_file, 'r') as f:
fixed_code = f.read()
code = code_header + fixed_code
exec(compile(code, 'Codex', 'exec'), globals())
except Exception as e2:
print(f'Not even the fixed code worked. Sample {sample_id} failed at compilation time with error: {e2}')
return None, code
queues = [queues_in_, queue_results]
image_patch_partial = partial(ImagePatch, queues=queues)
video_segment_partial = partial(VideoSegment, queues=queues)
llm_query_partial = partial(llm_query, queues=queues)
try:
result = globals()[f'execute_command_{sample_id}'](
# Inputs to the function
image, possible_answers, query,
# Classes to be used
image_patch_partial, video_segment_partial,
# Functions to be used
llm_query_partial, bool_to_yesno, distance, best_image_match)
except Exception as e:
# print full traceback
traceback.print_exc()
if retrying:
return None, code
print(f'Sample {sample_id} failed with error: {e}. Next you will see an "expected an indented block" error. ')
# Retry again with fixed code
new_code = "[" # This code will break upon execution, and it will be caught by the except clause
result = run_program((new_code, sample_id, image, possible_answers, query), queues_in_, input_type_,
retrying=True)[0]
# The function run_{sample_id} is defined globally (exec doesn't work locally). A cleaner alternative would be to
# save it in a global dict (replace globals() for dict_name in exec), but then it doesn't detect the imported
# libraries for some reason. Because defining it globally is not ideal, we just delete it after running it.
if f'execute_command_{sample_id}' in globals():
del globals()[f'execute_command_{sample_id}'] # If it failed to compile the code, it won't be defined
return result, code
def worker_init(queue_results_):
global queue_results
index_queue = mp.current_process()._identity[0] % len(queue_results_)
queue_results = queue_results_[index_queue]
def main():
mp.set_start_method('spawn')
from vision_processes import queues_in, finish_all_consumers, forward, manager
from datasets.dataset import MyDataset
batch_size = config.dataset.batch_size
num_processes = min(batch_size, 50)
if config.multiprocessing:
queue_results_main = manager.Queue()
queues_results = [manager.Queue() for _ in range(batch_size)]
else:
queue_results_main = None
queues_results = [None for _ in range(batch_size)]
codex = partial(forward, model_name='codex', queues=[queues_in, queue_results_main])
if config.clear_cache:
cache.clear()
if config.wandb:
import wandb
wandb.init(project="viper", config=OmegaConf.to_container(config))
# log the prompt file
wandb.save(config.codex.prompt)
dataset = MyDataset(**config.dataset)
with open(config.codex.prompt) as f:
base_prompt = f.read().strip()
codes_all = None
if config.use_cached_codex:
results = pd.read_csv(config.cached_codex_path)
codes_all = [r.split('# Answer is:')[1] for r in results['code']]
# python -c "from joblib import Memory; cache = Memory('cache/', verbose=0); cache.clear()"
dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=False, num_workers=0, pin_memory=True,
collate_fn=my_collate)
input_type = dataset.input_type
all_results = []
all_answers = []
all_codes = []
all_ids = []
all_querys = []
all_img_paths = []
all_possible_answers = []
all_query_types = []
with mp.Pool(processes=num_processes, initializer=worker_init, initargs=(queues_results,)) \
if config.multiprocessing else open(os.devnull, "w") as pool:
try:
n_batches = len(dataloader)
for i, batch in tqdm(enumerate(dataloader), total=n_batches):
# Combine all querys and get Codex predictions for them
# TODO compute Codex for next batch as current batch is being processed
if not config.use_cached_codex:
codes = codex(prompt=batch['query'], base_prompt=base_prompt)
else:
codes = codes_all[i * batch_size:(i + 1) * batch_size] # If cache
# Run the code
if config.execute_code:
if not config.multiprocessing:
# Otherwise, we would create a new model for every process
results = []
for c, sample_id, img, possible_answers, query in \
zip(codes, batch['sample_id'], batch['image'], batch['possible_answers'], batch['query']):
result = run_program([c, sample_id, img, possible_answers, query], queues_in, input_type)
results.append(result)
else:
results = list(pool.imap(partial(
run_program, queues_in_=queues_in, input_type_=input_type),
zip(codes, batch['sample_id'], batch['image'], batch['possible_answers'], batch['query'])))
else:
results = [(None, c) for c in codes]
warnings.warn("Not executing code! This is only generating the code. We set the flag "
"'execute_code' to False by default, because executing code generated by a language "
"model can be dangerous. Set the flag 'execute_code' to True if you want to execute "
"it.")
all_results += [r[0] for r in results]
all_codes += [r[1] for r in results]
all_ids += batch['sample_id']
all_answers += batch['answer']
all_possible_answers += batch['possible_answers']
all_query_types += batch['query_type']
all_querys += batch['query']
all_img_paths += [dataset.get_sample_path(idx) for idx in batch['index']]
if i % config.log_every == 0:
try:
accuracy = datasets.accuracy(all_results, all_answers, all_possible_answers, all_query_types)
console.print(f'Accuracy at Batch {i}/{n_batches}: {accuracy}')
except Exception as e:
console.print(f'Error computing accuracy: {e}')
except Exception as e:
# print full stack trace
traceback.print_exc()
console.print(f'Exception: {e}')
console.print("Completing logging and exiting...")
try:
accuracy = datasets.accuracy(all_results, all_answers, all_possible_answers, all_query_types)
console.print(f'Final accuracy: {accuracy}')
except Exception as e:
print(f'Error computing accuracy: {e}')
if config.save:
results_dir = pathlib.Path(config['results_dir'])
results_dir = results_dir / config.dataset.split
results_dir.mkdir(parents=True, exist_ok=True)
if not config.save_new_results:
filename = 'results.csv'
else:
existing_files = list(results_dir.glob('results_*.csv'))
if len(existing_files) == 0:
filename = 'results_0.csv'
else:
filename = 'results_' + str(max([int(ef.stem.split('_')[-1]) for ef in existing_files if
str.isnumeric(ef.stem.split('_')[-1])]) + 1) + '.csv'
print('Saving results to', filename)
df = pd.DataFrame([all_results, all_answers, all_codes, all_ids, all_querys, all_img_paths,
all_possible_answers]).T
df.columns = ['result', 'answer', 'code', 'id', 'query', 'img_path', 'possible_answers']
# make the result column a string
df['result'] = df['result'].apply(str)
df.to_csv(results_dir / filename, header=True, index=False, encoding='utf-8')
# torch.save([all_results, all_answers, all_codes, all_ids, all_querys, all_img_paths], results_dir/filename)
if config.wandb:
wandb.log({'accuracy': accuracy})
wandb.log({'results': wandb.Table(dataframe=df, allow_mixed_types=True)})
finish_all_consumers()
if __name__ == '__main__':
main()