# samkaufman/morello architecture

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

- Interactive diagram: https://gitdiagram.com/samkaufman/morello
- Repository: https://github.com/samkaufman/morello

## Overview

Main Components to Include:

##### Input Layer
- Neural Network Specifications ("Spec")
- Manual Scheduling Interface
- Command-line Interface

##### Core Compiler Components
- Synthesis Engine
- IR (Intermediate Representation) System
- Database System for Caching Optimization Decisions
- Scheduling System
- Code Generation Pipeline

##### Output Layer
- Generated C Implementation
- Performance Metrics
- Verification System

##### Component Organization

- Input sources at the top
- Processing layers in the middle
- Output generation at the bottom

b. Divide the core processing area into three main sections:
- Synthesis & Optimization (Left)
- IR & Scheduling (Middle)
- Code Generation (Right)

##### Key Relationships to Highlight

- Spec → Synthesis Engine → IR System
- Manual Scheduling → IR System
- IR System → Code Generation
- Optimization Database ↔ Synthesis Engine (bidirectional)

##### Specific Implementation Details

a. Target-specific components (based on /src/target/):
- x86 Backend
- ARM Backend
- Common CPU Operations

b. Code Generation Pipeline (based on /src/codegen/):
- C Code Generation
- Platform-specific Optimizations
- Header Generation

##### Additional Elements

- A feedback loop showing how the verification system validates the output
- Relationship between the synthesis database and the optimization process
- Where performance benchmarking occurs (based on /morello/benches/)

##### Specialized Features

- Memory management (/src/memorylimits.rs)
- Tensor operations (/src/tensorspec.rs)
- Tiling optimization (/src/tiling.rs)
- Pipeline scheduling (/src/scheduling.rs)

##### Performance Optimization
- Caching mechanism using the database
- How optimization decisions are stored and reused

##### Extensibility
- Extension points for new targets
- Plugin architecture for different backends

## Diagram (Mermaid)

```mermaid
graph TB
    %% Input Layer
    subgraph Input
        NNSpec["Neural Network Specifications"]:::input
        ManSched["Manual Scheduling Interface"]:::input
        CLI["Command-line Interface"]:::input
    end

    %% Core Processing Layer
    subgraph Processing
        %% Synthesis & Optimization
        subgraph SynthOpt
            SynthEng["Synthesis Engine"]:::process
            OptDB[(Optimization Database)]:::storage
            TileOpt["Tiling Optimization"]:::process
            MemMgmt["Memory Management"]:::process
        end

        %% IR & Scheduling
        subgraph IRScheduling
            IR["IR System"]:::process
            SchedSys["Scheduling System"]:::process
            TensorOps["Tensor Operations"]:::process
        end

        %% Code Generation
        subgraph CodeGen
            CGPipe["Code Generation Pipeline"]:::process
            subgraph Targets
                X86["x86 Backend"]:::target
                ARM["ARM Backend"]:::target
                CPU["Common CPU Operations"]:::target
            end
            ImpSys["Implementation System"]:::process
        end
    end

    %% Output Layer
    subgraph Output
        CImpl["Generated C Implementation"]:::output
        PerfMet["Performance Metrics"]:::output
        Verify["Verification System"]:::output
        Bench["Performance Benchmarking"]:::output
    end

    %% Relationships
    NNSpec --> SynthEng
    ManSched --> SchedSys
    CLI --> SynthEng
    SynthEng <--> OptDB
    SynthEng --> IR
    IR --> SchedSys
    SchedSys --> CGPipe
    TileOpt --> CGPipe
    MemMgmt --> CGPipe
    TensorOps --> IR
    CGPipe --> X86 & ARM
    X86 & ARM --> ImpSys
    CPU --> ImpSys
    ImpSys --> CImpl
    CImpl --> Verify
    Verify --> PerfMet
    PerfMet --> Bench

    %% Styles
    classDef input fill:#a8d1f0
    classDef process fill:#90EE90
    classDef storage fill:#FFE4B5
    classDef output fill:#FFA07A
    classDef target fill:#DDA0DD

    %% Click Events
    click NNSpec "https://github.com/samkaufman/morello/blob/main/morello/src/spec.rs"
    click SchedSys "https://github.com/samkaufman/morello/blob/main/morello/src/scheduling.rs"
    click OptDB "https://github.com/samkaufman/morello/blob/main/morello/src/db/mod.rs"
    click CGPipe "https://github.com/samkaufman/morello/blob/main/morello/src/codegen/mod.rs"
    click X86 "https://github.com/samkaufman/morello/blob/main/morello/src/target/x86.rs"
    click ARM "https://github.com/samkaufman/morello/blob/main/morello/src/target/arm.rs"
    click CPU "https://github.com/samkaufman/morello/blob/main/morello/src/target/common_actions.rs"
    click MemMgmt "https://github.com/samkaufman/morello/blob/main/morello/src/memorylimits.rs"
    click TensorOps "https://github.com/samkaufman/morello/blob/main/morello/src/tensorspec.rs"
    click Verify "https://github.com/samkaufman/morello/blob/main/morello/src/verification.rs"
    click Bench "https://github.com/samkaufman/morello/blob/main/morello/benches/synth.rs"
    click ImpSys "https://github.com/samkaufman/morello/blob/main/morello/src/imp/kernels.rs"
    click TileOpt "https://github.com/samkaufman/morello/blob/main/morello/src/tiling.rs"
```
