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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""IcyDolma dataset based on Common Crawl."""
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
import gzip
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
import datasets
|
| 8 |
+
import more_itertools
|
| 9 |
+
import numpy as np
|
| 10 |
+
import io
|
| 11 |
+
import json
|
| 12 |
+
import dataclasses
|
| 13 |
+
import pyzstd
|
| 14 |
+
from tensorflow.io import gfile
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
logger = datasets.logging.get_logger(__name__)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
_DESCRIPTION = """\
|
| 21 |
+
"""
|
| 22 |
+
|
| 23 |
+
_CITATION = """
|
| 24 |
+
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
_VARIANTS = ["v3"]
|
| 29 |
+
|
| 30 |
+
_DATA_URL = "gs://meliad2_us2/datasets/dolma/clustered_v3/train/shard_{}/subshard_{}/phase{}.jsonl"
|
| 31 |
+
|
| 32 |
+
@dataclasses.dataclass
|
| 33 |
+
class State:
|
| 34 |
+
file_index: int
|
| 35 |
+
file_position: int
|
| 36 |
+
|
| 37 |
+
def to_int(self) -> int:
|
| 38 |
+
return (self.file_position << 17) | self.file_index
|
| 39 |
+
|
| 40 |
+
@classmethod
|
| 41 |
+
def from_int(cls, value: int):
|
| 42 |
+
file_position = value >> 17
|
| 43 |
+
file_index = value & ((1 << 17) - 1)
|
| 44 |
+
return cls(file_index, file_position)
|
| 45 |
+
|
| 46 |
+
class ZstdReader:
|
| 47 |
+
def __init__(self,
|
| 48 |
+
filepaths,
|
| 49 |
+
worker_id,
|
| 50 |
+
num_workers,
|
| 51 |
+
file_loc=0,
|
| 52 |
+
buffer_size=65536,
|
| 53 |
+
deserialize_func=None,
|
| 54 |
+
):
|
| 55 |
+
self.filepaths = filepaths
|
| 56 |
+
n_shards = len(self.filepaths)
|
| 57 |
+
if n_shards == 0:
|
| 58 |
+
raise ValueError("No shards found")
|
| 59 |
+
|
| 60 |
+
logger.info(f"Found {len(self.filepaths)} files")
|
| 61 |
+
self.buffer_size = buffer_size
|
| 62 |
+
self.state = State.from_int(file_loc)
|
| 63 |
+
self.worker_id = worker_id
|
| 64 |
+
self.num_workers = num_workers
|
| 65 |
+
assert worker_id < num_workers, "worker_id must be less than num_workers"
|
| 66 |
+
if n_shards < num_workers:
|
| 67 |
+
self.workers_per_shard = num_workers // n_shards
|
| 68 |
+
self.filepaths = [self.filepaths[worker_id % n_shards]]
|
| 69 |
+
self.internal_worker_id = int(worker_id // n_shards)
|
| 70 |
+
else:
|
| 71 |
+
self.workers_per_shard = None
|
| 72 |
+
self.filepaths = list(more_itertools.distribute(num_workers, self.filepaths)[worker_id])
|
| 73 |
+
logger.info(f"Using {len(self.filepaths)} files")
|
| 74 |
+
|
| 75 |
+
if deserialize_func is None:
|
| 76 |
+
self.deserialize_func = deserialize
|
| 77 |
+
else:
|
| 78 |
+
self.deserialize_func = deserialize_func
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def __iter__(self):
|
| 83 |
+
"""This function returns the examples in the raw (text) form by iterating on all the files."""
|
| 84 |
+
while self.state.file_index<len(self.filepaths):
|
| 85 |
+
filepath = self.filepaths[self.state.file_index]
|
| 86 |
+
with gfile.GFile(filepath, 'rb') as f:
|
| 87 |
+
with pyzstd.ZstdFile(f, 'rb') as ifo:
|
| 88 |
+
raw_reader = MultiBytesIOReader(ifo,
|
| 89 |
+
buffer_size=self.buffer_size,
|
| 90 |
+
file_position=self.state.file_position)
|
| 91 |
+
if self.workers_per_shard is not None:
|
| 92 |
+
reader = more_itertools.distribute(self.workers_per_shard, raw_reader)[self.internal_worker_id]
|
| 93 |
+
else:
|
| 94 |
+
reader = raw_reader
|
| 95 |
+
for example in reader:
|
| 96 |
+
self.state.file_position = raw_reader.tell()
|
| 97 |
+
example = self.deserialize_func(example)
|
| 98 |
+
example["file_loc"] = self.state.to_int()
|
| 99 |
+
yield example
|
| 100 |
+
self.state.file_position = 0
|
| 101 |
+
self.state.file_index += 1
|
| 102 |
+
self.state = State.from_int(0)
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
def deserialize(data_point):
|
| 108 |
+
return json.loads(data_point.decode('utf-8'))
|
| 109 |
+
|
| 110 |
+
class MultiBytesIOReader:
|
| 111 |
+
def __init__(self,
|
| 112 |
+
decompressedStream: io.BytesIO,
|
| 113 |
+
buffer_size=65536,
|
| 114 |
+
file_position=0,
|
| 115 |
+
):
|
| 116 |
+
self.decompressedStream = decompressedStream
|
| 117 |
+
self.buffer_size = buffer_size
|
| 118 |
+
self.incomplete_line = bytearray()
|
| 119 |
+
self.position = file_position
|
| 120 |
+
|
| 121 |
+
def seek(self, position):
|
| 122 |
+
self.position = position
|
| 123 |
+
|
| 124 |
+
def tell(self):
|
| 125 |
+
return self.position
|
| 126 |
+
|
| 127 |
+
def __iter__(self):
|
| 128 |
+
self.decompressedStream.seek(self.position)
|
| 129 |
+
while True:
|
| 130 |
+
buffer = self.decompressedStream.read(self.buffer_size)
|
| 131 |
+
if not buffer:
|
| 132 |
+
break
|
| 133 |
+
buffer = self.incomplete_line + buffer
|
| 134 |
+
self.incomplete_line = bytearray()
|
| 135 |
+
lines = buffer.split(b'\n')
|
| 136 |
+
if lines and lines[-1]:
|
| 137 |
+
self.incomplete_line = lines.pop()
|
| 138 |
+
for line in lines:
|
| 139 |
+
if line:
|
| 140 |
+
self.position += len(line)+1
|
| 141 |
+
yield line
|
| 142 |
+
if self.incomplete_line:
|
| 143 |
+
self.position += len(self.incomplete_line)
|
| 144 |
+
yield self.incomplete_line
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
class IcyDolmaConfig(datasets.BuilderConfig):
|
| 148 |
+
"""BuilderConfig for IcyDolma."""
|
| 149 |
+
|
| 150 |
+
def __init__(self,
|
| 151 |
+
name="v3",
|
| 152 |
+
worker_id=None,
|
| 153 |
+
n_workers=None,
|
| 154 |
+
filepaths=None,
|
| 155 |
+
file_loc=None,
|
| 156 |
+
buffer_size=65536,
|
| 157 |
+
**kwargs):
|
| 158 |
+
"""BuilderConfig for IcyDolma.
|
| 159 |
+
Args:
|
| 160 |
+
**kwargs: keyword arguments forwarded to super.
|
| 161 |
+
"""
|
| 162 |
+
super(IcyDolmaConfig, self).__init__(name=name,**kwargs)
|
| 163 |
+
self.worker_id = worker_id
|
| 164 |
+
self.n_workers = n_workers
|
| 165 |
+
self.filepaths = filepaths
|
| 166 |
+
self.file_loc = file_loc
|
| 167 |
+
self.buffer_size = buffer_size
|
| 168 |
+
|
| 169 |
+
class IcyDolma(datasets.GeneratorBasedBuilder):
|
| 170 |
+
BUILDER_CONFIGS = [IcyDolmaConfig(name) for name in _VARIANTS]
|
| 171 |
+
|
| 172 |
+
def _info(self):
|
| 173 |
+
self.worker_id = self.config.worker_id
|
| 174 |
+
self.n_workers = self.config.n_workers
|
| 175 |
+
self.filepaths = self.config.filepaths
|
| 176 |
+
self.buffer_size = self.config.buffer_size
|
| 177 |
+
if self.config.filepaths is None:
|
| 178 |
+
self.filepaths = [_DATA_URL.format(i,j,k) for i in range(64) for j in range(64) for k in range(2)]
|
| 179 |
+
else:
|
| 180 |
+
self.filepaths = self.config.filepaths
|
| 181 |
+
if self.config.file_loc is not None:
|
| 182 |
+
self.file_loc = self.config.file_loc
|
| 183 |
+
else:
|
| 184 |
+
self.file_loc = 0
|
| 185 |
+
|
| 186 |
+
return datasets.DatasetInfo(
|
| 187 |
+
description=_DESCRIPTION,
|
| 188 |
+
features=datasets.Features(
|
| 189 |
+
{
|
| 190 |
+
"text": datasets.Value("string"),
|
| 191 |
+
"source": datasets.Value("string"),
|
| 192 |
+
"url": datasets.Value("string"),
|
| 193 |
+
"id": datasets.Value("int32"),
|
| 194 |
+
"file_loc": datasets.Value("int64"),
|
| 195 |
+
}
|
| 196 |
+
),
|
| 197 |
+
supervised_keys=None,
|
| 198 |
+
citation=_CITATION,
|
| 199 |
+
)
|
| 200 |
+
|
| 201 |
+
def _split_generators(self, _):
|
| 202 |
+
return [
|
| 203 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepaths": self.filepaths}),
|
| 204 |
+
]
|
| 205 |
+
|
| 206 |
+
def _generate_examples(self, filepaths):
|
| 207 |
+
"""This function returns the examples in the raw (text) form by iterating on all the files."""
|
| 208 |
+
id_ = 0
|
| 209 |
+
dataset = ZstdReader(filepaths=list(filepaths),
|
| 210 |
+
worker_id=self.worker_id if self.worker_id is not None else 0,
|
| 211 |
+
num_workers=self.n_workers if self.n_workers is not None else 1,
|
| 212 |
+
file_loc=self.file_loc,
|
| 213 |
+
buffer_size=self.buffer_size,
|
| 214 |
+
)
|
| 215 |
+
for example in dataset:
|
| 216 |
+
url = example["id"]
|
| 217 |
+
example["id"] = id_
|
| 218 |
+
example["url"] = url
|
| 219 |
+
id_ += 1
|
| 220 |
+
yield id_, example
|