rlx-cpu 0.2.17

CPU backend for RLX — SIMD kernels, BLAS dispatch, thread pool, arena executor
Documentation
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
// RLX — versatile ML compiler + runtime.
// Copyright (C) 2026 Eugene Hauptmann, Nataliya Kosmyna.
// SPDX-License-Identifier: MIT OR Apache-2.0
#![allow(unsafe_op_in_unsafe_fn)]
use crate::thunk::*;

#[allow(unused_variables)]
pub(crate) fn compile_custom(
    node: &rlx_ir::Node,
    graph: &Graph,
    arena: &crate::arena::Arena,
    matmul_fold: &std::collections::HashMap<NodeId, (NodeId, bool, NodeId, bool)>,
    rng_shared: &std::sync::Arc<std::sync::RwLock<rlx_ir::RngOptions>>,
    rng: rlx_ir::RngOptions,
) -> Thunk {
    let Op::Custom { name, attrs, .. } = &node.op else {
        unreachable!()
    };
    {
        let kernel = crate::op_registry::lookup_cpu_kernel(name).unwrap_or_else(|| {
            panic!(
                "compile_thunks: no CPU kernel registered for \
                         Op::Custom('{name}'). Register one via \
                         rlx_cpu::op_registry::register_cpu_kernel \
                         before compiling on the CPU backend."
            )
        });
        let inputs_v: Vec<(usize, u32, Shape)> = node
            .inputs
            .iter()
            .map(|&in_id| {
                let s = graph.node(in_id).shape.clone();
                let len = s.num_elements().unwrap_or(0) as u32;
                (node_offset(arena, in_id), len, s)
            })
            .collect();
        let out_len = node.shape.num_elements().unwrap_or(0) as u32;
        Thunk::CustomOp {
            kernel,
            inputs: inputs_v,
            output: (node_offset(arena, node.id), out_len, node.shape.clone()),
            attrs: attrs.clone(),
        }
    }
}

/// Lower a core SPD-manifold op to a `Thunk::CustomOp` carrying a
/// directly-instantiated F64 kernel (see `crate::spd_kernels`). Mirrors
/// [`compile_custom`] but takes the kernel by value instead of a
/// registry lookup — these are built-in, not user-registered.
pub(crate) fn compile_spd_custom(
    node: &rlx_ir::Node,
    graph: &Graph,
    arena: &crate::arena::Arena,
    kernel: std::sync::Arc<dyn crate::op_registry::CpuKernel>,
) -> Thunk {
    let inputs_v: Vec<(usize, u32, Shape)> = node
        .inputs
        .iter()
        .map(|&in_id| {
            let s = graph.node(in_id).shape.clone();
            let len = s
                .num_elements()
                .expect("SPD-manifold op requires statically-shaped inputs")
                as u32;
            (node_offset(arena, in_id), len, s)
        })
        .collect();
    let out_len = node
        .shape
        .num_elements()
        .expect("SPD-manifold op requires a statically-shaped output") as u32;
    Thunk::CustomOp {
        kernel,
        inputs: inputs_v,
        output: (node_offset(arena, node.id), out_len, node.shape.clone()),
        attrs: Vec::new(),
    }
}

#[allow(unused_variables)]
pub(crate) fn compile_custom_fn(
    node: &rlx_ir::Node,
    graph: &Graph,
    arena: &crate::arena::Arena,
    matmul_fold: &std::collections::HashMap<NodeId, (NodeId, bool, NodeId, bool)>,
    rng_shared: &std::sync::Arc<std::sync::RwLock<rlx_ir::RngOptions>>,
    rng: rlx_ir::RngOptions,
) -> Thunk {
    let Op::CustomFn {
        fwd_body,
        num_inputs,
        ..
    } = &node.op
    else {
        unreachable!()
    };
    {
        // Plan + compile the body sub-graph standalone, fill its
        // Constants (mirrors the Op::Scan body lowering), then
        // capture per-input copy specs and the output spec.
        // Body Inputs in NodeId order match the outer node's
        // operand vector by position.
        let body_plan = rlx_opt::memory::plan_memory(fwd_body);
        let body_offsets: HashMap<NodeId, usize> = body_plan
            .assignments
            .iter()
            .map(|(id, slot)| (*id, slot.offset))
            .collect();

        let mut body_input_ids: Vec<NodeId> = fwd_body
            .nodes()
            .iter()
            .filter(|n| matches!(n.op, Op::Input { .. }))
            .map(|n| n.id)
            .collect();
        body_input_ids.sort();
        assert_eq!(
            body_input_ids.len(),
            *num_inputs as usize,
            "Op::CustomFn fwd_body has {} Op::Input(s); declared num_inputs={}",
            body_input_ids.len(),
            *num_inputs,
        );

        let mut body_arena = crate::arena::Arena::from_plan(body_plan);
        for n in fwd_body.nodes() {
            if let Op::Constant { data } = &n.op
                && body_arena.has_buffer(n.id)
                && !data.is_empty()
            {
                match n.shape.dtype() {
                    rlx_ir::DType::F64 => {
                        let off = body_arena.byte_offset(n.id);
                        let buf = body_arena.raw_buf_mut();
                        let nb = (buf.len() - off).min(data.len());
                        buf[off..off + nb].copy_from_slice(&data[..nb]);
                    }
                    _ => {
                        let buf = body_arena.slice_mut(n.id);
                        let nf = data.len() / 4;
                        let nl = buf.len().min(nf);
                        for i in 0..nl {
                            let bytes = [
                                data[i * 4],
                                data[i * 4 + 1],
                                data[i * 4 + 2],
                                data[i * 4 + 3],
                            ];
                            buf[i] = f32::from_le_bytes(bytes);
                        }
                    }
                }
            }
        }
        let body_init = body_arena.raw_buf().to_vec();
        let body_schedule = compile_thunks_with_rng(fwd_body, &body_arena, rng);

        // Per primal input: (body_input_off, outer_input_off, bytes).
        let inputs_v: Vec<(usize, usize, u32)> = (0..*num_inputs as usize)
            .map(|i| {
                let body_in = body_input_ids[i];
                let body_off = body_offsets[&body_in];
                let outer_in = node.inputs[i];
                let outer_off = node_offset(arena, outer_in);
                let bytes = graph
                    .node(outer_in)
                    .shape
                    .size_bytes()
                    .expect("Op::CustomFn primal input must have static shape");
                (body_off, outer_off, bytes as u32)
            })
            .collect();

        let body_output_id = fwd_body
            .outputs
            .first()
            .copied()
            .expect("Op::CustomFn fwd_body must declare exactly one output");
        let body_output_off = body_offsets[&body_output_id];
        let out_bytes = node
            .shape
            .size_bytes()
            .expect("Op::CustomFn output must have static shape");

        Thunk::CustomFn {
            body: Arc::new(body_schedule),
            body_init: Arc::new(body_init),
            inputs: Arc::new(inputs_v),
            body_output_off,
            outer_output_off: node_offset(arena, node.id),
            out_bytes: out_bytes as u32,
        }
    }
}

// CustomFn dispatch (interpreted path). Mirrors the
// pre-compiled-closure variant elsewhere in this file.
// Patched by rlx-eda.
#[inline(always)]
pub(crate) fn exec_custom_fn(t: &Thunk, base: *mut u8) {
    let Thunk::CustomFn {
        body,
        body_init,
        inputs,
        body_output_off,
        outer_output_off,
        out_bytes,
    } = t
    else {
        unreachable!()
    };
    {
        let mut body_buf: Vec<u8> = (**body_init).clone();
        unsafe {
            for (body_in_off, outer_in_off, n_bytes) in inputs.iter() {
                let src = (base as *const u8).add(*outer_in_off);
                let dst = body_buf.as_mut_ptr().add(*body_in_off);
                std::ptr::copy_nonoverlapping(src, dst, *n_bytes as usize);
            }
        }
        execute_thunks(body, &mut body_buf);
        unsafe {
            let src = body_buf.as_ptr().add(*body_output_off);
            let dst = base.add(*outer_output_off);
            std::ptr::copy_nonoverlapping(src, dst, *out_bytes as usize);
        }
    }
}

#[inline(always)]
pub(crate) fn exec_sgd_momentum(t: &Thunk, base: *mut u8) {
    let Thunk::SgdMomentum {
        param,
        vel,
        grad,
        p_out,
        v_out,
        lr,
        mom,
        len,
    } = t
    else {
        unreachable!()
    };
    {
        // v' = mom·v + g ;  p' = p − lr·v'  — one fused pass per param,
        // replacing the 4 BinaryFull ops the in-graph SGD chain lowers
        // to. Element-wise, so in-place aliasing (p_out==param etc.) is
        // safe: read index i, write index i.
        let len = *len as usize;
        let (lr, mom) = (*lr, *mom);
        unsafe {
            let p = sl(*param, base, len);
            let v = sl(*vel, base, len);
            let g = sl(*grad, base, len);
            let po = sl_mut(*p_out, base, len);
            let vo = sl_mut(*v_out, base, len);
            if fast_conv_enabled() && crate::pool::should_parallelize(len) {
                let poa = po.as_mut_ptr() as usize;
                let voa = vo.as_mut_ptr() as usize;
                crate::pool::par_for(len, crate::pool::chunk_floor(len), &|off, cnt| {
                    for i in off..off + cnt {
                        let vn = mom * v[i] + g[i];
                        *((voa as *mut f32).add(i)) = vn;
                        *((poa as *mut f32).add(i)) = p[i] - lr * vn;
                    }
                });
            } else {
                for i in 0..len {
                    let vn = mom * v[i] + g[i];
                    vo[i] = vn;
                    po[i] = p[i] - lr * vn;
                }
            }
        }
    }
}

// (Thunk::DenseSolveF64 / Thunk::ScanBackward had panic
// stubs here as placeholders during the wire-up; both
// are now reached by the real implementations earlier in
// this same match — the stubs were dead code shadowed by
// the specific-pattern arms above. Removed.)
#[inline(always)]
pub(crate) fn exec_custom_op(t: &Thunk, base: *mut u8, arena_len: usize) {
    let Thunk::CustomOp {
        kernel,
        inputs,
        output,
        attrs,
    } = t
    else {
        unreachable!()
    };
    {
        let (out_off, out_len, out_shape) = output;
        unsafe {
            dispatch_custom_op(
                &**kernel, inputs, *out_off, *out_len, out_shape, attrs, base, arena_len,
            );
        }
    }
}

/// Shared dispatch path for `Thunk::CustomOp`. Builds a typed
/// [`CpuTensorRef`] for each input *at that input's declared dtype*
/// (so a sparse-LU op with mixed F64/I32 inputs gets the right
/// typed slices) and a [`CpuTensorMut`] for the output, then calls
/// the kernel's single `execute` method.
pub(crate) unsafe fn dispatch_custom_op(
    kernel: &dyn crate::op_registry::CpuKernel,
    inputs: &[(usize, u32, Shape)],
    out_off: usize,
    out_len: u32,
    out_shape: &Shape,
    attrs: &[u8],
    base: *mut u8,
    arena_len: usize,
) {
    use crate::op_registry::{CpuTensorMut, CpuTensorRef};
    use rlx_ir::DType;

    // Clamp a slice length so `[off, off + n*elem)` never runs past the arena.
    // Mirrors the defensive clamps in the Sgemm / Where thunks: a declared
    // length can exceed the buffer actually planned for a node (e.g. probe /
    // headroom-compile graphs pin an activation to a larger static shape than
    // its producer fills), and reading past the arena `Vec` segfaults. Real
    // inference has matching sizes, so this is a no-op there.
    let cap = |off: usize, elem: usize| -> usize { arena_len.saturating_sub(off) / elem.max(1) };

    // One arm per `DType` variant — single source of truth for
    // "which dtypes the CPU custom-op dispatcher wires." If a new
    // DType lands in `rlx-ir`, the compiler flags this match as
    // non-exhaustive and the gap gets named at the right place.
    macro_rules! build_in_view {
        ($shape:expr, $off:expr, $n:expr, $variant:ident, $rust_ty:ty) => {
            CpuTensorRef::$variant {
                data: unsafe {
                    sl_typed::<$rust_ty>(
                        $off,
                        base,
                        $n.min(cap($off, std::mem::size_of::<$rust_ty>())),
                    )
                },
                shape: $shape,
            }
        };
    }
    macro_rules! build_out_view {
        ($variant:ident, $rust_ty:ty) => {
            CpuTensorMut::$variant {
                data: unsafe {
                    sl_mut_typed::<$rust_ty>(
                        out_off,
                        base,
                        (out_len as usize).min(cap(out_off, std::mem::size_of::<$rust_ty>())),
                    )
                },
                shape: out_shape,
            }
        };
    }

    let in_views: Vec<CpuTensorRef<'_>> = inputs
        .iter()
        .map(|(off, len, shape)| {
            let n = *len as usize;
            let off = *off;
            match shape.dtype() {
                DType::F32 => build_in_view!(shape, off, n, F32, f32),
                DType::F64 => build_in_view!(shape, off, n, F64, f64),
                DType::F16 => build_in_view!(shape, off, n, F16, half::f16),
                DType::BF16 => build_in_view!(shape, off, n, BF16, half::bf16),
                DType::I8 => build_in_view!(shape, off, n, I8, i8),
                DType::I16 => build_in_view!(shape, off, n, I16, i16),
                DType::I32 => build_in_view!(shape, off, n, I32, i32),
                DType::I64 => build_in_view!(shape, off, n, I64, i64),
                DType::U8 => build_in_view!(shape, off, n, U8, u8),
                DType::U32 => build_in_view!(shape, off, n, U32, u32),
                DType::Bool => build_in_view!(shape, off, n, Bool, u8),
                // C64 isn't a CpuTensor variant today; the user-registered
                // op_registry path doesn't see complex inputs (those are
                // handled by built-in ops with dedicated kernels).
                DType::C64 => panic!(
                    "Op::Custom kernel input has DType::C64 — built-in \
                 complex ops handle their own kernels; user-registered \
                 ops don't yet see complex tensors"
                ),
                DType::C128 => panic!(
                    "Op::Custom kernel input has DType::C128 — built-in \
                 complex ops handle their own kernels; user-registered \
                 ops don't yet see complex tensors"
                ),
            }
        })
        .collect();

    let result = match out_shape.dtype() {
        DType::F32 => kernel.execute(&in_views, build_out_view!(F32, f32), attrs),
        DType::F64 => kernel.execute(&in_views, build_out_view!(F64, f64), attrs),
        DType::F16 => kernel.execute(&in_views, build_out_view!(F16, half::f16), attrs),
        DType::BF16 => kernel.execute(&in_views, build_out_view!(BF16, half::bf16), attrs),
        DType::I8 => kernel.execute(&in_views, build_out_view!(I8, i8), attrs),
        DType::I16 => kernel.execute(&in_views, build_out_view!(I16, i16), attrs),
        DType::I32 => kernel.execute(&in_views, build_out_view!(I32, i32), attrs),
        DType::I64 => kernel.execute(&in_views, build_out_view!(I64, i64), attrs),
        DType::U8 => kernel.execute(&in_views, build_out_view!(U8, u8), attrs),
        DType::U32 => kernel.execute(&in_views, build_out_view!(U32, u32), attrs),
        DType::Bool => kernel.execute(&in_views, build_out_view!(Bool, u8), attrs),
        DType::C64 => panic!("Op::Custom output DType::C64 not supported"),
        DType::C128 => panic!("Op::Custom output DType::C128 not supported"),
    };
    if let Err(e) = result {
        panic!("Op::Custom('{}') CPU kernel failed: {e}", kernel.name());
    }
}