Aicraft
Skip to main content

Vulkan API

#include "aicraft/vulkan.h"

Overview

Optional Vulkan compute backend for GPU-accelerated operations. Link with -lvulkan to enable.

Initialisation

ac_vulkan_init();                  // Auto-select best GPU
ac_vulkan_select_device(int idx); // Select specific GPU
ac_vulkan_shutdown(); // Release GPU resources

Device Info

typedef struct AcVkDeviceInfo {
char name[256];
int vram_mb;
int compute_units;
int max_workgroup_size;
} AcVkDeviceInfo;

AcVkDeviceInfo info = ac_vulkan_device_info();

Data Transfer

void ac_vulkan_upload(AcTensor *t);     // CPU → GPU
void ac_vulkan_download(AcTensor *t); // GPU → CPU
void ac_vulkan_sync(); // Wait for GPU

Compute Shaders

14 GLSL compute shaders are compiled at initialisation:

ShaderOperation
gemm.compGeneral matrix multiply
relu.compReLU activation
sigmoid.compSigmoid activation
softmax.compSoftmax
add.compElement-wise add
mul.compElement-wise mul
reduce_sum.compSum reduction
reduce_max.compMax reduction
transpose.compMatrix transpose
cross_entropy.compCE loss
mse.compMSE loss
adam_step.compAdam update
quantize.compINT8 quantisation
broadcast.compBroadcasting

Backend Dispatch

When Vulkan is initialised, operations on uploaded tensors automatically dispatch to GPU shaders. No code changes needed:

ac_vulkan_init();
ac_vulkan_upload(x);

// This runs on GPU automatically
AcTensor *y = ac_forward_seq(net, 2, x);

ac_vulkan_download(y);

Fallback

If Vulkan initialisation fails (e.g., no GPU), operations fall back to the CPU (SIMD) backend transparently.