Flax memory efficient attention (#2889) · Becktor/diffusers@dc27750 · GitHub
Skip to content

Commit dc27750

Browse files
pcuencamuhammad_hanifMuhHanif
authored
Flax memory efficient attention (huggingface#2889)
* add use_memory_efficient params placeholder * test * add memory efficient attention jax * add memory efficient attention jax * newline * forgot dot * Rename use_memory_efficient * Keep dtype last. * Actually use key_chunk_size * Rename symbol * Apply style * Rename use_memory_efficient * Keep dtype last * Pass `use_memory_efficient_attention` in `from_pretrained` * Move JAX memory efficient attention to attention_flax. * Simple test. * style --------- Co-authored-by: muhammad_hanif <muhammad_hanif@sofcograha.co.id> Co-authored-by: MuhHanif <48muhhanif@gmail.com>
1 parent 0df47ef commit dc27750

5 files changed

Lines changed: 216 additions & 9 deletions

File tree

‎src/diffusers/models/attention_flax.py‎

Lines changed: 147 additions & 8 deletions

‎src/diffusers/models/unet_2d_blocks_flax.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class FlaxCrossAttnDownBlock2D(nn.Module):
3737
Number of attention heads of each spatial transformer block
3838
add_downsample (:obj:`bool`, *optional*, defaults to `True`):
3939
Whether to add downsampling layer before each final output
40+
use_memory_efficient_attention (`bool`, *optional*, defaults to `False`):
41+
enable memory efficient attention https://arxiv.org/abs/2112.05682
4042
dtype (:obj:`jnp.dtype`, *optional*, defaults to jnp.float32):
4143
Parameters `dtype`
4244
"""
@@ -48,6 +50,7 @@ class FlaxCrossAttnDownBlock2D(nn.Module):
4850
add_downsample: bool = True
4951
use_linear_projection: bool = False
5052
only_cross_attention: bool = False
53+
use_memory_efficient_attention: bool = False
5154
dtype: jnp.dtype = jnp.float32
5255

5356
def setup(self):
@@ -72,6 +75,7 @@ def setup(self):
7275
depth=1,
7376
use_linear_projection=self.use_linear_projection,
7477
only_cross_attention=self.only_cross_attention,
78+
use_memory_efficient_attention=self.use_memory_efficient_attention,
7579
dtype=self.dtype,
7680
)
7781
attentions.append(attn_block)
@@ -172,6 +176,8 @@ class FlaxCrossAttnUpBlock2D(nn.Module):
172176
Number of attention heads of each spatial transformer block
173177
add_upsample (:obj:`bool`, *optional*, defaults to `True`):
174178
Whether to add upsampling layer before each final output
179+
use_memory_efficient_attention (`bool`, *optional*, defaults to `False`):
180+
enable memory efficient attention https://arxiv.org/abs/2112.05682
175181
dtype (:obj:`jnp.dtype`, *optional*, defaults to jnp.float32):
176182
Parameters `dtype`
177183
"""
@@ -184,6 +190,7 @@ class FlaxCrossAttnUpBlock2D(nn.Module):
184190
add_upsample: bool = True
185191
use_linear_projection: bool = False
186192
only_cross_attention: bool = False
193+
use_memory_efficient_attention: bool = False
187194
dtype: jnp.dtype = jnp.float32
188195

189196
def setup(self):
@@ -209,6 +216,7 @@ def setup(self):
209216
depth=1,
210217
use_linear_projection=self.use_linear_projection,
211218
only_cross_attention=self.only_cross_attention,
219+
use_memory_efficient_attention=self.use_memory_efficient_attention,
212220
dtype=self.dtype,
213221
)
214222
attentions.append(attn_block)
@@ -311,6 +319,8 @@ class FlaxUNetMidBlock2DCrossAttn(nn.Module):
311319
Number of attention blocks layers
312320
attn_num_head_channels (:obj:`int`, *optional*, defaults to 1):
313321
Number of attention heads of each spatial transformer block
322+
use_memory_efficient_attention (`bool`, *optional*, defaults to `False`):
323+
enable memory efficient attention https://arxiv.org/abs/2112.05682
314324
dtype (:obj:`jnp.dtype`, *optional*, defaults to jnp.float32):
315325
Parameters `dtype`
316326
"""
@@ -319,6 +329,7 @@ class FlaxUNetMidBlock2DCrossAttn(nn.Module):
319329
num_layers: int = 1
320330
attn_num_head_channels: int = 1
321331
use_linear_projection: bool = False
332+
use_memory_efficient_attention: bool = False
322333
dtype: jnp.dtype = jnp.float32
323334

324335
def setup(self):
@@ -341,6 +352,7 @@ def setup(self):
341352
d_head=self.in_channels // self.attn_num_head_channels,
342353
depth=1,
343354
use_linear_projection=self.use_linear_projection,
355+
use_memory_efficient_attention=self.use_memory_efficient_attention,
344356
dtype=self.dtype,
345357
)
346358
attentions.append(attn_block)

‎src/diffusers/models/unet_2d_condition_flax.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ class FlaxUNet2DConditionModel(nn.Module, FlaxModelMixin, ConfigMixin):
8888
flip_sin_to_cos (`bool`, *optional*, defaults to `True`):
8989
Whether to flip the sin to cos in the time embedding.
9090
freq_shift (`int`, *optional*, defaults to 0): The frequency shift to apply to the time embedding.
91+
use_memory_efficient_attention (`bool`, *optional*, defaults to `False`):
92+
enable memory efficient attention https://arxiv.org/abs/2112.05682
9193
9294
"""
9395

@@ -111,6 +113,7 @@ class FlaxUNet2DConditionModel(nn.Module, FlaxModelMixin, ConfigMixin):
111113
dtype: jnp.dtype = jnp.float32
112114
flip_sin_to_cos: bool = True
113115
freq_shift: int = 0
116+
use_memory_efficient_attention: bool = False
114117

115118
def init_weights(self, rng: jax.random.KeyArray) -> FrozenDict:
116119
# init input tensors
@@ -169,6 +172,7 @@ def setup(self):
169172
add_downsample=not is_final_block,
170173
use_linear_projection=self.use_linear_projection,
171174
only_cross_attention=only_cross_attention[i],
175+
use_memory_efficient_attention=self.use_memory_efficient_attention,
172176
dtype=self.dtype,
173177
)
174178
else:
@@ -190,6 +194,7 @@ def setup(self):
190194
dropout=self.dropout,
191195
attn_num_head_channels=attention_head_dim[-1],
192196
use_linear_projection=self.use_linear_projection,
197+
use_memory_efficient_attention=self.use_memory_efficient_attention,
193198
dtype=self.dtype,
194199
)
195200

@@ -217,6 +222,7 @@ def setup(self):
217222
dropout=self.dropout,
218223
use_linear_projection=self.use_linear_projection,
219224
only_cross_attention=only_cross_attention[i],
225+
use_memory_efficient_attention=self.use_memory_efficient_attention,
220226
dtype=self.dtype,
221227
)
222228
else:

‎src/diffusers/pipelines/pipeline_flax_utils.py‎

Lines changed: 7 additions & 1 deletion

0 commit comments

Comments
 (0)