# woues1/tinytorch architecture

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

- Interactive diagram: https://gitdiagram.com/woues1/tinytorch
- Repository: https://github.com/woues1/tinytorch

## Overview

1. **Repository purpose and shape**
   - `tinytorch` is a **Rust library crate** for minimal deep-learning primitives, not a full application.
   - The architecture is centered on a **generic tensor core** plus a small set of neural-network building blocks that can be composed for inference and simple model experiments.

2. **Top-level module boundaries**
   - `src/lib.rs` is the public entry point that re-exports the main submodules.
   - The crate is split into four functional domains:
     - `tensor/` for data representation and math
     - `nn/` for layers and model composition
     - `activations/` for nonlinearities
     - `losses/` for objective functions
   - `macros.rs` supports repeated operator implementations, especially for tensor elementwise behavior.

3. **Tensor core as the foundation**
   - `Tensor<T>` is the central data structure and is generic over floating-point numeric types.
   - The tensor subsystem handles:
     - shapes and strides
     - broadcasting
     - reshaping and transposition
     - matrix multiplication
     - reductions such as sum, max, and mean
   - This is the lowest-level dependency for nearly everything else in the crate.

4. **Tensor initialization and reduction utilities**
   - `src/tensor/init.rs` contains weight/array initialization helpers.
   - `src/tensor/reductions.rs` groups reduction logic, which is reused by activations, losses, and model internals.
   - These modules indicate the tensor layer is not just storage; it also owns the numerical primitives needed for model execution.

5. **Operations layer**
   - `src/ops/` contains lower-level math support, including `backward.rs`.
   - Based on the structure, this is an internal operations boundary for reusable numeric logic, separate from the public neural-network API.
   - The presence of `backward.rs` suggests some gradient-related or reverse-pass utilities, but the repository structure still reads primarily as a forward-computation framework rather than a full autograd engine.

6. **Activation subsystem**
   - `src/activations/` contains the nonlinear transforms:
     - ReLU
     - Sigmoid
     - Tanh
     - Softmax
     - Log-Softmax
     - GELU
   - Softmax is described as numerically stable via max-shifting, which is important for the numerical behavior of the model pipeline.
   - These functions sit between linear layers and/or output heads in the data flow.

7. **Loss subsystem**
   - `src/losses/` provides objective functions:
     - MSE
     - Binary Cross Entropy
     - Cross Entropy
   - Loss functions consume model outputs and targets, and are the terminal stage of the typical training-style pipeline.
   - Even though the README emphasizes inference, the loss layer is in place for evaluation or future optimization workflows.

8. **Neural-network composition layer**
   - `src/nn/` defines reusable model abstractions:
     - `Layer` trait for polymorphic layer behavior
     - `Linear` fully connected layer
     - `Dropout` regularization layer
     - `Sequential` container
     - `MLP` convenience model built from stacked linear layers
   - This is the architectural boundary where tensors become model pipelines: inputs flow through layers, activations, and possibly dropout before producing outputs.

9. **How data flows through the system**
   - Typical flow is:
     - create or load a `Tensor`
     - pass it through one or more `Layer` implementations
     - apply activations and optional dropout
     - compare the output to a target via a loss function
   - `Sequential` and `MLP` are the main composition mechanisms, while `Linear` is the fundamental learnable block.
   - The design is modular: layers depend on tensor operations, but the tensor layer does not depend on neural-network concepts.

10. **Public API and extensibility**
   - The crate exposes a small surface area through `lib.rs`, while internal modules remain organized by responsibility.
   - The `Layer` trait is the main extension point for custom components.
   - The macro module implies that common elementwise/broadcast patterns are centralized rather than duplicated across implementations.

11. **Testing boundary**
   - `src/tests/` contains focused tests for:
     - tensor behavior
     - activations
     - linear layers
     - losses
     - sequential and MLP composition
   - The test layout mirrors the production module layout, which makes the architecture easy to verify subsystem by subsystem.

12. **Build/runtime characteristics**
   - This is a **Cargo/Rust** project with dependencies on:
     - `num-traits` for generic numeric abstractions
     - `rand` and `rand_distr` for randomized initialization
   - The release profile is optimized for small binary size (`opt-level = 'z'`, LTO, stripped symbols), so deployment/runtime footprint is a conscious design concern.

Main entry points: [`src/tensor/tensor.rs`](https://github.com/woues1/tinytorch/blob/master/src/tensor/tensor.rs) (Tensor), [`src/nn/mod.rs`](https://github.com/woues1/tinytorch/blob/master/src/nn/mod.rs) (NN mod), [`src/lib.rs`](https://github.com/woues1/tinytorch/blob/master/src/lib.rs) (lib.rs), [`src/tensor/mod.rs`](https://github.com/woues1/tinytorch/blob/master/src/tensor/mod.rs) (Tensor mod)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["Public API"]
  node_lib["lib.rs<br/>crate entry<br/>[lib.rs]"]
  node_macros["macros.rs<br/>impl macros<br/>[macros.rs]"]
end

subgraph group_tensor["Tensor Core"]
  node_tensor_mod["Tensor mod<br/>[mod.rs]"]
  node_tensor_core[("Tensor<br/>[tensor.rs]")]
  node_tensor_init["Init<br/>[init.rs]"]
  node_tensor_reductions["Reductions<br/>[reductions.rs]"]
end

subgraph group_ops["Ops"]
  node_ops_mod["Ops mod<br/>[mod.rs]"]
  node_ops_backward["Backward<br/>reverse ops<br/>[backward.rs]"]
end

subgraph group_nn["Neural Network"]
  node_nn_mod["NN mod<br/>[mod.rs]"]
  node_layer_trait["Layer<br/>trait<br/>[layer.rs]"]
  node_linear["Linear<br/>dense layer<br/>[linear.rs]"]
  node_dropout["Dropout<br/>regularization<br/>[dropout.rs]"]
  node_sequential["Sequential<br/>container<br/>[sequential.rs]"]
  node_mlp["MLP<br/>model<br/>[mlp.rs]"]
end

subgraph group_funcs["Functions"]
  node_act_mod["Acts mod<br/>activations module<br/>[mod.rs]"]
  node_activation_trait["Activation<br/>trait<br/>[activation.rs]"]
  node_activations["Activations<br/>nonlinear ops<br/>[activations.rs]"]
  node_loss_mod["Losses mod<br/>[mod.rs]"]
  node_mse["MSE<br/>regression loss<br/>[mse.rs]"]
  node_bce["BCE<br/>classification loss"]
  node_ce["Cross Entropy<br/>classification loss<br/>[cross_entropy.rs]"]
end

subgraph group_tests["Tests"]
  node_tests_mod["Tests<br/>test harness<br/>[mod.rs]"]
  node_tensor_tests["Tensor tests<br/>[tensor_tests.rs]"]
  node_act_tests["Act tests<br/>activation tests"]
  node_nn_tests_mod["NN tests<br/>[mod.rs]"]
  node_linear_test["Linear test<br/>layer tests<br/>[linear_test.rs]"]
  node_sequential_test["Sequential test<br/>composition tests<br/>[sequential_test.rs]"]
  node_mlp_test["MLP test<br/>model tests<br/>[mlp_test.rs]"]
  node_losses_test["Loss tests<br/>[losses_test.rs]"]
end

node_lib -->|"re-exports"| node_tensor_mod
node_lib -->|"re-exports"| node_nn_mod
node_lib -->|"re-exports"| node_act_mod
node_lib -->|"re-exports"| node_loss_mod
node_lib -.->|"uses"| node_macros
node_tensor_mod -->|"contains"| node_tensor_core
node_tensor_mod -->|"contains"| node_tensor_init
node_tensor_mod -->|"contains"| node_tensor_reductions
node_tensor_mod -.->|"supports"| node_ops_mod
node_ops_mod -->|"contains"| node_ops_backward
node_tensor_core -.->|"feeds"| node_ops_backward
node_nn_mod -->|"defines"| node_layer_trait
node_nn_mod -->|"contains"| node_linear
node_nn_mod -->|"contains"| node_dropout
node_nn_mod -->|"contains"| node_sequential
node_nn_mod -->|"contains"| node_mlp
node_linear -.->|"uses"| node_tensor_core
node_dropout -.->|"uses"| node_tensor_core
node_sequential -->|"composes"| node_layer_trait
node_mlp -->|"builds from"| node_linear
node_mlp -.->|"uses"| node_sequential
node_act_mod -->|"defines"| node_activation_trait
node_act_mod -->|"contains"| node_activations
node_activations -.->|"uses"| node_tensor_core
node_loss_mod -->|"contains"| node_mse
node_loss_mod -->|"contains"| node_bce
node_loss_mod -->|"contains"| node_ce
node_mse -.->|"uses"| node_tensor_core
node_bce -.->|"uses"| node_tensor_core
node_ce -.->|"uses"| node_tensor_core
node_tests_mod -->|"includes"| node_tensor_tests
node_tests_mod -->|"includes"| node_act_tests
node_tests_mod -->|"includes"| node_nn_tests_mod
node_nn_tests_mod -->|"includes"| node_linear_test
node_nn_tests_mod -->|"includes"| node_sequential_test
node_nn_tests_mod -->|"includes"| node_mlp_test
node_nn_tests_mod -->|"includes"| node_losses_test

click node_lib "https://github.com/woues1/tinytorch/blob/master/src/lib.rs"
click node_macros "https://github.com/woues1/tinytorch/blob/master/src/macros.rs"
click node_tensor_mod "https://github.com/woues1/tinytorch/blob/master/src/tensor/mod.rs"
click node_tensor_core "https://github.com/woues1/tinytorch/blob/master/src/tensor/tensor.rs"
click node_tensor_init "https://github.com/woues1/tinytorch/blob/master/src/tensor/init.rs"
click node_tensor_reductions "https://github.com/woues1/tinytorch/blob/master/src/tensor/reductions.rs"
click node_ops_mod "https://github.com/woues1/tinytorch/blob/master/src/ops/mod.rs"
click node_ops_backward "https://github.com/woues1/tinytorch/blob/master/src/ops/backward.rs"
click node_nn_mod "https://github.com/woues1/tinytorch/blob/master/src/nn/mod.rs"
click node_layer_trait "https://github.com/woues1/tinytorch/blob/master/src/nn/layer.rs"
click node_linear "https://github.com/woues1/tinytorch/blob/master/src/nn/linear.rs"
click node_dropout "https://github.com/woues1/tinytorch/blob/master/src/nn/dropout.rs"
click node_sequential "https://github.com/woues1/tinytorch/blob/master/src/nn/sequential.rs"
click node_mlp "https://github.com/woues1/tinytorch/blob/master/src/nn/mlp.rs"
click node_act_mod "https://github.com/woues1/tinytorch/blob/master/src/activations/mod.rs"
click node_activation_trait "https://github.com/woues1/tinytorch/blob/master/src/activations/activation.rs"
click node_activations "https://github.com/woues1/tinytorch/blob/master/src/activations/activations.rs"
click node_loss_mod "https://github.com/woues1/tinytorch/blob/master/src/losses/mod.rs"
click node_mse "https://github.com/woues1/tinytorch/blob/master/src/losses/mse.rs"
click node_bce "https://github.com/woues1/tinytorch/blob/master/src/losses/binary_cross_entropy.rs"
click node_ce "https://github.com/woues1/tinytorch/blob/master/src/losses/cross_entropy.rs"
click node_tests_mod "https://github.com/woues1/tinytorch/blob/master/src/tests/mod.rs"
click node_tensor_tests "https://github.com/woues1/tinytorch/blob/master/src/tests/tensor_tests.rs"
click node_act_tests "https://github.com/woues1/tinytorch/blob/master/src/tests/activations_test.rs"
click node_nn_tests_mod "https://github.com/woues1/tinytorch/blob/master/src/tests/nn_tests/mod.rs"
click node_linear_test "https://github.com/woues1/tinytorch/blob/master/src/tests/nn_tests/linear_test.rs"
click node_sequential_test "https://github.com/woues1/tinytorch/blob/master/src/tests/nn_tests/sequential_test.rs"
click node_mlp_test "https://github.com/woues1/tinytorch/blob/master/src/tests/nn_tests/mlp_test.rs"
click node_losses_test "https://github.com/woues1/tinytorch/blob/master/src/tests/nn_tests/losses_test.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_lib,node_macros toneBlue
class node_tensor_mod,node_tensor_core,node_tensor_init,node_tensor_reductions toneAmber
class node_ops_mod,node_ops_backward toneMint
class node_nn_mod,node_layer_trait,node_linear,node_dropout,node_sequential,node_mlp toneRose
class node_act_mod,node_activation_trait,node_activations,node_loss_mod,node_mse,node_bce,node_ce toneIndigo
class node_tests_mod,node_tensor_tests,node_act_tests,node_nn_tests_mod,node_linear_test,node_sequential_test,node_mlp_test,node_losses_test toneTeal
```

## Components

### Public API

Crate entry and shared implementation macros.

- [lib.rs](https://github.com/woues1/tinytorch/blob/master/src/lib.rs): crate entry · Public module surface and re-exports. · `src/lib.rs`
- [macros.rs](https://github.com/woues1/tinytorch/blob/master/src/macros.rs): impl macros · Shared macros for repetitive operator implementations. · `src/macros.rs`

### Tensor Core

Core tensor representation and numerical primitives.

- [Tensor mod](https://github.com/woues1/tinytorch/blob/master/src/tensor/mod.rs): tensor module · Tensor subsystem entry point. · `src/tensor/mod.rs`
- [Tensor](https://github.com/woues1/tinytorch/blob/master/src/tensor/tensor.rs): generic tensor · Core tensor data structure and shape/stride handling. · `src/tensor/tensor.rs`
- [Init](https://github.com/woues1/tinytorch/blob/master/src/tensor/init.rs): initialization · Array and weight initialization helpers. · `src/tensor/init.rs`
- [Reductions](https://github.com/woues1/tinytorch/blob/master/src/tensor/reductions.rs): reductions · Sum, max, mean, and related reduction primitives. · `src/tensor/reductions.rs`

### Ops

Internal reusable math and backward-pass helpers.

- [Ops mod](https://github.com/woues1/tinytorch/blob/master/src/ops/mod.rs): ops module · Internal math and backward-pass boundary. · `src/ops/mod.rs`
- [Backward](https://github.com/woues1/tinytorch/blob/master/src/ops/backward.rs): reverse ops · Gradient-related or reverse-pass utilities. · `src/ops/backward.rs`

### Neural Network

Composable layers and model containers.

- [NN mod](https://github.com/woues1/tinytorch/blob/master/src/nn/mod.rs): nn module · Neural-network layer and composition entry point. · `src/nn/mod.rs`
- [Layer](https://github.com/woues1/tinytorch/blob/master/src/nn/layer.rs): trait · Polymorphic layer interface and extension point. · `src/nn/layer.rs`
- [Linear](https://github.com/woues1/tinytorch/blob/master/src/nn/linear.rs): dense layer · Fully connected learnable layer. · `src/nn/linear.rs`
- [Dropout](https://github.com/woues1/tinytorch/blob/master/src/nn/dropout.rs): regularization · Dropout regularization layer. · `src/nn/dropout.rs`
- [Sequential](https://github.com/woues1/tinytorch/blob/master/src/nn/sequential.rs): container · Layer chain composition. · `src/nn/sequential.rs`
- [MLP](https://github.com/woues1/tinytorch/blob/master/src/nn/mlp.rs): model · Convenience stacked multi-layer perceptron. · `src/nn/mlp.rs`

### Functions

Activations and loss functions built on tensors.

- [Acts mod](https://github.com/woues1/tinytorch/blob/master/src/activations/mod.rs): activations module · Activation subsystem entry point. · `src/activations/mod.rs`
- [Activation](https://github.com/woues1/tinytorch/blob/master/src/activations/activation.rs): trait · Shared activation interface. · `src/activations/activation.rs`
- [Activations](https://github.com/woues1/tinytorch/blob/master/src/activations/activations.rs): nonlinear ops · ReLU, sigmoid, tanh, softmax, log-softmax, GELU. · `src/activations/activations.rs`
- [Losses mod](https://github.com/woues1/tinytorch/blob/master/src/losses/mod.rs): losses module · Loss subsystem entry point. · `src/losses/mod.rs`
- [MSE](https://github.com/woues1/tinytorch/blob/master/src/losses/mse.rs): regression loss · Mean squared error objective. · `src/losses/mse.rs`
- [BCE](https://github.com/woues1/tinytorch/blob/master/src/losses/binary_cross_entropy.rs): classification loss · Binary cross entropy objective. · `src/losses/binary_cross_entropy.rs`
- [Cross Entropy](https://github.com/woues1/tinytorch/blob/master/src/losses/cross_entropy.rs): classification loss · Cross entropy objective. · `src/losses/cross_entropy.rs`

### Tests

Subsystem-focused test coverage.

- [Tests](https://github.com/woues1/tinytorch/blob/master/src/tests/mod.rs): test harness · Test module root. · `src/tests/mod.rs`
- [Tensor tests](https://github.com/woues1/tinytorch/blob/master/src/tests/tensor_tests.rs): tensor tests · Tensor behavior coverage. · `src/tests/tensor_tests.rs`
- [Act tests](https://github.com/woues1/tinytorch/blob/master/src/tests/activations_test.rs): activation tests · Activation coverage. · `src/tests/activations_test.rs`
- [NN tests](https://github.com/woues1/tinytorch/blob/master/src/tests/nn_tests/mod.rs): nn tests · Neural-network test suite root. · `src/tests/nn_tests/mod.rs`
- [Linear test](https://github.com/woues1/tinytorch/blob/master/src/tests/nn_tests/linear_test.rs): layer tests · Linear layer coverage. · `src/tests/nn_tests/linear_test.rs`
- [Sequential test](https://github.com/woues1/tinytorch/blob/master/src/tests/nn_tests/sequential_test.rs): composition tests · Sequential container coverage. · `src/tests/nn_tests/sequential_test.rs`
- [MLP test](https://github.com/woues1/tinytorch/blob/master/src/tests/nn_tests/mlp_test.rs): model tests · MLP composition coverage. · `src/tests/nn_tests/mlp_test.rs`
- [Loss tests](https://github.com/woues1/tinytorch/blob/master/src/tests/nn_tests/losses_test.rs): loss tests · Loss function coverage. · `src/tests/nn_tests/losses_test.rs`

## Connections

- lib.rs → Tensor mod: re-exports · Public entry exposes tensor subsystem.
- lib.rs → NN mod: re-exports · Public entry exposes neural-network subsystem.
- lib.rs → Acts mod: re-exports · Public entry exposes activations.
- lib.rs → Losses mod: re-exports · Public entry exposes losses.
- lib.rs → macros.rs: uses · Shared macro helpers support crate implementations.
- Tensor mod → Tensor: contains · Tensor module defines the core tensor type.
- Tensor mod → Init: contains · Initialization helpers live in the tensor layer.
- Tensor mod → Reductions: contains · Reduction primitives live in the tensor layer.
- Tensor mod → Ops mod: supports · Tensor math delegates to lower-level ops.
- Ops mod → Backward: contains · Backward-pass utilities are grouped under ops.
- Tensor → Backward: feeds · Tensor values are the basis for backward utilities.
- NN mod → Layer: defines · Layer trait is the main abstraction boundary.
- NN mod → Linear: contains · Dense learnable layer.
- NN mod → Dropout: contains · Regularization layer.
- NN mod → Sequential: contains · Container for chaining layers.
- NN mod → MLP: contains · Convenience stacked model.
- Linear → Tensor: uses · Linear layer is built on tensors.
- Dropout → Tensor: uses · Dropout operates on tensor values.
- Sequential → Layer: composes · Sequential stores polymorphic layers.
- MLP → Linear: builds from · MLP is assembled from linear blocks.
- MLP → Sequential: uses · MLP relies on sequential composition.
- Acts mod → Activation: defines · Shared activation interface.
- Acts mod → Activations: contains · Concrete nonlinear transforms.
- Activations → Tensor: uses · Activations transform tensor data.
- Losses mod → MSE: contains · Regression loss.
- Losses mod → BCE: contains · Binary classification loss.
- Losses mod → Cross Entropy: contains · Multiclass classification loss.
- MSE → Tensor: uses · Losses consume tensor outputs and targets.
- BCE → Tensor: uses · Losses consume tensor outputs and targets.
- Cross Entropy → Tensor: uses · Losses consume tensor outputs and targets.
- Tests → Tensor tests: includes · Tensor-focused tests.
- Tests → Act tests: includes · Activation-focused tests.
- Tests → NN tests: includes · Neural-network test suite.
- NN tests → Linear test: includes · Linear layer tests.
- NN tests → Sequential test: includes · Sequential tests.
- NN tests → MLP test: includes · MLP tests.
- NN tests → Loss tests: includes · Loss tests.
