MCPcopy Create free account
hub / github.com/huggingface/diffusers / GaussianFourierProjection

Class GaussianFourierProjection

src/diffusers/models/embeddings.py:1328–1356  ·  view source on GitHub ↗

Gaussian Fourier embeddings for noise levels.

Source from the content-addressed store, hash-verified

1326
1327
1328class GaussianFourierProjection(nn.Module):
1329 """Gaussian Fourier embeddings for noise levels."""
1330
1331 def __init__(
1332 self, embedding_size: int = 256, scale: float = 1.0, set_W_to_weight=True, log=True, flip_sin_to_cos=False
1333 ):
1334 super().__init__()
1335 self.weight = nn.Parameter(torch.randn(embedding_size) * scale, requires_grad=False)
1336 self.log = log
1337 self.flip_sin_to_cos = flip_sin_to_cos
1338
1339 if set_W_to_weight:
1340 # to delete later
1341 del self.weight
1342 self.W = nn.Parameter(torch.randn(embedding_size) * scale, requires_grad=False)
1343 self.weight = self.W
1344 del self.W
1345
1346 def forward(self, x):
1347 if self.log:
1348 x = torch.log(x)
1349
1350 x_proj = x[:, None] * self.weight[None, :] * 2 * np.pi
1351
1352 if self.flip_sin_to_cos:
1353 out = torch.cat([torch.cos(x_proj), torch.sin(x_proj)], dim=-1)
1354 else:
1355 out = torch.cat([torch.sin(x_proj), torch.cos(x_proj)], dim=-1)
1356 return out
1357
1358
1359class SinusoidalPositionalEmbedding(nn.Module):

Callers 5

_set_time_projMethod · 0.90
__init__Method · 0.85
__init__Method · 0.85
_set_time_projMethod · 0.85
__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…