Add cross-platform execution backend (Windows/macOS support)#1256
Open
the-shadow-0 wants to merge 1 commit intogoogle:mainfrom
Open
Add cross-platform execution backend (Windows/macOS support)#1256the-shadow-0 wants to merge 1 commit intogoogle:mainfrom
the-shadow-0 wants to merge 1 commit intogoogle:mainfrom
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
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.
Hi! 👋
This PR adds support for running Grain on Windows and macOS, while keeping the current multiprocessing behavior on Linux unchanged.
The goal was to make the library usable across platforms without introducing regressions or major refactors. The approach is minimal and focuses on correctness, determinism, safe shutdown behavior, and developer transparency.
What’s included
Execution backend abstraction
Introduced an
ExecutionBackendlayer to decouple concurrency logic from dataset iteration and data loading:fork/spawnas configured)queue.Queueandthreading.Thread)This design ensures platform-specific optimizations without modifying the core data pipeline logic.
Cross-platform support
fork)Deterministic behavior
Safer shutdown behavior
.get(timeout=...)andshould_stop.is_set().Environment configuration
Grain behavior can be controlled via environment variables:
Example usage:
Invalid values will raise a
ValueErrorimmediately to prevent silent fallback issues.Pickling behavior
GRAIN_STRICT_PICKLING=1) ensures consistency across platforms during testing.Shared memory handling
Usage examples
On Linux, this will use multiprocessing with shared memory.
On Windows/macOS, this will use threading.
Overriding the backend:
Internal architecture overview
ExecutionBackend abstraction
Process,Queue,Event, andSynchronizedIntare mapped to either native multiprocessing or threading primitives.DataLoader iterator
ExecutionBackend.Processand queues to fetch dataset elements.Checkpointing
get_state()andset_state()remain consistent.Testing
Performance
Known limitations
forkmay cause deadlocks in libraries like CUDA/JAX;spawncan be forced via environment variable.Notes for maintainers
execution_backend.py.ExecutionBackendfor other concurrency mechanisms (e.g., Ray, Dask).Thank you!
This PR aims to make Grain usable and deterministic on all major OS platforms, without sacrificing Linux performance or existing workflows.
Happy to iterate if maintainers have feedback! 🙏
📚 Documentation preview 📚: https://google-grain--1256.org.readthedocs.build/