acn-accel
ACN-Accel is a hybrid Python + C + Assembly project with AVX2 acceleration for core neural-network primitives.
The runtime chooses between baseline C, AVX2 intrinsics, and AVX2 assembly implementations based on:
- CPU feature availability (AVX2)
- input size threshold
Implemented Ops
Section titled “Implemented Ops”vector_add(float* a, float* b, float* out, int n)relu(float* x, float* out, int n)matmul(float* A, float* B, float* C, int M, int N, int K)
All tensors use row-major layout.
Architecture
Section titled “Architecture”Python API (ctypes) | v libacn (shared) | +--> adaptive.c (dispatcher + AVX2 detection) | | | +--> simd_avx2_asm.S (AVX2 assembly kernel) | | +--> simd_avx2.c (AVX2 intrinsics) | +--> core.c (baseline C)Adaptive Execution Logic
Section titled “Adaptive Execution Logic”- Runtime AVX2 detection:
acn_cpu_has_avx2() - Dispatch rules:
vector_add: ASM path whenn >= 2048, otherwise AVX2 intrinsics forn >= 256(if AVX2 is available)relu: AVX2 path whenn >= 256and AVX2 is availablematmul: AVX2 path whenM*N*K > 100000and AVX2 is available- otherwise baseline C path
Build Instructions
Section titled “Build Instructions”1. Configure + build
Section titled “1. Configure + build”cmake -S . -B build -DCMAKE_BUILD_TYPE=Releasecmake --build build -jThis produces shared library:
- Linux:
build/libacn.so - macOS:
build/libacn.dylib(or equivalent by generator)
2. Python environment
Section titled “2. Python environment”Install dependencies:
python -m pip install numpy pytest3. Run tests
Section titled “3. Run tests”PYTHONPATH=python ACN_LIB_PATH=build/libacn.so pytest -qOn macOS use .dylib path in ACN_LIB_PATH.
4. Run benchmark
Section titled “4. Run benchmark”PYTHONPATH=python ACN_LIB_PATH=build/libacn.so python python/benchmark.pyOn macOS use .dylib path in ACN_LIB_PATH.
Benchmark Output Example
Section titled “Benchmark Output Example”[ACN Benchmark]
Platform: Linux-...CPU: AVX2 = True
Matrix 512x512:NumPy: 120.00 msC: 85.00 msSIMD: 40.00 msAUTO: 41.00 msSpeedup SIMD vs NumPy: 3.00xNotes on Memory Handling
Section titled “Notes on Memory Handling”- SIMD code uses aligned loads/stores when input/output pointers are 32-byte aligned.
- Unaligned pointers are handled safely with
_mm256_loadu_ps/_mm256_storeu_ps. - Optional helpers are provided:
acn_aligned_alloc(alignment, size)acn_aligned_free(ptr)
Assembly Kernel
Section titled “Assembly Kernel”- File:
src/simd_avx2_asm.S - Function:
vector_add_avx2_asm(const float* a, const float* b, float* out, int n) - ISA: AVX2 (
ymmregisters,vmovups,vaddps, scalar tail loop) - Python access:
acn.vector_add(a, b, impl="asm")