Module: Ignis::JIT::Kernels::Elementwise

Defined in:
lib/nvruby/jit/kernels/elementwise.rb

Overview

Elementwise CUDA kernels for AI tensor operations. Includes arithmetic ops, initialization, and embedding ops.

Class Method Summary collapse

Class Method Details

.accumulateIgnis::JIT::Kernel

Accumulate gradients: dst += src (for gradient accumulation)

Returns:



251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/nvruby/jit/kernels/elementwise.rb', line 251

def accumulate
  source = <<~CUDA
    extern "C" __global__
    void accumulate(float* __restrict__ dst,
                    const float* __restrict__ src,
                    const int n) {
      int idx = blockIdx.x * blockDim.x + threadIdx.x;
      if (idx < n) {
        dst[idx] += src[idx];
      }
    }
  CUDA
  compile_cached(source, "accumulate")
end

.add_backward_broadcastIgnis::JIT::Kernel

Elementwise addition backward: grad passes through to both inputs (identity for add — no separate kernel needed, but useful for scalar broadcast)

Returns:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/nvruby/jit/kernels/elementwise.rb', line 31

def add_backward_broadcast
  source = <<~CUDA
    extern "C" __global__
    void add_backward_broadcast(const float* __restrict__ grad_output,
                                float* __restrict__ grad_bias,
                                const int batch_size,
                                const int features) {
      int f = blockIdx.x * blockDim.x + threadIdx.x;
      if (f < features) {
        float sum = 0.0f;
        for (int b = 0; b < batch_size; b++) {
          sum += grad_output[b * features + f];
        }
        grad_bias[f] = sum;
      }
    }
  CUDA
  compile_cached(source, "add_backward_broadcast")
end

.add_bias_rowsIgnis::JIT::Kernel

Row-broadcast bias add: out[r, c] = a[r, c] + bias (a is [rows, cols], bias is [cols]). Linear layer bias.

Returns:



458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/nvruby/jit/kernels/elementwise.rb', line 458

def add_bias_rows
  source = <<~CUDA
    extern "C" __global__
    void add_bias_rows(const float* __restrict__ a,
                       const float* __restrict__ bias,
                       float* __restrict__ out,
                       const int rows, const int cols) {
      int idx = blockIdx.x * blockDim.x + threadIdx.x;
      int total = rows * cols;
      if (idx < total) {
        out[idx] = a[idx] + bias[idx % cols];
      }
    }
  CUDA
  compile_cached(source, "add_bias_rows")
end

.add_forwardIgnis::JIT::Kernel

Elementwise addition forward: c = a + b

Returns:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/nvruby/jit/kernels/elementwise.rb', line 12

def add_forward
  source = <<~CUDA
    extern "C" __global__
    void add_forward(const float* __restrict__ a,
                     const float* __restrict__ b,
                     float* __restrict__ c,
                     const int n) {
      int idx = blockIdx.x * blockDim.x + threadIdx.x;
      if (idx < n) {
        c[idx] = a[idx] + b[idx];
      }
    }
  CUDA
  compile_cached(source, "add_forward")
end

.affine_forwardIgnis::JIT::Kernel

Affine transform: output = input * scale + shift (fp32). Used e.g. to map cuRAND U[0,1) into U[low, high).

Returns:



439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/nvruby/jit/kernels/elementwise.rb', line 439

def affine_forward
  source = <<~CUDA
    extern "C" __global__
    void affine_forward(const float* __restrict__ input,
                        float* __restrict__ output,
                        const float scale, const float shift,
                        const int n) {
      int idx = blockIdx.x * blockDim.x + threadIdx.x;
      if (idx < n) {
        output[idx] = input[idx] * scale + shift;
      }
    }
  CUDA
  compile_cached(source, "affine_forward")
end

.bf16_to_f32Ignis::JIT::Kernel

Dequantize bfloat16 → float32 on-device. bf16 is exactly the top 16 bits of an fp32 value (same sign/exponent, truncated mantissa), so widening is lossless: float32_bits = uint16(bf16) << 16. Lets us load bf16 checkpoints (e.g. Llama) into fp32 weights without materializing a giant host array.

Returns:



420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/nvruby/jit/kernels/elementwise.rb', line 420

def bf16_to_f32
  source = <<~CUDA
    extern "C" __global__
    void bf16_to_f32(const unsigned short* __restrict__ src,
                     float* __restrict__ dst,
                     const int n) {
      int i = blockIdx.x * blockDim.x + threadIdx.x;
      if (i < n) {
        unsigned int bits = ((unsigned int)src[i]) << 16;
        dst[i] = __uint_as_float(bits);
      }
    }
  CUDA
  compile_cached(source, "bf16_to_f32")
end

.broadcast_gradIgnis::JIT::Kernel

Broadcast scalar gradient back to original shape

Returns:



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/nvruby/jit/kernels/elementwise.rb', line 290

def broadcast_grad
  source = <<~CUDA
    extern "C" __global__
    void broadcast_grad(const float* __restrict__ grad_output,
                        float* __restrict__ grad_input,
                        const float scale,
                        const int n) {
      int idx = blockIdx.x * blockDim.x + threadIdx.x;
      if (idx < n) {
        grad_input[idx] = grad_output[0] * scale;
      }
    }
  CUDA
  compile_cached(source, "broadcast_grad")
end

.fillIgnis::JIT::Kernel

Fill tensor with a constant value

Returns:



161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/nvruby/jit/kernels/elementwise.rb', line 161

def fill
  source = <<~CUDA
    extern "C" __global__
    void fill(float* __restrict__ output,
              const float value,
              const int n) {
      int idx = blockIdx.x * blockDim.x + threadIdx.x;
      if (idx < n) {
        output[idx] = value;
      }
    }
  CUDA
  compile_cached(source, "fill")
end

.gather_rowsIgnis::JIT::Kernel

Gather rows for Embedding forward: output = weight[indices]

Returns:



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/nvruby/jit/kernels/elementwise.rb', line 204

def gather_rows
  source = <<~CUDA
    extern "C" __global__
    void gather_rows(const float* __restrict__ weight,
                     const int* __restrict__ indices,
                     float* __restrict__ output,
                     const int num_indices,
                     const int embed_dim) {
      int idx = blockIdx.x * blockDim.x + threadIdx.x;
      int total = num_indices * embed_dim;
      if (idx < total) {
        int row = idx / embed_dim;
        int col = idx % embed_dim;
        int src_row = indices[row];
        output[idx] = weight[src_row * embed_dim + col];
      }
    }
  CUDA
  compile_cached(source, "gather_rows")
end

.kaiming_uniform_initIgnis::JIT::Kernel

Kaiming uniform initialization: U(-bound, bound) Uses cuRAND-style Philox counter-based generator for reproducibility

Returns:



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/nvruby/jit/kernels/elementwise.rb', line 179

def kaiming_uniform_init
  source = <<~CUDA
    extern "C" __global__
    void kaiming_uniform_init(float* __restrict__ output,
                              const float bound,
                              const unsigned long long seed,
                              const int n) {
      int idx = blockIdx.x * blockDim.x + threadIdx.x;
      if (idx < n) {
        unsigned long long state = seed + (unsigned long long)idx;
        state ^= state >> 33;
        state *= 0xff51afd7ed558ccdULL;
        state ^= state >> 33;
        state *= 0xc4ceb9fe1a85ec53ULL;
        state ^= state >> 33;
        float u = (float)(state & 0xFFFFFFFF) / 4294967296.0f;
        output[idx] = (2.0f * u - 1.0f) * bound;
      }
    }
  CUDA
  compile_cached(source, "kaiming_uniform_init")
end

.max_forwardIgnis::JIT::Kernel

Elementwise maximum: c = max(a, b) (used by collective reductions)

Returns:



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/nvruby/jit/kernels/elementwise.rb', line 125

def max_forward
  source = <<~CUDA
    extern "C" __global__
    void max_forward(const float* __restrict__ a,
                     const float* __restrict__ b,
                     float* __restrict__ c,
                     const int n) {
      int idx = blockIdx.x * blockDim.x + threadIdx.x;
      if (idx < n) {
        c[idx] = fmaxf(a[idx], b[idx]);
      }
    }
  CUDA
  compile_cached(source, "max_forward")
end

.min_forwardIgnis::JIT::Kernel

Elementwise minimum: c = min(a, b) (used by collective reductions)

Returns:



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/nvruby/jit/kernels/elementwise.rb', line 107

def min_forward
  source = <<~CUDA
    extern "C" __global__
    void min_forward(const float* __restrict__ a,
                     const float* __restrict__ b,
                     float* __restrict__ c,
                     const int n) {
      int idx = blockIdx.x * blockDim.x + threadIdx.x;
      if (idx < n) {
        c[idx] = fminf(a[idx], b[idx]);
      }
    }
  CUDA
  compile_cached(source, "min_forward")
end

.mul_backwardIgnis::JIT::Kernel

Elementwise multiply backward for first operand: grad_a = grad * b

Returns:



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/nvruby/jit/kernels/elementwise.rb', line 89

def mul_backward
  source = <<~CUDA
    extern "C" __global__
    void mul_backward(const float* __restrict__ grad_output,
                      const float* __restrict__ other,
                      float* __restrict__ grad_input,
                      const int n) {
      int idx = blockIdx.x * blockDim.x + threadIdx.x;
      if (idx < n) {
        grad_input[idx] = grad_output[idx] * other[idx];
      }
    }
  CUDA
  compile_cached(source, "mul_backward")
end

.mul_forwardIgnis::JIT::Kernel

Elementwise multiplication forward: c = a * b (Hadamard product)

Returns:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/nvruby/jit/kernels/elementwise.rb', line 71

def mul_forward
  source = <<~CUDA
    extern "C" __global__
    void mul_forward(const float* __restrict__ a,
                     const float* __restrict__ b,
                     float* __restrict__ c,
                     const int n) {
      int idx = blockIdx.x * blockDim.x + threadIdx.x;
      if (idx < n) {
        c[idx] = a[idx] * b[idx];
      }
    }
  CUDA
  compile_cached(source, "mul_forward")
end

.scale_forwardIgnis::JIT::Kernel

Scalar multiplication: output = input * scalar

Returns:



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/nvruby/jit/kernels/elementwise.rb', line 143

def scale_forward
  source = <<~CUDA
    extern "C" __global__
    void scale_forward(const float* __restrict__ input,
                       float* __restrict__ output,
                       const float scalar,
                       const int n) {
      int idx = blockIdx.x * blockDim.x + threadIdx.x;
      if (idx < n) {
        output[idx] = input[idx] * scalar;
      }
    }
  CUDA
  compile_cached(source, "scale_forward")
end

.scatter_addIgnis::JIT::Kernel

Scatter add for Embedding backward: weight_grad[indices] += grad Uses atomicAdd for thread safety

Returns:



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/nvruby/jit/kernels/elementwise.rb', line 228

def scatter_add
  source = <<~CUDA
    extern "C" __global__
    void scatter_add(const float* __restrict__ grad_output,
                     const int* __restrict__ indices,
                     float* __restrict__ grad_weight,
                     const int num_indices,
                     const int embed_dim) {
      int idx = blockIdx.x * blockDim.x + threadIdx.x;
      int total = num_indices * embed_dim;
      if (idx < total) {
        int row = idx / embed_dim;
        int col = idx % embed_dim;
        int dst_row = indices[row];
        atomicAdd(&grad_weight[dst_row * embed_dim + col], grad_output[idx]);
      }
    }
  CUDA
  compile_cached(source, "scatter_add")
end

.scatter_colsIgnis::JIT::Kernel

Inverse of slice_cols: dst[r, col_off + c] = src[r, c]. Used to scatter per-head [seq, head_dim] results back into [seq, embed].

Returns:



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/nvruby/jit/kernels/elementwise.rb', line 370

def scatter_cols
  source = <<~CUDA
    extern "C" __global__
    void scatter_cols(const float* __restrict__ src,
                      float* __restrict__ dst,
                      const int rows, const int total_cols,
                      const int col_off, const int len) {
      int idx = blockIdx.x * blockDim.x + threadIdx.x;
      int total = rows * len;
      if (idx < total) {
        int r = idx / len;
        int c = idx % len;
        dst[r * total_cols + col_off + c] = src[idx];
      }
    }
  CUDA
  compile_cached(source, "scatter_cols")
end

.scatter_cols_addIgnis::JIT::Kernel

Accumulating scatter: dst[r, col_off + c] = src[r, c]. Used for GQA backward, where the group_size query heads sharing one KV head each contribute to the same dK/dV columns — their gradients must SUM, not overwrite. (Columns are disjoint across rows, so no atomics are needed: each (r, col_offc) is written by exactly one thread here; accumulation across heads happens via separate launches into the buffer.)

Returns:



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/nvruby/jit/kernels/elementwise.rb', line 396

def scatter_cols_add
  source = <<~CUDA
    extern "C" __global__
    void scatter_cols_add(const float* __restrict__ src,
                          float* __restrict__ dst,
                          const int rows, const int total_cols,
                          const int col_off, const int len) {
      int idx = blockIdx.x * blockDim.x + threadIdx.x;
      int total = rows * len;
      if (idx < total) {
        int r = idx / len;
        int c = idx % len;
        dst[r * total_cols + col_off + c] += src[idx];
      }
    }
  CUDA
  compile_cached(source, "scatter_cols_add")
end

.slice_colsIgnis::JIT::Kernel

Copy a contiguous column range [col_off, col_off+len) from each row. dst[r, c] = src[r, col_off + c] (dst is [rows, len], src is [rows, total_cols]). Used to split [seq, embed] projections into per-head [seq, head_dim] slices.

Returns:



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/nvruby/jit/kernels/elementwise.rb', line 348

def slice_cols
  source = <<~CUDA
    extern "C" __global__
    void slice_cols(const float* __restrict__ src,
                    float* __restrict__ dst,
                    const int rows, const int total_cols,
                    const int col_off, const int len) {
      int idx = blockIdx.x * blockDim.x + threadIdx.x;
      int total = rows * len;
      if (idx < total) {
        int r = idx / len;
        int c = idx % len;
        dst[idx] = src[r * total_cols + col_off + c];
      }
    }
  CUDA
  compile_cached(source, "slice_cols")
end

.sub_forwardIgnis::JIT::Kernel

Elementwise subtraction forward: c = a - b

Returns:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/nvruby/jit/kernels/elementwise.rb', line 53

def sub_forward
  source = <<~CUDA
    extern "C" __global__
    void sub_forward(const float* __restrict__ a,
                     const float* __restrict__ b,
                     float* __restrict__ c,
                     const int n) {
      int idx = blockIdx.x * blockDim.x + threadIdx.x;
      if (idx < n) {
        c[idx] = a[idx] - b[idx];
      }
    }
  CUDA
  compile_cached(source, "sub_forward")
end

.sum_reduceIgnis::JIT::Kernel

Sum reduction along the last dimension

Returns:



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/nvruby/jit/kernels/elementwise.rb', line 268

def sum_reduce
  source = <<~CUDA
    extern "C" __global__
    void sum_reduce(const float* __restrict__ input,
                    float* __restrict__ output,
                    const int outer_size,
                    const int reduce_size) {
      int idx = blockIdx.x * blockDim.x + threadIdx.x;
      if (idx < outer_size) {
        float sum = 0.0f;
        for (int j = 0; j < reduce_size; j++) {
          sum += input[idx * reduce_size + j];
        }
        output[idx] = sum;
      }
    }
  CUDA
  compile_cached(source, "sum_reduce")
end

.transpose_2dIgnis::JIT::Kernel

Transpose 2D matrix: output = input Tiled for coalesced memory access

Returns:



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
# File 'lib/nvruby/jit/kernels/elementwise.rb', line 309

def transpose_2d
  source = <<~CUDA
    #define TILE_DIM 32
    #define BLOCK_ROWS 8

    extern "C" __global__
    void transpose_2d(const float* __restrict__ input,
                      float* __restrict__ output,
                      const int rows,
                      const int cols) {
      __shared__ float tile[TILE_DIM][TILE_DIM + 1];

      int x = blockIdx.x * TILE_DIM + threadIdx.x;
      int y = blockIdx.y * TILE_DIM + threadIdx.y;

      for (int j = 0; j < TILE_DIM; j += BLOCK_ROWS) {
        if (x < cols && (y + j) < rows) {
          tile[threadIdx.y + j][threadIdx.x] = input[(y + j) * cols + x];
        }
      }
      __syncthreads();

      x = blockIdx.y * TILE_DIM + threadIdx.x;
      y = blockIdx.x * TILE_DIM + threadIdx.y;

      for (int j = 0; j < TILE_DIM; j += BLOCK_ROWS) {
        if (x < rows && (y + j) < cols) {
          output[(y + j) * rows + x] = tile[threadIdx.x][threadIdx.y + j];
        }
      }
    }
  CUDA
  compile_cached(source, "transpose_2d")
end