memra-engine 0.132.0

From-scratch CUDA LLM inference engine for NVIDIA RTX 50-series (sm_120a) and Hopper (sm_90a) - custom kernels, no frameworks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//! Gate-only multi-device graph for the matrix EP plain path.
//!
//! The graph is deliberately a parent graph with per-device child graphs.  The
//! owner and peer expert halves are captured on their own streams; the parent
//! owns the explicit owner->peer and peer->owner copies and the fork/join edges.
//! This is the shape that can include the real matrix EP join without trying to
//! capture `cudaMemcpyPeerAsync` itself.

use crate::dsv4_ep::{EpCompute, EpLayer, EpScratch};
use crate::dsv4_ffi as k;
use crate::dsv4_grouped::GroupedWork;
use cudarc::driver::{CudaGraph, CudaSlice, DevicePtr, DevicePtrMut};
use memra_runtime::Gpu;
use std::ffi::c_void;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};

type Res<T> = Result<T, String>;

static MATRIX_EP_GRAPH_FOR_GATE: AtomicBool = AtomicBool::new(false);
static MATRIX_EP_GRAPH_DISPATCHES: AtomicU64 = AtomicU64::new(0);

/// Gate-only process switch.  It has no environment reader or serving default.
pub(crate) fn set_for_gate(enabled: bool) -> bool {
    MATRIX_EP_GRAPH_FOR_GATE.swap(enabled, Ordering::AcqRel)
}

pub(crate) fn enabled() -> bool {
    MATRIX_EP_GRAPH_FOR_GATE.load(Ordering::Acquire)
}

pub(crate) fn dispatches() -> u64 {
    MATRIX_EP_GRAPH_DISPATCHES.load(Ordering::Acquire)
}

#[derive(Debug, Clone, Copy)]
struct CopySpec {
    src: cudarc::driver::sys::CUdeviceptr,
    dst: cudarc::driver::sys::CUdeviceptr,
    graph_ctx: cudarc::driver::sys::CUcontext,
    bytes: usize,
}

/// Parent graph plus the child graphs it references.  Child graphs must stay
/// alive until the parent exec is destroyed: CUDA clones their topology into
/// the parent at AddChildGraphNode time, but the retained handles are kept here
/// to preserve the allocation and module lifetime contract explicitly.
pub(crate) struct MatrixEpGraph {
    exec: cudarc::driver::sys::CUgraphExec,
    parent: cudarc::driver::sys::CUgraph,
    _children: Vec<CudaGraph>,
}

// The graph is owned by one decode state and launched by the decode thread.
// CUDA graph handles are process handles, not Rust-thread-bound references.
unsafe impl Send for MatrixEpGraph {}

impl Drop for MatrixEpGraph {
    fn drop(&mut self) {
        unsafe {
            let _ = cudarc::driver::sys::cuGraphExecDestroy(self.exec);
            let _ = cudarc::driver::sys::cuGraphDestroy(self.parent);
        }
    }
}

impl MatrixEpGraph {
    pub(crate) fn launch(&self, owner: &Gpu) -> Res<()> {
        owner.ctx.bind_to_thread().map_err(|e| e.to_string())?;
        let rc = unsafe {
            cudarc::driver::sys::cuGraphLaunch(
                self.exec,
                owner.stream().cu_stream() as cudarc::driver::sys::CUstream,
            )
        };
        if rc != cudarc::driver::sys::CUresult::CUDA_SUCCESS {
            return Err(format!("matrix EP graph launch: {rc:?}"));
        }
        MATRIX_EP_GRAPH_DISPATCHES.fetch_add(1, Ordering::AcqRel);
        Ok(())
    }
}

/// Empty/ready/fail-latched state for one model layer.  A refusal must not
/// recapture every token while the request continues on the eager twin.
pub(crate) enum MatrixEpGraphSlot {
    Empty,
    Ready(MatrixEpGraph),
    Refused,
}

impl MatrixEpGraphSlot {
    pub(crate) fn empty() -> Self {
        Self::Empty
    }
}

fn capture_child(gpu: &Gpu, mut body: impl FnMut() -> Res<()>) -> Res<CudaGraph> {
    use cudarc::driver::sys::{CUgraphInstantiate_flags, CUstreamCaptureMode};

    if gpu.ctx.is_event_tracking() {
        return Err("matrix EP graph requires event tracking disabled before allocation".into());
    }
    gpu.ctx.bind_to_thread().map_err(|e| e.to_string())?;
    let stream = gpu.stream();
    stream.synchronize().map_err(|e| e.to_string())?;
    stream
        .begin_capture(CUstreamCaptureMode::CU_STREAM_CAPTURE_MODE_RELAXED)
        .map_err(|e| format!("matrix EP graph begin capture: {e}"))?;
    let result = body();
    let ended = stream
        .end_capture(CUgraphInstantiate_flags::CUDA_GRAPH_INSTANTIATE_FLAG_AUTO_FREE_ON_LAUNCH);
    result?;
    let graph = ended
        .map_err(|e| format!("matrix EP graph end capture: {e}"))?
        .ok_or("matrix EP graph produced no child graph")?;
    graph
        .upload()
        .map_err(|e| format!("matrix EP graph child upload: {e}"))?;
    Ok(graph)
}

fn add_child(
    parent: cudarc::driver::sys::CUgraph,
    deps: &[cudarc::driver::sys::CUgraphNode],
    child: &CudaGraph,
) -> Res<cudarc::driver::sys::CUgraphNode> {
    let mut node = std::ptr::null_mut();
    let rc = unsafe {
        cudarc::driver::sys::cuGraphAddChildGraphNode(
            &mut node,
            parent,
            if deps.is_empty() {
                std::ptr::null()
            } else {
                deps.as_ptr()
            },
            deps.len(),
            child.cu_graph(),
        )
    };
    if rc != cudarc::driver::sys::CUresult::CUDA_SUCCESS {
        return Err(format!("matrix EP graph add child: {rc:?}"));
    }
    Ok(node)
}

fn add_copy(
    parent: cudarc::driver::sys::CUgraph,
    deps: &[cudarc::driver::sys::CUgraphNode],
    spec: CopySpec,
) -> Res<cudarc::driver::sys::CUgraphNode> {
    let copy = cudarc::driver::sys::CUDA_MEMCPY3D_st {
        srcXInBytes: 0,
        srcY: 0,
        srcZ: 0,
        srcLOD: 0,
        srcMemoryType: cudarc::driver::sys::CUmemorytype_enum::CU_MEMORYTYPE_DEVICE,
        srcHost: std::ptr::null(),
        srcDevice: spec.src,
        srcArray: std::ptr::null_mut(),
        reserved0: std::ptr::null_mut(),
        srcPitch: spec.bytes,
        srcHeight: 1,
        dstXInBytes: 0,
        dstY: 0,
        dstZ: 0,
        dstLOD: 0,
        dstMemoryType: cudarc::driver::sys::CUmemorytype_enum::CU_MEMORYTYPE_DEVICE,
        dstHost: std::ptr::null_mut(),
        dstDevice: spec.dst,
        dstArray: std::ptr::null_mut(),
        reserved1: std::ptr::null_mut(),
        dstPitch: spec.bytes,
        dstHeight: 1,
        WidthInBytes: spec.bytes,
        Height: 1,
        Depth: 1,
    };
    let mut node = std::ptr::null_mut();
    let rc = unsafe {
        cudarc::driver::sys::cuGraphAddMemcpyNode(
            &mut node,
            parent,
            if deps.is_empty() {
                std::ptr::null()
            } else {
                deps.as_ptr()
            },
            deps.len(),
            &copy,
            spec.graph_ctx,
        )
    };
    if rc != cudarc::driver::sys::CUresult::CUDA_SUCCESS {
        return Err(format!("matrix EP graph add P2P copy: {rc:?}"));
    }
    Ok(node)
}

fn add_parent(
    owner_child: CudaGraph,
    peer_child: CudaGraph,
    post_child: CudaGraph,
    copies: [CopySpec; 5],
) -> Res<MatrixEpGraph> {
    use cudarc::driver::sys;
    let mut parent = std::ptr::null_mut();
    let rc = unsafe { sys::cuGraphCreate(&mut parent, 0) };
    if rc != sys::CUresult::CUDA_SUCCESS {
        return Err(format!("matrix EP graph create parent: {rc:?}"));
    }
    let result = (|| -> Res<MatrixEpGraph> {
        let owner_node = add_child(parent, &[], &owner_child)?;
        let tx0 = add_copy(parent, &[], copies[0])?;
        let tx1 = add_copy(parent, &[], copies[1])?;
        let tx2 = add_copy(parent, &[], copies[2])?;
        let tx3 = add_copy(parent, &[], copies[3])?;
        let tx = [tx0, tx1, tx2, tx3];
        let peer_node = add_child(parent, &tx, &peer_child)?;
        let rx_node = add_copy(parent, &[peer_node], copies[4])?;
        let post_deps = [owner_node, rx_node];
        let _post_node = add_child(parent, &post_deps, &post_child)?;
        let mut exec = std::ptr::null_mut();
        let rc = unsafe { sys::cuGraphInstantiateWithFlags(&mut exec, parent, 0) };
        if rc != sys::CUresult::CUDA_SUCCESS {
            return Err(format!("matrix EP graph instantiate: {rc:?}"));
        }
        Ok(MatrixEpGraph {
            exec,
            parent,
            _children: vec![owner_child, peer_child, post_child],
        })
    })();
    if result.is_err() {
        unsafe {
            let _ = sys::cuGraphDestroy(parent);
        }
    }
    result
}

struct MergeSpec {
    local_contribution: cudarc::driver::sys::CUdeviceptr,
    returned: cudarc::driver::sys::CUdeviceptr,
    ids: cudarc::driver::sys::CUdeviceptr,
    slots: i32,
    hidden: i32,
    peer_first: i32,
    count: i32,
    stream: *mut c_void,
}

/// Build phase for the matrix EP graph.  The owner/peer `EpCompute` borrows are
/// confined to this method; that lets the caller capture the owner FFN tail
/// after the expert workspaces have been released.
pub(crate) struct MatrixEpGraphBuilder {
    owner: *const Gpu,
    owner_child: CudaGraph,
    peer_child: CudaGraph,
    copies: [CopySpec; 5],
    merge: MergeSpec,
}

impl MatrixEpGraphBuilder {
    // Capture borrows the existing two-rank workspaces and their exact kernel dimensions.
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn capture_expert(
        owner: &Gpu,
        peer: &Gpu,
        bank: &EpLayer,
        table: &CudaSlice<u64>,
        scale2: &CudaSlice<f32>,
        scale2_host: &[f32],
        local: &mut EpCompute<'_>,
        local_work: &mut GroupedWork,
        remote: &mut EpScratch,
        rows: usize,
        topk: usize,
        hidden: usize,
        limit: f32,
        allow_gu_fuse: bool,
    ) -> Res<Self> {
        if rows != 1 || topk == 0 || hidden == 0 {
            return Err("matrix EP graph currently supports only non-empty t=1 decode".into());
        }
        if crate::dsv4_grouped::route_validation_enabled()
            || crate::dsv4_grouped::mirror_validation_enabled()
        {
            return Err("matrix EP graph requires route and mirror validation disabled".into());
        }
        let peer_table = bank
            .peer_table
            .as_ref()
            .ok_or("matrix EP peer table missing")?;
        let global = bank
            .count
            .checked_mul(2)
            .ok_or("matrix EP graph expert count overflow")?;
        let slots = rows * topk;
        if !((bank.local_first == 0 && bank.peer_first == bank.count)
            || (bank.peer_first == 0 && bank.local_first == bank.count))
            || !local_work
                .routes
                .matches_partition(global, bank.local_first, bank.count)
            || !remote.grouped.as_ref().is_some_and(|work| {
                work.routes
                    .matches_partition(global, bank.peer_first, bank.count)
            })
        {
            return Err("matrix EP graph ownership mismatch".into());
        }
        local_work.set_gu_fuse_for_plain(allow_gu_fuse);
        if let Some(work) = remote.grouped.as_mut() {
            work.set_gu_fuse_for_plain(allow_gu_fuse);
        } else {
            return Err("matrix EP graph peer grouped workspace missing".into());
        }

        let owner_child = capture_child(owner, || {
            local_work.prepare(owner, local, scale2, scale2_host, rows, topk, true)?;
            local_work.gate_up(owner, table, local, limit)?;
            local_work.down(owner, table, local)?;
            Ok(())
        })?;

        let peer_child = {
            let peer_work = remote
                .grouped
                .as_mut()
                .ok_or("matrix EP graph peer grouped workspace missing")?;
            let mut peer_compute = EpCompute {
                xq: &remote.xq,
                xs: &remote.xs,
                ids: &remote.ids,
                weights: &remote.weights,
                g1: &mut remote.g1,
                g3: &mut remote.g3,
                h: &mut remote.h,
                hq: &mut remote.hq,
                hs: &mut remote.hs,
                contribution: &mut remote.contribution,
            };
            capture_child(peer, || {
                peer_work.prepare(
                    peer,
                    &peer_compute,
                    &bank.peer_s2,
                    scale2_host,
                    rows,
                    topk,
                    true,
                )?;
                peer_work.gate_up(peer, peer_table, &mut peer_compute, limit)?;
                peer_work.down(peer, peer_table, &mut peer_compute)?;
                Ok(())
            })?
        };

        let os = owner.stream();
        let ps = peer.stream();
        let owner_ctx = owner.ctx.cu_ctx();
        let peer_ctx = peer.ctx.cu_ctx();
        let ptr = |s: &cudarc::driver::CudaStream, x: &CudaSlice<u8>| x.device_ptr(s).0;
        let (src_xq, src_xs, src_ids, src_weights) = (
            ptr(&os, local.xq),
            local.xs.device_ptr(&os).0,
            local.ids.device_ptr(&os).0,
            local.weights.device_ptr(&os).0,
        );
        let (dst_xq, dst_xs, dst_ids, dst_weights) = (
            remote.xq.device_ptr_mut(&ps).0,
            remote.xs.device_ptr_mut(&ps).0,
            remote.ids.device_ptr_mut(&ps).0,
            remote.weights.device_ptr_mut(&ps).0,
        );
        let bytes_xq = rows * hidden;
        let bytes_xs = rows * hidden / 128 * std::mem::size_of::<f32>();
        let bytes_slots = slots * std::mem::size_of::<i32>();
        let bytes_weights = slots * std::mem::size_of::<f32>();
        let bytes_contribution = slots * hidden * std::mem::size_of::<f32>();
        let (rx_src, rx_dst) = (
            remote.contribution.device_ptr(&ps).0,
            remote.returned.device_ptr_mut(&os).0,
        );
        let copies = [
            CopySpec {
                src: src_xq,
                dst: dst_xq,
                graph_ctx: owner_ctx,
                bytes: bytes_xq,
            },
            CopySpec {
                src: src_xs,
                dst: dst_xs,
                graph_ctx: owner_ctx,
                bytes: bytes_xs,
            },
            CopySpec {
                src: src_ids,
                dst: dst_ids,
                graph_ctx: owner_ctx,
                bytes: bytes_slots,
            },
            CopySpec {
                src: src_weights,
                dst: dst_weights,
                graph_ctx: owner_ctx,
                bytes: bytes_weights,
            },
            CopySpec {
                src: rx_src,
                dst: rx_dst,
                graph_ctx: peer_ctx,
                bytes: bytes_contribution,
            },
        ];

        Ok(Self {
            owner,
            owner_child,
            peer_child,
            copies,
            merge: MergeSpec {
                local_contribution: local.contribution.device_ptr_mut(&os).0,
                returned: remote.returned.device_ptr(&os).0,
                ids: local.ids.device_ptr(&os).0,
                slots: slots as i32,
                hidden: hidden as i32,
                peer_first: bank.peer_first as i32,
                count: bank.count as i32,
                stream: os.cu_stream().cast(),
            },
        })
    }

    /// Finish the parent graph after the owner-side FFN tail closure is ready.
    pub(crate) fn finish<F>(self, mut post: F) -> Res<MatrixEpGraph>
    where
        F: FnMut() -> Res<()>,
    {
        let owner = unsafe { &*self.owner };
        let owner_post = capture_child(owner, || {
            unsafe {
                k::ck(
                    "matrix EP graph merge",
                    k::memra_dsv4_ep_merge_slots(
                        self.merge.local_contribution as *mut f32,
                        self.merge.returned as *const f32,
                        self.merge.ids as *const i32,
                        self.merge.slots,
                        self.merge.hidden,
                        self.merge.peer_first,
                        self.merge.count,
                        self.merge.stream,
                    ),
                )?;
            }
            post()
        })?;
        add_parent(self.owner_child, self.peer_child, owner_post, self.copies)
    }
}