(
self,
x: torch.Tensor,
cache: LayerCache,
attn_bias: AttnBias,
)
| 119 | self.attn_sub_norm = RMSNorm(dim, norm_eps) |
| 120 | |
| 121 | def forward( |
| 122 | self, |
| 123 | x: torch.Tensor, |
| 124 | cache: LayerCache, |
| 125 | attn_bias: AttnBias, |
| 126 | ) -> torch.Tensor: |
| 127 | |
| 128 | xqkv = self.wqkv(x) |
| 129 | xq = xqkv[:, : (self.n_local_heads * self.head_dim)] |
| 130 | xkv = xqkv[:, (self.n_local_heads * self.head_dim) :] |
| 131 | xk, xv = xkv.chunk(2, 1) |
| 132 | |
| 133 | output_shape = xq.shape |
| 134 | heads_per_group = self.n_local_heads // self.n_local_kv_heads |
| 135 | xq = xq.view( |
| 136 | 1, xq.shape[0], self.n_local_kv_heads, heads_per_group, self.head_dim |
| 137 | ) |
| 138 | xk = xk.view(1, xk.shape[0], self.n_local_kv_heads, 1, self.head_dim) |
| 139 | # xq = rearrange(xq, 'b (g h l d) -> 1 b h g (d l)', g=heads_per_group, h=self.n_local_kv_heads, d=self.head_dim // 2, l=2) |
| 140 | # xk = rearrange(xk, 'b (g l d) -> 1 b g 1 (d l)', g=self.n_local_kv_heads, d=self.head_dim // 2) |
| 141 | xv = xv.view(1, xv.shape[0], self.n_local_kv_heads, 1, self.head_dim) |
| 142 | cache_k, cache_v = cache |
| 143 | |
| 144 | xq = rope_padded( |
| 145 | xq=xq, |
| 146 | xk=xk, |
| 147 | xv=xv, |
| 148 | cache_k=cache_k, |
| 149 | cache_v=cache_v, |
| 150 | attn_bias=attn_bias, |
| 151 | theta=self.rope_theta, |
| 152 | ) |
| 153 | |
| 154 | output = fmha.memory_efficient_attention_forward( |
| 155 | xq, cache_k, cache_v, attn_bias, op = fmha.flash.FwOp |
| 156 | ) |
| 157 | |
| 158 | output = output.reshape(output_shape) |
| 159 | output = self.attn_sub_norm(output) |
| 160 | output = self.wo(output) |
| 161 | |
| 162 | return output |
| 163 | |
| 164 | @torch.compile |
| 165 | def squared_relu(x: torch.Tensor) -> torch.Tensor: |
nothing calls this directly
no outgoing calls
no test coverage detected