Logo Sugumaran B.
Writing / Engineering Log
First-Principles Engineering

Fundamentals to Frontier AI: An Engineering Odyssey

A systematic public engineering log decomposing computer systems from the ground up: C memory allocators, cache coherence, and Linux event loops, bridging directly into modern inference serving, GPU memory management, and autonomous agent swarms.

5
Core Phases
Week 1
Active Module
100%
Open Source
Zero
Black Boxes
PHASE 1 • WEEK 1 Active Build

Memory Architecture & Custom Allocators

Before we can understand GPU VRAM bottlenecks, KV-cache paging, or high-throughput LLM serving, we trace how operating systems allocate and address physical and virtual memory.

C11 Virtual Memory AddressSanitizer PyTorch CUDA Allocator Mechanical Sympathy

Day 1: Virtual Memory Layout & Struct Alignment

✓ Completed

Mapped the 64-bit Linux process virtual address space (Text, Data, BSS, Heap, Stack) and measured the mechanical penalty of memory alignment and silent compiler struct padding.

The Silicon-to-Synapse Bridge (Why this matters for AI):

Tensors in PyTorch/CUDA are contiguous blocks aligned to 256-byte or 512-byte boundaries for vectorized memory coalescing. When activation tensors and KV-cache blocks fragment memory, an inference pipeline throws "CUDA: Out of Memory" even when nvidia-smi shows gigabytes of total free VRAM. Understanding allocator coalescence is the bedrock of GPU optimisation.

// Comparing unpadded vs optimised struct layout: struct UnpaddedStruct { char a; double b; int c; char d; }; // 24 bytes (10 bytes padding overhead) struct OptimisedStruct { double b; int c; char a; char d; }; // 16 bytes (33% memory saved)

Day 2: Raw Syscalls & The Program Break: Bump Allocator (9.5 ns vs 169.7 ns glibc)

✓ Completed

Queried and expanded the Linux heap boundary via sbrk(), enforcing 16-byte bitwise alignment to build an ultra-low-latency $O(1)$ Bump/Arena Allocator. Benchmarked 1,000,000 allocations against standard glibc malloc.

The Silicon-to-Synapse Bridge (Why this matters for AI):

High-throughput LLM serving runtimes (vLLM, TensorRT-LLM) cannot call dynamic kernel allocators during active token generation. Intermediate activation tensors only live for the duration of a single forward pass layer. By using pre-allocated static scratchpad arenas, allocations complete in 9 nanoseconds and arena reset requires resetting an offset pointer (1 CPU cycle).

// 16-byte bitwise alignment & O(1) bump allocation #define ALIGN_UP(size) (((size) + 15) & ~15) void *ptr = arena->buffer + arena->offset; arena->offset += ALIGN_UP(size); // 9.48 ns per allocation; 17.9x faster than glibc sequential malloc.

Day 3: In-Band Metadata Headers & Linked-List Free Lists (malloc / free)

✓ Completed

Bump allocators are fast but cannot free individual chunks. Built a general-purpose Free-List Allocator prepending every chunk with a 16-byte aligned BlockHeader. Implemented First-Fit search, dynamic block splitting to kill internal fragmentation, and immediate neighbor coalescing.

The Silicon-to-Synapse Bridge (Why this matters for AI):

In autoregressive LLMs, caching Key-Value vectors traditionally wasted 60% to 80% of GPU VRAM due to rigid contiguous over-allocation. vLLM solved this through PagedAttention: managing GPU memory using a Free List of fixed 16-token physical blocks. By allocating blocks dynamically and returning freed blocks on request completion, VRAM waste plummeted under 4%, multiplying real-world serving throughput by 2x to 4x.

// In-band metadata header enables O(1) deallocation: BlockHeader *header = (BlockHeader *)((uint8_t *)ptr - HEADER_SIZE); header->is_free = 1; // Direct offset access without hash lookups or lock contention.

Upcoming Week 1 Sprints:

  • Day 4: External Fragmentation & Boundary-Tag Bidirectional Coalescence
  • Day 5: High-Throughput Microbenchmarks: Custom Allocator vs glibc ptmalloc
  • Weekend Capstone: The Silicon-to-Synapse Architecture Synthesis Essay (Substack & Medium)