rlx-runtime 0.2.14

RLX runtime — feature-gated backends, session API, compile+execute pipeline
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
// RLX — versatile ML compiler + runtime.
// Copyright (C) 2026 Eugene Hauptmann, Nataliya Kosmyna.
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Data-driven cross-backend parity for the standalone elementwise kernels —
//! the **runtime counterpart** to the canonical opcode tables in
//! `rlx_ir::opcodes`.
//!
//! Instead of one hand-written parity test per op with a `macro_rules!` arm per
//! backend (the old pattern, where "supported but never actually parity-tested
//! on backend X" was structurally easy — that's how the silent NeoX-rotate /
//! attention-mask / Compare-broadcast bugs survived), this fans a single table
//! out automatically:
//!
//! * **Forward parity** — every [`Activation`], [`BinaryOp`] and [`CmpOp`]
//!   variant (`*::ALL`) is run on the CPU oracle and on *every backend that is
//!   both compiled in and reports it can run the graph* (`supports_graph`), and
//!   the outputs are compared. Adding a variant to any of those enums extends
//!   the sweep with zero new code — and if a backend's kernel switch ever
//!   disagrees with the canonical opcode id (an off-by-one, a Round/Recip swap,
//!   a copy-paste from the wrong scheme), the wrong *function* is computed and
//!   the parity error jumps to O(1), failing here.
//! * **Gradient parity (CPU, finite differences)** — for the smooth
//!   activations, the analytic VJP from `grad_with_loss` is checked against
//!   central differences. Backward activations decompose to primitives on the
//!   GPU backends, so the CPU is the meaningful autodiff oracle; this catches
//!   sign flips / wrong derivative rules independent of backend.
//!
//! Run the fan-out against real backends with e.g.
//! `cargo test -p rlx-runtime --features metal,mlx,gpu elementwise`. With only
//! the default `cpu` feature, the CPU-oracle self-checks and the FD sweep still
//! run; the cross-backend loops simply find no extra backends and no-op.

#![cfg(feature = "cpu")]
#![allow(clippy::needless_range_loop)]

use rlx_ir::op::{Activation, BinaryOp, CmpOp, ReduceOp};
use rlx_ir::{DType, Graph, NodeId, Op, Shape};
use rlx_opt::autodiff::grad_with_loss;
use rlx_runtime::{Device, Session, is_available, supports_graph};

const N: usize = 64;

/// Backends compiled into this build that are actually present on the box.
/// Each push is `#[cfg]`-gated on its backend feature so we never name a
/// `Device` variant that wasn't built, then filtered by live `is_available`.
#[allow(clippy::vec_init_then_push)]
fn available_backends() -> Vec<Device> {
    let mut v: Vec<Device> = Vec::new();
    #[cfg(all(feature = "metal", target_os = "macos"))]
    v.push(Device::Metal);
    #[cfg(all(feature = "mlx", target_os = "macos"))]
    v.push(Device::Mlx);
    #[cfg(feature = "gpu")]
    v.push(Device::Gpu);
    #[cfg(feature = "cuda")]
    v.push(Device::Cuda);
    #[cfg(feature = "rocm")]
    v.push(Device::Rocm);
    #[cfg(feature = "vulkan")]
    v.push(Device::Vulkan);
    v.retain(|&d| is_available(d));
    v
}

fn run(device: Device, g: &Graph, inputs: &[(&str, &[f32])]) -> Vec<f32> {
    Session::new(device)
        .compile(g.clone())
        .run(inputs)
        .pop()
        .expect("graph produced no output")
}

/// Worst per-element error, absolute below unit magnitude and relative above —
/// tolerant enough to survive f32 transcendental-intrinsic differences across
/// vendors, tight enough that a wrong opcode (a different function entirely)
/// blows straight past it.
fn worst_rel(cpu: &[f32], got: &[f32]) -> f32 {
    assert_eq!(cpu.len(), got.len(), "length mismatch");
    cpu.iter()
        .zip(got)
        .map(|(c, g)| (c - g).abs() / c.abs().max(1.0))
        .fold(0.0, f32::max)
}

const PARITY_TOL: f32 = 5e-3;

// ── Forward parity ──────────────────────────────────────────────────────────

fn activation_graph(act: Activation) -> Graph {
    let mut g = Graph::new("act");
    let x = g.input("x", Shape::new(&[N], DType::F32));
    let y = g.activation(act, x, Shape::new(&[N], DType::F32));
    g.set_outputs(vec![y]);
    g
}

/// Domain-safe input for a forward activation (positive for log/sqrt/rsqrt,
/// away from 0 for recip, gentle range for tan).
fn activation_input(act: Activation) -> Vec<f32> {
    let base: Vec<f32> = (0..N)
        .map(|i| -2.0 + 4.0 * (i as f32) / (N as f32 - 1.0))
        .collect();
    match act {
        Activation::Log | Activation::Sqrt | Activation::Rsqrt => {
            base.iter().map(|v| v.abs() + 0.5).collect()
        }
        Activation::Recip => base
            .iter()
            .map(|v| if v.abs() < 0.3 { 0.6 } else { *v })
            .collect(),
        Activation::Tan => base.iter().map(|v| v * 0.5).collect(),
        _ => base,
    }
}

#[test]
fn activation_forward_parity() {
    let backends = available_backends();
    eprintln!("activation parity over backends: {backends:?}");
    for &act in &Activation::ALL {
        let g = activation_graph(act);
        let x = activation_input(act);
        let cpu = run(Device::Cpu, &g, &[("x", &x)]);
        assert!(
            cpu.iter().all(|v| v.is_finite()),
            "CPU oracle for {act:?} produced non-finite output"
        );
        for &dev in &backends {
            if !supports_graph(dev, &g) {
                eprintln!("skip {act:?} on {dev:?} (unsupported)");
                continue;
            }
            let got = run(dev, &g, &[("x", &x)]);
            let err = worst_rel(&cpu, &got);
            assert!(
                err < PARITY_TOL,
                "activation {act:?} on {dev:?}: worst_rel={err:.3e} \
                 (opcode/kernel mismatch?)"
            );
        }
    }
}

fn binary_graph(op: BinaryOp) -> Graph {
    let mut g = Graph::new("bin");
    let a = g.input("a", Shape::new(&[N], DType::F32));
    let b = g.input("b", Shape::new(&[N], DType::F32));
    let y = g.binary(op, a, b, Shape::new(&[N], DType::F32));
    g.set_outputs(vec![y]);
    g
}

/// Domain-safe operands for a binary op. Bitwise/shift ops act on
/// integer-valued operands; pow needs a positive base; div/mod need a nonzero
/// divisor.
fn binary_inputs(op: BinaryOp) -> (Vec<f32>, Vec<f32>) {
    let a: Vec<f32> = (0..N)
        .map(|i| (i as f32 * 0.19).sin() * 2.0 + 0.05)
        .collect();
    let b: Vec<f32> = (0..N)
        .map(|i| (i as f32 * 0.11).cos() * 2.0 + 0.05)
        .collect();
    match op {
        BinaryOp::Pow => (
            a.iter().map(|v| v.abs() + 0.2).collect(),
            b.iter().map(|v| v * 0.5).collect(),
        ),
        BinaryOp::Div | BinaryOp::Mod => (
            a,
            b.iter()
                .map(|v| if v.abs() < 0.5 { 0.9 } else { *v })
                .collect(),
        ),
        BinaryOp::BitAnd | BinaryOp::BitOr | BinaryOp::BitXor => (
            (0..N).map(|i| (i % 13) as f32).collect(),
            (0..N).map(|i| (i % 7) as f32).collect(),
        ),
        BinaryOp::Shl | BinaryOp::Shr => (
            (0..N).map(|i| (i % 17) as f32).collect(),
            (0..N).map(|i| (i % 4) as f32).collect(),
        ),
        _ => (a, b),
    }
}

/// Regression for the negative-base `pow` drift the rlxsl single source fixed:
/// `(-x)^k` for integer `k` must be signed/real on every backend (WGSL/GLSL/MSL
/// `pow()` returns NaN for a negative base, so the emitter now sign-corrects it).
/// Integer exponents keep the result finite, matching Rust `powf` on CPU.
#[test]
fn binary_pow_negative_base_matches_cpu() {
    let backends = available_backends();
    let g = binary_graph(BinaryOp::Pow);
    let a: Vec<f32> = (0..N).map(|i| -1.0 - (i % 5) as f32 * 0.5).collect(); // all negative
    let b: Vec<f32> = (0..N).map(|i| (i % 4) as f32).collect(); // exponents 0,1,2,3
    let cpu = run(Device::Cpu, &g, &[("a", &a), ("b", &b)]);
    assert!(
        cpu.iter().all(|v| v.is_finite()),
        "CPU pow neg-base oracle not finite"
    );
    for &dev in &backends {
        if !supports_graph(dev, &g) {
            continue;
        }
        let got = run(dev, &g, &[("a", &a), ("b", &b)]);
        let err = worst_rel(&cpu, &got);
        assert!(
            err < PARITY_TOL,
            "pow neg-base on {dev:?}: worst_rel={err:.3e} (bare GPU pow NaNs on negative base?)"
        );
    }
}

#[test]
fn binary_forward_parity() {
    let backends = available_backends();
    for &op in &BinaryOp::ALL {
        let g = binary_graph(op);
        let (a, b) = binary_inputs(op);
        let cpu = run(Device::Cpu, &g, &[("a", &a), ("b", &b)]);
        assert!(
            cpu.iter().all(|v| v.is_finite()),
            "CPU oracle for {op:?} produced non-finite output"
        );
        for &dev in &backends {
            if !supports_graph(dev, &g) {
                eprintln!("skip {op:?} on {dev:?} (unsupported)");
                continue;
            }
            let got = run(dev, &g, &[("a", &a), ("b", &b)]);
            let err = worst_rel(&cpu, &got);
            assert!(
                err < PARITY_TOL,
                "binary {op:?} on {dev:?}: worst_rel={err:.3e} (opcode/kernel mismatch?)"
            );
        }
    }
}

fn compare_graph(op: CmpOp) -> Graph {
    let mut g = Graph::new("cmp");
    let a = g.input("a", Shape::new(&[N], DType::F32));
    let b = g.input("b", Shape::new(&[N], DType::F32));
    let mask = g.add_node(Op::Compare(op), vec![a, b], Shape::new(&[N], DType::Bool));
    // Cast bool→f32 so the result reads back as a clean 0/1 vector (matches the
    // `Compare → Cast(F32)` pattern the backends already rely on).
    let y = g.add_node(
        Op::Cast { to: DType::F32 },
        vec![mask],
        Shape::new(&[N], DType::F32),
    );
    g.set_outputs(vec![y]);
    g
}

#[test]
fn compare_forward_parity() {
    let backends = available_backends();
    // Overlapping ranges so every predicate yields a mix of 0/1 (and some exact
    // equalities for Eq/Ne).
    let a: Vec<f32> = (0..N).map(|i| (i % 5) as f32 - 2.0).collect();
    let b: Vec<f32> = (0..N).map(|i| (i % 4) as f32 - 2.0).collect();
    for &op in &CmpOp::ALL {
        let g = compare_graph(op);
        let cpu = run(Device::Cpu, &g, &[("a", &a), ("b", &b)]);
        for &dev in &backends {
            if !supports_graph(dev, &g) {
                eprintln!("skip {op:?} on {dev:?} (unsupported)");
                continue;
            }
            let got = run(dev, &g, &[("a", &a), ("b", &b)]);
            let err = worst_rel(&cpu, &got);
            assert!(
                err < PARITY_TOL,
                "compare {op:?} on {dev:?}: worst_rel={err:.3e} (wrong predicate?)"
            );
        }
    }
}

/// The warm-tier codegen manifest (`rlxsl`) defines each activation's
/// scalar math once. This checks that its interpreter matches the trusted CPU
/// activation kernel for every variant — closing the loop with the GPU parity
/// above: manifest == CPU (here) and generated-GPU == CPU (parity), so the
/// generated GPU kernels are validated transitively. Runs GPU-free.
#[test]
fn kernel_dsl_matches_cpu_activation_oracle() {
    for &act in &Activation::ALL {
        let g = activation_graph(act);
        let x = activation_input(act);
        let cpu = run(Device::Cpu, &g, &[("x", &x)]);
        for (i, &xi) in x.iter().enumerate() {
            let dsl = rlxsl::eval_activation(act, xi);
            let c = cpu[i];
            let err = (dsl - c).abs() / c.abs().max(1.0);
            assert!(
                err < 5e-3,
                "{act:?} at x={xi}: manifest={dsl} cpu_kernel={c} err={err:e}"
            );
        }
    }
}

// ── Gradient parity (CPU finite differences) ─────────────────────────────────

const FD_M: usize = 8;

/// `loss = sum(act(x))` with `x` a param, so `grad_with_loss(&g, &[x])` yields
/// `[loss, d_loss/d_x]`. Returns the graph and the `x` node id.
fn activation_loss_graph(act: Activation) -> (Graph, NodeId) {
    let mut g = Graph::new("actloss");
    let x = g.param("x", Shape::new(&[FD_M], DType::F32));
    let y = g.activation(act, x, Shape::new(&[FD_M], DType::F32));
    let loss = g.add_node(
        Op::Reduce {
            op: ReduceOp::Sum,
            axes: vec![0],
            keep_dim: false,
        },
        vec![y],
        Shape::new(&[1], DType::F32),
    );
    g.set_outputs(vec![loss]);
    (g, x)
}

fn activation_forward_loss(act: Activation, x: &[f32]) -> f32 {
    let (g, _) = activation_loss_graph(act);
    let mut c = Session::new(Device::Cpu).compile(g);
    c.set_param("x", x);
    c.run(&[])[0][0]
}

fn fd_input(act: Activation) -> Vec<f32> {
    let base: Vec<f32> = (0..FD_M)
        .map(|i| -1.2 + 2.4 * (i as f32) / (FD_M as f32 - 1.0))
        .collect();
    match act {
        Activation::Log | Activation::Sqrt | Activation::Rsqrt => {
            base.iter().map(|v| v.abs() + 0.4).collect()
        }
        Activation::Recip => base
            .iter()
            .map(|v| if v.abs() < 0.3 { 0.6 } else { *v })
            .collect(),
        _ => base,
    }
}

/// Smooth activations only — the piecewise/STE ones (Relu at 0, Round, Floor,
/// Ceil, Sign) have derivatives that intentionally disagree with a finite
/// difference, so they are validated by forward parity, not FD.
const SMOOTH: &[Activation] = &[
    Activation::Gelu,
    Activation::GeluApprox,
    Activation::Silu,
    Activation::Sigmoid,
    Activation::Tanh,
    Activation::Exp,
    Activation::Log,
    Activation::Sqrt,
    Activation::Rsqrt,
    Activation::Softplus,
    Activation::Erf,
    Activation::Mish,
    Activation::Softsign,
    Activation::Atan,
    Activation::Recip,
];

#[test]
fn activation_vjp_finite_difference_cpu() {
    let eps = 1e-3f32;
    let abs_tol = 5e-3f32;
    let rel_tol = 5e-3f32;
    for &act in SMOOTH {
        let x = fd_input(act);
        let (g, x_id) = activation_loss_graph(act);
        let bwd = grad_with_loss(&g, &[x_id]);
        let mut c = Session::new(Device::Cpu).compile(bwd);
        c.set_param("x", &x);
        let outs = c.run(&[("d_output", &[1.0f32])]);
        assert_eq!(outs.len(), 2, "{act:?}: expected [loss, grad_x]");
        let grad = &outs[1];
        assert_eq!(grad.len(), FD_M, "{act:?}: grad length");
        for i in 0..FD_M {
            let mut xp = x.clone();
            let mut xm = x.clone();
            xp[i] += eps;
            xm[i] -= eps;
            let fd = (activation_forward_loss(act, &xp) - activation_forward_loss(act, &xm))
                / (2.0 * eps);
            let ad = grad[i];
            let abs_err = (fd - ad).abs();
            let rel_err = abs_err / fd.abs().max(1e-6);
            assert!(
                abs_err < abs_tol || rel_err < rel_tol,
                "{act:?} grad[{i}]: autodiff {ad:e} vs FD {fd:e} (abs {abs_err:e}, rel {rel_err:e})"
            );
        }
    }
}