Unconditional Image Generation
Transformers
Safetensors
tinyimagegen
feature-extraction
imagegen
unconditional-image
custom_code
Instructions to use fromziro/TinyImageGen-0.6M with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use fromziro/TinyImageGen-0.6M with Transformers:
# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("fromziro/TinyImageGen-0.6M", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload 3 files
Browse files- modeling_tinyimagegen.py +22 -1
modeling_tinyimagegen.py
CHANGED
|
@@ -81,6 +81,10 @@ class RotaryEmbedding2D(nn.Module):
|
|
| 81 |
self.register_buffer("inv_freq_w", inv_freq_w, persistent=False)
|
| 82 |
|
| 83 |
def forward(self, grid_h: int, grid_w: int, device: torch.device, dtype: torch.dtype = torch.float32):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
t_h = torch.arange(grid_h, device=device, dtype=torch.float32)
|
| 85 |
t_w = torch.arange(grid_w, device=device, dtype=torch.float32)
|
| 86 |
|
|
@@ -129,6 +133,9 @@ class HadamardMLP(nn.Module):
|
|
| 129 |
self.register_buffer("hadamard_mat", hadamard_mat, persistent=False)
|
| 130 |
|
| 131 |
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
|
|
|
|
|
|
|
|
| 132 |
mat = self.hadamard_mat.type_as(x)
|
| 133 |
h = (x * self.scale1) @ mat
|
| 134 |
g = F.silu(x * self.gate)
|
|
@@ -295,7 +302,14 @@ class TinyImageGenPreTrainedModel(PreTrainedModel):
|
|
| 295 |
elif os.path.exists(bin_file):
|
| 296 |
state_dict = torch.load(bin_file, map_location="cpu")
|
| 297 |
else:
|
| 298 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
|
| 300 |
model_keys = set(model.state_dict().keys())
|
| 301 |
st_keys = set(state_dict.keys())
|
|
@@ -307,6 +321,13 @@ class TinyImageGenPreTrainedModel(PreTrainedModel):
|
|
| 307 |
|
| 308 |
model.load_state_dict(state_dict, strict=True)
|
| 309 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
if torch_dtype is not None:
|
| 311 |
model.to(dtype=torch_dtype)
|
| 312 |
|
|
|
|
| 81 |
self.register_buffer("inv_freq_w", inv_freq_w, persistent=False)
|
| 82 |
|
| 83 |
def forward(self, grid_h: int, grid_w: int, device: torch.device, dtype: torch.dtype = torch.float32):
|
| 84 |
+
if self.inv_freq_h is None or self.inv_freq_h.device.type == "meta" or (self.inv_freq_h == 0).all():
|
| 85 |
+
self.inv_freq_h = 1.0 / (self.base ** (torch.arange(0, self.dim_h, 2, dtype=torch.float32, device=device) / self.dim_h))
|
| 86 |
+
self.inv_freq_w = 1.0 / (self.base ** (torch.arange(0, self.dim_w, 2, dtype=torch.float32, device=device) / self.dim_w))
|
| 87 |
+
|
| 88 |
t_h = torch.arange(grid_h, device=device, dtype=torch.float32)
|
| 89 |
t_w = torch.arange(grid_w, device=device, dtype=torch.float32)
|
| 90 |
|
|
|
|
| 133 |
self.register_buffer("hadamard_mat", hadamard_mat, persistent=False)
|
| 134 |
|
| 135 |
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 136 |
+
if not hasattr(self, "hadamard_mat") or self.hadamard_mat is None or self.hadamard_mat.device.type == "meta" or (self.hadamard_mat == 0).all() or self.hadamard_mat.abs().max() > 10.0:
|
| 137 |
+
hadamard_mat = get_hadamard_matrix(self.dim, dtype=torch.float32).to(x.device)
|
| 138 |
+
self.register_buffer("hadamard_mat", hadamard_mat, persistent=False)
|
| 139 |
mat = self.hadamard_mat.type_as(x)
|
| 140 |
h = (x * self.scale1) @ mat
|
| 141 |
g = F.silu(x * self.gate)
|
|
|
|
| 302 |
elif os.path.exists(bin_file):
|
| 303 |
state_dict = torch.load(bin_file, map_location="cpu")
|
| 304 |
else:
|
| 305 |
+
model = super().from_pretrained(pretrained_model_name_or_path, *model_args, config=config, **kwargs)
|
| 306 |
+
for module in model.modules():
|
| 307 |
+
if type(module).__name__ == "RotaryEmbedding2D":
|
| 308 |
+
module.inv_freq_h = 1.0 / (module.base ** (torch.arange(0, module.dim_h, 2, dtype=torch.float32) / module.dim_h))
|
| 309 |
+
module.inv_freq_w = 1.0 / (module.base ** (torch.arange(0, module.dim_w, 2, dtype=torch.float32) / module.dim_w))
|
| 310 |
+
elif type(module).__name__ == "HadamardMLP":
|
| 311 |
+
module.register_buffer("hadamard_mat", get_hadamard_matrix(module.dim, dtype=torch.float32), persistent=False)
|
| 312 |
+
return model
|
| 313 |
|
| 314 |
model_keys = set(model.state_dict().keys())
|
| 315 |
st_keys = set(state_dict.keys())
|
|
|
|
| 321 |
|
| 322 |
model.load_state_dict(state_dict, strict=True)
|
| 323 |
|
| 324 |
+
for module in model.modules():
|
| 325 |
+
if type(module).__name__ == "RotaryEmbedding2D":
|
| 326 |
+
module.inv_freq_h = 1.0 / (module.base ** (torch.arange(0, module.dim_h, 2, dtype=torch.float32) / module.dim_h))
|
| 327 |
+
module.inv_freq_w = 1.0 / (module.base ** (torch.arange(0, module.dim_w, 2, dtype=torch.float32) / module.dim_w))
|
| 328 |
+
elif type(module).__name__ == "HadamardMLP":
|
| 329 |
+
module.register_buffer("hadamard_mat", get_hadamard_matrix(module.dim, dtype=torch.float32), persistent=False)
|
| 330 |
+
|
| 331 |
if torch_dtype is not None:
|
| 332 |
model.to(dtype=torch_dtype)
|
| 333 |
|