|
| 1 | +import torch |
| 2 | +from executorch.examples.models.llama.attention import ForwardOptions |
| 3 | +from executorch.examples.models.llama.feed_forward import FeedForward |
| 4 | + |
| 5 | +from executorch.examples.models.llama.norm import RMSNorm |
| 6 | +from torch import nn |
| 7 | + |
| 8 | + |
| 9 | +class ShortConv(nn.Module): |
| 10 | + def __init__( |
| 11 | + self, |
| 12 | + dim: int, |
| 13 | + L_cache: int = 3, |
| 14 | + bias: bool = False, |
| 15 | + device: torch.device = None, |
| 16 | + dtype: torch.dtype = None, |
| 17 | + ): |
| 18 | + super().__init__() |
| 19 | + self.dim = dim |
| 20 | + self.L_cache = L_cache |
| 21 | + self.device = device |
| 22 | + self.dtype = dtype |
| 23 | + self.bias = bias |
| 24 | + |
| 25 | + self.conv = nn.Conv1d( |
| 26 | + dim, |
| 27 | + dim, |
| 28 | + kernel_size=L_cache, |
| 29 | + padding=0, ## we don't need padding since we handle it manually |
| 30 | + groups=dim, |
| 31 | + bias=bias, |
| 32 | + ) |
| 33 | + |
| 34 | + conv_state = torch.zeros( |
| 35 | + 1, ## batch size is assumed to be 1 for now |
| 36 | + dim, |
| 37 | + L_cache - 1, |
| 38 | + device="cpu", |
| 39 | + ) |
| 40 | + self.register_buffer("conv_state", conv_state) |
| 41 | + |
| 42 | + ## better performance in Executorch with separate projections |
| 43 | + self.B_proj = nn.Linear(dim, dim, bias=bias) |
| 44 | + self.C_proj = nn.Linear(dim, dim, bias=bias) |
| 45 | + self.x_proj = nn.Linear(dim, dim, bias=bias) |
| 46 | + |
| 47 | + self.out_proj = nn.Linear(dim, dim, bias=bias) |
| 48 | + |
| 49 | + def forward(self, x: torch.Tensor) -> torch.Tensor: |
| 50 | + batch_size, seqlen, dim = x.size() |
| 51 | + assert batch_size == 1, "batch_size must be 1" |
| 52 | + |
| 53 | + B = self.B_proj(x).transpose(-1, -2) # (batch_size, dim, seq_len) |
| 54 | + C = self.C_proj(x).transpose(-1, -2) # (batch_size, dim, seq_len) |
| 55 | + x = self.x_proj(x).transpose(-1, -2) # (batch_size, dim, seq_len) |
| 56 | + |
| 57 | + Bx = B * x # (batch_size, dim, seq_len) |
| 58 | + |
| 59 | + ## This is where we handle padding |
| 60 | + ## By default, the conv_state is initialized to 0. |
| 61 | + # So, assuming prefill is done on an empty cache, concatenating conv_state to the beginning of the sequence acts similary to |
| 62 | + ## using nn.Conv1d(padding=L_cache-1) (for prefill) without no manual padding. |
| 63 | + ## However, the manual padding has the added benefit of being correct during decode, when the cache is not initialized to 0. |
| 64 | + Bx = torch.cat( |
| 65 | + [self.conv_state, Bx], dim=-1 |
| 66 | + ) # (batch_size, dim, seq_len + L_cache - 1) |
| 67 | + |
| 68 | + ## Update the conv_state |
| 69 | + new_conv_state = Bx[ |
| 70 | + ..., -(self.L_cache - 1) : |
| 71 | + ] # (batch_size, dim, L_cache - 1) |
| 72 | + with torch.no_grad(): |
| 73 | + self.conv_state.copy_(new_conv_state) |
| 74 | + |
| 75 | + conv_out = self.conv(Bx)[..., : x.size(-1)] # (batch_size, dim, seq_len) |
| 76 | + y = C * conv_out # (batch_size, dim, seq_len) |
| 77 | + |
| 78 | + y = y.transpose(-1, -2) # (batch_size, seq_len, dim) |
| 79 | + y = y.contiguous() # (batch_size, seq_len, dim) |
| 80 | + y = self.out_proj(y) # (batch_size, seq_len, dim) |
| 81 | + return y |
| 82 | + |
| 83 | + def reset_cache(self): |
| 84 | + self.conv_state.zero_() |
| 85 | + |
| 86 | + |
| 87 | +class ShortConvBlock(nn.Module): |
| 88 | + def __init__(self, dim: int, hidden_dim: int, norm_eps: float): |
| 89 | + super().__init__() |
| 90 | + self.L_cache = 3 # hardcode 3 for now |
| 91 | + self.conv = ShortConv(dim, self.L_cache, bias=False) |
| 92 | + self.feed_forward = FeedForward(dim, hidden_dim) |
| 93 | + self.ffn_norm = RMSNorm(dim, norm_eps) |
| 94 | + # use attention_norm norm instead of operator_norm to unify with TransformerBlock |
| 95 | + self.attention_norm = RMSNorm(dim, norm_eps) |
| 96 | + |
| 97 | + def forward( |
| 98 | + self, |
| 99 | + x, |
| 100 | + freqs_cos=None, |
| 101 | + freqs_sin=None, |
| 102 | + _unused_attn_options: ForwardOptions = None, |
| 103 | + ): # x: 1xN |
| 104 | + h = self.conv.forward(self.attention_norm(x)) |
| 105 | + h = x + h |
| 106 | + out = h + self.feed_forward(self.ffn_norm(h)) |
| 107 | + return out, None |
| 108 | + |
| 109 | + def reset_cache(self): |
| 110 | + self.conv.reset_cache() |
0 commit comments