X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;f=quizz_machine.py;h=0d6d8f57cba918235f951333a64cb1a4c44133d2;hb=d283cd3d46a6323fec4c6a0970ac71e553e4a486;hp=198d279f3d8f9da8d0b602e622d3a8f690b907fd;hpb=240870f5535bac35a08c552108d032854a8e2c38;p=culture.git diff --git a/quizz_machine.py b/quizz_machine.py index 198d279..0d6d8f5 100755 --- a/quizz_machine.py +++ b/quizz_machine.py @@ -119,6 +119,20 @@ class QuizzMachine: b = torch.arange(input.size(1), device=input.device) > input.size(1) // 2 return b.long()[None, :].expand_as(input) + def generate_token_sequences(self, nb): + prompts, answers = self.problem.generate_prompts_and_answers(nb) + result = [] + + for prompt, answer in zip(prompts, answers): + if torch.rand(1) < 0.5: + a = [torch.tensor([self.token_forward]), prompt, answer] + else: + a = [torch.tensor([self.token_backward]), answer, prompt] + + result.append(torch.cat(a, dim=0)[None, :]) + + return torch.cat(result, dim=0) + def __init__( self, problem, @@ -131,28 +145,57 @@ class QuizzMachine: ): super().__init__() + v = problem.nb_token_values() + self.token_forward = v + self.token_backward = v + 1 + self.nb_token_values = v + 2 + self.problem = problem self.batch_size = batch_size self.device = device self.logger = logger - self.train_w_quizzes = self.problem.generate_token_sequences( - nb_train_samples - ).to(device) - self.test_w_quizzes = self.problem.generate_token_sequences(nb_test_samples).to( + self.train_w_quizzes = self.generate_token_sequences(nb_train_samples).to( device ) - self.nb_codes = max(self.train_w_quizzes.max(), self.test_w_quizzes.max()) + 1 + self.test_w_quizzes = self.generate_token_sequences(nb_test_samples).to(device) self.train_c_quizzes = [] self.test_c_quizzes = [] if result_dir is not None: - self.problem.save_quizzes( - self.train_w_quizzes[:72], result_dir, "culture_w_quizzes" + self.save_quizzes( + result_dir, "culture_w_quizzes", self.train_w_quizzes[:72] ) + def save_quizzes(self, result_dir, filename_prefix, quizzes, prediction=False): + print(f"DEBUG {quizzes.size()=}") + l = (quizzes.size(1) - 1) // 2 + forward = (quizzes[:, 0] == self.token_forward).long() + backward = (quizzes[:, 0] == self.token_backward).long() + assert forward.equal(1 - backward) + first = quizzes[:, 1 : 1 + l] + second = quizzes[:, 1 + l : 1 + 2 * l] + prompts = forward[:, None] * first + backward[:, None] * second + answers = forward[:, None] * second + backward[:, None] * first + + if prediction: + predicted_prompts = backward + predicted_answers = forward + else: + predicted_prompts = None + predicted_answers = None + + self.problem.save_quizzes( + result_dir, + filename_prefix, + prompts, + answers, + predicted_prompts, + predicted_answers, + ) + def batches(self, split="train", desc=None): assert split in {"train", "test"} if split == "train": @@ -193,7 +236,7 @@ class QuizzMachine: yield batch def vocabulary_size(self): - return self.nb_codes + return self.nb_token_values def produce_results( self, n_epoch, model, result_dir, deterministic_synthesis, nmax=1000 @@ -257,8 +300,11 @@ class QuizzMachine: device=self.device, ) - self.problem.save_quizzes( - result[:72], result_dir, f"culture_prediction_{n_epoch:04d}_{model.id:02d}" + self.save_quizzes( + result_dir, + f"culture_prediction_{n_epoch:04d}_{model.id:02d}", + quizzes=result[:72], + prediction=True, ) return main_test_accuracy @@ -267,7 +313,7 @@ class QuizzMachine: input = self.train_w_quizzes if for_train else self.test_w_quizzes nb = min(nb, input.size(0)) input[:-nb] = input[nb:].clone() - input[-nb:] = self.problem.generate_token_sequences(nb).to(self.device) + input[-nb:] = self.generate_token_sequences(nb).to(self.device) def store_c_quizzes(self, new_c_quizzes, for_train=True): if for_train: @@ -276,18 +322,18 @@ class QuizzMachine: self.test_c_quizzes.append(new_c_quizzes) def reverse_time(self, c_quizzes): - token_forward, token_backward = self.problem.direction_tokens() - l = (c_quizzes.size(1) - 1) // 2 - direction = c_quizzes[:, l : l + 1] - direction = self.problem.token_forward * ( - direction == self.problem.token_backward - ) + self.problem.token_backward * (direction == self.problem.token_forward) + direction = c_quizzes[:, 0:1] + direction = self.token_forward * ( + direction == self.token_backward + ) + self.token_backward * (direction == self.token_forward) - return torch.cat([c_quizzes[:, l + 1 :], direction, c_quizzes[:, :l]], dim=1) + return torch.cat( + [direction, c_quizzes[:, l + 1 :], c_quizzes[:, 1 : l + 1]], dim=1 + ) def compute_correctness( - self, c_quizzes, models_for_validation, both_direction=True + self, c_quizzes, models_for_validation, both_directions=False ): reversed_c_quizzes = self.reverse_time(c_quizzes) @@ -315,7 +361,7 @@ class QuizzMachine: correct = (c_quizzes == result).long().min(dim=-1).values - if both_direction: + if both_directions: reversed_result = reversed_c_quizzes.clone() masked_inplace_autoregression( @@ -344,77 +390,65 @@ class QuizzMachine: ############################################################### - def generate_quizzes(self, nb, model_for_generation, reverse_cleanup=False): + def generate_quizzes(self, nb, model_for_generation): c_quizzes = torch.empty( nb, self.train_w_quizzes.size(1), device=self.device, dtype=torch.int64 ) - ar_mask_prompt = torch.zeros(c_quizzes.size(), device=self.device) - ar_mask_prompt[:, : ar_mask_prompt.size(1) // 2 + 1] = 1 - ar_mask_solve = 1 - ar_mask_prompt - seq_logproba = torch.empty(ar_mask_prompt.size(0), device=self.device) + ar_mask_first = torch.zeros(c_quizzes.size(), device=self.device) + ar_mask_first[:, : ar_mask_first.size(1) // 2 + 1] = 1 + ar_mask_second = 1 - ar_mask_first + ar_mask_first[:, 0] = 0 + ar_mask_second[:, 0] = 0 - if reverse_cleanup: - warnings.warn("very high temperature with reversed cleanup", RuntimeWarning) - temperature = 10.0 - else: - temperature = 1.0 + seq_logproba = torch.empty(ar_mask_first.size(0), device=self.device) - # warnings.warn("noise injection", RuntimeWarning) - # noise_std = torch.rand(1).item() - # self.logger(f"{noise_std=}") + temperature = 10.0 - # mygpt.set_noise_injection(model_for_generation, noise_std) + # First, we generate the answer at high temperature + + c_quizzes[:, 0] = self.token_backward masked_inplace_autoregression( model=model_for_generation, batch_size=self.batch_size, input=c_quizzes, - ar_mask=ar_mask_prompt, + ar_mask=ar_mask_first, seq_logproba=seq_logproba, temperature=temperature, deterministic_synthesis=False, device=self.device, ) - # mygpt.set_noise_injection(model_for_generation, 0.0) - ave_seq_logproba = seq_logproba.mean() + # Then, we generate the prompt deterministically + masked_inplace_autoregression( model=model_for_generation, batch_size=self.batch_size, input=c_quizzes, - ar_mask=ar_mask_solve, + ar_mask=ar_mask_second, seq_logproba=seq_logproba, - temperature=temperature, + temperature=1.0, deterministic_synthesis=True, device=self.device, ) - if reverse_cleanup: - c_quizzes = self.reverse_time(c_quizzes) - masked_inplace_autoregression( - model=model_for_generation, - batch_size=self.batch_size, - input=c_quizzes, - ar_mask=ar_mask_solve, - seq_logproba=seq_logproba, - temperature=temperature, - deterministic_synthesis=True, - device=self.device, - ) + # Then we return the quizz, and re-generate the response, now + # deterministically - c_quizzes = self.reverse_time(c_quizzes) - masked_inplace_autoregression( - model=model_for_generation, - batch_size=self.batch_size, - input=c_quizzes, - ar_mask=ar_mask_solve, - seq_logproba=seq_logproba, - temperature=temperature, - deterministic_synthesis=True, - device=self.device, - ) + c_quizzes = self.reverse_time(c_quizzes) + + masked_inplace_autoregression( + model=model_for_generation, + batch_size=self.batch_size, + input=c_quizzes, + ar_mask=ar_mask_second, + seq_logproba=seq_logproba, + temperature=temperature, + deterministic_synthesis=True, + device=self.device, + ) return c_quizzes, seq_logproba.mean()