(self, hidden_states, context=None, deterministic=True)
| 179 | return tensor |
| 180 | |
| 181 | def __call__(self, hidden_states, context=None, deterministic=True): |
| 182 | context = hidden_states if context is None else context |
| 183 | |
| 184 | query_proj = self.query(hidden_states) |
| 185 | key_proj = self.key(context) |
| 186 | value_proj = self.value(context) |
| 187 | |
| 188 | if self.split_head_dim: |
| 189 | b = hidden_states.shape[0] |
| 190 | query_states = jnp.reshape(query_proj, (b, -1, self.heads, self.dim_head)) |
| 191 | key_states = jnp.reshape(key_proj, (b, -1, self.heads, self.dim_head)) |
| 192 | value_states = jnp.reshape(value_proj, (b, -1, self.heads, self.dim_head)) |
| 193 | else: |
| 194 | query_states = self.reshape_heads_to_batch_dim(query_proj) |
| 195 | key_states = self.reshape_heads_to_batch_dim(key_proj) |
| 196 | value_states = self.reshape_heads_to_batch_dim(value_proj) |
| 197 | |
| 198 | if self.use_memory_efficient_attention: |
| 199 | query_states = query_states.transpose(1, 0, 2) |
| 200 | key_states = key_states.transpose(1, 0, 2) |
| 201 | value_states = value_states.transpose(1, 0, 2) |
| 202 | |
| 203 | # this if statement create a chunk size for each layer of the unet |
| 204 | # the chunk size is equal to the query_length dimension of the deepest layer of the unet |
| 205 | |
| 206 | flatten_latent_dim = query_states.shape[-3] |
| 207 | if flatten_latent_dim % 64 == 0: |
| 208 | query_chunk_size = int(flatten_latent_dim / 64) |
| 209 | elif flatten_latent_dim % 16 == 0: |
| 210 | query_chunk_size = int(flatten_latent_dim / 16) |
| 211 | elif flatten_latent_dim % 4 == 0: |
| 212 | query_chunk_size = int(flatten_latent_dim / 4) |
| 213 | else: |
| 214 | query_chunk_size = int(flatten_latent_dim) |
| 215 | |
| 216 | hidden_states = jax_memory_efficient_attention( |
| 217 | query_states, key_states, value_states, query_chunk_size=query_chunk_size, key_chunk_size=4096 * 4 |
| 218 | ) |
| 219 | |
| 220 | hidden_states = hidden_states.transpose(1, 0, 2) |
| 221 | else: |
| 222 | # compute attentions |
| 223 | if self.split_head_dim: |
| 224 | attention_scores = jnp.einsum("b t n h, b f n h -> b n f t", key_states, query_states) |
| 225 | else: |
| 226 | attention_scores = jnp.einsum("b i d, b j d->b i j", query_states, key_states) |
| 227 | |
| 228 | attention_scores = attention_scores * self.scale |
| 229 | attention_probs = nn.softmax(attention_scores, axis=-1 if self.split_head_dim else 2) |
| 230 | |
| 231 | # attend to values |
| 232 | if self.split_head_dim: |
| 233 | hidden_states = jnp.einsum("b n f t, b t n h -> b f n h", attention_probs, value_states) |
| 234 | b = hidden_states.shape[0] |
| 235 | hidden_states = jnp.reshape(hidden_states, (b, -1, self.heads * self.dim_head)) |
| 236 | else: |
| 237 | hidden_states = jnp.einsum("b i j, b j d -> b i d", attention_probs, value_states) |
| 238 | hidden_states = self.reshape_batch_dim_to_heads(hidden_states) |
nothing calls this directly
no test coverage detected