File size: 11,534 Bytes
22a96ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import functools
import json
import logging
import os
import pprint
from typing import Tuple

from datasets import get_dataset_config_names, load_dataset, get_dataset_split_names
import jax
import numpy as np
from flax import jax_utils
from flax.jax_utils import pad_shard_unpad
from transformers import AutoTokenizer, FlaxAutoModelForSeq2SeqLM
import pandas as pd

logger = logging.getLogger(__name__)

DATASET_NAME = "imdb"
OUTPUT_DIR = "./imdb_dutch"
MODEL_370 = "yhavinga/ul2-large-en-nl"
# BATCH_SIZE = 64
BATCH_SIZE = 32
# BATCH_SIZE = 2
MODEL_MAX_LENGTH = 370
MAX_WORDS = int(MODEL_MAX_LENGTH / 3)
END_MARKS = (".", "?", "!", '"', "'", "\n")


class FlaxModel:
    def __init__(self, model_name: str, tokenizer_name: str, tokenizer_args={}):
        """
        Initializes the FlaxModel with the specified model and tokenizer names, as well as tokenizer arguments.
        """
        self.model = FlaxAutoModelForSeq2SeqLM.from_pretrained(
            model_name, use_auth_token=True
        )
        self.model.params = self.model.to_fp32(self.model.params, mask=None)
        self.tokenizer_args = {
            # "model_max_length": self.model.config.max_length,
            **tokenizer_args,
        }
        self.tokenizer = AutoTokenizer.from_pretrained(
            tokenizer_name, use_auth_token=True, **self.tokenizer_args
        )
        # if not (
        #     self.model.config.max_length
        #     == self.tokenizer.model_max_length
        #     == self.tokenizer_args.get("model_max_length")
        # ):
        #     print(
        #         f"Warning: model max length {self.model.config.max_length} != tokenizer max length {self.tokenizer.model_max_length} != tokenizer_args max length {tokenizer_args.get('model_max_length')}"
        #     )
        #     raise ValueError("Model and tokenizer max_length should be equal")

        self.params = jax_utils.replicate(self.model.params)

        kwargs = {
            "max_length": self.tokenizer.model_max_length,
            "length_penalty": 1.0,
            "num_beams": 4,
            "early_stopping": True,
        }

        def shard(xs):
            local_device_count = jax.local_device_count()
            return jax.tree_map(
                lambda x: x.reshape((local_device_count, -1) + x.shape[1:]), xs
            )

        def generate_step(params, batch):
            self.model.params = params
            output_ids = self.model.generate(
                batch["input_ids"], attention_mask=batch["attention_mask"], **kwargs
            )
            return output_ids.sequences

        self.p_generate_step = jax.pmap(generate_step, "batch")

    @functools.lru_cache()
    def translate_batch(self, texts: Tuple[str]):
        overflowed = False
        texts = list(texts)
        if self.model.config.prefix:
            texts = [self.model.config.prefix + x for x in texts]
        texts = [x.replace("\n", "<n>").replace("<br />", "<n>") for x in texts]
        inputs = self.tokenizer(
            texts,
            max_length=self.tokenizer_args.get("model_max_length"),
            truncation=True,
            padding="max_length",
            return_tensors="np",
        )
        if not np.array_equal(
            inputs.data["input_ids"][:, self.tokenizer.model_max_length - 1],
            np.zeros(BATCH_SIZE),
        ):
            overflowed = True
            return BATCH_SIZE * [""], overflowed

        batch = inputs.data
        print(f"Batch inputs shape is {batch['input_ids'].shape}")
        translated = pad_shard_unpad(self.p_generate_step)(self.params, batch)
        predictions = jax.device_get(
            translated.reshape(-1, self.tokenizer.model_max_length)
        )
        if not np.array_equal(
            predictions[:, self.tokenizer.model_max_length - 1],
            np.zeros(BATCH_SIZE),
        ):
            overflowed = True

        output = [
            self.tokenizer.decode(t, skip_special_tokens=False) for t in predictions
        ]
        # If there is <extra_id in the output, remove it and everything after it
        output = [
            x.replace("<pad>", "").replace("</s>", "").split("<extra_id")[0]
            for x in output
        ]
        output = [x.replace("<n>", "<br />").strip() for x in output]
        return output, overflowed


def split_text(text):
    text_parts = []
    current_part = ""

    def split_on_end_marks(text):
        sentences = []
        current_sentence = ""
        for char in text:
            if char in END_MARKS:
                sentences.append(current_sentence + char)
                current_sentence = ""
            else:
                current_sentence += char

        # Add the final sentence if it wasn't ended by an end of line mark
        if current_sentence:
            sentences.append(current_sentence)
        return sentences

    text_lines = split_on_end_marks(text)

    for line in text_lines:
        # If adding the line to the current part would not exceed MAX_WORDS words, add it to the current part
        if len((current_part + line).split()) <= MAX_WORDS:
            current_part += line
        # If adding the line to the current part would exceed 200 characters, add the current part to the list and reset the current part
        else:
            if len(current_part) > 0:
                text_parts.append(current_part)
            while len(line.split()) > MAX_WORDS:
                # print(f"Line {line} is longer than MAX_WORDS words")
                current_part = " ".join(line.split()[:MAX_WORDS])
                text_parts.append(current_part + " ")
                line = " ".join(line.split()[MAX_WORDS:])
            current_part = line
    # Add the final part to the list
    text_parts.append(current_part)
    text_parts[-1] = text_parts[-1].rstrip()
    return text_parts


def test_split_text():
    # Test with single line that is less than MAX_WORDS words
    text = " ".join([f"n{i}" for i in range(MAX_WORDS - 20)])
    a = list(text)
    a[150] = END_MARKS[0]
    text = "".join(a)
    text_parts = split_text(text)
    assert text_parts == [text]

    # Test with single line that is exactly MAX_WORDS words
    text = " ".join([f"n{i}" for i in range(MAX_WORDS)])
    a = list(text)
    a[10] = END_MARKS[0]
    text = "".join(a)
    text_parts = split_text(text)
    assert text_parts == [text]

    # Test with single line that is more than MAX_WORDS words
    text = " ".join([f"n{i}" for i in range(MAX_WORDS + 1)])
    a = list(text)
    a[150] = END_MARKS[0]
    text = "".join(a)
    text_parts = split_text(text)
    assert text_parts == [text[:151], text[151:]]

    # Test with multiple lines, none of which are more than 200 characters
    text = "\n".join([f"n{i}" for i in range(10)])
    text_parts = split_text(text)
    assert text_parts == [text]

    # Test with 500 words
    text = " ".join([f"n{i}" for i in range(500)])
    a = list(text)
    a[150] = END_MARKS[0]
    a[300] = END_MARKS[0]
    a[550] = END_MARKS[0]
    a[600] = END_MARKS[0]
    a[750] = END_MARKS[0]
    a[900] = END_MARKS[0]
    a[950] = END_MARKS[0]
    a[1000] = END_MARKS[0]
    text = "".join(a)
    text_parts = split_text(text)
    assert all(
        [len(x.split()) <= MAX_WORDS for x in text_parts]
    ), "Not all text parts are less than MAX_WORDS words"
    assert "".join(text_parts) == text, "Text parts concatenated != original text"


test_split_text()


def get_file_lines(filename):
    """
    Get the number of lines in a file, 0 if the file does not exist.
    """
    lines = 0
    if os.path.exists(filename):
        with open(filename) as f:
            with open(filename, "r") as f:
                lines = len(f.readlines())
                print(f"{filename} already has {lines} lines")
    return lines


SEP = "\n"
# SEP="<unk>"


def main():
    os.makedirs(OUTPUT_DIR, exist_ok=True)

    model_370 = FlaxModel(
        MODEL_370, MODEL_370, tokenizer_args={"model_max_length": MODEL_MAX_LENGTH}
    )

    for config in get_dataset_config_names(DATASET_NAME):
        print(f"Processing config {config}")
        ds = load_dataset(DATASET_NAME, config)
        # for split in ["validation"]:
        for split in get_dataset_split_names(DATASET_NAME, config):
            output_file = f"{OUTPUT_DIR}/{DATASET_NAME}_dutch_{config}-{split}.json"
            num_examples = len(ds[split])
            # fn = partial(encode_in_single_text, validation=(split == "validation"))
            # single_text_ds = ds[split].map(fn, num_proc=6).sort("length", reverse=True)
            # # fn = partial(batch_single_text_decode, validation=(split == "validation"))
            # # decoded_ds = single_text_ds.map(fn, num_proc=6)
            #
            lines = get_file_lines(output_file)
            start_batch_index = lines // BATCH_SIZE
            with open(output_file, mode="ab" if lines else "wb") as writer:
                for batch_index in range(start_batch_index, num_examples // BATCH_SIZE):
                    ds_split = ds[split]
                    batch = ds_split[
                        batch_index * BATCH_SIZE : (batch_index + 1) * BATCH_SIZE
                    ]
                    print(
                        f"Translating batch {batch_index} of {num_examples // BATCH_SIZE}"
                    )

                    translated, overflow = model_370.translate_batch(
                        tuple(batch["text"])
                    )
                    translated_batch = [{"text": x} for x in translated]
                    if overflow:
                        batch_text_splitted = [
                            split_text(text) for text in batch["text"]
                        ]
                        max_parts = max(
                            [len(text) for text in batch_text_splitted]
                        )
                        text_translated = [""] * BATCH_SIZE
                        for part_index in range(max_parts):
                            text_parts_i = [
                                text[part_index] if part_index < len(text) else ""
                                for text in batch_text_splitted
                            ]
                            (
                                text_part_translated,
                                overflow,
                            ) = model_370.translate_batch(tuple(text_parts_i))
                            if overflow:
                                print(
                                    f"This shouldn't happen, overflow on a splitted text: {text_parts_i}"
                                )
                            for bi in range(BATCH_SIZE):
                                text_translated[bi] += " " + text_part_translated[bi] if text_parts_i[bi] != "" else ""
                        for bi in range(BATCH_SIZE):
                            translated_batch[bi]["text"] = text_translated[bi].strip()

                    # write each object in the batch as a separate line
                    for bi in range(BATCH_SIZE):
                        example = {
                            "text": translated_batch[bi]["text"],
                            "text_en": batch["text"][bi],
                            "label": batch["label"][bi],
                        }

                        pprint.pprint(example)
                        writer.write(json.dumps(example).encode("utf-8"))
                        writer.write("\n".encode("utf-8"))


if __name__ == "__main__":
    main()