File size: 23,438 Bytes
6dd0ae1 |
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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 |
import os
import re
import heapq
import pickle
import struct
import contextlib
import numpy as np
from typing import Generator
from pathlib import Path
from dataclasses import dataclass, field
from fsspec.spec import AbstractBufferedFile
from datatrove.data import DocumentsPipeline
from datatrove.io import DataFolderLike, get_datafolder
from datatrove.pipeline.base import PipelineStep
from datatrove.pipeline.writers.disk_base import DiskWriter
from datatrove.utils.binaryio import read_tuples_from_file, seek_to_start
from datatrove.utils.hashing import HashConfig, create_hash_func
from datatrove.utils.logging import logger
from datatrove.utils.text import TextNormConfig, ngrams
from datatrove.utils.typeshelper import StatHints
# http://en.wikipedia.org/wiki/Mersenne_prime
_mersenne_prime = np.uint64((1 << 61) - 1)
SENTINEL = (1 << 32) - 1
dataset_map = {}
datasets = []
cnt = 0
def transform(dataset_idx: int, line_no: int) -> int:
global cnt
global dataset_map
global datasets
if dataset_idx not in dataset_map:
dataset_map[dataset_idx] = cnt
datasets.append(dataset_idx)
cnt += 1
return line_no * 10000 + dataset_map[dataset_idx]
@dataclass
class CustomMinhashConfig:
"""Configuration for Min-Hash deduplication
Args:
n_grams: n-grams size to use
num_buckets: number of buckets to use
seed: random seed used to generate the hash function parameters. Should be the same on all workers to ensure they all have the same parameters
"""
n_grams: int = 13
num_buckets: int = 9
num_hashs: int = 128
seed: int = 1
norm_config: TextNormConfig = field(default_factory=TextNormConfig)
hash_config: HashConfig = field(default_factory=HashConfig)
def __str__(self):
return f"{self.n_grams}ng_{self.num_buckets}bs_{self.hash_config}"
@dataclass(order=True)
class HashSig:
"""Hash signature for a given document in a given bucket
Args:
sig: tuple of hashes
file_id: file id
doc_id: document id
reader_id: reader id. Used to know from where the next signature should be requested
"""
sig: tuple[int]
doc_id: int
data_index: int
reader_id: int
def is_from_index(self):
return False
def read_sigs(
file: AbstractBufferedFile,
config: CustomMinhashConfig,
index_file: bool = False,
min_hash: int = 0,
max_hash: int = _mersenne_prime,
ensure_order: bool = True,
lines_to_buffer: int = 5,
reader_id: int = 0,
) -> Generator:
"""Read signatures from a file
Args:
file: file to read from
reader_id: reader id
config: minhash configuration (a MinhashConfig object)
index_file: is index file
"""
line_format = f"{config.n_grams}{config.hash_config.struct_format}{'I' if not index_file else ''}"
with file as f:
if f.size == 0:
return
seek_to_start(f, min_hash, line_format, config.hash_config.struct_format)
last = None
# CC-MAIN-2013-20_00000.minhash.sig
file_stem = Path(file.path).name.removesuffix(".minhash.sig")
data_index = 0
if len(file_stem) == 21:
dataset_year = int(file_stem[8:12])
dataset_num = int(file_stem[13:15])
parquet_num = int(file_stem[16:21])
# 13 20 00000
data_index = ((dataset_year % 100) * 100 + dataset_num) * 100000 + parquet_num
for data in read_tuples_from_file(f, line_format, lines_to_buffer=lines_to_buffer):
sigdata = data if index_file else data[:-1]
assert sigdata[0] >= min_hash and (
ensure_order is False or last is None or sigdata >= last
), f"Hash order error. {f.tell()=}, {min_hash=}, {sigdata=}, {last=}"
if sigdata[0] >= max_hash:
break
last = sigdata
yield (
HashSig(sig=sigdata, doc_id=-1, data_index=data_index, reader_id=reader_id)
if index_file
else HashSig(sig=sigdata, doc_id=data[-1], data_index=data_index, reader_id=reader_id)
)
class CustomMinhashDedupSignature(PipelineStep):
"""Minhash Deduplication: First Pipeline Step
Compute the minhash signature for each document and write it to disk.
Args:
output_folder: output folder
config: minhash configuration (a MinhashConfig object)
"""
type = "🫂 - DEDUP"
name = "🎯 MinHash stage 1"
def __init__(self, output_folder: DataFolderLike, config: CustomMinhashConfig = None, naming_prefix: str = ''):
super().__init__()
self.output_folder = get_datafolder(output_folder)
self.config = config or CustomMinhashConfig()
self.num_hashes = self.config.num_hashs
self.naming_prefix = naming_prefix
self._parameters = None
self._hash_func = create_hash_func(self.config.hash_config)
@property
def parameters(self):
"""Minhash parameters
Create parameters for a random bijective permutation function
that maps a 32/64-bit hash value to another 32/64-bit hash value.
http://en.wikipedia.org/wiki/Universal_hashing
Note: For 64-bit hashes the upper-bound for codomain is not [0,2**64) but [0,2**61 - 1)
"""
if self._parameters is None:
gen = np.random.RandomState(self.config.seed)
self._parameters = np.array(
[
(
gen.randint(1, _mersenne_prime, dtype=np.uint64),
gen.randint(0, _mersenne_prime, dtype=np.uint64),
)
for _ in range(self.num_hashes)
],
dtype=np.uint64,
).T
return self._parameters
def get_signature(self, shingles: np.ndarray) -> list[list[int]]:
"""Get the signature for a set of shingles (n-grams)
Args:
shingles: shingles (n-grams) numpy uint64 array of size (N, 1)
Returns:
list (num buckets) of lists of integers (hashes)
"""
a, b = self.parameters
phv = (shingles * a + b) % _mersenne_prime
if self.config.hash_config.precision == 32:
phv = np.bitwise_and(phv, self.config.hash_config.max)
return [
x.tolist()
for x in np.split(np.min(phv, axis=0)[:self.config.num_buckets * self.config.n_grams], self.config.num_buckets)
]
def get_shingles(self, text: str) -> np.ndarray:
"""Get shingles (hashed n-grams) from a string of text
Shingles are created by hashing n-grams of simplified text (lower cases, whitespace normalized, no punctuation, etc).
Args:
text: input text
Returns:
numpy array of shingles: dtype = uint64, shape = (number of n_grams in string, 1)
"""
return np.fromiter(
[
self._hash_func("".join(x))
for x in ngrams(text, self.config.n_grams)
],
dtype=np.uint64,
).reshape((-1, 1))
# def write_buckets(self, batch_id, rank, band_hash_value_list):
# try:
# for bucket in range(self.config.num_buckets):
# with self.output_folder.open(f"{bucket:02d}/{batch_id:05d}_{rank:05d}.minhash.pkl", mode="wb") as fout:
# pickle.dump(band_hash_value_list[bucket], fout)
# except Exception as e:
# logger.error(f"dump minhash fail, error: {e}")
def run(self, data: DocumentsPipeline, rank: int = 0, world_size: int = 1) -> DocumentsPipeline:
buckets = [
self.output_folder.open(f"bucket_{bi:03d}/{self.naming_prefix}_{rank:05d}.minhash.sig", mode="wb")
for bi in range(self.config.num_buckets)
]
with self.track_time():
# batch_id = 0
for doc_idx, doc in enumerate(data):
self.stat_update(StatHints.total)
shingles = self.get_shingles(doc.text)
if shingles.size != 0:
sig = self.get_signature(shingles)
for bi, (bucket, bucket_sig) in enumerate(zip(buckets, sig)):
# print(f"{self.n_grams=} {bucket_sig=}")
bucket.write(
struct.pack(
f"<{self.config.n_grams}{self.config.hash_config.struct_format}I",
*bucket_sig,
doc_idx,
)
)
for file in buckets:
file.close()
logger.info("Sorting buckets...")
for bi in range(len(buckets)):
# read one by one, sort and write back
sigs = sorted(
read_sigs(
self.output_folder.open(f"bucket_{bi:03d}/{self.naming_prefix}_{rank:05d}.minhash.sig", mode="rb"),
self.config,
ensure_order=False,
lines_to_buffer=-1, # load everything in one go
)
)
with self.output_folder.open(f"bucket_{bi:03d}/{self.naming_prefix}_{rank:05d}.minhash.sig", mode="wb") as fo:
for sig in sigs:
fo.write(
struct.pack(
f"<{self.config.n_grams}{self.config.hash_config.struct_format}I",
*sig.sig,
sig.doc_id,
)
)
class CustomMinhashDedupBuckets(PipelineStep):
"""Minhash Deduplication: Second Pipeline Step
Find duplicate pairs from the signatures and possibly an index. Can also save an index with the new signatures.
Args:
input_folder: input folder containing the signature from step 1
output_folder: output folder where results (document duplicate pairs) will be saved
index_folder: index folder. If set, we will load all index files in this folder and use them as a reference for deduplicating the current dataset (remove any matches on our dataset with signatures from the index)
config: minhash configuration (a MinhashConfig object)
only_dedup_in_index: only deduplicate versus index (ignore any matches between 2 documents in our input dataset)
create_index_name: create index name. If this parameter is set, index files will be created with this name that other datasets can use as a reference for dedup. Set to `None` to disable index file creation.
"""
type = "🫂 - DEDUP"
name = "🎯 MinHash stage 2"
def __init__(
self,
input_folder: DataFolderLike,
output_folder: DataFolderLike,
config: CustomMinhashConfig = None,
lines_to_buffer: int = 5,
):
super().__init__()
self.input_folder = get_datafolder(input_folder)
self.output_folder = get_datafolder(output_folder)
self.config = config or CustomMinhashConfig()
self.lines_to_buffer = lines_to_buffer
def get_worker_hash_range(self, sig_files, rank, world_size):
workers_per_bucket = world_size // self.config.num_buckets
bucket, bucket_worker = divmod(rank, workers_per_bucket)
hash_min, hash_max = (
0,
_mersenne_prime if self.config.hash_config.precision == 64 else self.config.hash_config.max,
)
if workers_per_bucket > 1 and len(sig_files):
# take the first file and find bucket_worker boundaries. all workers in a bucket process the same set of
# files, so this should be consistent across workers (and span the entire range of hashes)
with self.input_folder.open(sig_files[0], mode="rb") as f:
line_size = struct.calcsize(f"{self.config.n_grams}{self.config.hash_config.struct_format}I")
L, rem = divmod(f.size, line_size)
assert rem == 0, "file size not divisible by line size"
assert L >= workers_per_bucket, f"tried to use {workers_per_bucket=} but there are only {L} lines"
if bucket_worker > 0:
# not first
f.seek(line_size * (L // workers_per_bucket) * bucket_worker, os.SEEK_SET)
hash_min = struct.unpack(
self.config.hash_config.struct_format,
f.read(struct.calcsize(self.config.hash_config.struct_format)),
)[0]
if bucket_worker + 1 < workers_per_bucket:
# not last
f.seek(line_size * (L // workers_per_bucket) * (bucket_worker + 1), os.SEEK_SET)
hash_max = struct.unpack(
self.config.hash_config.struct_format,
f.read(struct.calcsize(self.config.hash_config.struct_format)),
)[0]
return hash_min, hash_max
def run(self, data: DocumentsPipeline = None, rank: int = 0, world_size: int = 1):
assert data is None, "You should not use an input block before MinhashDedupBuckets"
assert (world_size % self.config.num_buckets) == 0, "Number of tasks must be divisible by num_buckets"
workers_per_bucket = world_size // self.config.num_buckets
bucket, bucket_worker = divmod(rank, workers_per_bucket)
with self.track_time():
sig_files = self.input_folder.list_files(subdirectory=f"bucket_{bucket:03d}")
hash_min, hash_max = self.get_worker_hash_range(sig_files, rank, world_size)
logger.info(
f"Running worker {bucket_worker + 1}/{workers_per_bucket} on bucket {bucket:03d}. "
f"Hash range: {[hash_min, hash_max]}"
)
sig_readers = [
read_sigs(
file,
self.config,
min_hash=hash_min,
max_hash=hash_max,
lines_to_buffer=self.lines_to_buffer,
reader_id=file_i,
)
for file_i, file in enumerate(self.input_folder.open_files(sig_files, mode="rb"))
]
pq = [x for x in [next(sig_reader, None) for sig_reader in sig_readers] if x is not None]
heapq.heapify(pq)
logger.info("Finished initializing signatures priority queue.")
with self.output_folder.open(f"{bucket:05d}_{bucket_worker:02d}.dups", mode="wb") as out_f:
last: HashSig | None = None
while pq:
v: HashSig = heapq.heappop(pq)
assert last is None or v >= last, f"Sig queue sort error. {v=} < {last=}"
if last and last.sig == v.sig:
# write (file_id1, doc_id1, file_id2, doc_id2)
out_f.write(
struct.pack("<4I", last.data_index, last.doc_id, v.data_index, v.doc_id)
)
self.stat_update("total_matches")
last = v
next_sig = next(sig_readers[v.reader_id], None)
if next_sig:
assert next_sig >= v, f"Next sig sort error. {next_sig=} < {v=}"
heapq.heappush(pq, next_sig)
class CustomMinhashDedupCluster(PipelineStep):
"""Minhash Deduplication: Third Pipeline Step
Cluster the documents using the previously found duplicate pairs. If A-B and B-C are duplicate pairs, then we will have the A-B-C cluster. Only one document per cluster will be kept after filtering
"""
type = "🫂 - DEDUP"
name = "🎯 MinHash stage 3"
def __init__(
self,
input_folder: DataFolderLike,
output_folder: DataFolderLike,
config: CustomMinhashConfig = None,
ignore_index_matches: bool = False,
lines_to_buffer: int = 5,
):
super().__init__()
self.input_folder = get_datafolder(input_folder)
self.output_folder = get_datafolder(output_folder)
self.config = config or CustomMinhashConfig()
self.ignore_index_matches = ignore_index_matches
self.lines_to_buffer = lines_to_buffer
def run(self, data: DocumentsPipeline = None, _: int = 0, world_size: int = 1):
global datasets
global dataset_map
dup_files = self.input_folder.list_files(glob_pattern="*.dups")
assert (
len(dup_files) % self.config.num_buckets
) == 0, "Number of .dups files should be divisible by number of buckets"
assert world_size == 1, "World size must be 1 for clustering"
union_set = np.arange(0, 1_500_000 * 10_000, dtype=np.uint64)
exists = np.zeros(1_500_000 * 10_000, dtype=bool)
max_no = 0
def parent(x):
exists[x] = 1
if union_set[x] == x:
return x
# Path Compression
union_set[x] = parent(union_set[x])
return union_set[x]
with self.track_time():
for dup_file in dup_files:
with self.input_folder.open(dup_file, "rb") as dupf:
logger.info(f"Processing {dup_file}")
for f1, d1, f2, d2 in read_tuples_from_file(dupf, "4I", lines_to_buffer=self.lines_to_buffer):
a, b = transform(f1, d1), transform(f2, d2)
if a > max_no:
max_no = a
if b > max_no:
max_no = b
union_set[parent(b)] = parent(a)
logger.info("Outputing")
with self.output_folder.get_output_file_manager(mode="wb") as output_mg:
for node in range(max_no + 1):
if exists[node]:
self.stat_update("duplicates")
p = parent(node)
if node != p:
dataset_idx = datasets[node % 10000]
dataset_year = dataset_idx // 10000000
dataset_num = (dataset_idx // 100000) % 100
parquet_num = dataset_idx % 100000
output_mg.write(f"CC-MAIN-20{dataset_year:02d}-{dataset_num:02d}_{parquet_num:05d}.remove", struct.pack("<I", node // 10000))
self.stat_update("to_remove")
class CustomMinhashDedupFilter(PipelineStep):
"""Minhash Deduplication: Fourth (and final) Pipeline Step
Filter the documents based on the minhash clusters to keep only one per cluster
"""
type = "🫂 - DEDUP"
name = "🎯 MinHash stage 4"
def __init__(
self,
remove_id_input_folder: DataFolderLike,
sig_input_folder: DataFolderLike,
exclusion_writer: DiskWriter = None,
lines_to_buffer: int = 5,
naming_prefix: str = '',
config: CustomMinhashConfig = None,
):
super().__init__()
self.remove_id_folder = get_datafolder(remove_id_input_folder)
self.sig_folder = get_datafolder(sig_input_folder)
self.exclusion_writer = exclusion_writer
self.lines_to_buffer = lines_to_buffer
self.naming_prefix = naming_prefix
self.config = config or CustomMinhashConfig()
def run(self, data: DocumentsPipeline, rank: int = 0, world_size: int = 1):
files = self.remove_id_folder.list_files(glob_pattern=f"{self.naming_prefix}*{rank:05d}.remove")
if not files or len(files) == 0:
logger.info(f"Found 0 files by pattern {self.naming_prefix}*{rank:05d}.remove, maybe no dups")
for bucket in range(self.config.num_buckets):
for sig_file_name in self.sig_folder.list_files(glob_pattern=f"bucket_{bucket:03d}/{self.naming_prefix}*{rank:05d}.minhash.sig"):
file_name = Path(sig_file_name).name.removesuffix(".minhash.sig")
save_docs = []
for sig in read_sigs(
self.sig_folder.open(f"bucket_{bucket:03d}/{file_name}.minhash.sig", "rb"),
self.config,
ensure_order=False,
):
self.stat_update(StatHints.total)
save_doc = {}
save_doc['doc_id'] = file_name + ':' + str(sig.doc_id)
save_doc['hash'] = bytes(np.array(sig.sig).astype(np.uint64).byteswap().data)
save_docs.append(save_doc)
self.stat_update(StatHints.forwarded)
with self.sig_folder.open(f"bucket_{bucket:03d}/{file_name}.pkl", "wb") as out_file:
pickle.dump(save_docs, out_file)
return
single_int = struct.Struct("<I")
for file in files:
logger.info(f"Processing {file}")
remove_id_file = self.remove_id_folder.open(file, "rb")
logger.info(remove_id_file)
remove_ids = set()
while True:
chunk = remove_id_file.read(single_int.size)
if not chunk:
break
remove_ids.add(single_int.unpack(chunk))
remove_id_file.close()
file_name = Path(file).name.removesuffix(".remove")
for bucket in range(self.config.num_buckets):
save_docs = []
for sig in read_sigs(
self.sig_folder.open(f"bucket_{bucket:03d}/{file_name}.minhash.sig", "rb"),
self.config,
ensure_order=False,
):
self.stat_update(StatHints.total)
if sig.doc_id in remove_ids:
continue
save_doc = {}
save_doc['doc_id'] = file_name + ':' + str(sig.doc_id)
save_doc['hash'] = bytes(np.array(sig.sig).astype(np.uint64).byteswap().data)
save_docs.append(save_doc)
self.stat_update(StatHints.forwarded)
with self.sig_folder.open(f"bucket_{bucket:03d}/{file_name}.pkl", "wb") as out_file:
pickle.dump(save_docs, out_file)
# for bucket_id in range(self.config.num_buckets):
# save_doc = {}
# save_doc[self.config.doc_id_field_name] = doc.metadata['dump'] + ':' + doc.id
# save_doc[self.config.hash_field_name] = hash_value_list[bucket_id]
# bucket_id = int.from_bytes(hash_value_list[bucket_id], 'big') % self.config.num_buckets
# band_hash_value_list[bucket_id].append(save_doc)
# if doc_idx % self.config.write_line_num == 0 and doc_idx != 0:
# self.write_bands(rank, batch_id, band_hash_value_list)
# batch_id += 1
# band_hash_value_list = [[[] for _ in range(self.config.num_buckets)] for _ in range(self.config.num_buckets)]
# if len(band_hash_value_list[0]) > 0:
# self.write_bands(rank, batch_id, band_hash_value_list)
|