cheatah
Module

gpu::linalg

cheatah-gpu-linalg v0.4.4-alpha — Biome Standard 0.6.3-alpha

Classes

Concepts

Functions

fn void cholesky(Array< T > &out, const Array< T > &a) #

Device cholesky — host bridge (download → cheatah host kernel → upload).

Parameters
out

The pre-allocated n×n device factor L (filled by upload).

a

The n×n symmetric positive-definite device operand (throws otherwise, from the host kernel).

Complexity

host O(n³) (cheatah's column-by-column Cholesky) + O(n²) elements over the bus.

Host allocation

host f64 copies of a and the factor (O(n²) each; an f32 element adds one O(n²) conversion vector per transfer) + the host kernel's own O(n²) private factor scratch.

GPU allocation

none — out was allocated by cheatah's front; transfers recycle the context's pooled staging buffers.

Unit testgpu:bridge
fn void inv(Array< T > &out, const Array< T > &a) #

Device inv — host bridge (LU with partial pivoting + whole-identity back-solve).

Parameters
out

The pre-allocated n×n device inverse.

a

The square device operand (a singular matrix throws, from the host kernel).

Complexity

host O(n³) + O(n²) elements over the bus.

Host allocation

host f64 copies of a and the result (O(n²) each) + the host kernel's LU and identity back-solve scratch (O(n²)); f32 adds conversion vectors.

GPU allocation

none — out pre-allocated by cheatah's front; pooled staging only.

Unit testgpu:bridge
fn void pinv(Array< T > &out, const Array< T > &a) #

Device pinv — host bridge (Moore–Penrose pseudo-inverse via Golub–Reinsch SVD; the result is the transposed shape n×m for an m×n input).

Parameters
out

The pre-allocated n×m device pseudo-inverse.

a

The m×n device operand (any shape).

Complexity

host iterative O(n³) via SVD + O(m·n) elements over the bus.

Host allocation

host f64 copies of a and the result + the host kernel's SVD/assembly scratch (O(m·n)); f32 adds conversion vectors.

GPU allocation

none — out pre-allocated by cheatah's front; pooled staging only.

Unit testgpu:bridge
fn void matrix_power(Array< T > &out, const Array< T > &a, long long n) #

Device matrix_power — host bridge (binary exponentiation; negative n via the host inverse).

Parameters
out

The pre-allocated square device result Aⁿ.

a

The square device operand.

n

The integer exponent (0 yields the identity).

Complexity

host O(n³·log|n|) by binary exponentiation + O(n²) elements over the bus.

Host allocation

host f64 copies of a and the result + the binary-exponentiation products' own intermediates on the host; f32 adds conversion vectors.

GPU allocation

none — out pre-allocated by cheatah's front; pooled staging only.

Unit testgpu:bridge
fn void solve(Array< T > &out, const Array< T > &a, const Array< T > &b) #

Device solve — host bridge (x from A·x = b via LU with partial pivoting).

Parameters
out

The pre-allocated length-n device solution vector.

a

The n×n device coefficient matrix.

b

The length-n device right-hand side.

Complexity

host O(n³) + O(n²) elements over the bus.

Host allocation

host f64 copies of a, b and x + the host kernel's O(n²) LU scratch; f32 adds conversion vectors.

GPU allocation

none — out pre-allocated by cheatah's front; pooled staging only.

Unit testgpu:bridge
fn void lstsq(Array< T > &out, const Array< T > &a, const Array< T > &b) #

Device lstsq — host bridge (min ‖Ax−b‖, computed as pinv(A)·b on the host).

Parameters
out

The pre-allocated device solution (a.cols × b.cols).

a

The m×n device coefficient matrix.

b

The m×k device right-hand side.

Complexity

host iterative O(n³) via SVD + O(m·n) elements over the bus.

Host allocation

host f64 copies of a, b and x + the intermediate n×m pseudo-inverse and its SVD scratch on the host; f32 adds conversion vectors.

GPU allocation

none — out pre-allocated by cheatah's front; pooled staging only.

Unit testgpu:bridge
fn void svdvals(Array< T > &out, const Array< T > &a) #

Device svdvals — host bridge (descending singular values, length min(m,n) — the values-only fast path of the Golub–Reinsch reduction, a large constant factor below the full svd).

Parameters
out

The pre-allocated length-min(m,n) device singular-value vector.

a

The m×n device operand.

Complexity

host iterative O(n³) + O(m·n) elements over the bus.

Host allocation

host f64 copies of a and the values + the reduction's own O(m·n) workspace on the host; f32 adds conversion vectors.

GPU allocation

none — out pre-allocated by cheatah's front; pooled staging only.

Unit testgpu:bridge
fn void qr(Array< T > &q, Array< T > &r, const Array< T > &a) #

Device qr — host bridge (reduced Householder QR; requires rows ≥ cols, from the host kernel).

Parameters
q

The pre-allocated m×n device orthonormal factor.

r

The pre-allocated n×n device upper-triangular factor.

a

The m×n device operand.

Complexity

host O(n³) + O(m·n) elements over the bus.

Host allocation

host f64 copies of a and both factors + the host kernel's O(m²) private factorization scratch; f32 adds conversion vectors.

GPU allocation

none — q and r pre-allocated by cheatah's front; pooled staging only.

Unit testgpu:bridge
fn void svd(Array< T > &u, Array< T > &s, Array< T > &vh, const Array< T > &a) #

Device svd — host bridge (Golub–Reinsch; requires rows ≥ cols, from the host kernel).

Parameters
u

The pre-allocated m×n device left-singular-vector factor.

s

The pre-allocated length-n device singular-value vector (descending).

vh

The pre-allocated n×n device Vᴴ factor.

a

The m×n device operand.

Complexity

host iterative O(n³) + O(m·n) elements over the bus.

Host allocation

host f64 copies of a and all three factors + the reduction's own O(m·n) workspace on the host; f32 adds conversion vectors.

GPU allocation

none — outputs pre-allocated by cheatah's front; pooled staging only.

Unit testgpu:bridge
fn void eig(Array< ndarray::complex_of_t< T > > &values, Array< ndarray::complex_of_t< T > > &vectors, const Array< T > &a) #

Device eig — host bridge (Hessenberg + shifted QR; COMPLEX outputs from a real or complex square input, since a real matrix can have complex eigenpairs).

Parameters
values

The pre-allocated length-n complex device spectrum.

vectors

The pre-allocated n×n complex device eigenvector matrix (columns).

a

The n×n device operand.

Complexity

host iterative O(n³) + O(n²) elements over the bus.

Host allocation

host c128 copies of a's widening plus both outputs + the host kernel's O(n²) factorization/eigenvector scratch; narrower elements add conversion vectors.

GPU allocation

none — outputs pre-allocated by cheatah's front; pooled staging only.

Unit testgpu:bridge
fn void eigvals(Array< ndarray::complex_of_t< T > > &out, const Array< T > &a) #

Device eigvals — host bridge (the COMPLEX spectrum of a real or complex square matrix, descending; symmetric input routes through tridiagonal QL on the host, the rest through Hessenberg + shifted QR).

Parameters
out

The pre-allocated length-n complex device spectrum.

a

The n×n device operand.

Complexity

host iterative O(n³) + O(n²) elements over the bus.

Host allocation

host c128 copies of the operand and spectrum + the host iteration's own O(n²) scratch; narrower elements add conversion vectors.

GPU allocation

none — out pre-allocated by cheatah's front; pooled staging only.

Unit testgpu:bridge
fn void eigh(Array< ndarray::real_base_t< T > > &values, Array< T > &vectors, const Array< T > &a) #

Device eigh — host bridge (Householder tridiagonalization + QL for a symmetric/Hermitian input: a REAL spectrum plus eigenvectors matching the input's element type).

Parameters
values

The pre-allocated length-n REAL device spectrum (descending).

vectors

The pre-allocated n×n device eigenvector matrix (input's element type).

a

The n×n symmetric/Hermitian device operand.

Complexity

host iterative O(n³) + O(n²) elements over the bus.

Host allocation

host f64/c128 copies of the operand and both outputs + the host solver's own O(n²) scratch; narrower elements add conversion vectors.

GPU allocation

none — outputs pre-allocated by cheatah's front; pooled staging only.

Unit testgpu:bridge
fn void eigvalsh(Array< ndarray::real_base_t< T > > &out, const Array< T > &a) #

Device eigvalsh — host bridge (the REAL spectrum of a symmetric/Hermitian matrix, descending — tridiagonal QL skipping the eigenvector accumulation).

Parameters
out

The pre-allocated length-n REAL device spectrum.

a

The n×n symmetric/Hermitian device operand.

Complexity

host iterative O(n³) + O(n²) elements over the bus.

Host allocation

host copies of the operand and spectrum + the host solver's own O(n²) scratch; narrower elements add conversion vectors.

GPU allocation

none — out pre-allocated by cheatah's front; pooled staging only.

Unit testgpu:bridge
fn void det(T &out, const Array< T > &a) #

Device det — host bridge (LU with partial pivoting; pivot product × permutation sign).

Parameters
out

The caller's scalar determinant (narrowed back to a's element type).

a

The square device operand.

Complexity

host O(n³) + O(n²) elements downloaded.

Host allocation

one host f64 copy of a + the host kernel's O(n²) LU scratch; f32 adds a conversion vector.

GPU allocation

none — download-only; pooled staging.

Unit testgpu:bridge
fn void slogdet(cheatah::linalg::SLogDet &out, const Array< T > &a) #

Device slogdet — host bridge (sign and log|det| via LU — the overflow-safe determinant).

Parameters
out

The caller's SLogDet result (plain doubles).

a

The square device operand.

Complexity

host O(n³) + O(n²) elements downloaded.

Host allocation

one host f64 copy of a + the host kernel's O(n²) LU scratch; f32 adds a conversion vector.

GPU allocation

none — download-only; pooled staging.

Unit testgpu:bridge
fn void cond(T &out, const Array< T > &a) #

Device cond — host bridge (largest/smallest singular-value ratio from a Golub–Reinsch SVD).

Parameters
out

The caller's scalar condition number.

a

The device operand.

Complexity

host iterative O(n³) via SVD + O(n²) elements downloaded.

Host allocation

one host f64 copy of a + the host kernel's O(n²) factorization scratch; f32 adds a conversion vector.

GPU allocation

none — download-only; pooled staging.

Unit testgpu:bridge
fn void matrix_rank(long long &out, const Array< T > &a) #

Device matrix_rank — host bridge (SVD singular-value thresholding).

Parameters
out

The caller's rank (cheatah's signed integer).

a

The device operand.

Complexity

host iterative O(n³) via SVD + O(n²) elements downloaded.

Host allocation

one host f64 copy of a + the host kernel's O(n²) factorization scratch; f32 adds a conversion vector.

GPU allocation

none — download-only; pooled staging.

Unit testgpu:bridge
fn void norm(T &out, const Array< T > &a) #

Device norm — host bridge (L2 for vectors, Frobenius for matrices — dispatched on rank by the host kernel).

Parameters
out

The caller's scalar norm.

a

The device operand (any rank).

Complexity

host O(size) over the elements + the same O(size) downloaded.

Host allocation

one host f64 copy of a (the host kernel itself sums in place); f32 adds a conversion vector.

GPU allocation

none — download-only; pooled staging.

Unit testgpu:bridge
fn bool available() noexcept #

Whether the device context can come up on this machine — a cached one-shot probe that NEVER throws.

This is the runtime "is there a GPU?" question (the nullopt-probe pattern at the linalg layer): gate work on it instead of hand-rolling a try/catch around a first dispatch. detail::ctx() itself still throws on first touch when the device is genuinely required.

Returns

true when the backend context is (or can be) live; false when bring-up failed.

Complexity

O(1) after the first call (the probe result is cached).

Host allocation

none after the first call.

GPU allocation

none.

Unit testgpu:guards
fn const std::string & unavailable_reason() noexcept #

Why available() is false — the probe's exception text, "" while available.

The consumer's skip note ("gpu lane skipped: …") should carry this instead of inventing its own reason.

Returns

The bring-up failure message, or the empty string while the device is available.

Complexity

O(1) after the first call (the reason is cached).

Host allocation

one cached string on the first call.

GPU allocation

none.

Unit testgpu:guards
fn void im2col2d(Array< T > &col, const Array< T > &x, long long B, long long C, long long H, long long W, long long KH, long long KW, long long OH, long long OW, long long stride, long long pad) #

Device batched 2-D im2col GATHER into the caller's column block: col[(c·KH+kh)·KW+kw, b·OO + oh·OW + ow] = x[b, c, oh·stride − pad + kh, ow·stride − pad + kw] with out-of-plane (padding) cells written as ZEROS — a FULL overwrite, so a reused workspace needs no zeroing pass.

One thread per col element, writes contiguous. Mirrors the host reference's element order and zero-padding exactly (pure data movement — bit-identical).

Parameters
col

The [C·KH·KW, B·OH·OW]-element device destination (workspace, overwritten).

x

The [B, C·H·W]-element device input block, laid out [B][C][H][W].

B

The batch size.

C

The input channel count.

H

The input plane height.

W

The input plane width.

KH

The kernel height.

KW

The kernel width.

OH

The output plane height ((H + 2·pad − KH)/stride + 1).

OW

The output plane width.

stride

The spatial step (≥ 1).

pad

The symmetric zero padding (≥ 0). Throws on an operand size mismatch.

Complexity

O(B·C·KH·KW·OH·OW) device work in one blocking dispatch.

Host allocation

none — the 10-uint dims scratch recycles through the pool.

GPU allocation

none — both data buffers are the caller's.

Unit testgpu:conv
fn void col2im2d(Array< T > &dx, const Array< T > &dcol, long long B, long long C, long long H, long long W, long long KH, long long KW, long long OH, long long OW, long long stride, long long pad) #

Device batched 2-D col2im — the EXACT adjoint of im2col2d, in GATHER form: each dx cell sums its ≤ KH·KW contributing dcol cells (skipping stride-incompatible and out-of-range kernel offsets), one thread per dx element, FULL overwrite (the scatter-add's zeroing folded in).

The kh-major accumulation order matches the host reference's scatter-add per cell, so the result is bit-identical — and deterministic by construction, no atomics.

Parameters
dx

The [B, C·H·W]-element device input-gradient block, overwritten.

dcol

The [C·KH·KW, B·OH·OW]-element device column gradients.

B

The batch size.

C

The input channel count.

H

The input plane height.

W

The input plane width.

KH

The kernel height.

KW

The kernel width.

OH

The output plane height.

OW

The output plane width.

stride

The spatial step (≥ 1).

pad

The symmetric zero padding (≥ 0). Throws on an operand size mismatch.

Complexity

O(B·C·H·W·KH·KW) device work in one blocking dispatch (≤ KH·KW adds per cell).

Host allocation

none — the 10-uint dims scratch recycles through the pool.

GPU allocation

none — both data buffers are the caller's.

Unit testgpu:conv
fn void conv_bias_act(Array< T > &a, const Array< T > &yc, const Array< T > &bias, long long B, long long F, long long OO, long long act_code) #

Device conv forward epilogue, fused THREE-in-one — bias add + activation + layout transpose: a[(b·F+f)·OO + o] = act(yc[f·B·OO + b·OO + o] + bias[f]), taking the GEMM's filter-major [F, B·OO] output to the per-sample [B, F·OO] activation block in ONE dispatch.

One thread per output element. f64 note: tanh/sigmoid evaluate their transcendental in f32 on the device (SPIR-V has no f64 exp/tanh); identity/relu stay full-width exact.

Parameters
a

The [B, F·OO]-element device activation block, overwritten.

yc

The [F, B·OO]-element device GEMM output.

bias

The F-element device bias row.

B

The batch size.

F

The filter (output-channel) count.

OO

The output plane element count OH·OW.

act_code

The activation selector: 0 = identity, 1 = relu, 2 = tanh, 3 = sigmoid (throws on any other code, and on an operand size mismatch).

Complexity

O(B·F·OO) device work in one blocking dispatch.

Host allocation

none — content-cached dims scratch only.

GPU allocation

none — all three data buffers are the caller's.

Unit testgpu:conv
fn void conv_act_grad(Array< T > &dyc, const Array< T > &d, const Array< T > &a, long long B, long long F, long long OO, long long act_code) #

Device conv backward epilogue (the adjoint of conv_bias_act's act + layout, fused): dyc[f·B·OO + b·OO + o] = d[(b·F+f)·OO + o] · act'(a[(b·F+f)·OO + o]) — the chain-rule step and the transpose back to filter-major in ONE dispatch.

The derivative is evaluated FROM THE OUTPUT a = f(z) (identity' = 1, relu' = [a > 0], tanh' = 1 − a², sigmoid' = a(1 − a)) — pure arithmetic, exact in both element widths, no retained pre-activations.

Parameters
dyc

The [F, B·OO]-element device delta block, overwritten.

d

The [B, F·OO]-element device upstream gradient dL/dA.

a

The [B, F·OO]-element device activations (the forward output).

B

The batch size.

F

The filter (output-channel) count.

OO

The output plane element count OH·OW.

act_code

The activation selector: 0 = identity, 1 = relu, 2 = tanh, 3 = sigmoid (throws on any other code, and on an operand size mismatch).

Complexity

O(B·F·OO) device work in one blocking dispatch.

Host allocation

none — content-cached dims scratch only.

GPU allocation

none — all three data buffers are the caller's.

Unit testgpu:conv
fn add · 2 overloads
void add(device_array< T > &out, const device_array< T > &a, const device_array< T > &b)#
device_array< T > add(const device_array< T > &a, const device_array< T > &b)#

out = a + b (same shape).

Parameters
out

The destination (may alias a or b).

a

The left device operand.

b

The right device operand (throws on a shape mismatch).

Complexity

O(n) device work in one blocking vec4 dispatch.

Host allocation

none — content-cached dims scratch only.

GPU allocation

none — all buffers are the caller's.

Unit testgpu:elementwise
fn sub · 2 overloads
void sub(device_array< T > &out, const device_array< T > &a, const device_array< T > &b)#
device_array< T > sub(const device_array< T > &a, const device_array< T > &b)#

out = a − b (same shape).

Parameters
out

The destination (may alias an operand).

a

The left device operand.

b

The right device operand (throws on a shape mismatch).

Complexity

O(n) device work in one blocking vec4 dispatch.

Host allocation

none — content-cached dims scratch only.

GPU allocation

none.

Unit testgpu:elementwise
fn mul · 2 overloads
void mul(device_array< T > &out, const device_array< T > &a, const device_array< T > &b)#
device_array< T > mul(const device_array< T > &a, const device_array< T > &b)#

out = a ⊙ b, the Hadamard product (same shape).

Parameters
out

The destination (may alias an operand).

a

The left device operand.

b

The right device operand (throws on a shape mismatch).

Complexity

O(n) device work in one blocking vec4 dispatch.

Host allocation

none — content-cached dims scratch only.

GPU allocation

none.

Unit testgpu:elementwise
fn divide · 2 overloads
void divide(device_array< T > &out, const device_array< T > &a, const device_array< T > &b)#
device_array< T > divide(const device_array< T > &a, const device_array< T > &b)#

out = a ⊘ b, elementwise division (same shape).

Parameters
out

The destination (may alias an operand).

a

The dividend device operand.

b

The divisor device operand (throws on a shape mismatch).

Complexity

O(n) device work in one blocking vec4 dispatch.

Host allocation

none — content-cached dims scratch only.

GPU allocation

none.

Unit testgpu:elementwise
fn axpy · 3 overloads
void axpy(device_array< T > &out, T alpha, const device_array< T > &x, const device_array< T > &y)#
device_array< T > axpy(T alpha, const device_array< T > &x, const device_array< T > &y)#
void axpy(host_array< T > &out, T alpha, const host_array< T > &x, const host_array< T > &y)#

out = α·x + y in ONE dispatch — the fused training-loop primitive (same shapes; operator chaining costs two dispatches and a temporary).

Parameters
out

The destination (may alias x or y).

alpha

The scalar coefficient.

x

The scaled device operand.

y

The added device operand (throws on a shape mismatch).

Complexity

O(n) device work in one blocking vec4 dispatch.

Host allocation

none — the 1-element α buffer and dims scratch recycle through the pool.

GPU allocation

none.

Unit testgpu:elementwise
fn device_array< T > sqrt(const device_array< T > &a) #

√a elementwise, allocating (cheatah's sqrt ufunc spelling).

Parameters
a

The device operand.

Returns

A fresh device array holding the elementwise square root.

Complexity

O(n) device work in one blocking vec4 dispatch.

Host allocation

none — content-cached dims scratch only.

GPU allocation

one pooled device data buffer for the result.

Unit testgpu:elementwise
fn device_array< T > exp(const device_array< T > &a) #

eᵃ elementwise, allocating.

f64 note: SPIR-V (and GPU hardware generally) has NO double transcendentals — GLSL.std.450 Exp/Log exist only for 16/32-bit floats — so the double form is evaluated on the HOST over the downloaded elements, full f64 precision; float runs the device kernel.

Parameters
a

The device operand.

Returns

A fresh device array holding eᵃ.

Complexity

O(n): one device dispatch for float; a host loop + round-trip for double.

Host allocation

none for float (dims scratch only); one O(n) host vector for the double host path.

GPU allocation

one pooled device data buffer for the result.

Unit testgpu:elementwise
fn device_array< T > log(const device_array< T > &a) #

ln a elementwise, allocating (same f64 host-evaluation note as exp).

Parameters
a

The device operand.

Returns

A fresh device array holding ln a.

Complexity

O(n): one device dispatch for float; a host loop + round-trip for double.

Host allocation

none for float (dims scratch only); one O(n) host vector for the double host path.

GPU allocation

one pooled device data buffer for the result.

Unit testgpu:elementwise
fn device_array< T > abs(const device_array< T > &a) #

|a| elementwise, allocating.

Parameters
a

The device operand.

Returns

A fresh device array holding the elementwise absolute value.

Complexity

O(n) device work in one blocking vec4 dispatch.

Host allocation

none — content-cached dims scratch only.

GPU allocation

one pooled device data buffer for the result.

Unit testgpu:elementwise
fn sum · 2 overloads
T sum(const device_array< T > &a)#
void sum(device_array< T > &out, const device_array< T > &a)#

Σ aᵢ over the whole array — the DETERMINISTIC partial-sum contract (a size-picked one- or two-stage partial kernel + an on-device finalize, fused into one submission; fixed association order, so the same bits come back every run).

Parameters
a

The device operand (any rank; 0 elements yield T{}).

Returns

The scalar sum, downloaded as ONE element.

Complexity

O(n) device work; one fused submission (partial + finalize).

Host allocation

none — pooled partial/result scratch; sizeof(T) readback.

GPU allocation

none — pooled scratch only.

Unit testgpu:elementwise
fn mean · 2 overloads
T mean(const device_array< T > &a)#
void mean(device_array< T > &out, const device_array< T > &a)#

The arithmetic mean Σ aᵢ / n (n = 0 yields 0, matching an empty sum).

Parameters
a

The device operand.

Returns

The scalar mean.

Complexity

O(n) device work (one fused sum) + one host division.

Host allocation

none — pooled partial/result scratch; sizeof(T) readback.

GPU allocation

none — pooled scratch only.

Unit testgpu:elementwise
fn operator+ · 3 overloads
device_array< T > operator+(const device_array< T > &a, const device_array< T > &b)#
device_array< T > operator+(const device_array< T > &a, T s)#
device_array< T > operator+(T s, const device_array< T > &a)#

a + b elementwise (the spelling purr's codegen emits; resolves here by ADL).

Parameters
a

The left device operand.

b

The right device operand (throws on a shape mismatch).

Returns

A fresh device array holding the sum.

Complexity

O(n) device work in one blocking vec4 dispatch.

Host allocation

none — content-cached dims scratch only.

GPU allocation

one pooled device data buffer for the result.

Unit testgpu:operators
fn operator- · 4 overloads
device_array< T > operator-(const device_array< T > &a, const device_array< T > &b)#
device_array< T > operator-(const device_array< T > &a, T s)#
device_array< T > operator-(T s, const device_array< T > &a)#
device_array< T > operator-(const device_array< T > &a)#

a − b elementwise.

Parameters
a

The left device operand.

b

The right device operand (throws on a shape mismatch).

Returns

A fresh device array holding the difference.

Complexity

O(n) device work in one blocking vec4 dispatch.

Host allocation

none — content-cached dims scratch only.

GPU allocation

one pooled device data buffer for the result.

Unit testgpu:operators
fn operator* · 3 overloads
device_array< T > operator*(const device_array< T > &a, const device_array< T > &b)#
device_array< T > operator*(const device_array< T > &a, T s)#
device_array< T > operator*(T s, const device_array< T > &a)#

a ⊙ b elementwise.

Parameters
a

The left device operand.

b

The right device operand (throws on a shape mismatch).

Returns

A fresh device array holding the Hadamard product.

Complexity

O(n) device work in one blocking vec4 dispatch.

Host allocation

none — content-cached dims scratch only.

GPU allocation

one pooled device data buffer for the result.

Unit testgpu:operators
fn operator/ · 3 overloads
device_array< T > operator/(const device_array< T > &a, const device_array< T > &b)#
device_array< T > operator/(const device_array< T > &a, T s)#
device_array< T > operator/(T s, const device_array< T > &a)#

a ⊘ b elementwise.

Parameters
a

The dividend device operand.

b

The divisor device operand (throws on a shape mismatch).

Returns

A fresh device array holding the quotient.

Complexity

O(n) device work in one blocking vec4 dispatch.

Host allocation

none — content-cached dims scratch only.

GPU allocation

one pooled device data buffer for the result.

Unit testgpu:operators
fn operator+= · 2 overloads
device_array< T > & operator+=(device_array< T > &a, const device_array< T > &b)#
device_array< T > & operator+=(device_array< T > &a, T s)#

a += b in place.

Parameters
a

The accumulating device operand.

b

The added device operand.

Returns

a.

Complexity

O(n) device work in one blocking vec4 dispatch.

Host allocation

none — pooled scratch only.

GPU allocation

none — in place.

Unit testgpu:operators
fn operator-= · 2 overloads
device_array< T > & operator-=(device_array< T > &a, const device_array< T > &b)#
device_array< T > & operator-=(device_array< T > &a, T s)#

a −= b in place.

Parameters
a

The accumulating device operand.

b

The subtracted device operand.

Returns

a.

Complexity

O(n) device work in one blocking vec4 dispatch.

Host allocation

none — pooled scratch only.

GPU allocation

none — in place.

Unit testgpu:operators
fn operator*= · 2 overloads
device_array< T > & operator*=(device_array< T > &a, const device_array< T > &b)#
device_array< T > & operator*=(device_array< T > &a, T s)#

a ⊙= b in place.

Parameters
a

The accumulating device operand.

b

The multiplying device operand.

Returns

a.

Complexity

O(n) device work in one blocking vec4 dispatch.

Host allocation

none — pooled scratch only.

GPU allocation

none — in place.

Unit testgpu:operators
fn operator/= · 2 overloads
device_array< T > & operator/=(device_array< T > &a, const device_array< T > &b)#
device_array< T > & operator/=(device_array< T > &a, T s)#

a ⊘= b in place.

Parameters
a

The accumulating device operand.

b

The dividing device operand.

Returns

a.

Complexity

O(n) device work in one blocking vec4 dispatch.

Host allocation

none — pooled scratch only.

GPU allocation

none — in place.

Unit testgpu:operators
fn const detail::Context::TransferStats & stats() #

The transfer/dispatch ledger of the process-wide context (uploads, downloads, dispatches).

Returns

The live counters (a reference into the context — reads reflect later ops).

Complexity

O(1).

Host allocation

none.

GPU allocation

none.

Unit testexample:vector_pipeline
fn void reset_stats() #

Zero the ledger (examples call this right before a hot loop, then assert on stats).

Complexity

O(1).

Host allocation

none.

GPU allocation

none.

Unit testexample:vector_pipeline
fn device_array< T > to_device(const host_array< T > &a) #

Upload a host array into a fresh device array (same shape + element type; the operand must be contiguous — pack first via ndarray's reshape/copy if it is a strided view, else this throws).

Parameters
a

The contiguous host array.

Returns

A device array holding a copy of a's elements.

Complexity

O(n) transfer.

Host allocation

metadata only — the Vulkan upload stages through a pooled host-visible buffer.

GPU allocation

one pooled device data buffer of size()·sizeof(T).

Unit testexample:vector_pipeline
fn host_array< T > to_host(const device_array< T > &a) #

Download a device array into a fresh host array (same shape + element type).

A SYNC POINT.

Parameters
a

The device array.

Returns

A host ndarray holding a copy of a's elements.

Complexity

O(n) transfer.

Host allocation

the O(n) host result; the Vulkan readback stages through a pooled host-cached buffer.

GPU allocation

none.

Unit testexample:mlp_forward
fn device_array< T > full(const std::vector< long long > &shape, T value) #

A device array of the given shape, every element value (ndarray's full).

Real elements fill ON DEVICE (no host staging vector, no PCIe upload); complex elements take the staged-upload fallback.

Parameters
shape

The dimensions (cheatah's signed integers; negatives throw).

value

The fill value.

Returns

A fresh device array of value s.

Complexity

O(n) device work (real) or O(n) host fill + upload (complex).

Host allocation

none for real elements (pooled scalar/dims scratch); one O(n) host vector for complex.

GPU allocation

one pooled device data buffer for the result.

Unit testgpu:elementwise
fn device_array< double > zeros(const std::vector< long long > &shape) #

A double device array of zeros (ndarray's zeros) — the device-side fill kernel, no upload.

Parameters
shape

The dimensions (negatives throw).

Returns

A fresh zeroed device array.

Complexity

O(n) device work.

Host allocation

none — pooled scalar/dims scratch only.

GPU allocation

one pooled device data buffer for the result.

Unit testgpu:elementwise
fn device_array< double > ones(const std::vector< long long > &shape) #

A double device array of ones (ndarray's ones) — the device-side fill kernel, no upload.

Parameters
shape

The dimensions (negatives throw).

Returns

A fresh device array of ones.

Complexity

O(n) device work.

Host allocation

none — pooled scalar/dims scratch only.

GPU allocation

one pooled device data buffer for the result.

Unit testexample:batched_inference
fn device_array< T > full_like(const device_array< T > &a, T value) #

Same shape/element as a, every element value — ADL-reachable, so generic code can call it unqualified on either container.

Parameters
a

The array whose shape/element is copied.

value

The fill value.

Returns

A fresh device array of value s.

Complexity

O(n) device work (real elements fill on device).

Host allocation

none for real elements; one O(n) host vector for complex.

GPU allocation

one pooled device data buffer for the result.

fn device_array< T > zeros_like(const device_array< T > &a) #

Zeros with a's shape/element (ADL-reachable).

Parameters
a

The array whose shape/element is copied.

Returns

A fresh zeroed device array.

Complexity

O(n) device work.

Host allocation

none for real elements.

GPU allocation

one pooled device data buffer for the result.

fn device_array< T > ones_like(const device_array< T > &a) #

Ones with a's shape/element (ADL-reachable).

Parameters
a

The array whose shape/element is copied.

Returns

A fresh device array of ones.

Complexity

O(n) device work.

Host allocation

none for real elements.

GPU allocation

one pooled device data buffer for the result.

fn array · 2 overloads
device_array< T > array(const std::vector< T > &values)#
device_array< T > array(std::initializer_list< T > values)#

A 1-D device array from a host vector (ndarray's array).

Parameters
values

The host elements.

Returns

A fresh 1-D device array holding a copy of values.

Complexity

O(n) transfer.

Host allocation

metadata only — pooled staging on Vulkan.

GPU allocation

one pooled device data buffer for the result.

Unit testexample:vector_pipeline
fn device_array< T > scalar(T value) #

A 0-d scalar device array.

Parameters
value

The element.

Returns

A fresh 0-d device array holding value.

Complexity

O(1) transfer.

Host allocation

metadata only.

GPU allocation

one pooled device data buffer (one size class, 256 B minimum).

fn device_array< T > arange(T start, T stop, T step) #

Evenly spaced values in [start, stop) with the given step (ndarray's arange; a zero step throws).

Parameters
start

The first value.

stop

The exclusive bound.

step

The (non-zero) increment.

Returns

A fresh 1-D device array of the sequence.

Complexity

O(n) host generation + O(n) transfer.

Host allocation

one O(n) host vector (the sequence is generated host-side, then uploaded).

GPU allocation

one pooled device data buffer for the result.

fn device_array< T > reshape(const device_array< T > &a, const std::vector< long long > &shape) #

Reshape — ZERO-COPY: device arrays are always contiguous, so the new view shares the buffer (throws on a negative dimension, an element-count overflow, or a count mismatch).

Parameters
a

The device array to re-view.

shape

The new dimensions (same element count).

Returns

A sibling array sharing a's device buffer under the new shape.

Complexity

O(ndim).

Host allocation

the new shape/strides vectors.

GPU allocation

none — zero-copy by design.

Unit testexample:mlp_forward
fn T get(const device_array< T > &a, const std::vector< long long > &index) #

Read ONE element by signed multi-index (ndarray's get) — a single-element SYNC POINT (exactly sizeof(T) crosses the bus; throws on a rank mismatch or an out-of-range index).

Parameters
a

The device array.

index

One signed index per dimension.

Returns

The element's value.

Complexity

O(ndim) index math + an O(1) offset download.

Host allocation

none fresh — the Vulkan readback stages through a pooled host-cached buffer.

GPU allocation

none.

Unit testexample:linear_regression_gd
fn std::string to_string(const device_array< T > &a) #

Render like ndarray renders (downloads, then reuses ndarray's own to_string — identical output by construction).

A SYNC POINT.

Parameters
a

The device array.

Returns

The rendered text.

Complexity

O(n) download + O(n) formatting.

Host allocation

a full host copy of a plus the string.

GPU allocation

none.

fn std::ostream & operator<<(std::ostream &os, const device_array< T > &a) #

Stream form — same rendering as ndarray's operator<< (a SYNC POINT, via to_string).

Parameters
os

The destination stream.

a

The device array.

Returns

os.

Complexity

O(n) download + O(n) formatting.

Host allocation

a full host copy of a plus the string.

GPU allocation

none.

fn long long size_of(const device_array< T > &a) #

Element count as cheatah's signed integer (ndarray's size_of).

Parameters
a

The device array.

Returns

The element count.

Complexity

O(1).

Host allocation

none.

GPU allocation

none.

fn bool is_contiguous(const device_array< T > &a) #

Device arrays are always contiguous (parity with ndarray's is_contiguous).

Parameters
a

The device array (unused — contiguity is structural).

Returns

true, always.

Complexity

O(1).

Host allocation

none.

GPU allocation

none.

fn device_array< U > astype(const device_array< T > &a) #

Element conversion (ndarray's astype) — HOST round-trip today (documented; a device convert kernel is a listed next step).

Parameters
a

The device array to convert.

Returns

A fresh device array with each element cast to U.

Complexity

O(n): download, convert on the host, upload.

Host allocation

two O(n) host vectors (the download and the converted copy).

GPU allocation

one pooled device data buffer for the result.

fn void matmul(Array< T > &out, const Array< T > &a, const Array< T > &b) #

Device GEMM: out = a @ b, row-major, a is M×K, b is K×N, out is M×N.

Invoked by cheatah's allocating matmul(a, b) front (which has already allocated out); routes to the fast 128×128 (or 64-row) register-tiled kernel when the shape tiles exactly, the guarded 64×64 edge kernel otherwise, and the one-dispatch batched kernel for 3-D [B,M,K][B,K,N] operands.

Parameters
out

The pre-allocated M×N (or B×M×N) device product.

a

The M×K (or B×M×K) device left operand.

b

The K×N (or B×K×N) device right operand (throws on an inner-dimension mismatch).

Complexity

O(M·N·K) device work (O(B·M·N·K) batched) in one blocking dispatch.

Host allocation

none — the dims scratch is a content-cached pooled buffer (repeat shapes reuse the same handle).

GPU allocation

none — all three buffers are the caller's.

Unit testgpu:matmul gpu:matmul_batched
fn void outer(Array< T > &out, const Array< T > &a, const Array< T > &b) #

Device outer product: out[n×m] = aᵢ·bⱼ.

Invoked by cheatah's allocating outer(a, b) front; one thread per output element over an m×n 2-D grid, blocking on completion.

Parameters
out

The pre-allocated n×m device product.

a

The length-n device vector.

b

The length-m device vector.

Complexity

O(n·m) device work in one blocking dispatch.

Host allocation

none — content-cached dims scratch only.

GPU allocation

none — all buffers are the caller's.

Unit testgpu:outer
fn void conj_transpose(Array< T > &out, const Array< T > &a) #

Device conjugate transpose: out[c×r] = aᴴ for an r×c input (the kernel conjugates complex elements; conjugation is the identity for real ones).

Invoked by cheatah's allocating front; one thread per input element, blocking on completion.

Parameters
out

The pre-allocated c×r device transpose.

a

The r×c device operand (throws unless 2-D).

Complexity

O(r·c) device work in one blocking dispatch.

Host allocation

none — content-cached dims scratch only.

GPU allocation

none — both buffers are the caller's.

Unit testgpu:transpose
fn void kron(Array< T > &out, const Array< T > &a, const Array< T > &b) #

Device Kronecker product: out[(ar·br)×(ac·bc)] = a ⊗ b.

Invoked by cheatah's allocating kron(a, b) front; one thread per output element, blocking on completion.

Parameters
out

The pre-allocated (ar·br)×(ac·bc) device product.

a

The ar×ac device left operand (throws unless 2-D).

b

The br×bc device right operand (throws unless 2-D).

Complexity

O(n⁴) in the output area (one multiply per output element).

Host allocation

none — content-cached dims scratch only.

GPU allocation

none — all buffers are the caller's.

Unit testgpu:kron
fn dot · 2 overloads
void dot(T &out, const Array< T > &a, const Array< T > &b)#
void dot(Array< T > &out, const Array< T > &a, const Array< T > &b)#

Device dot product (bilinear Σ aᵢbᵢ) into the caller's scalar — the DeviceArray overload of cheatah's scalar-out reduction kernel, dispatched as deterministic partial sums (the same bits every run), finalized on device and downloaded as ONE element.

Parameters
out

The caller's scalar result.

a

The first device vector.

b

The second device vector (throws on a length mismatch).

Complexity

O(n) device work; one fused submission (partial + finalize).

Host allocation

none — the partial/result scratch recycles through the pooled allocator; the readback is sizeof(T).

GPU allocation

none — pooled scratch only.

Unit testgpu:dot
fn void vdot(T &out, const Array< T > &a, const Array< T > &b) #

Device vdot: the Hermitian Σ conj(aᵢ)·bᵢ for complex elements (its own conjugating kernel); for real elements the conjugation is the identity and it shares dot's kernel.

Parameters
out

The caller's scalar result.

a

The first (conjugated) device vector.

b

The second device vector (throws on a length mismatch).

Complexity

O(n) device work; one fused submission (partial + finalize).

Host allocation

none — pooled partial/result scratch; sizeof(T) readback.

GPU allocation

none — pooled scratch only.

Unit testgpu:dot gpu:complex
fn void inner(T &out, const Array< T > &a, const Array< T > &b) #

Device inner product — the bilinear Σ aᵢbᵢ, identical to dot for flattened vectors (dispatched through the same kernel).

Parameters
out

The caller's scalar result.

a

The first device vector.

b

The second device vector (throws on a length mismatch).

Complexity

O(n) device work; one fused submission (partial + finalize).

Host allocation

none — pooled partial/result scratch; sizeof(T) readback.

GPU allocation

none — pooled scratch only.

Unit testgpu:dot
fn void trace(T &out, const Array< T > &a) #

Device trace (diagonal sum of a 2-D matrix) into the caller's scalar — deterministic partial sums over the strided diagonal, summed on the host in a fixed order.

Parameters
out

The caller's scalar result.

a

The r×c device matrix (min(r,c) diagonal elements are summed).

Complexity

O(min(r,c)) device work + O(P) host partial summation (P ≤ 256).

Host allocation

none — the P-element partial buffer and dims scratch recycle through the pool.

GPU allocation

none — pooled scratch only.

Unit testgpu:trace

Types

type cheatah::ndarray::basic_ndarray< T > host_array #

The host container under its device-parity name: step<host_array>(…) vs step<device_array>(…) is the entire CPU↔GPU switch.