Datasets:

Modalities:
Text
ArXiv:
Libraries:
Datasets
Maurice Weber commited on
Commit
2cae54f
1 Parent(s): 04e7975

add dedupe flag

Browse files
Files changed (1) hide show
  1. RedPajama-Data-V2.py +61 -13
RedPajama-Data-V2.py CHANGED
@@ -23,6 +23,8 @@ import os
23
  import gzip
24
  from typing import List
25
 
 
 
26
  logger = datasets.logging.get_logger(__name__)
27
 
28
  _DESCRIPTION = """\
@@ -188,13 +190,22 @@ class RedPajamaV2(datasets.GeneratorBasedBuilder):
188
  ]
189
  })
190
 
 
 
 
 
 
 
 
 
191
  return [
192
  datasets.SplitGenerator(
193
  name=datasets.Split.TRAIN,
194
  gen_kwargs={
195
  "listings_ids": {"head_middle": listings},
196
  "documents_files": documents_files,
197
- "quality_signals_files": quality_signals_files
 
198
  }
199
  )
200
  ]
@@ -235,16 +246,21 @@ class RedPajamaV2(datasets.GeneratorBasedBuilder):
235
  line.strip() for line in f
236
  ])
237
 
238
- # build urls pointing to documents and quality signals
239
  document_urls = {}
240
  quality_signals_urls = {}
 
241
  for part, part_listings_ids in listings_ids.items():
 
242
  document_urls[part] = []
243
  quality_signals_urls[part] = []
 
 
244
  for lst_id in part_listings_ids:
245
  document_urls[part].append(
246
  os.path.join(_URL_BASE, f"documents/{lst_id}.json.gz")
247
  )
 
248
  if part != "head_middle":
249
  continue
250
 
@@ -254,8 +270,15 @@ class RedPajamaV2(datasets.GeneratorBasedBuilder):
254
  )
255
  )
256
 
 
 
 
 
 
 
257
  documents_files = dl_manager.download(document_urls)
258
  quality_signals_files = dl_manager.download(quality_signals_urls)
 
259
 
260
  return [
261
  datasets.SplitGenerator(
@@ -263,7 +286,8 @@ class RedPajamaV2(datasets.GeneratorBasedBuilder):
263
  gen_kwargs={
264
  "listings_ids": listings_ids,
265
  "documents_files": documents_files,
266
- "quality_signals_files": quality_signals_files
 
267
  }
268
  )
269
  ]
@@ -275,13 +299,15 @@ class RedPajamaV2(datasets.GeneratorBasedBuilder):
275
  return self._split_generators_full(dl_manager)
276
 
277
  def _generate_examples(
278
- self, listings_ids, documents_files, quality_signals_files
 
279
  ):
280
  key = 0
281
  for part in documents_files.keys():
282
  part_docs_files = documents_files[part]
283
  part_qs_files = quality_signals_files[part]
284
  part_listings_ids = listings_ids[part]
 
285
 
286
  if len(part_qs_files) == 0:
287
  for sample in self._handle_tail_partition(
@@ -292,7 +318,9 @@ class RedPajamaV2(datasets.GeneratorBasedBuilder):
292
  continue
293
 
294
  for sample in self._handle_head_middle_partition(
295
- part, part_docs_files, part_qs_files, part_listings_ids
 
 
296
  ):
297
  yield key, sample
298
  key += 1
@@ -303,7 +331,7 @@ class RedPajamaV2(datasets.GeneratorBasedBuilder):
303
  for row, doc in enumerate(df):
304
  doc_id = f"{listing_id}.json.gz/{row}"
305
  try:
306
- yield self.handle_record(part, doc_id, doc, None)
307
  except Exception as e:
308
  print(f'doc_file: {doc_file}')
309
  print(f'row: {row}')
@@ -311,22 +339,41 @@ class RedPajamaV2(datasets.GeneratorBasedBuilder):
311
  raise e
312
 
313
  def _handle_head_middle_partition(
314
- self, part, docs_files, qs_files, listings_ids
315
  ):
316
  assert len(docs_files) == len(qs_files)
317
 
318
  listings_ids = listings_ids[:len(docs_files)]
319
 
320
- for doc_file, qs_file, listings_id in zip(
321
- docs_files, qs_files, listings_ids
322
  ):
 
 
 
 
 
 
 
 
 
 
323
  try:
324
  with gzip.open(doc_file, "rt", encoding="utf-8") as df:
325
  with gzip.open(qs_file, "rt", encoding="utf-8") as qf:
326
  for row, (doc, qs) in enumerate(zip(df, qf)):
327
  doc_id = f"{listings_id}.json.gz/{row}"
 
 
 
 
 
 
328
  try:
329
- yield self.handle_record(part, doc_id, doc, qs)
 
 
 
330
  except Exception as e:
331
  print(f'failed handling row {row} in '
332
  f'{doc_file} ({qs_file})')
@@ -339,7 +386,7 @@ class RedPajamaV2(datasets.GeneratorBasedBuilder):
339
  continue
340
 
341
  @staticmethod
342
- def handle_record(part, doc_id, doc, qs):
343
  doc = json.loads(doc)
344
  qs = json.loads(qs) if qs is not None else {}
345
 
@@ -352,11 +399,12 @@ class RedPajamaV2(datasets.GeneratorBasedBuilder):
352
  "digest": doc["digest"],
353
  }
354
 
355
- quality_signals = json.dumps(qs.get("quality_signals", {}))
 
356
 
357
  return {
358
  "raw_content": doc["raw_content"],
359
  "doc_id": doc_id,
360
  "meta": json.dumps(meta),
361
- "quality_signals": quality_signals,
362
  }
 
23
  import gzip
24
  from typing import List
25
 
26
+ import pyarrow.parquet as pq
27
+
28
  logger = datasets.logging.get_logger(__name__)
29
 
30
  _DESCRIPTION = """\
 
190
  ]
191
  })
192
 
193
+ # fetch ids of duplicates
194
+ duplicates_ids_files = dl_manager.download({
195
+ "head_middle": [
196
+ f"sample/duplicates/{lst}.duplicates.parquet"
197
+ for lst in listings
198
+ ]
199
+ })
200
+
201
  return [
202
  datasets.SplitGenerator(
203
  name=datasets.Split.TRAIN,
204
  gen_kwargs={
205
  "listings_ids": {"head_middle": listings},
206
  "documents_files": documents_files,
207
+ "quality_signals_files": quality_signals_files,
208
+ "duplicates_ids_files": duplicates_ids_files
209
  }
210
  )
211
  ]
 
246
  line.strip() for line in f
247
  ])
248
 
249
+ # build urls pointing to documents, quality signals and duplicate ids
250
  document_urls = {}
251
  quality_signals_urls = {}
252
+ duplicates_ids_urls = {}
253
  for part, part_listings_ids in listings_ids.items():
254
+
255
  document_urls[part] = []
256
  quality_signals_urls[part] = []
257
+ duplicates_ids_urls[part] = []
258
+
259
  for lst_id in part_listings_ids:
260
  document_urls[part].append(
261
  os.path.join(_URL_BASE, f"documents/{lst_id}.json.gz")
262
  )
263
+
264
  if part != "head_middle":
265
  continue
266
 
 
270
  )
271
  )
272
 
273
+ duplicates_ids_urls[part].append(
274
+ os.path.join(
275
+ _URL_BASE, f"duplicates/{lst_id}.duplicates.parquet"
276
+ )
277
+ )
278
+
279
  documents_files = dl_manager.download(document_urls)
280
  quality_signals_files = dl_manager.download(quality_signals_urls)
281
+ duplicates_ids_files = dl_manager.download(duplicates_ids_urls)
282
 
283
  return [
284
  datasets.SplitGenerator(
 
286
  gen_kwargs={
287
  "listings_ids": listings_ids,
288
  "documents_files": documents_files,
289
+ "quality_signals_files": quality_signals_files,
290
+ "duplicates_ids_files": duplicates_ids_files
291
  }
292
  )
293
  ]
 
299
  return self._split_generators_full(dl_manager)
300
 
301
  def _generate_examples(
302
+ self, listings_ids, documents_files, quality_signals_files,
303
+ duplicates_ids_files
304
  ):
305
  key = 0
306
  for part in documents_files.keys():
307
  part_docs_files = documents_files[part]
308
  part_qs_files = quality_signals_files[part]
309
  part_listings_ids = listings_ids[part]
310
+ part_duplicates_ids_files = duplicates_ids_files[part]
311
 
312
  if len(part_qs_files) == 0:
313
  for sample in self._handle_tail_partition(
 
318
  continue
319
 
320
  for sample in self._handle_head_middle_partition(
321
+ part, part_docs_files, part_qs_files,
322
+ part_duplicates_ids_files, part_listings_ids
323
+
324
  ):
325
  yield key, sample
326
  key += 1
 
331
  for row, doc in enumerate(df):
332
  doc_id = f"{listing_id}.json.gz/{row}"
333
  try:
334
+ yield self.handle_record(part, doc_id, doc, None, None)
335
  except Exception as e:
336
  print(f'doc_file: {doc_file}')
337
  print(f'row: {row}')
 
339
  raise e
340
 
341
  def _handle_head_middle_partition(
342
+ self, part, docs_files, qs_files, dupes_files, listings_ids,
343
  ):
344
  assert len(docs_files) == len(qs_files)
345
 
346
  listings_ids = listings_ids[:len(docs_files)]
347
 
348
+ for doc_file, qs_file, dupe_file, listings_id in zip(
349
+ docs_files, qs_files, dupes_files, listings_ids
350
  ):
351
+ # load duplicates
352
+ try:
353
+ with open(dupe_file, "rb") as df:
354
+ duplicates = set(pq.read_table(
355
+ df, columns=["doc_id"], use_pandas_metadata=False
356
+ )["doc_id"].to_pylist())
357
+ except Exception as e:
358
+ print(f'failed loading duplicate ids from {dupe_file}.')
359
+ duplicates = set()
360
+
361
  try:
362
  with gzip.open(doc_file, "rt", encoding="utf-8") as df:
363
  with gzip.open(qs_file, "rt", encoding="utf-8") as qf:
364
  for row, (doc, qs) in enumerate(zip(df, qf)):
365
  doc_id = f"{listings_id}.json.gz/{row}"
366
+
367
+ if doc_id in duplicates:
368
+ is_duplicate = True
369
+ else:
370
+ is_duplicate = False
371
+
372
  try:
373
+ yield self.handle_record(
374
+ part, doc_id, doc, qs,
375
+ is_duplicate=is_duplicate
376
+ )
377
  except Exception as e:
378
  print(f'failed handling row {row} in '
379
  f'{doc_file} ({qs_file})')
 
386
  continue
387
 
388
  @staticmethod
389
+ def handle_record(part, doc_id, doc, qs, is_duplicate=None):
390
  doc = json.loads(doc)
391
  qs = json.loads(qs) if qs is not None else {}
392
 
 
399
  "digest": doc["digest"],
400
  }
401
 
402
+ quality_signals = qs.get("quality_signals", {})
403
+ quality_signals["is_duplicate"] = is_duplicate
404
 
405
  return {
406
  "raw_content": doc["raw_content"],
407
  "doc_id": doc_id,
408
  "meta": json.dumps(meta),
409
+ "quality_signals": json.dumps(quality_signals),
410
  }