forward method
void
forward(})
Runs n tokens at positions position, ... whose embeddings are
already in residual: every layer, and with withLogits the final
norm and LM head of the last token into logits. With capture, the
residual stream is copied after every layer (see captured).
With meanwhile the command buffer is committed without waiting, then
meanwhile runs on the CPU while the GPU computes, and the pass waits
for the GPU afterwards, also when meanwhile throws. It must not use
the GPU.
Implementation
void forward(
int n,
int position, {
required bool withLogits,
required bool capture,
void Function()? meanwhile,
}) {
if (_disposed) throw StateError('the GPU forward pass is disposed');
if (n < 1 || n > maxChunk) throw ArgumentError('chunk of $n tokens');
if (position + n > maxSeq) throw StateError('position exceeds maxSeq');
final sw = Stopwatch()..start();
final c = config;
final h = c.hiddenSize;
final hd = c.headDim;
final eps = c.normEps;
final pair = hd ~/ 2;
final rope = _rope.view;
for (var t = 0; t < n; t++) {
_fullRope.setPosition(position + t);
_slidingRope.setPosition(position + t);
final at = t * 4 * pair;
rope
..setAll(at, _fullRope.cos)
..setAll(at + pair, _fullRope.sin)
..setAll(at + 2 * pair, _slidingRope.cos)
..setAll(at + 3 * pair, _slidingRope.sin);
}
final steering = _steering;
var dispatched = 0;
void matmul(_Matrix w, _Buffer x, int y, [int yOffset = 0]) {
w.encode(x.handle, y, yOffset, n);
dispatched++;
}
void norm(_Buffer x, int w, _Buffer out, {bool accumulate = false}) {
_rmsNorm(
x.handle,
0,
_small.handle,
w,
out.handle,
0,
n,
h,
eps,
accumulate ? 1 : 0,
);
dispatched++;
}
void snapshot(int slot) {
_copy(_x.handle, 0, _capture.handle, slot * maxChunk * h * 4, n * h);
dispatched++;
}
void headNormRope(
int x,
int xOffset,
int xStride,
int w,
int heads,
int ropeOffset,
) {
_headNormRope(
x,
xOffset,
xStride,
_small.handle,
w,
_rope.handle,
ropeOffset,
4 * pair,
heads,
hd,
n,
eps,
);
dispatched++;
}
_begin();
if (capture) snapshot(0);
for (var i = 0; i < _layers.length; i++) {
final l = _layers[i];
norm(_x, l.operatorNorm, _a);
if (l.inProj != null) {
matmul(l.inProj!, _a, _bcx.handle);
_conv(
_bcx.handle,
_small.handle,
l.convKernel,
l.history,
_y.handle,
h,
c.convKernel,
n,
);
dispatched++;
matmul(l.outProj!, _y, _m.handle);
} else {
// K and V go straight into the chunk's cache rows.
final rows = position * c.kvDim * 4;
final ropeOffset = (l.sliding ? 2 * pair : 0) * 4;
matmul(l.q!, _a, _q.handle);
matmul(l.k!, _a, l.keys, rows);
matmul(l.v!, _a, l.values, rows);
matmul(l.gate!, _a, _gate.handle);
headNormRope(
_q.handle,
0,
c.numHeads * hd,
l.qNorm,
c.numHeads,
ropeOffset,
);
headNormRope(l.keys, rows, c.kvDim, l.kNorm, c.numKvHeads, ropeOffset);
_attention(
_q.handle,
l.keys,
l.values,
_gate.handle,
_attnOut.handle,
position,
n,
c.numHeads,
c.numKvHeads,
hd,
l.sliding ? c.slidingWindow : 0,
);
dispatched++;
matmul(l.o!, _attnOut, _m.handle);
}
norm(_m, l.postOperatorNorm, _x, accumulate: true);
norm(_x, l.ffnNorm, _a);
matmul(l.w1, _a, _g.handle);
matmul(l.w3, _a, _u.handle);
_swiglu(_g.handle, _u.handle, n * c.intermediateSize);
dispatched++;
matmul(l.w2, _g, _m.handle);
norm(_m, l.postFfnNorm, _x, accumulate: true);
// The residual leaving layer i is complete: steer it before the
// capture, the next layer and the LM head read it.
if (steering != null && steering.layers[i]) {
_steer(
_x.handle,
0,
steering.buffer.handle,
i * h * 4,
n,
h,
steering.mode,
steering.scale,
);
dispatched++;
}
if (capture) snapshot(i + 1);
}
if (withLogits) {
_rmsNorm(
_x.handle,
(n - 1) * h * 4,
_small.handle,
_finalNorm,
_normed.handle,
0,
1,
h,
eps,
0,
);
_lmHead.encode(_normed.handle, _logits.handle, 0, 1);
dispatched += 2;
}
encodeMicros += sw.elapsedMicroseconds;
if (meanwhile == null) {
_finish();
} else {
if (_commit(0) != 1) throw StateError('GPU command buffer failed');
try {
meanwhile();
} finally {
final ok = _waitGpu() == 1;
gpuMicros += (_lastGpuMs() * 1000).round();
if (!ok) throw StateError('GPU command buffer failed');
}
}
calls++;
tokens += n;
dispatches += dispatched;
}