-
Notifications
You must be signed in to change notification settings - Fork 20
/
minhash_deduplication_alt.py
336 lines (291 loc) · 10.5 KB
/
minhash_deduplication_alt.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author : Chenghao Mou (mouchenghao@gmail.com)
# created : 10/4/22
from __future__ import annotations
import gc
import hashlib
import logging
import multiprocessing as mp
import os
import random
import re
import struct
import time
import warnings
from collections import defaultdict
from itertools import tee
from pathlib import Path
from typing import Any
from typing import Dict
from typing import Iterable
from typing import List
from typing import Tuple
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=FutureWarning)
import datasets
import numpy as np
import typer
from datasets import load_dataset
from scipy.integrate import quad as integrate
from tqdm import tqdm
SEED = 42
NON_ALPHA = re.compile("[^A-Za-z_0-9]")
RNG = np.random.RandomState(SEED)
MAX_HASH = np.uint64((1 << 32) - 1)
MERSENNE_PRIME = np.uint64((1 << 61) - 1)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
datasets.logging.set_verbosity_error()
def ngrams(sequence: List[str], n: int) -> Iterable:
"""
Directly taken from nltk package to avoid dependency.
Parameters
----------
sequence : list
The sequence of items to be n-grammed.
n : int
The order of the n-grams to be extracted.
Returns
-------
Iterable
The n-grams generated from the sequence.
"""
iterables = tee(sequence, n)
for i, sub_iterable in enumerate(iterables):
for _ in range(i):
next(sub_iterable, None)
return zip(*iterables)
def sha1_hash32(data):
"""
Directly taken from datasketch package to avoid dependency.
Parameters
----------
data : bytes
Returns
-------
int
"""
return struct.unpack("<I", hashlib.sha1(data).digest()[:4])[0]
def embed_func(
content: str,
idx: int,
*,
num_perm: int,
ngram_size: int,
hashranges: List[Tuple[int, int]],
permutations: np.ndarray,
) -> Dict[str, Any]:
"""
Combined with some datasketch code to better parallelize computation.
Parameters
----------
content : str
The content to be embedded.
idx : int
The index of the content.
num_perm : int
The number of permutations.
ngram_size : int
The size of n-grams.
hashranges : List[Tuple[int, int]]
The ranges of hash values.
permutations : np.ndarray
The permutations for the minhash.
Returns
-------
Dict[str, Any]
The hash values in each range and the index.
"""
hashvalues = np.ones(num_perm, dtype=np.uint64) * MAX_HASH
tokens = {" ".join(t) for t in ngrams(NON_ALPHA.split(content), ngram_size)}
hv = np.array([sha1_hash32(token.encode("utf-8")) for token in tokens], dtype=np.uint64) # noqa: E501
a, b = permutations
phv = np.bitwise_and(((hv * np.tile(a, (len(hv), 1)).T).T + b) % MERSENNE_PRIME, MAX_HASH) # noqa: E501
hashvalues = np.vstack([phv, hashvalues]).min(axis=0)
Hs = [bytes(hashvalues[start:end].byteswap().data) for start, end in hashranges]
return {"__signatures__": Hs, "__id__": idx}
def optimal_param(
threshold: float,
num_perm: int,
false_positive_weight: float = 0.5,
false_negative_weight: float = 0.5,
):
"""
Compute the optimal `MinHashLSH` parameter that minimizes the weighted sum
of probabilities of false positive and false negative, taken from datasketch.
Parameters
----------
threshold : float
The threshold for similarity.
num_perm : int
The number of permutations.
false_positive_weight : float
The weight of false positive.
false_negative_weight : float
The weight of false negative.
Returns
-------
Tuple[int, int]
The optimal `b` and `r` parameters.
The number of bands, and the number of rows per band respectively.
"""
def false_positive_probability(threshold: float, b: int, r: int):
"""Source: `datasketch.lsh`"""
def proba(s):
return 1 - (1 - s ** float(r)) ** float(b)
a, _ = integrate(proba, 0.0, threshold)
return a
def false_negative_probability(threshold: float, b: int, r: int):
"""Source: `datasketch.lsh`"""
def proba(s):
return 1 - (1 - (1 - s ** float(r)) ** float(b))
a, _ = integrate(proba, threshold, 1.0)
return a
min_error = float("inf")
opt = (0, 0)
for b in range(1, num_perm + 1):
max_r = int(num_perm / b)
for r in range(1, max_r + 1):
fp = false_positive_probability(threshold, b, r)
fn = false_negative_probability(threshold, b, r)
error = fp * false_positive_weight + fn * false_negative_weight
if error < min_error:
min_error = error
opt = (b, r)
return opt
class UnionFind:
def __init__(self):
self.parent: Dict[int, int] = {}
def find(self, x):
if x not in self.parent:
self.parent[x] = x
if self.parent[x] != x:
self.parent[x] = self.find(self.parent[x])
return self.parent[x]
def union(self, x, y):
px = self.find(x)
py = self.find(y)
self.parent[px] = self.parent[py] = min(px, py)
if __name__ == "__main__":
def run(
dataset: str = typer.Option("codeparrot/codeparrot-clean-valid", help="The dataset to use"), # noqa: E501
config: str = typer.Option("default", help="Dataset config"),
split: str = typer.Option("train", help="Dataset split"),
data_dir: str = typer.Option(None, help="Dataset data directory"),
revision: str = typer.Option("main", help="Dataset revision"),
column: str = typer.Option("content", help="Dataset column"),
cache_dir: str = typer.Option(".cache", help="Cache directory"),
ngram_size: int = typer.Option(5, help="The ngram size to use for MinHash"),
num_perm: int = typer.Option(256, help="Number of permutations"),
threshold: float = typer.Option(0.7, help="Minhash threshold"),
output: str = typer.Option(None, help="Store the deduplicated dataset"),
):
global uf
OUTPUT_BASE = Path(output or "output")
OUTPUT_BASE.mkdir(exist_ok=True, parents=True)
output = OUTPUT_BASE / "deduplicated"
logging.basicConfig(level=logging.INFO)
time_measures = {}
start_time = time.time()
B, R = optimal_param(threshold, num_perm)
HASH_RANGES = [(i * R, (i + 1) * R) for i in range(B)]
HASH_TABLES = [defaultdict(set) for _ in range(B)]
time_measures["load_dataset"] = time.time()
ds = load_dataset(
dataset,
config,
data_dir=data_dir,
split=split,
use_auth_token=True,
cache_dir=cache_dir,
revision=revision,
num_proc=os.cpu_count(),
)
time_measures["load_dataset"] = time.time() - time_measures["load_dataset"]
DATA_SIZE = len(ds)
PERMUTATIONS = np.array(
[
(
RNG.randint(1, MERSENNE_PRIME, dtype=np.uint64),
RNG.randint(0, MERSENNE_PRIME, dtype=np.uint64),
)
for _ in range(num_perm)
],
dtype=np.uint64,
).T
time_measures["minhash"] = time.time()
embedded = ds.map(
function=embed_func,
fn_kwargs={
"num_perm": num_perm,
"hashranges": HASH_RANGES,
"ngram_size": ngram_size,
"permutations": PERMUTATIONS,
},
input_columns=[column],
remove_columns=ds.column_names,
num_proc=os.cpu_count(),
with_indices=True,
desc="Fingerprinting...",
)
time_measures["minhash"] = time.time() - time_measures["minhash"]
time_measures["clustering"] = time.time()
batch_size: int = 10000
for i in tqdm(
range(0, len(embedded), batch_size), dynamic_ncols=True, desc="Iterating MinHashes..." # noqa: E501
):
batch = embedded[i : i + batch_size]
for key, Hs in zip(batch["__id__"], batch["__signatures__"]):
for H, hashtable in zip(Hs, HASH_TABLES):
hashtable[H].add(key)
for table in tqdm(HASH_TABLES, dynamic_ncols=True, desc="Clustering..."):
for cluster in table.values():
if len(cluster) <= 1:
continue
idx = min(cluster)
for x in cluster:
uf.union(x, idx)
time_measures["clustering"] = time.time() - time_measures["clustering"]
time_measures["filtering"] = time.time()
gc.freeze()
gc.disable()
ds = ds.map(
function=lambda _, idx: {"__cluster__": uf.find(idx)},
with_indices=True,
num_proc=os.cpu_count(),
new_fingerprint=str(random.getrandbits(128)),
desc="Finding clusters...",
)
gc.enable()
gc.collect()
# This is where the deduplication happens
# Since there is no easy groupby in datasets
# I will use this simple filter for now
final_data = ds.filter(
function=lambda record, idx: record["__cluster__"] == idx,
with_indices=True,
num_proc=os.cpu_count(),
desc="Filtering clusters...",
)
time_measures["filtering"] = time.time() - time_measures["filtering"]
time_measures["save"] = time.time()
final_data = final_data.remove_columns(["__cluster__"])
final_data.save_to_disk(output)
time_measures["save"] = time.time() - time_measures["save"]
FINAL_DATA_SIZE = len(final_data)
DUP_SIZE = DATA_SIZE - FINAL_DATA_SIZE
PAD = 32
for key, value in time_measures.items():
logger.info(f"{key:<{PAD}}: {value:.2f} seconds")
logger.info(f"{'Data Number (before)':<{PAD}}: {DATA_SIZE}")
logger.info(
f"{'Data Number (after)':<{PAD}}: {FINAL_DATA_SIZE} ({FINAL_DATA_SIZE / DATA_SIZE:.2%})" # noqa: E501
)
logger.info(f"{'Duplicate Number':<{PAD}}: {DUP_SIZE} ({DUP_SIZE / DATA_SIZE:.2%})") # noqa: E501
logger.info(f"{'Total Time':<{PAD}}: {time.time() - start_time:.2f} seconds")
logger.info(f"{'Deduplicated Dataset':<{PAD}}: {output}")
logger.info("🤗 Happy Deduplicating 🤗")
mp.set_start_method("fork", force=True)
uf = UnionFind()
typer.run(run)