UPCAST and AVX2
Background
One of tinygrad's AST axis optimizations is UPCAST (the yellow axes in DEBUG=2 output). To UPCAST an axis is to perform some amount of unrolling in the rendered code (thus increasing the amount of work done per core / thread). Once some of the operations have been unrolled, depending on the contiguity and alignment of the data, the loads and stores can also be vectorized.
Upcasting is applied as a SPLIT OptOp, taking in the axis to apply the upcast to as well as the amount to upcast. This would apply an UPCAST of 4 elements to the second axis:
k.apply_opt(Opt(OptOps.SPLIT, 1, (4, AxisType.UPCAST)))
Applying this just modifies the axis in the AST (by creating an axis of type UPCAST), so at this stage it isn't yet determined whether this will remain scalar unrolling or become vectorized. Next, the expander and devectorize stages run to completely unroll all the instructions into scalar loads and stores, offset by their index (e.g., idx+0, idx+1, idx+2, ...).
memory_coalescing, which runs after, controls what UOp patterns actually get vectorized. This is done by grouping all LOAD/STORE UOps by their buffer and address and identifying cases where multiple such UOps are reading from or writing to a contiguous subslice of the buffer. On a 4-element AXPY kernel, going into the memory coalescing rewrite, the uir is:
%0 = param 4 slot=0 dtype=f32 device=CPU
%1 = index %0 0
%2 = param 4 slot=1 dtype=f32 device=CPU
%3 = index %2 0
%4 = load %3
%5 = mul 2.0 %4
%6 = param 4 slot=2 dtype=f32 device=CPU
%7 = index %6 0
%8 = load %7
%9 = add %5 %8
%10 = store %1 %9
%11 = index %0 1
%12 = index %2 1
%13 = load %12
%14 = mul 2.0 %13
%15 = index %6 1
%16 = load %15
%17 = add %14 %16
%18 = store %11 %17
%19 = index %0 2
%20 = index %2 2
%21 = load %20
%22 = mul 2.0 %21
%23 = index %6 2
%24 = load %23
%25 = add %22 %24
%26 = store %19 %25
%27 = index %0 3
%28 = index %2 3
%29 = load %28
%30 = mul 2.0 %29
%31 = index %6 3
%32 = load %31
%33 = add %30 %32
%34 = store %27 %33
...
%8, %16, %24, and %32 are taking indices 0–3 of %6, which is a buffer of length 4. We can then rewrite this graph with a single UOp, %x = shrink %6 0 4, which creates a 4-element contiguous slice of values from %6 starting at offset 0. A subsequent load on %x will actually load these 4 values. The corresponding operation for STORE replaces %10, %18, %26, and %34 by stacking the values being stored (%x = stack %9 %17 %25 %33) and then executing a single store into shrink %0 0 4 taking %x as the second argument.
After this rewrite, the new uir is:
%0 = param 4 slot=0 dtype=f32 device=CPU
%1 = shrink %0 0 4
%2 = param 4 slot=1 dtype=f32 device=CPU
%3 = shrink %2 0 4
%4 = load %3
%5 = index %4 0
%6 = mul 2.0 %5
%7 = param 4 slot=2 dtype=f32 device=CPU
%8 = shrink %7 0 4
%9 = load %8 ; NOTE: vectorized load
%10 = index %9 0
%11 = add %6 %10
%12 = index %4 1
%13 = mul 2.0 %12
%14 = index %9 1
%15 = add %13 %14
%16 = index %4 2
%17 = mul 2.0 %16
%18 = index %9 2
%19 = add %17 %18
%20 = index %4 3
%21 = mul 2.0 %20
%22 = index %9 3
%23 = add %21 %22
%24 = stack %11 %15 %19 %23
%25 = store %1 %24 ; NOTE: vectorized store
...
AVX2
Almost every modern x86 CPU supports AVX2, which has 256-bit vector registers, allowing arithmetic operations on 8 floats at a time. While tinygrad's CPU backend allows passing in the required feature flags to enable AVX2 (e.g., DEV=CPU:CLANG:x86_64,znver3,avx2), the renderer and codegen paths on CPU only support vectors of up to 4 floats (128-bit ops or SSE). On high arithmetic intensity operations like GEMMs, we would expect to see double the throughput from using AVX2 (i.e., VFMADD132PS operating on 128-bit or 256-bit registers has 4 cycles of latency and a reciprocal throughput of 0.5, so enabling AVX2 gives twice the throughput).
Making these changes was fairly simple overall. There are two paths where this is handled, as the BEAM and non-BEAM (heuristic) optimization paths need separate treatment for identifying upcastable axes. You can see the diff here. We can confirm that this works by inspecting the names of the rendered kernels or by reading the disassembly.
For example, running this from the above branch shows that we have correctly applied an UPCAST of length 8:
VIZ=1 DEBUG=2 DEV=CPU:CLANG:x86_64,znver3,avx2 python3 -c "from tinygrad import Tensor, TinyJit
@TinyJit
def saxpy(a, x,y):
return (a*x + y).realize()
a = 2.0
x = Tensor.empty((1 << 23)).contiguous()
y = Tensor.empty((1 << 23)).contiguous()
saxpy(a,x,y)"

Inspecting the disassembly ("View Disassembly" in the profiler) shows that the compiled code is using 256-bit vector instructions:

This isn't the best example to show the improved performance from AVX2, since AXPY doesn't benefit from wider loads as both the 128-bit and 256-bit values are loaded from DRAM in a single cache line and the arithmetic intensity is too low for wider vector operations to matter.
A better example for showing the performance improvement is with a GEMM, since for large enough matrices it is FLOP-bound, so the 8-float UPCASTs help with performance. Benchmarking square matrix GEMMs from 128×128 to 768×768 in increments of 32 shows that the AVX2 UPCAST codegen achieves a 1.78x geomean speedup over master. Unfortunately, both of these results still lag behind NumPy/OpenBLAS's hand-tuned assembly GEMMs:
