Add BagzDataSource for BagZ file format support - #1376
Open
Marlon666 wants to merge 1 commit into
Open
Conversation
Add `BagzDataSource`, a first-class `RandomAccessDataSource` for reading BagZ (.bagz) files in PyGrain. - Supports single path or list/sequence of paths (normalizing into comma-separated strings). - Implements lazy initialization (`self.reader` property) and custom `__getstate__`/`__setstate__` to reset open C++ handles across pickle/multiprocessing worker boundaries (fork-safe). - Integrates with Grain's API initialization counter (`_api_usage_counter`) and reading stage metrics (`@dataset_stats.trace_input_pipeline`). - Adds comprehensive unit tests (`BagzDataSourceTest`) covering random access, empty sequence validation, and pickle serialization.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Overview
This PR introduces
BagzDataSource, a first-classRandomAccessDataSourcein PyGrain for reading BagZ (.bagz) datasets.While native C++
bagz.Readercan be used directly in simple single-process scripts, integrating BagZ into distributed multiprocessing training pipelines requires additional handling for pickling, fork-safety, and path list normalization.BagzDataSourceprovides an official, zero-overhead bridge to address these requirements.Key Features & Motivation
In distributed multi-worker pipelines (
grain_worker_count > 0orElasticIterator), PyGrain serializes (pickle) the data source across worker processes. Because open C++bagz.Readerhandles cannot be pickled (TypeError: cannot pickle 'bagz.lib.bagz.Reader' object) and sharing open file descriptors across forked workers is unsafe on POSIX / GCS FUSE mounts,BagzDataSourceimplements custom__getstate__/__setstate__to reset reader handles and lazily instantiatingbagz.Readeron demand inside each worker.Supports passing a single path string/FileInstruction as well as a list/sequence of paths (transparently normalizing sequences into comma-separated strings required by
bagz.Reader).Integrates with Grain's API initialization counter (
_api_usage_counter) and read-stage monitoring (@dataset_stats.trace_input_pipeline).Testing
grain/_src/python/data_sources_test.py(BagzDataSourceTest), covering:RandomAccessDataSourcedata_sources_test.pypass cleanly.📚 Documentation preview 📚: https://google-grain--1376.org.readthedocs.build/