Skip to content

Commit 5c5228c

Browse files
committed
flashnext cuda: the fused read-outs' branches live in cuda_batch_hc, so cuda_batch.zig stays within 600 lines
The attention and MoE blocks' writeback and read-out (fused where cuda_batch_hc.fuses allows, else the two launches) move from cuda_batch.zig into cuda_batch_hc.attnReadout and mlpReadout: the same calls in the same order.
1 parent 3a3f6b6 commit 5c5228c

2 files changed

Lines changed: 23 additions & 14 deletions

File tree

‎zig/src/families/flashnext/cuda_batch.zig‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -539,13 +539,7 @@ pub fn Pass(comptime Tri: type) type {
539539
if (self.pending != null) try self.writeback(0, n);
540540
try self.pleBlock(lw, ids);
541541
}
542-
if (hc.fuses(self, lw.attn_hc, n, keep.attn_hc)) {
543-
try hc.writebackNorm(self, lw.attn_hc, 0, n);
544-
try hc.readoutNative(self, lw.attn_hc, 0, n);
545-
} else {
546-
try self.writeback(0, n);
547-
try self.readoutKeep(lw.attn_hc, 0, n, keep.attn_hc);
548-
}
542+
try hc.attnReadout(self, lw.attn_hc, n, keep.attn_hc);
549543
self.zero_slices = zs.usable(self.s.part_zero, keep.mix_branch, self.m.collective != null, lw.out.native != null);
550544
if (lw.linear) {
551545
try self.gdnBlock(lw, layer, wt.linearIndex(layer), keep);
@@ -555,13 +549,7 @@ pub fn Pass(comptime Tri: type) type {
555549
if (keep.mix_branch != 0) try self.sumSlices(s.part, keep.mix_branch, true, n * dims, self.out_slices);
556550
const sl = zs.slices(self.zero_slices, s.cap, n, self.out_slices);
557551
self.zero_slices = false;
558-
if (hc.fuses(self, lw.mlp_hc, n, keep.mlp_hc)) {
559-
try self.tri.hcWritebackNorm(s.h, s.h, s.pss, s.part, s.inj, s.dummy, s.dummy, lw.mlp_hc.scale.ptr, s.norm, eps, sl.stride, n, dims, streams, 4, 1, 1, sl.count);
560-
try hc.readoutNative(self, lw.mlp_hc, 0, n);
561-
} else {
562-
try self.tri.hcWriteback(s.h, s.h, s.pss, s.part, "*fp32", s.inj, s.dummy, "*bf16", s.dummy, "*bf16", sl.stride, n, dims, streams, 4, 1, 1, sl.count);
563-
try self.readoutKeep(lw.mlp_hc, 0, n, keep.mlp_hc);
564-
}
552+
try hc.mlpReadout(self, lw.mlp_hc, n, keep.mlp_hc, sl);
565553
try self.moeBlockKeep(lw, keep);
566554
try self.moeAdds(layer, keep);
567555
self.pending = layer;

‎zig/src/families/flashnext/cuda_batch_hc.zig‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,24 @@ pub fn readoutNative(pass: anytype, hc_w: wt.HcW, r0: usize, n: usize) !void {
6060
try pass.k.nativeMatmul(pass.stream, act, low, n, up, s.hc_up, s.native_part, .bf16);
6161
try pass.tri.hcDenseMix(s.hc_up, norm, s.mixed + r0 * dims * 2, s.xsm + r0 * (dims / 32) * 4, n, dims, streams);
6262
}
63+
64+
/// The attention block's pending writeback and its read-out: fused where fuses() allows, else the two as before.
65+
pub fn attnReadout(pass: anytype, hc_w: wt.HcW, n: usize, keep: parts.HcKeep) !void {
66+
if (fuses(pass, hc_w, n, keep)) {
67+
try writebackNorm(pass, hc_w, 0, n);
68+
return readoutNative(pass, hc_w, 0, n);
69+
}
70+
try pass.writeback(0, n);
71+
try pass.readoutKeep(hc_w, 0, n, keep);
72+
}
73+
74+
/// The MoE block's writeback of part over its slices (sl) and its read-out, fused the same way.
75+
pub fn mlpReadout(pass: anytype, hc_w: wt.HcW, n: usize, keep: parts.HcKeep, sl: anytype) !void {
76+
const s = pass.s;
77+
if (fuses(pass, hc_w, n, keep)) {
78+
try pass.tri.hcWritebackNorm(s.h, s.h, s.pss, s.part, s.inj, s.dummy, s.dummy, hc_w.scale.ptr, s.norm, eps, sl.stride, n, dims, streams, 4, 1, 1, sl.count);
79+
return readoutNative(pass, hc_w, 0, n);
80+
}
81+
try pass.tri.hcWriteback(s.h, s.h, s.pss, s.part, "*fp32", s.inj, s.dummy, "*bf16", s.dummy, "*bf16", sl.stride, n, dims, streams, 4, 1, 1, sl.count);
82+
try pass.readoutKeep(hc_w, 0, n, keep);
83+
}

0 commit comments

Comments
 (0)