Download benchmarks/tasks/rule_extraction.py from Pulsar-Dynamic-Technologies/Adria: direct link, hf CLI and curl.
- Browser
- Download file 5.65 kB
-
https://huggingface.co/Pulsar-Dynamic-Technologies/Adria/resolve/main/benchmarks/tasks/rule_extraction.py
- Command line
-
hf download hf://Pulsar-Dynamic-Technologies/Adria/benchmarks/tasks/rule_extraction.py
-
curl -L -o rule_extraction.py https://huggingface.co/Pulsar-Dynamic-Technologies/Adria/resolve/main/benchmarks/tasks/rule_extraction.py
5.65 kB
| 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:] | |
| 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) | |
| 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), | |
| ) | |
| 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) | |
| 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) | |