Activation Functions as Verified Operators #
Implements element-wise activation functions (ReLU, Sigmoid, Tanh) as verified operators with kernel fusion support.
These are perfect examples of fusable operations because they:
- Are element-wise (no cross-element dependencies)
- Compose nicely:
MatMul |> ReLU |> Softmax - Can be fused into preceding operations
Mathematical Definitions #
ReLU: f(x) = max(0, x)
Sigmoid: f(x) = 1 / (1 + exp(-x))
Tanh: f(x) = tanh(x)
- Forward:
y = tanh(x) - Backward:
dy/dx = 1 - y²
Activation function type
- Identity : ActivationType
- ReLU : ActivationType
- Sigmoid : ActivationType
- Gelu : ActivationType
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- Hesper.Op.Activation.instBEqActivationType.beq x✝ y✝ = (x✝.ctorIdx == y✝.ctorIdx)
Instances For
ReLU Activation #
Equations
Instances For
Equations
Instances For
CPU ReLU backward pass: gradient is 1 if x > 0, else 0
Equations
- One or more equations did not get rendered due to their size.
Instances For
GPU ReLU Kernel #
ReLU as a WGSL expression transformation. This is a pure function that can be fused into other kernels.
Equations
Instances For
GPU ReLU forward kernel. Element-wise operation that can be fused with other kernels.
Example usage:
let fused = matmul_kernel |> relu_kernel -- Fuses matmul + relu
Instances For
GPU ReLU backward kernel. Gradient: 1 if x > 0, else 0
Takes (input, grad_output) and returns grad_input. For ReLU: grad_input = grad_output if input > 0, else 0
Equations
- One or more equations did not get rendered due to their size.
Instances For
VerifiedOpFusion Instance for ReLU #
Equations
- One or more equations did not get rendered due to their size.
Helper Functions #
Create ReLU input from TensorData
Equations
- Hesper.Op.Activation.mkReLUInput data = { x := data }
Instances For
Extract TensorData from ReLU output
Equations
- Hesper.Op.Activation.getReLUOutput output = output.y
Instances For
Sigmoid Activation (TODO) #
Sigmoid as WGSL expression: 1 / (1 + exp(-x))
Equations
- Hesper.Op.Activation.sigmoidExp x = (Hesper.WGSL.Exp.litF32 1.0).div ((Hesper.WGSL.Exp.litF32 1.0).add x.neg.exp)
Instances For
Tanh Activation (TODO) #
Tanh as WGSL expression