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
-
.accumulate ⇒ Ignis::JIT::Kernel
Accumulate gradients: dst += src (for gradient accumulation).
-
.add_backward_broadcast ⇒ Ignis::JIT::Kernel
Elementwise addition backward: grad passes through to both inputs (identity for add — no separate kernel needed, but useful for scalar broadcast).
-
.add_bias_rows ⇒ Ignis::JIT::Kernel
Row-broadcast bias add: out[r, c] = a[r, c] + bias (a is [rows, cols], bias is [cols]).
-
.add_forward ⇒ Ignis::JIT::Kernel
Elementwise addition forward: c = a + b.
-
.affine_forward ⇒ Ignis::JIT::Kernel
Affine transform: output = input * scale + shift (fp32).
-
.bf16_to_f32 ⇒ Ignis::JIT::Kernel
Dequantize bfloat16 → float32 on-device.
-
.broadcast_grad ⇒ Ignis::JIT::Kernel
Broadcast scalar gradient back to original shape.
-
.fill ⇒ Ignis::JIT::Kernel
Fill tensor with a constant value.
-
.gather_rows ⇒ Ignis::JIT::Kernel
Gather rows for Embedding forward: output = weight[indices].
-
.kaiming_uniform_init ⇒ Ignis::JIT::Kernel
Kaiming uniform initialization: U(-bound, bound) Uses cuRAND-style Philox counter-based generator for reproducibility.
-
.max_forward ⇒ Ignis::JIT::Kernel
Elementwise maximum: c = max(a, b) (used by collective reductions).
-
.min_forward ⇒ Ignis::JIT::Kernel
Elementwise minimum: c = min(a, b) (used by collective reductions).
-
.mul_backward ⇒ Ignis::JIT::Kernel
Elementwise multiply backward for first operand: grad_a = grad * b.
-
.mul_forward ⇒ Ignis::JIT::Kernel
Elementwise multiplication forward: c = a * b (Hadamard product).
-
.scale_forward ⇒ Ignis::JIT::Kernel
Scalar multiplication: output = input * scalar.
-
.scatter_add ⇒ Ignis::JIT::Kernel
Scatter add for Embedding backward: weight_grad[indices] += grad Uses atomicAdd for thread safety.
-
.scatter_cols ⇒ Ignis::JIT::Kernel
Inverse of slice_cols: dst[r, col_off + c] = src[r, c].
-
.scatter_cols_add ⇒ Ignis::JIT::Kernel
Accumulating scatter: dst[r, col_off + c] += src[r, c].
-
.slice_cols ⇒ Ignis::JIT::Kernel
Copy a contiguous column range [col_off, col_off+len) from each row.
-
.sub_forward ⇒ Ignis::JIT::Kernel
Elementwise subtraction forward: c = a - b.
-
.sum_reduce ⇒ Ignis::JIT::Kernel
Sum reduction along the last dimension.
- .transpose_2d ⇒ Ignis::JIT::Kernel
Class Method Details
.accumulate ⇒ Ignis::JIT::Kernel
Accumulate gradients: dst += src (for gradient accumulation)
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_broadcast ⇒ Ignis::JIT::Kernel
Elementwise addition backward: grad passes through to both inputs (identity for add — no separate kernel needed, but useful for scalar broadcast)
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_rows ⇒ Ignis::JIT::Kernel
Row-broadcast bias add: out[r, c] = a[r, c] + bias (a is [rows, cols], bias is [cols]). Linear layer bias.
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_forward ⇒ Ignis::JIT::Kernel
Elementwise addition forward: c = a + b
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_forward ⇒ Ignis::JIT::Kernel
Affine transform: output = input * scale + shift (fp32). Used e.g. to map cuRAND U[0,1) into U[low, high).
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_f32 ⇒ Ignis::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.
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_grad ⇒ Ignis::JIT::Kernel
Broadcast scalar gradient back to original shape
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 |
.fill ⇒ Ignis::JIT::Kernel
Fill tensor with a constant value
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_rows ⇒ Ignis::JIT::Kernel
Gather rows for Embedding forward: output = weight[indices]
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_init ⇒ Ignis::JIT::Kernel
Kaiming uniform initialization: U(-bound, bound) Uses cuRAND-style Philox counter-based generator for reproducibility
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_forward ⇒ Ignis::JIT::Kernel
Elementwise maximum: c = max(a, b) (used by collective reductions)
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_forward ⇒ Ignis::JIT::Kernel
Elementwise minimum: c = min(a, b) (used by collective reductions)
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_backward ⇒ Ignis::JIT::Kernel
Elementwise multiply backward for first operand: grad_a = grad * b
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_forward ⇒ Ignis::JIT::Kernel
Elementwise multiplication forward: c = a * b (Hadamard product)
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_forward ⇒ Ignis::JIT::Kernel
Scalar multiplication: output = input * scalar
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_add ⇒ Ignis::JIT::Kernel
Scatter add for Embedding backward: weight_grad[indices] += grad Uses atomicAdd for thread safety
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_cols ⇒ Ignis::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].
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_add ⇒ Ignis::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.)
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_cols ⇒ Ignis::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.
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_forward ⇒ Ignis::JIT::Kernel
Elementwise subtraction forward: c = a - b
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_reduce ⇒ Ignis::JIT::Kernel
Sum reduction along the last dimension
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_2d ⇒ Ignis::JIT::Kernel
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 |