# zk4x/zyx architecture

> Architecture diagram of the GitHub repository zk4x/zyx, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/zk4x/zyx
- Repository: https://github.com/zk4x/zyx

## Overview

Zyx is a cross-platform machine-learning tensor library. Users compose tensor operations eagerly or record them on a Tape; the tape supports differentiation and deferred realization, while the core turns work into kernels that are optimized and dispatched to hardware backends. Neural-network layers and optimizers support training, and the repository also includes Python bindings and an ONNX evaluator. The README documents the main execution path; source-index references confirm several internal connections, but many subsystem details were not sampled.

Main entry points: [`zyx/src/tensor/mod.rs`](https://github.com/zk4x/zyx/blob/main/zyx/src/tensor/mod.rs) (Tensor operations), [`zyx/src/kernel/autotune.rs`](https://github.com/zk4x/zyx/blob/main/zyx/src/kernel/autotune.rs) (Kernel autotuning), [`zyx-nn/src/decoder_block.rs`](https://github.com/zk4x/zyx/blob/main/zyx-nn/src/decoder_block.rs) (Neural-network layers), [`zyx/src/kernel/mod.rs`](https://github.com/zk4x/zyx/blob/main/zyx/src/kernel/mod.rs) (Kernel IR)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Tensor and graph core"]
  node_tensor["Tensor operations<br/>[mod.rs]"]
  node_eager["Eager execution<br/>[runtime.rs]"]
  node_tape["Tape graph<br/>[tape.rs]"]
  node_autograd["Autograd<br/>[autograd.rs]"]
  node_graphopt["Graph optimization<br/>[mod.rs]"]
  node_kernelize["Kernelization<br/>[kernelizer.rs]"]
  node_types["Dtypes and constants<br/>[dtype.rs]"]
  node_scalars["Scalar types<br/>[scalar.rs]"]
  node_shapes["Tensor shapes<br/>[shape.rs]"]
  node_hashers["Kernel hashing<br/>[hashers.rs]"]
  node_rng["Random generation<br/>[rng.rs]"]
  node_slab["Memory slabs<br/>[slab.rs]"]
end

subgraph group_compute["Kernel execution"]
  node_kernelir["Kernel IR<br/>[mod.rs]"]
  node_autotune["Kernel autotuning<br/>[autotune.rs]"]
  node_cost["Kernel cost model<br/>[cost.rs]"]
  node_debug["Kernel debugging<br/>[debug.rs]"]
  node_backends["Device backends<br/>[mod.rs]"]
  node_codegen["Backend code generation<br/>[mod.rs]"]
  node_storage[("Disk and host pools<br/>[disk.rs]")]
end

subgraph group_ml["Model and training"]
  node_nn["Neural-network layers<br/>[decoder_block.rs]"]
  node_optim["Optimizers<br/>[adam.rs]"]
  node_derive["Module derive<br/>[lib.rs]"]
end

subgraph group_interfaces["Formats and interfaces"]
  node_python["Python bindings<br/>[lib.rs]"]
  node_onnx["ONNX evaluation<br/>[eval.rs]"]
  node_viz["Graph visualizer<br/>[server.rs]"]
end

node_user(("ML application"))

node_user -->|"composes"| node_tensor
node_tensor -->|"executes eagerly"| node_eager
node_user -->|"records work"| node_tape
node_tape -->|"differentiates"| node_autograd
node_tape -->|"optimizes graph"| node_graphopt
node_graphopt -.->|"lowers graph"| node_kernelize
node_kernelize -.->|"builds kernels"| node_kernelir
node_eager -->|"fuses operations"| node_kernelir
node_graphopt -->|"selects kernels"| node_kernelir
node_kernelir -->|"tunes candidates"| node_autotune
node_autotune -.->|"estimates cost"| node_cost
node_autotune -->|"measures launches"| node_backends
node_backends -.->|"dispatches generation"| node_codegen
node_backends -->|"loads pools"| node_storage
node_tensor -->|"uses types"| node_types
node_types -->|"uses scalar values"| node_scalars
node_tensor -->|"validates dimensions"| node_shapes
node_autotune -->|"hashes candidates"| node_hashers
node_autotune -->|"uses random seeds"| node_rng
node_debug -->|"inspects storage"| node_slab
node_user -->|"builds models"| node_nn
node_nn -.->|"composes operations"| node_tensor
node_user -->|"updates parameters"| node_optim
node_optim -.->|"updates model"| node_nn
node_nn -->|"derives Module"| node_derive
node_python -.->|"exposes API"| node_tensor
node_user -.->|"evaluates model"| node_onnx
node_eager -.->|"serves graph views"| node_viz

click node_tensor "https://github.com/zk4x/zyx/blob/main/zyx/src/tensor/mod.rs"
click node_eager "https://github.com/zk4x/zyx/blob/main/zyx/src/runtime.rs"
click node_tape "https://github.com/zk4x/zyx/blob/main/zyx/src/tape.rs"
click node_autograd "https://github.com/zk4x/zyx/blob/main/zyx/src/graph/autograd.rs"
click node_graphopt "https://github.com/zk4x/zyx/blob/main/zyx/src/graph/mod.rs"
click node_kernelize "https://github.com/zk4x/zyx/blob/main/zyx/src/graph/kernelizer.rs"
click node_kernelir "https://github.com/zk4x/zyx/blob/main/zyx/src/kernel/mod.rs"
click node_autotune "https://github.com/zk4x/zyx/blob/main/zyx/src/kernel/autotune.rs"
click node_cost "https://github.com/zk4x/zyx/blob/main/zyx/src/kernel/cost.rs"
click node_debug "https://github.com/zk4x/zyx/blob/main/zyx/src/kernel/debug.rs"
click node_backends "https://github.com/zk4x/zyx/blob/main/zyx/src/backend/mod.rs"
click node_codegen "https://github.com/zk4x/zyx/blob/main/zyx/src/codegen/mod.rs"
click node_storage "https://github.com/zk4x/zyx/blob/main/zyx/src/backend/disk.rs"
click node_types "https://github.com/zk4x/zyx/blob/main/zyx/src/dtype.rs"
click node_scalars "https://github.com/zk4x/zyx/blob/main/zyx/src/scalar.rs"
click node_shapes "https://github.com/zk4x/zyx/blob/main/zyx/src/shape.rs"
click node_hashers "https://github.com/zk4x/zyx/blob/main/zyx/src/hashers.rs"
click node_rng "https://github.com/zk4x/zyx/blob/main/zyx/src/rng.rs"
click node_slab "https://github.com/zk4x/zyx/blob/main/zyx/src/slab.rs"
click node_nn "https://github.com/zk4x/zyx/blob/main/zyx-nn/src/decoder_block.rs"
click node_optim "https://github.com/zk4x/zyx/blob/main/zyx-optim/src/adam.rs"
click node_derive "https://github.com/zk4x/zyx/blob/main/zyx-derive/src/lib.rs"
click node_python "https://github.com/zk4x/zyx/blob/main/zyx-py/src/lib.rs"
click node_onnx "https://github.com/zk4x/zyx/blob/main/zyx-onnx/src/eval.rs"
click node_viz "https://github.com/zk4x/zyx/blob/main/zyx/src/viz/server.rs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_tensor,node_eager,node_tape,node_autograd,node_graphopt,node_kernelize,node_types,node_scalars,node_shapes,node_hashers,node_rng,node_slab toneBlue
class node_kernelir,node_autotune,node_cost,node_debug,node_backends,node_codegen,node_storage toneAmber
class node_nn,node_optim,node_derive toneMint
class node_python,node_onnx,node_viz toneRose
class node_user toneIndigo
```

## Components

### Tensor and graph core

- [Tensor operations](https://github.com/zk4x/zyx/blob/main/zyx/src/tensor/mod.rs): `zyx/src/tensor/mod.rs`
- [Eager execution](https://github.com/zk4x/zyx/blob/main/zyx/src/runtime.rs): `zyx/src/runtime.rs`
- [Tape graph](https://github.com/zk4x/zyx/blob/main/zyx/src/tape.rs): `zyx/src/tape.rs`
- [Autograd](https://github.com/zk4x/zyx/blob/main/zyx/src/graph/autograd.rs): `zyx/src/graph/autograd.rs`
- [Graph optimization](https://github.com/zk4x/zyx/blob/main/zyx/src/graph/mod.rs): `zyx/src/graph/mod.rs`
- [Kernelization](https://github.com/zk4x/zyx/blob/main/zyx/src/graph/kernelizer.rs): `zyx/src/graph/kernelizer.rs`
- [Dtypes and constants](https://github.com/zk4x/zyx/blob/main/zyx/src/dtype.rs): `zyx/src/dtype.rs`
- [Scalar types](https://github.com/zk4x/zyx/blob/main/zyx/src/scalar.rs): `zyx/src/scalar.rs`
- [Tensor shapes](https://github.com/zk4x/zyx/blob/main/zyx/src/shape.rs): `zyx/src/shape.rs`
- [Kernel hashing](https://github.com/zk4x/zyx/blob/main/zyx/src/hashers.rs): `zyx/src/hashers.rs`
- [Random generation](https://github.com/zk4x/zyx/blob/main/zyx/src/rng.rs): `zyx/src/rng.rs`
- [Memory slabs](https://github.com/zk4x/zyx/blob/main/zyx/src/slab.rs): `zyx/src/slab.rs`

### Kernel execution

- [Kernel IR](https://github.com/zk4x/zyx/blob/main/zyx/src/kernel/mod.rs): `zyx/src/kernel/mod.rs`
- [Kernel autotuning](https://github.com/zk4x/zyx/blob/main/zyx/src/kernel/autotune.rs): `zyx/src/kernel/autotune.rs`
- [Kernel cost model](https://github.com/zk4x/zyx/blob/main/zyx/src/kernel/cost.rs): `zyx/src/kernel/cost.rs`
- [Kernel debugging](https://github.com/zk4x/zyx/blob/main/zyx/src/kernel/debug.rs): `zyx/src/kernel/debug.rs`
- [Device backends](https://github.com/zk4x/zyx/blob/main/zyx/src/backend/mod.rs): `zyx/src/backend/mod.rs`
- [Backend code generation](https://github.com/zk4x/zyx/blob/main/zyx/src/codegen/mod.rs): `zyx/src/codegen/mod.rs`
- [Disk and host pools](https://github.com/zk4x/zyx/blob/main/zyx/src/backend/disk.rs): `zyx/src/backend/disk.rs`

### Model and training

- [Neural-network layers](https://github.com/zk4x/zyx/blob/main/zyx-nn/src/decoder_block.rs): `zyx-nn/src/decoder_block.rs`
- [Optimizers](https://github.com/zk4x/zyx/blob/main/zyx-optim/src/adam.rs): `zyx-optim/src/adam.rs`
- [Module derive](https://github.com/zk4x/zyx/blob/main/zyx-derive/src/lib.rs): `zyx-derive/src/lib.rs`

### Formats and interfaces

- [Python bindings](https://github.com/zk4x/zyx/blob/main/zyx-py/src/lib.rs): `zyx-py/src/lib.rs`
- [ONNX evaluation](https://github.com/zk4x/zyx/blob/main/zyx-onnx/src/eval.rs): `zyx-onnx/src/eval.rs`
- [Graph visualizer](https://github.com/zk4x/zyx/blob/main/zyx/src/viz/server.rs): `zyx/src/viz/server.rs`

### Other components

- **ML application**

## Connections

- ML application → Tensor operations: composes (evidence: `README.md`)
- Tensor operations → Eager execution: executes eagerly (evidence: `README.md`)
- ML application → Tape graph: records work (evidence: `README.md`)
- Tape graph → Autograd: differentiates (evidence: `README.md`)
- Tape graph → Graph optimization: optimizes graph (evidence: `README.md`)
- Graph optimization → Kernelization: lowers graph (evidence: `zyx/src/graph/mod.rs`)
- Kernelization → Kernel IR: builds kernels
- Eager execution → Kernel IR: fuses operations (evidence: `README.md`)
- Graph optimization → Kernel IR: selects kernels (evidence: `README.md`)
- Kernel IR → Kernel autotuning: tunes candidates (evidence: `README.md`)
- Kernel autotuning → Kernel cost model: estimates cost (evidence: `zyx/src/kernel/autotune.rs`)
- Kernel autotuning → Device backends: measures launches (evidence: `zyx/src/kernel/autotune.rs`)
- Device backends → Backend code generation: dispatches generation
- Device backends → Disk and host pools: loads pools (evidence: `zyx/src/backend/cuda.rs`)
- Tensor operations → Dtypes and constants: uses types (evidence: `zyx/src/tensor/mod.rs`)
- Dtypes and constants → Scalar types: uses scalar values (evidence: `zyx/src/dtype.rs`)
- Tensor operations → Tensor shapes: validates dimensions (evidence: `zyx/src/tensor/mod.rs`)
- Kernel autotuning → Kernel hashing: hashes candidates (evidence: `zyx/src/kernel/autotune.rs`)
- Kernel autotuning → Random generation: uses random seeds (evidence: `zyx/src/kernel/autotune.rs`)
- Kernel debugging → Memory slabs: inspects storage (evidence: `zyx/src/kernel/debug.rs`)
- ML application → Neural-network layers: builds models (evidence: `README.md`)
- Neural-network layers → Tensor operations: composes operations
- ML application → Optimizers: updates parameters (evidence: `README.md`)
- Optimizers → Neural-network layers: updates model
- Neural-network layers → Module derive: derives Module
- Python bindings → Tensor operations: exposes API
- ML application → ONNX evaluation: evaluates model
- Eager execution → Graph visualizer: serves graph views (evidence: `zyx/src/runtime.rs`)
