# pc003/tensor_alloc architecture

> Architecture diagram of the GitHub repository pc003/tensor_alloc, 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/pc003/tensor_alloc
- Repository: https://github.com/pc003/tensor_alloc

## Overview

tensor_alloc is a CPU tensor-memory allocator with a C API, aligned pooling, safe size-class selection, ownership checks, metrics, and event tracing. Its main workflow validates a tensor request, chooses a capacity, allocates and registers memory, then records metrics and tracing; freeing returns the block to the pool. A separate Python workflow collects and analyzes workload traces, builds training labels, trains a predictor, and evaluates it. The README documents learned prediction in the runtime; some internal wiring beyond the sampled API and predictor sources is not shown.

Main entry points: [`src/api.cpp`](https://github.com/pc003/tensor_alloc/blob/main/src/api.cpp) (Public allocator API), [`src/predictor_interface.h`](https://github.com/pc003/tensor_alloc/blob/main/src/predictor_interface.h) (Predictor interface), [`python/collect_trace.py`](https://github.com/pc003/tensor_alloc/blob/main/python/collect_trace.py) (Workload collection), [`python/build_labels.py`](https://github.com/pc003/tensor_alloc/blob/main/python/build_labels.py) (Label generation)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Allocator runtime"]
  node_api["Public allocator API<br/>[api.cpp]"]
  node_validation["Request validation"]
  node_backend["CPU memory pool<br/>[cpu_backend.cpp]"]
  node_registry[("Allocation registry")]
end

subgraph group_policy["Sizing policy"]
  node_sizeclass["Size classes<br/>[size_class.cpp]"]
  node_predictor["Predictor interface"]
  node_learned["Learned tree inference"]
  node_fallback["Baseline fallback"]
end

subgraph group_observability["Metrics and tracing"]
  node_metrics["Runtime metrics<br/>[metrics.cpp]"]
  node_trace["JSONL trace recorder<br/>[trace_recorder.cpp]"]
end

subgraph group_learning["Trace and learning"]
  node_collector["Workload collection<br/>[collect_trace.py]"]
  node_analyzer["Trace analysis<br/>[analyze_trace.py]"]
  node_labels["Label generation<br/>[build_labels.py]"]
  node_trainer["Predictor training<br/>[train_predictor.py]"]
  node_evaluation["Policy evaluation"]
end

node_caller(("C/C++ caller"))

node_caller -->|"requests allocation"| node_api
node_api -->|"validates request"| node_validation
node_api -->|"selects capacity"| node_predictor
node_predictor -.->|"checks class"| node_sizeclass
node_predictor -.->|"falls back"| node_fallback
node_predictor -.->|"uses learned policy"| node_learned
node_api -->|"allocates or frees"| node_backend
node_api -->|"registers ownership"| node_registry
node_api -->|"records metrics"| node_metrics
node_api -->|"records events"| node_trace
node_caller -->|"generates workload"| node_collector
node_collector -->|"produces trace for"| node_analyzer
node_collector -->|"supplies trace to"| node_labels
node_labels -->|"supplies dataset to"| node_trainer
node_labels -->|"supplies dataset to"| node_evaluation
node_trainer -->|"supplies model to"| node_evaluation
node_trainer -.->|"exports predictor model"| node_learned

click node_api "https://github.com/pc003/tensor_alloc/blob/main/src/api.cpp"
click node_validation "https://github.com/pc003/tensor_alloc/blob/main/src/request_validation.cpp"
click node_backend "https://github.com/pc003/tensor_alloc/blob/main/src/cpu_backend.cpp"
click node_registry "https://github.com/pc003/tensor_alloc/blob/main/src/allocation_registry.cpp"
click node_sizeclass "https://github.com/pc003/tensor_alloc/blob/main/src/size_class.cpp"
click node_predictor "https://github.com/pc003/tensor_alloc/blob/main/src/predictor_interface.h"
click node_learned "https://github.com/pc003/tensor_alloc/blob/main/src/learned_predictor.cpp"
click node_fallback "https://github.com/pc003/tensor_alloc/blob/main/src/predictor_fallback.cpp"
click node_metrics "https://github.com/pc003/tensor_alloc/blob/main/src/metrics.cpp"
click node_trace "https://github.com/pc003/tensor_alloc/blob/main/src/trace_recorder.cpp"
click node_collector "https://github.com/pc003/tensor_alloc/blob/main/python/collect_trace.py"
click node_analyzer "https://github.com/pc003/tensor_alloc/blob/main/python/analyze_trace.py"
click node_labels "https://github.com/pc003/tensor_alloc/blob/main/python/build_labels.py"
click node_trainer "https://github.com/pc003/tensor_alloc/blob/main/python/train_predictor.py"
click node_evaluation "https://github.com/pc003/tensor_alloc/blob/main/python/evaluate_predictor.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_api,node_validation,node_backend,node_registry toneBlue
class node_sizeclass,node_predictor,node_learned,node_fallback toneAmber
class node_metrics,node_trace toneMint
class node_collector,node_analyzer,node_labels,node_trainer,node_evaluation toneRose
class node_caller toneIndigo
```

## Components

### Allocator runtime

- [Public allocator API](https://github.com/pc003/tensor_alloc/blob/main/src/api.cpp): `src/api.cpp`
- [Request validation](https://github.com/pc003/tensor_alloc/blob/main/src/request_validation.cpp): `src/request_validation.cpp`
- [CPU memory pool](https://github.com/pc003/tensor_alloc/blob/main/src/cpu_backend.cpp): `src/cpu_backend.cpp`
- [Allocation registry](https://github.com/pc003/tensor_alloc/blob/main/src/allocation_registry.cpp): `src/allocation_registry.cpp`

### Sizing policy

- [Size classes](https://github.com/pc003/tensor_alloc/blob/main/src/size_class.cpp): `src/size_class.cpp`
- [Predictor interface](https://github.com/pc003/tensor_alloc/blob/main/src/predictor_interface.h): `src/predictor_interface.h`
- [Learned tree inference](https://github.com/pc003/tensor_alloc/blob/main/src/learned_predictor.cpp): `src/learned_predictor.cpp`
- [Baseline fallback](https://github.com/pc003/tensor_alloc/blob/main/src/predictor_fallback.cpp): `src/predictor_fallback.cpp`

### Metrics and tracing

- [Runtime metrics](https://github.com/pc003/tensor_alloc/blob/main/src/metrics.cpp): `src/metrics.cpp`
- [JSONL trace recorder](https://github.com/pc003/tensor_alloc/blob/main/src/trace_recorder.cpp): `src/trace_recorder.cpp`

### Trace and learning

- [Workload collection](https://github.com/pc003/tensor_alloc/blob/main/python/collect_trace.py): `python/collect_trace.py`
- [Trace analysis](https://github.com/pc003/tensor_alloc/blob/main/python/analyze_trace.py): `python/analyze_trace.py`
- [Label generation](https://github.com/pc003/tensor_alloc/blob/main/python/build_labels.py): `python/build_labels.py`
- [Predictor training](https://github.com/pc003/tensor_alloc/blob/main/python/train_predictor.py): `python/train_predictor.py`
- [Policy evaluation](https://github.com/pc003/tensor_alloc/blob/main/python/evaluate_predictor.py): `python/evaluate_predictor.py`

### Other components

- **C/C++ caller**

## Connections

- C/C++ caller → Public allocator API: requests allocation (evidence: `README.md`)
- Public allocator API → Request validation: validates request (evidence: `src/api.cpp`)
- Public allocator API → Predictor interface: selects capacity (evidence: `src/api.cpp`)
- Predictor interface → Size classes: checks class
- Predictor interface → Baseline fallback: falls back
- Predictor interface → Learned tree inference: uses learned policy (evidence: `README.md`)
- Public allocator API → CPU memory pool: allocates or frees (evidence: `src/api.cpp`)
- Public allocator API → Allocation registry: registers ownership (evidence: `src/api.cpp`)
- Public allocator API → Runtime metrics: records metrics (evidence: `src/api.cpp`)
- Public allocator API → JSONL trace recorder: records events (evidence: `src/api.cpp`)
- C/C++ caller → Workload collection: generates workload (evidence: `README.md`)
- Workload collection → Trace analysis: produces trace for (evidence: `README.md`)
- Workload collection → Label generation: supplies trace to (evidence: `README.md`)
- Label generation → Predictor training: supplies dataset to (evidence: `README.md`)
- Label generation → Policy evaluation: supplies dataset to (evidence: `README.md`)
- Predictor training → Policy evaluation: supplies model to (evidence: `README.md`)
- Predictor training → Learned tree inference: exports predictor model (evidence: `README.md`)
