from torch import nn
from torch.nn import functional as F
+import mygpt
from mygpt import BracketedSequence
######################################################################
class Gang(nn.Module):
def __init__(self, models, nb_models_for_generation, mode="groupthink"):
super().__init__()
- self.models = models
+ self.models = nn.ModuleList(models)
self.nb_models_for_generation = nb_models_for_generation
self.mode = mode
nb_train_samples,
nb_test_samples,
batch_size,
- result_dir=None,
- logger=None,
+ result_dir,
+ logger,
device=torch.device("cpu"),
):
super().__init__()
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
return self.nb_codes
def produce_results(
- self, n_epoch, model, result_dir, logger, deterministic_synthesis, nmax=1000
+ self, n_epoch, model, result_dir, deterministic_synthesis, nmax=1000
):
- def compute_accuracy(input, logger=None):
+ def compute_accuracy(input):
input = input[:nmax]
ar_mask = self.make_ar_mask(input)
result = input.clone() * (1 - ar_mask)
train_nb_total, train_nb_correct = compute_accuracy(self.train_w_quizzes)
- logger(
+ self.logger(
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}%"
)
- test_nb_total, test_nb_correct = compute_accuracy(self.test_w_quizzes, logger)
+ test_nb_total, test_nb_correct = compute_accuracy(self.test_w_quizzes)
- logger(
+ self.logger(
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}%"
)
main_test_accuracy = test_nb_correct / test_nb_total
- logger(f"main_test_accuracy {n_epoch} {main_test_accuracy}")
+ self.logger(f"main_test_accuracy {n_epoch} {main_test_accuracy}")
##############################
nb, self.train_w_quizzes.size(1), device=self.device, dtype=torch.int64
)
- ar_mask = torch.full(c_quizzes.size(), 1, device=self.device)
- seq_logproba = torch.empty(ar_mask.size(0), device=self.device)
-
- # bracketing of the temperature to get the target logproba
+ 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)
+ warnings.warn("noise injection", RuntimeWarning)
temperature = 1
- d_temperature = 1 / 3
-
- while True:
- seq_logproba[...] = 0
-
- masked_inplace_autoregression(
- model=model_for_generation,
- batch_size=self.batch_size,
- input=c_quizzes,
- ar_mask=ar_mask,
- seq_logproba=seq_logproba,
- temperature=temperature,
- deterministic_synthesis=False,
- # progress_bar_desc="sampling c_quizzes",
- device=self.device,
- )
+ noise_std = torch.rand(1).item()
+ self.logger(f"{noise_std=}")
+ mygpt.set_noise_injection(model_for_generation, noise_std)
- ave_seq_logproba = seq_logproba.mean()
+ masked_inplace_autoregression(
+ model=model_for_generation,
+ batch_size=self.batch_size,
+ input=c_quizzes,
+ ar_mask=ar_mask_prompt,
+ seq_logproba=seq_logproba,
+ temperature=temperature,
+ deterministic_synthesis=False,
+ # progress_bar_desc="sampling c_quizzes",
+ device=self.device,
+ )
- # If we do not have target logprobs, get out now
- if min_ave_seq_logproba is None:
- break
+ ave_seq_logproba = seq_logproba.mean()
- # Oh man that's ugly
- if ave_seq_logproba < min_ave_seq_logproba:
- if d_temperature > 0:
- d_temperature *= -1 / 3
- temperature += d_temperature
- elif ave_seq_logproba > min_ave_seq_logproba * 0.99:
- if d_temperature < 0:
- d_temperature *= -1 / 3
- temperature += d_temperature
- else:
- break
+ 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,
+ # progress_bar_desc="sampling c_quizzes",
+ device=self.device,
+ )
- logger(f"changing temperature to {temperature}")
+ mygpt.set_noise_injection(model_for_generation, 0.0)
return c_quizzes, seq_logproba.mean()
min_ave_seq_logproba,
n_epoch,
result_dir,
- logger,
):
c_quizzes, ave_seq_logproba = self.generate_quizzes(
nb, model_for_generation, min_ave_seq_logproba
min_ave_seq_logproba,
n_epoch,
result_dir,
- logger,
):
model_for_generation = Gang(models, nb_models_for_generation, mode)
models_for_validation = models
min_ave_seq_logproba,
n_epoch,
result_dir,
- logger,
)