ember.cpp 0.1.0
C++ inference engine for Llama-family and paperkiln models (CPU, CUDA, WebAssembly)
Loading...
Searching...
No Matches
graph_ir_mamba.h
Go to the documentation of this file.
1#pragma once
2// CPU kernels for Mamba-1's IR ops (paperkiln include/microtorch/ir.hpp: conv1d_causal and
3// selective_scan; numpy reference tools/ir_numpy.py; llama.cpp ggml_ssm_conv / ggml_ssm_scan).
4// One token (row) per call, so prefill is the sequential scan and decode its T = 1 case, and the
5// interpreter can snapshot the state after any row. Every product is rounded before it is added
6// (compiled with -ffp-contract=off) and sums run in the ascending order the spec names. ROUNDING.
7namespace ember_ir {
8namespace cpu {
9
10// Depthwise causal convolution over time. x [C] is this token; state [K - 1, C] holds the K - 1
11// previous inputs, oldest first (0 at the start). With xx = state's rows then x:
12// out[c] = sum over k = 0..K-1, ascending, of w[c, k] * xx[k][c] (+ bias[c] after the sum)
13// then state becomes the last K - 1 rows of xx. w is [C, K] row-major; bias may be null.
14void conv1d_causal_row(const float* x, const float* w, const float* bias, float* state, float* out, int C, int K);
15
16// Mamba-1's selective scan, one token. x and dt [D], B and C [N]; A [D, N] as stored (llama.cpp
17// writes -exp(A_log)); d [D] or null; s [D, N] the state. For every channel c:
18// delta = softplus ? (dt[c] <= 20 ? log1p(exp(dt[c])) : dt[c]) : dt[c]
19// xd = x[c] * delta; s[c, n] = s[c, n] * exp(delta * A[c, n]) + B[n] * xd
20// y[c] = sum over n ascending of s[c, n] * C[n] (+ d[c] * x[c] after the sum)
21void selective_scan_row(const float* x, const float* dt, const float* B, const float* C, const float* A,
22 const float* d, float* s, float* y, int D, int N, bool softplus);
23
24} // namespace cpu
25} // namespace ember_ir
node out
size_t j
void conv1d_causal_row(const float *x, const float *w, const float *bias, float *state, float *out, int C, int K)
void selective_scan_row(const float *x, const float *dt, const float *B, const float *C, const float *A, const float *d, float *s, float *y, int D, int N, bool softplus)
Definition api.h:20