Skip to content

Commit a6a25ce

Browse files
akashgokulkashif
andauthored
Fix Flax flip_sin_to_cos (huggingface#1369)
* Fix Flax flip_sin_to_cos * Adding flip_sin_to_cos Co-authored-by: Kashif Rasul <kashif.rasul@gmail.com>
1 parent b85bb07 commit a6a25ce

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

src/diffusers/models/embeddings_flax.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,11 @@ class FlaxTimesteps(nn.Module):
8484
Time step embedding dimension
8585
"""
8686
dim: int = 32
87+
flip_sin_to_cos: bool = False
8788
freq_shift: float = 1
8889

8990
@nn.compact
9091
def __call__(self, timesteps):
9192
return get_sinusoidal_embeddings(
92-
timesteps, embedding_dim=self.dim, freq_shift=self.freq_shift, flip_sin_to_cos=True
93+
timesteps, embedding_dim=self.dim, flip_sin_to_cos=self.flip_sin_to_cos, freq_shift=self.freq_shift
9394
)

src/diffusers/models/unet_2d_condition_flax.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ class FlaxUNet2DConditionModel(nn.Module, FlaxModelMixin, ConfigMixin):
8585
The dimension of the cross attention features.
8686
dropout (`float`, *optional*, defaults to 0):
8787
Dropout probability for down, up and bottleneck blocks.
88+
flip_sin_to_cos (`bool`, *optional*, defaults to `True`):
89+
Whether to flip the sin to cos in the time embedding.
90+
freq_shift (`int`, *optional*, defaults to 0): The frequency shift to apply to the time embedding.
91+
8892
"""
8993

9094
sample_size: int = 32
@@ -105,6 +109,7 @@ class FlaxUNet2DConditionModel(nn.Module, FlaxModelMixin, ConfigMixin):
105109
dropout: float = 0.0
106110
use_linear_projection: bool = False
107111
dtype: jnp.dtype = jnp.float32
112+
flip_sin_to_cos: bool = True
108113
freq_shift: int = 0
109114

110115
def init_weights(self, rng: jax.random.PRNGKey) -> FrozenDict:
@@ -133,7 +138,9 @@ def setup(self):
133138
)
134139

135140
# time
136-
self.time_proj = FlaxTimesteps(block_out_channels[0], freq_shift=self.config.freq_shift)
141+
self.time_proj = FlaxTimesteps(
142+
block_out_channels[0], flip_sin_to_cos=self.flip_sin_to_cos, freq_shift=self.config.freq_shift
143+
)
137144
self.time_embedding = FlaxTimestepEmbedding(time_embed_dim, dtype=self.dtype)
138145

139146
only_cross_attention = self.only_cross_attention

0 commit comments

Comments
 (0)