API reference

The portable public surface lives in six headers:

Header Contents
<brotensor/tensor.h> Tensor, Dtype, Device, factories, migration, host accessors, dtype + bit-conversion helpers
<brotensor/runtime.h> init() / shutdown(), device policy, compute_dtype(), device memory, sync
<brotensor/ops.h> The device-neutral op surface — an umbrella over the per-category headers in <brotensor/ops/> (see op-coverage.md)
<brotensor/kernel_interop.h> Zero-copy tensor buffer views (TensorBufferView), byte H2D/D2H helpers, device_has_float64(), and stream-ordered kernel dispatch (dispatch_spirv, dispatch_ptx, dispatch_msl)
<brotensor/safetensors.h> safetensors reader + writer
<brotensor/gguf.h> GGUF reader

Three further headers expose backend-specific extensions:

Header Contents
<brotensor/cuda_graph.h> Device-neutral graph capture / replay (CudaGraph, CudaGraphCapture) on CUDA / Vulkan — gate on graph_capture_available(Device)
<brotensor/vulkan.h> Vulkan batching control, VulkanGraph, stats, device_info — gate on BROTENSOR_HAS_VULKAN (vulkan.md)
<brotensor/metal_interop.h> Legacy Metal custom-kernel surface — Obj-C++ / .mm consumers only (pure C++20 code should prefer <brotensor/kernel_interop.h>)

All preconditions and dispatch failures throw std::runtime_error with a "brotensor: <op>: <reason>" message.

Tensor

A row-major (rows, cols) buffer with runtime Dtype + Device tags. Copyable (device-aware deep copy) and movable. Rank-1 data is (N, 1); higher-rank layouts are flattened per each op's shape contract.

enum class DeviceType { CPU, CUDA, Metal, VULKAN };
struct Device { DeviceType type; int index; };   // Device::cuda(i), ::metal(i), ::vulkan(i), ...
enum class Dtype  { FP32, FP16, INT8, INT32, BF16, F64,
                    Q4_0, Q4_1, Q5_0, Q5_1, Q8_0, Q8_1,        // GGUF legacy blocks
                    Q2_K, Q3_K, Q4_K, Q5_K, Q6_K, Q8_K };       // GGUF K-quant superblocks

Factories

Factory Meaning
Tensor::zeros(r, c, dt = FP32) / Tensor::empty(r, c, dt = FP32) Allocate on the default device. zeros zero-fills; empty leaves contents undefined.
Tensor::zeros_on(dev, r, c, dt) / Tensor::empty_on(dev, r, c, dt) Same, pinned to an explicit device.
Tensor::from_host(ptr, r, c) (+ _fp16 / _bf16 / _int8 variants) Copy a host buffer to a new tensor on the default device. FP16/BF16 take uint16_t bit patterns, INT8 takes int8_t.
Tensor::from_host_on(dev, ptr, r, c) (+ _fp16_on / _bf16_on / _int8_on) Same, pinned to an explicit device.
Tensor::from_raw_bytes_on(dev, src, r, c, dt, nbytes) Dtype-agnostic byte-level bootstrap. Copies raw bytes rather than interpreting elements, so unlike from_host* it works for any dtype including the opaque GGUF block-quant carriers. nbytes must equal dtype_storage_bytes(dt, r*c).
Tensor::mat(r, c) / Tensor::vec(n) Zero-filled FP32 host (CPU) tensors — build parameters on the host, then migrate with to().
Tensor::view(dev, ptr, r, c, dt) Non-owning view over an existing backend-resident pointer. resize() on a view throws.

Migration and readback

Member Meaning
t.to(device) Returns a copy migrated to another backend; the source is unchanged.
t.clone() Device-preserving deep copy.
t.to_host_vector() (+ _fp16 / _bf16) Read back to a std::vector (float / uint16_t bits).
t.copy_to_host(dst) (+ _fp16 / _bf16) Read back into a caller-owned buffer.
t.zero() memset the buffer to zero over bytes().
t.resize(r, c, dt) Reshape in place; contents undefined afterwards (call zero() if needed), device preserved. Throws on a negative dimension or a non-owning view.

resize() reuses storage whenever the requested shape fits the existing allocation — capacity is the high-water mark of the tensor's past sizes — and reallocates only when growing past it. A no-op when the shape and dtype already match. So a scratch buffer cycling through shapes stabilises at its largest size instead of reallocating every call, and its device pointer stays stable — which is what makes a tensor reusable across a CUDA-graph-captured op sequence.

Call sync(device) / sync_all() before reading GPU results back to the host — GPU ops are asynchronous.

CPU-resident tensors additionally expose direct host accessors (host_f32_mut(), at(), operator[], …) — see tensor.h.

Dtype helpers

Free functions for sizing a buffer without special-casing the quant carriers:

Helper Meaning
dtype_size_bytes(dt) Bytes per element. Returns 0 for the block-quant dtypes — they aren't element-addressable.
dtype_block_size(dt) Elements per block (32 for the legacy quants, 256 for the K-quants, 1 otherwise).
dtype_block_bytes(dt) Encoded bytes per block.
dtype_storage_bytes(dt, n) Bytes needed for n elements. Use this for buffer sizes — it's correct for quant and non-quant dtypes alike.
dtype_is_quant(dt) Whether dt is a GGUF block-quant carrier.
device_name(dev) The backend kind as a string ("CPU" / "CUDA" / "Metal" / "vulkan", ":N" suffixed past card 0).

Bit-conversion helpers

fp32_to_fp16_bits / fp16_bits_to_fp32 / fp32_to_bf16_bits / bf16_bits_to_fp32 — pure-CPU scalar conversions between FP32 and half/bfloat bit patterns, for tests and small host-side preprocessing.

Runtime

Function Meaning
init() Idempotent. Probes and registers the CUDA / Metal / Vulkan backends. CPU is always registered (static-init), so CPU-only code works without calling it.
shutdown() Joins the CPU backend's worker threads. Idempotent, and safe even if init() was never called. See the note below — call it before returning from main().
default_device() Where no-suffix factories allocate. Best available: CUDA > Metal > Vulkan > CPU.
set_default_device(dev) Global override. Also overridable per-process via the BROTENSOR_DEFAULT_DEVICE env var (cpu / cuda / metal / vulkan (vk), optionally :N).
DeviceScope scope(dev) RAII per-scope default-device override.
compute_dtype() The dtype a model loader should upload weights at for the current default device: FP32 on CPU, FP16 on a GPU.
available_devices() / is_available(dev) Backends registered in this binary at runtime.
sync(dev) / sync_all() Drain pending backend work (no-op on CPU).
device_mem_info(dev, free, total) Device-wide free/total bytes. Returns false (outputs untouched) when the backend can't report; CPU always returns false.
device_mem_trim(dev, keep_bytes = 0) Return the allocator's cached-but-unused memory to the driver, keeping at most keep_bytes. Syncs the device first so stream-ordered frees are reclaimable.
device_product_name(dev) The card's human-readable name (e.g. "NVIDIA GeForce RTX 4090") — distinct from device_name(), which is the backend kind. "" if unavailable.

Shutdown. The CPU backend's worker threads otherwise live until the thread pool's Meyers-singleton destructor runs during static destruction, by which point the OS has already suspended every other thread. A worker suspended mid-op while holding a global lock (e.g. the Debug CRT's iterator-checking mutex) can deadlock the main thread's own exit-time destructors on that same lock. shutdown() makes the teardown deterministic instead.

Trimming. device_mem_trim is worth calling between pipeline phases with very different scratch shapes: cached blocks count against device residency, and on Windows (WDDM) sustained near-full commit makes the OS demote large resident allocations to shared memory — silently turning weight reads into PCIe traffic.

Profiling (<brotensor/profile.h>)

profile_begin() / profile_end(iterations) bracket a region on the CUDA backend and return a text report of where its GPU time went, per iteration:

Times are CUPTI activity records, stamped by the GPU, so the profiler adds nothing to the streams and a host-bound region is measured as it runs; tagging each launch costs the host about 0.2 µs. CUPTI loads at run time: BROTENSOR_CUPTI names the library, otherwise the newest under $CUDA_PATH/extras/CUPTI/lib64 (or /usr/local/cuda on Linux) is used, and without one the report says so. Peaks come from the device's clocks, memory bus and SM architecture; the tensor peak is the dense FP16/BF16-in, FP32-accumulate mma.sync rate, which GeForce parts run at half the rate of their workstation siblings.

Vulkan and Metal report the same span, gaps and per-op table, with the GPU time of every command but without the fill, work and peak columns. On Vulkan each device's stream brackets the commands it records with two timestamp queries, named after the pipeline's shader; a graph launch is a single graph replay row, since its recorded command buffers carry no queries of the session (BROTENSOR_GRAPHS=0 turns capture off, so a step runs and is timed op by op). On Metal each op's compute or blit pass carries timestamp counter samples at its start and end, and GPU ticks map to nanoseconds against the host clock over the session. A build with several GPU backends reports a section for each device that ran work.

An op reports its work with detail::cuda::prof::work(flops, bytes, tensor_cores) (or gemm_work for a GEMM shape) just before its launch (src/cuda/cuda_profile.h). The FP32 implicit-GEMM convs (SIMT and tensor-core) and their split-K fold, the SIMT GEMM / GEMV paths, the tensor-core GEMMs (batched, strided, split-K, NN), unary and binary elementwise ops, and row / column norms do; other kernels show time and fill only.

safetensors (<brotensor/safetensors.h>)

mmap'd zero-copy reader plus a writer. Namespace brotensor::safetensors.

GGUF (<brotensor/gguf.h>)

mmap'd reader for GGUF model files. Namespace brotensor::gguf.

Note that carrier support is broader than op support: the reader will load any of the above, but only Q4_K / Q6_K / Q8_0 are consumed by the fused dequant / matmul kernels (see op-coverage.md). Loading a Q5_K tensor succeeds; calling a matmul on it throws.