Skip to content
fn

fused_linear_relu

→Tensor
fused_linear_relu(x: Tensor, weight: Tensor, bias: Tensor)

Fused linear + ReLU forward kernel.

Computes ReLU(xW⊤+b)\text{ReLU}(xW^\top + b) in a single CPU pass — the pre-activation tensor is never materialised, halving memory traffic on large MLP blocks. Falls back to the unfused two-op path whenever any input requires gradient so that autograd remains correct.

Parameters

Input, shape (..., in_features).
weightTensor
Weight matrix, shape (out_features, in_features).
biasTensor
Bias vector, shape (out_features,).

Returns

Tensor

relu(x @ weight.T + bias), shape (..., out_features).

Notes

y=max⁡ ⁣(0, xW⊤+b)y = \max\!\big(0,\, x W^\top + b\big)

Implementation: a single BLAS SGEMM followed by vDSP vrelu over the GEMM output buffer for CPU float32; on Metal and in the half formats, the backend's linear followed by its ReLU. Runs the fused kernel when grad-mode is disabled or none of x / weight / bias has requires_grad=True. In training mode the call routes through the standard linear + relu graph so autograd's backward derivations are exact.

Examples

>>> import lucid
>>> from lucid.nn.functional import fused_linear_relu
>>> x = lucid.randn(8, 16)
>>> w = lucid.randn(32, 16)
>>> b = lucid.zeros(32)
>>> fused_linear_relu(x, w, b).shape
(8, 32)

Used by 2