Skip to content

Commit 38a7917

Browse files
committed
Add NNXCombinedTimestepGuidanceTextProjEmbeddings class
1 parent 66b0105 commit 38a7917

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

src/maxdiffusion/models/embeddings_flax.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,80 @@ def __call__(self, timestep, guidance, pooled_projection=None):
535535
return conditioning
536536

537537

538+
class NNXCombinedTimestepGuidanceTextProjEmbeddings(nnx.Module):
539+
540+
def __init__(
541+
self,
542+
rngs: nnx.Rngs,
543+
embedding_dim: int,
544+
pooled_projection_dim: int = 0,
545+
guidance_embeds: bool = True,
546+
frequency_embedding_size: int = 256,
547+
dtype: jnp.dtype = jnp.float32,
548+
weights_dtype: jnp.dtype = jnp.float32,
549+
precision: Optional[jax.lax.Precision] = None,
550+
):
551+
self.embedding_dim = embedding_dim
552+
self.pooled_projection_dim = pooled_projection_dim
553+
self.guidance_embeds = guidance_embeds
554+
self.frequency_embedding_size = frequency_embedding_size
555+
self.dtype = dtype
556+
557+
self.time_proj = NNXTimesteps(num_channels=frequency_embedding_size, flip_sin_to_cos=True, downscale_freq_shift=0)
558+
self.timestep_embedder = NNXTimestepEmbedding(
559+
rngs=rngs,
560+
in_channels=frequency_embedding_size,
561+
time_embed_dim=embedding_dim,
562+
dtype=dtype,
563+
weights_dtype=weights_dtype,
564+
)
565+
566+
if guidance_embeds:
567+
self.guidance_proj = NNXTimesteps(num_channels=frequency_embedding_size, flip_sin_to_cos=True, downscale_freq_shift=0)
568+
self.guidance_embedder = NNXTimestepEmbedding(
569+
rngs=rngs,
570+
in_channels=frequency_embedding_size,
571+
time_embed_dim=embedding_dim,
572+
dtype=dtype,
573+
weights_dtype=weights_dtype,
574+
)
575+
576+
if pooled_projection_dim > 0:
577+
self.pooled_embedder = NNXPixArtAlphaTextProjection(
578+
rngs=rngs,
579+
in_features=pooled_projection_dim,
580+
out_features=embedding_dim,
581+
act_fn="silu",
582+
dtype=dtype,
583+
weights_dtype=weights_dtype,
584+
)
585+
586+
def __call__(
587+
self,
588+
timestep: jax.Array,
589+
guidance: Optional[jax.Array] = None,
590+
pooled_projection: Optional[jax.Array] = None,
591+
) -> jax.Array:
592+
timesteps_proj = self.time_proj(timestep)
593+
dtype = pooled_projection.dtype if pooled_projection is not None else jnp.float32
594+
timestep_emb = self.timestep_embedder(timesteps_proj.astype(dtype))
595+
596+
if self.guidance_embeds and guidance is not None:
597+
guidance_proj = self.guidance_proj(guidance)
598+
guidance_emb = self.guidance_embedder(guidance_proj.astype(dtype))
599+
time_guidance_emb = timestep_emb + guidance_emb
600+
else:
601+
time_guidance_emb = timestep_emb
602+
603+
if pooled_projection is not None and self.pooled_projection_dim > 0:
604+
pooled_projections = self.pooled_embedder(pooled_projection)
605+
conditioning = time_guidance_emb + pooled_projections
606+
else:
607+
conditioning = time_guidance_emb
608+
609+
return conditioning
610+
611+
538612
class NNXTimesteps(nnx.Module):
539613

540614
def __init__(self, num_channels: int, flip_sin_to_cos: bool, downscale_freq_shift: float, scale: int = 1):

0 commit comments

Comments
 (0)