|
| 1 | +from typing import List |
| 2 | +import numpy as np |
| 3 | +import random |
| 4 | +from collections import defaultdict |
| 5 | +import json |
| 6 | + |
| 7 | +random.seed(100) |
| 8 | +np.random.seed(100) |
| 9 | + |
| 10 | + |
| 11 | +def sample_spansfor_variablelength(seqlen, num_spans, span_lengths: List[int]): |
| 12 | + sum_lengths = sum(span_lengths) |
| 13 | + # We need a gap of atleast 1 token between two spans. Number of heads is computed based on longer spans (+1) |
| 14 | + # and offset is also up by +1 |
| 15 | + # Range of Number of possible span starts |
| 16 | + num_heads = seqlen - (sum_lengths - num_spans + num_spans) |
| 17 | + if num_heads < num_spans: |
| 18 | + return None |
| 19 | + indices = range(seqlen - (sum_lengths - num_spans)) |
| 20 | + result = [] |
| 21 | + offset = 0 |
| 22 | + # Randomly sample n=num_spans heads |
| 23 | + for i, idx in enumerate(sorted(random.sample(indices, num_spans))): |
| 24 | + # These heads are 0-indexed, to this we add the offset we've covered in the seq |
| 25 | + idx += offset |
| 26 | + span_length = span_lengths[i] |
| 27 | + result.append((idx, idx + span_length)) |
| 28 | + offset += span_length - 1 + 1 |
| 29 | + return result |
| 30 | + |
| 31 | + |
| 32 | +def make_instance(min_passage_length: int, max_passage_length: int, |
| 33 | + min_span_length: int, max_span_length: int, count_value: int): |
| 34 | + |
| 35 | + passage_length = random.randint(min_passage_length, max_passage_length) |
| 36 | + # Mean: 0, Std: 0.2, Size: PassageLength |
| 37 | + attention = np.abs(np.random.normal(0.0, 0.1, passage_length)) |
| 38 | + |
| 39 | + if count_value > 0: |
| 40 | + span_lengths = [random.randint(min_span_length, max_span_length) for _ in range(count_value)] |
| 41 | + # Sample n=count_value spans of the same length. Ends are exclusive |
| 42 | + # sampled_spans = self.sample_spans(passage_length, count_value, span_length) |
| 43 | + sampled_spans = sample_spansfor_variablelength(passage_length, count_value, span_lengths) |
| 44 | + if sampled_spans is None: |
| 45 | + return None |
| 46 | + |
| 47 | + for (start, end) in sampled_spans: |
| 48 | + attention[start:end] += 1.0 |
| 49 | + |
| 50 | + attention_sum = sum(attention) |
| 51 | + attention = attention / attention_sum |
| 52 | + |
| 53 | + return attention |
| 54 | + |
| 55 | +def _get_length_buckets(min_passage_length, max_passage_length): |
| 56 | + if min_passage_length == max_passage_length: |
| 57 | + return [(min_passage_length, max_passage_length)] |
| 58 | + |
| 59 | + min_length_buckets = [min_passage_length] |
| 60 | + max_length_buckets = [] |
| 61 | + |
| 62 | + # Add start, end + 100 until end <= max_passage_length |
| 63 | + i = 1 |
| 64 | + while True: |
| 65 | + potential_max_len = i * 100 + min_passage_length |
| 66 | + if potential_max_len <= max_passage_length: |
| 67 | + max_length_buckets.append(potential_max_len) |
| 68 | + min_length_buckets.append(max_length_buckets[-1]) # Last end is next's start |
| 69 | + |
| 70 | + i += 1 |
| 71 | + else: |
| 72 | + break |
| 73 | + if len(max_length_buckets) == 0 or max_length_buckets[-1] != max_passage_length: # This was left out |
| 74 | + max_length_buckets.append(max_passage_length) |
| 75 | + |
| 76 | + if min_length_buckets[-1] == max_passage_length: |
| 77 | + min_length_buckets = min_length_buckets[:-1] |
| 78 | + |
| 79 | + return list(zip(min_length_buckets, max_length_buckets)) |
| 80 | + |
| 81 | + |
| 82 | +def make_data(min_passage_length, max_passage_length, min_span_length, max_span_length, |
| 83 | + samples_per_bucket_count: int, max_count_value: int = 7): |
| 84 | + # For each 100 length bucket, and count value, generate 1000 examples in train mode, and 100 in val mode |
| 85 | + num_instances_per_bucket_per_count = samples_per_bucket_count |
| 86 | + |
| 87 | + # List of min and max passage |
| 88 | + minmax_passagelen_tuples = _get_length_buckets(min_passage_length, max_passage_length) |
| 89 | + data_dicts = [] |
| 90 | + |
| 91 | + lenbucket_count_dict = defaultdict() |
| 92 | + |
| 93 | + for count_value in range(0, max_count_value + 1): |
| 94 | + print(f"Count Value: {count_value}") |
| 95 | + for min_plen, max_plen in minmax_passagelen_tuples: |
| 96 | + instances_for_bucket = 0 |
| 97 | + for i in range(num_instances_per_bucket_per_count): |
| 98 | + attention = make_instance(min_passage_length=min_plen, max_passage_length=max_plen, |
| 99 | + min_span_length=min_span_length, max_span_length=max_span_length, |
| 100 | + count_value=count_value) |
| 101 | + if attention is None: |
| 102 | + continue |
| 103 | + if count_value not in lenbucket_count_dict: |
| 104 | + lenbucket_count_dict[count_value] = defaultdict(int) |
| 105 | + lenbucket_count_dict[count_value][(min_plen, max_plen)] += 1 |
| 106 | + attention = attention.tolist() |
| 107 | + data_dicts.append({'attention': attention, 'count_value': count_value}) |
| 108 | + instances_for_bucket += 1 |
| 109 | + print(f"{min_plen}, {max_plen} :: {instances_for_bucket}") |
| 110 | + print('\n') |
| 111 | + |
| 112 | + print(lenbucket_count_dict) |
| 113 | + return data_dicts |
| 114 | + |
| 115 | + |
| 116 | +def write_data_to_file(data, filepath): |
| 117 | + with open(filepath, 'w') as f: |
| 118 | + json.dump(data, f) |
| 119 | + |
| 120 | + |
| 121 | +if __name__=='__main__': |
| 122 | + train_data = make_data(min_passage_length=100, max_passage_length=600, min_span_length=5, |
| 123 | + max_span_length=15, max_count_value=7, samples_per_bucket_count=2000) |
| 124 | + |
| 125 | + dev_data = make_data(min_passage_length=100, max_passage_length=600, min_span_length=5, |
| 126 | + max_span_length=15, max_count_value=7, samples_per_bucket_count=500) |
| 127 | + |
| 128 | + train_data_path = "./resources/data/drop_s/synthetic/pattn2count/train.json" |
| 129 | + dev_data_path = "./resources/data/drop_s/synthetic/pattn2count/dev.json" |
| 130 | + |
| 131 | + |
| 132 | + write_data_to_file(train_data, train_data_path) |
| 133 | + write_data_to_file(dev_data, dev_data_path) |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | + |
0 commit comments