Architecture
This document describes the system architecture, module design, and extension points of TensorCraft-HPC.
Design Philosophy
TensorCraft-HPC follows three core principles:
- Readability first — Code is written to be read; each kernel demonstrates the optimization progression.
- Header-only — Zero build complexity for C++ users; include and go.
- OpenSpec-driven — Active work starts in
openspec/changes/, while accepted baselines live inopenspec/specs/.
System Architecture
Directory Structure
modern-ai-kernels/
├── include/tensorcraft/ # Header-only library
│ ├── core/ # Utilities
│ │ ├── cuda_check.hpp # CUDA error checking
│ │ ├── features.hpp # Compile-time GPU detection
│ │ └── type_traits.hpp # Type utilities
│ ├── memory/ # Memory management
│ │ ├── tensor.hpp # RAII GPU tensor
│ │ └── memory_pool.hpp # Optional pooling
│ └── kernels/ # All compute kernels
│ ├── gemm.hpp # Matrix multiplication
│ ├── attention.hpp # Attention mechanisms
│ ├── normalization.hpp # LayerNorm, RMSNorm
│ ├── softmax.hpp # Softmax variants
│ ├── conv2d.hpp # 2D convolution
│ ├── sparse.hpp # Sparse operations
│ └── fusion.hpp # Fused operators and quantization helpers
├── src/python_ops/ # Python bindings (pybind11)
├── tests/ # Unit tests (GoogleTest)
├── benchmarks/ # Performance benchmarks
├── docs/ # VitePress documentation
└── openspec/ # Specification workflow
├── specs/ # Accepted specifications
├── changes/ # Active change proposals
└── archive/ # Completed changesGEMM Optimization Path
The GEMM kernel demonstrates the progressive optimization approach:
Performance Characteristics
| Stage | Memory Traffic | Compute Efficiency | Relative Speed |
|---|---|---|---|
| Naive | O(N³) global | ~1% | 1x |
| Tiled | O(N²) global | ~10% | 10x |
| Double Buffer | O(N²) global | ~30% | 30x |
| Tensor Core | O(N²) global | ~80% | 80x |
FlashAttention Implementation
Key Innovations
- Tiled computation — Process attention blocks that fit in SRAM.
- Online softmax — Incrementally update softmax statistics.
- Recomputation — Recompute attention weights rather than storing them.
Memory Management
RAII Pattern
cpp
// Automatic memory management
{
tensorcraft::FloatTensor A({4096, 4096});
// use A...
} // Released automatically when scope exitsMemory Pool (Optional)
Compile-Time Feature Detection
features.hpp provides compile-time GPU capability detection:
cpp
// Automatically detected at compile time
#if TENSORCRAFT_HAS_WMMA
// Use Tensor Cores (SM70+)
#endif
#if TENSORCRAFT_HAS_FP8
// Use FP8 types (SM90+)
#endif
#if TENSORCRAFT_HAS_TMA
// Use Tensor Memory Accelerator (SM90+)
#endifOpenSpec Workflow
Spec Structure
Each accepted baseline in openspec/specs/ contains:
- Requirements — What the component must do.
- Contracts — API guarantees and invariants.
- Acceptance Criteria — How to verify compliance.
Extension Points
Adding a New Kernel
- Create a spec proposal in
openspec/changes/. - Implement the header in
include/tensorcraft/kernels/. - Add GoogleTest unit tests.
- Add performance benchmarks.
- Update documentation.
- Review the work against that active change.
- Promote the accepted baseline to
openspec/specs/.
Adding Python Bindings
cpp
// src/python_ops/bindings.cpp
m.def("my_kernel", &tensorcraft::kernels::my_kernel,
"A new kernel",
py::arg("input"),
py::arg("output"));