File size: 10,539 Bytes
8beebbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import argparse
import logging
import sys
from pathlib import Path

from datasets import Dataset, DatasetDict, load_dataset

from .common import (
    CACHE_DIR,
    DEFAULT_DATASET_NAME,
    DEFAULT_INPUT_MAX_LENGTH,
    DEFAULT_MODEL_NAME,
    DEFAULT_SUMMARY_COLUMN,
    DEFAULT_TARGET_MAX_LENGTH,
    DEFAULT_TEXT_COLUMN,
    IS_WINDOWS,
    PROCESSED_DIR,
    build_preprocess_function,
    count_words,
    ensure_project_dirs,
    load_tokenizer,
    maybe_limit_split,
    normalize_text,
    write_json,
)

LOGGER = logging.getLogger(__name__)


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(
        description="Clean, filter, deduplicate, and tokenize XSum for BART."
    )
    parser.add_argument("--dataset-name", default=DEFAULT_DATASET_NAME)
    parser.add_argument("--dataset-config", default=None)
    parser.add_argument("--model-name", default=DEFAULT_MODEL_NAME)
    parser.add_argument("--text-column", default=DEFAULT_TEXT_COLUMN)
    parser.add_argument("--summary-column", default=DEFAULT_SUMMARY_COLUMN)
    parser.add_argument("--cache-dir", default=str(CACHE_DIR))
    parser.add_argument("--output-dir", default=str(PROCESSED_DIR / "xsum_bart_base"))
    parser.add_argument("--max-input-length", type=int, default=DEFAULT_INPUT_MAX_LENGTH)
    parser.add_argument("--max-target-length", type=int, default=DEFAULT_TARGET_MAX_LENGTH)
    parser.add_argument("--min-document-words", type=int, default=50)
    parser.add_argument("--max-document-words", type=int, default=1024)
    parser.add_argument("--min-summary-words", type=int, default=5)
    parser.add_argument("--train-samples", type=int, default=None)
    parser.add_argument("--validation-samples", type=int, default=None)
    parser.add_argument("--test-samples", type=int, default=None)
    parser.add_argument(
        "--num-proc",
        type=int,
        default=1,
        help="Worker processes for dataset.map(). Forced to 1 on Windows.",
    )
    parser.add_argument(
        "--debug",
        action="store_true",
        help="Use tiny split sizes (256/64/64) for a fast smoke-test.",
    )
    return parser.parse_args()


def clean_batch(
    batch: dict[str, list[str]], text_column: str, summary_column: str
) -> dict[str, list[str]]:
    return {
        text_column: [normalize_text(text) for text in batch[text_column]],
        summary_column: [normalize_text(text) for text in batch[summary_column]],
    }


def is_valid_example(
    example: dict[str, str],
    text_column: str,
    summary_column: str,
    min_document_words: int,
    max_document_words: int,
    min_summary_words: int,
) -> bool:
    document_length = count_words(example.get(text_column, ""))
    summary_length = count_words(example.get(summary_column, ""))
    return (
        min_document_words <= document_length <= max_document_words
        and summary_length >= min_summary_words
        and bool(example.get(text_column, "").strip())
        and bool(example.get(summary_column, "").strip())
    )


def deduplicate_split(split: Dataset, text_column: str) -> tuple[Dataset, int]:
    """Remove exact-duplicate documents using a hash set (O(n) time)."""
    seen: set[str] = set()
    keep: list[int] = []
    for index, example in enumerate(split):
        doc = example[text_column]
        if doc in seen:
            continue
        seen.add(doc)
        keep.append(index)
    removed = len(split) - len(keep)
    return split.select(keep), removed


def _safe_output_dir(output_dir: Path) -> None:
    """Raise FileExistsError if the directory is non-empty, with PermissionError guard."""
    if not output_dir.exists():
        return
    try:
        non_empty = any(output_dir.iterdir())
    except PermissionError as exc:
        raise PermissionError(
            f"Cannot read output directory '{output_dir}'. "
            "It may be locked by another process (e.g. OneDrive sync)."
        ) from exc
    if non_empty:
        raise FileExistsError(
            f"Output directory '{output_dir}' is not empty. "
            "Choose a new path or clear it first."
        )


def _resolve_num_proc(requested: int) -> int:
    """Force num_proc=1 on Windows; warn if the user asked for more."""
    if IS_WINDOWS and requested > 1:
        LOGGER.warning(
            "Multiprocessing with num_proc=%d is unreliable on Windows "
            "(datasets uses fork). Falling back to num_proc=1.",
            requested,
        )
        return 1
    return requested


def main() -> None:
    logging.basicConfig(
        level=logging.INFO,
        format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
    )
    args = parse_args()
    ensure_project_dirs()

    # ── Validate length arguments ──────────────────────────────────────────────
    if args.max_input_length <= args.max_target_length:
        raise ValueError(
            f"--max-input-length ({args.max_input_length}) must be greater than "
            f"--max-target-length ({args.max_target_length})."
        )

    # ── Debug mode: use None-safe check so --train-samples 0 is respected ─────
    if args.debug:
        if args.train_samples is None:
            args.train_samples = 256
        if args.validation_samples is None:
            args.validation_samples = 64
        if args.test_samples is None:
            args.test_samples = 64

    output_dir = Path(args.output_dir)
    _safe_output_dir(output_dir)

    num_proc = _resolve_num_proc(args.num_proc)

    # ── Load dataset ───────────────────────────────────────────────────────────
    LOGGER.info("Loading dataset '%s'…", args.dataset_name)
    try:
        dataset = load_dataset(
            args.dataset_name,
            args.dataset_config,
            cache_dir=args.cache_dir,
        )
    except Exception as exc:
        raise RuntimeError(
            f"Failed to load dataset '{args.dataset_name}'. "
            "Check your internet connection and dataset name."
        ) from exc

    # ── Validate expected splits exist ────────────────────────────────────────
    required_splits = {"train", "validation", "test"}
    missing = required_splits - set(dataset.keys())
    if missing:
        LOGGER.warning(
            "Dataset '%s' is missing splits: %s. Skipping those splits.",
            args.dataset_name,
            missing,
        )

    subset_limits = {
        "train": args.train_samples,
        "validation": args.validation_samples,
        "test": args.test_samples,
    }
    dataset = DatasetDict(
        {
            split_name: maybe_limit_split(split, subset_limits.get(split_name))
            for split_name, split in dataset.items()
        }
    )

    # ── Normalize ──────────────────────────────────────────────────────────────
    LOGGER.info("Normalizing text…")
    dataset = dataset.map(
        clean_batch,
        batched=True,
        fn_kwargs={
            "text_column": args.text_column,
            "summary_column": args.summary_column,
        },
        num_proc=num_proc,
        desc="Whitespace cleanup",
    )

    # ── Filter ────────────────────────────────────────────────────────────────
    LOGGER.info("Filtering unusable rows…")
    dataset = dataset.filter(
        is_valid_example,
        fn_kwargs={
            "text_column": args.text_column,
            "summary_column": args.summary_column,
            "min_document_words": args.min_document_words,
            "max_document_words": args.max_document_words,
            "min_summary_words": args.min_summary_words,
        },
        num_proc=num_proc,
        desc="Length filtering",
    )

    # ── Deduplicate ───────────────────────────────────────────────────────────
    dedupe_report: dict[str, int] = {}
    deduped_splits: dict[str, Dataset] = {}
    LOGGER.info("Deduplicating rows…")
    for split_name, split in dataset.items():
        deduped_split, removed = deduplicate_split(split, args.text_column)
        deduped_splits[split_name] = deduped_split
        dedupe_report[split_name] = removed
    dataset = DatasetDict(deduped_splits)

    # ── Tokenize ──────────────────────────────────────────────────────────────
    tokenizer = load_tokenizer(args.model_name)
    preprocess_fn = build_preprocess_function(
        tokenizer=tokenizer,
        text_column=args.text_column,
        summary_column=args.summary_column,
        max_input_length=args.max_input_length,
        max_target_length=args.max_target_length,
    )
    LOGGER.info("Tokenizing rows…")
    tokenized_dataset = dataset.map(
        preprocess_fn,
        batched=True,
        num_proc=num_proc,
        desc="Tokenization",
    )

    # ── Save ──────────────────────────────────────────────────────────────────
    LOGGER.info("Saving tokenized dataset to %s", output_dir)
    tokenized_dataset.save_to_disk(str(output_dir))

    manifest = {
        "dataset_name": args.dataset_name,
        "dataset_config": args.dataset_config,
        "model_name": args.model_name,
        "text_column": args.text_column,
        "summary_column": args.summary_column,
        "max_input_length": args.max_input_length,
        "max_target_length": args.max_target_length,
        "subset_limits": subset_limits,
        "splits": {name: len(split) for name, split in tokenized_dataset.items()},
        "duplicates_removed": dedupe_report,
    }
    write_json(output_dir / "manifest.json", manifest)
    LOGGER.info("Finished preprocessing. Split sizes: %s", manifest["splits"])


if __name__ == "__main__":
    main()