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

Class _RMSNormWrapper

verify.py:478–507  ·  view source on GitHub ↗

Wraps RMSNorm-like modules to use an optimized kernel_fn.

Source from the content-addressed store, hash-verified

476
477
478class _RMSNormWrapper(nn.Module):
479 """Wraps RMSNorm-like modules to use an optimized kernel_fn."""
480
481 def __init__(self, original: nn.Module, kernel_fn: Callable):
482 super().__init__()
483 self.original = original
484 self.kernel_fn = kernel_fn
485 # RMSNorm typically has a 'weight' attribute
486 self.weight = getattr(original, "weight", None)
487 self.eps = getattr(original, "eps", 1e-6)
488
489 def forward(self, x: torch.Tensor) -> torch.Tensor:
490 orig_shape = x.shape
491 if x.dim() > 2:
492 x_2d = x.reshape(-1, x.shape[-1])
493 else:
494 x_2d = x
495
496 if self.weight is not None:
497 try:
498 out = self.kernel_fn(x_2d, self.weight, self.eps)
499 except TypeError:
500 out = self.kernel_fn(x_2d, self.weight)
501 else:
502 out = self.kernel_fn(x_2d)
503
504 if len(orig_shape) > 2:
505 out = out.reshape(orig_shape)
506
507 return out
508
509
510class OptimizedModelContext:

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected