Text-to-Image
Transformers
PyTorch
ONNX
ezfgraphic
feature-extraction
transformer
custom_code
EZFGraphic
Instructions to use OpenRussianAI/EZFGraphic with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use OpenRussianAI/EZFGraphic with Transformers:
# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("OpenRussianAI/EZFGraphic", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Fix: all_tied_weights_keys
Browse files- modeling_ezfgraphic.py +93 -91
modeling_ezfgraphic.py
CHANGED
|
@@ -1,91 +1,93 @@
|
|
| 1 |
-
from .configuration_ezfgraphic import EZFGraphicConfig
|
| 2 |
-
|
| 3 |
-
import torch, torch.nn as nn, torch.nn.functional as F
|
| 4 |
-
from transformers import PreTrainedModel
|
| 5 |
-
|
| 6 |
-
class RMSNorm(nn.Module):
|
| 7 |
-
def __init__(self, d, eps=1e-5):
|
| 8 |
-
super().__init__()
|
| 9 |
-
self.w = nn.Parameter(torch.ones(d)); self.eps = eps
|
| 10 |
-
def forward(self, x):
|
| 11 |
-
return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps) * self.w
|
| 12 |
-
|
| 13 |
-
class Attention(nn.Module):
|
| 14 |
-
def __init__(self, cfg):
|
| 15 |
-
super().__init__()
|
| 16 |
-
self.qkv = nn.Linear(cfg.d_model, 3 * cfg.d_model, bias=False)
|
| 17 |
-
self.proj = nn.Linear(cfg.d_model, cfg.d_model, bias=False)
|
| 18 |
-
self.drop = nn.Dropout(cfg.dropout)
|
| 19 |
-
self.register_buffer("mask", torch.tril(torch.ones(512, 512)).bool())
|
| 20 |
-
def forward(self, x):
|
| 21 |
-
B, T, C = x.shape
|
| 22 |
-
q, k, v = self.qkv(x).chunk(3, dim=-1)
|
| 23 |
-
att = (q @ k.transpose(-2, -1)) / (C ** 0.5)
|
| 24 |
-
att = att.masked_fill(~self.mask[:T, :T], float("-inf"))
|
| 25 |
-
att = self.drop(F.softmax(att, dim=-1))
|
| 26 |
-
return self.proj(att @ v)
|
| 27 |
-
|
| 28 |
-
class FeedForward(nn.Module):
|
| 29 |
-
def __init__(self, cfg):
|
| 30 |
-
super().__init__()
|
| 31 |
-
self.fc1 = nn.Linear(cfg.d_model, 2 * cfg.d_model)
|
| 32 |
-
self.fc2 = nn.Linear(2 * cfg.d_model, cfg.d_model)
|
| 33 |
-
self.drop = nn.Dropout(cfg.dropout)
|
| 34 |
-
def forward(self, x):
|
| 35 |
-
return self.drop(self.fc2(F.relu(self.fc1(x))))
|
| 36 |
-
|
| 37 |
-
class Block(nn.Module):
|
| 38 |
-
def __init__(self, cfg):
|
| 39 |
-
super().__init__()
|
| 40 |
-
self.ln1 = RMSNorm(cfg.d_model); self.attn = Attention(cfg)
|
| 41 |
-
self.ln2 = RMSNorm(cfg.d_model); self.ffn = FeedForward(cfg)
|
| 42 |
-
def forward(self, x):
|
| 43 |
-
x = x + self.attn(self.ln1(x))
|
| 44 |
-
x = x + self.ffn(self.ln2(x))
|
| 45 |
-
return x
|
| 46 |
-
|
| 47 |
-
class EZFGraphic(PreTrainedModel):
|
| 48 |
-
config_class = EZFGraphicConfig
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
self.
|
| 55 |
-
|
| 56 |
-
self.
|
| 57 |
-
|
| 58 |
-
self.
|
| 59 |
-
self.
|
| 60 |
-
self.
|
| 61 |
-
self.
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
nn.Linear(
|
| 65 |
-
nn.ReLU(),
|
| 66 |
-
nn.Linear(
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
from .configuration_ezfgraphic import EZFGraphicConfig
|
| 2 |
+
|
| 3 |
+
import torch, torch.nn as nn, torch.nn.functional as F
|
| 4 |
+
from transformers import PreTrainedModel
|
| 5 |
+
|
| 6 |
+
class RMSNorm(nn.Module):
|
| 7 |
+
def __init__(self, d, eps=1e-5):
|
| 8 |
+
super().__init__()
|
| 9 |
+
self.w = nn.Parameter(torch.ones(d)); self.eps = eps
|
| 10 |
+
def forward(self, x):
|
| 11 |
+
return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps) * self.w
|
| 12 |
+
|
| 13 |
+
class Attention(nn.Module):
|
| 14 |
+
def __init__(self, cfg):
|
| 15 |
+
super().__init__()
|
| 16 |
+
self.qkv = nn.Linear(cfg.d_model, 3 * cfg.d_model, bias=False)
|
| 17 |
+
self.proj = nn.Linear(cfg.d_model, cfg.d_model, bias=False)
|
| 18 |
+
self.drop = nn.Dropout(cfg.dropout)
|
| 19 |
+
self.register_buffer("mask", torch.tril(torch.ones(512, 512)).bool())
|
| 20 |
+
def forward(self, x):
|
| 21 |
+
B, T, C = x.shape
|
| 22 |
+
q, k, v = self.qkv(x).chunk(3, dim=-1)
|
| 23 |
+
att = (q @ k.transpose(-2, -1)) / (C ** 0.5)
|
| 24 |
+
att = att.masked_fill(~self.mask[:T, :T], float("-inf"))
|
| 25 |
+
att = self.drop(F.softmax(att, dim=-1))
|
| 26 |
+
return self.proj(att @ v)
|
| 27 |
+
|
| 28 |
+
class FeedForward(nn.Module):
|
| 29 |
+
def __init__(self, cfg):
|
| 30 |
+
super().__init__()
|
| 31 |
+
self.fc1 = nn.Linear(cfg.d_model, 2 * cfg.d_model)
|
| 32 |
+
self.fc2 = nn.Linear(2 * cfg.d_model, cfg.d_model)
|
| 33 |
+
self.drop = nn.Dropout(cfg.dropout)
|
| 34 |
+
def forward(self, x):
|
| 35 |
+
return self.drop(self.fc2(F.relu(self.fc1(x))))
|
| 36 |
+
|
| 37 |
+
class Block(nn.Module):
|
| 38 |
+
def __init__(self, cfg):
|
| 39 |
+
super().__init__()
|
| 40 |
+
self.ln1 = RMSNorm(cfg.d_model); self.attn = Attention(cfg)
|
| 41 |
+
self.ln2 = RMSNorm(cfg.d_model); self.ffn = FeedForward(cfg)
|
| 42 |
+
def forward(self, x):
|
| 43 |
+
x = x + self.attn(self.ln1(x))
|
| 44 |
+
x = x + self.ffn(self.ln2(x))
|
| 45 |
+
return x
|
| 46 |
+
|
| 47 |
+
class EZFGraphic(PreTrainedModel):
|
| 48 |
+
config_class = EZFGraphicConfig
|
| 49 |
+
_tied_weights_keys = []
|
| 50 |
+
all_tied_weights_keys = {}
|
| 51 |
+
def __init__(self, config):
|
| 52 |
+
super().__init__(config)
|
| 53 |
+
cfg = config
|
| 54 |
+
self.cfg = cfg
|
| 55 |
+
self.text_emb = nn.Embedding(cfg.vocab_size, cfg.d_model)
|
| 56 |
+
self.pos_emb_text = nn.Embedding(cfg.ctx, cfg.d_model)
|
| 57 |
+
n_patches = (cfg.image_size // cfg.patch_size) ** 2
|
| 58 |
+
self.n_patches = n_patches
|
| 59 |
+
self.pos_emb_visual = nn.Embedding(n_patches, cfg.d_model)
|
| 60 |
+
self.blocks = nn.ModuleList([Block(cfg) for _ in range(cfg.n_layer)])
|
| 61 |
+
self.ln_f = RMSNorm(cfg.d_model)
|
| 62 |
+
self.patch_pixels = cfg.patch_size * cfg.patch_size * 3
|
| 63 |
+
self.pixel_decoder = nn.Sequential(
|
| 64 |
+
nn.Linear(cfg.d_model, 512),
|
| 65 |
+
nn.ReLU(),
|
| 66 |
+
nn.Linear(512, 1024),
|
| 67 |
+
nn.ReLU(),
|
| 68 |
+
nn.Linear(1024, self.patch_pixels),
|
| 69 |
+
)
|
| 70 |
+
def encode_text(self, text_ids):
|
| 71 |
+
B, T = text_ids.shape
|
| 72 |
+
pos = torch.arange(T, device=text_ids.device)
|
| 73 |
+
x = self.text_emb(text_ids) + self.pos_emb_text(pos)
|
| 74 |
+
for b in self.blocks:
|
| 75 |
+
x = b(x)
|
| 76 |
+
return self.ln_f(x)
|
| 77 |
+
def decode_pixels(self, x):
|
| 78 |
+
B, T, _ = x.shape
|
| 79 |
+
patches = self.pixel_decoder(x)
|
| 80 |
+
ps = self.cfg.patch_size
|
| 81 |
+
h = w = self.cfg.image_size // ps
|
| 82 |
+
patches = patches.view(B, h, w, ps, ps, 3)
|
| 83 |
+
pixels = patches.permute(0, 5, 1, 3, 2, 4).reshape(B, 3, self.cfg.image_size, self.cfg.image_size)
|
| 84 |
+
return pixels
|
| 85 |
+
def forward(self, text_ids):
|
| 86 |
+
x = self.encode_text(text_ids)
|
| 87 |
+
last = x[:, -1:, :]
|
| 88 |
+
visual_input = last.expand(-1, self.n_patches, -1).contiguous()
|
| 89 |
+
visual_input = visual_input + self.pos_emb_visual(
|
| 90 |
+
torch.arange(self.n_patches, device=text_ids.device)
|
| 91 |
+
).unsqueeze(0)
|
| 92 |
+
pixels = self.decode_pixels(visual_input)
|
| 93 |
+
return pixels
|