ferrotensor 0.2.1

GPU-accelerated machine learning library for training and inference in Rust
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
//! GPU Compute Kernels (WGSL)
//!
//! The `ferrotensor` tensor API dispatches a small set of generic kernels. Each one
//! selects its operation through an op code in a uniform, so adding an
//! element-wise op means adding one `case` here and one entry in
//! `tensor::cpu`, which holds the reference implementation the GPU result is
//! tested against.
//!
//! 1-D kernels are dispatched on a 2-D grid (`x + y * nx * 256`) so tensors
//! larger than 65535 workgroups still fit in one dispatch.

/// Op codes shared by the `unary` kernel and the CPU reference implementation.
pub(crate) mod unary_op {
    pub const RELU: u32 = 0;
    pub const SILU: u32 = 1;
    pub const GELU: u32 = 2;
    pub const SIGMOID: u32 = 3;
    pub const TANH: u32 = 4;
    pub const EXP: u32 = 5;
    pub const LOG: u32 = 6;
    pub const ABS: u32 = 7;
    pub const SQRT: u32 = 8;
    pub const NEG: u32 = 9;
    pub const ADD_SCALAR: u32 = 10;
    pub const MUL_SCALAR: u32 = 11;
    pub const POW_SCALAR: u32 = 12;
    pub const CLAMP: u32 = 13;
    pub const LEAKY_RELU: u32 = 14;
    pub const STEP: u32 = 15;
    pub const SIGN: u32 = 16;
    pub const RECIP: u32 = 17;
    pub const SILU_GRAD: u32 = 18;
    pub const GELU_GRAD: u32 = 19;
    pub const RSUB_SCALAR: u32 = 20;
    pub const SQUARE: u32 = 21;
    pub const IN_RANGE: u32 = 22;
    pub const SIN: u32 = 23;
    pub const COS: u32 = 24;
    pub const LEAKY_RELU_GRAD: u32 = 25;
}

/// Op codes for the broadcasting `binary` kernel.
pub(crate) mod binary_op {
    pub const ADD: u32 = 0;
    pub const SUB: u32 = 1;
    pub const MUL: u32 = 2;
    pub const DIV: u32 = 3;
    pub const MAX: u32 = 4;
    pub const MIN: u32 = 5;
    pub const GT: u32 = 6;
    pub const LT: u32 = 7;
    pub const EQ: u32 = 8;
    pub const GE: u32 = 9;
    pub const LE: u32 = 10;
}

/// Op codes for the axis `reduce` kernel.
pub(crate) mod reduce_op {
    pub const SUM: u32 = 0;
    pub const MAX: u32 = 1;
    pub const MIN: u32 = 2;
}

/// WGSL source for every kernel in [`ComputeEngine`](super::ComputeEngine).
pub(crate) const KERNELS_WGSL: &str = r#"
// ============================================================================
// FERROTENSOR TENSOR KERNELS
// ============================================================================

fn flat_index(gid: vec3<u32>, nwg: vec3<u32>) -> u32 {
    return gid.x + gid.y * nwg.x * 256u;
}

fn gelu_tanh(x: f32) -> f32 {
    let c = 0.7978845608;
    return 0.5 * x * (1.0 + tanh(c * (x + 0.044715 * x * x * x)));
}

fn gelu_tanh_grad(x: f32) -> f32 {
    let c = 0.7978845608;
    let t = tanh(c * (x + 0.044715 * x * x * x));
    let dt = (1.0 - t * t) * c * (1.0 + 3.0 * 0.044715 * x * x);
    return 0.5 * (1.0 + t) + 0.5 * x * dt;
}

fn sigmoid_f(x: f32) -> f32 {
    return 1.0 / (1.0 + exp(-x));
}

// --- Element-wise unary ops -------------------------------------------------

struct UnaryParams {
    op: u32,
    n: u32,
    a: f32,
    b: f32,
};

@group(0) @binding(0) var<storage, read> un_in: array<f32>;
@group(0) @binding(1) var<storage, read_write> un_out: array<f32>;
@group(0) @binding(2) var<uniform> un_p: UnaryParams;

@compute @workgroup_size(256)
fn unary(@builtin(global_invocation_id) gid: vec3<u32>,
         @builtin(num_workgroups) nwg: vec3<u32>) {
    let i = flat_index(gid, nwg);
    if (i >= un_p.n) { return; }
    let x = un_in[i];
    let a = un_p.a;
    let b = un_p.b;
    var y = x;
    switch un_p.op {
        case 0u: { y = max(x, 0.0); }
        case 1u: { y = x * sigmoid_f(x); }
        case 2u: { y = gelu_tanh(x); }
        case 3u: { y = sigmoid_f(x); }
        case 4u: { y = tanh(x); }
        case 5u: { y = exp(x); }
        case 6u: { y = log(x); }
        case 7u: { y = abs(x); }
        case 8u: { y = sqrt(x); }
        case 9u: { y = -x; }
        case 10u: { y = x + a; }
        case 11u: { y = x * a; }
        case 12u: {
            // pow is undefined for negative bases in WGSL; handle integer exponents.
            let ai = round(a);
            if (x < 0.0 && ai == a) {
                let r = pow(-x, a);
                y = select(r, -r, (i32(ai) & 1) == 1);
            } else if (x == 0.0 && a > 0.0) {
                y = 0.0;
            } else {
                y = pow(x, a);
            }
        }
        case 13u: { y = clamp(x, a, b); }
        case 14u: { y = select(a * x, x, x > 0.0); }
        case 15u: { y = select(0.0, 1.0, x > 0.0); }
        case 16u: { y = sign(x); }
        case 17u: { y = 1.0 / x; }
        case 18u: {
            let s = sigmoid_f(x);
            y = s * (1.0 + x * (1.0 - s));
        }
        case 19u: { y = gelu_tanh_grad(x); }
        case 20u: { y = a - x; }
        case 21u: { y = x * x; }
        case 22u: { y = select(0.0, 1.0, x >= a && x <= b); }
        case 23u: { y = sin(x); }
        case 24u: { y = cos(x); }
        case 25u: { y = select(a, 1.0, x > 0.0); }
        default: { y = x; }
    }
    un_out[i] = y;
}

// --- Broadcasting binary ops ------------------------------------------------
// meta = [out_shape(ndim), a_strides(ndim), b_strides(ndim)]; a broadcast
// dimension has stride 0.

struct BinaryParams {
    op: u32,
    n: u32,
    ndim: u32,
    _pad: u32,
};

@group(0) @binding(0) var<storage, read> bin_a: array<f32>;
@group(0) @binding(1) var<storage, read> bin_b: array<f32>;
@group(0) @binding(2) var<storage, read_write> bin_out: array<f32>;
@group(0) @binding(3) var<uniform> bin_p: BinaryParams;
@group(0) @binding(4) var<storage, read> bin_meta: array<u32>;

@compute @workgroup_size(256)
fn binary(@builtin(global_invocation_id) gid: vec3<u32>,
          @builtin(num_workgroups) nwg: vec3<u32>) {
    let i = flat_index(gid, nwg);
    if (i >= bin_p.n) { return; }
    let nd = bin_p.ndim;
    var rem = i;
    var ia = 0u;
    var ib = 0u;
    for (var d = 0u; d < nd; d = d + 1u) {
        let axis = nd - 1u - d;
        let size = bin_meta[axis];
        let idx = rem % size;
        rem = rem / size;
        ia = ia + idx * bin_meta[nd + axis];
        ib = ib + idx * bin_meta[2u * nd + axis];
    }
    let x = bin_a[ia];
    let y = bin_b[ib];
    var r = 0.0;
    switch bin_p.op {
        case 0u: { r = x + y; }
        case 1u: { r = x - y; }
        case 2u: { r = x * y; }
        case 3u: { r = x / y; }
        case 4u: { r = max(x, y); }
        case 5u: { r = min(x, y); }
        case 6u: { r = select(0.0, 1.0, x > y); }
        case 7u: { r = select(0.0, 1.0, x < y); }
        case 8u: { r = select(0.0, 1.0, x == y); }
        case 9u: { r = select(0.0, 1.0, x >= y); }
        case 10u: { r = select(0.0, 1.0, x <= y); }
        default: { r = x; }
    }
    bin_out[i] = r;
}

// --- Axis reductions --------------------------------------------------------
// The input is viewed as [outer, dim, inner]; one thread per (outer, inner).

struct AxisParams {
    op: u32,
    outer: u32,
    dim: u32,
    inner: u32,
};

@group(0) @binding(0) var<storage, read> red_in: array<f32>;
@group(0) @binding(1) var<storage, read_write> red_out: array<f32>;
@group(0) @binding(2) var<uniform> red_p: AxisParams;

@compute @workgroup_size(256)
fn reduce(@builtin(global_invocation_id) gid: vec3<u32>,
          @builtin(num_workgroups) nwg: vec3<u32>) {
    let i = flat_index(gid, nwg);
    if (i >= red_p.outer * red_p.inner) { return; }
    let o = i / red_p.inner;
    let k = i % red_p.inner;
    let base = o * red_p.dim * red_p.inner + k;
    var acc = red_in[base];
    for (var j = 1u; j < red_p.dim; j = j + 1u) {
        let v = red_in[base + j * red_p.inner];
        switch red_p.op {
            case 1u: { acc = max(acc, v); }
            case 2u: { acc = min(acc, v); }
            default: { acc = acc + v; }
        }
    }
    red_out[i] = acc;
}

// --- Softmax / log-softmax along an axis -------------------------------------
// op 0 = softmax, op 1 = log_softmax.

@group(0) @binding(0) var<storage, read> sm_in: array<f32>;
@group(0) @binding(1) var<storage, read_write> sm_out: array<f32>;
@group(0) @binding(2) var<uniform> sm_p: AxisParams;

@compute @workgroup_size(256)
fn softmax(@builtin(global_invocation_id) gid: vec3<u32>,
           @builtin(num_workgroups) nwg: vec3<u32>) {
    let i = flat_index(gid, nwg);
    if (i >= sm_p.outer * sm_p.inner) { return; }
    let o = i / sm_p.inner;
    let k = i % sm_p.inner;
    let base = o * sm_p.dim * sm_p.inner + k;
    var m = sm_in[base];
    for (var j = 1u; j < sm_p.dim; j = j + 1u) {
        m = max(m, sm_in[base + j * sm_p.inner]);
    }
    var s = 0.0;
    for (var j = 0u; j < sm_p.dim; j = j + 1u) {
        s = s + exp(sm_in[base + j * sm_p.inner] - m);
    }
    let log_s = log(s);
    for (var j = 0u; j < sm_p.dim; j = j + 1u) {
        let idx = base + j * sm_p.inner;
        let z = sm_in[idx] - m;
        if (sm_p.op == 1u) {
            sm_out[idx] = z - log_s;
        } else {
            sm_out[idx] = exp(z) / s;
        }
    }
}

// --- Batched tiled matrix multiplication -----------------------------------
// out[b] = a[b] @ b[b]; flags bit 0: a is batched, bit 1: b is batched.

struct MatmulParams {
    m: u32,
    k: u32,
    n: u32,
    flags: u32,
};

@group(0) @binding(0) var<storage, read> mm_a: array<f32>;
@group(0) @binding(1) var<storage, read> mm_b: array<f32>;
@group(0) @binding(2) var<storage, read_write> mm_out: array<f32>;
@group(0) @binding(3) var<uniform> mm_p: MatmulParams;

var<workgroup> tile_a: array<array<f32, 16>, 16>;
var<workgroup> tile_b: array<array<f32, 16>, 16>;

@compute @workgroup_size(16, 16, 1)
fn matmul(@builtin(global_invocation_id) gid: vec3<u32>,
          @builtin(local_invocation_id) lid: vec3<u32>) {
    let row = gid.y;
    let col = gid.x;
    let batch = gid.z;
    let m = mm_p.m;
    let k = mm_p.k;
    let n = mm_p.n;
    let a_off = select(0u, batch * m * k, (mm_p.flags & 1u) != 0u);
    let b_off = select(0u, batch * k * n, (mm_p.flags & 2u) != 0u);
    let o_off = batch * m * n;

    var acc = 0.0;
    let num_tiles = (k + 15u) / 16u;
    for (var t = 0u; t < num_tiles; t = t + 1u) {
        let ac = t * 16u + lid.x;
        if (row < m && ac < k) {
            tile_a[lid.y][lid.x] = mm_a[a_off + row * k + ac];
        } else {
            tile_a[lid.y][lid.x] = 0.0;
        }
        let br = t * 16u + lid.y;
        if (br < k && col < n) {
            tile_b[lid.y][lid.x] = mm_b[b_off + br * n + col];
        } else {
            tile_b[lid.y][lid.x] = 0.0;
        }
        workgroupBarrier();
        for (var i = 0u; i < 16u; i = i + 1u) {
            acc = acc + tile_a[lid.y][i] * tile_b[i][lid.x];
        }
        workgroupBarrier();
    }
    if (row < m && col < n) {
        mm_out[o_off + row * n + col] = acc;
    }
}

// --- Strided copy (permute, expand, narrow) ---------------------------------
// meta = [out_shape(ndim), source stride of each output dim(ndim)]; reads
// start at `offset`. Stride 0 broadcasts, permuted strides transpose.

struct PermuteParams {
    n: u32,
    ndim: u32,
    offset: u32,
    _pad: u32,
};

@group(0) @binding(0) var<storage, read> perm_in: array<f32>;
@group(0) @binding(1) var<storage, read_write> perm_out: array<f32>;
@group(0) @binding(2) var<uniform> perm_p: PermuteParams;
@group(0) @binding(3) var<storage, read> perm_meta: array<u32>;

@compute @workgroup_size(256)
fn permute(@builtin(global_invocation_id) gid: vec3<u32>,
           @builtin(num_workgroups) nwg: vec3<u32>) {
    let i = flat_index(gid, nwg);
    if (i >= perm_p.n) { return; }
    let nd = perm_p.ndim;
    var rem = i;
    var src = perm_p.offset;
    for (var d = 0u; d < nd; d = d + 1u) {
        let axis = nd - 1u - d;
        let size = perm_meta[axis];
        src = src + (rem % size) * perm_meta[nd + axis];
        rem = rem / size;
    }
    perm_out[i] = perm_in[src];
}

// --- Row gather (embedding lookup / index_select on dim 0) -------------------
// With ids_f32 set, the indices are f32 values (an embedding's id tensor);
// an id outside [0, rows) gives a row of zeros.

struct GatherParams {
    n_idx: u32,
    row: u32,
    rows: u32,
    ids_f32: u32,
};

@group(0) @binding(0) var<storage, read> gat_table: array<f32>;
@group(0) @binding(1) var<storage, read> gat_idx: array<u32>;
@group(0) @binding(2) var<storage, read_write> gat_out: array<f32>;
@group(0) @binding(3) var<uniform> gat_p: GatherParams;

@compute @workgroup_size(256)
fn gather_rows(@builtin(global_invocation_id) gid: vec3<u32>,
               @builtin(num_workgroups) nwg: vec3<u32>) {
    let i = flat_index(gid, nwg);
    if (i >= gat_p.n_idx * gat_p.row) { return; }
    let r = i / gat_p.row;
    let c = i % gat_p.row;
    var id = gat_idx[r];
    if (gat_p.ids_f32 != 0u) {
        let f = bitcast<f32>(id);
        id = select(gat_p.rows, u32(f), f >= 0.0 && f < f32(gat_p.rows));
    }
    if (id >= gat_p.rows) {
        gat_out[i] = 0.0;
        return;
    }
    gat_out[i] = gat_table[id * gat_p.row + c];
}
"#;