.. _analysis: Analyzing the Performance ========================= .. currentmodule:: spdl.pipeline In this section, we examine the performance statistics gathered from a production training system. .. note:: To setup the pipeline to collect runtime performance statistics, please refer to the :ref:`logging` section and the :py:mod:`performance_analysis` example. The pipeline consists of four stages: download, preprocess, batch, and transfer. The following code snippet and diagram illustrate this. .. code-block:: python pipeline = ( PipelineBuilder() .add_source(Dataset()) .aggregate(8) .pipe(download, concurrency=12) .disaggregate() .pipe(preprocess, concurrency=12) .aggregate(4) .pipe(collate) .pipe(gpu_transfer) .add_sink() .build(num_threads=12) ) .. image:: ../_static/data/queue_analysis_pipeline.png :width: 100% :alt: The pipeline of the example: the source, an aggregation into batches of 8, download with 12 tasks, disaggregation, preprocessing with 12 tasks, an aggregation into batches of 4, collation, GPU transfer and the sink. .. _compare-loader-implementations: When a replacement loader is slower ----------------------------------- Read the original and replacement implementations side by side before changing tuning knobs. Trace the same samples from source lookup to the batch consumed by the trainer, including defaults and work hidden behind wrappers. Compare: * **What work runs.** Check sampling, decoding, transforms, batching and transfers. Look for work the original skips, reuses or performs once per batch, but the replacement repeats per sample or epoch. * **Where work runs.** Check what stays inside each worker, what crosses a thread or process boundary, and where the loader waits, copies or turns compact buffers into Python objects. For example, the original may pass compact indices into a worker and return a finished batch, while the replacement passes expanded rows back before assembling the batch. * **What resources it uses.** Check worker and native thread pools, cache placement and reuse, prefetch limits, and the amount of data held in flight. Identical worker counts or a full ready queue do not establish equivalent work or cost. Use timings or traces to test which differences explain the slowdown. Close one confirmed gap at a time, reusing original components where possible. Verify that the replacement produces equivalent batches and matches end-to-end throughput at comparable resource cost. Compare the same data, model, hardware and measurement windows, accounting for all workers and checking cold and warm behavior separately. To compare resource cost per process and per thread (CPU, kernel time, page faults and memory), see :ref:`thread-resource-usage`. Once the replacement reaches parity, use it as the baseline for further optimization. .. _compact-loader-metadata: Keep loader metadata in compact representations ----------------------------------------------- Avoid JSON as the default format for large or frequently read dataset metadata. Keep it for small configuration or interchange files where human readability matters. In the loader's hot path, a faster JSON parser can save time scanning bytes while the conversion into Python dictionaries, lists and numbers still dominates. For tabular metadata, prefer a typed columnar representation such as Arrow or Parquet. For fixed-width numeric records, consider a format such as NumPy's ``.npy``. Keep values in those representations through the consumer instead of immediately expanding them back into Python objects. When a numeric list contains more than a few hundred values of the same type, use a NumPy array or PyTorch tensor with an explicit dtype instead of a Python list. This is a rule of thumb, not a cutoff: a typed buffer stores the values together so the interpreter does not have to manage a separate Python object or list slot for each value. Preserve shape, precision and missing-value semantics, and measure reading, conversion and consumption together. TaskPerfStats ------------- The :py:class:`TaskPerfStats` class contains information about the stage functions. The statistics are aggregated (averaged or summed) over the (possibly concurrent) invocations within the measurement window. Average Time ~~~~~~~~~~~~ The :py:attr:`TaskPerfStats.ave_time` represents the average time of successful function invocations. Typically, download time is the largest, followed by decoding/preprocessing, collation, and transfer. When decoding is lightweight but the data size is large (such as processing long audio in WAV format), collation and transfer can take longer than preprocessing. In the following, we can see that the duration for downloading data changes over the course of training. We also see occasional spikes. The performance of downloading is affected by both external factors (i.e. the state of networking) and internal factors (such as the size of data requested). As we will see later, the speed of download is fast enough for this case, so the overall change is not an issue here. Other stages show the consistent performance throughout the training. .. raw:: html
The number of tasks executed ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The :py:attr:`TaskPerfStats.num_tasks` represents the number of function invocation completed, including both successful and failure completion. You can obtain the number of successful task counts, by subtracting :py:attr:`TaskPerfStats.num_failures` from ``TaskPerfStats.num_tasks``. In this case, there was no failure during the training, so we omit the plot for that. .. raw:: html QueuePerfStats -------------- When a pipeline is built, queues are inserted between each stage. The outputs from stage functions are put in their corresponding queues. The inputs to the stage functions are retrieved from queues upstream to them. The following animation illustrates this. Using queues makes it easy to generalize the architecture of the pipeline, as it allows for the independent orchestration of stage functions. .. image:: ../_static/data/queue_overview.gif :width: 100% :alt: Animation of a pipeline with a source, Stage 1 with three tasks, Stage 2 with two tasks and the foreground loop, connected by queues. Stage 2 is the slowest, so the queue before it stays full and Stage 1 tasks wait to put their results, while the queue after it is empty and the foreground waits to get the next item. In this example, Stage 2 is the slowest stage. Queues that have the next item available are shown in green, and empty queues in red. The queue before it stays full, so Stage 1 tasks finish early and wait to put their results ("put wait"). The queue after it is mostly empty, so the foreground waits to get the next item ("get wait"). The statistics below measure these conditions. .. note:: - In the following plots, a legned ``