rlx-cuda 0.2.5

NVIDIA CUDA backend — cuBLAS for matmul + NVRTC-compiled kernels for everything else, via the pure-Rust `cudarc` crate.
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// RLX — versatile ML compiler + runtime.
// Copyright (C) 2026 Eugene Hauptmann, Nataliya Kosmyna.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Host-side training backward ops for CUDA device arenas (D2H → CPU → H2D).

use cudarc::driver::{CudaSlice, CudaStream};
use std::sync::Arc;

fn run_on_arena(
    stream: &Arc<CudaStream>,
    buffer: &mut CudaSlice<f32>,
    arena_size_bytes: usize,
    f: impl FnOnce(*mut u8),
) {
    let n_f32 = arena_size_bytes / 4;
    stream
        .synchronize()
        .expect("rlx-cuda: training_bwd pre-sync failed");
    let mut host = vec![0f32; n_f32];
    stream
        .memcpy_dtoh(&buffer.slice(..), &mut host)
        .expect("rlx-cuda: training_bwd arena dtoh failed");
    f(host.as_mut_ptr() as *mut u8);
    stream
        .memcpy_htod(&host, &mut buffer.slice_mut(..))
        .expect("rlx-cuda: training_bwd arena htod failed");
}

pub fn run_rms_norm_backward_input(
    stream: &Arc<CudaStream>,
    buffer: &mut CudaSlice<f32>,
    arena_size_bytes: usize,
    x: usize,
    gamma: usize,
    beta: usize,
    dy: usize,
    dx: usize,
    rows: u32,
    h: u32,
    eps: f32,
) {
    run_on_arena(stream, buffer, arena_size_bytes, |base| unsafe {
        rlx_cpu::thunk::execute_rms_norm_backward_input_f32(
            x, gamma, beta, dy, dx, rows, h, eps, base,
        );
    });
}

pub fn run_rms_norm_backward_gamma(
    stream: &Arc<CudaStream>,
    buffer: &mut CudaSlice<f32>,
    arena_size_bytes: usize,
    x: usize,
    gamma: usize,
    beta: usize,
    dy: usize,
    dgamma: usize,
    rows: u32,
    h: u32,
    eps: f32,
) {
    run_on_arena(stream, buffer, arena_size_bytes, |base| unsafe {
        rlx_cpu::thunk::execute_rms_norm_backward_gamma_f32(
            x, gamma, beta, dy, dgamma, rows, h, eps, base,
        );
    });
}

pub fn run_rms_norm_backward_beta(
    stream: &Arc<CudaStream>,
    buffer: &mut CudaSlice<f32>,
    arena_size_bytes: usize,
    x: usize,
    gamma: usize,
    beta: usize,
    dy: usize,
    dbeta: usize,
    rows: u32,
    h: u32,
    eps: f32,
) {
    run_on_arena(stream, buffer, arena_size_bytes, |base| unsafe {
        rlx_cpu::thunk::execute_rms_norm_backward_beta_f32(
            x, gamma, beta, dy, dbeta, rows, h, eps, base,
        );
    });
}

pub fn run_rope_backward(
    stream: &Arc<CudaStream>,
    buffer: &mut CudaSlice<f32>,
    arena_size_bytes: usize,
    dy: usize,
    cos: usize,
    sin: usize,
    dx: usize,
    batch: u32,
    seq: u32,
    hidden: u32,
    head_dim: u32,
    n_rot: u32,
    cos_len: u32,
) {
    run_on_arena(stream, buffer, arena_size_bytes, |base| unsafe {
        rlx_cpu::thunk::execute_rope_backward_f32(
            dy, cos, sin, dx, batch, seq, hidden, head_dim, n_rot, cos_len, base,
        );
    });
}

pub fn run_cumsum_backward(
    stream: &Arc<CudaStream>,
    buffer: &mut CudaSlice<f32>,
    arena_size_bytes: usize,
    dy: usize,
    dx: usize,
    rows: u32,
    cols: u32,
    exclusive: bool,
) {
    run_on_arena(stream, buffer, arena_size_bytes, |base| unsafe {
        rlx_cpu::thunk::execute_cumsum_backward_f32(dy, dx, rows, cols, exclusive, base);
    });
}

pub fn run_gather_backward(
    stream: &Arc<CudaStream>,
    buffer: &mut CudaSlice<f32>,
    arena_size_bytes: usize,
    dy: usize,
    indices: usize,
    dst: usize,
    outer: u32,
    axis_dim: u32,
    num_idx: u32,
    trailing: u32,
) {
    run_on_arena(stream, buffer, arena_size_bytes, |base| unsafe {
        rlx_cpu::thunk::execute_gather_backward_f32(
            dy, indices, dst, outer, axis_dim, num_idx, trailing, base,
        );
    });
}

pub fn run_maxpool2d_backward(
    stream: &Arc<CudaStream>,
    buffer: &mut CudaSlice<f32>,
    x_off: usize,
    dy_off: usize,
    dx_off: usize,
    n: u32,
    c: u32,
    h: u32,
    w: u32,
    h_out: u32,
    w_out: u32,
    kh: u32,
    kw: u32,
    sh: u32,
    sw: u32,
    ph: u32,
    pw: u32,
) {
    let x_len = (n as usize) * (c as usize) * (h as usize) * (w as usize);
    let dy_len = (n as usize) * (c as usize) * (h_out as usize) * (w_out as usize);
    stream
        .synchronize()
        .expect("rlx-cuda: maxpool2d_bwd pre-sync failed");
    let mut x_host = vec![0f32; x_len];
    let mut dy_host = vec![0f32; dy_len];
    let mut dx_host = vec![0f32; x_len];
    stream
        .memcpy_dtoh(&buffer.slice(x_off..x_off + x_len), &mut x_host)
        .expect("rlx-cuda: maxpool2d_bwd x dtoh failed");
    stream
        .memcpy_dtoh(&buffer.slice(dy_off..dy_off + dy_len), &mut dy_host)
        .expect("rlx-cuda: maxpool2d_bwd dy dtoh failed");
    rlx_cpu::training_bwd::maxpool2d_backward_nchw(
        &x_host,
        &dy_host,
        &mut dx_host,
        n as usize,
        c as usize,
        h as usize,
        w as usize,
        h_out as usize,
        w_out as usize,
        kh as usize,
        kw as usize,
        sh as usize,
        sw as usize,
        ph as usize,
        pw as usize,
    );
    stream
        .memcpy_htod(&dx_host, &mut buffer.slice_mut(dx_off..dx_off + x_len))
        .expect("rlx-cuda: maxpool2d_bwd dx htod failed");
}

#[allow(clippy::too_many_arguments)]
pub fn run_conv2d_forward(
    stream: &Arc<CudaStream>,
    buffer: &mut CudaSlice<f32>,
    arena_size_bytes: usize,
    in_off: u32,
    w_off: u32,
    out_off: u32,
    n: u32,
    c_in: u32,
    c_out: u32,
    h: u32,
    w: u32,
    h_out: u32,
    w_out: u32,
    kh: u32,
    kw: u32,
    sh: u32,
    sw: u32,
    ph: u32,
    pw: u32,
    dh: u32,
    dw: u32,
    groups: u32,
) {
    run_on_arena(stream, buffer, arena_size_bytes, |base| unsafe {
        rlx_cpu::thunk::execute_conv2d_forward_f32(
            (in_off as usize) * 4,
            (w_off as usize) * 4,
            (out_off as usize) * 4,
            n,
            c_in,
            h,
            w,
            c_out,
            h_out,
            w_out,
            kh,
            kw,
            sh,
            sw,
            ph,
            pw,
            dh,
            dw,
            groups,
            base,
        );
    });
}

#[allow(clippy::too_many_arguments)]
pub fn run_conv2d_backward_input(
    stream: &Arc<CudaStream>,
    buffer: &mut CudaSlice<f32>,
    dy_off: usize,
    w_off: usize,
    dx_off: usize,
    n: u32,
    c_in: u32,
    h: u32,
    w_in: u32,
    c_out: u32,
    h_out: u32,
    w_out: u32,
    kh: u32,
    kw: u32,
    sh: u32,
    sw: u32,
    ph: u32,
    pw: u32,
    dh: u32,
    dw: u32,
    groups: u32,
) {
    let n = n as usize;
    let c_in = c_in as usize;
    let h = h as usize;
    let w_in = w_in as usize;
    let c_out = c_out as usize;
    let h_out = h_out as usize;
    let w_out = w_out as usize;
    let groups = groups.max(1) as usize;
    let c_in_per_g = c_in / groups;
    let kh = kh as usize;
    let kw = kw as usize;
    let dy_len = n * c_out * h_out * w_out;
    let w_len = c_out * c_in_per_g * kh * kw;
    let dx_len = n * c_in * h * w_in;
    let scratch_len = dy_len + w_len + dx_len;
    stream
        .synchronize()
        .expect("rlx-cuda: conv2d_bwd_input pre-sync failed");
    let mut scratch = vec![0f32; scratch_len];
    stream
        .memcpy_dtoh(
            &buffer.slice(dy_off..dy_off + dy_len),
            &mut scratch[..dy_len],
        )
        .expect("rlx-cuda: conv2d_bwd_input dy dtoh failed");
    stream
        .memcpy_dtoh(
            &buffer.slice(w_off..w_off + w_len),
            &mut scratch[dy_len..dy_len + w_len],
        )
        .expect("rlx-cuda: conv2d_bwd_input w dtoh failed");
    let dx_base = (dy_len + w_len) * 4;
    unsafe {
        rlx_cpu::conv_bwd::execute_conv2d_backward_input_f32(
            scratch.as_mut_ptr() as *mut u8,
            0,
            dy_len * 4,
            dx_base,
            n as u32,
            c_in as u32,
            h as u32,
            w_in as u32,
            c_out as u32,
            h_out as u32,
            w_out as u32,
            kh as u32,
            kw as u32,
            sh,
            sw,
            ph,
            pw,
            dh,
            dw,
            groups as u32,
        );
    }
    stream
        .memcpy_htod(
            &scratch[dy_len + w_len..],
            &mut buffer.slice_mut(dx_off..dx_off + dx_len),
        )
        .expect("rlx-cuda: conv2d_bwd_input dx htod failed");
}

#[allow(clippy::too_many_arguments)]
pub fn run_conv2d_backward_weight(
    stream: &Arc<CudaStream>,
    buffer: &mut CudaSlice<f32>,
    x_off: usize,
    dy_off: usize,
    dw_off: usize,
    n: u32,
    c_in: u32,
    h: u32,
    w: u32,
    c_out: u32,
    h_out: u32,
    w_out: u32,
    kh: u32,
    kw: u32,
    sh: u32,
    sw: u32,
    ph: u32,
    pw: u32,
    dh: u32,
    dw_dil: u32,
    groups: u32,
) {
    let n = n as usize;
    let c_in = c_in as usize;
    let h = h as usize;
    let w = w as usize;
    let c_out = c_out as usize;
    let h_out = h_out as usize;
    let w_out = w_out as usize;
    let groups = groups.max(1) as usize;
    let c_in_per_g = c_in / groups;
    let kh = kh as usize;
    let kw = kw as usize;
    let x_len = n * c_in * h * w;
    let dy_len = n * c_out * h_out * w_out;
    let dw_len = c_out * c_in_per_g * kh * kw;
    let scratch_len = x_len + dy_len + dw_len;
    stream
        .synchronize()
        .expect("rlx-cuda: conv2d_bwd_weight pre-sync failed");
    let mut scratch = vec![0f32; scratch_len];
    stream
        .memcpy_dtoh(&buffer.slice(x_off..x_off + x_len), &mut scratch[..x_len])
        .expect("rlx-cuda: conv2d_bwd_weight x dtoh failed");
    stream
        .memcpy_dtoh(
            &buffer.slice(dy_off..dy_off + dy_len),
            &mut scratch[x_len..x_len + dy_len],
        )
        .expect("rlx-cuda: conv2d_bwd_weight dy dtoh failed");
    let dw_base = (x_len + dy_len) * 4;
    unsafe {
        rlx_cpu::conv_bwd::execute_conv2d_backward_weight_f32(
            scratch.as_mut_ptr() as *mut u8,
            0,
            x_len * 4,
            dw_base,
            n as u32,
            c_in as u32,
            h as u32,
            w as u32,
            c_out as u32,
            h_out as u32,
            w_out as u32,
            kh as u32,
            kw as u32,
            sh,
            sw,
            ph,
            pw,
            dh,
            dw_dil,
            groups as u32,
        );
    }
    stream
        .memcpy_htod(
            &scratch[x_len + dy_len..],
            &mut buffer.slice_mut(dw_off..dw_off + dw_len),
        )
        .expect("rlx-cuda: conv2d_bwd_weight dw htod failed");
}