MCPcopy Create free account
hub / github.com/Monalissaa/DisenDiff / IterativeEmbedding

Class IterativeEmbedding

src/iterative_embedding.py:3–50  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1from diffusers.models.embeddings import TimestepEmbedding, Timesteps
2import torch
3class IterativeEmbedding(torch.nn.Module):
4 def __init__(self, size, newtoken=1):
5 super().__init__()
6
7 timestep_input_dim = 768
8 time_embed_dim = 2048
9 flip_sin_to_cos = True
10 freq_shift = 0
11 self.time_proj = Timesteps(timestep_input_dim, flip_sin_to_cos, freq_shift)
12 # timestep_input_dim = block_out_channels[0]
13 self.time_embedding = TimestepEmbedding(
14 timestep_input_dim,
15 time_embed_dim,
16 act_fn="silu",
17 out_dim=timestep_input_dim,
18 post_act_fn=None,
19 cond_proj_dim=None,
20 )
21 # self.time_emb_proj = torch.nn.Linear(time_embed_dim, timestep_input_dim)
22
23 # embed_dim = 768
24
25 # self.iterative_embedding = torch.nn.Parameter(torch.randn(768), requires_grad=True)
26 self.expand_embeddings = torch.nn.Embedding(size, 768)
27 self.expand_embeddings.weight.data.zero_()
28 self.expand_embeddings.weight.data[-newtoken:] = torch.ones(768)
29 self.expand_embeddings.weight.requires_grad = False
30
31
32 def forward(
33 self,
34 input_ids,
35 timesteps,
36 ) -> torch.Tensor:
37 intput_shape = input_ids.size()
38
39 timesteps = timesteps.expand(intput_shape[0])
40 t_emb = self.time_proj(timesteps)
41
42
43 # emb = self.time_embedding(t_emb)[:, :, None]
44 emb = self.time_embedding(t_emb)
45 # emb = emb.repeat(intput_shape[1]).view(intput_shape[0], intput_shape[1], -1)
46 emb = emb.repeat(1, 77, 1).view(intput_shape[0], intput_shape[1], -1)
47 # print(offset_embedding.shape)
48 position_embedding = self.expand_embeddings(input_ids)
49 embedding = emb * position_embedding
50 return embedding

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected