vt_utils.py 8.98 KB
Newer Older
Baber's avatar
Baber committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Copyright (c) 2024, NVIDIA CORPORATION.  All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# adapted from https://github.com/NVIDIA/RULER/blob/main/scripts/data/synthetic/variable_tracking.py

import itertools
import random
import string

import datasets
import numpy as np
from tqdm import tqdm

Baber's avatar
Baber committed
25
from lm_eval.tasks.ruler.common_utils import DEFAULT_SEQ_LENGTHS, get_tokenizer
Baber's avatar
Baber committed
26

Baber's avatar
fix vt  
Baber committed
27

Baber's avatar
Baber committed
28
29
30
31
TASKS = {
    "variable_tracking": {
        "tokens_to_generate": 30,
        "template": """Memorize and track the chain(s) of variable assignment hidden in the following text.\n\n{context}\nQuestion: Find all variables that are assigned the value {query} in the text above.""",
Baber's avatar
fix vt  
Baber committed
32
        "answer_prefix": """ Answer: According to the chain(s) of variable assignment in the text above, {num_v} variables are assgined the value {query}, they are: """,
Baber's avatar
Baber committed
33
34
35
36
    },
}

TEMPLATE = (
Baber's avatar
fix vt  
Baber committed
37
    TASKS["variable_tracking"]["template"] + TASKS["variable_tracking"]["answer_prefix"]
Baber's avatar
Baber committed
38
39
40
)


Baber's avatar
cleanup  
Baber committed
41
42
43
def generate_chains(
    num_chains: int, num_hops: int, is_icl: bool = False
) -> tuple[list[list[str]], list[list[str]]]:
Baber's avatar
Baber committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    vars_all = []
    k = 5 if not is_icl else 3
    num_hops = num_hops if not is_icl else min(10, num_hops)
    vars_all = [
        "".join(random.choices(string.ascii_uppercase, k=k)).upper()
        for _ in range((num_hops + 1) * num_chains)
    ]
    while len(set(vars_all)) < num_chains * (num_hops + 1):
        vars_all.append("".join(random.choices(string.ascii_uppercase, k=k)).upper())

    vars_ret = []
    chains_ret = []
    for i in range(0, len(vars_all), num_hops + 1):
        this_vars = vars_all[i : i + num_hops + 1]
        vars_ret.append(this_vars)
        this_chain = [f"VAR {this_vars[0]} = {np.random.randint(10000, 99999)}"]
        for j in range(num_hops):
            this_chain.append(f"VAR {this_vars[j + 1]} = VAR {this_vars[j]} ")
        chains_ret.append(this_chain)
    return vars_ret, chains_ret


Baber's avatar
fix vt  
Baber committed
66
def generate_input_output(num_noises, num_chains, num_hops, is_icl=False):
Baber's avatar
Baber committed
67
68
69
70
71
72
73
74
75
76
77
78
    vars, chains = generate_chains(num_chains, num_hops, is_icl=is_icl)

    noise = "The grass is green. The sky is blue. The sun is yellow. Here we go. There and back again.\n"

    # Create a list of the repeated noise
    sentences = [noise] * num_noises
    if len(sentences) <= len(chains[0]):
        sentences = [
            n + "." if len(n.strip()) > 0 else n
            for n in [x for noise in sentences for x in noise.split(".")]
        ]
        try:
Baber's avatar
Baber committed
79
80
81
            assert len(sentences) > len(chains[0]), (
                "Noises too short, unable to generate data"
            )
Baber's avatar
fix vt  
Baber committed
82
        except:  # noqa: E722
Baber's avatar
Baber committed
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
            print("reduces chain length for not enough noises")
            chains = [chain[: len(sentences) - 1] for chain in chains]
    # sample random positions to insert variable assignment
    for chain_i in chains:
        # sample random positions (sorted) to insert variable assignment
        positions = list(sorted(random.sample(range(len(sentences)), len(chain_i))))
        for insert_pi, j in zip(positions, range(len(chain_i))):
            sentences.insert(insert_pi + j, chain_i[j])

    # Insert the passkey sentence at the random position
    context = " ".join(sentences)
    context = context.replace(". \n", ".\n")

    template = TEMPLATE
    if (
        is_icl
        and template
        != TASKS["variable_tracking"]["template"]
        + TASKS["variable_tracking"]["answer_prefix"]
    ):
        # remove model template
        cutoff = template.index(TASKS["variable_tracking"]["template"][:20])
        cutoff_ans = template.index(TASKS["variable_tracking"]["answer_prefix"][:10])
        template = (
            " ".join(template[cutoff:cutoff_ans].split()[:-1]) + template[cutoff_ans:]
        )

    value = chains[0][0].split("=")[-1].strip()
    input_text = template.format(context=context, query=value, num_v=num_hops + 1)

    return input_text, vars[0]


Baber's avatar
cleanup  
Baber committed
116
def randomize_icl(icl_example: str) -> str:
Baber's avatar
Baber committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
    icl_tgt_cut = icl_example.index(TASKS["variable_tracking"]["answer_prefix"][-10:])
    icl_tgt = icl_example[icl_tgt_cut + 10 :].strip().split()
    for item in icl_tgt:
        new_item = "".join(random.choices(string.ascii_uppercase, k=len(item))).upper()
        icl_example = icl_example.replace(item, new_item)
    return icl_example


def sys_vartrack_w_noise_random(
    tokenizer,
    num_samples: int,
    max_seq_length: int,
    incremental: int = 10,
    num_chains: int = 1,
    num_hops: int = 4,
    add_fewshot: bool = True,
    tokens_to_generate=30,
    icl_example: dict = None,
    remove_newline_tab=False,
Baber's avatar
fix vt  
Baber committed
136
):
Baber's avatar
Baber committed
137
    write_jsons = []
Baber's avatar
fix vt  
Baber committed
138
139
    tokens_to_generate = tokens_to_generate

Baber's avatar
Baber committed
140
141
142
143
144
145
146
    # Find the perfect num_noises
    num_noises = incremental

    total_tokens = 0  # Track the total tokens generated for this example
    example_tokens = 0
    if add_fewshot and (icl_example is not None):
        icl_example_out = " ".join(icl_example["outputs"])
Baber's avatar
fix vt  
Baber committed
147
148
        icl_example = icl_example["input"] + " " + icl_example_out + "\n\n"
        example_tokens = len(tokenizer(icl_example).input_ids)
Baber's avatar
Baber committed
149
150
151
152
153
154

    while total_tokens + tokens_to_generate + example_tokens < max_seq_length:
        input_text, answer = generate_input_output(
            num_noises, num_chains, num_hops, is_icl=add_fewshot & (icl_example is None)
        )
        # Calculate the number of tokens in the example
Baber's avatar
fix vt  
Baber committed
155
156
157
158
        total_tokens = len(tokenizer(input_text + f" {answer}").input_ids)
        print(
            f"Max length {max_seq_length} | Current length {total_tokens + tokens_to_generate + example_tokens} | Noises: {num_noises}"
        )
Baber's avatar
Baber committed
159
160
161
162
        if total_tokens + tokens_to_generate + example_tokens > max_seq_length:
            num_noises -= incremental
            break
        num_noises += incremental
Baber's avatar
fix vt  
Baber committed
163
    print("Num noises:", num_noises)
Baber's avatar
Baber committed
164
165

    # Generate samples
Baber's avatar
fix vt  
Baber committed
166
    for index in tqdm(range(num_samples)):
Baber's avatar
Baber committed
167
168
169
170
171
172
173
174
175
176
        used_noises = num_noises
        while True:
            try:
                input_text, answer = generate_input_output(
                    used_noises,
                    num_chains,
                    num_hops,
                    is_icl=add_fewshot & (icl_example is None),
                )
                length = (
Baber's avatar
fix vt  
Baber committed
177
                    len(tokenizer(input_text).input_ids)
Baber's avatar
Baber committed
178
179
180
181
182
                    + tokens_to_generate
                    + example_tokens
                )
                assert length <= max_seq_length, f"{length} exceeds max_seq_length."
                break
Baber's avatar
fix vt  
Baber committed
183
            except:  # noqa: E722
Baber's avatar
Baber committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
                if used_noises > incremental:
                    used_noises -= incremental

        if add_fewshot and (icl_example is not None):
            # insert icl_example between model template and input
            cutoff = input_text.index(TASKS["variable_tracking"]["template"][:20])
            input_text = (
                input_text[:cutoff]
                + randomize_icl(icl_example)
                + "\n\n"
                + input_text[cutoff:]
            )
        if remove_newline_tab:
            input_text = " ".join(
                input_text.replace("\n", " ").replace("\t", " ").strip().split()
            )

Baber's avatar
fix vt  
Baber committed
201
202
203
        gen_prefix_index = input_text.rfind(
            " Answer: According to the chain(s) of variable assignment"
        )
Baber's avatar
Baber committed
204
        gen_prefix = input_text[gen_prefix_index:].strip()
Baber's avatar
fix vt  
Baber committed
205
206
207
        # This condition is to check if we are generating the few-shot.
        if icl_example is not None:
            input_text = input_text[:gen_prefix_index]
Baber's avatar
Baber committed
208
209
210
211
212
213
        formatted_output = {
            "index": index,
            "input": input_text,
            "outputs": answer,
            "length": length,
            "max_length": max_seq_length,
Baber's avatar
Baber committed
214
            "gen_prefix": gen_prefix.strip(),
Baber's avatar
Baber committed
215
216
217
218
219
220
        }
        write_jsons.append(formatted_output)

    return write_jsons


Baber's avatar
cleanup  
Baber committed
221
def get_dataset(pretrained, seq=None, **kwargs) -> list[dict]:
Baber's avatar
Baber committed
222
223
    tokenizer = get_tokenizer(pretrained)
    icl_example = sys_vartrack_w_noise_random(
Baber's avatar
fix vt  
Baber committed
224
225
226
227
        tokenizer=tokenizer,
        num_samples=1,
        max_seq_length=500,
        incremental=5,
Baber's avatar
Baber committed
228
229
230
231
232
233
234
235
236
237
    )[0]
    write_jsons = sys_vartrack_w_noise_random(
        tokenizer=tokenizer,
        num_samples=500,
        max_seq_length=seq,
        icl_example=icl_example,
    )
    return write_jsons


Baber's avatar
cleanup  
Baber committed
238
def get_vt_dataset(**kwargs) -> dict[str, datasets.Dataset]:
Baber's avatar
Baber committed
239
    pretrained = kwargs.get("tokenizer", kwargs.get("pretrained", {}))
Baber's avatar
nit  
Baber committed
240
241
242
243
    df = (
        get_dataset(pretrained, seq=seq)
        for seq in kwargs.pop("max_seq_lengths", DEFAULT_SEQ_LENGTHS)
    )
Baber's avatar
Baber committed
244
245
246
247
248
249

    return {
        "test": datasets.Dataset.from_list(
            list(itertools.chain.from_iterable(df)), split=datasets.Split.TEST
        )
    }