Expand description
Shared candle tensor primitives duplicated (byte-for-byte, or modulo a
hardcoded-vs-parameterized constant) across the per-model infer/mod.rs
files in crates/models/*. Extracted so the arithmetic lives in one
place; every call site keeps producing bit-identical results to its
former private copy.
Functions§
- layer_
norm - Standard LayerNorm over the last dim:
(x - mean) / sqrt(var + eps) * weight + bias. - linear
y = x @ w^T + b, flattening any leading dims ofxinto a batch dim so this works for rank-2 or higher inputs against a rank-2 weight[d_out, d_in].- linear_
bias - linear_
nobias - load_
tensor - Dequantize a named GGUF tensor and cast it to
dtype. - load_
vec - Dequantize a named GGUF tensor to F32 and flatten it to a plain
Vec<f32>. - load_
weight - Load an F32 weight PyTorch stores as
(d_out, d_in). Candle reverses the GGUF shape back to(d_out, d_in); thedim(0)check guards against a stray transposed store (the Q8_0 transpose trick some converters apply) by flipping the tensor back. - make_
causal_ mask - Additive causal mask of shape
[1, 1, seq, seq]:0.0wherenum_masked <= k <= q,-infelsewhere.num_maskedlets a prefix of keys be masked out regardless of query position (used for left-padded contexts); pass0for a plain causal mask. - rms_
norm - RMSNorm over the last dim:
x / sqrt(mean(x^2) + eps) * weight(weight optional — some models fold the scale into a separate op and call this withNone). Computes inx’s own dtype — cast beforehand if a caller needs a fixed compute precision regardless of input dtype. - swiglu_
ffn - SwiGLU feed-forward:
w2(silu(w1(x)) * w3(x)), all projections bias-free. - try_
load_ tensor - Same as
load_tensor, but returnsOk(None)instead of erroring when the tensor is absent.