Spaces:
Running on Zero
Running on Zero
Download src/core/network/nn/utils.py from ceccurr/EquiFusion: direct link, hf CLI and curl.
- Browser
- Download file 832 Bytes
-
https://huggingface.co/spaces/ceccurr/EquiFusion/resolve/main/src/core/network/nn/utils.py
- Command line
-
hf download hf://spaces/ceccurr/EquiFusion/src/core/network/nn/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/ceccurr/EquiFusion/resolve/main/src/core/network/nn/utils.py
832 Bytes
| import torch | |
| import math | |
| from torch import nn, einsum, Tensor | |
| from torch.nn import Module, ModuleList | |
| import torch.nn.functional as F | |
| from einops import rearrange, reduce | |
| from einops.layers.torch import Rearrange | |
| # from https://github.com/lucidrains/denoising-diffusion-pytorch/blob/main/denoising_diffusion_pytorch/denoising_diffusion_pytorch_1d.py | |
| class SinusoidalPosEmb(Module): | |
| def __init__(self, dim, theta=10000): | |
| super().__init__() | |
| self.dim = dim | |
| self.theta = theta | |
| def forward(self, x): | |
| device = x.device | |
| half_dim = self.dim // 2 | |
| emb = math.log(self.theta) / (half_dim - 1) | |
| emb = torch.exp(torch.arange(half_dim, device=device) * -emb) | |
| emb = x[:, None] * emb[None, :] | |
| emb = torch.cat((emb.sin(), emb.cos()), dim=-1) | |
| return emb | |