# chatelo/machine-learning-in-rust-from-scratch architecture

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

- Interactive diagram: https://gitdiagram.com/chatelo/machine-learning-in-rust-from-scratch
- Repository: https://github.com/chatelo/machine-learning-in-rust-from-scratch

## Overview

This is a chapter-based Rust machine-learning project. Its current focus is loading SmolLM2 with Candle, checking it against a reference, generating text, scoring it on Shakespeare, and fine-tuning it; earlier chapters also provide tabular and tweet-sentiment workflows plus HTTP predictions. The shown `main` dispatches several chapter commands, while the README documents the LLM stages. Many earlier model implementations were not excerpted, so their internal calls are not inferred here.

Main entry points: [`src/main.rs`](https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/main.rs) (Stage dispatcher), [`src/server.rs`](https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/server.rs) (Prediction API), [`src/llm.rs`](https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/llm.rs) (SmolLM2 runtime), [`src/metrics.rs`](https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/metrics.rs) (Model metrics)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entry and serving"]
  node_stages["Stage dispatcher<br/>[main.rs]"]
  node_http_api["Prediction API<br/>[server.rs]"]
end

subgraph group_data["Data preparation"]
  node_tabular["Heart data prep<br/>[data.rs]"]
  node_tweet_data["Tweet preprocessing<br/>[text.rs]"]
  node_explore["Data exploration<br/>[explore.rs]"]
  node_heart_data["Framingham files<br/>[framingham.csv]"]
  node_shakespeare["Shakespeare text<br/>[shakespeare.txt]"]
end

subgraph group_models["Prediction models"]
  node_linear["Linear regression<br/>[linear.rs]"]
  node_logistic["Logistic regression<br/>[logistic.rs]"]
  node_tree["Decision tree<br/>[tree.rs]"]
  node_forest["Random forest<br/>[forest.rs]"]
  node_svm["Tweet SVM<br/>[svm.rs]"]
  node_bayes["Naive Bayes<br/>[bayes.rs]"]
  node_knn["Patient k-NN<br/>[knn.rs]"]
  node_neural["Neural networks<br/>[nn.rs]"]
  node_model_files[("Saved model files<br/>[catalog.json]")]
end

subgraph group_language["Language models"]
  node_tiny_gpt["Tiny GPT<br/>[gpt.rs]"]
  node_smollm["SmolLM2 runtime<br/>[llm.rs]"]
  node_weights["Pretrained weights<br/>[gpt_model.json]"]
end

subgraph group_evaluation["Evaluation"]
  node_metrics["Model metrics<br/>[metrics.rs]"]
end

node_cli(("CLI user"))
node_http(("HTTP client"))

node_cli -->|"selects stage"| node_stages
node_stages -->|"runs exploration"| node_explore
node_stages -->|"runs clean or split"| node_tabular
node_stages -->|"runs training"| node_linear
node_stages -->|"dispatches stages"| node_logistic
node_stages -->|"starts serving"| node_http_api
node_stages -->|"dispatches stages"| node_tree
node_stages -->|"dispatches stages"| node_forest
node_stages -->|"dispatches stages"| node_tweet_data
node_stages -->|"dispatches stages"| node_neural
node_stages -->|"dispatches stages"| node_tiny_gpt
node_stages -->|"dispatches LLM stages"| node_smollm
node_http -.->|"sends requests"| node_http_api
node_http_api -->|"predicts with"| node_logistic
node_http_api -->|"predicts with"| node_tree
node_http_api -->|"predicts with"| node_forest
node_http_api -->|"generates with"| node_tiny_gpt
node_http_api -->|"generates with"| node_smollm
node_forest -->|"grows trees"| node_tree
node_smollm -->|"compares against"| node_tiny_gpt
node_smollm -.->|"trains and scores"| node_shakespeare
node_smollm -.->|"loads model weights"| node_weights
node_tabular -->|"cleans and splits"| node_heart_data
node_tabular -.->|"supports evaluation"| node_metrics
node_tweet_data -->|"vectorizes text"| node_svm
node_logistic -->|"scores predictions"| node_metrics
node_tree -->|"scores predictions"| node_metrics
node_forest -->|"scores predictions"| node_metrics
node_neural -->|"scores predictions"| node_metrics
node_model_files -.->|"stores versions"| node_svm
node_stages -.->|"dispatches stages"| node_bayes
node_stages -.->|"dispatches stages"| node_knn
node_tabular -.->|"supplies features"| node_logistic

click node_stages "https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/main.rs"
click node_http_api "https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/server.rs"
click node_tabular "https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/data.rs"
click node_tweet_data "https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/text.rs"
click node_explore "https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/explore.rs"
click node_linear "https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/linear.rs"
click node_logistic "https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/logistic.rs"
click node_tree "https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/tree.rs"
click node_forest "https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/forest.rs"
click node_svm "https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/svm.rs"
click node_bayes "https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/bayes.rs"
click node_knn "https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/knn.rs"
click node_neural "https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/nn.rs"
click node_metrics "https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/metrics.rs"
click node_tiny_gpt "https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/gpt.rs"
click node_smollm "https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/llm.rs"
click node_heart_data "https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/data/framingham.csv"
click node_shakespeare "https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/data/shakespeare.txt"
click node_model_files "https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/models/svm/catalog.json"
click node_weights "https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/gpt_model.json"

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_stages,node_http_api,node_cli,node_http toneBlue
class node_tabular,node_tweet_data,node_explore,node_heart_data,node_shakespeare toneAmber
class node_linear,node_logistic,node_tree,node_forest,node_svm,node_bayes,node_knn,node_neural,node_model_files toneMint
class node_tiny_gpt,node_smollm,node_weights toneRose
class node_metrics toneIndigo
```

## Components

### Entry and serving

- [Stage dispatcher](https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/main.rs): `src/main.rs`
- [Prediction API](https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/server.rs): `src/server.rs`

### Data preparation

- [Heart data prep](https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/data.rs): `src/data.rs`
- [Tweet preprocessing](https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/text.rs): `src/text.rs`
- [Data exploration](https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/explore.rs): `src/explore.rs`
- [Framingham files](https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/data/framingham.csv): `data/framingham.csv`
- [Shakespeare text](https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/data/shakespeare.txt): `data/shakespeare.txt`

### Prediction models

- [Linear regression](https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/linear.rs): `src/linear.rs`
- [Logistic regression](https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/logistic.rs): `src/logistic.rs`
- [Decision tree](https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/tree.rs): `src/tree.rs`
- [Random forest](https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/forest.rs): `src/forest.rs`
- [Tweet SVM](https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/svm.rs): `src/svm.rs`
- [Naive Bayes](https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/bayes.rs): `src/bayes.rs`
- [Patient k-NN](https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/knn.rs): `src/knn.rs`
- [Neural networks](https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/nn.rs): `src/nn.rs`
- [Saved model files](https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/models/svm/catalog.json): `models/svm/catalog.json`

### Language models

- [Tiny GPT](https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/gpt.rs): `src/gpt.rs`
- [SmolLM2 runtime](https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/llm.rs): `src/llm.rs`
- [Pretrained weights](https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/gpt_model.json): `gpt_model.json`

### Evaluation

- [Model metrics](https://github.com/chatelo/machine-learning-in-rust-from-scratch/blob/main/src/metrics.rs): `src/metrics.rs`

### Other components

- **CLI user**
- **HTTP client**

## Connections

- CLI user → Stage dispatcher: selects stage (evidence: `src/main.rs`)
- Stage dispatcher → Data exploration: runs exploration (evidence: `src/main.rs`)
- Stage dispatcher → Heart data prep: runs clean or split (evidence: `src/main.rs`)
- Stage dispatcher → Linear regression: runs training (evidence: `src/main.rs`)
- Stage dispatcher → Logistic regression: dispatches stages (evidence: `src/main.rs`)
- Stage dispatcher → Prediction API: starts serving (evidence: `src/main.rs`)
- Stage dispatcher → Decision tree: dispatches stages (evidence: `src/main.rs`)
- Stage dispatcher → Random forest: dispatches stages (evidence: `src/main.rs`)
- Stage dispatcher → Tweet preprocessing: dispatches stages (evidence: `src/main.rs`)
- Stage dispatcher → Neural networks: dispatches stages (evidence: `src/main.rs`)
- Stage dispatcher → Tiny GPT: dispatches stages (evidence: `src/main.rs`)
- Stage dispatcher → SmolLM2 runtime: dispatches LLM stages (evidence: `src/main.rs`)
- HTTP client → Prediction API: sends requests (evidence: `README.md`)
- Prediction API → Logistic regression: predicts with (evidence: `src/server.rs`)
- Prediction API → Decision tree: predicts with (evidence: `src/server.rs`)
- Prediction API → Random forest: predicts with (evidence: `src/server.rs`)
- Prediction API → Tiny GPT: generates with (evidence: `src/server.rs`)
- Prediction API → SmolLM2 runtime: generates with (evidence: `src/server.rs`)
- Random forest → Decision tree: grows trees (evidence: `src/forest.rs`)
- SmolLM2 runtime → Tiny GPT: compares against (evidence: `src/llm.rs`)
- SmolLM2 runtime → Shakespeare text: trains and scores (evidence: `README.md`)
- SmolLM2 runtime → Pretrained weights: loads model weights (evidence: `README.md`)
- Heart data prep → Framingham files: cleans and splits (evidence: `src/data.rs`)
- Heart data prep → Model metrics: supports evaluation
- Tweet preprocessing → Tweet SVM: vectorizes text (evidence: `src/svm.rs`)
- Logistic regression → Model metrics: scores predictions (evidence: `src/logistic.rs`)
- Decision tree → Model metrics: scores predictions (evidence: `src/tree.rs`)
- Random forest → Model metrics: scores predictions (evidence: `src/forest.rs`)
- Neural networks → Model metrics: scores predictions (evidence: `src/nn.rs`)
- Saved model files → Tweet SVM: stores versions
- Stage dispatcher → Naive Bayes: dispatches stages (evidence: `src/main.rs`)
- Stage dispatcher → Patient k-NN: dispatches stages (evidence: `src/main.rs`)
- Heart data prep → Logistic regression: supplies features (evidence: `src/logistic.rs`)
