forked from kohya-ss/sd-scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for EDM2 timestep weighting network
- Loading branch information
Showing
3 changed files
with
139 additions
and
0 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,110 @@ | ||
# adapted from https://github.com/NVlabs/edm2/blob/3a6682d3d25395df64863d3cea563bf3f3380769/training/networks_edm2.py | ||
|
||
import torch | ||
import numpy as np | ||
import os | ||
from safetensors.torch import load_file | ||
|
||
#---------------------------------------------------------------------------- | ||
# Normalize given tensor to unit magnitude with respect to the given | ||
# dimensions. Default = all dimensions except the first. | ||
|
||
def normalize(x, dim=None, eps=1e-4): | ||
if dim is None: | ||
dim = list(range(1, x.ndim)) | ||
norm = torch.linalg.vector_norm(x, dim=dim, keepdim=True, dtype=torch.float32) | ||
norm = torch.add(eps, norm, alpha=np.sqrt(norm.numel() / x.numel())) | ||
return x / norm.to(x.dtype) | ||
|
||
|
||
class MPFourier(torch.nn.Module): | ||
def __init__(self, num_channels, bandwidth=1): | ||
super().__init__() | ||
self.register_buffer('freqs', 2 * np.pi * torch.randn(num_channels) * bandwidth) | ||
self.register_buffer('phases', 2 * np.pi * torch.rand(num_channels)) | ||
|
||
def forward(self, x): | ||
y = x.to(torch.float32) | ||
y = y.ger(self.freqs.to(torch.float32)) | ||
y = y + self.phases.to(torch.float32) | ||
y = y.cos() * np.sqrt(2) | ||
return y.to(x.dtype) | ||
|
||
class MPConv(torch.nn.Module): | ||
def __init__(self, in_channels, out_channels, kernel): | ||
super().__init__() | ||
self.out_channels = out_channels | ||
self.weight = torch.nn.Parameter(torch.randn(out_channels, in_channels, *kernel)) | ||
|
||
def forward(self, x, gain=1): | ||
w = self.weight.to(torch.float32) | ||
if self.training: | ||
with torch.no_grad(): | ||
self.weight.copy_(normalize(w)) # forced weight normalization | ||
w = normalize(w) # traditional weight normalization | ||
w = w * (gain / np.sqrt(w[0].numel())) # magnitude-preserving scaling | ||
w = w.to(x.dtype) | ||
if w.ndim == 2: | ||
return x @ w.t() | ||
assert w.ndim == 4 | ||
return torch.nn.functional.conv2d(x, w, padding=(w.shape[-1]//2,)) | ||
|
||
class TimestepUncertaintyLossNetwork(torch.nn.Module): | ||
def __init__(self, | ||
logvar_channels = 128, # Intermediate dimensionality for uncertainty estimation. | ||
): | ||
super().__init__() | ||
self.logvar_fourier = MPFourier(logvar_channels) | ||
self.logvar_linear = MPConv(logvar_channels, 1, kernel=[]) | ||
|
||
def forward(self, sigma): | ||
c_noise = sigma.reshape(-1, 1, 1, 1).flatten().log() / 4 | ||
logvar = self.logvar_linear(self.logvar_fourier(c_noise)).reshape(-1, 1, 1, 1) | ||
return logvar | ||
|
||
def loss(self, sigma, loss): | ||
logvar = self.forward(sigma) | ||
return loss / logvar.exp() + logvar | ||
|
||
def load_weights(self, file, dtype=None): | ||
if not os.path.exists(file): | ||
print(f"WARNING: Could not load weights from '{file}' because the file does not exist.") | ||
return | ||
|
||
if os.path.splitext(file)[1] == ".safetensors": | ||
from safetensors.torch import load_file | ||
state_dict = load_file(file) | ||
else: | ||
state_dict = torch.load(file) | ||
|
||
if dtype is not None: | ||
for key in list(state_dict.keys()): | ||
v = state_dict[key] | ||
v = v.detach().clone().to(dtype) | ||
state_dict[key] = v | ||
|
||
self.load_state_dict(state_dict) | ||
|
||
def save_weights(self, file, dtype=torch.float32, metadata={}): | ||
metadata = {} | ||
|
||
state_dict = self.state_dict() | ||
|
||
if dtype is not None: | ||
for key in list(state_dict.keys()): | ||
v = state_dict[key] | ||
v = v.detach().clone().to("cpu").to(dtype) | ||
state_dict[key] = v | ||
|
||
if os.path.splitext(file)[1] == ".safetensors": | ||
from safetensors.torch import save_file | ||
from library import train_util | ||
|
||
# Precalculate model hashes to save time on indexing | ||
model_hash, legacy_hash = train_util.precalculate_safetensors_hashes(state_dict, metadata) | ||
metadata["sshs_model_hash"] = model_hash | ||
metadata["sshs_legacy_hash"] = legacy_hash | ||
|
||
save_file(state_dict, file, metadata) | ||
else: | ||
torch.save(state_dict, file) |
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