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
7
namespace
ember_ir
{
8
namespace
cpu {
9
10
void
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
25
void
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
n
int n
Definition
graph_ir_cuda.cu:326
v
std::vector< float > v
Definition
graph_ir_expr.cpp:257
c
float c
Definition
graph_ir_expr.cpp:256
graph_ir_mamba.h
k
K k
Definition
graph_ir_recurrence.cpp:124
state
bool state
Definition
graph_ir_recurrence.cpp:182
out
node out
Definition
graph_ir_schema.h:9
j
size_t j
Definition
graph_ir_validate.cpp:222
ember_ir::cpu::conv1d_causal_row
void conv1d_causal_row(const float *x, const float *w, const float *bias, float *state, float *out, int C, int K)
Definition
graph_ir_mamba.cpp:10
ember_ir::cpu::selective_scan_row
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
graph_ir_mamba.cpp:25
ember_ir
Definition
api.h:20
graph_ir_mamba.cpp
Generated by
1.9.8