(self, x, context=None, mask=None)
| 142 | change_checkpoint(self.model.diffusion_model) |
| 143 | |
| 144 | def new_forward(self, x, context=None, mask=None): |
| 145 | h = self.heads |
| 146 | crossattn = False |
| 147 | if context is not None: |
| 148 | crossattn = True |
| 149 | q = self.to_q(x) |
| 150 | context = default(context, x) |
| 151 | k = self.to_k(context) |
| 152 | v = self.to_v(context) |
| 153 | |
| 154 | if crossattn: |
| 155 | modifier = torch.ones_like(k) |
| 156 | modifier[:, :1, :] = modifier[:, :1, :]*0. |
| 157 | k = modifier*k + (1-modifier)*k.detach() |
| 158 | v = modifier*v + (1-modifier)*v.detach() |
| 159 | |
| 160 | q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> (b h) n d', h=h), (q, k, v)) |
| 161 | sim = einsum('b i d, b j d -> b i j', q, k) * self.scale |
| 162 | attn = sim.softmax(dim=-1) |
| 163 | |
| 164 | out = einsum('b i j, b j d -> b i d', attn, v) |
| 165 | out = rearrange(out, '(b h) n d -> b n (h d)', h=h) |
| 166 | return self.to_out(out) |
| 167 | |
| 168 | def change_forward(model): |
| 169 | for layer in model.children(): |
nothing calls this directly
no outgoing calls
no test coverage detected