| 131 | |
| 132 | |
| 133 | class AttentionControl(abc.ABC): |
| 134 | |
| 135 | def step_callback(self, x_t): |
| 136 | return x_t |
| 137 | |
| 138 | def between_steps(self): |
| 139 | return |
| 140 | |
| 141 | @property |
| 142 | def num_uncond_att_layers(self): |
| 143 | return self.num_att_layers if LOW_RESOURCE else 0 |
| 144 | |
| 145 | @abc.abstractmethod |
| 146 | def forward(self, attn, is_cross: bool, place_in_unet: str): |
| 147 | raise NotImplementedError |
| 148 | |
| 149 | def __call__(self, attn, is_cross: bool, place_in_unet: str): |
| 150 | if self.cur_att_layer >= self.num_uncond_att_layers: |
| 151 | if LOW_RESOURCE: |
| 152 | attn = self.forward(attn, is_cross, place_in_unet) |
| 153 | else: |
| 154 | h = attn.shape[0] |
| 155 | attn[h // 2:] = self.forward(attn[h // 2:], is_cross, place_in_unet) |
| 156 | self.cur_att_layer += 1 |
| 157 | # print(self.num_att_layers) |
| 158 | # print(self.num_uncond_att_layers) |
| 159 | if self.cur_att_layer == self.num_att_layers + self.num_uncond_att_layers: |
| 160 | self.cur_att_layer = 0 |
| 161 | self.cur_step += 1 |
| 162 | # print(self.cur_step) |
| 163 | self.between_steps() |
| 164 | return attn |
| 165 | |
| 166 | def reset(self): |
| 167 | self.cur_step = 0 |
| 168 | self.cur_att_layer = 0 |
| 169 | |
| 170 | def __init__(self): |
| 171 | self.cur_step = 0 |
| 172 | self.num_att_layers = -1 |
| 173 | self.cur_att_layer = 0 |
| 174 | |
| 175 | |
| 176 | class AttentionStore(AttentionControl): |
nothing calls this directly
no outgoing calls
no test coverage detected