MCPcopy Create free account
hub / github.com/RightNow-AI/autokernel / _LinearWrapper

Class _LinearWrapper

verify.py:408–438  ·  view source on GitHub ↗

Wraps nn.Linear to use an optimized matmul kernel_fn.

Source from the content-addressed store, hash-verified

406
407
408class _LinearWrapper(nn.Module):
409 """Wraps nn.Linear to use an optimized matmul kernel_fn."""
410
411 def __init__(self, original: nn.Linear, kernel_fn: Callable):
412 super().__init__()
413 self.original = original
414 self.kernel_fn = kernel_fn
415 self.weight = original.weight
416 self.bias = original.bias
417
418 def forward(self, x: torch.Tensor) -> torch.Tensor:
419 # Reshape to 2D for kernel_fn, then reshape back
420 orig_shape = x.shape
421 if x.dim() > 2:
422 x_2d = x.reshape(-1, x.shape[-1])
423 else:
424 x_2d = x
425
426 # kernel_fn expects (A, B) where A @ B = C
427 # For nn.Linear: output = input @ weight.T + bias
428 # So we call kernel_fn(input, weight.T)
429 weight_t = self.weight.t().contiguous()
430 out = self.kernel_fn(x_2d, weight_t)
431
432 if self.bias is not None:
433 out = out + self.bias
434
435 if len(orig_shape) > 2:
436 out = out.reshape(*orig_shape[:-1], out.shape[-1])
437
438 return out
439
440
441class _LayerNormWrapper(nn.Module):

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected