# knowaguy4u-cell/tinygrad architecture

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

- Interactive diagram: https://gitdiagram.com/knowaguy4u-cell/tinygrad
- Repository: https://github.com/knowaguy4u-cell/tinygrad

## Overview

tinygrad is an end-to-end deep-learning framework. A user builds lazy tensor expressions, optionally differentiates them and applies neural-network or optimizer functionality, then realizes computations; realization schedules and lowers operations into device kernels for execution. The graph also preserves the documented JIT, datasets, and accelerator capabilities, plus the repository’s LLM and MoE-routing branches. Relationships are based on README statements and SOURCE INDEX references; unsampled modules are represented by their supported paths, without assuming their internal call flow.

Main entry points: [`tinygrad/tensor.py`](https://github.com/knowaguy4u-cell/tinygrad/blob/master/tinygrad/tensor.py) (Tensor API), [`tinygrad/engine/realize.py`](https://github.com/knowaguy4u-cell/tinygrad/blob/master/tinygrad/engine/realize.py) (Realization engine), [`tinygrad/mixin/op.py`](https://github.com/knowaguy4u-cell/tinygrad/blob/master/tinygrad/mixin/op.py) (Tensor operations), [`tinygrad/uop/ops.py`](https://github.com/knowaguy4u-cell/tinygrad/blob/master/tinygrad/uop/ops.py) (UOp graph)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["Tensor and learning API"]
  node_tensor["Tensor API<br/>[tensor.py]"]
  node_tensor_ops["Tensor operations<br/>[op.py]"]
  node_autograd["Autograd functions<br/>[function.py]"]
  node_dtypes["Dtypes<br/>[dtype.py]"]
end

subgraph group_compiler["IR and compilation"]
  node_uop["UOp graph<br/>[ops.py]"]
  node_schedule["Scheduling"]
  node_codegen["Code generation"]
  node_renderer["Kernel renderers"]
end

subgraph group_execution["Execution and devices"]
  node_realize["Realization engine<br/>[realize.py]"]
  node_jit["JIT capture<br/>[jit.py]"]
  node_device["Device and buffers<br/>[device.py]"]
  node_runtime["Device runtimes"]
end

subgraph group_learning["Learning workloads"]
  node_nn["NN and optimizers"]
  node_datasets["Datasets<br/>[datasets.py]"]
end

subgraph group_specialized["Specialized workloads"]
  node_llm["LLM models<br/>[model.py]"]
  node_moe["MoE routing<br/>[moe_routing.py]"]
end

node_user(("Framework user"))
node_result(("Computed tensors"))

node_user -->|"builds expressions"| node_tensor
node_tensor -->|"delegates operations"| node_tensor_ops
node_tensor_ops -->|"builds graph"| node_uop
node_tensor -->|"uses types"| node_dtypes
node_tensor_ops -.->|"records gradients"| node_autograd
node_uop -->|"prepares graph"| node_schedule
node_schedule -->|"feeds execution"| node_realize
node_realize -->|"lowers kernels"| node_codegen
node_codegen -->|"renders kernels"| node_renderer
node_realize -->|"dispatches work"| node_device
node_device -->|"creates runtimes"| node_runtime
node_user -->|"captures calls"| node_jit
node_jit -->|"replays computation"| node_tensor
node_user -->|"trains models"| node_nn
node_user -->|"loads data"| node_datasets
node_nn -->|"uses tensors"| node_tensor
node_user -.->|"runs models"| node_llm
node_user -.->|"routes experts"| node_moe
node_realize -->|"produces tensors"| node_result

click node_tensor "https://github.com/knowaguy4u-cell/tinygrad/blob/master/tinygrad/tensor.py"
click node_tensor_ops "https://github.com/knowaguy4u-cell/tinygrad/blob/master/tinygrad/mixin/op.py"
click node_autograd "https://github.com/knowaguy4u-cell/tinygrad/blob/master/tinygrad/function.py"
click node_dtypes "https://github.com/knowaguy4u-cell/tinygrad/blob/master/tinygrad/dtype.py"
click node_uop "https://github.com/knowaguy4u-cell/tinygrad/blob/master/tinygrad/uop/ops.py"
click node_schedule "https://github.com/knowaguy4u-cell/tinygrad/tree/master/tinygrad/schedule"
click node_codegen "https://github.com/knowaguy4u-cell/tinygrad/tree/master/tinygrad/codegen"
click node_renderer "https://github.com/knowaguy4u-cell/tinygrad/tree/master/tinygrad/renderer"
click node_realize "https://github.com/knowaguy4u-cell/tinygrad/blob/master/tinygrad/engine/realize.py"
click node_jit "https://github.com/knowaguy4u-cell/tinygrad/blob/master/tinygrad/engine/jit.py"
click node_device "https://github.com/knowaguy4u-cell/tinygrad/blob/master/tinygrad/device.py"
click node_runtime "https://github.com/knowaguy4u-cell/tinygrad/tree/master/tinygrad/runtime"
click node_nn "https://github.com/knowaguy4u-cell/tinygrad/tree/master/tinygrad/nn"
click node_datasets "https://github.com/knowaguy4u-cell/tinygrad/blob/master/tinygrad/nn/datasets.py"
click node_llm "https://github.com/knowaguy4u-cell/tinygrad/blob/master/tinygrad/llm/model.py"
click node_moe "https://github.com/knowaguy4u-cell/tinygrad/blob/master/extra/gemm/moe_routing.py"

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_tensor_ops,node_autograd,node_dtypes,node_user toneBlue
class node_uop,node_schedule,node_codegen,node_renderer toneAmber
class node_realize,node_jit,node_device,node_runtime toneMint
class node_nn,node_datasets toneRose
class node_llm,node_moe,node_result toneIndigo
```

## Components

### Tensor and learning API

- [Tensor API](https://github.com/knowaguy4u-cell/tinygrad/blob/master/tinygrad/tensor.py): `tinygrad/tensor.py`
- [Tensor operations](https://github.com/knowaguy4u-cell/tinygrad/blob/master/tinygrad/mixin/op.py): `tinygrad/mixin/op.py`
- [Autograd functions](https://github.com/knowaguy4u-cell/tinygrad/blob/master/tinygrad/function.py): `tinygrad/function.py`
- [Dtypes](https://github.com/knowaguy4u-cell/tinygrad/blob/master/tinygrad/dtype.py): `tinygrad/dtype.py`

### IR and compilation

- [UOp graph](https://github.com/knowaguy4u-cell/tinygrad/blob/master/tinygrad/uop/ops.py): `tinygrad/uop/ops.py`
- [Scheduling](https://github.com/knowaguy4u-cell/tinygrad/tree/master/tinygrad/schedule): `tinygrad/schedule`
- [Code generation](https://github.com/knowaguy4u-cell/tinygrad/tree/master/tinygrad/codegen): `tinygrad/codegen`
- [Kernel renderers](https://github.com/knowaguy4u-cell/tinygrad/tree/master/tinygrad/renderer): `tinygrad/renderer`

### Execution and devices

- [Realization engine](https://github.com/knowaguy4u-cell/tinygrad/blob/master/tinygrad/engine/realize.py): `tinygrad/engine/realize.py`
- [JIT capture](https://github.com/knowaguy4u-cell/tinygrad/blob/master/tinygrad/engine/jit.py): `tinygrad/engine/jit.py`
- [Device and buffers](https://github.com/knowaguy4u-cell/tinygrad/blob/master/tinygrad/device.py): `tinygrad/device.py`
- [Device runtimes](https://github.com/knowaguy4u-cell/tinygrad/tree/master/tinygrad/runtime): `tinygrad/runtime`

### Learning workloads

- [NN and optimizers](https://github.com/knowaguy4u-cell/tinygrad/tree/master/tinygrad/nn): `tinygrad/nn`
- [Datasets](https://github.com/knowaguy4u-cell/tinygrad/blob/master/tinygrad/nn/datasets.py): `tinygrad/nn/datasets.py`

### Specialized workloads

- [LLM models](https://github.com/knowaguy4u-cell/tinygrad/blob/master/tinygrad/llm/model.py): `tinygrad/llm/model.py`
- [MoE routing](https://github.com/knowaguy4u-cell/tinygrad/blob/master/extra/gemm/moe_routing.py): `extra/gemm/moe_routing.py`

### Other components

- **Framework user**
- **Computed tensors**

## Connections

- Framework user → Tensor API: builds expressions
- Tensor API → Tensor operations: delegates operations (evidence: `tinygrad/tensor.py`)
- Tensor operations → UOp graph: builds graph (evidence: `tinygrad/mixin/op.py`)
- Tensor API → Dtypes: uses types (evidence: `tinygrad/tensor.py`)
- Tensor operations → Autograd functions: records gradients
- UOp graph → Scheduling: prepares graph (evidence: `tinygrad/uop/ops.py`)
- Scheduling → Realization engine: feeds execution (evidence: `tinygrad/tensor.py`)
- Realization engine → Code generation: lowers kernels (evidence: `tinygrad/engine/realize.py`)
- Code generation → Kernel renderers: renders kernels (evidence: `tinygrad/renderer/__init__.py`)
- Realization engine → Device and buffers: dispatches work (evidence: `tinygrad/engine/realize.py`)
- Device and buffers → Device runtimes: creates runtimes (evidence: `tinygrad/engine/realize.py`)
- Framework user → JIT capture: captures calls
- JIT capture → Tensor API: replays computation (evidence: `tinygrad/__init__.py`)
- Framework user → NN and optimizers: trains models
- Framework user → Datasets: loads data
- NN and optimizers → Tensor API: uses tensors (evidence: `tinygrad/nn/onnx.py`)
- Framework user → LLM models: runs models
- Framework user → MoE routing: routes experts
- Realization engine → Computed tensors: produces tensors
