forked from Sygil-Dev/muse-maskgit-pytorch
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Sygil-Dev#36 from Sygil-Dev/dev
Merge dev to main.
- Loading branch information
Showing
20 changed files
with
777 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
github: [ZeroCool940711] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from .attention import CrossAttention, MemoryEfficientCrossAttention | ||
from .mlp import SwiGLU, SwiGLUFFN, SwiGLUFFNFused | ||
|
||
__all__ = [ | ||
"SwiGLU", | ||
"SwiGLUFFN", | ||
"SwiGLUFFNFused", | ||
"CrossAttention", | ||
"MemoryEfficientCrossAttention", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
from inspect import isfunction | ||
from typing import Any, Callable, Optional | ||
|
||
from einops import rearrange | ||
from torch import nn | ||
|
||
try: | ||
from xformers.ops import memory_efficient_attention | ||
except ImportError: | ||
memory_efficient_attention = None | ||
|
||
|
||
def exists(x): | ||
return x is not None | ||
|
||
|
||
def default(val, d): | ||
if exists(val): | ||
return val | ||
return d() if isfunction(d) else d | ||
|
||
|
||
class CrossAttention(nn.Module): | ||
def __init__( | ||
self, | ||
query_dim, | ||
context_dim=None, | ||
heads=8, | ||
dim_head=64, | ||
dropout=0.0, | ||
): | ||
super().__init__() | ||
inner_dim = dim_head * heads | ||
context_dim = default(context_dim, query_dim) | ||
|
||
self.scale = dim_head**-0.5 | ||
self.heads = heads | ||
|
||
self.to_q = nn.Linear(query_dim, inner_dim, bias=False) | ||
self.to_k = nn.Linear(context_dim, inner_dim, bias=False) | ||
self.to_v = nn.Linear(context_dim, inner_dim, bias=False) | ||
|
||
self.to_out = nn.Sequential(nn.Linear(inner_dim, query_dim), nn.Dropout(dropout)) | ||
|
||
def forward(self, x, context=None): | ||
h = self.heads | ||
|
||
q = self.to_q(x) | ||
context = default(context, x) | ||
k = self.to_k(context) | ||
v = self.to_v(context) | ||
|
||
q, k, v = map(lambda t: rearrange(t, "b n (h d) -> (b h) n d", h=h), (q, k, v)) | ||
q = q * self.scale | ||
|
||
sim = q @ k.transpose(-2, -1) | ||
sim = sim.softmax(dim=-1) | ||
|
||
out = sim @ v | ||
out = rearrange(out, "(b h) n d -> b n (h d)", h=h) | ||
return self.to_out(out) | ||
|
||
|
||
class MemoryEfficientCrossAttention(nn.Module): | ||
# https://github.com/MatthieuTPHR/diffusers/blob/d80b531ff8060ec1ea982b65a1b8df70f73aa67c/src/diffusers/models/attention.py#L223 | ||
def __init__( | ||
self, | ||
query_dim, | ||
context_dim=None, | ||
heads=8, | ||
dim_head=64, | ||
dropout=0.0, | ||
): | ||
super().__init__() | ||
inner_dim = dim_head * heads | ||
context_dim = default(context_dim, query_dim) | ||
|
||
self.heads = heads | ||
self.dim_head = dim_head | ||
|
||
self.to_q = nn.Linear(query_dim, inner_dim, bias=False) | ||
self.to_k = nn.Linear(context_dim, inner_dim, bias=False) | ||
self.to_v = nn.Linear(context_dim, inner_dim, bias=False) | ||
|
||
self.to_out = nn.Sequential(nn.Linear(inner_dim, query_dim), nn.Dropout(dropout)) | ||
self.attention_op: Optional[Callable] = None | ||
|
||
def forward(self, x, context=None): | ||
q = self.to_q(x) | ||
context = default(context, x) | ||
k = self.to_k(context) | ||
v = self.to_v(context) | ||
|
||
b, _, _ = q.shape | ||
q, k, v = map( | ||
lambda t: t.unsqueeze(3) | ||
.reshape(b, t.shape[1], self.heads, self.dim_head) | ||
.permute(0, 2, 1, 3) | ||
.reshape(b * self.heads, t.shape[1], self.dim_head) | ||
.contiguous(), | ||
(q, k, v), | ||
) | ||
|
||
out = memory_efficient_attention(q, k, v, attn_bias=None, op=self.attention_op) | ||
|
||
out = ( | ||
out.unsqueeze(0) | ||
.reshape(b, self.heads, out.shape[1], self.dim_head) | ||
.permute(0, 2, 1, 3) | ||
.reshape(b, out.shape[1], self.heads * self.dim_head) | ||
) | ||
return self.to_out(out) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
from typing import Optional | ||
|
||
import torch.nn.functional as F | ||
from torch import Tensor, nn | ||
|
||
|
||
class SwiGLUFFN(nn.Module): | ||
def __init__( | ||
self, | ||
in_features: int, | ||
hidden_features: Optional[int] = None, | ||
out_features: Optional[int] = None, | ||
bias: bool = True, | ||
) -> None: | ||
super().__init__() | ||
out_features = out_features or in_features | ||
hidden_features = hidden_features or in_features | ||
self.w12 = nn.Linear(in_features, 2 * hidden_features, bias=bias) | ||
self.w3 = nn.Linear(hidden_features, out_features, bias=bias) | ||
|
||
def forward(self, x: Tensor) -> Tensor: | ||
x12 = self.w12(x) | ||
x1, x2 = x12.chunk(2, dim=-1) | ||
hidden = F.silu(x1) * x2 | ||
return self.w3(hidden) | ||
|
||
|
||
try: | ||
from xformers.ops import SwiGLU | ||
except ImportError: | ||
SwiGLU = SwiGLUFFN | ||
|
||
|
||
class SwiGLUFFNFused(SwiGLU): | ||
def __init__( | ||
self, | ||
in_features: int, | ||
hidden_features: Optional[int] = None, | ||
out_features: Optional[int] = None, | ||
bias: bool = True, | ||
) -> None: | ||
out_features = out_features or in_features | ||
hidden_features = hidden_features or in_features | ||
hidden_features = (int(hidden_features * 2 / 3) + 7) // 8 * 8 | ||
super().__init__( | ||
in_features=in_features, | ||
hidden_features=hidden_features, | ||
out_features=out_features, | ||
bias=bias, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from .config import VQVAEConfig | ||
from .vqvae import VQVAE | ||
|
||
__all__ = [ | ||
"VQVAE", | ||
"VQVAEConfig", | ||
] |
Oops, something went wrong.