Harley-ml commited on
Commit
637b87f
·
verified ·
1 Parent(s): 18d3148

Upload 3 files

Browse files
Files changed (1) hide show
  1. 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
- return super().from_pretrained(pretrained_model_name_or_path, *model_args, config=config, **kwargs)
 
 
 
 
 
 
 
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