# goldstraw/rust_transformer architecture

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

- Interactive diagram: https://gitdiagram.com/goldstraw/rust_transformer
- Repository: https://github.com/goldstraw/rust_transformer

## Overview

##### Main Components
- Data Ingestion & Preprocessing: Represent the dataset components (imdb_dataset.csv and word_embeddings.json). This includes the modules responsible for loading the IMDb reviews and the pre-trained word embeddings.
- Embedding Layer (src/embedding.rs): Transforms input tokens into vector representations.
- Positional Encoding (src/positional_encoder.rs): Adds sequence information to embeddings.
- Self-Attention Mechanism (src/self_attention.rs and src/multi_headed_attention.rs): Computes attention scores.
- Encoder Block(s) (src/encoder_block.rs, src/block.rs): Composite block that contains the self-attention and subsequent processing (e.g., normalization, add-and-norm from src/add_and_norm.rs).
- Feedforward or Dense Layers (src/dense.rs): Applies further transformation.
- Transformer Integration (src/transformer.rs): Orchestrates the overall transformation process.
- Execution Flow: Show how the main entry point (src/main.rs) kicks off the overall process, with orchestration possibly extended in src/run.rs. Additionally, include the logger (src/logger.rs) that logs key events during training/testing.
- Dataset Handling: Incorporate the dataset processing logic (src/dataset.rs) which likely prepares and splits the movie review data for training and testing.

##### Relationships and Interactions
- Embedding Layer to the Positional Encoder, indicating sequential processing.
- Link the results from the encoder stacks back into an output layer or post-processing module that provides the sentiment classification output.

##### Architectural Patterns and Design Principles
- Modular Design: Emphasize that each component (attention, normalization, encoding, dense layers) is implemented in separate files, promoting separation of concerns.
- Layered Processing: The pipeline follows a sequential layered pattern where data flows from the input through embeddings, positional encoding, attention mechanisms, and finally the dense layers.
- Reusable Components: The transformer blocks are abstracted in dedicated modules, which can be represented as reusable and scalable building blocks.

##### Relevant Technologies/Frameworks
- Rust & Cargo: At the foundation, represent the Rust ecosystem enforcing performance and memory-safety.
- Custom Implementation: Note that the design avoids external machine learning libraries, underlining a ground-up implementation.
- File Dependencies: Mention that configuration files like Cargo.toml and Cargo.lock manage package dependencies, though they are not part of the runtime data flow.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Data Ingestion & Preprocessing
    subgraph "Data Ingestion & Preprocessing"
        DS["IMDb Dataset"]:::data
        WE["Word Embeddings"]:::data
        DH["Dataset Handling"]:::module
    end

    %% Transformer Engine
    subgraph "Transformer Engine"
        EM["Embedding Module"]:::module
        PE["Positional Encoder"]:::module
        SA["Self-Attention"]:::module
        MHA["Multi-Head Attention"]:::module
        subgraph "Encoder Block"
            ECB["Encoder Core"]:::module
            BE["Block & Norm"]:::module
        end
        DF["Dense/Feed-Forward"]:::module
        TI["Transformer Integration"]:::module
    end

    %% Execution Flow
    subgraph "Execution Flow"
        ME["Main Entry"]:::execution
        RP["Run Process"]:::execution
        LG["Logger"]:::support
    end

    %% Project Configuration
    subgraph "Project Configuration"
        CT["Cargo.toml"]:::support
        CL["Cargo.lock"]:::support
    end

    %% Data Flow Connections
    DS --> DH
    WE --> DH
    DH --> EM
    EM --> PE
    PE --> SA
    SA --> MHA
    MHA --> ECB
    ECB --> BE
    BE --> DF
    DF --> TI
    ME --> DH
    TI --> RP
    RP --> LG

    %% Styles
    classDef data fill:#ffebcd,stroke:#333,stroke-width:2px;
    classDef module fill:#add8e6,stroke:#333,stroke-width:2px;
    classDef execution fill:#90ee90,stroke:#333,stroke-width:2px;
    classDef support fill:#fffacd,stroke:#333,stroke-width:2px;

    %% Click Events
    click DS "https://github.com/goldstraw/rust_transformer/blob/main/imdb_dataset.csv"
    click WE "https://github.com/goldstraw/rust_transformer/blob/main/word_embeddings.json"
    click DH "https://github.com/goldstraw/rust_transformer/blob/main/src/dataset.rs"
    click EM "https://github.com/goldstraw/rust_transformer/blob/main/src/embedding.rs"
    click PE "https://github.com/goldstraw/rust_transformer/blob/main/src/positional_encoder.rs"
    click SA "https://github.com/goldstraw/rust_transformer/blob/main/src/self_attention.rs"
    click MHA "https://github.com/goldstraw/rust_transformer/blob/main/src/multi_headed_attention.rs"
    click ECB "https://github.com/goldstraw/rust_transformer/blob/main/src/encoder_block.rs"
    click BE "https://github.com/goldstraw/rust_transformer/blob/main/src/add_and_norm.rs"
    click DF "https://github.com/goldstraw/rust_transformer/blob/main/src/dense.rs"
    click TI "https://github.com/goldstraw/rust_transformer/blob/main/src/transformer.rs"
    click ME "https://github.com/goldstraw/rust_transformer/blob/main/src/main.rs"
    click RP "https://github.com/goldstraw/rust_transformer/blob/main/src/run.rs"
    click LG "https://github.com/goldstraw/rust_transformer/blob/main/src/logger.rs"
    click CT "https://github.com/goldstraw/rust_transformer/blob/main/Cargo.toml"
    click CL "https://github.com/goldstraw/rust_transformer/blob/main/Cargo.lock"
```
