3 # Any copyright is dedicated to the Public Domain.
4 # https://creativecommons.org/publicdomain/zero/1.0/
6 # Written by Francois Fleuret <francois@fleuret.org>
8 import math, os, tqdm, warnings
10 import torch, torchvision
13 from torch.nn import functional as F
15 from mygpt import BracketedSequence
17 # from graph import save_attention_image
18 save_attention_image = None
20 ######################################################################
23 def masked_inplace_autoregression(
28 deterministic_synthesis,
29 forbidden_tokens=None,
31 progress_bar_desc="autoregression",
32 device=torch.device("cpu"),
34 assert input.size() == ar_mask.size()
36 batches = zip(input.split(batch_size), ar_mask.split(batch_size))
38 if progress_bar_desc is not None:
42 desc=progress_bar_desc,
43 total=(input.size(0) + batch_size - 1) // batch_size,
46 with torch.autograd.no_grad():
50 for input, ar_mask in batches:
51 model.masked_inplace_autoregression(
54 deterministic_synthesis,
62 ######################################################################
66 def batches(self, split="train"):
69 def vocabulary_size(self):
73 self, n_epoch, model, result_dir, logger, deterministic_synthesis
78 class TaskFromFile(Task):
79 def tensorize(self, pairs, shuffle):
80 len_max = max([len(x[0]) for x in pairs])
86 [self.char2id[c] for c in s[0] + "#" * (len_max - len(s[0]))]
94 pred_mask = torch.cat(
98 [int(c) for c in s[1] + "0" * (len_max - len(s[1]))]
107 i = torch.randperm(input.size(0))
108 input = input[i].contiguous()
109 pred_mask = pred_mask[i].contiguous()
111 return input, pred_mask
113 # trim all the tensors in the tuple z to remove as much token from
114 # left and right in the first tensor. If z is a tuple, all its
115 # elements are trimed according to the triming for the first
116 def trim(self, z, token="#"):
117 n = self.char2id[token]
120 i = (1 - (F.pad(x, (1, 1), value=n) == n).min(0).values.long()).cumsum(0)
121 a, b = (i == 0).nonzero().max(), (i == i.max()).nonzero().min()
122 return tuple([t[:, a:b] for t in z])
124 i = (1 - (F.pad(z, (1, 1), value=n) == n).min(0).values.long()).cumsum(0)
125 a, b = (i == 0).nonzero().max(), (i == i.max()).nonzero().min()
136 device=torch.device("cpu"),
138 self.batch_size = batch_size
141 def read_file(filename, nb=-1):
143 with open(filename, "r") as f:
145 sequence = f.readline().strip()
148 pred_mask = f.readline().strip()
149 assert len(sequence) == len(pred_mask)
150 assert set(pred_mask).issubset({"0", "1", "2"}), f"{set(pred_mask)}"
151 pairs.append((sequence, pred_mask))
157 assert len(pairs) == nb
161 train_pairs = read_file(train_filename, nb_train_samples)
162 test_pairs = read_file(test_filename, nb_test_samples)
164 symbols = ["#"] + list(
165 set("".join([x[0] for x in train_pairs + test_pairs])) - set(["#"])
167 self.char2id = dict([(c, n) for n, c in enumerate(symbols)])
168 self.id2char = dict([(n, c) for c, n in self.char2id.items()])
170 self.train_input, self.train_pred_masks = self.tensorize(
171 train_pairs, shuffle=shuffle
173 self.test_input, self.test_pred_masks = self.tensorize(
174 test_pairs, shuffle=shuffle
177 def batches(self, split="train", nb_to_use=-1, desc=None):
178 assert split in {"train", "test"}
179 input = self.train_input if split == "train" else self.test_input
181 input = input[:nb_to_use]
183 desc = f"epoch-{split}"
184 for batch in tqdm.tqdm(
185 input.split(self.batch_size), dynamic_ncols=True, desc=desc
187 yield self.trim(batch).to(self.device)
189 def vocabulary_size(self):
190 return len(self.char2id)
192 def tensor2str(self, t):
193 return ["".join([self.id2char[x.item()] for x in s]) for s in t]
196 self, n_epoch, model, result_dir, logger, deterministic_synthesis
198 correct = self.trim(self.test_input[:1000]).to(self.device)
199 result = correct.clone()
200 pred_mask = self.test_pred_masks[:1000, : result.size(1)].to(self.device)
201 ar_mask = (pred_mask > 0).long()
202 result *= 1 - ar_mask # paraaaaanoiaaaaaaa
204 logger(f"----------------------------------------------------------")
206 for e in self.tensor2str(result[:50]):
207 logger(f"test_before {e}")
209 masked_inplace_autoregression(
214 deterministic_synthesis,
218 logger(f"----------------------------------------------------------")
220 for e, c in zip(self.tensor2str(result[:50]), self.tensor2str(correct[:50])):
221 logger(f"test_after {e}")
222 logger(f"correct {c}")
224 logger(f"----------------------------------------------------------")
226 err_mask = (pred_mask == 2).long()
227 nb_total = err_mask.sum().item()
228 nb_correct = ((correct == result).long() * err_mask).sum().item()
230 logger(f"test_performance {n_epoch} {nb_total=} {nb_correct=}")
231 logger(f"main_test_accuracy {n_epoch} {nb_correct / nb_total}")
247 device=torch.device("cpu"),
252 self.batch_size = batch_size
254 self.problem = problem
256 self.train_input, self.train_ar_mask = self.problem.generate_sequences(
259 self.test_input, self.test_ar_mask = self.problem.generate_sequences(
263 self.train_input, self.train_ar_mask = self.train_input.to(
265 ), self.train_ar_mask.to(device)
266 self.test_input, self.test_ar_mask = self.test_input.to(
268 ), self.test_ar_mask.to(device)
270 self.nb_codes = max(self.train_input.max(), self.test_input.max()) + 1
272 # A bit of paranoia never hurts
273 assert self.nb_codes <= max_nb_codes
274 assert self.train_input.min() >= 0
275 assert self.test_input.min() >= 0
276 assert tuple(x.item() for x in self.train_ar_mask.unique()) in {
281 assert tuple(x.item() for x in self.test_ar_mask.unique()) in {
287 if logger is not None:
288 for s, a in zip(self.train_input[:100], self.train_ar_mask[:100]):
289 logger(f"train_sequences {self.problem.seq2str(s)}")
290 a = "".join(["01"[x.item()] for x in a])
293 def batches(self, split="train", nb_to_use=-1, desc=None):
294 assert split in {"train", "test"}
295 input = self.train_input if split == "train" else self.test_input
297 input = input[:nb_to_use]
299 desc = f"epoch-{split}"
300 for batch in tqdm.tqdm(
301 input.split(self.batch_size), dynamic_ncols=True, desc=desc
305 def vocabulary_size(self):
309 self, n_epoch, model, result_dir, logger, deterministic_synthesis, nmax=1000
311 def compute_accuracy(input, ar_mask, logger=None):
312 input, ar_mask = input[:nmax], ar_mask[:nmax]
313 result = input.clone() * (1 - ar_mask)
315 masked_inplace_autoregression(
320 deterministic_synthesis,
321 progress_bar_desc=None,
325 log_ground_truth = ar_mask.min() == 0
327 if logger is not None:
328 for sp, st in zip(result[:10], input[:10]):
330 f"test_sequences {n_epoch} prediction {self.problem.seq2str(sp)}"
334 f" {n_epoch} ground truth {self.problem.seq2str(st)}"
337 nb_total, nb_correct = self.problem.compute_nb_correct(
338 input, ar_mask, result
341 # nb_total = ar_mask.sum().item()
342 # nb_correct = ((result == input).long() * ar_mask).sum().item()
344 return nb_total, nb_correct
346 train_nb_total, train_nb_correct = compute_accuracy(
347 self.train_input, self.train_ar_mask
351 f"accuracy_train {n_epoch} nb_total {train_nb_total} nb_correct {train_nb_correct} accuracy {(100.0*train_nb_correct)/train_nb_total:.02f}%"
354 test_nb_total, test_nb_correct = compute_accuracy(
355 self.test_input, self.test_ar_mask, logger
359 f"accuracy_test {n_epoch} nb_total {test_nb_total} nb_correct {test_nb_correct} accuracy {(100.0*test_nb_correct)/test_nb_total:.02f}%"
362 logger(f"main_test_accuracy {n_epoch} {test_nb_correct/test_nb_total}")
364 if save_attention_image is not None:
366 ns = torch.randint(self.test_input.size(0), (1,)).item()
367 input = self.test_input[ns : ns + 1].clone()
369 with torch.autograd.no_grad():
372 # model.record_attention(True)
373 model(BracketedSequence(input))
375 # ram = model.retrieve_attention()
376 # model.record_attention(False)
378 # tokens_output = [c for c in self.problem.seq2str(input[0])]
379 # tokens_input = ["n/a"] + tokens_output[:-1]
380 # for n_head in range(ram[0].size(1)):
381 # filename = os.path.join(
382 # result_dir, f"sandbox_attention_{k}_h{n_head}.pdf"
384 # attention_matrices = [m[0, n_head] for m in ram]
385 # save_attention_image(
389 # attention_matrices,
391 ##min_total_attention=0.9,
395 # logger(f"wrote {filename}")
398 ######################################################################
403 class PicoCLVR(Task):
404 # Make a tensor from a list of strings
405 def tensorize(self, descr):
406 token_descr = [s.strip().split(" ") for s in descr]
407 l = max([len(s) for s in token_descr])
408 token_descr = [s + ["<nul>"] * (l - len(s)) for s in token_descr]
409 id_descr = [[self.token2id[u] for u in s] for s in token_descr]
410 return torch.tensor(id_descr, device=self.device)
412 # Make a list of strings from a tensor
413 def detensorize(self, x):
414 return [" ".join([self.id2token[t.item()] for t in r]) for r in x]
416 # trim all the tensors in the tuple z to remove as much token from
417 # left and right in the first tensor. If z is a tuple, all its
418 # elements are trimed according to the triming for the first
419 def trim(self, z, token="<nul>"):
420 n = self.token2id[token]
423 i = (1 - (F.pad(x, (1, 1), value=n) == n).min(0).values.long()).cumsum(0)
424 a, b = (i == 0).nonzero().max(), (i == i.max()).nonzero().min()
425 return tuple([t[:, a:b] for t in z])
427 i = (1 - (F.pad(z, (1, 1), value=n) == n).min(0).values.long()).cumsum(0)
428 a, b = (i == 0).nonzero().max(), (i == i.max()).nonzero().min()
431 ######################
442 device=torch.device("cpu"),
448 def generate_descr(nb, cache_suffix, pruner):
449 return picoclvr.generate(
459 self.batch_size = batch_size
461 self.pruner_train = pruner_train
462 self.pruner_eval = pruner_eval
464 if logger is not None:
466 f"generating {nb_train_samples+nb_test_samples} samples (can take some time)"
469 self.train_descr = generate_descr(
470 nb_train_samples, "train", pruner=self.pruner_train
472 self.test_descr = generate_descr(nb_test_samples, "test", pruner=None)
474 # Build the tokenizer
475 tokens = {"<nul>", "<img>"}
476 for d in [self.train_descr, self.test_descr]:
478 for t in s.strip().split(" "):
480 # make this set a sorted list to get the same tensors given
482 tokens = list(tokens)
484 self.token2id = dict([(t, n) for n, t in enumerate(tokens)])
485 self.id2token = dict([(n, t) for n, t in enumerate(tokens)])
486 self.t_img, self.t_nul = self.token2id["<img>"], self.token2id["<nul>"]
488 # Tokenize the train and test sets
489 self.train_input = self.tensorize(self.train_descr)
490 self.test_input = self.tensorize(self.test_descr)
492 def batches(self, split="train"):
493 assert split in {"train", "test"}
494 input = self.train_input if split == "train" else self.test_input
495 for batch in tqdm.tqdm(
496 input.split(self.batch_size), dynamic_ncols=True, desc=f"epoch-{split}"
498 yield self.trim(batch)
500 def vocabulary_size(self):
501 return len(self.token2id)
503 def compute_missing_properties(
504 self, n_epoch, model, logger, deterministic_synthesis, pruner=None
506 acc_nb_requested_properties = []
507 acc_nb_missing_properties = []
510 for input in tqdm.tqdm(
511 self.test_input.split(self.batch_size),
513 desc=f"test-properties",
515 result = input.clone()
516 ar_mask = (result == self.t_img).long().cumsum(dim=1).clamp(max=1)
517 result = (1 - ar_mask) * result + ar_mask * self.t_nul
518 masked_inplace_autoregression(
523 deterministic_synthesis,
524 progress_bar_desc=None,
528 result_descr = self.detensorize(result)
529 np = picoclvr.nb_properties(
535 nb_requested_properties, _, nb_missing_properties = zip(*np)
536 acc_nb_requested_properties += nb_requested_properties
537 acc_nb_missing_properties += nb_missing_properties
538 acc_nb_results += len(result_descr)
540 nb_requested_properties = sum(acc_nb_requested_properties)
541 nb_missing_properties = sum(acc_nb_missing_properties)
543 prefix = "" if pruner is None else "pruned_"
544 logger(f"nb_{prefix}samples {n_epoch} {acc_nb_results}")
546 f"property_{prefix}nb {n_epoch} requested {sum(acc_nb_requested_properties)} missing {sum(acc_nb_missing_properties)}"
549 f"property_{prefix}miss {n_epoch} {100*nb_missing_properties/nb_requested_properties:.02f}%"
553 f"main_test_accuracy {n_epoch} {1-nb_missing_properties/nb_requested_properties}"
556 ######################################################################
559 self, n_epoch, model, result_dir, logger, deterministic_synthesis
561 self.compute_missing_properties(n_epoch, model, logger, deterministic_synthesis)
563 if self.pruner_eval is not None:
564 self.compute_missing_properties(n_epoch, model, self.pruner_eval)
566 nb_tokens_to_generate = self.height * self.width + 3
571 for primer_descr in [
572 "red above green <sep> green top <sep> blue right of red",
573 "there is red <sep> there is yellow <sep> there is blue",
574 "red below yellow <sep> yellow below green <sep> green below blue <sep> red right <sep> yellow left <sep> green right <sep> blue left",
575 "green bottom <sep> yellow bottom <sep> green left of blue <sep> yellow right of blue <sep> blue top",
577 primer += [primer_descr + " <img>"] * nb_per_primer
579 result = self.tensorize(primer)
580 fill = result.new_full(
581 result.size()[:-1] + (self.height * self.width + 1,), self.t_nul
583 result = torch.cat((result, fill), 1)
584 ar_mask = (result == self.t_nul).long()
585 masked_inplace_autoregression(
590 deterministic_synthesis,
593 result_descr = self.detensorize(result)
595 np = picoclvr.nb_properties(result_descr, height=self.height, width=self.width)
597 acc_nb_requested_properties, _, acc_nb_missing_properties = zip(*np)
598 acc_nb_results = len(result_descr)
600 nb_requested_properties = sum(acc_nb_requested_properties)
601 nb_missing_properties = sum(acc_nb_missing_properties)
604 logger(f"nb_{prefix}samples {n_epoch} {acc_nb_results}")
606 f"property_{prefix}nb {n_epoch} requested {sum(acc_nb_requested_properties)} missing {sum(acc_nb_missing_properties)}"
609 f"property_{prefix}miss {n_epoch} {100*nb_missing_properties/nb_requested_properties:.02f}%"
612 img = picoclvr.descr2img(result_descr, height=self.height, width=self.width)
616 img = F.pad(img.squeeze(1), pad=(1, 1, 1, 1), value=64)
620 torchvision.utils.make_grid(x, padding=1, pad_value=64)[None]
626 image_name = os.path.join(result_dir, f"picoclvr_result_{n_epoch:04d}.png")
627 torchvision.utils.save_image(
628 img / 255.0, image_name, nrow=nb_per_primer, padding=1, pad_value=0.0
630 logger(f"wrote {image_name}")
633 ######################################################################
638 self, nb_train_samples, nb_test_samples, batch_size, device=torch.device("cpu")
642 self.nb_train_samples = (nb_train_samples,)
643 self.nb_test_samples = (nb_test_samples,)
644 self.batch_size = batch_size
646 data_set = torchvision.datasets.MNIST(root="./data", train=True, download=True)
647 self.train_input = data_set.data[:nb_train_samples].view(-1, 28 * 28).long()
648 data_set = torchvision.datasets.MNIST(root="./data", train=False, download=True)
649 self.test_input = data_set.data[:nb_test_samples].view(-1, 28 * 28).long()
651 def batches(self, split="train", nb_to_use=-1, desc=None):
652 assert split in {"train", "test"}
653 input = self.train_input if split == "train" else self.test_input
655 input = input[:nb_to_use]
657 desc = f"epoch-{split}"
658 for batch in tqdm.tqdm(
659 input.split(self.batch_size), dynamic_ncols=True, desc=desc
663 def vocabulary_size(self):
667 self, n_epoch, model, result_dir, logger, deterministic_synthesis
669 results = torch.empty(64, 28 * 28, device=self.device, dtype=torch.int64)
670 ar_mask = torch.full_like(results, 1)
671 masked_inplace_autoregression(
676 deterministic_synthesis,
679 image_name = os.path.join(result_dir, f"mnist_result_{n_epoch:04d}.png")
680 torchvision.utils.save_image(
681 1 - results.reshape(-1, 1, 28, 28) / 255.0,
686 logger(f"wrote {image_name}")
689 ######################################################################
695 def map2seq(self, *m):
696 return torch.cat([x.flatten(1) for x in m], 1)
698 def seq2map(self, s):
699 s = s.reshape(s.size(0), -1, self.height, self.width)
700 return (s[:, k] for k in range(s.size(1)))
710 device=torch.device("cpu"),
714 self.batch_size = batch_size
719 train_mazes, train_paths, _ = maze.create_maze_data(
724 progress_bar=lambda x: tqdm.tqdm(x, dynamic_ncols=True, desc=f"data-train"),
726 self.train_input = self.map2seq(train_mazes.to(device), train_paths.to(device))
728 test_mazes, test_paths, _ = maze.create_maze_data(
733 progress_bar=lambda x: tqdm.tqdm(x, dynamic_ncols=True, desc=f"data-test"),
735 self.test_input = self.map2seq(test_mazes.to(device), test_paths.to(device))
737 self.nb_codes = max(self.train_input.max(), self.test_input.max()) + 1
739 def batches(self, split="train", nb_to_use=-1, desc=None):
740 assert split in {"train", "test"}
741 input = self.train_input if split == "train" else self.test_input
743 input = input[:nb_to_use]
745 desc = f"epoch-{split}"
746 for batch in tqdm.tqdm(
747 input.split(self.batch_size), dynamic_ncols=True, desc=desc
751 def vocabulary_size(self):
755 self, model, split="train", nb_to_use=-1, deterministic_synthesis=False
757 nb_total, nb_correct = 0, 0
759 self.width * self.height,
760 self.width * self.height,
765 for input in self.batches(split, nb_to_use):
766 result = input.clone()
767 ar_mask = result.new_zeros(result.size())
768 ar_mask[:, self.height * self.width :] = 1
769 result *= 1 - ar_mask
770 masked_inplace_autoregression(
775 deterministic_synthesis,
776 progress_bar_desc=None,
779 mazes, paths = self.seq2map(result)
780 path_correctness = maze.path_correctness(mazes, paths)
781 nb_correct += path_correctness.long().sum()
782 nb_total += mazes.size(0)
784 optimal_path_lengths = (
785 (input[:, self.height * self.width :] == maze.v_path).long().sum(1)
787 predicted_path_lengths = (
788 (result[:, self.height * self.width :] == maze.v_path).long().sum(1)
790 optimal_path_lengths = optimal_path_lengths[path_correctness]
791 predicted_path_lengths = predicted_path_lengths[path_correctness]
792 count[optimal_path_lengths, predicted_path_lengths] += 1
798 : count.sum(1).nonzero().max() + 1, : count.sum(0).nonzero().max() + 1
801 return nb_total, nb_correct, count
804 self, n_epoch, model, result_dir, logger, deterministic_synthesis
806 train_nb_total, train_nb_correct, count = self.compute_error(
810 deterministic_synthesis=deterministic_synthesis,
813 f"accuracy_train {n_epoch} nb_total {train_nb_total} nb_correct {train_nb_correct} accuracy {(100.0*train_nb_correct)/train_nb_total:.02f}%"
816 test_nb_total, test_nb_correct, count = self.compute_error(
820 deterministic_synthesis=deterministic_synthesis,
823 f"accuracy_test {n_epoch} nb_total {test_nb_total} nb_correct {test_nb_correct} accuracy {(100.0*test_nb_correct)/test_nb_total:.02f}%"
826 logger(f"main_test_accuracy {n_epoch} {test_nb_correct/test_nb_total}")
828 if count is not None:
829 proportion_optimal = count.diagonal().sum().float() / count.sum()
830 logger(f"proportion_optimal_test {proportion_optimal*100:.02f}%")
832 os.path.join(result_dir, f"maze_result_{n_epoch:04d}.txt"), "w"
834 for i in range(count.size(0)):
835 for j in range(count.size(1)):
836 eol = " " if j < count.size(1) - 1 else "\n"
837 f.write(f"{count[i,j]}{eol}")
839 input = self.test_input[:48]
840 result = input.clone()
841 ar_mask = result.new_zeros(result.size())
842 ar_mask[:, self.height * self.width :] = 1
843 result *= 1 - ar_mask
844 masked_inplace_autoregression(
849 deterministic_synthesis,
853 mazes, paths = self.seq2map(input)
854 _, predicted_paths = self.seq2map(result)
856 filename = os.path.join(result_dir, f"maze_result_{n_epoch:04d}.png")
861 predicted_paths=predicted_paths,
862 path_correct=maze.path_correctness(mazes, predicted_paths),
863 path_optimal=maze.path_optimality(paths, predicted_paths),
865 logger(f"wrote {filename}")
868 ######################################################################
885 device=torch.device("cpu"),
889 self.batch_size = batch_size
893 self.prompt_length = prompt_length
895 self.train_input, self.train_prior_visits, _, _ = snake.generate_sequences(
904 self.test_input, self.test_prior_visits, _, _ = snake.generate_sequences(
914 self.nb_codes = max(self.train_input.max(), self.test_input.max()) + 1
916 def batches(self, split="train", nb_to_use=-1, desc=None):
917 assert split in {"train", "test"}
918 input = self.train_input if split == "train" else self.test_input
920 input = input[:nb_to_use]
922 desc = f"epoch-{split}"
923 for batch in tqdm.tqdm(
924 input.split(self.batch_size), dynamic_ncols=True, desc=desc
928 def vocabulary_size(self):
932 self, n_epoch, model, result_dir, logger, deterministic_synthesis
934 def compute_nb_correct(input, prior_visits):
935 result = input.clone()
936 i = torch.arange(result.size(1), device=result.device)[None, :]
938 torch.logical_and(i >= self.prompt_length * 2, i % 2 == 0)
942 result *= 1 - ar_mask
944 masked_inplace_autoregression(
949 deterministic_synthesis,
953 nb_total = ((prior_visits > 0) * ar_mask).sum()
955 nb_correct = ((result == input).long() * (prior_visits > 0) * ar_mask).sum()
957 return nb_total, nb_correct
959 test_nb_total, test_nb_correct = compute_nb_correct(
960 self.test_input[:1000], self.test_prior_visits[:1000]
964 f"accuracy_test {n_epoch} nb_total {test_nb_total} nb_correct {test_nb_correct} accuracy {(100.0*test_nb_correct)/test_nb_total:.02f}%"
967 logger(f"main_test_accuracy {n_epoch} {test_nb_correct/test_nb_total}")
970 ######################################################################
986 fraction_values_for_train=None,
987 device=torch.device("cpu"),
991 self.batch_size = batch_size
992 self.nb_steps = nb_steps
993 self.nb_stacks = nb_stacks
994 self.nb_digits = nb_digits
997 if fraction_values_for_train is None:
998 values_for_train = None
999 values_for_test = None
1001 all = torch.randperm(10**nb_digits)
1002 nb_for_train = int(all.size(0) * fraction_values_for_train)
1003 values_for_train = all[:nb_for_train]
1004 values_for_test = all[nb_for_train:]
1006 self.train_input, self.train_stack_counts = stack.generate_sequences(
1015 self.test_input, self.test_stack_counts = stack.generate_sequences(
1024 i = torch.logical_and(self.test_input % 2 == 1, self.test_input < 2 * nb_stacks)
1025 counts = self.test_stack_counts.flatten()[i.flatten()]
1026 counts = F.one_hot(counts).sum(0)
1027 logger(f"test_pop_stack_counts {counts}")
1029 self.nb_codes = max(self.train_input.max(), self.test_input.max()) + 1
1031 def batches(self, split="train", nb_to_use=-1, desc=None):
1032 assert split in {"train", "test"}
1033 input = self.train_input if split == "train" else self.test_input
1035 input = input[:nb_to_use]
1037 desc = f"epoch-{split}"
1038 for batch in tqdm.tqdm(
1039 input.split(self.batch_size), dynamic_ncols=True, desc=desc
1043 def vocabulary_size(self):
1044 return self.nb_codes
1046 def produce_results(
1047 self, n_epoch, model, result_dir, logger, deterministic_synthesis
1049 def compute_nb_correct(input):
1050 result = input.clone()
1051 stack.remove_popped_values(result, self.nb_stacks, self.nb_digits)
1052 ar_mask = (result != input).long()
1053 masked_inplace_autoregression(
1058 deterministic_synthesis,
1062 errors = ((result != input).long() * ar_mask).reshape(
1063 -1, 1 + self.nb_digits
1065 ar_mask = ar_mask.reshape(-1, 1 + self.nb_digits)
1067 nb_total = ar_mask.max(1).values.sum()
1068 nb_correct = nb_total - errors.max(1).values.sum()
1070 return nb_total, nb_correct
1072 test_nb_total, test_nb_correct = compute_nb_correct(self.test_input[:1000])
1075 f"accuracy_test {n_epoch} nb_total {test_nb_total} nb_correct {test_nb_correct} accuracy {(100.0*test_nb_correct)/test_nb_total:.02f}%"
1078 logger(f"main_test_accuracy {n_epoch} {test_nb_correct/test_nb_total}")
1080 ##############################################################
1081 # Log a few generated sequences
1082 input = self.test_input[:10, : 12 * (1 + self.nb_digits)]
1083 result = input.clone()
1084 stack.remove_popped_values(result, self.nb_stacks, self.nb_digits)
1085 ar_mask = (result != input).long()
1087 # for n in range(result.size(0)):
1089 # f"test_before {stack.seq_to_str(result[n],nb_stacks=self.nb_stacks,nb_digits=self.nb_digits)}"
1092 masked_inplace_autoregression(
1097 deterministic_synthesis,
1101 for n in range(result.size(0)):
1103 f"test_after {stack.seq_to_str(result[n],nb_stacks=self.nb_stacks,nb_digits=self.nb_digits)}"
1105 ##############################################################
1108 ######################################################################
1114 def tensorize(self, sequences):
1115 len_max = max([len(x) for x in sequences])
1121 self.token2id[str(c)]
1122 for c in s + ["<nul>"] * (len_max - len(s))
1131 def seq2str(self, seq):
1132 return " ".join([self.id2token[i] for i in seq])
1139 nb_starting_values=3,
1145 device=torch.device("cpu"),
1149 self.batch_size = batch_size
1150 self.device = device
1151 self.no_prog = no_prog
1155 nb_starting_values=nb_starting_values,
1156 nb_result_values_max=4 * nb_starting_values,
1157 max_input=max_input,
1161 for _ in tqdm.tqdm(range(nb_train_samples), desc="train-data")
1166 nb_starting_values=nb_starting_values,
1167 nb_result_values_max=4 * nb_starting_values,
1168 max_input=max_input,
1172 for _ in tqdm.tqdm(range(nb_test_samples), desc="test-data")
1176 set(["<nul>"] + [x for l in train_sequences + test_sequences for x in l])
1178 val_max = max([x if type(x) is int else 0 for x in symbols])
1179 symbols = list(filter(lambda x: type(x) is str, symbols))
1181 symbols += [str(n) for n in range(val_max + 1)]
1182 self.token2id = dict([(c, n) for n, c in enumerate(symbols)])
1183 self.id2token = dict([(n, c) for c, n in self.token2id.items()])
1185 self.t_nul = self.token2id["<nul>"]
1186 self.t_input = self.token2id["<in>"]
1187 self.t_output = self.token2id["<out>"]
1188 self.t_prog = self.token2id["<prg>"]
1189 self.t_end = self.token2id["<end>"]
1191 self.train_input = self.tensorize(train_sequences)
1192 self.test_input = self.tensorize(test_sequences)
1195 # Excise the program from every train and test example
1196 k = torch.arange(self.train_input.size(1), device=self.train_input.device)[
1200 ((self.train_input == self.t_prog).long() * k)
1201 .max(1, keepdim=True)
1204 self.train_input = (
1205 self.train_input * (k <= p).long()
1206 + self.t_end * (k == p + 1).long()
1207 + self.t_nul * (k > p + 1).long()
1209 k = torch.arange(self.test_input.size(1), device=self.test_input.device)[
1213 ((self.test_input == self.t_prog).long() * k)
1214 .max(1, keepdim=True)
1218 self.test_input * (k <= p).long()
1219 + self.t_end * (k == p + 1).long()
1220 + self.t_nul * (k > p + 1).long()
1223 if logger is not None:
1224 logger(f"value_max {val_max}")
1225 for x in self.train_input[:25]:
1226 end = (x != self.t_nul).nonzero().max().item() + 1
1227 seq = [self.id2token[i.item()] for i in x[:end]]
1229 logger(f"example_seq {s}")
1231 self.nb_codes = max(self.train_input.max(), self.test_input.max()) + 1
1233 def batches(self, split="train", nb_to_use=-1, desc=None):
1234 assert split in {"train", "test"}
1235 input = self.train_input if split == "train" else self.test_input
1237 input = input[:nb_to_use]
1239 desc = f"epoch-{split}"
1240 for batch in tqdm.tqdm(
1241 input.split(self.batch_size), dynamic_ncols=True, desc=desc
1243 last = (batch != self.t_nul).max(0).values.nonzero().max() + 3
1244 batch = batch[:, :last].to(self.device)
1247 def vocabulary_size(self):
1248 return self.nb_codes
1250 def produce_results(
1251 self, n_epoch, model, result_dir, logger, deterministic_synthesis
1253 # --------------------------------------------------------------------
1254 def compute_nb_errors_prog(input, nb_to_log=0):
1255 result = input.clone()
1256 s = (result == self.t_prog).long()
1257 ar_mask = (s.cumsum(dim=1) - s).clamp(min=0, max=1)
1258 result = (1 - ar_mask) * result + ar_mask * self.t_nul
1260 masked_inplace_autoregression(
1265 deterministic_synthesis,
1269 sum_nb_total, sum_nb_errors = 0, 0
1270 for one_input, one_result in zip(input, result):
1271 seq = [self.id2token[i.item()] for i in one_result]
1272 nb_total, nb_errors, prog, stacks = rpl.compute_nb_errors(seq)
1274 sum_nb_errors += 0 if nb_errors == 0 else 1
1276 gt_seq = [self.id2token[i.item()] for i in one_input]
1277 _, _, gt_prog, _ = rpl.compute_nb_errors(gt_seq)
1278 gt_prog = " ".join([str(x) for x in gt_prog])
1279 prog = " ".join([str(x) for x in prog])
1280 comment = "*" if nb_errors == 0 else "-"
1281 logger(f"{comment} PROG [{gt_prog}] PREDICTED [{prog}]")
1282 for start_stack, target_stack, result_stack, correct in stacks:
1283 comment = "*" if correct else "-"
1284 start_stack = " ".join([str(x) for x in start_stack])
1285 target_stack = " ".join([str(x) for x in target_stack])
1286 result_stack = " ".join([str(x) for x in result_stack])
1288 f" {comment} [{start_stack}] -> [{target_stack}] PREDICTED [{result_stack}]"
1292 return sum_nb_total, sum_nb_errors
1294 # --------------------------------------------------------------------
1295 def compute_nb_errors_output(input, nb_to_log=0):
1296 result = input.clone()
1297 k = torch.arange(result.size(1), device=result.device)[None, :]
1299 ((result == self.t_output) * k).max(dim=1, keepdim=True).values
1302 ((result == self.t_prog) * k).max(dim=1, keepdim=True).values
1304 ar_mask = (k > last_output_idx).long() * (k < first_prog_idx).long()
1305 result = (1 - ar_mask) * result + ar_mask * self.t_nul
1307 masked_inplace_autoregression(
1312 deterministic_synthesis,
1316 sum_nb_total, sum_nb_errors = 0, 0
1317 for one_input, one_result, i, j in zip(
1318 input, result, last_output_idx, first_prog_idx
1320 seq = [self.id2token[i.item()] for i in one_result]
1322 correct = (one_input - one_result).abs().max() == 0
1323 sum_nb_errors += 0 if correct else 1
1326 self.id2token[i.item()] for i in one_result[i : j + 1]
1329 self.id2token[i.item()] for i in one_input[i : j + 1]
1331 comment = "*" if correct else "-"
1332 result_stack = " ".join([str(x) for x in result_stack])
1333 target_stack = " ".join([str(x) for x in target_stack])
1335 f"output_test {comment} [{target_stack}] PREDICTED [{result_stack}]"
1339 return sum_nb_total, sum_nb_errors
1341 # --------------------------------------------------------------------
1343 if not self.no_prog:
1344 test_nb_total, test_nb_errors = compute_nb_errors_prog(
1345 self.test_input[:1000].to(self.device), nb_to_log=10
1349 f"accuracy_prog_test {n_epoch} nb_total {test_nb_total} nb_errors {test_nb_errors} accuracy {100.0*(1-test_nb_errors/test_nb_total):.02f}%"
1352 logger(f"main_test_accuracy {n_epoch} {1-test_nb_errors/test_nb_total}")
1354 test_nb_total, test_nb_errors = compute_nb_errors_output(
1355 self.test_input[:1000].to(self.device), nb_to_log=10
1359 f"accuracy_output_test {n_epoch} nb_total {test_nb_total} nb_errors {test_nb_errors} accuracy {100.0*(1-test_nb_errors/test_nb_total):.02f}%"
1362 if save_attention_image is None:
1363 logger("no save_attention_image (is pycairo installed?)")
1365 ns = torch.randint(self.test_input.size(0), (1,)).item()
1366 input = self.test_input[ns : ns + 1].clone()
1367 last = (input != self.t_nul).max(0).values.nonzero().max() + 3
1368 input = input[:, :last].to(self.device)
1370 with torch.autograd.no_grad():
1373 model.record_attention(True)
1374 model(BracketedSequence(input))
1376 ram = model.retrieve_attention()
1377 model.record_attention(False)
1379 tokens_output = [self.id2token[i.item()] for i in input[0]]
1380 tokens_input = ["n/a"] + tokens_output[:-1]
1381 for n_head in range(ram[0].size(1)):
1382 filename = os.path.join(
1383 result_dir, f"rpl_attention_{n_epoch}_h{n_head}.pdf"
1385 attention_matrices = [m[0, n_head] for m in ram]
1386 save_attention_image(
1392 # min_total_attention=0.9,
1396 logger(f"wrote {filename}")
1399 ######################################################################
1406 def tensorize(self, sequences):
1407 len_max = max([len(x) for x in sequences])
1412 [self.char2id[c] for c in s + "#" * (len_max - len(s))]
1429 device=torch.device("cpu"),
1433 self.batch_size = batch_size
1434 self.device = device
1436 train_sequences = expr.generate_sequences(
1438 nb_variables=nb_variables,
1439 length=sequence_length,
1440 operand_max=operand_max,
1441 result_max=result_max,
1444 test_sequences = expr.generate_sequences(
1446 nb_variables=nb_variables,
1447 length=sequence_length,
1448 operand_max=operand_max,
1449 result_max=result_max,
1452 symbols = list(set("#" + "".join(train_sequences + test_sequences)))
1455 self.char2id = dict([(c, n) for n, c in enumerate(symbols)])
1456 self.id2char = dict([(n, c) for c, n in self.char2id.items()])
1458 self.filler, self.space = self.char2id["#"], self.char2id[" "]
1460 self.train_input = self.tensorize(train_sequences)
1461 self.test_input = self.tensorize(test_sequences)
1463 self.nb_codes = max(self.train_input.max(), self.test_input.max()) + 1
1465 def batches(self, split="train", nb_to_use=-1, desc=None):
1466 assert split in {"train", "test"}
1467 input = self.train_input if split == "train" else self.test_input
1469 input = input[:nb_to_use]
1471 desc = f"epoch-{split}"
1472 for batch in tqdm.tqdm(
1473 input.split(self.batch_size), dynamic_ncols=True, desc=desc
1475 last = (batch != self.filler).max(0).values.nonzero().max() + 3
1476 batch = batch[:, :last]
1479 def vocabulary_size(self):
1480 return self.nb_codes
1482 def seq2str(self, s):
1483 return "".join([self.id2char[k.item()] for k in s])
1485 def produce_results(
1491 deterministic_synthesis,
1494 def compute_nb_correct(input):
1495 result = input.clone()
1496 s = (result == self.space).long()
1497 ar_mask = (s.cumsum(dim=1) - s).clamp(min=0, max=1)
1498 result = (1 - ar_mask) * result + ar_mask * self.filler
1499 masked_inplace_autoregression(
1504 deterministic_synthesis,
1508 nb_total = input.size(0)
1509 nb_correct = (input == result).long().min(1).values.sum()
1511 #######################################################################
1512 # Comput predicted vs. true variable values
1514 nb_delta = torch.zeros(5, dtype=torch.int64)
1517 values_input = expr.extract_results([self.seq2str(s) for s in input])
1518 values_result = expr.extract_results([self.seq2str(s) for s in result])
1520 filename = os.path.join(result_dir, f"expr_result_{n_epoch:04d}.txt")
1522 with open(filename, "w") as f:
1523 for i, r in zip(values_input, values_result):
1524 for n, vi in i.items():
1526 f.write(f"{vi} {-1 if vr is None else vr}\n")
1528 if vr is None or vr < 0:
1532 if d >= nb_delta.size(0):
1537 ######################################################################
1539 return nb_total, nb_correct, nb_delta, nb_missed
1546 ) = compute_nb_correct(self.test_input[:10000])
1549 f"accuracy_test {n_epoch} nb_total {test_nb_total} nb_correct {test_nb_correct} accuracy {(100.0*test_nb_correct)/test_nb_total:.02f}%"
1552 logger(f"main_test_accuracy {n_epoch} {test_nb_correct/test_nb_total}")
1554 nb_total = test_nb_delta.sum() + test_nb_missed
1555 for d in range(test_nb_delta.size(0)):
1557 f"error_value {n_epoch} delta {d} {test_nb_delta[d]} {test_nb_delta[d]*100/nb_total:.02f}%"
1560 f"error_value {n_epoch} missed {test_nb_missed} {test_nb_missed*100/nb_total:.02f}%"
1563 ##############################################################
1564 # Log a few generated sequences
1565 if input_file is None:
1566 input = self.test_input[:10]
1568 with open(input_file, "r") as f:
1569 sequences = [e.strip() for e in f.readlines()]
1570 sequences = [s + " " + "#" * 50 for s in sequences]
1571 input = self.tensorize(sequences)
1573 result = input.clone()
1574 s = (result == self.space).long()
1575 ar_mask = (s.cumsum(dim=1) - s).clamp(min=0, max=1)
1576 result = (1 - ar_mask) * result + ar_mask * self.filler
1578 for n in range(result.size(0)):
1579 logger(f"test_before {self.seq2str(result[n])}")
1581 masked_inplace_autoregression(
1586 deterministic_synthesis,
1590 correct = (1 - ar_mask) * self.space + ar_mask * input
1591 for n in range(result.size(0)):
1592 comment = "GOOD" if (result[n] - input[n]).abs().max() == 0 else ""
1593 logger(f"test_after {self.seq2str(result[n])} {comment}")
1594 logger(f"truth {self.seq2str(correct[n])}")
1595 ##############################################################
1598 ######################################################################
1604 # Make a tensor from a list of strings
1605 def str2tensor(self, descr):
1606 token_descr = [s.strip().split(" ") for s in descr]
1607 l = max([len(s) for s in token_descr])
1608 token_descr = [s + ["#"] * (l - len(s)) for s in token_descr]
1609 id_descr = [[self.token2id[u] for u in s] for s in token_descr]
1610 return torch.tensor(id_descr, device=self.device)
1612 # Make a list of strings from a tensor
1613 def tensor2str(self, x):
1614 return [" ".join([self.id2token[t.item()] for t in r]) for r in x]
1616 # trim all the tensors in the tuple z to remove as much token from
1617 # left and right in the first tensor. If z is a tuple, all its
1618 # elements are trimed according to the triming for the first
1619 def trim(self, z, token="#"):
1620 n = self.token2id[token]
1621 if type(z) == tuple:
1623 i = (1 - (F.pad(x, (1, 1), value=n) == n).min(0).values.long()).cumsum(0)
1624 a, b = (i == 0).nonzero().max(), (i == i.max()).nonzero().min()
1625 return tuple([t[:, a:b] for t in z])
1627 i = (1 - (F.pad(z, (1, 1), value=n) == n).min(0).values.long()).cumsum(0)
1628 a, b = (i == 0).nonzero().max(), (i == i.max()).nonzero().min()
1631 ######################
1641 device=torch.device("cpu"),
1645 self.device = device
1646 self.batch_size = batch_size
1647 self.grid_factory = grid.GridFactory(size=size)
1648 self.fraction_play = fraction_play
1650 if logger is not None:
1652 f"generating {nb_train_samples+nb_test_samples} samples (can take some time)"
1655 self.train_descr = self.grid_factory.generate_samples(
1656 nb=nb_train_samples,
1657 fraction_play=fraction_play,
1658 progress_bar=lambda r: tqdm.tqdm(r),
1661 self.test_descr = self.grid_factory.generate_samples(
1662 nb=nb_test_samples, fraction_play=0.0, progress_bar=lambda r: tqdm.tqdm(r)
1665 if fraction_play > 0:
1666 self.play_descr = self.grid_factory.generate_samples(
1667 nb=25, fraction_play=1.0, progress_bar=lambda r: tqdm.tqdm(r)
1670 self.play_descr = []
1672 # Build the tokenizer
1674 for d in [self.train_descr, self.test_descr, self.play_descr]:
1676 for t in s.strip().split(" "):
1678 # make this set a sorted list to get the same tensors given
1680 tokens = list(tokens)
1682 tokens = ["#"] + tokens
1683 self.token2id = dict([(t, n) for n, t in enumerate(tokens)])
1684 self.id2token = dict([(n, t) for n, t in enumerate(tokens)])
1685 self.t_nul = self.token2id["#"]
1686 self.t_true = self.token2id["true"]
1687 self.t_false = self.token2id["false"]
1688 self.t_pipe = self.token2id["|"]
1690 # Tokenize the train and test sets
1691 self.train_input = self.str2tensor(self.train_descr)
1692 self.test_input = self.str2tensor(self.test_descr)
1694 None if len(self.play_descr) == 0 else self.str2tensor(self.play_descr)
1697 def batches(self, split="train"):
1698 assert split in {"train", "test"}
1699 input = self.train_input if split == "train" else self.test_input
1700 for batch in tqdm.tqdm(
1701 input.split(self.batch_size), dynamic_ncols=True, desc=f"epoch-{split}"
1703 yield self.trim(batch)
1705 def vocabulary_size(self):
1706 return len(self.token2id)
1708 def produce_results(
1709 self, n_epoch, model, result_dir, logger, deterministic_synthesis
1711 correct = self.test_input[:1000]
1712 result = correct.clone()
1713 ar_mask = torch.logical_or(result == self.t_true, result == self.t_false).long()
1714 result *= 1 - ar_mask # paraaaaanoiaaaaaaa
1716 logger(f"----------------------------------------------------------")
1718 for e in self.tensor2str(result[:10]):
1719 logger(f"test_before {e}")
1721 masked_inplace_autoregression(
1726 deterministic_synthesis,
1730 logger(f"----------------------------------------------------------")
1732 for e in self.tensor2str(result[:10]):
1733 logger(f"test_after {e}")
1735 logger(f"----------------------------------------------------------")
1737 nb_total = ar_mask.sum().item()
1738 nb_correct = ((correct == result).long() * ar_mask).sum().item()
1740 logger(f"test_performance {n_epoch} {nb_total=} {nb_correct=}")
1741 logger(f"main_test_accuracy {n_epoch} {nb_correct / nb_total}")
1743 if self.play_input is not None:
1744 result = self.play_input.clone()
1745 ar_mask = (result == self.t_pipe).long().cumsum(dim=1).clamp(max=1)
1746 result *= 1 - ar_mask # paraaaaanoiaaaaaaa
1748 logger(f"----------------------------------------------------------")
1750 for e in self.tensor2str(result[:10]):
1751 logger(f"play_before {e}")
1753 masked_inplace_autoregression(
1758 deterministic_synthesis,
1762 logger(f"----------------------------------------------------------")
1764 for e in self.tensor2str(result[:10]):
1765 logger(f"play_after {e}")
1767 logger(f"----------------------------------------------------------")
1770 ######################################################################
1776 ######################
1785 device=torch.device("cpu"),
1789 self.device = device
1790 self.batch_size = batch_size
1791 self.nb_samples_per_mlp = 256
1793 if logger is not None:
1795 f"generating {nb_train_samples+nb_test_samples} samples (can take some time)"
1798 seq, q_test_set, test_error = qmlp.generate_sequence_and_test_set(
1799 nb_mlps=nb_train_samples + nb_test_samples,
1800 nb_samples=self.nb_samples_per_mlp,
1804 nb_mlps_per_batch=1024,
1807 self.train_input = seq[:nb_train_samples]
1808 self.train_q_test_set = q_test_set[:nb_train_samples]
1809 self.train_ref_test_errors = test_error[:nb_train_samples]
1810 self.test_input = seq[nb_train_samples:]
1811 self.test_q_test_set = q_test_set[nb_train_samples:]
1812 self.test_ref_test_errors = test_error[nb_train_samples:]
1814 filename = os.path.join(result_dir, f"train_errors_ref.dat")
1815 with open(filename, "w") as f:
1816 for e in self.train_ref_test_errors:
1819 filename = os.path.join(result_dir, f"test_errors_ref.dat")
1820 with open(filename, "w") as f:
1821 for e in self.test_ref_test_errors:
1824 self.nb_codes = max(self.train_input.max(), self.test_input.max()) + 1
1826 def batches(self, split="train"):
1827 assert split in {"train", "test"}
1828 input = self.train_input if split == "train" else self.test_input
1829 for batch in tqdm.tqdm(
1830 input.split(self.batch_size), dynamic_ncols=True, desc=f"epoch-{split}"
1834 def vocabulary_size(self):
1835 return self.nb_codes
1837 def produce_results(
1838 self, n_epoch, model, result_dir, logger, deterministic_synthesis
1840 correct = self.test_input[:1000]
1841 result = correct.clone()
1843 torch.arange(result.size(1), device=result.device)
1844 > self.nb_samples_per_mlp * 3 + 1
1846 ar_mask = ar_mask.expand_as(result)
1847 result *= 1 - ar_mask # paraaaaanoiaaaaaaa
1849 masked_inplace_autoregression(
1854 deterministic_synthesis,
1858 q_train_set = result[:, : self.nb_samples_per_mlp * 3]
1859 q_params = result[:, self.nb_samples_per_mlp * 3 + 1 :]
1860 error_test = qmlp.evaluate_q_params(q_params, self.test_q_test_set)
1862 filename = os.path.join(result_dir, f"test_errors_{n_epoch:04d}.dat")
1863 with open(filename, "w") as f:
1864 for e in error_test:
1868 ######################################################################
1885 device=torch.device("cpu"),
1889 self.batch_size = batch_size
1890 self.device = device
1892 self.world = greed.GreedWorld(height, width, T, nb_walls, nb_coins)
1894 states, actions, rewards = self.world.generate_episodes(
1895 nb_train_samples + nb_test_samples
1897 seq = self.world.episodes2seq(states, actions, rewards)
1898 self.train_input = seq[:nb_train_samples].to(self.device)
1899 self.test_input = seq[nb_train_samples:].to(self.device)
1901 def wipe_lookahead_rewards(self, batch):
1902 t = torch.arange(batch.size(1), device=batch.device)[None, :]
1903 u = torch.randint(batch.size(1), (batch.size(0), 1), device=batch.device)
1904 lr_mask = (t <= u).long() * (
1905 t % self.world.it_len == self.world.index_lookahead_reward
1909 lr_mask * self.world.lookahead_reward2code(greed.REWARD_UNKNOWN)
1910 + (1 - lr_mask) * batch
1913 def batches(self, split="train", nb_to_use=-1, desc=None):
1914 assert split in {"train", "test"}
1915 input = self.train_input if split == "train" else self.test_input
1917 input = input[:nb_to_use]
1919 desc = f"epoch-{split}"
1920 for batch in tqdm.tqdm(
1921 input.split(self.batch_size), dynamic_ncols=True, desc=desc
1923 yield self.wipe_lookahead_rewards(batch)
1925 def vocabulary_size(self):
1926 return self.world.nb_codes
1928 def thinking_autoregression(
1929 self, n_epoch, model, result_dir, logger, deterministic_synthesis, nmax=1000
1933 def ar(result, ar_mask, logit_biases=None):
1934 ar_mask = ar_mask.expand_as(result)
1935 result *= 1 - ar_mask
1936 masked_inplace_autoregression(
1941 deterministic_synthesis=deterministic_synthesis,
1942 logit_biases=logit_biases,
1944 progress_bar_desc=None,
1946 warnings.warn("keeping thinking snapshots", RuntimeWarning)
1947 snapshots.append(result[:10].detach().clone())
1949 # Generate iteration after iteration
1951 result = self.test_input[:250].clone()
1952 # Erase all the content but that of the first iteration
1953 result[:, self.world.it_len :] = -1
1954 # Set the lookahead_reward of the firs to UNKNOWN
1955 result[:, self.world.index_lookahead_reward] = self.world.lookahead_reward2code(
1956 greed.REWARD_UNKNOWN
1959 t = torch.arange(result.size(1), device=result.device)[None, :]
1962 range(0, result.size(1), self.world.it_len),
1965 # Generate the next state but keep the initial one, the
1966 # lookahead_reward of previous iterations are set to
1970 :, u + self.world.index_lookahead_reward
1971 ] = self.world.lookahead_reward2code(greed.REWARD_UNKNOWN)
1972 ar_mask = (t >= u + self.world.index_states).long() * (
1973 t < u + self.world.index_states + self.world.state_len
1977 # Generate the action and reward with lookahead_reward to +1
1979 :, u + self.world.index_lookahead_reward
1980 ] = self.world.lookahead_reward2code(greed.REWARD_PLUS)
1981 ar_mask = (t >= u + self.world.index_reward).long() * (
1982 t <= u + self.world.index_action
1986 # Set the lookahead_reward to UNKNOWN for the next iterations
1988 :, u + self.world.index_lookahead_reward
1989 ] = self.world.lookahead_reward2code(gree.REWARD_UNKNOWN)
1991 filename = os.path.join(result_dir, f"test_thinking_compute_{n_epoch:04d}.txt")
1992 with open(filename, "w") as f:
1995 lr, s, a, r = self.world.seq2episodes(
1998 str = self.world.episodes2str(
1999 lr, s, a, r, unicode=True, ansi_colors=True
2004 # Saving the generated sequences
2006 lr, s, a, r = self.world.seq2episodes(result)
2007 str = self.world.episodes2str(lr, s, a, r, unicode=True, ansi_colors=True)
2009 filename = os.path.join(result_dir, f"test_thinking_seq_{n_epoch:04d}.txt")
2010 with open(filename, "w") as f:
2012 logger(f"wrote {filename}")
2014 def produce_results(
2015 self, n_epoch, model, result_dir, logger, deterministic_synthesis, nmax=1000
2017 result = self.wipe_lookahead_rewards(self.test_input[:250].clone())
2019 # Saving the ground truth
2021 lr, s, a, r = self.world.seq2episodes(
2024 str = self.world.episodes2str(lr, s, a, r, unicode=True, ansi_colors=True)
2026 filename = os.path.join(result_dir, f"test_true_seq_{n_epoch:04d}.txt")
2027 with open(filename, "w") as f:
2029 logger(f"wrote {filename}")
2031 # Re-generating from the first frame
2034 torch.arange(result.size(1), device=result.device) >= self.world.it_len
2036 ar_mask = ar_mask.expand_as(result)
2037 result *= 1 - ar_mask # paraaaaanoiaaaaaaa
2039 masked_inplace_autoregression(
2044 deterministic_synthesis,
2048 # Saving the generated sequences
2050 lr, s, a, r = self.world.seq2episodes(
2053 str = self.world.episodes2str(lr, s, a, r, unicode=True, ansi_colors=True)
2055 filename = os.path.join(result_dir, f"test_seq_{n_epoch:04d}.txt")
2056 with open(filename, "w") as f:
2058 logger(f"wrote {filename}")
2060 self.thinking_autoregression(
2061 n_epoch, model, result_dir, logger, deterministic_synthesis, nmax
2065 ######################################################################