|
| 1 | +import torch |
| 2 | + |
| 3 | +from ...cutotune import CutoTuneParameter |
| 4 | +from ...utils import ensure_contiguous |
| 5 | +from .backward import _backward |
| 6 | +from .forward import _forward |
| 7 | +from .torch_implementation import fused_residual_add_rmsnorm_torch |
| 8 | + |
| 9 | + |
| 10 | +class _FusedResidualAddRMSNorm_Cute(torch.autograd.Function): |
| 11 | + @staticmethod |
| 12 | + @ensure_contiguous |
| 13 | + def forward( |
| 14 | + ctx, |
| 15 | + x: torch.Tensor, |
| 16 | + residual: torch.Tensor, |
| 17 | + weight: torch.Tensor | None, |
| 18 | + eps: float | None, |
| 19 | + multiplier: float | None, |
| 20 | + memory_efficient: bool, |
| 21 | + kernel_backend_forward: str, |
| 22 | + kernel_backend_backward: str, |
| 23 | + BLOCK_SIZE_B_forward: int, |
| 24 | + BLOCK_SIZE_B_backward: int, |
| 25 | + BLOCK_SIZE_H_forward: int, |
| 26 | + BLOCK_SIZE_H_backward: int, |
| 27 | + ) -> tuple[torch.Tensor]: |
| 28 | + if weight is not None: |
| 29 | + assert weight.dim() == 1, "weight should be 1D" |
| 30 | + assert weight.size(-1) == x.size(-1), "hidden size for x and weight tensor is different" |
| 31 | + assert weight.type() == x.type(), "tensors weight and y should have same dtype" |
| 32 | + |
| 33 | + is_x_1d = x.dim() == 1 |
| 34 | + if is_x_1d: |
| 35 | + x = x.unsqueeze(0) |
| 36 | + |
| 37 | + if eps is None: |
| 38 | + eps = torch.finfo(x.dtype).eps |
| 39 | + |
| 40 | + output, added_x_residual, rmsnorm_denominator = _forward( |
| 41 | + x=x, |
| 42 | + residual=residual, |
| 43 | + weight=weight, |
| 44 | + eps=eps, |
| 45 | + multiplier=multiplier, |
| 46 | + memory_efficient=memory_efficient, |
| 47 | + kernel_backend=kernel_backend_forward, |
| 48 | + BLOCK_SIZE_B=BLOCK_SIZE_B_forward, |
| 49 | + BLOCK_SIZE_H=BLOCK_SIZE_H_forward, |
| 50 | + ) |
| 51 | + |
| 52 | + ctx.save_for_backward(added_x_residual, weight, rmsnorm_denominator) |
| 53 | + |
| 54 | + if is_x_1d: |
| 55 | + output = output.squeeze(0) |
| 56 | + added_x_residual = added_x_residual.squeeze(0) |
| 57 | + |
| 58 | + ctx.is_x_1d = is_x_1d |
| 59 | + ctx.kernel_backend_backward = kernel_backend_backward |
| 60 | + ctx.eps = eps |
| 61 | + ctx.multiplier = multiplier |
| 62 | + ctx.BLOCK_SIZE_B_backward = BLOCK_SIZE_B_backward |
| 63 | + ctx.BLOCK_SIZE_H_backward = BLOCK_SIZE_H_backward |
| 64 | + |
| 65 | + return output, added_x_residual |
| 66 | + |
| 67 | + @staticmethod |
| 68 | + @ensure_contiguous |
| 69 | + def backward(ctx, output_grad: torch.Tensor, added_x_residual_grad: torch.Tensor) -> tuple[torch.Tensor | None]: |
| 70 | + added_x_residual, weight, rmsnorm_denominator = ctx.saved_tensors |
| 71 | + |
| 72 | + x_grad, residual_grad, weight_grad = _backward( |
| 73 | + added_x_residual=added_x_residual, |
| 74 | + weight=weight, |
| 75 | + eps=ctx.eps, |
| 76 | + multiplier=ctx.multiplier, |
| 77 | + rmsnorm_denominator=rmsnorm_denominator, |
| 78 | + output_grad=output_grad, |
| 79 | + added_x_residual_grad=added_x_residual_grad, |
| 80 | + kernel_backend=ctx.kernel_backend_backward, |
| 81 | + BLOCK_SIZE_B=ctx.BLOCK_SIZE_B_backward, |
| 82 | + BLOCK_SIZE_H=ctx.BLOCK_SIZE_H_backward, |
| 83 | + ) |
| 84 | + |
| 85 | + if ctx.is_x_1d: |
| 86 | + x_grad = x_grad.squeeze(0) |
| 87 | + residual_grad = residual_grad.squeeze(0) |
| 88 | + |
| 89 | + return x_grad, residual_grad, weight_grad, *[None] * 9 |
| 90 | + |
| 91 | + |
| 92 | +def fused_residual_add_rmsnorm_cute( |
| 93 | + x: torch.Tensor, |
| 94 | + residual: torch.Tensor, |
| 95 | + weight: torch.Tensor | None, |
| 96 | + eps: float | None, |
| 97 | + multiplier: float | None = None, |
| 98 | + memory_efficient: bool = False, |
| 99 | + kernel_backend_forward: str = CutoTuneParameter(), |
| 100 | + kernel_backend_backward: str = CutoTuneParameter(), |
| 101 | + BLOCK_SIZE_B_forward: int = CutoTuneParameter(), |
| 102 | + BLOCK_SIZE_B_backward: int = CutoTuneParameter(), |
| 103 | + BLOCK_SIZE_H_forward: int = CutoTuneParameter(), |
| 104 | + BLOCK_SIZE_H_backward: int = CutoTuneParameter(), |
| 105 | +) -> tuple[torch.Tensor]: |
| 106 | + return _FusedResidualAddRMSNorm_Cute.apply( |
| 107 | + x, |
| 108 | + residual, |
| 109 | + weight, |
| 110 | + eps, |
| 111 | + multiplier, |
| 112 | + memory_efficient, |
| 113 | + kernel_backend_forward, |
| 114 | + kernel_backend_backward, |
| 115 | + BLOCK_SIZE_B_forward, |
| 116 | + BLOCK_SIZE_B_backward, |
| 117 | + BLOCK_SIZE_H_forward, |
| 118 | + BLOCK_SIZE_H_backward, |
| 119 | + ) |
0 commit comments