Element-wise Operations #
Implements element-wise tensor operations for neural network layers.
Operations #
1. Add (Residual Connections) #
c[i] = a[i] + b[i]
Used in residual connections throughout transformer blocks.
2. Multiply #
c[i] = a[i] × b[i]
Used for gating mechanisms in FFN.
3. ReLU² (Squared ReLU) Activation #
ReLU²(x) = max(0, x)²
Used in BitNet b1.58 FFN layers (LLM_FFN_RELU_SQR).
4. GELU Activation #
GELU(x) ≈ 0.5 × x × (1 + tanh(√(2/π) × (x + 0.044715 × x³)))
Gaussian Error Linear Unit - used in some transformer variants.
5. Scale #
c[i] = a[i] × scalar
Used for normalization and attention scaling.
Performance #
Element-wise ops are typically memory-bound:
Compute: 1 FLOP per element
Memory: 2 reads + 1 write = 3 × 4 bytes = 12 bytes
Arithmetic intensity: 1 / 12 ≈ 0.083 FLOP/byte
On A100:
- Peak FLOPS: 19.5 TFLOPS
- Memory bandwidth: 2039 GB/s
- Bandwidth limit: 2039 / 12 ≈ 170 GFLOPS
- Utilization: 170 / 19500 ≈ 0.9% (memory-bound!)
Optimization strategies:
- Kernel fusion: Combine multiple element-wise ops
- Vectorization: Process 4 elements per thread (vec4)
- In-place: Reuse input buffer as output when possible
References #
- ReLU²: Used in BitNet b1.58 (LLM_FFN_RELU_SQR in llama.cpp)
- GELU: "Gaussian Error Linear Units" (Hendrycks & Gimpel, 2016)
Configuration #
Equations
Equations
- One or more equations did not get rendered due to their size.
Instances For
Addition (Residual Connections) #
Element-wise addition: c = a + b
Used for residual connections in transformer blocks.
@param config Operation configuration
Equations
- One or more equations did not get rendered due to their size.
Instances For
Execute element-wise addition
@param device WebGPU device @param aBuf Input buffer A @param bBuf Input buffer B @param cBuf Output buffer C @param config Configuration
Equations
- One or more equations did not get rendered due to their size.
Instances For
Multiplication (Gating) #
Element-wise multiplication: c = a × b
Used for gating in FFN layers.
@param config Operation configuration
Equations
- One or more equations did not get rendered due to their size.
Instances For
Execute element-wise multiplication
@param device WebGPU device @param aBuf Input buffer A @param bBuf Input buffer B @param cBuf Output buffer C @param config Configuration
Equations
- One or more equations did not get rendered due to their size.
Instances For
ReLU² Activation #
ReLU² (Squared ReLU) activation: y = max(0, x)²
Used in BitNet b1.58 FFN layers (LLM_FFN_RELU_SQR in llama.cpp).
@param config Operation configuration
Equations
- One or more equations did not get rendered due to their size.
Instances For
Execute ReLU² activation
@param device WebGPU device @param inputBuf Input buffer @param outputBuf Output buffer @param config Configuration
Equations
- One or more equations did not get rendered due to their size.
Instances For
GELU Activation #
GELU activation (tanh approximation)
GELU(x) ≈ 0.5 × x × (1 + tanh(√(2/π) × (x + 0.044715 × x³)))
Used in some transformer variants (BERT, GPT-2).
@param config Operation configuration
Equations
- One or more equations did not get rendered due to their size.
Instances For
Execute GELU activation
@param device WebGPU device @param inputBuf Input buffer @param outputBuf Output buffer @param config Configuration
Equations
- One or more equations did not get rendered due to their size.
Instances For
Scalar Operations #
Scalar multiplication: c = a × scalar
Used for scaling operations.
@param config Operation configuration @param scalar Scalar value to multiply by
Equations
- One or more equations did not get rendered due to their size.
Instances For
Execute scalar multiplication
@param device WebGPU device @param inputBuf Input buffer @param outputBuf Output buffer @param config Configuration @param scalar Scalar multiplier
Equations
- One or more equations did not get rendered due to their size.
Instances For
Fused Operations (Optimization) #
Fused: ReLU² + Multiply (for BitNet FFN gating)
Computes: c = ReLU²(a) × b = max(0, a)² × b
This is the gating mechanism in BitNet b1.58 FFN:
gate = ReLU²(W_gate @ x)
up = W_up @ x
result = gate × up
Fusing ReLU² + multiply saves one global memory roundtrip.
@param config Operation configuration
Equations
- One or more equations did not get rendered due to their size.
Instances For
Execute fused ReLU² + multiply
@param device WebGPU device @param aBuf Input buffer A (will be ReLU²'d) @param bBuf Input buffer B (multiplier) @param cBuf Output buffer C @param config Configuration
Equations
- One or more equations did not get rendered due to their size.
Instances For
Clamp (Gradient Clipping) #
In-place clamp kernel: data[i] = clamp(data[i], minVal, maxVal) Uses single read-write buffer to avoid aliasing issues.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Execute in-place clamp: buf[i] = clamp(buf[i], minVal, maxVal)
Equations
- One or more equations did not get rendered due to their size.