HeadlinesBriefing favicon HeadlinesBriefing.com

Building a Tensor Library from Scratch in C

Hacker News •
×

This post details the construction of a complete, accelerated tensor library in C, inspired by Bellard's unpublished libnc. Tensors are represented as flat arrays paired with metadata—shape, strides, and dimension count—enabling efficient multi-dimensional indexing without repeated calculations. A reference-counted ownership model manages memory, supporting views, transposes, and reshapes without data duplication. Elementwise operations (unary and binary) are implemented via generic loops over the flattened data, covering activations like ReLU, sigmoid, and tanh, plus arithmetic ops. Broadcasting is deliberately omitted for simplicity, assuming aligned tensor shapes in target models.

Performance demands push the design toward GPU acceleration. The author evaluates vendor APIs—CUDA, OpenCL, Vulkan, Metal, WebGPU—and selects Metal for its unified memory on Apple Silicon and runtime kernel compilation via MSL. Kernels are embedded as C strings, allowing straightforward offloading of elementwise ops like ReLU. The piece argues that even optimized CPU paths with BLAS and OpenMP cannot match GPU throughput, making accelerator support essential for modern neural network workloads.