File size: 10,230 Bytes
ad68b7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- coding: utf-8 -*-
"""
DEDUPLICATE litdata_pretrain_final β€” remove near-duplicate documents.

Strategy:
  1. Scan all 308 chunks, hash each document (first 200 tokens)
  2. Keep first occurrence, mark subsequent duplicates for removal
  3. Rebuild chunks with duplicates removed (same format, new files)
  4. Update index.json

Memory-efficient: processes 10 chunks at a time, uses hash set.
"""

import json
import os
import time
import hashlib
from pathlib import Path

import numpy as np

ROOT = Path(__file__).resolve().parent.parent.parent
FINAL_DIR = ROOT / "Base" / "data" / "litdata_pretrain_final"
BLOCK_SIZE = 1025
DTYPE = np.int32
EOS_TOKEN_ID = 0
CHUNK_BYTES_TARGET = 64 * 1024 * 1024
HASH_WINDOW = 200  # first N tokens for hash


def read_chunk(filepath):
    with open(filepath, "rb") as f:
        raw = f.read()
    num_blocks = np.frombuffer(raw[:4], dtype=np.uint32)[0]
    header_size = 4 + (num_blocks + 1) * 4
    data_bytes = raw[header_size:]
    expected_tokens = num_blocks * BLOCK_SIZE
    expected_bytes = expected_tokens * DTYPE().itemsize
    tokens = np.frombuffer(data_bytes[:expected_bytes], dtype=DTYPE)
    return tokens, int(num_blocks)


def extract_documents(tokens):
    """Extract individual documents separated by EOS."""
    eos_positions = np.where(tokens == EOS_TOKEN_ID)[0]
    docs = []
    start = 0
    for eos_pos in eos_positions:
        if eos_pos > start:
            docs.append(tokens[start:eos_pos])
        start = eos_pos + 1
    # Trailing partial (no EOS at end β€” crosses chunk boundary)
    if start < len(tokens):
        remaining = tokens[start:]
        if len(remaining) > 0:
            docs.append(remaining)
    return docs


def doc_hash(token_array):
    """Hash first HASH_WINDOW tokens of a document."""
    key = token_array[:HASH_WINDOW].tobytes()
    return hashlib.md5(key).hexdigest()


def write_chunk(filepath, tokens_array, block_size=BLOCK_SIZE):
    """Write a litdata chunk from a flat token array."""
    num_blocks = len(tokens_array) // block_size
    if num_blocks == 0:
        return None
    actual = num_blocks * block_size
    data = np.array(tokens_array[:actual], dtype=DTYPE)

    header_num = np.array([num_blocks], dtype=np.uint32)
    offsets = np.arange(num_blocks + 1, dtype=np.uint32) * (block_size * DTYPE().itemsize)
    header = np.concatenate([header_num, offsets])

    with open(filepath, "wb") as f:
        header.tofile(f)
        data.tofile(f)

    return {
        "chunk_bytes": int(header.nbytes + data.nbytes),
        "chunk_size": num_blocks,
        "dim": int(actual),
        "filename": os.path.basename(filepath),
    }


class StreamingDeduplicator:
    """Accumulates deduplicated tokens and writes chunks."""

    def __init__(self, output_dir, backup_suffix="_dedup"):
        self.output_dir = Path(output_dir)
        self.dtype_size = DTYPE().itemsize
        self.tokens_per_chunk = (CHUNK_BYTES_TARGET // self.dtype_size // BLOCK_SIZE) * BLOCK_SIZE
        self.buffer = []
        self.chunk_idx = 0
        self.chunks_meta = []
        self.total_tokens = 0

    def add_doc(self, doc_tokens):
        self.buffer.extend(doc_tokens.tolist())
        self.buffer.append(EOS_TOKEN_ID)
        while len(self.buffer) >= self.tokens_per_chunk:
            self._flush()

    def _flush(self):
        if len(self.buffer) < BLOCK_SIZE:
            return
        take = min(len(self.buffer), self.tokens_per_chunk)
        num_blocks = take // BLOCK_SIZE
        if num_blocks == 0:
            return
        actual = num_blocks * BLOCK_SIZE
        
        data = np.array(self.buffer[:actual], dtype=DTYPE)
        self.buffer = self.buffer[actual:]

        filename = f"chunk-0-{self.chunk_idx}.bin"
        filepath = self.output_dir / filename

        header_num = np.array([num_blocks], dtype=np.uint32)
        offsets = np.arange(num_blocks + 1, dtype=np.uint32) * (BLOCK_SIZE * self.dtype_size)
        header = np.concatenate([header_num, offsets])

        with open(filepath, "wb") as f:
            header.tofile(f)
            data.tofile(f)

        meta = {
            "chunk_bytes": int(header.nbytes + data.nbytes),
            "chunk_size": num_blocks,
            "dim": int(actual),
            "filename": filename,
        }
        self.chunks_meta.append(meta)
        self.total_tokens += actual
        self.chunk_idx += 1
        if self.chunk_idx % 25 == 0:
            print(f"      Written {self.chunk_idx} deduped chunks ({self.total_tokens:,} tokens)")

    def finalize(self):
        while len(self.buffer) >= BLOCK_SIZE:
            self._flush()
        discarded = len(self.buffer)
        self.buffer = []
        return self.total_tokens, discarded


def main():
    t_start = time.time()

    with open(FINAL_DIR / "index.json") as f:
        index = json.load(f)
    chunks_meta = index["chunks"]
    config = index.get("config", {})
    num_chunks = len(chunks_meta)

    original_tokens = sum(c["dim"] for c in chunks_meta)

    print(f"{'='*75}")
    print(f"  DEDUPLICATING litdata_pretrain_final")
    print(f"{'='*75}")
    print(f"  Chunks: {num_chunks}")
    print(f"  Original tokens: {original_tokens:,}")
    print()

    # ── PASS 1: Scan all chunks, collect hashes, identify duplicates ──────
    print(f"  PASS 1: Scanning all chunks for duplicates...")
    seen_hashes = set()
    dup_count = 0
    keep_count = 0
    total_docs = 0
    # We need to track which is first occurrence
    # But since we process sequentially, just check "in seen_hashes"

    # Use a temp dir to write deduplicated data, then swap
    TEMP_DIR = FINAL_DIR.parent / "litdata_pretrain_dedup_temp"
    if TEMP_DIR.exists():
        import shutil
        shutil.rmtree(str(TEMP_DIR))
    os.makedirs(str(TEMP_DIR))

    writer = StreamingDeduplicator(TEMP_DIR)

    for ci, meta in enumerate(chunks_meta):
        filepath = FINAL_DIR / meta["filename"]
        tokens, num_blocks = read_chunk(filepath)
        docs = extract_documents(tokens)

        chunk_dups = 0
        chunk_kept = 0

        for doc in docs:
            total_docs += 1
            if len(doc) < 10:
                # Very short fragments β€” keep (usually chunk-boundary partials)
                writer.add_doc(doc)
                keep_count += 1
                chunk_kept += 1
                continue

            h = doc_hash(doc)
            if h in seen_hashes:
                dup_count += 1
                chunk_dups += 1
            else:
                seen_hashes.add(h)
                writer.add_doc(doc)
                keep_count += 1
                chunk_kept += 1

        if (ci + 1) % 25 == 0 or ci == num_chunks - 1:
            print(f"    Chunk {ci+1}/{num_chunks}: total docs={total_docs:,}, kept={keep_count:,}, dupes removed={dup_count:,}")

    # Finalize
    final_tokens, discarded = writer.finalize()
    new_chunks = writer.chunk_idx

    print(f"\n  PASS 1 COMPLETE:")
    print(f"    Total documents scanned: {total_docs:,}")
    print(f"    Documents kept:          {keep_count:,}")
    print(f"    Duplicates removed:      {dup_count:,} ({100*dup_count/max(total_docs,1):.2f}%)")
    print(f"    Unique hashes:           {len(seen_hashes):,}")
    print(f"    Tokens after dedup:      {final_tokens:,}")
    print(f"    Token reduction:         {original_tokens - final_tokens:,} ({100*(original_tokens-final_tokens)/original_tokens:.2f}%)")
    print(f"    Chunks after dedup:      {new_chunks}")
    print(f"    Discarded partial:       {discarded} tokens")

    # ── PASS 2: Swap temp into final ──────────────────────────────────────
    print(f"\n  PASS 2: Replacing original with deduplicated data...")

    # Remove old chunk files
    for meta in chunks_meta:
        old_file = FINAL_DIR / meta["filename"]
        if old_file.exists():
            os.remove(str(old_file))

    # Move new chunk files from temp to final
    import shutil
    for meta in writer.chunks_meta:
        src = TEMP_DIR / meta["filename"]
        dst = FINAL_DIR / meta["filename"]
        shutil.move(str(src), str(dst))

    # Remove temp dir
    shutil.rmtree(str(TEMP_DIR))

    # Update index.json
    new_index = {
        "chunks": writer.chunks_meta,
        "config": config,
        "updated_at": str(time.time()),
    }
    with open(FINAL_DIR / "index.json", "w") as f:
        json.dump(new_index, f, indent=2)

    elapsed = time.time() - t_start

    # ── Report ────────────────────────────────────────────────────────────
    report = []
    report.append(f"{'='*75}")
    report.append(f"  DEDUPLICATION REPORT β€” litdata_pretrain_final")
    report.append(f"{'='*75}")
    report.append(f"")
    report.append(f"  Time: {elapsed:.0f}s ({elapsed/60:.1f} min)")
    report.append(f"")
    report.append(f"  BEFORE:")
    report.append(f"    Chunks:    {num_chunks}")
    report.append(f"    Tokens:    {original_tokens:,}")
    report.append(f"    Documents: {total_docs:,}")
    report.append(f"")
    report.append(f"  AFTER:")
    report.append(f"    Chunks:    {new_chunks}")
    report.append(f"    Tokens:    {final_tokens:,}")
    report.append(f"    Documents: {keep_count:,}")
    report.append(f"")
    report.append(f"  REMOVED:")
    report.append(f"    Duplicate docs: {dup_count:,} ({100*dup_count/max(total_docs,1):.2f}%)")
    report.append(f"    Tokens removed: {original_tokens - final_tokens:,} ({100*(original_tokens-final_tokens)/original_tokens:.2f}%)")
    report.append(f"")
    report.append(f"  Format: litdata binary (int32, BLOCK_SIZE={BLOCK_SIZE}, EOS={EOS_TOKEN_ID})")
    report.append(f"  Location: {FINAL_DIR}")
    report.append(f"{'='*75}")

    full_report = '\n'.join(report)
    print(f"\n{full_report}")

    with open(FINAL_DIR / "DEDUP_REPORT.txt", "w", encoding="utf-8") as f:
        f.write(full_report)

    print(f"\n  Saved to: {FINAL_DIR / 'DEDUP_REPORT.txt'}")
    print(f"  Done! Dataset is now clean and deduplicated.")


if __name__ == "__main__":
    main()