How woues1/tinytorch works
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: tensor.rs Tensor · nn/mod.rs NN mod · lib.rs lib.rs · tensor/mod.rs Tensor mod
Components 29
Public API
Crate entry and shared implementation macros.
Tensor Core
Core tensor representation and numerical primitives.
- Tensor mod · Tensor subsystem entry point.
src/tensor/mod.rs - Tensor · Core tensor data structure and shape/stride handling.
src/tensor/tensor.rs - Init · Array and weight initialization helpers.
src/tensor/init.rs - Reductions · Sum, max, mean, and related reduction primitives.
src/tensor/reductions.rs
Ops
Internal reusable math and backward-pass helpers.
Neural Network
Composable layers and model containers.
- NN mod · Neural-network layer and composition entry point.
src/nn/mod.rs - Layer · Polymorphic layer interface and extension point.
src/nn/layer.rs - Linear · Fully connected learnable layer.
src/nn/linear.rs - Dropout · Dropout regularization layer.
src/nn/dropout.rs - Sequential · Layer chain composition.
src/nn/sequential.rs - MLP · Convenience stacked multi-layer perceptron.
src/nn/mlp.rs
Functions
Activations and loss functions built on tensors.
- Acts mod · Activation subsystem entry point.
src/activations/mod.rs - Activation · Shared activation interface.
src/activations/activation.rs - Activations · ReLU, sigmoid, tanh, softmax, log-softmax, GELU.
src/activations/activations.rs - Losses mod · Loss subsystem entry point.
src/losses/mod.rs - MSE · Mean squared error objective.
src/losses/mse.rs - BCE · Binary cross entropy objective.
src/losses/binary_cross_entropy.rs - Cross Entropy · Cross entropy objective.
src/losses/cross_entropy.rs
Tests
Subsystem-focused test coverage.
- Tests · Test module root.
src/tests/mod.rs - Tensor tests · Tensor behavior coverage.
src/tests/tensor_tests.rs - Act tests · Activation coverage.
src/tests/activations_test.rs - NN tests · Neural-network test suite root.
src/tests/nn_tests/mod.rs - Linear test · Linear layer coverage.
src/tests/nn_tests/linear_test.rs - Sequential test · Sequential container coverage.
src/tests/nn_tests/sequential_test.rs - MLP test · MLP composition coverage.
src/tests/nn_tests/mlp_test.rs - Loss tests · Loss function coverage.
src/tests/nn_tests/losses_test.rs
Connections 37
- 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.