netcl: OpenCL Deep Learning from First Principles
netcl: OpenCL Deep Learning from First Principles
netcl is a high-performance deep learning framework built from scratch on PyOpenCL. It runs on GPUs, CPUs, and accelerators from any hardware vendor supporting OpenCL 1.2 or 2.0: AMD Radeon, Intel Arc, Apple Silicon, NVIDIA GeForce, ARM Mali, and embedded SoCs. There is no CUDA dependency anywhere in the codebase.
On top of low-level OpenCL memory buffers, netcl provides a dynamic Tape-based autograd engine, a runtime JIT compiler that fuses elementwise operations into single kernel launches, native mixed precision with automatic FP16 detection, and a host-staged distributed training engine for multi-GPU workstations.
- Guided Tracks: Step-by-step walkthroughs from first principles in Learning Tracks.
- Technical Reference: Symbol signatures and hardware specifications in API Index and Architecture Manual.
Core Conceptual Framework (4 Fundamental Premises)
If you are coming from PyTorch, TensorFlow, or JAX, netcl differs in four foundational ways:
Explicit Hardware Wire"] --> T["2. Tensor vs Node
cl_mem vs Tape Graph"] T --> F["3. Fused Kernels
Local Memory Tiling"] F --> M["4. Multi-GPU
Host Staged Ring Reduction"]
- The Queue is the Physical Wire:
PyTorch has an implicit global CUDA device context (
x.cuda()). OpenCL does not. Every tensor creation, memory copy, and kernel execution requires an explicit commandqueue. The queue is your direct pipe to GPU hardware cores. - Tensor is Raw Memory; Node is the Autograd Graph:
In netcl,
netcl.Tensoris purely a wrapper around device memory (cl_mem) with shapes and strides. It stores zero gradient history. To compute gradients, wrap your tensors withag.tensor(x)inside awith ag.Tape() as tape:block. This creates graphNodeobjects that the tape tracks for reverse-mode automatic differentiation. - Device-Resident & Fused Kernels: Consumer GPUs often have 8 GB to 16 GB of VRAM. Naive operations that materialize full $(N \times K)$ distance matrices (clustering) or $(S \times S)$ attention maps (transformers) trigger out-of-memory errors. NetCL fuses reductions, online Softmax, and elementwise operations directly into GPU local memory and registers.
- Universal Workstation Multi-GPU: Multi-GPU setups in datacenter clusters rely on expensive NVLink bridges and NCCL. NetCL enables distributed data-parallel training across mismatched, commodity GPUs (such as an AMD Radeon combined with an Intel Arc) on a single workstation using shared-memory multiprocessing and host-based all-reduce.
30-Second Example: Device, Model & Training Step
The snippet below demonstrates device discovery, tensor creation, a forward pass, backpropagation through the tape, and an optimizer update:
import numpy as np
import netcl.autograd as ag
from netcl.core.device import manager
from netcl.core.tensor import Tensor
from netcl.nn import Linear, ReLU, Sequential
from netcl.optim import AdamW
# 1. Connect to the hardware (auto-detects GPU, falls back to CPU)
dev = manager.default("auto")
q = dev.queue
print(f"Active Device: {dev.device_name} ({dev.backend})")
# 2. Build a neural network
model = Sequential(
Linear(q, 784, 256), ReLU(),
Linear(q, 256, 128), ReLU(),
Linear(q, 128, 10),
)
opt = AdamW(model.parameters(), lr=1e-3, weight_decay=1e-4)
# 3. Create raw device tensors (synthetic batch: 32 samples, 10 classes)
x_raw = Tensor.from_host(q, np.random.randn(32, 784).astype(np.float32))
y_raw = Tensor.from_host(q, np.random.randint(0, 10, size=(32,)).astype(np.int32))
# 4. Record forward operations on the Autograd Tape
with ag.Tape() as tape:
# ag.tensor() wraps raw Tensors into Autograd Nodes
x_node = ag.tensor(x_raw)
y_node = ag.tensor(y_raw)
logits = model(x_node)
loss = ag.cross_entropy(logits, y_node)
# 5. Reverse-mode automatic differentiation and optimizer step
tape.backward(loss)
opt.step()
opt.zero_grad()
print("Step completed. Batch loss:", loss.value.to_host()[0])
Start Learning: The 7 Guided Curriculum Paths
Explore the complete, first-principles deep learning knowledge curriculum tailored to your background:
- Curriculum Hub & Learning Paths: Overview of all 7 learning tracks.
- Path 1: Absolute Beginner: From curve fitting, vectors, and calculus to training your first neural network.
- Path 2: PyTorch Practitioner: Fast-track transition covering command queues, tape mechanics, and data-parallelism.
- Path 3: LLMs & Transformers: BPE tokenization, Rotary Embeddings (RoPE), and FlashAttention tiling.
- Path 4: Vision & Convolutions: 2D locality,
im2col, Winograd algebra, and ResNet skip connections. - Path 5: Unsupervised & Clustering: KMeans, Graph Laplacians, LOBPCG eigensolvers, and self-supervised learning (SimCLR, SwAV).
- Path 6: Evolutionary Algorithms: Black-box optimization, Natural Evolution Strategies (NES), and Neural Architecture Search (NAS).
- Path 7: Systems & Kernel Hacker: OpenCL memory pools, JIT compilation, and writing custom OpenCL kernels.
Feature Matrix
Device and Tensor Layer
- Tensor: Device buffer wrapped over
cl_memwith integrated BufferPool, supporting FP16, FP32, and FP64 data types. - OpenCLBackend: Asynchronous command queues, pinned host memory staging, thread-safe execution, and clean error reporting.
- DeviceManager: Discovers all OpenCL platforms and devices on the system, exposing them via standardized
DeviceHandleobjects.
Compute and Operations
- Accelerated Ops: High-throughput matrix multiplication, 2D convolutions (im2col and Winograd), pooling, and BatchNorm2d.
- Fused Kernels: Single-launch fused primitives including
linear+relu,conv+relu,batchnorm+relu, andmatmul+bias+relu. - JIT Compiler: Traces elementwise computational subgraphs and emits unified OpenCL C programs at runtime.
Training & Architecture
- Autograd Tape: Dynamic tape-based reverse-mode automatic differentiation with topological graph sorting.
- Optimizers: Fused GPU implementations of SGD, Momentum, Adam, AdamW, RMSProp, and learning rate schedulers.
- Transformers & Modern LM: RMSNorm, Rotary Positional Embeddings (RoPE), SwiGLU activations, KV caching, and streaming FlashAttention.
- Tokenization & Chat: Byte-Pair Encoding (
BPETokenizer) with regex pre-splitting and multi-turn chat templates (ChatFormat). - Distributed Engine: Multi-process data-parallelism with shared-memory host reduction and device-side pairwise sums.
Where to Next
- Quickstart Guide: Install, run the smoke test, and train a model in 5 minutes.
- First Neural Network (MNIST): Build an end-to-end digit classification pipeline with dataset loading and checkpointing.
- Understanding Autograd: Deep dive into the Tape mechanism, node wrapping, and custom autograd operations.
- Frequently Asked Questions: Common questions about device selection, performance, and OpenCL compatibility.