-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement zstd Compression Support for JSONL and Parquet Files #230
Merged
guipenedo
merged 11 commits into
huggingface:main
from
justHungryMan:feature/zstd_compression
Aug 28, 2024
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f5911ee
Add zstandard dependency for compression support
justHungryMan c416ca9
feat: Add zstd compression support for jsonl reader
justHungryMan 5a4697f
feat: Add zstd compression support for ParquetWriter
justHungryMan 5998f0f
feat: Update DiskWriter to handle the other compression for Parquet f…
justHungryMan f69e768
Remove annotaion
justHungryMan 749291f
feat: Update compression handling in DiskWriter and ParquetWriter
justHungryMan b0c1e24
Update src/datatrove/pipeline/writers/disk_base.py
justHungryMan becf062
Update src/datatrove/pipeline/writers/parquet.py
justHungryMan deae3bc
Refactor constructor to explicitly set default compression to None
justHungryMan 40deea9
Add validation for compression parameter in ParquetWriter
justHungryMan 1ed3869
Update src/datatrove/pipeline/writers/disk_base.py
justHungryMan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
from collections import Counter, defaultdict | ||
from typing import IO, Callable | ||
from typing import IO, Callable, Literal | ||
|
||
from datatrove.io import DataFolderLike | ||
from datatrove.pipeline.writers.disk_base import DiskWriter | ||
|
@@ -14,24 +14,31 @@ def __init__( | |
self, | ||
output_folder: DataFolderLike, | ||
output_filename: str = None, | ||
compression: str | None = None, | ||
compression: Literal["snappy", "gzip", "brotli", "lz4", "zstd"] | None = None, | ||
adapter: Callable = None, | ||
batch_size: int = 1000, | ||
expand_metadata: bool = False, | ||
max_file_size: int = 5 * 2**30, # 5GB | ||
): | ||
# Validate the compression setting | ||
if compression not in {"snappy", "gzip", "brotli", "lz4", "zstd", None}: | ||
raise ValueError( | ||
"Invalid compression type. Allowed types are 'snappy', 'gzip', 'brotli', 'lz4', 'zstd', or None." | ||
) | ||
|
||
super().__init__( | ||
output_folder, | ||
output_filename, | ||
compression, | ||
adapter, | ||
compression=None, # Ensure superclass initializes without compression | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Set superclass compression parameter to None to leverage pyarrow's native compression functions. |
||
adapter=adapter, | ||
mode="wb", | ||
expand_metadata=expand_metadata, | ||
max_file_size=max_file_size, | ||
) | ||
self._writers = {} | ||
self._batches = defaultdict(list) | ||
self._file_counter = Counter() | ||
self.compression = compression | ||
justHungryMan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.batch_size = batch_size | ||
|
||
def _on_file_switch(self, original_name, old_filename, new_filename): | ||
|
@@ -62,7 +69,9 @@ def _write(self, document: dict, file_handler: IO, filename: str): | |
|
||
if filename not in self._writers: | ||
self._writers[filename] = pq.ParquetWriter( | ||
file_handler, schema=pa.RecordBatch.from_pylist([document]).schema | ||
file_handler, | ||
schema=pa.RecordBatch.from_pylist([document]).schema, | ||
compression=self.compression, | ||
) | ||
self._batches[filename].append(document) | ||
if len(self._batches[filename]) == self.batch_size: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import shutil | ||
import tempfile | ||
import unittest | ||
|
||
from datatrove.data import Document | ||
from datatrove.pipeline.readers.jsonl import JsonlReader | ||
from datatrove.pipeline.writers.jsonl import JsonlWriter | ||
|
||
|
||
class TestZstdCompression(unittest.TestCase): | ||
def setUp(self): | ||
# Create a temporary directory | ||
self.tmp_dir = tempfile.mkdtemp() | ||
self.addCleanup(shutil.rmtree, self.tmp_dir) | ||
|
||
def test_jsonl_writer_reader(self): | ||
data = [ | ||
Document(text=text, id=str(i), metadata={"somedata": 2 * i, "somefloat": i * 0.4, "somestring": "hello"}) | ||
for i, text in enumerate(["hello", "text2", "more text"]) | ||
] | ||
with JsonlWriter(output_folder=self.tmp_dir, compression="zstd") as w: | ||
for doc in data: | ||
w.write(doc) | ||
reader = JsonlReader(self.tmp_dir, compression="zstd") | ||
c = 0 | ||
for read_doc, original in zip(reader(), data): | ||
read_doc.metadata.pop("file_path", None) | ||
assert read_doc == original | ||
c += 1 | ||
assert c == len(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import shutil | ||
import tempfile | ||
import unittest | ||
|
||
from datatrove.data import Document | ||
from datatrove.pipeline.readers.parquet import ParquetReader | ||
from datatrove.pipeline.writers.parquet import ParquetWriter | ||
|
||
|
||
class TestZstdCompression(unittest.TestCase): | ||
def setUp(self): | ||
# Create a temporary directory | ||
self.tmp_dir = tempfile.mkdtemp() | ||
self.addCleanup(shutil.rmtree, self.tmp_dir) | ||
|
||
def test_parquet_writer_reader(self): | ||
data = [ | ||
Document(text=text, id=str(i), metadata={"somedata": 2 * i, "somefloat": i * 0.4, "somestring": "hello"}) | ||
for i, text in enumerate(["hello", "text2", "more text"]) | ||
] | ||
with ParquetWriter(output_folder=self.tmp_dir, compression="zstd") as w: | ||
for doc in data: | ||
w.write(doc) | ||
reader = ParquetReader(self.tmp_dir) | ||
c = 0 | ||
for read_doc, original in zip(reader(), data): | ||
read_doc.metadata.pop("file_path", None) | ||
assert read_doc == original | ||
c += 1 | ||
assert c == len(data) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add validation check for supported protocols