Skip to content

Commit 3866f76

Browse files
committed
feat: Use local variable for state recursion
Branch: GraniteFourPerf Signed-off-by: Gabe Goodhart <[email protected]>
1 parent a5334f9 commit 3866f76

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

ggml/src/ggml-metal/ggml-metal.metal

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,8 +1788,11 @@ kernel void kernel_ssm_scan_f32_group(
17881788

17891789
device const int32_t * ids = (device const int32_t *) src6;
17901790

1791-
device const float * s0 = (device const float *) ((device const char *) src0 + ir*args.nb02 + ids[i3]*args.nb03);
1792-
device float * s = (device float *) ((device char *) dst + ir*args.nb02 + i3*args.nb03 + s_off);
1791+
device const float * s0_buff = (device const float *) ((device const char *) src0 + ir*args.nb02 + ids[i3]*args.nb03);
1792+
device float * s_buff = (device float *) ((device char *) dst + ir*args.nb02 + i3*args.nb03 + s_off);
1793+
const int64_t i = tpitg.x + i1*nc;
1794+
float s0 = s0_buff[i];
1795+
float s = s_buff[i];
17931796

17941797
device const float * A = (device const float *) ((device const char *) src3 + ir*args.nb31); // {1, nh}
17951798
device const float * x_block = (device const float *) ((device const char *) src1 + i1*nb10 + ir*args.nb11 + i3*args.nb13);
@@ -1809,9 +1812,8 @@ kernel void kernel_ssm_scan_f32_group(
18091812
const float x_dt = x[0] * dt_soft_plus;
18101813
const float dA = exp(dt_soft_plus * A[0]);
18111814

1812-
const int64_t i = tpitg.x + i1*nc;
1813-
const float state = (s0[i] * dA) + (B[tpitg.x] * x_dt);
1814-
s[i] = state;
1815+
const float state = (s0 * dA) + (B[tpitg.x] * x_dt);
1816+
s = state;
18151817

18161818
// Parallel sum: This relies on the fact that this kernel will be
18171819
// dispatched with each threadgroup having (d_state, 1, 1) threads which
@@ -1851,6 +1853,9 @@ kernel void kernel_ssm_scan_f32_group(
18511853
// recurse
18521854
s0 = s;
18531855
}
1856+
1857+
// Assign the final state to the output buffer
1858+
s_buff[i] = s;
18541859
}
18551860

18561861
kernel void kernel_rwkv_wkv6_f32(

0 commit comments

Comments
 (0)