ember.cpp 0.1.0
C++ inference engine for Llama-family and paperkiln models (CPU, CUDA, WebAssembly)
Loading...
Searching...
No Matches
graph_ir_mamba.cpp
Go to the documentation of this file.
1#include "graph_ir_mamba.h"
2
3#include <cmath>
4#include <cstddef>
5#include <cstring>
6
7namespace ember_ir {
8namespace cpu {
9
10void conv1d_causal_row(const float* x, const float* w, const float* bias, float* state, float* out, int C, int K) {
11 for (int c = 0; c < C; ++c) {
12 float acc = 0.0f;
13 for (int k = 0; k < K; ++k) {
14 const float v = k < K - 1 ? state[static_cast<size_t>(k) * C + c] : x[c];
15 acc = acc + w[static_cast<size_t>(c) * K + k] * v;
16 }
17 out[c] = bias ? acc + bias[c] : acc;
18 }
19 if (K > 1) { // the state becomes the last K - 1 rows of [state; x]
20 std::memmove(state, state + C, static_cast<size_t>(K - 2) * C * sizeof(float));
21 std::memcpy(state + static_cast<size_t>(K - 2) * C, x, static_cast<size_t>(C) * sizeof(float));
22 }
23}
24
25void selective_scan_row(const float* x, const float* dt, const float* B, const float* C, const float* A,
26 const float* d, float* s, float* y, int D, int N, bool softplus) {
27 for (int c = 0; c < D; ++c) {
28 const float delta = softplus ? (dt[c] <= 20.0f ? std::log1p(std::exp(dt[c])) : dt[c]) : dt[c];
29 const float xd = x[c] * delta;
30 float* sc = s + static_cast<size_t>(c) * N;
31 const float* ac = A + static_cast<size_t>(c) * N;
32 float acc = 0.0f;
33 for (int n = 0; n < N; ++n) {
34 const float decay = sc[n] * std::exp(delta * ac[n]);
35 sc[n] = decay + B[n] * xd;
36 acc = acc + sc[n] * C[n];
37 }
38 y[c] = d ? acc + d[c] * x[c] : acc;
39 }
40}
41
42} // namespace cpu
43} // namespace ember_ir
int n
std::vector< float > v
float c
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