# mosaicml/streaming architecture

> Architecture diagram of the GitHub repository mosaicml/streaming, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mosaicml/streaming
- Repository: https://github.com/mosaicml/streaming

## Overview

##### Main Components
- Client/Training Applications: Represented by the entry points that users interact with (e.g., PyTorch DataLoader using StreamingDataset).
- Streaming Core Engine: Centered in the “streaming” directory. This module handles:
– Data ingestion and shuffling
– Local caching and disk usage limits
– Data formatting (supporting MDS, CSV/TSV, JSONL, etc.)
– Integration for different data modalities (text, image, video, multimodal)
- Data Conversion and Preparation Tools: Found in examples, scripts, and conversion subfolders (e.g., “scripts/compression”, “streaming/multimodal/convert”, “streaming/text/convert”) that prepare raw data into MDS shards.
- Simulation and Testing: The “simulation” directory contains components for simulating a distributed environment, handling reproducibility (determinism), establishing interfaces for CLI, UI, and testing components.
- Cloud Storage Integration and Remote Data Access: Indicated via the README usage examples – the system streams from remote sources (AWS S3, OCI, GCS, etc.) and caches data locally.
- Utilities and Frameworks: The “scripts”, “tests”, and “docs” folders suggest the presence of various utilities for performance monitoring (hashing, compression, partitioning) as well as rigorous testing and documentation.

##### Relationships and Interactions
- Clients/Training Applications interact with the StreamingDataset (from the streaming module), fetching data which triggers:
– Caching to a local directory, managed by internal logic that deletes least-recently used shards based on a configurable cache limit.
– Data shuffling and mixing (utilizing submodules in “streaming/base/shuffle” and “streaming/base/batching”) that maintain reproducibility.
- Simulation services (in the “simulation” directory) can be represented as optional scaffolding that can simulate multi-node distributed behavior as well as provide CLI or GUI interfaces (modules in “simulation/interfaces”).
- Testing and utilities are separate from core operations but confirm the performance and correctness of the system. They should be depicted as peripheral components fed into the quality assurance of the core system.

##### Architectural Patterns and Design Principles
- Modularity: The separation into directories such as streaming, simulation, scripts, and tests indicates a modular design where each component (e.g., core streaming, simulation interfaces, conversion utilities) is loosely coupled.
- Separation of Concerns: Distinct concerns are split among data ingestion, conversion, streaming/shuffling/data handling, and interfacing with external systems. This supports ease of maintenance and extensibility.
- Distributed / Multi-node Determinism: Emphasize the design principle of absolute ordering across nodes/GPU devices which provides true determinism and instant resumption.
- Extensibility: The design supports many data modalities (image, text, video, multimodal) and cloud providers through the use of configurable modules.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client/Training Applications
    subgraph "Client/Training Applications"
        CTA["PyTorch DataLoader (Client App)"]:::client
        E1["Example: cifar10 notebook"]:::client
        E2["Example: synthetic_nlp notebook"]:::client
    end

    %% Streaming Core Engine
    subgraph "Streaming Core Engine"
        SD["StreamingDataset"]:::core
        DS["Dataset Handler"]:::core
        ST["Stream Engine"]:::core
        SH["Shuffling Module"]:::core
        BT["Batching Module"]:::core
        LM["Local Cache & Disk Management"]:::core
    end

    %% Data Conversion & Preparation Tools
    subgraph "Data Conversion & Preparation Tools"
        DF["DataFrame to MDS Converter"]:::conversion
        TC["Text Conversion (C4)"]:::conversion
        MC["Multimodal Conversion"]:::conversion
        CS["Compression Scripts"]:::conversion
        PS["Partition Scripts"]:::conversion
    end

    %% Simulation & Testing
    subgraph "Simulation & Testing"
        SM["Simulation Core: main"]:::simulation
        SS["Simulation Core: sim_dataset"]:::simulation
        SCI["Simulation Interface CLI"]:::simulation
        SIU["Simulation Interface UI"]:::simulation
        TS["Testing Suite"]:::simulation
    end

    %% Cloud & Remote Storage
    subgraph "Cloud & Remote Storage"
        CSConn["Cloud Storage Connection"]:::cloud
    end

    %% Utilities & Documentation
    subgraph "Utilities & Documentation"
        HU["Hashing Utilities"]:::util
        SU["Shuffle Utilities"]:::util
        DU["Documentation & Guides"]:::util
    end

    %% Data Flow Connections
    E1 -->|"exampleUsage"| CTA
    E2 -->|"exampleUsage"| CTA
    CTA -->|"invokes"| SD
    SD -->|"fetches data"| DS
    DS -->|"processes stream"| ST
    ST -->|"applies shuffling"| SH
    ST -->|"applies batching"| BT
    ST -->|"manages cache"| LM

    %% Conversion to Cloud Storage Flow
    DF -->|"prepares"| CSConn
    TC -->|"prepares"| CSConn
    MC -->|"prepares"| CSConn
    CS -->|"compresses"| CSConn
    PS -->|"partitions"| CSConn
    CSConn -->|"streams data to"| LM

    %% Simulation and Testing Flow
    SM -->|"simulates distribution"| SD
    SS -->|"simulates dataset"| DS
    SCI -->|"CLI_interface"| SM
    SIU -->|"UI_interface"| SM
    TS -->|"validates"| SD

    %% Utilities Connections
    HU -->|"supports caching"| LM
    SU -->|"aids shuffling"| SH
    DU -->|"cloudConfigGuides"| CSConn

    %% Click Events
    click CTA "https://github.com/mosaicml/streaming/blob/main/streaming/base/dataloader.py"
    click E1 "https://github.com/mosaicml/streaming/blob/main/examples/cifar10.ipynb"
    click E2 "https://github.com/mosaicml/streaming/blob/main/examples/synthetic_nlp.ipynb"
    click SD "https://github.com/mosaicml/streaming/blob/main/streaming/base/dataset.py"
    click ST "https://github.com/mosaicml/streaming/blob/main/streaming/base/stream.py"
    click SH "https://github.com/mosaicml/streaming/tree/main/streaming/base/shuffle"
    click BT "https://github.com/mosaicml/streaming/tree/main/streaming/base/batching"
    click DF "https://github.com/mosaicml/streaming/blob/main/streaming/base/converters/dataframe_to_mds.py"
    click TC "https://github.com/mosaicml/streaming/blob/main/streaming/text/convert/c4.py"
    click MC "https://github.com/mosaicml/streaming/blob/main/streaming/multimodal/convert/laion/laion400m/convert_and_upload.py"
    click CS "https://github.com/mosaicml/streaming/tree/main/scripts/compression"
    click PS "https://github.com/mosaicml/streaming/tree/main/scripts/partition"
    click SM "https://github.com/mosaicml/streaming/blob/main/simulation/core/main.py"
    click SS "https://github.com/mosaicml/streaming/blob/main/simulation/core/sim_dataset.py"
    click SCI "https://github.com/mosaicml/streaming/blob/main/simulation/interfaces/sim_cli.py"
    click SIU "https://github.com/mosaicml/streaming/blob/main/simulation/interfaces/sim_ui.py"
    click TS "https://github.com/mosaicml/streaming/tree/main/tests"
    click CSConn "https://github.com/mosaicml/streaming/blob/main/streaming/base/storage/download.py"
    click CSConn "https://github.com/mosaicml/streaming/blob/main/streaming/base/storage/upload.py"
    click HU "https://github.com/mosaicml/streaming/tree/main/scripts/hashing"
    click SU "https://github.com/mosaicml/streaming/tree/main/scripts/shuffle"
    click DU "https://github.com/mosaicml/streaming/tree/main/docs"

    %% Styles
    classDef client fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef core fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef conversion fill:#FFB6C1,stroke:#000,stroke-width:2px;
    classDef simulation fill:#F0E68C,stroke:#000,stroke-width:2px;
    classDef cloud fill:#FFA07A,stroke:#000,stroke-width:2px;
    classDef util fill:#D3D3D3,stroke:#000,stroke-width:2px;
```
