File size: 5,648 Bytes
bd992b7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | import random
from typing import Generator
import torch
from benchmarks.registry import Batch, BatchGenerator, task
PAD = 0
SEP = 1
def _period_continuation_impl(seq_len: int, batch_size: int, min_period: int, max_period: int, seed: int | None = None) -> BatchGenerator:
rng = random.Random(seed)
buffer_input, buffer_target, buffer_mask = [], [], []
while True:
while len(buffer_input) < batch_size:
period = rng.randint(min_period, max_period)
n_cycles = rng.randint(2, 4)
symbols = list(range(2, 2 + period))
rng.shuffle(symbols)
prefix = []
for _ in range(n_cycles):
prefix.extend(symbols)
answer = symbols.copy()
if len(prefix) + 1 + len(answer) > seq_len:
period = min_period
n_cycles = max(2, (seq_len - period - 1) // period)
symbols = list(range(2, 2 + period))
rng.shuffle(symbols)
prefix = []
for _ in range(n_cycles):
prefix.extend(symbols)
answer = symbols.copy()
for i in range(len(answer)):
input_seq = prefix + [SEP] + answer[:i]
target_seq = prefix[1:] + [SEP] + answer[:i+1]
input_ids = torch.full((seq_len,), PAD, dtype=torch.long)
target_ids = torch.full((seq_len,), PAD, dtype=torch.long)
loss_mask = torch.zeros(seq_len, dtype=torch.bool)
input_ids[:len(input_seq)] = torch.tensor(input_seq, dtype=torch.long)
target_ids[:len(target_seq)] = torch.tensor(target_seq, dtype=torch.long)
ans_pos = len(input_seq) - 1
if ans_pos < seq_len:
loss_mask[ans_pos] = True
buffer_input.append(input_ids)
buffer_target.append(target_ids)
buffer_mask.append(loss_mask)
yield (
torch.stack(buffer_input[:batch_size]),
torch.stack(buffer_target[:batch_size]),
torch.stack(buffer_mask[:batch_size]),
)
buffer_input = buffer_input[batch_size:]
buffer_target = buffer_target[batch_size:]
buffer_mask = buffer_mask[batch_size:]
@task("period_continuation_64", vocab_size=14, seq_len=64)
def period_continuation_64(seq_len: int, batch_size: int, seed: int | None = None) -> BatchGenerator:
return _period_continuation_impl(seq_len, batch_size, min_period=2, max_period=6, seed=seed)
@task("period_continuation_128", vocab_size=14, seq_len=128)
def period_continuation_128(seq_len: int, batch_size: int, seed: int | None = None) -> BatchGenerator:
return _period_continuation_impl(seq_len, batch_size, min_period=2, max_period=12, seed=seed)
def _finite_state_machine_impl(seq_len: int, batch_size: int, seed: int | None = None) -> BatchGenerator:
rng = random.Random(seed)
while True:
batch_input = []
batch_target = []
batch_mask = []
for _ in range(batch_size):
n_states = rng.randint(2, 6)
n_inputs = rng.randint(2, 4)
STATE_START = 2
INPUT_START = STATE_START + n_states
table = {}
for s in range(n_states):
for inp in range(n_inputs):
next_state = rng.randint(0, n_states - 1)
table[(s, inp)] = next_state
table_flat = []
for s in range(n_states):
for inp in range(n_inputs):
table_flat.append(table[(s, inp)] + STATE_START)
remaining_len = seq_len - len(table_flat)
if remaining_len <= 0:
remaining_len = seq_len // 2
table_flat = table_flat[:remaining_len // 2]
input_stream = [rng.randint(0, n_inputs - 1) + INPUT_START for _ in range(remaining_len)]
states = []
current_state = 0
for inp in input_stream:
inp_idx = inp - INPUT_START
current_state = table[(current_state, inp_idx)]
states.append(current_state + STATE_START)
input_seq = table_flat + input_stream
target_seq = table_flat[1:] + [input_stream[0]] + states
input_ids = torch.full((seq_len,), PAD, dtype=torch.long)
target_ids = torch.full((seq_len,), PAD, dtype=torch.long)
loss_mask = torch.zeros(seq_len, dtype=torch.bool)
input_ids[:len(input_seq)] = torch.tensor(input_seq, dtype=torch.long)
target_ids[:len(target_seq)] = torch.tensor(target_seq, dtype=torch.long)
table_len = len(table_flat)
for i in range(len(states)):
pos = table_len + i
if pos < seq_len:
loss_mask[pos] = True
batch_input.append(input_ids)
batch_target.append(target_ids)
batch_mask.append(loss_mask)
yield (
torch.stack(batch_input),
torch.stack(batch_target),
torch.stack(batch_mask),
)
@task("finite_state_machine_64", vocab_size=16, seq_len=64)
def finite_state_machine_64(seq_len: int, batch_size: int, seed: int | None = None) -> BatchGenerator:
return _finite_state_machine_impl(seq_len, batch_size, seed)
@task("finite_state_machine_128", vocab_size=16, seq_len=128)
def finite_state_machine_128(seq_len: int, batch_size: int, seed: int | None = None) -> BatchGenerator:
return _finite_state_machine_impl(seq_len, batch_size, seed)
|