Paradigm Shift¶
Concurrency Structure¶
When adopting SPDL, it is important to understand the difference in how SPDL structure the concurrency, compared against conventional data loaders.
In conventional data loading, each worker process runs the entire pipeline. The pipeline is implemented as DataSet. Each process applies all the steps to one sample at a time, then collates the samples into a batch. So at most one sample per process is in progress, and every step gets the same concurrency, which is the number of processes.
Whereas SPDL parallelizes the pipeline stage-by-stage, using different concurrency. Each stage applies one operation to one sample at a time per task, and the samples are aggregated into a batch at the end. The concurrency of each stage is configured independently, so an expensive stage can be given more concurrency than a cheap one. This approach is better fit for achieving higher throughput.
It is worth noting that in this setup, there is no equivalent of Dataset class.
This paradigm shift makes it difficult to achieve mechanical migration (such as one-line-change or swap-the-class type of migration) to SPDL. Coding agents, however, can now carry out such a migration well. With the migration guidance in the Knowledge Base, an agent reuses the functions and classes of the original implementation, and checks the data and performance parity before optimizing. See Working with Agents.
Thread Safety and the GIL¶
In conventional data loading, each worker process runs its own copy of the pipeline, so the functions in the pipeline do not run concurrently within a process. In SPDL, the stage functions run concurrently in a thread pool by default. When they do, this has two consequences for migration.
Functions must be thread-safe. An object that is not thread-safe needs one instance per thread. For example, HuggingFace’s Tokenizers releases the GIL but is not thread-safe (see this issue). Thread local storage is a simple way to create one instance per thread.
Throughput depends on functions releasing the GIL. A function that holds the GIL blocks the other stages while it runs. Mixing such functions with those that release the GIL can severely degrade performance. See Working Around the GIL for which operations release the GIL, and what to do when a function does not.
Both apply only to the stages that run on threads. A stage whose functions are not thread-safe or hold the GIL can run in a process pool instead, and the whole pipeline can run in subprocesses. See Execution Models: MT, MTP, and MP for how to choose and mix them.