######################################################################
+class Gang(nn.Module):
+ def __init__(self, models, nb_models_for_generation, mode="groupthink"):
+ super().__init__()
+ self.models = models
+ self.nb_models_for_generation = nb_models_for_generation
+ self.mode = mode
+
+ def forward(self, bs):
+ # If first = 0, we are re-starting an auto-regressive process,
+ # that's the right moment to randomize who gonna do it
+ if bs.first == 0:
+ self.models_to_use = [
+ self.models[k]
+ for k in torch.randperm(len(self.models))[
+ : self.nb_models_for_generation
+ ]
+ ]
+
+ all_the_logits = torch.cat(
+ [model(bs).x[None] for model in self.models_to_use], dim=0
+ )
+
+ if self.mode == "groupthink":
+ y = all_the_logits.mean(dim=0)
+ elif self.mode == "groupwork":
+ m = torch.rand(all_the_logits.size(), device=all_the_logits.device)
+ m = (m.sort(dim=0).indices == 0).long()
+ y = (y * m).sum(dim=0)
+ else:
+ raise ValueError(f"Invalid mode {self.mode}")
+
+ return BracketedSequence(y, bs.first, bs.nb)
+
+
+######################################################################
+
+# ar_mask is a tensor with 0s and 1s, of same shape as input, with
+# 1s where tokens should be generated. The others are kept
+# unchanged.
+
+
+def one_batch_masked_inplace_autoregression(
+ model,
+ input,
+ ar_mask,
+ seq_logproba,
+ temperature=1.0,
+ deterministic_synthesis=False,
+ forbidden_tokens=None,
+ forced_biases=None,
+):
+ to_generate = (ar_mask.sum(0) > 0).nonzero()
+
+ if to_generate.min() > 0:
+ model(
+ BracketedSequence(input, 0, to_generate.min())
+ ) # Needed to initialize the model's cache
+ for s in range(to_generate.min(), to_generate.max() + 1):
+ output = model(BracketedSequence(input, s, 1)).x
+
+ logits = output[:, s]
+
+ logits = (logits / temperature).log_softmax(dim=-1)
+
+ if forbidden_tokens is not None:
+ logits = logits.masked_fill(forbidden_tokens, float("-inf"))
+
+ if forced_biases is not None:
+ logits = logits + forced_biases[None, :]
+
+ if deterministic_synthesis:
+ t_next = logits.argmax(-1)
+ else:
+ dist = torch.distributions.categorical.Categorical(logits=logits)
+ t_next = dist.sample()
+
+ all_n = torch.arange(t_next.size(0))
+ seq_logproba += logits[all_n, t_next].sum(dim=-1)
+
+ input[:, s] = ar_mask[:, s] * t_next + (1 - ar_mask[:, s]) * input[:, s]
+
+
def masked_inplace_autoregression(
model,
batch_size,
deterministic_synthesis,
forbidden_tokens=None,
logit_biases=None,
- progress_bar_desc="autoregression",
+ progress_bar_desc=None,
device=torch.device("cpu"),
):
assert input.size() == ar_mask.size()
model.eval()
for input, ar_mask, seq_logproba in batches:
- model.masked_inplace_autoregression(
+ one_batch_masked_inplace_autoregression(
+ model=model,
input=input,
ar_mask=ar_mask,
seq_logproba=seq_logproba,
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_seq(nb_train_samples).to(device)
- self.test_w_quizzes = self.problem.generate_seq(nb_test_samples).to(device)
+ 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(
+ device
+ )
self.nb_codes = max(self.train_w_quizzes.max(), self.test_w_quizzes.max()) + 1
if len(c_quizzes) > 0:
c_quizzes = torch.cat(c_quizzes, dim=0)
if c_quizzes.size(0) > w_quizzes.size(0) // 2:
- i = torch.randperm(w_quizzes.size(0))[: w_quizzes.size(0) // 2]
+ i = torch.randperm(c_quizzes.size(0))[: w_quizzes.size(0) // 2]
c_quizzes = c_quizzes[i]
i = torch.randperm(w_quizzes.size(0))[
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}")
##############################
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_seq(nb).to(self.device)
+ input[-nb:] = self.problem.generate_token_sequences(nb).to(self.device)
def store_c_quizzes(self, new_c_quizzes, for_train=True):
if for_train:
else:
self.test_c_quizzes.append(new_c_quizzes)
- def create_c_quizzes(
- self,
- n_epoch,
- result_dir,
- logger,
- nb,
- model,
- other_models,
- min_ave_seq_logproba,
- ):
- ###############################################################
- # Generate quizzes with model
-
- c_quizzes = torch.empty(
- 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)
-
- temperature = 1
- d_temperature = 1 / 3
-
- while True:
- seq_logproba[...] = 0
-
- masked_inplace_autoregression(
- model=model,
- 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,
- )
-
- ave_seq_logproba = seq_logproba.mean()
-
- if min_ave_seq_logproba is None:
- break
-
- # 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
-
- logger(f"chaging temperature to {temperature}")
-
- ###############################################################
+ def comput_correctness(self, c_quizzes, models_for_validation):
# Create the reverse quizzes
token_forward, token_backward = self.problem.direction_tokens()
ar_mask = self.make_ar_mask(c_quizzes)
seq_logproba = torch.empty(ar_mask.size(0), device=self.device)
- ###############################################################
- # Check how many of the other models can solve them in both
- # directions
+ # Check how many of models can solve the quizzes in both directions
- nb_correct = []
+ nb_correct = 0
- for m in other_models:
+ for model in models_for_validation:
result = c_quizzes.clone()
masked_inplace_autoregression(
- model=m,
+ model=model,
batch_size=self.batch_size,
input=result,
ar_mask=ar_mask,
seq_logproba=seq_logproba,
temperature=1.0,
deterministic_synthesis=True,
- progress_bar_desc="solving c_quizzes",
+ # progress_bar_desc="solving c_quizzes",
device=self.device,
)
reverse_result = reverse_c_quizzes.clone()
masked_inplace_autoregression(
- model=m,
+ model=model,
batch_size=self.batch_size,
input=reverse_result,
ar_mask=ar_mask,
seq_logproba=seq_logproba,
temperature=1.0,
deterministic_synthesis=True,
- progress_bar_desc="solving reversed c_quizzes",
+ # progress_bar_desc="solving reversed c_quizzes",
device=self.device,
)
(reverse_c_quizzes == reverse_result).long().min(dim=-1).values
)
- nb_correct.append((correct * reverse_correct)[None, :])
+ nb_correct += correct * reverse_correct
+
+ return nb_correct
+
+ ###############################################################
- nb_correct = torch.cat(nb_correct, dim=0).sum(dim=0)
+ def generate_quizzes(self, nb, model_for_generation, min_ave_seq_logproba):
+ 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)
+
+ # bracketing of the temperature to get the target logproba
+
+ warnings.warn("high temperature!", RuntimeWarning)
+ temperature = 2
+ d_temperature = 1 / 3
+
+ while True:
+ seq_logproba[...] = 0
- return c_quizzes, nb_correct, 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,
+ )
+
+ 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_solve,
+ seq_logproba=seq_logproba,
+ temperature=temperature,
+ deterministic_synthesis=True,
+ # 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
+
+ # 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
+
+ self.logger(f"changing temperature to {temperature}")
+
+ return c_quizzes, seq_logproba.mean()
+
+ ######################################################################
+
+ def create_c_quizzes(
+ self,
+ nb,
+ model_for_generation,
+ models_for_validation,
+ min_ave_seq_logproba,
+ n_epoch,
+ result_dir,
+ ):
+ c_quizzes, ave_seq_logproba = self.generate_quizzes(
+ nb, model_for_generation, min_ave_seq_logproba
+ )
+
+ nb_correct = self.comput_correctness(c_quizzes, models_for_validation)
+
+ return c_quizzes, nb_correct, ave_seq_logproba
+
+ ######################################################################
+
+ def gang_create_c_quizzes(
+ self,
+ nb,
+ nb_models_for_generation,
+ models,
+ mode,
+ min_ave_seq_logproba,
+ n_epoch,
+ result_dir,
+ ):
+ model_for_generation = Gang(models, nb_models_for_generation, mode)
+ models_for_validation = models
+ return self.create_c_quizzes(
+ nb,
+ model_for_generation,
+ models_for_validation,
+ min_ave_seq_logproba,
+ n_epoch,
+ result_dir,
+ )