# Written by Francois Fleuret <francois@fleuret.org>
-# torch.backends.cuda.matmul.allow_tf23
-# torch.autocast(torch.bfloat16)
-
import math, sys, argparse, time, tqdm, os
import torch, torchvision
from torch import nn
from torch.nn import functional as F
-import mygpt, tasks, tensorstack
+import ffutils
+import mygpt, tasks, problems
######################################################################
parser.add_argument(
"--task",
type=str,
- default="picoclvr",
- help="picoclvr, mnist, maze, snake, stack, expr",
+ default="sandbox",
+ help="sandbox, picoclvr, mnist, maze, snake, stack, expr, rpl, world",
)
parser.add_argument("--log_filename", type=str, default="train.log", help=" ")
parser.add_argument("--seed", type=int, default=0)
+parser.add_argument("--max_percents_of_test_in_train", type=int, default=1)
+
+########################################
+
parser.add_argument("--nb_epochs", type=int, default=None)
parser.add_argument("--batch_size", type=int, default=None)
parser.add_argument("--learning_rate_schedule", type=str, default="10: 2e-5,30: 4e-6")
-parser.add_argument("--dim_model", type=int, default=512)
+########################################
-parser.add_argument("--dim_keys", type=int, default=64)
+parser.add_argument("--model", type=str, default="37M")
-parser.add_argument("--dim_hidden", type=int, default=2048)
+parser.add_argument("--dim_model", type=int, default=None)
-parser.add_argument("--nb_heads", type=int, default=8)
+parser.add_argument("--dim_keys", type=int, default=None)
-parser.add_argument("--nb_blocks", type=int, default=12)
+parser.add_argument("--dim_hidden", type=int, default=None)
+
+parser.add_argument("--nb_heads", type=int, default=None)
+
+parser.add_argument("--nb_blocks", type=int, default=None)
parser.add_argument("--dropout", type=float, default=0.1)
+########################################
+
parser.add_argument("--deterministic_synthesis", action="store_true", default=False)
parser.add_argument("--no_checkpoint", action="store_true", default=False)
parser.add_argument("--checkpoint_name", type=str, default="checkpoint.pth")
+##############################
+# rpl options
+
+parser.add_argument("--rpl_nb_starting_values", type=int, default=5)
+
+parser.add_argument("--rpl_max_input", type=int, default=9)
+
+parser.add_argument("--rpl_prog_len", type=int, default=10)
+
+parser.add_argument("--rpl_nb_runs", type=int, default=8)
+
+parser.add_argument("--rpl_no_prog", action="store_true", default=False)
+
+##############################
+# sandbox options
+
+parser.add_argument("--sandbox_level", type=int, default=0)
+
+parser.add_argument("--sandbox_levels_nb_items", type=int, default=25)
+
+parser.add_argument("--sandbox_levels_len_source", type=int, default=6)
+
+parser.add_argument("--sandbox_levels_len_result", type=int, default=8)
+
##############################
# picoclvr options
parser.add_argument("--snake_length", type=int, default=200)
##############################
-# Snake options
+# Stack options
parser.add_argument("--stack_nb_steps", type=int, default=100)
parser.add_argument("--expr_nb_variables", type=int, default=5)
-parser.add_argument("--expr_sequence_length", type=int, default=30)
+parser.add_argument("--expr_sequence_length", type=int, default=40)
+
+parser.add_argument("--expr_operand_max", type=int, default=9)
+
+parser.add_argument("--expr_result_max", type=int, default=99)
+
+parser.add_argument("--expr_input_file", type=str, default=None)
+
+##############################
+# World options
+
+parser.add_argument("--world_vqae_nb_epochs", type=int, default=25)
######################################################################
######################################################################
-default_args = {
+default_task_args = {
+ "sandbox": {
+ "nb_epochs": 50,
+ "batch_size": 25,
+ "nb_train_samples": 100000,
+ "nb_test_samples": 10000,
+ },
"picoclvr": {
"nb_epochs": 25,
"batch_size": 25,
"nb_test_samples": 1000,
},
"expr": {
- "nb_epochs": 50,
+ "nb_epochs": 40,
"batch_size": 25,
- "nb_train_samples": 250000,
+ "nb_train_samples": 1000000,
"nb_test_samples": 10000,
},
+ "rpl": {
+ "nb_epochs": 40,
+ "batch_size": 25,
+ "nb_train_samples": 100000,
+ "nb_test_samples": 10000,
+ },
+ "world": {
+ "nb_epochs": 10,
+ "batch_size": 25,
+ "nb_train_samples": 25000,
+ "nb_test_samples": 1000,
+ },
}
-if args.task in default_args:
- for k, v in default_args[args.task].items():
+if args.task in default_task_args:
+ for k, v in default_task_args[args.task].items():
if getattr(args, k) is None:
setattr(args, k, v)
######################################################################
+default_model_args = {
+ "17K": {
+ "dim_model": 32,
+ "dim_keys": 32,
+ "dim_hidden": 32,
+ "nb_heads": 2,
+ "nb_blocks": 2,
+ },
+ "37M": {
+ "dim_model": 512,
+ "dim_keys": 64,
+ "dim_hidden": 2048,
+ "nb_heads": 8,
+ "nb_blocks": 12,
+ },
+ "122M": {
+ "dim_model": 768,
+ "dim_keys": 64,
+ "dim_hidden": 2048,
+ "nb_heads": 8,
+ "nb_blocks": 24,
+ },
+ "352M": {
+ "dim_model": 1024,
+ "dim_keys": 64,
+ "dim_hidden": 2048,
+ "nb_heads": 8,
+ "nb_blocks": 48,
+ },
+}
+
+if args.model in default_model_args:
+ for k, v in default_model_args[args.model].items():
+ if getattr(args, k) is None:
+ setattr(args, k, v)
+else:
+ raise ValueError(f"Unknown model {args.model}")
+
+######################################################################
+
try:
os.mkdir(args.result_dir)
except FileExistsError:
######################################################################
-if args.task == "picoclvr":
+if args.task == "sandbox":
+ if args.sandbox_level == 0:
+ problem = problems.ProblemLevel0(
+ nb_sentences=args.sandbox_levels_nb_items,
+ len_prompt=args.sandbox_levels_len_source,
+ len_result=args.sandbox_levels_len_result,
+ )
+ elif args.sandbox_level == 1:
+ problem = problems.ProblemLevel1(
+ nb_operators=args.sandbox_levels_nb_items,
+ len_source=args.sandbox_levels_len_source,
+ len_result=args.sandbox_levels_len_result,
+ )
+ elif args.sandbox_level == 2:
+ problem = problems.ProblemLevel2(
+ len_source=args.sandbox_levels_len_source,
+ len_result=args.sandbox_levels_len_result,
+ )
+ else:
+ raise ValueError(f"Unknown sandbox level {args.sandbox_level}")
+
+ task = tasks.SandBox(
+ # problem,
+ # problems.ProblemAddition(zero_padded=False, inverted_result=False),
+ problems.ProblemLenId(len_max=args.sandbox_levels_len_source),
+ nb_train_samples=args.nb_train_samples,
+ nb_test_samples=args.nb_test_samples,
+ batch_size=args.batch_size,
+ logger=log_string,
+ device=device,
+ )
+
+elif args.task == "picoclvr":
task = tasks.PicoCLVR(
nb_train_samples=args.nb_train_samples,
nb_test_samples=args.nb_test_samples,
nb_test_samples=args.nb_test_samples,
nb_variables=args.expr_nb_variables,
sequence_length=args.expr_sequence_length,
+ operand_max=args.expr_operand_max,
+ result_max=args.expr_result_max,
+ batch_size=args.batch_size,
+ device=device,
+ )
+
+elif args.task == "rpl":
+ task = tasks.RPL(
+ nb_train_samples=args.nb_train_samples,
+ nb_test_samples=args.nb_test_samples,
batch_size=args.batch_size,
+ nb_starting_values=args.rpl_nb_starting_values,
+ max_input=args.rpl_max_input,
+ prog_len=args.rpl_prog_len,
+ nb_runs=args.rpl_nb_runs,
+ no_prog=args.rpl_no_prog,
+ logger=log_string,
+ device=device,
+ )
+
+elif args.task == "world":
+ task = tasks.World(
+ nb_train_samples=args.nb_train_samples,
+ nb_test_samples=args.nb_test_samples,
+ batch_size=args.batch_size,
+ vqae_nb_epochs=args.world_vqae_nb_epochs,
+ logger=log_string,
device=device,
)
######################################################################
+if args.task == "expr" and args.expr_input_file is not None:
+ task.produce_results(
+ n_epoch=nb_epochs_finished,
+ model=model,
+ result_dir=args.result_dir,
+ logger=log_string,
+ deterministic_synthesis=args.deterministic_synthesis,
+ input_file=args.expr_input_file,
+ )
+
+ exit(0)
+
+######################################################################
+
nb_epochs = args.nb_epochs if args.nb_epochs > 0 else nb_epochs_default
# Compute the entropy of the training tokens
entropy = -torch.xlogy(token_probas, token_probas).sum()
train_set_perplexity = math.exp(entropy)
-##############################
-
+######################################################################
# A bit of paranoia never hurts
-train_examples = {}
-for input in task.batches(split="train"):
- assert input.dim()==2 and input.dtype==torch.int64
- for x in input:
- train_examples[x.sum().item()]=x
+def subsets_as_tuples(batches, cs):
+ s = set()
+ for batch in batches:
+ for x in batch:
+ s.add(tuple([v.item() for v in x]))
+ if len(s) == cs:
+ yield s
+ s = set()
+ yield s
+
-for input in task.batches(split="test"):
- assert input.dim()==2 and input.dtype==torch.int64
- for x in input:
- y = train_examples.get(x.sum().item())
- if y is not None:
- assert x.size() != y.size() or (x-y).abs().sum() > 0
+nb_test, nb_in_train = 0, 0
+for test_subset in subsets_as_tuples(task.batches(split="test"), 25000):
+ in_train = set()
+ for train_subset in subsets_as_tuples(task.batches(split="train"), 25000):
+ in_train.update(test_subset.intersection(train_subset))
+ nb_in_train += len(in_train)
+ nb_test += len(test_subset)
-del train_examples
+log_string(
+ f"data_check {nb_in_train*100/nb_test:.02f}% ({nb_in_train}/{nb_test}) of test samples are in the train set"
+)
+
+assert (
+ nb_in_train <= args.max_percents_of_test_in_train * nb_test / 100
+), f"More than {args.max_percents_of_test_in_train}% of test samples are in the train set"
##############################
if nb_epochs_finished >= nb_epochs:
task.produce_results(
- nb_epochs_finished,
- model,
- args.result_dir,
- log_string,
- args.deterministic_synthesis,
+ n_epoch=nb_epochs_finished,
+ model=model,
+ result_dir=args.result_dir,
+ logger=log_string,
+ deterministic_synthesis=args.deterministic_synthesis,
)
for n_epoch in range(nb_epochs_finished, nb_epochs):
)
task.produce_results(
- n_epoch, model, args.result_dir, log_string, args.deterministic_synthesis
+ n_epoch=n_epoch,
+ model=model,
+ result_dir=args.result_dir,
+ logger=log_string,
+ deterministic_synthesis=args.deterministic_synthesis,
)
checkpoint = {