Unconditional Image Generation
Diffusers
Safetensors
English
afm
adversarial-flow-models
class-conditional
imagenet
Instructions to use BiliSakura/AFM-diffusers with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use BiliSakura/AFM-diffusers with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("BiliSakura/AFM-diffusers", dtype=torch.bfloat16, device_map="cuda") prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" image = pipe(prompt).images[0] - Notebooks
- Google Colab
- Kaggle
Upload folder using huggingface_hub
Browse files- AFM-B-2-1NFE-guided/generator/config.json +17 -3
- AFM-B-2-1NFE-guided/generator/diffusion_pytorch_model.safetensors +2 -2
- AFM-B-2-1NFE-guided/generator/modeling_afm.py +22 -166
- AFM-B-2-1NFE-noguide/generator/config.json +17 -3
- AFM-B-2-1NFE-noguide/generator/diffusion_pytorch_model.safetensors +2 -2
- AFM-B-2-1NFE-noguide/generator/modeling_afm.py +22 -166
- AFM-L-2-1NFE-guided/generator/config.json +17 -3
- AFM-L-2-1NFE-guided/generator/diffusion_pytorch_model.safetensors +2 -2
- AFM-L-2-1NFE-guided/generator/modeling_afm.py +22 -166
- AFM-L-2-1NFE-noguide/generator/config.json +17 -3
- AFM-L-2-1NFE-noguide/generator/diffusion_pytorch_model.safetensors +2 -2
- AFM-L-2-1NFE-noguide/generator/modeling_afm.py +22 -166
- AFM-M-2-1NFE-guided/generator/config.json +17 -3
- AFM-M-2-1NFE-guided/generator/diffusion_pytorch_model.safetensors +2 -2
- AFM-M-2-1NFE-guided/generator/modeling_afm.py +22 -166
- AFM-M-2-1NFE-noguide/generator/config.json +17 -3
- AFM-M-2-1NFE-noguide/generator/diffusion_pytorch_model.safetensors +2 -2
- AFM-M-2-1NFE-noguide/generator/modeling_afm.py +22 -166
- AFM-XL-2-112layer-1NFE-guided/generator/config.json +17 -3
- AFM-XL-2-112layer-1NFE-guided/generator/diffusion_pytorch_model.safetensors +2 -2
- AFM-XL-2-112layer-1NFE-guided/generator/modeling_afm.py +22 -166
- AFM-XL-2-1NFE-guided/generator/__pycache__/modeling_afm.cpython-312.pyc +0 -0
- AFM-XL-2-1NFE-guided/generator/config.json +17 -3
- AFM-XL-2-1NFE-guided/generator/diffusion_pytorch_model.safetensors +2 -2
- AFM-XL-2-1NFE-guided/generator/modeling_afm.py +22 -166
- AFM-XL-2-1NFE-noguide/generator/config.json +17 -3
- AFM-XL-2-1NFE-noguide/generator/diffusion_pytorch_model.safetensors +2 -2
- AFM-XL-2-1NFE-noguide/generator/modeling_afm.py +22 -166
- AFM-XL-2-2NFE-guided/generator/config.json +17 -3
- AFM-XL-2-2NFE-guided/generator/diffusion_pytorch_model.safetensors +2 -2
- AFM-XL-2-2NFE-guided/generator/modeling_afm.py +22 -166
- AFM-XL-2-2NFE-noguide/generator/config.json +17 -3
- AFM-XL-2-2NFE-noguide/generator/diffusion_pytorch_model.safetensors +2 -2
- AFM-XL-2-2NFE-noguide/generator/modeling_afm.py +22 -166
- AFM-XL-2-4NFE-guided/generator/config.json +17 -3
- AFM-XL-2-4NFE-guided/generator/diffusion_pytorch_model.safetensors +2 -2
- AFM-XL-2-4NFE-guided/generator/modeling_afm.py +22 -166
- AFM-XL-2-56layer-1NFE-guided/generator/config.json +17 -3
- AFM-XL-2-56layer-1NFE-guided/generator/diffusion_pytorch_model.safetensors +2 -2
- AFM-XL-2-56layer-1NFE-guided/generator/modeling_afm.py +22 -166
AFM-B-2-1NFE-guided/generator/config.json
CHANGED
|
@@ -1,6 +1,22 @@
|
|
| 1 |
{
|
| 2 |
-
"_class_name": "
|
| 3 |
"_diffusers_version": "0.36.0",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
"model_type": "AFM-B/2",
|
| 5 |
"architecture": "standard",
|
| 6 |
"repeat": 1,
|
|
@@ -12,9 +28,7 @@
|
|
| 12 |
"class_dropout_prob": 0.0,
|
| 13 |
"input_size": 32,
|
| 14 |
"num_classes": 1000,
|
| 15 |
-
"in_channels": 4,
|
| 16 |
"depth": 12,
|
| 17 |
"hidden_size": 768,
|
| 18 |
-
"patch_size": 2,
|
| 19 |
"num_heads": 12
|
| 20 |
}
|
|
|
|
| 1 |
{
|
| 2 |
+
"_class_name": "DiTTransformer2DModel",
|
| 3 |
"_diffusers_version": "0.36.0",
|
| 4 |
+
"sample_size": 32,
|
| 5 |
+
"num_layers": 12,
|
| 6 |
+
"num_attention_heads": 12,
|
| 7 |
+
"attention_head_dim": 64,
|
| 8 |
+
"in_channels": 4,
|
| 9 |
+
"out_channels": 4,
|
| 10 |
+
"patch_size": 2,
|
| 11 |
+
"attention_bias": true,
|
| 12 |
+
"activation_fn": "gelu-approximate",
|
| 13 |
+
"num_embeds_ada_norm": 1000,
|
| 14 |
+
"norm_type": "ada_norm_zero",
|
| 15 |
+
"norm_elementwise_affine": false,
|
| 16 |
+
"dropout": 0.0,
|
| 17 |
+
"norm_num_groups": 32,
|
| 18 |
+
"norm_eps": 1e-05,
|
| 19 |
+
"upcast_attention": false,
|
| 20 |
"model_type": "AFM-B/2",
|
| 21 |
"architecture": "standard",
|
| 22 |
"repeat": 1,
|
|
|
|
| 28 |
"class_dropout_prob": 0.0,
|
| 29 |
"input_size": 32,
|
| 30 |
"num_classes": 1000,
|
|
|
|
| 31 |
"depth": 12,
|
| 32 |
"hidden_size": 768,
|
|
|
|
| 33 |
"num_heads": 12
|
| 34 |
}
|
AFM-B-2-1NFE-guided/generator/diffusion_pytorch_model.safetensors
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:72d904dc41423e59c3e70f429a5a12eb01613e04b104f3cfb875b13ba77186c1
|
| 3 |
+
size 589737592
|
AFM-B-2-1NFE-guided/generator/modeling_afm.py
CHANGED
|
@@ -2,7 +2,6 @@
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
-
import copy
|
| 6 |
import math
|
| 7 |
from typing import Any, Dict, Optional
|
| 8 |
|
|
@@ -10,15 +9,10 @@ import torch
|
|
| 10 |
import torch.nn.functional as F
|
| 11 |
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
| 12 |
from diffusers.models.modeling_utils import ModelMixin
|
|
|
|
| 13 |
from torch import nn
|
| 14 |
|
| 15 |
|
| 16 |
-
def get_dit_transformer_class():
|
| 17 |
-
from diffusers.models.transformers.dit_transformer_2d import DiTTransformer2DModel
|
| 18 |
-
|
| 19 |
-
return DiTTransformer2DModel
|
| 20 |
-
|
| 21 |
-
|
| 22 |
def build_dit_config(
|
| 23 |
*,
|
| 24 |
depth: int = 28,
|
|
@@ -54,110 +48,15 @@ def build_dit_config(
|
|
| 54 |
}
|
| 55 |
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
if "t_embedder.mlp.0.weight" in converted:
|
| 64 |
-
timestep_weights = {
|
| 65 |
-
"linear_1.weight": converted.pop("t_embedder.mlp.0.weight"),
|
| 66 |
-
"linear_1.bias": converted.pop("t_embedder.mlp.0.bias"),
|
| 67 |
-
"linear_2.weight": converted.pop("t_embedder.mlp.2.weight"),
|
| 68 |
-
"linear_2.bias": converted.pop("t_embedder.mlp.2.bias"),
|
| 69 |
-
}
|
| 70 |
-
else:
|
| 71 |
-
timestep_weights = None
|
| 72 |
-
class_embedding = converted.pop("y_embedder.embedding_table.weight")
|
| 73 |
-
if class_embedding.shape[0] == 1000:
|
| 74 |
-
null_class = torch.zeros(1, class_embedding.shape[1], dtype=class_embedding.dtype)
|
| 75 |
-
class_embedding = torch.cat([class_embedding, null_class], dim=0)
|
| 76 |
-
|
| 77 |
-
for block_idx in range(depth):
|
| 78 |
-
if timestep_weights is not None:
|
| 79 |
-
for key, tensor in timestep_weights.items():
|
| 80 |
-
converted[f"transformer_blocks.{block_idx}.norm1.emb.timestep_embedder.{key}"] = tensor.clone()
|
| 81 |
-
converted[f"transformer_blocks.{block_idx}.norm1.emb.class_embedder.embedding_table.weight"] = (
|
| 82 |
-
class_embedding.clone()
|
| 83 |
-
)
|
| 84 |
-
converted[f"transformer_blocks.{block_idx}.norm1.linear.weight"] = converted[
|
| 85 |
-
f"blocks.{block_idx}.adaLN_modulation.1.weight"
|
| 86 |
-
]
|
| 87 |
-
converted[f"transformer_blocks.{block_idx}.norm1.linear.bias"] = converted[
|
| 88 |
-
f"blocks.{block_idx}.adaLN_modulation.1.bias"
|
| 89 |
-
]
|
| 90 |
-
|
| 91 |
-
q, k, v = torch.chunk(converted[f"blocks.{block_idx}.attn.qkv.weight"], 3, dim=0)
|
| 92 |
-
q_bias, k_bias, v_bias = torch.chunk(converted[f"blocks.{block_idx}.attn.qkv.bias"], 3, dim=0)
|
| 93 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_q.weight"] = q
|
| 94 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_q.bias"] = q_bias
|
| 95 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_k.weight"] = k
|
| 96 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_k.bias"] = k_bias
|
| 97 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_v.weight"] = v
|
| 98 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_v.bias"] = v_bias
|
| 99 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_out.0.weight"] = converted[
|
| 100 |
-
f"blocks.{block_idx}.attn.proj.weight"
|
| 101 |
-
]
|
| 102 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_out.0.bias"] = converted[
|
| 103 |
-
f"blocks.{block_idx}.attn.proj.bias"
|
| 104 |
-
]
|
| 105 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.0.proj.weight"] = converted[
|
| 106 |
-
f"blocks.{block_idx}.mlp.fc1.weight"
|
| 107 |
-
]
|
| 108 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.0.proj.bias"] = converted[
|
| 109 |
-
f"blocks.{block_idx}.mlp.fc1.bias"
|
| 110 |
-
]
|
| 111 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.2.weight"] = converted[
|
| 112 |
-
f"blocks.{block_idx}.mlp.fc2.weight"
|
| 113 |
-
]
|
| 114 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.2.bias"] = converted[
|
| 115 |
-
f"blocks.{block_idx}.mlp.fc2.bias"
|
| 116 |
-
]
|
| 117 |
-
|
| 118 |
-
for suffix in (
|
| 119 |
-
"attn.qkv.weight",
|
| 120 |
-
"attn.qkv.bias",
|
| 121 |
-
"attn.proj.weight",
|
| 122 |
-
"attn.proj.bias",
|
| 123 |
-
"mlp.fc1.weight",
|
| 124 |
-
"mlp.fc1.bias",
|
| 125 |
-
"mlp.fc2.weight",
|
| 126 |
-
"mlp.fc2.bias",
|
| 127 |
-
"adaLN_modulation.1.weight",
|
| 128 |
-
"adaLN_modulation.1.bias",
|
| 129 |
-
):
|
| 130 |
-
converted.pop(f"blocks.{block_idx}.{suffix}", None)
|
| 131 |
-
|
| 132 |
-
converted["proj_out_1.weight"] = converted.pop("final_layer.adaLN_modulation.1.weight")
|
| 133 |
-
converted["proj_out_1.bias"] = converted.pop("final_layer.adaLN_modulation.1.bias")
|
| 134 |
-
converted["proj_out_2.weight"] = converted.pop("final_layer.linear.weight")
|
| 135 |
-
converted["proj_out_2.bias"] = converted.pop("final_layer.linear.bias")
|
| 136 |
-
|
| 137 |
-
converted.pop("pos_embed", None)
|
| 138 |
-
for block_idx in range(depth):
|
| 139 |
-
for suffix in ("norm1.weight", "norm1.bias", "norm2.weight", "norm2.bias"):
|
| 140 |
-
converted.pop(f"blocks.{block_idx}.{suffix}", None)
|
| 141 |
-
|
| 142 |
-
return {key: tensor.detach().clone().contiguous() for key, tensor in converted.items()}
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
def load_legacy_dit_state_dict(
|
| 146 |
-
state_dict: Dict[str, torch.Tensor],
|
| 147 |
-
depth: int,
|
| 148 |
-
patch_size: int,
|
| 149 |
-
hidden_size: int,
|
| 150 |
-
num_heads: int,
|
| 151 |
-
) -> Dict[str, torch.Tensor]:
|
| 152 |
-
if "transformer_blocks.0.attn1.to_q.weight" in state_dict:
|
| 153 |
-
return state_dict
|
| 154 |
-
if any(key.startswith("blocks.") for key in state_dict):
|
| 155 |
-
return _convert_legacy_dit_state_dict(state_dict, depth)
|
| 156 |
-
return state_dict
|
| 157 |
|
| 158 |
|
| 159 |
def forward_dit_output(
|
| 160 |
-
transformer,
|
| 161 |
hidden_states: torch.Tensor,
|
| 162 |
timestep: Optional[torch.Tensor],
|
| 163 |
class_labels: torch.Tensor,
|
|
@@ -207,23 +106,6 @@ def forward_dit_output(
|
|
| 207 |
)
|
| 208 |
|
| 209 |
|
| 210 |
-
def remap_transformer_state_dict(
|
| 211 |
-
state_dict: Dict[str, torch.Tensor],
|
| 212 |
-
legacy_kwargs: Dict[str, int],
|
| 213 |
-
extra_prefixes: tuple[str, ...] = (),
|
| 214 |
-
) -> Dict[str, torch.Tensor]:
|
| 215 |
-
state_dict = load_legacy_dit_state_dict(state_dict, **legacy_kwargs)
|
| 216 |
-
remapped: Dict[str, torch.Tensor] = {}
|
| 217 |
-
for key, value in state_dict.items():
|
| 218 |
-
if any(key.startswith(prefix) for prefix in extra_prefixes):
|
| 219 |
-
remapped[key] = value
|
| 220 |
-
elif key.startswith("transformer."):
|
| 221 |
-
remapped[key] = value
|
| 222 |
-
else:
|
| 223 |
-
remapped[f"transformer.{key}"] = value
|
| 224 |
-
return remapped
|
| 225 |
-
|
| 226 |
-
|
| 227 |
def split_output_channels(output: torch.Tensor, in_channels: int) -> torch.Tensor:
|
| 228 |
if output.shape[1] == in_channels:
|
| 229 |
return output
|
|
@@ -259,6 +141,14 @@ class TimestepEmbedder(nn.Module):
|
|
| 259 |
return self.mlp(emb.to(dtype=weight_dtype))
|
| 260 |
|
| 261 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
class Generator(nn.Module):
|
| 263 |
def __init__(
|
| 264 |
self,
|
|
@@ -279,14 +169,7 @@ class Generator(nn.Module):
|
|
| 279 |
super().__init__()
|
| 280 |
self.use_t_src = use_t_src
|
| 281 |
self.use_t_tgt = use_t_tgt
|
| 282 |
-
self.
|
| 283 |
-
"depth": depth,
|
| 284 |
-
"hidden_size": hidden_size,
|
| 285 |
-
"patch_size": patch_size,
|
| 286 |
-
"num_heads": num_heads,
|
| 287 |
-
}
|
| 288 |
-
dit_cls = get_dit_transformer_class()
|
| 289 |
-
self.transformer = dit_cls(
|
| 290 |
**build_dit_config(
|
| 291 |
depth=depth,
|
| 292 |
hidden_size=hidden_size,
|
|
@@ -306,20 +189,9 @@ class Generator(nn.Module):
|
|
| 306 |
nn.init.normal_(self.t_tgt_embedder.mlp[2].weight, std=0.02)
|
| 307 |
|
| 308 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 309 |
-
|
| 310 |
-
has_extra = any(key.startswith(extra) for key in state_dict)
|
| 311 |
-
has_t_embedder = any("t_embedder" in key for key in state_dict)
|
| 312 |
-
remapped = remap_transformer_state_dict(state_dict, self._legacy_kwargs, extra_prefixes=extra)
|
| 313 |
-
if self.use_t_tgt and not has_extra:
|
| 314 |
-
strict = False
|
| 315 |
-
if not has_t_embedder:
|
| 316 |
strict = False
|
| 317 |
-
|
| 318 |
-
if not has_t_embedder:
|
| 319 |
-
for name, param in self.named_parameters():
|
| 320 |
-
if "timestep_embedder" in name:
|
| 321 |
-
param.data.zero_()
|
| 322 |
-
return out
|
| 323 |
|
| 324 |
def forward(self, x, y, t_src=None, t_tgt=None):
|
| 325 |
if self.use_t_src and t_src is not None:
|
|
@@ -353,14 +225,7 @@ class GeneratorDeep(nn.Module):
|
|
| 353 |
):
|
| 354 |
super().__init__()
|
| 355 |
self.repeat = repeat
|
| 356 |
-
self.
|
| 357 |
-
"depth": depth,
|
| 358 |
-
"hidden_size": hidden_size,
|
| 359 |
-
"patch_size": patch_size,
|
| 360 |
-
"num_heads": num_heads,
|
| 361 |
-
}
|
| 362 |
-
dit_cls = get_dit_transformer_class()
|
| 363 |
-
self.transformer = dit_cls(
|
| 364 |
**build_dit_config(
|
| 365 |
depth=depth,
|
| 366 |
hidden_size=hidden_size,
|
|
@@ -376,16 +241,7 @@ class GeneratorDeep(nn.Module):
|
|
| 376 |
self.in_channels = in_channels
|
| 377 |
|
| 378 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 379 |
-
|
| 380 |
-
has_t_embedder = any("t_embedder" in key for key in state_dict)
|
| 381 |
-
if not has_t_embedder:
|
| 382 |
-
strict = False
|
| 383 |
-
out = super().load_state_dict(remapped, strict=strict)
|
| 384 |
-
if not has_t_embedder:
|
| 385 |
-
for name, param in self.named_parameters():
|
| 386 |
-
if "timestep_embedder" in name:
|
| 387 |
-
param.data.zero_()
|
| 388 |
-
return out
|
| 389 |
|
| 390 |
def forward(self, x, y, *args, **kwargs):
|
| 391 |
hidden_states = self.transformer.pos_embed(x)
|
|
@@ -429,7 +285,7 @@ class GeneratorDeep(nn.Module):
|
|
| 429 |
return split_output_channels(output, self.in_channels)
|
| 430 |
|
| 431 |
|
| 432 |
-
class AFMGenerator2DModel(
|
| 433 |
config_name = "config.json"
|
| 434 |
|
| 435 |
@register_to_config
|
|
@@ -470,7 +326,7 @@ class AFMGenerator2DModel(Generator, ModelMixin, ConfigMixin):
|
|
| 470 |
)
|
| 471 |
|
| 472 |
|
| 473 |
-
class AFMGeneratorDeep2DModel(
|
| 474 |
config_name = "config.json"
|
| 475 |
|
| 476 |
@register_to_config
|
|
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
|
|
|
| 5 |
import math
|
| 6 |
from typing import Any, Dict, Optional
|
| 7 |
|
|
|
|
| 9 |
import torch.nn.functional as F
|
| 10 |
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
| 11 |
from diffusers.models.modeling_utils import ModelMixin
|
| 12 |
+
from diffusers.models.transformers.dit_transformer_2d import DiTTransformer2DModel
|
| 13 |
from torch import nn
|
| 14 |
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def build_dit_config(
|
| 17 |
*,
|
| 18 |
depth: int = 28,
|
|
|
|
| 48 |
}
|
| 49 |
|
| 50 |
|
| 51 |
+
_PREFIX_KEYS = ("transformer.", "t_tgt_embedder.")
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def _wrap_dit_state_dict(state_dict: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]:
|
| 55 |
+
return {k if k.startswith(_PREFIX_KEYS) else f"transformer.{k}": v for k, v in state_dict.items()}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
def forward_dit_output(
|
| 59 |
+
transformer: DiTTransformer2DModel,
|
| 60 |
hidden_states: torch.Tensor,
|
| 61 |
timestep: Optional[torch.Tensor],
|
| 62 |
class_labels: torch.Tensor,
|
|
|
|
| 106 |
)
|
| 107 |
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
def split_output_channels(output: torch.Tensor, in_channels: int) -> torch.Tensor:
|
| 110 |
if output.shape[1] == in_channels:
|
| 111 |
return output
|
|
|
|
| 141 |
return self.mlp(emb.to(dtype=weight_dtype))
|
| 142 |
|
| 143 |
|
| 144 |
+
class _AFMCheckpointMixin(ModelMixin):
|
| 145 |
+
def _fix_state_dict_keys_on_load(self, state_dict):
|
| 146 |
+
wrapped = _wrap_dit_state_dict(state_dict)
|
| 147 |
+
state_dict.clear()
|
| 148 |
+
state_dict.update(wrapped)
|
| 149 |
+
super()._fix_state_dict_keys_on_load(state_dict)
|
| 150 |
+
|
| 151 |
+
|
| 152 |
class Generator(nn.Module):
|
| 153 |
def __init__(
|
| 154 |
self,
|
|
|
|
| 169 |
super().__init__()
|
| 170 |
self.use_t_src = use_t_src
|
| 171 |
self.use_t_tgt = use_t_tgt
|
| 172 |
+
self.transformer = DiTTransformer2DModel(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
**build_dit_config(
|
| 174 |
depth=depth,
|
| 175 |
hidden_size=hidden_size,
|
|
|
|
| 189 |
nn.init.normal_(self.t_tgt_embedder.mlp[2].weight, std=0.02)
|
| 190 |
|
| 191 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 192 |
+
if self.use_t_tgt and not any(key.startswith("t_tgt_embedder.") for key in state_dict):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
strict = False
|
| 194 |
+
return super().load_state_dict(_wrap_dit_state_dict(state_dict), strict=strict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
def forward(self, x, y, t_src=None, t_tgt=None):
|
| 197 |
if self.use_t_src and t_src is not None:
|
|
|
|
| 225 |
):
|
| 226 |
super().__init__()
|
| 227 |
self.repeat = repeat
|
| 228 |
+
self.transformer = DiTTransformer2DModel(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
**build_dit_config(
|
| 230 |
depth=depth,
|
| 231 |
hidden_size=hidden_size,
|
|
|
|
| 241 |
self.in_channels = in_channels
|
| 242 |
|
| 243 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 244 |
+
return super().load_state_dict(_wrap_dit_state_dict(state_dict), strict=strict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
def forward(self, x, y, *args, **kwargs):
|
| 247 |
hidden_states = self.transformer.pos_embed(x)
|
|
|
|
| 285 |
return split_output_channels(output, self.in_channels)
|
| 286 |
|
| 287 |
|
| 288 |
+
class AFMGenerator2DModel(_AFMCheckpointMixin, Generator, ConfigMixin):
|
| 289 |
config_name = "config.json"
|
| 290 |
|
| 291 |
@register_to_config
|
|
|
|
| 326 |
)
|
| 327 |
|
| 328 |
|
| 329 |
+
class AFMGeneratorDeep2DModel(_AFMCheckpointMixin, GeneratorDeep, ConfigMixin):
|
| 330 |
config_name = "config.json"
|
| 331 |
|
| 332 |
@register_to_config
|
AFM-B-2-1NFE-noguide/generator/config.json
CHANGED
|
@@ -1,6 +1,22 @@
|
|
| 1 |
{
|
| 2 |
-
"_class_name": "
|
| 3 |
"_diffusers_version": "0.36.0",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
"model_type": "AFM-B/2",
|
| 5 |
"architecture": "standard",
|
| 6 |
"repeat": 1,
|
|
@@ -12,9 +28,7 @@
|
|
| 12 |
"class_dropout_prob": 0.0,
|
| 13 |
"input_size": 32,
|
| 14 |
"num_classes": 1000,
|
| 15 |
-
"in_channels": 4,
|
| 16 |
"depth": 12,
|
| 17 |
"hidden_size": 768,
|
| 18 |
-
"patch_size": 2,
|
| 19 |
"num_heads": 12
|
| 20 |
}
|
|
|
|
| 1 |
{
|
| 2 |
+
"_class_name": "DiTTransformer2DModel",
|
| 3 |
"_diffusers_version": "0.36.0",
|
| 4 |
+
"sample_size": 32,
|
| 5 |
+
"num_layers": 12,
|
| 6 |
+
"num_attention_heads": 12,
|
| 7 |
+
"attention_head_dim": 64,
|
| 8 |
+
"in_channels": 4,
|
| 9 |
+
"out_channels": 4,
|
| 10 |
+
"patch_size": 2,
|
| 11 |
+
"attention_bias": true,
|
| 12 |
+
"activation_fn": "gelu-approximate",
|
| 13 |
+
"num_embeds_ada_norm": 1000,
|
| 14 |
+
"norm_type": "ada_norm_zero",
|
| 15 |
+
"norm_elementwise_affine": false,
|
| 16 |
+
"dropout": 0.0,
|
| 17 |
+
"norm_num_groups": 32,
|
| 18 |
+
"norm_eps": 1e-05,
|
| 19 |
+
"upcast_attention": false,
|
| 20 |
"model_type": "AFM-B/2",
|
| 21 |
"architecture": "standard",
|
| 22 |
"repeat": 1,
|
|
|
|
| 28 |
"class_dropout_prob": 0.0,
|
| 29 |
"input_size": 32,
|
| 30 |
"num_classes": 1000,
|
|
|
|
| 31 |
"depth": 12,
|
| 32 |
"hidden_size": 768,
|
|
|
|
| 33 |
"num_heads": 12
|
| 34 |
}
|
AFM-B-2-1NFE-noguide/generator/diffusion_pytorch_model.safetensors
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:af0b3881166119fbca961d253c9e96a0414e56096531dd34046961c934977051
|
| 3 |
+
size 589737592
|
AFM-B-2-1NFE-noguide/generator/modeling_afm.py
CHANGED
|
@@ -2,7 +2,6 @@
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
-
import copy
|
| 6 |
import math
|
| 7 |
from typing import Any, Dict, Optional
|
| 8 |
|
|
@@ -10,15 +9,10 @@ import torch
|
|
| 10 |
import torch.nn.functional as F
|
| 11 |
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
| 12 |
from diffusers.models.modeling_utils import ModelMixin
|
|
|
|
| 13 |
from torch import nn
|
| 14 |
|
| 15 |
|
| 16 |
-
def get_dit_transformer_class():
|
| 17 |
-
from diffusers.models.transformers.dit_transformer_2d import DiTTransformer2DModel
|
| 18 |
-
|
| 19 |
-
return DiTTransformer2DModel
|
| 20 |
-
|
| 21 |
-
|
| 22 |
def build_dit_config(
|
| 23 |
*,
|
| 24 |
depth: int = 28,
|
|
@@ -54,110 +48,15 @@ def build_dit_config(
|
|
| 54 |
}
|
| 55 |
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
if "t_embedder.mlp.0.weight" in converted:
|
| 64 |
-
timestep_weights = {
|
| 65 |
-
"linear_1.weight": converted.pop("t_embedder.mlp.0.weight"),
|
| 66 |
-
"linear_1.bias": converted.pop("t_embedder.mlp.0.bias"),
|
| 67 |
-
"linear_2.weight": converted.pop("t_embedder.mlp.2.weight"),
|
| 68 |
-
"linear_2.bias": converted.pop("t_embedder.mlp.2.bias"),
|
| 69 |
-
}
|
| 70 |
-
else:
|
| 71 |
-
timestep_weights = None
|
| 72 |
-
class_embedding = converted.pop("y_embedder.embedding_table.weight")
|
| 73 |
-
if class_embedding.shape[0] == 1000:
|
| 74 |
-
null_class = torch.zeros(1, class_embedding.shape[1], dtype=class_embedding.dtype)
|
| 75 |
-
class_embedding = torch.cat([class_embedding, null_class], dim=0)
|
| 76 |
-
|
| 77 |
-
for block_idx in range(depth):
|
| 78 |
-
if timestep_weights is not None:
|
| 79 |
-
for key, tensor in timestep_weights.items():
|
| 80 |
-
converted[f"transformer_blocks.{block_idx}.norm1.emb.timestep_embedder.{key}"] = tensor.clone()
|
| 81 |
-
converted[f"transformer_blocks.{block_idx}.norm1.emb.class_embedder.embedding_table.weight"] = (
|
| 82 |
-
class_embedding.clone()
|
| 83 |
-
)
|
| 84 |
-
converted[f"transformer_blocks.{block_idx}.norm1.linear.weight"] = converted[
|
| 85 |
-
f"blocks.{block_idx}.adaLN_modulation.1.weight"
|
| 86 |
-
]
|
| 87 |
-
converted[f"transformer_blocks.{block_idx}.norm1.linear.bias"] = converted[
|
| 88 |
-
f"blocks.{block_idx}.adaLN_modulation.1.bias"
|
| 89 |
-
]
|
| 90 |
-
|
| 91 |
-
q, k, v = torch.chunk(converted[f"blocks.{block_idx}.attn.qkv.weight"], 3, dim=0)
|
| 92 |
-
q_bias, k_bias, v_bias = torch.chunk(converted[f"blocks.{block_idx}.attn.qkv.bias"], 3, dim=0)
|
| 93 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_q.weight"] = q
|
| 94 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_q.bias"] = q_bias
|
| 95 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_k.weight"] = k
|
| 96 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_k.bias"] = k_bias
|
| 97 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_v.weight"] = v
|
| 98 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_v.bias"] = v_bias
|
| 99 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_out.0.weight"] = converted[
|
| 100 |
-
f"blocks.{block_idx}.attn.proj.weight"
|
| 101 |
-
]
|
| 102 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_out.0.bias"] = converted[
|
| 103 |
-
f"blocks.{block_idx}.attn.proj.bias"
|
| 104 |
-
]
|
| 105 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.0.proj.weight"] = converted[
|
| 106 |
-
f"blocks.{block_idx}.mlp.fc1.weight"
|
| 107 |
-
]
|
| 108 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.0.proj.bias"] = converted[
|
| 109 |
-
f"blocks.{block_idx}.mlp.fc1.bias"
|
| 110 |
-
]
|
| 111 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.2.weight"] = converted[
|
| 112 |
-
f"blocks.{block_idx}.mlp.fc2.weight"
|
| 113 |
-
]
|
| 114 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.2.bias"] = converted[
|
| 115 |
-
f"blocks.{block_idx}.mlp.fc2.bias"
|
| 116 |
-
]
|
| 117 |
-
|
| 118 |
-
for suffix in (
|
| 119 |
-
"attn.qkv.weight",
|
| 120 |
-
"attn.qkv.bias",
|
| 121 |
-
"attn.proj.weight",
|
| 122 |
-
"attn.proj.bias",
|
| 123 |
-
"mlp.fc1.weight",
|
| 124 |
-
"mlp.fc1.bias",
|
| 125 |
-
"mlp.fc2.weight",
|
| 126 |
-
"mlp.fc2.bias",
|
| 127 |
-
"adaLN_modulation.1.weight",
|
| 128 |
-
"adaLN_modulation.1.bias",
|
| 129 |
-
):
|
| 130 |
-
converted.pop(f"blocks.{block_idx}.{suffix}", None)
|
| 131 |
-
|
| 132 |
-
converted["proj_out_1.weight"] = converted.pop("final_layer.adaLN_modulation.1.weight")
|
| 133 |
-
converted["proj_out_1.bias"] = converted.pop("final_layer.adaLN_modulation.1.bias")
|
| 134 |
-
converted["proj_out_2.weight"] = converted.pop("final_layer.linear.weight")
|
| 135 |
-
converted["proj_out_2.bias"] = converted.pop("final_layer.linear.bias")
|
| 136 |
-
|
| 137 |
-
converted.pop("pos_embed", None)
|
| 138 |
-
for block_idx in range(depth):
|
| 139 |
-
for suffix in ("norm1.weight", "norm1.bias", "norm2.weight", "norm2.bias"):
|
| 140 |
-
converted.pop(f"blocks.{block_idx}.{suffix}", None)
|
| 141 |
-
|
| 142 |
-
return {key: tensor.detach().clone().contiguous() for key, tensor in converted.items()}
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
def load_legacy_dit_state_dict(
|
| 146 |
-
state_dict: Dict[str, torch.Tensor],
|
| 147 |
-
depth: int,
|
| 148 |
-
patch_size: int,
|
| 149 |
-
hidden_size: int,
|
| 150 |
-
num_heads: int,
|
| 151 |
-
) -> Dict[str, torch.Tensor]:
|
| 152 |
-
if "transformer_blocks.0.attn1.to_q.weight" in state_dict:
|
| 153 |
-
return state_dict
|
| 154 |
-
if any(key.startswith("blocks.") for key in state_dict):
|
| 155 |
-
return _convert_legacy_dit_state_dict(state_dict, depth)
|
| 156 |
-
return state_dict
|
| 157 |
|
| 158 |
|
| 159 |
def forward_dit_output(
|
| 160 |
-
transformer,
|
| 161 |
hidden_states: torch.Tensor,
|
| 162 |
timestep: Optional[torch.Tensor],
|
| 163 |
class_labels: torch.Tensor,
|
|
@@ -207,23 +106,6 @@ def forward_dit_output(
|
|
| 207 |
)
|
| 208 |
|
| 209 |
|
| 210 |
-
def remap_transformer_state_dict(
|
| 211 |
-
state_dict: Dict[str, torch.Tensor],
|
| 212 |
-
legacy_kwargs: Dict[str, int],
|
| 213 |
-
extra_prefixes: tuple[str, ...] = (),
|
| 214 |
-
) -> Dict[str, torch.Tensor]:
|
| 215 |
-
state_dict = load_legacy_dit_state_dict(state_dict, **legacy_kwargs)
|
| 216 |
-
remapped: Dict[str, torch.Tensor] = {}
|
| 217 |
-
for key, value in state_dict.items():
|
| 218 |
-
if any(key.startswith(prefix) for prefix in extra_prefixes):
|
| 219 |
-
remapped[key] = value
|
| 220 |
-
elif key.startswith("transformer."):
|
| 221 |
-
remapped[key] = value
|
| 222 |
-
else:
|
| 223 |
-
remapped[f"transformer.{key}"] = value
|
| 224 |
-
return remapped
|
| 225 |
-
|
| 226 |
-
|
| 227 |
def split_output_channels(output: torch.Tensor, in_channels: int) -> torch.Tensor:
|
| 228 |
if output.shape[1] == in_channels:
|
| 229 |
return output
|
|
@@ -259,6 +141,14 @@ class TimestepEmbedder(nn.Module):
|
|
| 259 |
return self.mlp(emb.to(dtype=weight_dtype))
|
| 260 |
|
| 261 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
class Generator(nn.Module):
|
| 263 |
def __init__(
|
| 264 |
self,
|
|
@@ -279,14 +169,7 @@ class Generator(nn.Module):
|
|
| 279 |
super().__init__()
|
| 280 |
self.use_t_src = use_t_src
|
| 281 |
self.use_t_tgt = use_t_tgt
|
| 282 |
-
self.
|
| 283 |
-
"depth": depth,
|
| 284 |
-
"hidden_size": hidden_size,
|
| 285 |
-
"patch_size": patch_size,
|
| 286 |
-
"num_heads": num_heads,
|
| 287 |
-
}
|
| 288 |
-
dit_cls = get_dit_transformer_class()
|
| 289 |
-
self.transformer = dit_cls(
|
| 290 |
**build_dit_config(
|
| 291 |
depth=depth,
|
| 292 |
hidden_size=hidden_size,
|
|
@@ -306,20 +189,9 @@ class Generator(nn.Module):
|
|
| 306 |
nn.init.normal_(self.t_tgt_embedder.mlp[2].weight, std=0.02)
|
| 307 |
|
| 308 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 309 |
-
|
| 310 |
-
has_extra = any(key.startswith(extra) for key in state_dict)
|
| 311 |
-
has_t_embedder = any("t_embedder" in key for key in state_dict)
|
| 312 |
-
remapped = remap_transformer_state_dict(state_dict, self._legacy_kwargs, extra_prefixes=extra)
|
| 313 |
-
if self.use_t_tgt and not has_extra:
|
| 314 |
-
strict = False
|
| 315 |
-
if not has_t_embedder:
|
| 316 |
strict = False
|
| 317 |
-
|
| 318 |
-
if not has_t_embedder:
|
| 319 |
-
for name, param in self.named_parameters():
|
| 320 |
-
if "timestep_embedder" in name:
|
| 321 |
-
param.data.zero_()
|
| 322 |
-
return out
|
| 323 |
|
| 324 |
def forward(self, x, y, t_src=None, t_tgt=None):
|
| 325 |
if self.use_t_src and t_src is not None:
|
|
@@ -353,14 +225,7 @@ class GeneratorDeep(nn.Module):
|
|
| 353 |
):
|
| 354 |
super().__init__()
|
| 355 |
self.repeat = repeat
|
| 356 |
-
self.
|
| 357 |
-
"depth": depth,
|
| 358 |
-
"hidden_size": hidden_size,
|
| 359 |
-
"patch_size": patch_size,
|
| 360 |
-
"num_heads": num_heads,
|
| 361 |
-
}
|
| 362 |
-
dit_cls = get_dit_transformer_class()
|
| 363 |
-
self.transformer = dit_cls(
|
| 364 |
**build_dit_config(
|
| 365 |
depth=depth,
|
| 366 |
hidden_size=hidden_size,
|
|
@@ -376,16 +241,7 @@ class GeneratorDeep(nn.Module):
|
|
| 376 |
self.in_channels = in_channels
|
| 377 |
|
| 378 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 379 |
-
|
| 380 |
-
has_t_embedder = any("t_embedder" in key for key in state_dict)
|
| 381 |
-
if not has_t_embedder:
|
| 382 |
-
strict = False
|
| 383 |
-
out = super().load_state_dict(remapped, strict=strict)
|
| 384 |
-
if not has_t_embedder:
|
| 385 |
-
for name, param in self.named_parameters():
|
| 386 |
-
if "timestep_embedder" in name:
|
| 387 |
-
param.data.zero_()
|
| 388 |
-
return out
|
| 389 |
|
| 390 |
def forward(self, x, y, *args, **kwargs):
|
| 391 |
hidden_states = self.transformer.pos_embed(x)
|
|
@@ -429,7 +285,7 @@ class GeneratorDeep(nn.Module):
|
|
| 429 |
return split_output_channels(output, self.in_channels)
|
| 430 |
|
| 431 |
|
| 432 |
-
class AFMGenerator2DModel(
|
| 433 |
config_name = "config.json"
|
| 434 |
|
| 435 |
@register_to_config
|
|
@@ -470,7 +326,7 @@ class AFMGenerator2DModel(Generator, ModelMixin, ConfigMixin):
|
|
| 470 |
)
|
| 471 |
|
| 472 |
|
| 473 |
-
class AFMGeneratorDeep2DModel(
|
| 474 |
config_name = "config.json"
|
| 475 |
|
| 476 |
@register_to_config
|
|
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
|
|
|
| 5 |
import math
|
| 6 |
from typing import Any, Dict, Optional
|
| 7 |
|
|
|
|
| 9 |
import torch.nn.functional as F
|
| 10 |
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
| 11 |
from diffusers.models.modeling_utils import ModelMixin
|
| 12 |
+
from diffusers.models.transformers.dit_transformer_2d import DiTTransformer2DModel
|
| 13 |
from torch import nn
|
| 14 |
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def build_dit_config(
|
| 17 |
*,
|
| 18 |
depth: int = 28,
|
|
|
|
| 48 |
}
|
| 49 |
|
| 50 |
|
| 51 |
+
_PREFIX_KEYS = ("transformer.", "t_tgt_embedder.")
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def _wrap_dit_state_dict(state_dict: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]:
|
| 55 |
+
return {k if k.startswith(_PREFIX_KEYS) else f"transformer.{k}": v for k, v in state_dict.items()}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
def forward_dit_output(
|
| 59 |
+
transformer: DiTTransformer2DModel,
|
| 60 |
hidden_states: torch.Tensor,
|
| 61 |
timestep: Optional[torch.Tensor],
|
| 62 |
class_labels: torch.Tensor,
|
|
|
|
| 106 |
)
|
| 107 |
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
def split_output_channels(output: torch.Tensor, in_channels: int) -> torch.Tensor:
|
| 110 |
if output.shape[1] == in_channels:
|
| 111 |
return output
|
|
|
|
| 141 |
return self.mlp(emb.to(dtype=weight_dtype))
|
| 142 |
|
| 143 |
|
| 144 |
+
class _AFMCheckpointMixin(ModelMixin):
|
| 145 |
+
def _fix_state_dict_keys_on_load(self, state_dict):
|
| 146 |
+
wrapped = _wrap_dit_state_dict(state_dict)
|
| 147 |
+
state_dict.clear()
|
| 148 |
+
state_dict.update(wrapped)
|
| 149 |
+
super()._fix_state_dict_keys_on_load(state_dict)
|
| 150 |
+
|
| 151 |
+
|
| 152 |
class Generator(nn.Module):
|
| 153 |
def __init__(
|
| 154 |
self,
|
|
|
|
| 169 |
super().__init__()
|
| 170 |
self.use_t_src = use_t_src
|
| 171 |
self.use_t_tgt = use_t_tgt
|
| 172 |
+
self.transformer = DiTTransformer2DModel(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
**build_dit_config(
|
| 174 |
depth=depth,
|
| 175 |
hidden_size=hidden_size,
|
|
|
|
| 189 |
nn.init.normal_(self.t_tgt_embedder.mlp[2].weight, std=0.02)
|
| 190 |
|
| 191 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 192 |
+
if self.use_t_tgt and not any(key.startswith("t_tgt_embedder.") for key in state_dict):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
strict = False
|
| 194 |
+
return super().load_state_dict(_wrap_dit_state_dict(state_dict), strict=strict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
def forward(self, x, y, t_src=None, t_tgt=None):
|
| 197 |
if self.use_t_src and t_src is not None:
|
|
|
|
| 225 |
):
|
| 226 |
super().__init__()
|
| 227 |
self.repeat = repeat
|
| 228 |
+
self.transformer = DiTTransformer2DModel(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
**build_dit_config(
|
| 230 |
depth=depth,
|
| 231 |
hidden_size=hidden_size,
|
|
|
|
| 241 |
self.in_channels = in_channels
|
| 242 |
|
| 243 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 244 |
+
return super().load_state_dict(_wrap_dit_state_dict(state_dict), strict=strict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
def forward(self, x, y, *args, **kwargs):
|
| 247 |
hidden_states = self.transformer.pos_embed(x)
|
|
|
|
| 285 |
return split_output_channels(output, self.in_channels)
|
| 286 |
|
| 287 |
|
| 288 |
+
class AFMGenerator2DModel(_AFMCheckpointMixin, Generator, ConfigMixin):
|
| 289 |
config_name = "config.json"
|
| 290 |
|
| 291 |
@register_to_config
|
|
|
|
| 326 |
)
|
| 327 |
|
| 328 |
|
| 329 |
+
class AFMGeneratorDeep2DModel(_AFMCheckpointMixin, GeneratorDeep, ConfigMixin):
|
| 330 |
config_name = "config.json"
|
| 331 |
|
| 332 |
@register_to_config
|
AFM-L-2-1NFE-guided/generator/config.json
CHANGED
|
@@ -1,6 +1,22 @@
|
|
| 1 |
{
|
| 2 |
-
"_class_name": "
|
| 3 |
"_diffusers_version": "0.36.0",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
"model_type": "AFM-L/2",
|
| 5 |
"architecture": "standard",
|
| 6 |
"repeat": 1,
|
|
@@ -12,9 +28,7 @@
|
|
| 12 |
"class_dropout_prob": 0.0,
|
| 13 |
"input_size": 32,
|
| 14 |
"num_classes": 1000,
|
| 15 |
-
"in_channels": 4,
|
| 16 |
"depth": 24,
|
| 17 |
"hidden_size": 1024,
|
| 18 |
-
"patch_size": 2,
|
| 19 |
"num_heads": 16
|
| 20 |
}
|
|
|
|
| 1 |
{
|
| 2 |
+
"_class_name": "DiTTransformer2DModel",
|
| 3 |
"_diffusers_version": "0.36.0",
|
| 4 |
+
"sample_size": 32,
|
| 5 |
+
"num_layers": 24,
|
| 6 |
+
"num_attention_heads": 16,
|
| 7 |
+
"attention_head_dim": 64,
|
| 8 |
+
"in_channels": 4,
|
| 9 |
+
"out_channels": 4,
|
| 10 |
+
"patch_size": 2,
|
| 11 |
+
"attention_bias": true,
|
| 12 |
+
"activation_fn": "gelu-approximate",
|
| 13 |
+
"num_embeds_ada_norm": 1000,
|
| 14 |
+
"norm_type": "ada_norm_zero",
|
| 15 |
+
"norm_elementwise_affine": false,
|
| 16 |
+
"dropout": 0.0,
|
| 17 |
+
"norm_num_groups": 32,
|
| 18 |
+
"norm_eps": 1e-05,
|
| 19 |
+
"upcast_attention": false,
|
| 20 |
"model_type": "AFM-L/2",
|
| 21 |
"architecture": "standard",
|
| 22 |
"repeat": 1,
|
|
|
|
| 28 |
"class_dropout_prob": 0.0,
|
| 29 |
"input_size": 32,
|
| 30 |
"num_classes": 1000,
|
|
|
|
| 31 |
"depth": 24,
|
| 32 |
"hidden_size": 1024,
|
|
|
|
| 33 |
"num_heads": 16
|
| 34 |
}
|
AFM-L-2-1NFE-guided/generator/diffusion_pytorch_model.safetensors
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:193cda8648b3cd31432402b249df6bd67ee7f0bc59ac00080f3f2d811919e871
|
| 3 |
+
size 2046429144
|
AFM-L-2-1NFE-guided/generator/modeling_afm.py
CHANGED
|
@@ -2,7 +2,6 @@
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
-
import copy
|
| 6 |
import math
|
| 7 |
from typing import Any, Dict, Optional
|
| 8 |
|
|
@@ -10,15 +9,10 @@ import torch
|
|
| 10 |
import torch.nn.functional as F
|
| 11 |
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
| 12 |
from diffusers.models.modeling_utils import ModelMixin
|
|
|
|
| 13 |
from torch import nn
|
| 14 |
|
| 15 |
|
| 16 |
-
def get_dit_transformer_class():
|
| 17 |
-
from diffusers.models.transformers.dit_transformer_2d import DiTTransformer2DModel
|
| 18 |
-
|
| 19 |
-
return DiTTransformer2DModel
|
| 20 |
-
|
| 21 |
-
|
| 22 |
def build_dit_config(
|
| 23 |
*,
|
| 24 |
depth: int = 28,
|
|
@@ -54,110 +48,15 @@ def build_dit_config(
|
|
| 54 |
}
|
| 55 |
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
if "t_embedder.mlp.0.weight" in converted:
|
| 64 |
-
timestep_weights = {
|
| 65 |
-
"linear_1.weight": converted.pop("t_embedder.mlp.0.weight"),
|
| 66 |
-
"linear_1.bias": converted.pop("t_embedder.mlp.0.bias"),
|
| 67 |
-
"linear_2.weight": converted.pop("t_embedder.mlp.2.weight"),
|
| 68 |
-
"linear_2.bias": converted.pop("t_embedder.mlp.2.bias"),
|
| 69 |
-
}
|
| 70 |
-
else:
|
| 71 |
-
timestep_weights = None
|
| 72 |
-
class_embedding = converted.pop("y_embedder.embedding_table.weight")
|
| 73 |
-
if class_embedding.shape[0] == 1000:
|
| 74 |
-
null_class = torch.zeros(1, class_embedding.shape[1], dtype=class_embedding.dtype)
|
| 75 |
-
class_embedding = torch.cat([class_embedding, null_class], dim=0)
|
| 76 |
-
|
| 77 |
-
for block_idx in range(depth):
|
| 78 |
-
if timestep_weights is not None:
|
| 79 |
-
for key, tensor in timestep_weights.items():
|
| 80 |
-
converted[f"transformer_blocks.{block_idx}.norm1.emb.timestep_embedder.{key}"] = tensor.clone()
|
| 81 |
-
converted[f"transformer_blocks.{block_idx}.norm1.emb.class_embedder.embedding_table.weight"] = (
|
| 82 |
-
class_embedding.clone()
|
| 83 |
-
)
|
| 84 |
-
converted[f"transformer_blocks.{block_idx}.norm1.linear.weight"] = converted[
|
| 85 |
-
f"blocks.{block_idx}.adaLN_modulation.1.weight"
|
| 86 |
-
]
|
| 87 |
-
converted[f"transformer_blocks.{block_idx}.norm1.linear.bias"] = converted[
|
| 88 |
-
f"blocks.{block_idx}.adaLN_modulation.1.bias"
|
| 89 |
-
]
|
| 90 |
-
|
| 91 |
-
q, k, v = torch.chunk(converted[f"blocks.{block_idx}.attn.qkv.weight"], 3, dim=0)
|
| 92 |
-
q_bias, k_bias, v_bias = torch.chunk(converted[f"blocks.{block_idx}.attn.qkv.bias"], 3, dim=0)
|
| 93 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_q.weight"] = q
|
| 94 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_q.bias"] = q_bias
|
| 95 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_k.weight"] = k
|
| 96 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_k.bias"] = k_bias
|
| 97 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_v.weight"] = v
|
| 98 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_v.bias"] = v_bias
|
| 99 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_out.0.weight"] = converted[
|
| 100 |
-
f"blocks.{block_idx}.attn.proj.weight"
|
| 101 |
-
]
|
| 102 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_out.0.bias"] = converted[
|
| 103 |
-
f"blocks.{block_idx}.attn.proj.bias"
|
| 104 |
-
]
|
| 105 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.0.proj.weight"] = converted[
|
| 106 |
-
f"blocks.{block_idx}.mlp.fc1.weight"
|
| 107 |
-
]
|
| 108 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.0.proj.bias"] = converted[
|
| 109 |
-
f"blocks.{block_idx}.mlp.fc1.bias"
|
| 110 |
-
]
|
| 111 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.2.weight"] = converted[
|
| 112 |
-
f"blocks.{block_idx}.mlp.fc2.weight"
|
| 113 |
-
]
|
| 114 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.2.bias"] = converted[
|
| 115 |
-
f"blocks.{block_idx}.mlp.fc2.bias"
|
| 116 |
-
]
|
| 117 |
-
|
| 118 |
-
for suffix in (
|
| 119 |
-
"attn.qkv.weight",
|
| 120 |
-
"attn.qkv.bias",
|
| 121 |
-
"attn.proj.weight",
|
| 122 |
-
"attn.proj.bias",
|
| 123 |
-
"mlp.fc1.weight",
|
| 124 |
-
"mlp.fc1.bias",
|
| 125 |
-
"mlp.fc2.weight",
|
| 126 |
-
"mlp.fc2.bias",
|
| 127 |
-
"adaLN_modulation.1.weight",
|
| 128 |
-
"adaLN_modulation.1.bias",
|
| 129 |
-
):
|
| 130 |
-
converted.pop(f"blocks.{block_idx}.{suffix}", None)
|
| 131 |
-
|
| 132 |
-
converted["proj_out_1.weight"] = converted.pop("final_layer.adaLN_modulation.1.weight")
|
| 133 |
-
converted["proj_out_1.bias"] = converted.pop("final_layer.adaLN_modulation.1.bias")
|
| 134 |
-
converted["proj_out_2.weight"] = converted.pop("final_layer.linear.weight")
|
| 135 |
-
converted["proj_out_2.bias"] = converted.pop("final_layer.linear.bias")
|
| 136 |
-
|
| 137 |
-
converted.pop("pos_embed", None)
|
| 138 |
-
for block_idx in range(depth):
|
| 139 |
-
for suffix in ("norm1.weight", "norm1.bias", "norm2.weight", "norm2.bias"):
|
| 140 |
-
converted.pop(f"blocks.{block_idx}.{suffix}", None)
|
| 141 |
-
|
| 142 |
-
return {key: tensor.detach().clone().contiguous() for key, tensor in converted.items()}
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
def load_legacy_dit_state_dict(
|
| 146 |
-
state_dict: Dict[str, torch.Tensor],
|
| 147 |
-
depth: int,
|
| 148 |
-
patch_size: int,
|
| 149 |
-
hidden_size: int,
|
| 150 |
-
num_heads: int,
|
| 151 |
-
) -> Dict[str, torch.Tensor]:
|
| 152 |
-
if "transformer_blocks.0.attn1.to_q.weight" in state_dict:
|
| 153 |
-
return state_dict
|
| 154 |
-
if any(key.startswith("blocks.") for key in state_dict):
|
| 155 |
-
return _convert_legacy_dit_state_dict(state_dict, depth)
|
| 156 |
-
return state_dict
|
| 157 |
|
| 158 |
|
| 159 |
def forward_dit_output(
|
| 160 |
-
transformer,
|
| 161 |
hidden_states: torch.Tensor,
|
| 162 |
timestep: Optional[torch.Tensor],
|
| 163 |
class_labels: torch.Tensor,
|
|
@@ -207,23 +106,6 @@ def forward_dit_output(
|
|
| 207 |
)
|
| 208 |
|
| 209 |
|
| 210 |
-
def remap_transformer_state_dict(
|
| 211 |
-
state_dict: Dict[str, torch.Tensor],
|
| 212 |
-
legacy_kwargs: Dict[str, int],
|
| 213 |
-
extra_prefixes: tuple[str, ...] = (),
|
| 214 |
-
) -> Dict[str, torch.Tensor]:
|
| 215 |
-
state_dict = load_legacy_dit_state_dict(state_dict, **legacy_kwargs)
|
| 216 |
-
remapped: Dict[str, torch.Tensor] = {}
|
| 217 |
-
for key, value in state_dict.items():
|
| 218 |
-
if any(key.startswith(prefix) for prefix in extra_prefixes):
|
| 219 |
-
remapped[key] = value
|
| 220 |
-
elif key.startswith("transformer."):
|
| 221 |
-
remapped[key] = value
|
| 222 |
-
else:
|
| 223 |
-
remapped[f"transformer.{key}"] = value
|
| 224 |
-
return remapped
|
| 225 |
-
|
| 226 |
-
|
| 227 |
def split_output_channels(output: torch.Tensor, in_channels: int) -> torch.Tensor:
|
| 228 |
if output.shape[1] == in_channels:
|
| 229 |
return output
|
|
@@ -259,6 +141,14 @@ class TimestepEmbedder(nn.Module):
|
|
| 259 |
return self.mlp(emb.to(dtype=weight_dtype))
|
| 260 |
|
| 261 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
class Generator(nn.Module):
|
| 263 |
def __init__(
|
| 264 |
self,
|
|
@@ -279,14 +169,7 @@ class Generator(nn.Module):
|
|
| 279 |
super().__init__()
|
| 280 |
self.use_t_src = use_t_src
|
| 281 |
self.use_t_tgt = use_t_tgt
|
| 282 |
-
self.
|
| 283 |
-
"depth": depth,
|
| 284 |
-
"hidden_size": hidden_size,
|
| 285 |
-
"patch_size": patch_size,
|
| 286 |
-
"num_heads": num_heads,
|
| 287 |
-
}
|
| 288 |
-
dit_cls = get_dit_transformer_class()
|
| 289 |
-
self.transformer = dit_cls(
|
| 290 |
**build_dit_config(
|
| 291 |
depth=depth,
|
| 292 |
hidden_size=hidden_size,
|
|
@@ -306,20 +189,9 @@ class Generator(nn.Module):
|
|
| 306 |
nn.init.normal_(self.t_tgt_embedder.mlp[2].weight, std=0.02)
|
| 307 |
|
| 308 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 309 |
-
|
| 310 |
-
has_extra = any(key.startswith(extra) for key in state_dict)
|
| 311 |
-
has_t_embedder = any("t_embedder" in key for key in state_dict)
|
| 312 |
-
remapped = remap_transformer_state_dict(state_dict, self._legacy_kwargs, extra_prefixes=extra)
|
| 313 |
-
if self.use_t_tgt and not has_extra:
|
| 314 |
-
strict = False
|
| 315 |
-
if not has_t_embedder:
|
| 316 |
strict = False
|
| 317 |
-
|
| 318 |
-
if not has_t_embedder:
|
| 319 |
-
for name, param in self.named_parameters():
|
| 320 |
-
if "timestep_embedder" in name:
|
| 321 |
-
param.data.zero_()
|
| 322 |
-
return out
|
| 323 |
|
| 324 |
def forward(self, x, y, t_src=None, t_tgt=None):
|
| 325 |
if self.use_t_src and t_src is not None:
|
|
@@ -353,14 +225,7 @@ class GeneratorDeep(nn.Module):
|
|
| 353 |
):
|
| 354 |
super().__init__()
|
| 355 |
self.repeat = repeat
|
| 356 |
-
self.
|
| 357 |
-
"depth": depth,
|
| 358 |
-
"hidden_size": hidden_size,
|
| 359 |
-
"patch_size": patch_size,
|
| 360 |
-
"num_heads": num_heads,
|
| 361 |
-
}
|
| 362 |
-
dit_cls = get_dit_transformer_class()
|
| 363 |
-
self.transformer = dit_cls(
|
| 364 |
**build_dit_config(
|
| 365 |
depth=depth,
|
| 366 |
hidden_size=hidden_size,
|
|
@@ -376,16 +241,7 @@ class GeneratorDeep(nn.Module):
|
|
| 376 |
self.in_channels = in_channels
|
| 377 |
|
| 378 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 379 |
-
|
| 380 |
-
has_t_embedder = any("t_embedder" in key for key in state_dict)
|
| 381 |
-
if not has_t_embedder:
|
| 382 |
-
strict = False
|
| 383 |
-
out = super().load_state_dict(remapped, strict=strict)
|
| 384 |
-
if not has_t_embedder:
|
| 385 |
-
for name, param in self.named_parameters():
|
| 386 |
-
if "timestep_embedder" in name:
|
| 387 |
-
param.data.zero_()
|
| 388 |
-
return out
|
| 389 |
|
| 390 |
def forward(self, x, y, *args, **kwargs):
|
| 391 |
hidden_states = self.transformer.pos_embed(x)
|
|
@@ -429,7 +285,7 @@ class GeneratorDeep(nn.Module):
|
|
| 429 |
return split_output_channels(output, self.in_channels)
|
| 430 |
|
| 431 |
|
| 432 |
-
class AFMGenerator2DModel(
|
| 433 |
config_name = "config.json"
|
| 434 |
|
| 435 |
@register_to_config
|
|
@@ -470,7 +326,7 @@ class AFMGenerator2DModel(Generator, ModelMixin, ConfigMixin):
|
|
| 470 |
)
|
| 471 |
|
| 472 |
|
| 473 |
-
class AFMGeneratorDeep2DModel(
|
| 474 |
config_name = "config.json"
|
| 475 |
|
| 476 |
@register_to_config
|
|
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
|
|
|
| 5 |
import math
|
| 6 |
from typing import Any, Dict, Optional
|
| 7 |
|
|
|
|
| 9 |
import torch.nn.functional as F
|
| 10 |
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
| 11 |
from diffusers.models.modeling_utils import ModelMixin
|
| 12 |
+
from diffusers.models.transformers.dit_transformer_2d import DiTTransformer2DModel
|
| 13 |
from torch import nn
|
| 14 |
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def build_dit_config(
|
| 17 |
*,
|
| 18 |
depth: int = 28,
|
|
|
|
| 48 |
}
|
| 49 |
|
| 50 |
|
| 51 |
+
_PREFIX_KEYS = ("transformer.", "t_tgt_embedder.")
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def _wrap_dit_state_dict(state_dict: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]:
|
| 55 |
+
return {k if k.startswith(_PREFIX_KEYS) else f"transformer.{k}": v for k, v in state_dict.items()}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
def forward_dit_output(
|
| 59 |
+
transformer: DiTTransformer2DModel,
|
| 60 |
hidden_states: torch.Tensor,
|
| 61 |
timestep: Optional[torch.Tensor],
|
| 62 |
class_labels: torch.Tensor,
|
|
|
|
| 106 |
)
|
| 107 |
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
def split_output_channels(output: torch.Tensor, in_channels: int) -> torch.Tensor:
|
| 110 |
if output.shape[1] == in_channels:
|
| 111 |
return output
|
|
|
|
| 141 |
return self.mlp(emb.to(dtype=weight_dtype))
|
| 142 |
|
| 143 |
|
| 144 |
+
class _AFMCheckpointMixin(ModelMixin):
|
| 145 |
+
def _fix_state_dict_keys_on_load(self, state_dict):
|
| 146 |
+
wrapped = _wrap_dit_state_dict(state_dict)
|
| 147 |
+
state_dict.clear()
|
| 148 |
+
state_dict.update(wrapped)
|
| 149 |
+
super()._fix_state_dict_keys_on_load(state_dict)
|
| 150 |
+
|
| 151 |
+
|
| 152 |
class Generator(nn.Module):
|
| 153 |
def __init__(
|
| 154 |
self,
|
|
|
|
| 169 |
super().__init__()
|
| 170 |
self.use_t_src = use_t_src
|
| 171 |
self.use_t_tgt = use_t_tgt
|
| 172 |
+
self.transformer = DiTTransformer2DModel(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
**build_dit_config(
|
| 174 |
depth=depth,
|
| 175 |
hidden_size=hidden_size,
|
|
|
|
| 189 |
nn.init.normal_(self.t_tgt_embedder.mlp[2].weight, std=0.02)
|
| 190 |
|
| 191 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 192 |
+
if self.use_t_tgt and not any(key.startswith("t_tgt_embedder.") for key in state_dict):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
strict = False
|
| 194 |
+
return super().load_state_dict(_wrap_dit_state_dict(state_dict), strict=strict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
def forward(self, x, y, t_src=None, t_tgt=None):
|
| 197 |
if self.use_t_src and t_src is not None:
|
|
|
|
| 225 |
):
|
| 226 |
super().__init__()
|
| 227 |
self.repeat = repeat
|
| 228 |
+
self.transformer = DiTTransformer2DModel(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
**build_dit_config(
|
| 230 |
depth=depth,
|
| 231 |
hidden_size=hidden_size,
|
|
|
|
| 241 |
self.in_channels = in_channels
|
| 242 |
|
| 243 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 244 |
+
return super().load_state_dict(_wrap_dit_state_dict(state_dict), strict=strict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
def forward(self, x, y, *args, **kwargs):
|
| 247 |
hidden_states = self.transformer.pos_embed(x)
|
|
|
|
| 285 |
return split_output_channels(output, self.in_channels)
|
| 286 |
|
| 287 |
|
| 288 |
+
class AFMGenerator2DModel(_AFMCheckpointMixin, Generator, ConfigMixin):
|
| 289 |
config_name = "config.json"
|
| 290 |
|
| 291 |
@register_to_config
|
|
|
|
| 326 |
)
|
| 327 |
|
| 328 |
|
| 329 |
+
class AFMGeneratorDeep2DModel(_AFMCheckpointMixin, GeneratorDeep, ConfigMixin):
|
| 330 |
config_name = "config.json"
|
| 331 |
|
| 332 |
@register_to_config
|
AFM-L-2-1NFE-noguide/generator/config.json
CHANGED
|
@@ -1,6 +1,22 @@
|
|
| 1 |
{
|
| 2 |
-
"_class_name": "
|
| 3 |
"_diffusers_version": "0.36.0",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
"model_type": "AFM-L/2",
|
| 5 |
"architecture": "standard",
|
| 6 |
"repeat": 1,
|
|
@@ -12,9 +28,7 @@
|
|
| 12 |
"class_dropout_prob": 0.0,
|
| 13 |
"input_size": 32,
|
| 14 |
"num_classes": 1000,
|
| 15 |
-
"in_channels": 4,
|
| 16 |
"depth": 24,
|
| 17 |
"hidden_size": 1024,
|
| 18 |
-
"patch_size": 2,
|
| 19 |
"num_heads": 16
|
| 20 |
}
|
|
|
|
| 1 |
{
|
| 2 |
+
"_class_name": "DiTTransformer2DModel",
|
| 3 |
"_diffusers_version": "0.36.0",
|
| 4 |
+
"sample_size": 32,
|
| 5 |
+
"num_layers": 24,
|
| 6 |
+
"num_attention_heads": 16,
|
| 7 |
+
"attention_head_dim": 64,
|
| 8 |
+
"in_channels": 4,
|
| 9 |
+
"out_channels": 4,
|
| 10 |
+
"patch_size": 2,
|
| 11 |
+
"attention_bias": true,
|
| 12 |
+
"activation_fn": "gelu-approximate",
|
| 13 |
+
"num_embeds_ada_norm": 1000,
|
| 14 |
+
"norm_type": "ada_norm_zero",
|
| 15 |
+
"norm_elementwise_affine": false,
|
| 16 |
+
"dropout": 0.0,
|
| 17 |
+
"norm_num_groups": 32,
|
| 18 |
+
"norm_eps": 1e-05,
|
| 19 |
+
"upcast_attention": false,
|
| 20 |
"model_type": "AFM-L/2",
|
| 21 |
"architecture": "standard",
|
| 22 |
"repeat": 1,
|
|
|
|
| 28 |
"class_dropout_prob": 0.0,
|
| 29 |
"input_size": 32,
|
| 30 |
"num_classes": 1000,
|
|
|
|
| 31 |
"depth": 24,
|
| 32 |
"hidden_size": 1024,
|
|
|
|
| 33 |
"num_heads": 16
|
| 34 |
}
|
AFM-L-2-1NFE-noguide/generator/diffusion_pytorch_model.safetensors
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e8fb9aadfdae6bbcfa0334d7769414bc9cecba5bfd106db0aeaa082a34f9b329
|
| 3 |
+
size 2046429144
|
AFM-L-2-1NFE-noguide/generator/modeling_afm.py
CHANGED
|
@@ -2,7 +2,6 @@
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
-
import copy
|
| 6 |
import math
|
| 7 |
from typing import Any, Dict, Optional
|
| 8 |
|
|
@@ -10,15 +9,10 @@ import torch
|
|
| 10 |
import torch.nn.functional as F
|
| 11 |
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
| 12 |
from diffusers.models.modeling_utils import ModelMixin
|
|
|
|
| 13 |
from torch import nn
|
| 14 |
|
| 15 |
|
| 16 |
-
def get_dit_transformer_class():
|
| 17 |
-
from diffusers.models.transformers.dit_transformer_2d import DiTTransformer2DModel
|
| 18 |
-
|
| 19 |
-
return DiTTransformer2DModel
|
| 20 |
-
|
| 21 |
-
|
| 22 |
def build_dit_config(
|
| 23 |
*,
|
| 24 |
depth: int = 28,
|
|
@@ -54,110 +48,15 @@ def build_dit_config(
|
|
| 54 |
}
|
| 55 |
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
if "t_embedder.mlp.0.weight" in converted:
|
| 64 |
-
timestep_weights = {
|
| 65 |
-
"linear_1.weight": converted.pop("t_embedder.mlp.0.weight"),
|
| 66 |
-
"linear_1.bias": converted.pop("t_embedder.mlp.0.bias"),
|
| 67 |
-
"linear_2.weight": converted.pop("t_embedder.mlp.2.weight"),
|
| 68 |
-
"linear_2.bias": converted.pop("t_embedder.mlp.2.bias"),
|
| 69 |
-
}
|
| 70 |
-
else:
|
| 71 |
-
timestep_weights = None
|
| 72 |
-
class_embedding = converted.pop("y_embedder.embedding_table.weight")
|
| 73 |
-
if class_embedding.shape[0] == 1000:
|
| 74 |
-
null_class = torch.zeros(1, class_embedding.shape[1], dtype=class_embedding.dtype)
|
| 75 |
-
class_embedding = torch.cat([class_embedding, null_class], dim=0)
|
| 76 |
-
|
| 77 |
-
for block_idx in range(depth):
|
| 78 |
-
if timestep_weights is not None:
|
| 79 |
-
for key, tensor in timestep_weights.items():
|
| 80 |
-
converted[f"transformer_blocks.{block_idx}.norm1.emb.timestep_embedder.{key}"] = tensor.clone()
|
| 81 |
-
converted[f"transformer_blocks.{block_idx}.norm1.emb.class_embedder.embedding_table.weight"] = (
|
| 82 |
-
class_embedding.clone()
|
| 83 |
-
)
|
| 84 |
-
converted[f"transformer_blocks.{block_idx}.norm1.linear.weight"] = converted[
|
| 85 |
-
f"blocks.{block_idx}.adaLN_modulation.1.weight"
|
| 86 |
-
]
|
| 87 |
-
converted[f"transformer_blocks.{block_idx}.norm1.linear.bias"] = converted[
|
| 88 |
-
f"blocks.{block_idx}.adaLN_modulation.1.bias"
|
| 89 |
-
]
|
| 90 |
-
|
| 91 |
-
q, k, v = torch.chunk(converted[f"blocks.{block_idx}.attn.qkv.weight"], 3, dim=0)
|
| 92 |
-
q_bias, k_bias, v_bias = torch.chunk(converted[f"blocks.{block_idx}.attn.qkv.bias"], 3, dim=0)
|
| 93 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_q.weight"] = q
|
| 94 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_q.bias"] = q_bias
|
| 95 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_k.weight"] = k
|
| 96 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_k.bias"] = k_bias
|
| 97 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_v.weight"] = v
|
| 98 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_v.bias"] = v_bias
|
| 99 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_out.0.weight"] = converted[
|
| 100 |
-
f"blocks.{block_idx}.attn.proj.weight"
|
| 101 |
-
]
|
| 102 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_out.0.bias"] = converted[
|
| 103 |
-
f"blocks.{block_idx}.attn.proj.bias"
|
| 104 |
-
]
|
| 105 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.0.proj.weight"] = converted[
|
| 106 |
-
f"blocks.{block_idx}.mlp.fc1.weight"
|
| 107 |
-
]
|
| 108 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.0.proj.bias"] = converted[
|
| 109 |
-
f"blocks.{block_idx}.mlp.fc1.bias"
|
| 110 |
-
]
|
| 111 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.2.weight"] = converted[
|
| 112 |
-
f"blocks.{block_idx}.mlp.fc2.weight"
|
| 113 |
-
]
|
| 114 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.2.bias"] = converted[
|
| 115 |
-
f"blocks.{block_idx}.mlp.fc2.bias"
|
| 116 |
-
]
|
| 117 |
-
|
| 118 |
-
for suffix in (
|
| 119 |
-
"attn.qkv.weight",
|
| 120 |
-
"attn.qkv.bias",
|
| 121 |
-
"attn.proj.weight",
|
| 122 |
-
"attn.proj.bias",
|
| 123 |
-
"mlp.fc1.weight",
|
| 124 |
-
"mlp.fc1.bias",
|
| 125 |
-
"mlp.fc2.weight",
|
| 126 |
-
"mlp.fc2.bias",
|
| 127 |
-
"adaLN_modulation.1.weight",
|
| 128 |
-
"adaLN_modulation.1.bias",
|
| 129 |
-
):
|
| 130 |
-
converted.pop(f"blocks.{block_idx}.{suffix}", None)
|
| 131 |
-
|
| 132 |
-
converted["proj_out_1.weight"] = converted.pop("final_layer.adaLN_modulation.1.weight")
|
| 133 |
-
converted["proj_out_1.bias"] = converted.pop("final_layer.adaLN_modulation.1.bias")
|
| 134 |
-
converted["proj_out_2.weight"] = converted.pop("final_layer.linear.weight")
|
| 135 |
-
converted["proj_out_2.bias"] = converted.pop("final_layer.linear.bias")
|
| 136 |
-
|
| 137 |
-
converted.pop("pos_embed", None)
|
| 138 |
-
for block_idx in range(depth):
|
| 139 |
-
for suffix in ("norm1.weight", "norm1.bias", "norm2.weight", "norm2.bias"):
|
| 140 |
-
converted.pop(f"blocks.{block_idx}.{suffix}", None)
|
| 141 |
-
|
| 142 |
-
return {key: tensor.detach().clone().contiguous() for key, tensor in converted.items()}
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
def load_legacy_dit_state_dict(
|
| 146 |
-
state_dict: Dict[str, torch.Tensor],
|
| 147 |
-
depth: int,
|
| 148 |
-
patch_size: int,
|
| 149 |
-
hidden_size: int,
|
| 150 |
-
num_heads: int,
|
| 151 |
-
) -> Dict[str, torch.Tensor]:
|
| 152 |
-
if "transformer_blocks.0.attn1.to_q.weight" in state_dict:
|
| 153 |
-
return state_dict
|
| 154 |
-
if any(key.startswith("blocks.") for key in state_dict):
|
| 155 |
-
return _convert_legacy_dit_state_dict(state_dict, depth)
|
| 156 |
-
return state_dict
|
| 157 |
|
| 158 |
|
| 159 |
def forward_dit_output(
|
| 160 |
-
transformer,
|
| 161 |
hidden_states: torch.Tensor,
|
| 162 |
timestep: Optional[torch.Tensor],
|
| 163 |
class_labels: torch.Tensor,
|
|
@@ -207,23 +106,6 @@ def forward_dit_output(
|
|
| 207 |
)
|
| 208 |
|
| 209 |
|
| 210 |
-
def remap_transformer_state_dict(
|
| 211 |
-
state_dict: Dict[str, torch.Tensor],
|
| 212 |
-
legacy_kwargs: Dict[str, int],
|
| 213 |
-
extra_prefixes: tuple[str, ...] = (),
|
| 214 |
-
) -> Dict[str, torch.Tensor]:
|
| 215 |
-
state_dict = load_legacy_dit_state_dict(state_dict, **legacy_kwargs)
|
| 216 |
-
remapped: Dict[str, torch.Tensor] = {}
|
| 217 |
-
for key, value in state_dict.items():
|
| 218 |
-
if any(key.startswith(prefix) for prefix in extra_prefixes):
|
| 219 |
-
remapped[key] = value
|
| 220 |
-
elif key.startswith("transformer."):
|
| 221 |
-
remapped[key] = value
|
| 222 |
-
else:
|
| 223 |
-
remapped[f"transformer.{key}"] = value
|
| 224 |
-
return remapped
|
| 225 |
-
|
| 226 |
-
|
| 227 |
def split_output_channels(output: torch.Tensor, in_channels: int) -> torch.Tensor:
|
| 228 |
if output.shape[1] == in_channels:
|
| 229 |
return output
|
|
@@ -259,6 +141,14 @@ class TimestepEmbedder(nn.Module):
|
|
| 259 |
return self.mlp(emb.to(dtype=weight_dtype))
|
| 260 |
|
| 261 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
class Generator(nn.Module):
|
| 263 |
def __init__(
|
| 264 |
self,
|
|
@@ -279,14 +169,7 @@ class Generator(nn.Module):
|
|
| 279 |
super().__init__()
|
| 280 |
self.use_t_src = use_t_src
|
| 281 |
self.use_t_tgt = use_t_tgt
|
| 282 |
-
self.
|
| 283 |
-
"depth": depth,
|
| 284 |
-
"hidden_size": hidden_size,
|
| 285 |
-
"patch_size": patch_size,
|
| 286 |
-
"num_heads": num_heads,
|
| 287 |
-
}
|
| 288 |
-
dit_cls = get_dit_transformer_class()
|
| 289 |
-
self.transformer = dit_cls(
|
| 290 |
**build_dit_config(
|
| 291 |
depth=depth,
|
| 292 |
hidden_size=hidden_size,
|
|
@@ -306,20 +189,9 @@ class Generator(nn.Module):
|
|
| 306 |
nn.init.normal_(self.t_tgt_embedder.mlp[2].weight, std=0.02)
|
| 307 |
|
| 308 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 309 |
-
|
| 310 |
-
has_extra = any(key.startswith(extra) for key in state_dict)
|
| 311 |
-
has_t_embedder = any("t_embedder" in key for key in state_dict)
|
| 312 |
-
remapped = remap_transformer_state_dict(state_dict, self._legacy_kwargs, extra_prefixes=extra)
|
| 313 |
-
if self.use_t_tgt and not has_extra:
|
| 314 |
-
strict = False
|
| 315 |
-
if not has_t_embedder:
|
| 316 |
strict = False
|
| 317 |
-
|
| 318 |
-
if not has_t_embedder:
|
| 319 |
-
for name, param in self.named_parameters():
|
| 320 |
-
if "timestep_embedder" in name:
|
| 321 |
-
param.data.zero_()
|
| 322 |
-
return out
|
| 323 |
|
| 324 |
def forward(self, x, y, t_src=None, t_tgt=None):
|
| 325 |
if self.use_t_src and t_src is not None:
|
|
@@ -353,14 +225,7 @@ class GeneratorDeep(nn.Module):
|
|
| 353 |
):
|
| 354 |
super().__init__()
|
| 355 |
self.repeat = repeat
|
| 356 |
-
self.
|
| 357 |
-
"depth": depth,
|
| 358 |
-
"hidden_size": hidden_size,
|
| 359 |
-
"patch_size": patch_size,
|
| 360 |
-
"num_heads": num_heads,
|
| 361 |
-
}
|
| 362 |
-
dit_cls = get_dit_transformer_class()
|
| 363 |
-
self.transformer = dit_cls(
|
| 364 |
**build_dit_config(
|
| 365 |
depth=depth,
|
| 366 |
hidden_size=hidden_size,
|
|
@@ -376,16 +241,7 @@ class GeneratorDeep(nn.Module):
|
|
| 376 |
self.in_channels = in_channels
|
| 377 |
|
| 378 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 379 |
-
|
| 380 |
-
has_t_embedder = any("t_embedder" in key for key in state_dict)
|
| 381 |
-
if not has_t_embedder:
|
| 382 |
-
strict = False
|
| 383 |
-
out = super().load_state_dict(remapped, strict=strict)
|
| 384 |
-
if not has_t_embedder:
|
| 385 |
-
for name, param in self.named_parameters():
|
| 386 |
-
if "timestep_embedder" in name:
|
| 387 |
-
param.data.zero_()
|
| 388 |
-
return out
|
| 389 |
|
| 390 |
def forward(self, x, y, *args, **kwargs):
|
| 391 |
hidden_states = self.transformer.pos_embed(x)
|
|
@@ -429,7 +285,7 @@ class GeneratorDeep(nn.Module):
|
|
| 429 |
return split_output_channels(output, self.in_channels)
|
| 430 |
|
| 431 |
|
| 432 |
-
class AFMGenerator2DModel(
|
| 433 |
config_name = "config.json"
|
| 434 |
|
| 435 |
@register_to_config
|
|
@@ -470,7 +326,7 @@ class AFMGenerator2DModel(Generator, ModelMixin, ConfigMixin):
|
|
| 470 |
)
|
| 471 |
|
| 472 |
|
| 473 |
-
class AFMGeneratorDeep2DModel(
|
| 474 |
config_name = "config.json"
|
| 475 |
|
| 476 |
@register_to_config
|
|
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
|
|
|
| 5 |
import math
|
| 6 |
from typing import Any, Dict, Optional
|
| 7 |
|
|
|
|
| 9 |
import torch.nn.functional as F
|
| 10 |
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
| 11 |
from diffusers.models.modeling_utils import ModelMixin
|
| 12 |
+
from diffusers.models.transformers.dit_transformer_2d import DiTTransformer2DModel
|
| 13 |
from torch import nn
|
| 14 |
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def build_dit_config(
|
| 17 |
*,
|
| 18 |
depth: int = 28,
|
|
|
|
| 48 |
}
|
| 49 |
|
| 50 |
|
| 51 |
+
_PREFIX_KEYS = ("transformer.", "t_tgt_embedder.")
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def _wrap_dit_state_dict(state_dict: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]:
|
| 55 |
+
return {k if k.startswith(_PREFIX_KEYS) else f"transformer.{k}": v for k, v in state_dict.items()}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
def forward_dit_output(
|
| 59 |
+
transformer: DiTTransformer2DModel,
|
| 60 |
hidden_states: torch.Tensor,
|
| 61 |
timestep: Optional[torch.Tensor],
|
| 62 |
class_labels: torch.Tensor,
|
|
|
|
| 106 |
)
|
| 107 |
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
def split_output_channels(output: torch.Tensor, in_channels: int) -> torch.Tensor:
|
| 110 |
if output.shape[1] == in_channels:
|
| 111 |
return output
|
|
|
|
| 141 |
return self.mlp(emb.to(dtype=weight_dtype))
|
| 142 |
|
| 143 |
|
| 144 |
+
class _AFMCheckpointMixin(ModelMixin):
|
| 145 |
+
def _fix_state_dict_keys_on_load(self, state_dict):
|
| 146 |
+
wrapped = _wrap_dit_state_dict(state_dict)
|
| 147 |
+
state_dict.clear()
|
| 148 |
+
state_dict.update(wrapped)
|
| 149 |
+
super()._fix_state_dict_keys_on_load(state_dict)
|
| 150 |
+
|
| 151 |
+
|
| 152 |
class Generator(nn.Module):
|
| 153 |
def __init__(
|
| 154 |
self,
|
|
|
|
| 169 |
super().__init__()
|
| 170 |
self.use_t_src = use_t_src
|
| 171 |
self.use_t_tgt = use_t_tgt
|
| 172 |
+
self.transformer = DiTTransformer2DModel(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
**build_dit_config(
|
| 174 |
depth=depth,
|
| 175 |
hidden_size=hidden_size,
|
|
|
|
| 189 |
nn.init.normal_(self.t_tgt_embedder.mlp[2].weight, std=0.02)
|
| 190 |
|
| 191 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 192 |
+
if self.use_t_tgt and not any(key.startswith("t_tgt_embedder.") for key in state_dict):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
strict = False
|
| 194 |
+
return super().load_state_dict(_wrap_dit_state_dict(state_dict), strict=strict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
def forward(self, x, y, t_src=None, t_tgt=None):
|
| 197 |
if self.use_t_src and t_src is not None:
|
|
|
|
| 225 |
):
|
| 226 |
super().__init__()
|
| 227 |
self.repeat = repeat
|
| 228 |
+
self.transformer = DiTTransformer2DModel(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
**build_dit_config(
|
| 230 |
depth=depth,
|
| 231 |
hidden_size=hidden_size,
|
|
|
|
| 241 |
self.in_channels = in_channels
|
| 242 |
|
| 243 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 244 |
+
return super().load_state_dict(_wrap_dit_state_dict(state_dict), strict=strict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
def forward(self, x, y, *args, **kwargs):
|
| 247 |
hidden_states = self.transformer.pos_embed(x)
|
|
|
|
| 285 |
return split_output_channels(output, self.in_channels)
|
| 286 |
|
| 287 |
|
| 288 |
+
class AFMGenerator2DModel(_AFMCheckpointMixin, Generator, ConfigMixin):
|
| 289 |
config_name = "config.json"
|
| 290 |
|
| 291 |
@register_to_config
|
|
|
|
| 326 |
)
|
| 327 |
|
| 328 |
|
| 329 |
+
class AFMGeneratorDeep2DModel(_AFMCheckpointMixin, GeneratorDeep, ConfigMixin):
|
| 330 |
config_name = "config.json"
|
| 331 |
|
| 332 |
@register_to_config
|
AFM-M-2-1NFE-guided/generator/config.json
CHANGED
|
@@ -1,6 +1,22 @@
|
|
| 1 |
{
|
| 2 |
-
"_class_name": "
|
| 3 |
"_diffusers_version": "0.36.0",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
"model_type": "AFM-M/2",
|
| 5 |
"architecture": "standard",
|
| 6 |
"repeat": 1,
|
|
@@ -12,9 +28,7 @@
|
|
| 12 |
"class_dropout_prob": 0.0,
|
| 13 |
"input_size": 32,
|
| 14 |
"num_classes": 1000,
|
| 15 |
-
"in_channels": 4,
|
| 16 |
"depth": 16,
|
| 17 |
"hidden_size": 1024,
|
| 18 |
-
"patch_size": 2,
|
| 19 |
"num_heads": 16
|
| 20 |
}
|
|
|
|
| 1 |
{
|
| 2 |
+
"_class_name": "DiTTransformer2DModel",
|
| 3 |
"_diffusers_version": "0.36.0",
|
| 4 |
+
"sample_size": 32,
|
| 5 |
+
"num_layers": 16,
|
| 6 |
+
"num_attention_heads": 16,
|
| 7 |
+
"attention_head_dim": 64,
|
| 8 |
+
"in_channels": 4,
|
| 9 |
+
"out_channels": 4,
|
| 10 |
+
"patch_size": 2,
|
| 11 |
+
"attention_bias": true,
|
| 12 |
+
"activation_fn": "gelu-approximate",
|
| 13 |
+
"num_embeds_ada_norm": 1000,
|
| 14 |
+
"norm_type": "ada_norm_zero",
|
| 15 |
+
"norm_elementwise_affine": false,
|
| 16 |
+
"dropout": 0.0,
|
| 17 |
+
"norm_num_groups": 32,
|
| 18 |
+
"norm_eps": 1e-05,
|
| 19 |
+
"upcast_attention": false,
|
| 20 |
"model_type": "AFM-M/2",
|
| 21 |
"architecture": "standard",
|
| 22 |
"repeat": 1,
|
|
|
|
| 28 |
"class_dropout_prob": 0.0,
|
| 29 |
"input_size": 32,
|
| 30 |
"num_classes": 1000,
|
|
|
|
| 31 |
"depth": 16,
|
| 32 |
"hidden_size": 1024,
|
|
|
|
| 33 |
"num_heads": 16
|
| 34 |
}
|
AFM-M-2-1NFE-guided/generator/diffusion_pytorch_model.safetensors
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4350f61ca9852882a8123ad337705fe805a7f419acbc715c3aae2d9d8c36684c
|
| 3 |
+
size 1367130048
|
AFM-M-2-1NFE-guided/generator/modeling_afm.py
CHANGED
|
@@ -2,7 +2,6 @@
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
-
import copy
|
| 6 |
import math
|
| 7 |
from typing import Any, Dict, Optional
|
| 8 |
|
|
@@ -10,15 +9,10 @@ import torch
|
|
| 10 |
import torch.nn.functional as F
|
| 11 |
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
| 12 |
from diffusers.models.modeling_utils import ModelMixin
|
|
|
|
| 13 |
from torch import nn
|
| 14 |
|
| 15 |
|
| 16 |
-
def get_dit_transformer_class():
|
| 17 |
-
from diffusers.models.transformers.dit_transformer_2d import DiTTransformer2DModel
|
| 18 |
-
|
| 19 |
-
return DiTTransformer2DModel
|
| 20 |
-
|
| 21 |
-
|
| 22 |
def build_dit_config(
|
| 23 |
*,
|
| 24 |
depth: int = 28,
|
|
@@ -54,110 +48,15 @@ def build_dit_config(
|
|
| 54 |
}
|
| 55 |
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
if "t_embedder.mlp.0.weight" in converted:
|
| 64 |
-
timestep_weights = {
|
| 65 |
-
"linear_1.weight": converted.pop("t_embedder.mlp.0.weight"),
|
| 66 |
-
"linear_1.bias": converted.pop("t_embedder.mlp.0.bias"),
|
| 67 |
-
"linear_2.weight": converted.pop("t_embedder.mlp.2.weight"),
|
| 68 |
-
"linear_2.bias": converted.pop("t_embedder.mlp.2.bias"),
|
| 69 |
-
}
|
| 70 |
-
else:
|
| 71 |
-
timestep_weights = None
|
| 72 |
-
class_embedding = converted.pop("y_embedder.embedding_table.weight")
|
| 73 |
-
if class_embedding.shape[0] == 1000:
|
| 74 |
-
null_class = torch.zeros(1, class_embedding.shape[1], dtype=class_embedding.dtype)
|
| 75 |
-
class_embedding = torch.cat([class_embedding, null_class], dim=0)
|
| 76 |
-
|
| 77 |
-
for block_idx in range(depth):
|
| 78 |
-
if timestep_weights is not None:
|
| 79 |
-
for key, tensor in timestep_weights.items():
|
| 80 |
-
converted[f"transformer_blocks.{block_idx}.norm1.emb.timestep_embedder.{key}"] = tensor.clone()
|
| 81 |
-
converted[f"transformer_blocks.{block_idx}.norm1.emb.class_embedder.embedding_table.weight"] = (
|
| 82 |
-
class_embedding.clone()
|
| 83 |
-
)
|
| 84 |
-
converted[f"transformer_blocks.{block_idx}.norm1.linear.weight"] = converted[
|
| 85 |
-
f"blocks.{block_idx}.adaLN_modulation.1.weight"
|
| 86 |
-
]
|
| 87 |
-
converted[f"transformer_blocks.{block_idx}.norm1.linear.bias"] = converted[
|
| 88 |
-
f"blocks.{block_idx}.adaLN_modulation.1.bias"
|
| 89 |
-
]
|
| 90 |
-
|
| 91 |
-
q, k, v = torch.chunk(converted[f"blocks.{block_idx}.attn.qkv.weight"], 3, dim=0)
|
| 92 |
-
q_bias, k_bias, v_bias = torch.chunk(converted[f"blocks.{block_idx}.attn.qkv.bias"], 3, dim=0)
|
| 93 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_q.weight"] = q
|
| 94 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_q.bias"] = q_bias
|
| 95 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_k.weight"] = k
|
| 96 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_k.bias"] = k_bias
|
| 97 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_v.weight"] = v
|
| 98 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_v.bias"] = v_bias
|
| 99 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_out.0.weight"] = converted[
|
| 100 |
-
f"blocks.{block_idx}.attn.proj.weight"
|
| 101 |
-
]
|
| 102 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_out.0.bias"] = converted[
|
| 103 |
-
f"blocks.{block_idx}.attn.proj.bias"
|
| 104 |
-
]
|
| 105 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.0.proj.weight"] = converted[
|
| 106 |
-
f"blocks.{block_idx}.mlp.fc1.weight"
|
| 107 |
-
]
|
| 108 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.0.proj.bias"] = converted[
|
| 109 |
-
f"blocks.{block_idx}.mlp.fc1.bias"
|
| 110 |
-
]
|
| 111 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.2.weight"] = converted[
|
| 112 |
-
f"blocks.{block_idx}.mlp.fc2.weight"
|
| 113 |
-
]
|
| 114 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.2.bias"] = converted[
|
| 115 |
-
f"blocks.{block_idx}.mlp.fc2.bias"
|
| 116 |
-
]
|
| 117 |
-
|
| 118 |
-
for suffix in (
|
| 119 |
-
"attn.qkv.weight",
|
| 120 |
-
"attn.qkv.bias",
|
| 121 |
-
"attn.proj.weight",
|
| 122 |
-
"attn.proj.bias",
|
| 123 |
-
"mlp.fc1.weight",
|
| 124 |
-
"mlp.fc1.bias",
|
| 125 |
-
"mlp.fc2.weight",
|
| 126 |
-
"mlp.fc2.bias",
|
| 127 |
-
"adaLN_modulation.1.weight",
|
| 128 |
-
"adaLN_modulation.1.bias",
|
| 129 |
-
):
|
| 130 |
-
converted.pop(f"blocks.{block_idx}.{suffix}", None)
|
| 131 |
-
|
| 132 |
-
converted["proj_out_1.weight"] = converted.pop("final_layer.adaLN_modulation.1.weight")
|
| 133 |
-
converted["proj_out_1.bias"] = converted.pop("final_layer.adaLN_modulation.1.bias")
|
| 134 |
-
converted["proj_out_2.weight"] = converted.pop("final_layer.linear.weight")
|
| 135 |
-
converted["proj_out_2.bias"] = converted.pop("final_layer.linear.bias")
|
| 136 |
-
|
| 137 |
-
converted.pop("pos_embed", None)
|
| 138 |
-
for block_idx in range(depth):
|
| 139 |
-
for suffix in ("norm1.weight", "norm1.bias", "norm2.weight", "norm2.bias"):
|
| 140 |
-
converted.pop(f"blocks.{block_idx}.{suffix}", None)
|
| 141 |
-
|
| 142 |
-
return {key: tensor.detach().clone().contiguous() for key, tensor in converted.items()}
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
def load_legacy_dit_state_dict(
|
| 146 |
-
state_dict: Dict[str, torch.Tensor],
|
| 147 |
-
depth: int,
|
| 148 |
-
patch_size: int,
|
| 149 |
-
hidden_size: int,
|
| 150 |
-
num_heads: int,
|
| 151 |
-
) -> Dict[str, torch.Tensor]:
|
| 152 |
-
if "transformer_blocks.0.attn1.to_q.weight" in state_dict:
|
| 153 |
-
return state_dict
|
| 154 |
-
if any(key.startswith("blocks.") for key in state_dict):
|
| 155 |
-
return _convert_legacy_dit_state_dict(state_dict, depth)
|
| 156 |
-
return state_dict
|
| 157 |
|
| 158 |
|
| 159 |
def forward_dit_output(
|
| 160 |
-
transformer,
|
| 161 |
hidden_states: torch.Tensor,
|
| 162 |
timestep: Optional[torch.Tensor],
|
| 163 |
class_labels: torch.Tensor,
|
|
@@ -207,23 +106,6 @@ def forward_dit_output(
|
|
| 207 |
)
|
| 208 |
|
| 209 |
|
| 210 |
-
def remap_transformer_state_dict(
|
| 211 |
-
state_dict: Dict[str, torch.Tensor],
|
| 212 |
-
legacy_kwargs: Dict[str, int],
|
| 213 |
-
extra_prefixes: tuple[str, ...] = (),
|
| 214 |
-
) -> Dict[str, torch.Tensor]:
|
| 215 |
-
state_dict = load_legacy_dit_state_dict(state_dict, **legacy_kwargs)
|
| 216 |
-
remapped: Dict[str, torch.Tensor] = {}
|
| 217 |
-
for key, value in state_dict.items():
|
| 218 |
-
if any(key.startswith(prefix) for prefix in extra_prefixes):
|
| 219 |
-
remapped[key] = value
|
| 220 |
-
elif key.startswith("transformer."):
|
| 221 |
-
remapped[key] = value
|
| 222 |
-
else:
|
| 223 |
-
remapped[f"transformer.{key}"] = value
|
| 224 |
-
return remapped
|
| 225 |
-
|
| 226 |
-
|
| 227 |
def split_output_channels(output: torch.Tensor, in_channels: int) -> torch.Tensor:
|
| 228 |
if output.shape[1] == in_channels:
|
| 229 |
return output
|
|
@@ -259,6 +141,14 @@ class TimestepEmbedder(nn.Module):
|
|
| 259 |
return self.mlp(emb.to(dtype=weight_dtype))
|
| 260 |
|
| 261 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
class Generator(nn.Module):
|
| 263 |
def __init__(
|
| 264 |
self,
|
|
@@ -279,14 +169,7 @@ class Generator(nn.Module):
|
|
| 279 |
super().__init__()
|
| 280 |
self.use_t_src = use_t_src
|
| 281 |
self.use_t_tgt = use_t_tgt
|
| 282 |
-
self.
|
| 283 |
-
"depth": depth,
|
| 284 |
-
"hidden_size": hidden_size,
|
| 285 |
-
"patch_size": patch_size,
|
| 286 |
-
"num_heads": num_heads,
|
| 287 |
-
}
|
| 288 |
-
dit_cls = get_dit_transformer_class()
|
| 289 |
-
self.transformer = dit_cls(
|
| 290 |
**build_dit_config(
|
| 291 |
depth=depth,
|
| 292 |
hidden_size=hidden_size,
|
|
@@ -306,20 +189,9 @@ class Generator(nn.Module):
|
|
| 306 |
nn.init.normal_(self.t_tgt_embedder.mlp[2].weight, std=0.02)
|
| 307 |
|
| 308 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 309 |
-
|
| 310 |
-
has_extra = any(key.startswith(extra) for key in state_dict)
|
| 311 |
-
has_t_embedder = any("t_embedder" in key for key in state_dict)
|
| 312 |
-
remapped = remap_transformer_state_dict(state_dict, self._legacy_kwargs, extra_prefixes=extra)
|
| 313 |
-
if self.use_t_tgt and not has_extra:
|
| 314 |
-
strict = False
|
| 315 |
-
if not has_t_embedder:
|
| 316 |
strict = False
|
| 317 |
-
|
| 318 |
-
if not has_t_embedder:
|
| 319 |
-
for name, param in self.named_parameters():
|
| 320 |
-
if "timestep_embedder" in name:
|
| 321 |
-
param.data.zero_()
|
| 322 |
-
return out
|
| 323 |
|
| 324 |
def forward(self, x, y, t_src=None, t_tgt=None):
|
| 325 |
if self.use_t_src and t_src is not None:
|
|
@@ -353,14 +225,7 @@ class GeneratorDeep(nn.Module):
|
|
| 353 |
):
|
| 354 |
super().__init__()
|
| 355 |
self.repeat = repeat
|
| 356 |
-
self.
|
| 357 |
-
"depth": depth,
|
| 358 |
-
"hidden_size": hidden_size,
|
| 359 |
-
"patch_size": patch_size,
|
| 360 |
-
"num_heads": num_heads,
|
| 361 |
-
}
|
| 362 |
-
dit_cls = get_dit_transformer_class()
|
| 363 |
-
self.transformer = dit_cls(
|
| 364 |
**build_dit_config(
|
| 365 |
depth=depth,
|
| 366 |
hidden_size=hidden_size,
|
|
@@ -376,16 +241,7 @@ class GeneratorDeep(nn.Module):
|
|
| 376 |
self.in_channels = in_channels
|
| 377 |
|
| 378 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 379 |
-
|
| 380 |
-
has_t_embedder = any("t_embedder" in key for key in state_dict)
|
| 381 |
-
if not has_t_embedder:
|
| 382 |
-
strict = False
|
| 383 |
-
out = super().load_state_dict(remapped, strict=strict)
|
| 384 |
-
if not has_t_embedder:
|
| 385 |
-
for name, param in self.named_parameters():
|
| 386 |
-
if "timestep_embedder" in name:
|
| 387 |
-
param.data.zero_()
|
| 388 |
-
return out
|
| 389 |
|
| 390 |
def forward(self, x, y, *args, **kwargs):
|
| 391 |
hidden_states = self.transformer.pos_embed(x)
|
|
@@ -429,7 +285,7 @@ class GeneratorDeep(nn.Module):
|
|
| 429 |
return split_output_channels(output, self.in_channels)
|
| 430 |
|
| 431 |
|
| 432 |
-
class AFMGenerator2DModel(
|
| 433 |
config_name = "config.json"
|
| 434 |
|
| 435 |
@register_to_config
|
|
@@ -470,7 +326,7 @@ class AFMGenerator2DModel(Generator, ModelMixin, ConfigMixin):
|
|
| 470 |
)
|
| 471 |
|
| 472 |
|
| 473 |
-
class AFMGeneratorDeep2DModel(
|
| 474 |
config_name = "config.json"
|
| 475 |
|
| 476 |
@register_to_config
|
|
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
|
|
|
| 5 |
import math
|
| 6 |
from typing import Any, Dict, Optional
|
| 7 |
|
|
|
|
| 9 |
import torch.nn.functional as F
|
| 10 |
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
| 11 |
from diffusers.models.modeling_utils import ModelMixin
|
| 12 |
+
from diffusers.models.transformers.dit_transformer_2d import DiTTransformer2DModel
|
| 13 |
from torch import nn
|
| 14 |
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def build_dit_config(
|
| 17 |
*,
|
| 18 |
depth: int = 28,
|
|
|
|
| 48 |
}
|
| 49 |
|
| 50 |
|
| 51 |
+
_PREFIX_KEYS = ("transformer.", "t_tgt_embedder.")
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def _wrap_dit_state_dict(state_dict: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]:
|
| 55 |
+
return {k if k.startswith(_PREFIX_KEYS) else f"transformer.{k}": v for k, v in state_dict.items()}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
def forward_dit_output(
|
| 59 |
+
transformer: DiTTransformer2DModel,
|
| 60 |
hidden_states: torch.Tensor,
|
| 61 |
timestep: Optional[torch.Tensor],
|
| 62 |
class_labels: torch.Tensor,
|
|
|
|
| 106 |
)
|
| 107 |
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
def split_output_channels(output: torch.Tensor, in_channels: int) -> torch.Tensor:
|
| 110 |
if output.shape[1] == in_channels:
|
| 111 |
return output
|
|
|
|
| 141 |
return self.mlp(emb.to(dtype=weight_dtype))
|
| 142 |
|
| 143 |
|
| 144 |
+
class _AFMCheckpointMixin(ModelMixin):
|
| 145 |
+
def _fix_state_dict_keys_on_load(self, state_dict):
|
| 146 |
+
wrapped = _wrap_dit_state_dict(state_dict)
|
| 147 |
+
state_dict.clear()
|
| 148 |
+
state_dict.update(wrapped)
|
| 149 |
+
super()._fix_state_dict_keys_on_load(state_dict)
|
| 150 |
+
|
| 151 |
+
|
| 152 |
class Generator(nn.Module):
|
| 153 |
def __init__(
|
| 154 |
self,
|
|
|
|
| 169 |
super().__init__()
|
| 170 |
self.use_t_src = use_t_src
|
| 171 |
self.use_t_tgt = use_t_tgt
|
| 172 |
+
self.transformer = DiTTransformer2DModel(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
**build_dit_config(
|
| 174 |
depth=depth,
|
| 175 |
hidden_size=hidden_size,
|
|
|
|
| 189 |
nn.init.normal_(self.t_tgt_embedder.mlp[2].weight, std=0.02)
|
| 190 |
|
| 191 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 192 |
+
if self.use_t_tgt and not any(key.startswith("t_tgt_embedder.") for key in state_dict):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
strict = False
|
| 194 |
+
return super().load_state_dict(_wrap_dit_state_dict(state_dict), strict=strict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
def forward(self, x, y, t_src=None, t_tgt=None):
|
| 197 |
if self.use_t_src and t_src is not None:
|
|
|
|
| 225 |
):
|
| 226 |
super().__init__()
|
| 227 |
self.repeat = repeat
|
| 228 |
+
self.transformer = DiTTransformer2DModel(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
**build_dit_config(
|
| 230 |
depth=depth,
|
| 231 |
hidden_size=hidden_size,
|
|
|
|
| 241 |
self.in_channels = in_channels
|
| 242 |
|
| 243 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 244 |
+
return super().load_state_dict(_wrap_dit_state_dict(state_dict), strict=strict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
def forward(self, x, y, *args, **kwargs):
|
| 247 |
hidden_states = self.transformer.pos_embed(x)
|
|
|
|
| 285 |
return split_output_channels(output, self.in_channels)
|
| 286 |
|
| 287 |
|
| 288 |
+
class AFMGenerator2DModel(_AFMCheckpointMixin, Generator, ConfigMixin):
|
| 289 |
config_name = "config.json"
|
| 290 |
|
| 291 |
@register_to_config
|
|
|
|
| 326 |
)
|
| 327 |
|
| 328 |
|
| 329 |
+
class AFMGeneratorDeep2DModel(_AFMCheckpointMixin, GeneratorDeep, ConfigMixin):
|
| 330 |
config_name = "config.json"
|
| 331 |
|
| 332 |
@register_to_config
|
AFM-M-2-1NFE-noguide/generator/config.json
CHANGED
|
@@ -1,6 +1,22 @@
|
|
| 1 |
{
|
| 2 |
-
"_class_name": "
|
| 3 |
"_diffusers_version": "0.36.0",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
"model_type": "AFM-M/2",
|
| 5 |
"architecture": "standard",
|
| 6 |
"repeat": 1,
|
|
@@ -12,9 +28,7 @@
|
|
| 12 |
"class_dropout_prob": 0.0,
|
| 13 |
"input_size": 32,
|
| 14 |
"num_classes": 1000,
|
| 15 |
-
"in_channels": 4,
|
| 16 |
"depth": 16,
|
| 17 |
"hidden_size": 1024,
|
| 18 |
-
"patch_size": 2,
|
| 19 |
"num_heads": 16
|
| 20 |
}
|
|
|
|
| 1 |
{
|
| 2 |
+
"_class_name": "DiTTransformer2DModel",
|
| 3 |
"_diffusers_version": "0.36.0",
|
| 4 |
+
"sample_size": 32,
|
| 5 |
+
"num_layers": 16,
|
| 6 |
+
"num_attention_heads": 16,
|
| 7 |
+
"attention_head_dim": 64,
|
| 8 |
+
"in_channels": 4,
|
| 9 |
+
"out_channels": 4,
|
| 10 |
+
"patch_size": 2,
|
| 11 |
+
"attention_bias": true,
|
| 12 |
+
"activation_fn": "gelu-approximate",
|
| 13 |
+
"num_embeds_ada_norm": 1000,
|
| 14 |
+
"norm_type": "ada_norm_zero",
|
| 15 |
+
"norm_elementwise_affine": false,
|
| 16 |
+
"dropout": 0.0,
|
| 17 |
+
"norm_num_groups": 32,
|
| 18 |
+
"norm_eps": 1e-05,
|
| 19 |
+
"upcast_attention": false,
|
| 20 |
"model_type": "AFM-M/2",
|
| 21 |
"architecture": "standard",
|
| 22 |
"repeat": 1,
|
|
|
|
| 28 |
"class_dropout_prob": 0.0,
|
| 29 |
"input_size": 32,
|
| 30 |
"num_classes": 1000,
|
|
|
|
| 31 |
"depth": 16,
|
| 32 |
"hidden_size": 1024,
|
|
|
|
| 33 |
"num_heads": 16
|
| 34 |
}
|
AFM-M-2-1NFE-noguide/generator/diffusion_pytorch_model.safetensors
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:42a43983d07f79b9bb9a7d6de1ef1a3f3f201310f7fb30245973189e1a90dbf8
|
| 3 |
+
size 1367130048
|
AFM-M-2-1NFE-noguide/generator/modeling_afm.py
CHANGED
|
@@ -2,7 +2,6 @@
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
-
import copy
|
| 6 |
import math
|
| 7 |
from typing import Any, Dict, Optional
|
| 8 |
|
|
@@ -10,15 +9,10 @@ import torch
|
|
| 10 |
import torch.nn.functional as F
|
| 11 |
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
| 12 |
from diffusers.models.modeling_utils import ModelMixin
|
|
|
|
| 13 |
from torch import nn
|
| 14 |
|
| 15 |
|
| 16 |
-
def get_dit_transformer_class():
|
| 17 |
-
from diffusers.models.transformers.dit_transformer_2d import DiTTransformer2DModel
|
| 18 |
-
|
| 19 |
-
return DiTTransformer2DModel
|
| 20 |
-
|
| 21 |
-
|
| 22 |
def build_dit_config(
|
| 23 |
*,
|
| 24 |
depth: int = 28,
|
|
@@ -54,110 +48,15 @@ def build_dit_config(
|
|
| 54 |
}
|
| 55 |
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
if "t_embedder.mlp.0.weight" in converted:
|
| 64 |
-
timestep_weights = {
|
| 65 |
-
"linear_1.weight": converted.pop("t_embedder.mlp.0.weight"),
|
| 66 |
-
"linear_1.bias": converted.pop("t_embedder.mlp.0.bias"),
|
| 67 |
-
"linear_2.weight": converted.pop("t_embedder.mlp.2.weight"),
|
| 68 |
-
"linear_2.bias": converted.pop("t_embedder.mlp.2.bias"),
|
| 69 |
-
}
|
| 70 |
-
else:
|
| 71 |
-
timestep_weights = None
|
| 72 |
-
class_embedding = converted.pop("y_embedder.embedding_table.weight")
|
| 73 |
-
if class_embedding.shape[0] == 1000:
|
| 74 |
-
null_class = torch.zeros(1, class_embedding.shape[1], dtype=class_embedding.dtype)
|
| 75 |
-
class_embedding = torch.cat([class_embedding, null_class], dim=0)
|
| 76 |
-
|
| 77 |
-
for block_idx in range(depth):
|
| 78 |
-
if timestep_weights is not None:
|
| 79 |
-
for key, tensor in timestep_weights.items():
|
| 80 |
-
converted[f"transformer_blocks.{block_idx}.norm1.emb.timestep_embedder.{key}"] = tensor.clone()
|
| 81 |
-
converted[f"transformer_blocks.{block_idx}.norm1.emb.class_embedder.embedding_table.weight"] = (
|
| 82 |
-
class_embedding.clone()
|
| 83 |
-
)
|
| 84 |
-
converted[f"transformer_blocks.{block_idx}.norm1.linear.weight"] = converted[
|
| 85 |
-
f"blocks.{block_idx}.adaLN_modulation.1.weight"
|
| 86 |
-
]
|
| 87 |
-
converted[f"transformer_blocks.{block_idx}.norm1.linear.bias"] = converted[
|
| 88 |
-
f"blocks.{block_idx}.adaLN_modulation.1.bias"
|
| 89 |
-
]
|
| 90 |
-
|
| 91 |
-
q, k, v = torch.chunk(converted[f"blocks.{block_idx}.attn.qkv.weight"], 3, dim=0)
|
| 92 |
-
q_bias, k_bias, v_bias = torch.chunk(converted[f"blocks.{block_idx}.attn.qkv.bias"], 3, dim=0)
|
| 93 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_q.weight"] = q
|
| 94 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_q.bias"] = q_bias
|
| 95 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_k.weight"] = k
|
| 96 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_k.bias"] = k_bias
|
| 97 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_v.weight"] = v
|
| 98 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_v.bias"] = v_bias
|
| 99 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_out.0.weight"] = converted[
|
| 100 |
-
f"blocks.{block_idx}.attn.proj.weight"
|
| 101 |
-
]
|
| 102 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_out.0.bias"] = converted[
|
| 103 |
-
f"blocks.{block_idx}.attn.proj.bias"
|
| 104 |
-
]
|
| 105 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.0.proj.weight"] = converted[
|
| 106 |
-
f"blocks.{block_idx}.mlp.fc1.weight"
|
| 107 |
-
]
|
| 108 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.0.proj.bias"] = converted[
|
| 109 |
-
f"blocks.{block_idx}.mlp.fc1.bias"
|
| 110 |
-
]
|
| 111 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.2.weight"] = converted[
|
| 112 |
-
f"blocks.{block_idx}.mlp.fc2.weight"
|
| 113 |
-
]
|
| 114 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.2.bias"] = converted[
|
| 115 |
-
f"blocks.{block_idx}.mlp.fc2.bias"
|
| 116 |
-
]
|
| 117 |
-
|
| 118 |
-
for suffix in (
|
| 119 |
-
"attn.qkv.weight",
|
| 120 |
-
"attn.qkv.bias",
|
| 121 |
-
"attn.proj.weight",
|
| 122 |
-
"attn.proj.bias",
|
| 123 |
-
"mlp.fc1.weight",
|
| 124 |
-
"mlp.fc1.bias",
|
| 125 |
-
"mlp.fc2.weight",
|
| 126 |
-
"mlp.fc2.bias",
|
| 127 |
-
"adaLN_modulation.1.weight",
|
| 128 |
-
"adaLN_modulation.1.bias",
|
| 129 |
-
):
|
| 130 |
-
converted.pop(f"blocks.{block_idx}.{suffix}", None)
|
| 131 |
-
|
| 132 |
-
converted["proj_out_1.weight"] = converted.pop("final_layer.adaLN_modulation.1.weight")
|
| 133 |
-
converted["proj_out_1.bias"] = converted.pop("final_layer.adaLN_modulation.1.bias")
|
| 134 |
-
converted["proj_out_2.weight"] = converted.pop("final_layer.linear.weight")
|
| 135 |
-
converted["proj_out_2.bias"] = converted.pop("final_layer.linear.bias")
|
| 136 |
-
|
| 137 |
-
converted.pop("pos_embed", None)
|
| 138 |
-
for block_idx in range(depth):
|
| 139 |
-
for suffix in ("norm1.weight", "norm1.bias", "norm2.weight", "norm2.bias"):
|
| 140 |
-
converted.pop(f"blocks.{block_idx}.{suffix}", None)
|
| 141 |
-
|
| 142 |
-
return {key: tensor.detach().clone().contiguous() for key, tensor in converted.items()}
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
def load_legacy_dit_state_dict(
|
| 146 |
-
state_dict: Dict[str, torch.Tensor],
|
| 147 |
-
depth: int,
|
| 148 |
-
patch_size: int,
|
| 149 |
-
hidden_size: int,
|
| 150 |
-
num_heads: int,
|
| 151 |
-
) -> Dict[str, torch.Tensor]:
|
| 152 |
-
if "transformer_blocks.0.attn1.to_q.weight" in state_dict:
|
| 153 |
-
return state_dict
|
| 154 |
-
if any(key.startswith("blocks.") for key in state_dict):
|
| 155 |
-
return _convert_legacy_dit_state_dict(state_dict, depth)
|
| 156 |
-
return state_dict
|
| 157 |
|
| 158 |
|
| 159 |
def forward_dit_output(
|
| 160 |
-
transformer,
|
| 161 |
hidden_states: torch.Tensor,
|
| 162 |
timestep: Optional[torch.Tensor],
|
| 163 |
class_labels: torch.Tensor,
|
|
@@ -207,23 +106,6 @@ def forward_dit_output(
|
|
| 207 |
)
|
| 208 |
|
| 209 |
|
| 210 |
-
def remap_transformer_state_dict(
|
| 211 |
-
state_dict: Dict[str, torch.Tensor],
|
| 212 |
-
legacy_kwargs: Dict[str, int],
|
| 213 |
-
extra_prefixes: tuple[str, ...] = (),
|
| 214 |
-
) -> Dict[str, torch.Tensor]:
|
| 215 |
-
state_dict = load_legacy_dit_state_dict(state_dict, **legacy_kwargs)
|
| 216 |
-
remapped: Dict[str, torch.Tensor] = {}
|
| 217 |
-
for key, value in state_dict.items():
|
| 218 |
-
if any(key.startswith(prefix) for prefix in extra_prefixes):
|
| 219 |
-
remapped[key] = value
|
| 220 |
-
elif key.startswith("transformer."):
|
| 221 |
-
remapped[key] = value
|
| 222 |
-
else:
|
| 223 |
-
remapped[f"transformer.{key}"] = value
|
| 224 |
-
return remapped
|
| 225 |
-
|
| 226 |
-
|
| 227 |
def split_output_channels(output: torch.Tensor, in_channels: int) -> torch.Tensor:
|
| 228 |
if output.shape[1] == in_channels:
|
| 229 |
return output
|
|
@@ -259,6 +141,14 @@ class TimestepEmbedder(nn.Module):
|
|
| 259 |
return self.mlp(emb.to(dtype=weight_dtype))
|
| 260 |
|
| 261 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
class Generator(nn.Module):
|
| 263 |
def __init__(
|
| 264 |
self,
|
|
@@ -279,14 +169,7 @@ class Generator(nn.Module):
|
|
| 279 |
super().__init__()
|
| 280 |
self.use_t_src = use_t_src
|
| 281 |
self.use_t_tgt = use_t_tgt
|
| 282 |
-
self.
|
| 283 |
-
"depth": depth,
|
| 284 |
-
"hidden_size": hidden_size,
|
| 285 |
-
"patch_size": patch_size,
|
| 286 |
-
"num_heads": num_heads,
|
| 287 |
-
}
|
| 288 |
-
dit_cls = get_dit_transformer_class()
|
| 289 |
-
self.transformer = dit_cls(
|
| 290 |
**build_dit_config(
|
| 291 |
depth=depth,
|
| 292 |
hidden_size=hidden_size,
|
|
@@ -306,20 +189,9 @@ class Generator(nn.Module):
|
|
| 306 |
nn.init.normal_(self.t_tgt_embedder.mlp[2].weight, std=0.02)
|
| 307 |
|
| 308 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 309 |
-
|
| 310 |
-
has_extra = any(key.startswith(extra) for key in state_dict)
|
| 311 |
-
has_t_embedder = any("t_embedder" in key for key in state_dict)
|
| 312 |
-
remapped = remap_transformer_state_dict(state_dict, self._legacy_kwargs, extra_prefixes=extra)
|
| 313 |
-
if self.use_t_tgt and not has_extra:
|
| 314 |
-
strict = False
|
| 315 |
-
if not has_t_embedder:
|
| 316 |
strict = False
|
| 317 |
-
|
| 318 |
-
if not has_t_embedder:
|
| 319 |
-
for name, param in self.named_parameters():
|
| 320 |
-
if "timestep_embedder" in name:
|
| 321 |
-
param.data.zero_()
|
| 322 |
-
return out
|
| 323 |
|
| 324 |
def forward(self, x, y, t_src=None, t_tgt=None):
|
| 325 |
if self.use_t_src and t_src is not None:
|
|
@@ -353,14 +225,7 @@ class GeneratorDeep(nn.Module):
|
|
| 353 |
):
|
| 354 |
super().__init__()
|
| 355 |
self.repeat = repeat
|
| 356 |
-
self.
|
| 357 |
-
"depth": depth,
|
| 358 |
-
"hidden_size": hidden_size,
|
| 359 |
-
"patch_size": patch_size,
|
| 360 |
-
"num_heads": num_heads,
|
| 361 |
-
}
|
| 362 |
-
dit_cls = get_dit_transformer_class()
|
| 363 |
-
self.transformer = dit_cls(
|
| 364 |
**build_dit_config(
|
| 365 |
depth=depth,
|
| 366 |
hidden_size=hidden_size,
|
|
@@ -376,16 +241,7 @@ class GeneratorDeep(nn.Module):
|
|
| 376 |
self.in_channels = in_channels
|
| 377 |
|
| 378 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 379 |
-
|
| 380 |
-
has_t_embedder = any("t_embedder" in key for key in state_dict)
|
| 381 |
-
if not has_t_embedder:
|
| 382 |
-
strict = False
|
| 383 |
-
out = super().load_state_dict(remapped, strict=strict)
|
| 384 |
-
if not has_t_embedder:
|
| 385 |
-
for name, param in self.named_parameters():
|
| 386 |
-
if "timestep_embedder" in name:
|
| 387 |
-
param.data.zero_()
|
| 388 |
-
return out
|
| 389 |
|
| 390 |
def forward(self, x, y, *args, **kwargs):
|
| 391 |
hidden_states = self.transformer.pos_embed(x)
|
|
@@ -429,7 +285,7 @@ class GeneratorDeep(nn.Module):
|
|
| 429 |
return split_output_channels(output, self.in_channels)
|
| 430 |
|
| 431 |
|
| 432 |
-
class AFMGenerator2DModel(
|
| 433 |
config_name = "config.json"
|
| 434 |
|
| 435 |
@register_to_config
|
|
@@ -470,7 +326,7 @@ class AFMGenerator2DModel(Generator, ModelMixin, ConfigMixin):
|
|
| 470 |
)
|
| 471 |
|
| 472 |
|
| 473 |
-
class AFMGeneratorDeep2DModel(
|
| 474 |
config_name = "config.json"
|
| 475 |
|
| 476 |
@register_to_config
|
|
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
|
|
|
| 5 |
import math
|
| 6 |
from typing import Any, Dict, Optional
|
| 7 |
|
|
|
|
| 9 |
import torch.nn.functional as F
|
| 10 |
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
| 11 |
from diffusers.models.modeling_utils import ModelMixin
|
| 12 |
+
from diffusers.models.transformers.dit_transformer_2d import DiTTransformer2DModel
|
| 13 |
from torch import nn
|
| 14 |
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def build_dit_config(
|
| 17 |
*,
|
| 18 |
depth: int = 28,
|
|
|
|
| 48 |
}
|
| 49 |
|
| 50 |
|
| 51 |
+
_PREFIX_KEYS = ("transformer.", "t_tgt_embedder.")
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def _wrap_dit_state_dict(state_dict: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]:
|
| 55 |
+
return {k if k.startswith(_PREFIX_KEYS) else f"transformer.{k}": v for k, v in state_dict.items()}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
def forward_dit_output(
|
| 59 |
+
transformer: DiTTransformer2DModel,
|
| 60 |
hidden_states: torch.Tensor,
|
| 61 |
timestep: Optional[torch.Tensor],
|
| 62 |
class_labels: torch.Tensor,
|
|
|
|
| 106 |
)
|
| 107 |
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
def split_output_channels(output: torch.Tensor, in_channels: int) -> torch.Tensor:
|
| 110 |
if output.shape[1] == in_channels:
|
| 111 |
return output
|
|
|
|
| 141 |
return self.mlp(emb.to(dtype=weight_dtype))
|
| 142 |
|
| 143 |
|
| 144 |
+
class _AFMCheckpointMixin(ModelMixin):
|
| 145 |
+
def _fix_state_dict_keys_on_load(self, state_dict):
|
| 146 |
+
wrapped = _wrap_dit_state_dict(state_dict)
|
| 147 |
+
state_dict.clear()
|
| 148 |
+
state_dict.update(wrapped)
|
| 149 |
+
super()._fix_state_dict_keys_on_load(state_dict)
|
| 150 |
+
|
| 151 |
+
|
| 152 |
class Generator(nn.Module):
|
| 153 |
def __init__(
|
| 154 |
self,
|
|
|
|
| 169 |
super().__init__()
|
| 170 |
self.use_t_src = use_t_src
|
| 171 |
self.use_t_tgt = use_t_tgt
|
| 172 |
+
self.transformer = DiTTransformer2DModel(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
**build_dit_config(
|
| 174 |
depth=depth,
|
| 175 |
hidden_size=hidden_size,
|
|
|
|
| 189 |
nn.init.normal_(self.t_tgt_embedder.mlp[2].weight, std=0.02)
|
| 190 |
|
| 191 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 192 |
+
if self.use_t_tgt and not any(key.startswith("t_tgt_embedder.") for key in state_dict):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
strict = False
|
| 194 |
+
return super().load_state_dict(_wrap_dit_state_dict(state_dict), strict=strict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
def forward(self, x, y, t_src=None, t_tgt=None):
|
| 197 |
if self.use_t_src and t_src is not None:
|
|
|
|
| 225 |
):
|
| 226 |
super().__init__()
|
| 227 |
self.repeat = repeat
|
| 228 |
+
self.transformer = DiTTransformer2DModel(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
**build_dit_config(
|
| 230 |
depth=depth,
|
| 231 |
hidden_size=hidden_size,
|
|
|
|
| 241 |
self.in_channels = in_channels
|
| 242 |
|
| 243 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 244 |
+
return super().load_state_dict(_wrap_dit_state_dict(state_dict), strict=strict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
def forward(self, x, y, *args, **kwargs):
|
| 247 |
hidden_states = self.transformer.pos_embed(x)
|
|
|
|
| 285 |
return split_output_channels(output, self.in_channels)
|
| 286 |
|
| 287 |
|
| 288 |
+
class AFMGenerator2DModel(_AFMCheckpointMixin, Generator, ConfigMixin):
|
| 289 |
config_name = "config.json"
|
| 290 |
|
| 291 |
@register_to_config
|
|
|
|
| 326 |
)
|
| 327 |
|
| 328 |
|
| 329 |
+
class AFMGeneratorDeep2DModel(_AFMCheckpointMixin, GeneratorDeep, ConfigMixin):
|
| 330 |
config_name = "config.json"
|
| 331 |
|
| 332 |
@register_to_config
|
AFM-XL-2-112layer-1NFE-guided/generator/config.json
CHANGED
|
@@ -1,6 +1,22 @@
|
|
| 1 |
{
|
| 2 |
-
"_class_name": "
|
| 3 |
"_diffusers_version": "0.36.0",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
"model_type": "AFM-XL/2",
|
| 5 |
"architecture": "deep",
|
| 6 |
"repeat": 4,
|
|
@@ -12,9 +28,7 @@
|
|
| 12 |
"class_dropout_prob": 0.0,
|
| 13 |
"input_size": 32,
|
| 14 |
"num_classes": 1000,
|
| 15 |
-
"in_channels": 4,
|
| 16 |
"depth": 28,
|
| 17 |
"hidden_size": 1152,
|
| 18 |
-
"patch_size": 2,
|
| 19 |
"num_heads": 16
|
| 20 |
}
|
|
|
|
| 1 |
{
|
| 2 |
+
"_class_name": "DiTTransformer2DModel",
|
| 3 |
"_diffusers_version": "0.36.0",
|
| 4 |
+
"sample_size": 32,
|
| 5 |
+
"num_layers": 28,
|
| 6 |
+
"num_attention_heads": 16,
|
| 7 |
+
"attention_head_dim": 72,
|
| 8 |
+
"in_channels": 4,
|
| 9 |
+
"out_channels": 4,
|
| 10 |
+
"patch_size": 2,
|
| 11 |
+
"attention_bias": true,
|
| 12 |
+
"activation_fn": "gelu-approximate",
|
| 13 |
+
"num_embeds_ada_norm": 1000,
|
| 14 |
+
"norm_type": "ada_norm_zero",
|
| 15 |
+
"norm_elementwise_affine": false,
|
| 16 |
+
"dropout": 0.0,
|
| 17 |
+
"norm_num_groups": 32,
|
| 18 |
+
"norm_eps": 1e-05,
|
| 19 |
+
"upcast_attention": false,
|
| 20 |
"model_type": "AFM-XL/2",
|
| 21 |
"architecture": "deep",
|
| 22 |
"repeat": 4,
|
|
|
|
| 28 |
"class_dropout_prob": 0.0,
|
| 29 |
"input_size": 32,
|
| 30 |
"num_classes": 1000,
|
|
|
|
| 31 |
"depth": 28,
|
| 32 |
"hidden_size": 1152,
|
|
|
|
| 33 |
"num_heads": 16
|
| 34 |
}
|
AFM-XL-2-112layer-1NFE-guided/generator/diffusion_pytorch_model.safetensors
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4cbdfe1714c45d0d2245a3de495a709b4c45d4f87bc01bfd0e734474b85afdd9
|
| 3 |
+
size 2999296584
|
AFM-XL-2-112layer-1NFE-guided/generator/modeling_afm.py
CHANGED
|
@@ -2,7 +2,6 @@
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
-
import copy
|
| 6 |
import math
|
| 7 |
from typing import Any, Dict, Optional
|
| 8 |
|
|
@@ -10,15 +9,10 @@ import torch
|
|
| 10 |
import torch.nn.functional as F
|
| 11 |
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
| 12 |
from diffusers.models.modeling_utils import ModelMixin
|
|
|
|
| 13 |
from torch import nn
|
| 14 |
|
| 15 |
|
| 16 |
-
def get_dit_transformer_class():
|
| 17 |
-
from diffusers.models.transformers.dit_transformer_2d import DiTTransformer2DModel
|
| 18 |
-
|
| 19 |
-
return DiTTransformer2DModel
|
| 20 |
-
|
| 21 |
-
|
| 22 |
def build_dit_config(
|
| 23 |
*,
|
| 24 |
depth: int = 28,
|
|
@@ -54,110 +48,15 @@ def build_dit_config(
|
|
| 54 |
}
|
| 55 |
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
if "t_embedder.mlp.0.weight" in converted:
|
| 64 |
-
timestep_weights = {
|
| 65 |
-
"linear_1.weight": converted.pop("t_embedder.mlp.0.weight"),
|
| 66 |
-
"linear_1.bias": converted.pop("t_embedder.mlp.0.bias"),
|
| 67 |
-
"linear_2.weight": converted.pop("t_embedder.mlp.2.weight"),
|
| 68 |
-
"linear_2.bias": converted.pop("t_embedder.mlp.2.bias"),
|
| 69 |
-
}
|
| 70 |
-
else:
|
| 71 |
-
timestep_weights = None
|
| 72 |
-
class_embedding = converted.pop("y_embedder.embedding_table.weight")
|
| 73 |
-
if class_embedding.shape[0] == 1000:
|
| 74 |
-
null_class = torch.zeros(1, class_embedding.shape[1], dtype=class_embedding.dtype)
|
| 75 |
-
class_embedding = torch.cat([class_embedding, null_class], dim=0)
|
| 76 |
-
|
| 77 |
-
for block_idx in range(depth):
|
| 78 |
-
if timestep_weights is not None:
|
| 79 |
-
for key, tensor in timestep_weights.items():
|
| 80 |
-
converted[f"transformer_blocks.{block_idx}.norm1.emb.timestep_embedder.{key}"] = tensor.clone()
|
| 81 |
-
converted[f"transformer_blocks.{block_idx}.norm1.emb.class_embedder.embedding_table.weight"] = (
|
| 82 |
-
class_embedding.clone()
|
| 83 |
-
)
|
| 84 |
-
converted[f"transformer_blocks.{block_idx}.norm1.linear.weight"] = converted[
|
| 85 |
-
f"blocks.{block_idx}.adaLN_modulation.1.weight"
|
| 86 |
-
]
|
| 87 |
-
converted[f"transformer_blocks.{block_idx}.norm1.linear.bias"] = converted[
|
| 88 |
-
f"blocks.{block_idx}.adaLN_modulation.1.bias"
|
| 89 |
-
]
|
| 90 |
-
|
| 91 |
-
q, k, v = torch.chunk(converted[f"blocks.{block_idx}.attn.qkv.weight"], 3, dim=0)
|
| 92 |
-
q_bias, k_bias, v_bias = torch.chunk(converted[f"blocks.{block_idx}.attn.qkv.bias"], 3, dim=0)
|
| 93 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_q.weight"] = q
|
| 94 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_q.bias"] = q_bias
|
| 95 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_k.weight"] = k
|
| 96 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_k.bias"] = k_bias
|
| 97 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_v.weight"] = v
|
| 98 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_v.bias"] = v_bias
|
| 99 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_out.0.weight"] = converted[
|
| 100 |
-
f"blocks.{block_idx}.attn.proj.weight"
|
| 101 |
-
]
|
| 102 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_out.0.bias"] = converted[
|
| 103 |
-
f"blocks.{block_idx}.attn.proj.bias"
|
| 104 |
-
]
|
| 105 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.0.proj.weight"] = converted[
|
| 106 |
-
f"blocks.{block_idx}.mlp.fc1.weight"
|
| 107 |
-
]
|
| 108 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.0.proj.bias"] = converted[
|
| 109 |
-
f"blocks.{block_idx}.mlp.fc1.bias"
|
| 110 |
-
]
|
| 111 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.2.weight"] = converted[
|
| 112 |
-
f"blocks.{block_idx}.mlp.fc2.weight"
|
| 113 |
-
]
|
| 114 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.2.bias"] = converted[
|
| 115 |
-
f"blocks.{block_idx}.mlp.fc2.bias"
|
| 116 |
-
]
|
| 117 |
-
|
| 118 |
-
for suffix in (
|
| 119 |
-
"attn.qkv.weight",
|
| 120 |
-
"attn.qkv.bias",
|
| 121 |
-
"attn.proj.weight",
|
| 122 |
-
"attn.proj.bias",
|
| 123 |
-
"mlp.fc1.weight",
|
| 124 |
-
"mlp.fc1.bias",
|
| 125 |
-
"mlp.fc2.weight",
|
| 126 |
-
"mlp.fc2.bias",
|
| 127 |
-
"adaLN_modulation.1.weight",
|
| 128 |
-
"adaLN_modulation.1.bias",
|
| 129 |
-
):
|
| 130 |
-
converted.pop(f"blocks.{block_idx}.{suffix}", None)
|
| 131 |
-
|
| 132 |
-
converted["proj_out_1.weight"] = converted.pop("final_layer.adaLN_modulation.1.weight")
|
| 133 |
-
converted["proj_out_1.bias"] = converted.pop("final_layer.adaLN_modulation.1.bias")
|
| 134 |
-
converted["proj_out_2.weight"] = converted.pop("final_layer.linear.weight")
|
| 135 |
-
converted["proj_out_2.bias"] = converted.pop("final_layer.linear.bias")
|
| 136 |
-
|
| 137 |
-
converted.pop("pos_embed", None)
|
| 138 |
-
for block_idx in range(depth):
|
| 139 |
-
for suffix in ("norm1.weight", "norm1.bias", "norm2.weight", "norm2.bias"):
|
| 140 |
-
converted.pop(f"blocks.{block_idx}.{suffix}", None)
|
| 141 |
-
|
| 142 |
-
return {key: tensor.detach().clone().contiguous() for key, tensor in converted.items()}
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
def load_legacy_dit_state_dict(
|
| 146 |
-
state_dict: Dict[str, torch.Tensor],
|
| 147 |
-
depth: int,
|
| 148 |
-
patch_size: int,
|
| 149 |
-
hidden_size: int,
|
| 150 |
-
num_heads: int,
|
| 151 |
-
) -> Dict[str, torch.Tensor]:
|
| 152 |
-
if "transformer_blocks.0.attn1.to_q.weight" in state_dict:
|
| 153 |
-
return state_dict
|
| 154 |
-
if any(key.startswith("blocks.") for key in state_dict):
|
| 155 |
-
return _convert_legacy_dit_state_dict(state_dict, depth)
|
| 156 |
-
return state_dict
|
| 157 |
|
| 158 |
|
| 159 |
def forward_dit_output(
|
| 160 |
-
transformer,
|
| 161 |
hidden_states: torch.Tensor,
|
| 162 |
timestep: Optional[torch.Tensor],
|
| 163 |
class_labels: torch.Tensor,
|
|
@@ -207,23 +106,6 @@ def forward_dit_output(
|
|
| 207 |
)
|
| 208 |
|
| 209 |
|
| 210 |
-
def remap_transformer_state_dict(
|
| 211 |
-
state_dict: Dict[str, torch.Tensor],
|
| 212 |
-
legacy_kwargs: Dict[str, int],
|
| 213 |
-
extra_prefixes: tuple[str, ...] = (),
|
| 214 |
-
) -> Dict[str, torch.Tensor]:
|
| 215 |
-
state_dict = load_legacy_dit_state_dict(state_dict, **legacy_kwargs)
|
| 216 |
-
remapped: Dict[str, torch.Tensor] = {}
|
| 217 |
-
for key, value in state_dict.items():
|
| 218 |
-
if any(key.startswith(prefix) for prefix in extra_prefixes):
|
| 219 |
-
remapped[key] = value
|
| 220 |
-
elif key.startswith("transformer."):
|
| 221 |
-
remapped[key] = value
|
| 222 |
-
else:
|
| 223 |
-
remapped[f"transformer.{key}"] = value
|
| 224 |
-
return remapped
|
| 225 |
-
|
| 226 |
-
|
| 227 |
def split_output_channels(output: torch.Tensor, in_channels: int) -> torch.Tensor:
|
| 228 |
if output.shape[1] == in_channels:
|
| 229 |
return output
|
|
@@ -259,6 +141,14 @@ class TimestepEmbedder(nn.Module):
|
|
| 259 |
return self.mlp(emb.to(dtype=weight_dtype))
|
| 260 |
|
| 261 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
class Generator(nn.Module):
|
| 263 |
def __init__(
|
| 264 |
self,
|
|
@@ -279,14 +169,7 @@ class Generator(nn.Module):
|
|
| 279 |
super().__init__()
|
| 280 |
self.use_t_src = use_t_src
|
| 281 |
self.use_t_tgt = use_t_tgt
|
| 282 |
-
self.
|
| 283 |
-
"depth": depth,
|
| 284 |
-
"hidden_size": hidden_size,
|
| 285 |
-
"patch_size": patch_size,
|
| 286 |
-
"num_heads": num_heads,
|
| 287 |
-
}
|
| 288 |
-
dit_cls = get_dit_transformer_class()
|
| 289 |
-
self.transformer = dit_cls(
|
| 290 |
**build_dit_config(
|
| 291 |
depth=depth,
|
| 292 |
hidden_size=hidden_size,
|
|
@@ -306,20 +189,9 @@ class Generator(nn.Module):
|
|
| 306 |
nn.init.normal_(self.t_tgt_embedder.mlp[2].weight, std=0.02)
|
| 307 |
|
| 308 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 309 |
-
|
| 310 |
-
has_extra = any(key.startswith(extra) for key in state_dict)
|
| 311 |
-
has_t_embedder = any("t_embedder" in key for key in state_dict)
|
| 312 |
-
remapped = remap_transformer_state_dict(state_dict, self._legacy_kwargs, extra_prefixes=extra)
|
| 313 |
-
if self.use_t_tgt and not has_extra:
|
| 314 |
-
strict = False
|
| 315 |
-
if not has_t_embedder:
|
| 316 |
strict = False
|
| 317 |
-
|
| 318 |
-
if not has_t_embedder:
|
| 319 |
-
for name, param in self.named_parameters():
|
| 320 |
-
if "timestep_embedder" in name:
|
| 321 |
-
param.data.zero_()
|
| 322 |
-
return out
|
| 323 |
|
| 324 |
def forward(self, x, y, t_src=None, t_tgt=None):
|
| 325 |
if self.use_t_src and t_src is not None:
|
|
@@ -353,14 +225,7 @@ class GeneratorDeep(nn.Module):
|
|
| 353 |
):
|
| 354 |
super().__init__()
|
| 355 |
self.repeat = repeat
|
| 356 |
-
self.
|
| 357 |
-
"depth": depth,
|
| 358 |
-
"hidden_size": hidden_size,
|
| 359 |
-
"patch_size": patch_size,
|
| 360 |
-
"num_heads": num_heads,
|
| 361 |
-
}
|
| 362 |
-
dit_cls = get_dit_transformer_class()
|
| 363 |
-
self.transformer = dit_cls(
|
| 364 |
**build_dit_config(
|
| 365 |
depth=depth,
|
| 366 |
hidden_size=hidden_size,
|
|
@@ -376,16 +241,7 @@ class GeneratorDeep(nn.Module):
|
|
| 376 |
self.in_channels = in_channels
|
| 377 |
|
| 378 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 379 |
-
|
| 380 |
-
has_t_embedder = any("t_embedder" in key for key in state_dict)
|
| 381 |
-
if not has_t_embedder:
|
| 382 |
-
strict = False
|
| 383 |
-
out = super().load_state_dict(remapped, strict=strict)
|
| 384 |
-
if not has_t_embedder:
|
| 385 |
-
for name, param in self.named_parameters():
|
| 386 |
-
if "timestep_embedder" in name:
|
| 387 |
-
param.data.zero_()
|
| 388 |
-
return out
|
| 389 |
|
| 390 |
def forward(self, x, y, *args, **kwargs):
|
| 391 |
hidden_states = self.transformer.pos_embed(x)
|
|
@@ -429,7 +285,7 @@ class GeneratorDeep(nn.Module):
|
|
| 429 |
return split_output_channels(output, self.in_channels)
|
| 430 |
|
| 431 |
|
| 432 |
-
class AFMGenerator2DModel(
|
| 433 |
config_name = "config.json"
|
| 434 |
|
| 435 |
@register_to_config
|
|
@@ -470,7 +326,7 @@ class AFMGenerator2DModel(Generator, ModelMixin, ConfigMixin):
|
|
| 470 |
)
|
| 471 |
|
| 472 |
|
| 473 |
-
class AFMGeneratorDeep2DModel(
|
| 474 |
config_name = "config.json"
|
| 475 |
|
| 476 |
@register_to_config
|
|
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
|
|
|
| 5 |
import math
|
| 6 |
from typing import Any, Dict, Optional
|
| 7 |
|
|
|
|
| 9 |
import torch.nn.functional as F
|
| 10 |
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
| 11 |
from diffusers.models.modeling_utils import ModelMixin
|
| 12 |
+
from diffusers.models.transformers.dit_transformer_2d import DiTTransformer2DModel
|
| 13 |
from torch import nn
|
| 14 |
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def build_dit_config(
|
| 17 |
*,
|
| 18 |
depth: int = 28,
|
|
|
|
| 48 |
}
|
| 49 |
|
| 50 |
|
| 51 |
+
_PREFIX_KEYS = ("transformer.", "t_tgt_embedder.")
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def _wrap_dit_state_dict(state_dict: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]:
|
| 55 |
+
return {k if k.startswith(_PREFIX_KEYS) else f"transformer.{k}": v for k, v in state_dict.items()}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
def forward_dit_output(
|
| 59 |
+
transformer: DiTTransformer2DModel,
|
| 60 |
hidden_states: torch.Tensor,
|
| 61 |
timestep: Optional[torch.Tensor],
|
| 62 |
class_labels: torch.Tensor,
|
|
|
|
| 106 |
)
|
| 107 |
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
def split_output_channels(output: torch.Tensor, in_channels: int) -> torch.Tensor:
|
| 110 |
if output.shape[1] == in_channels:
|
| 111 |
return output
|
|
|
|
| 141 |
return self.mlp(emb.to(dtype=weight_dtype))
|
| 142 |
|
| 143 |
|
| 144 |
+
class _AFMCheckpointMixin(ModelMixin):
|
| 145 |
+
def _fix_state_dict_keys_on_load(self, state_dict):
|
| 146 |
+
wrapped = _wrap_dit_state_dict(state_dict)
|
| 147 |
+
state_dict.clear()
|
| 148 |
+
state_dict.update(wrapped)
|
| 149 |
+
super()._fix_state_dict_keys_on_load(state_dict)
|
| 150 |
+
|
| 151 |
+
|
| 152 |
class Generator(nn.Module):
|
| 153 |
def __init__(
|
| 154 |
self,
|
|
|
|
| 169 |
super().__init__()
|
| 170 |
self.use_t_src = use_t_src
|
| 171 |
self.use_t_tgt = use_t_tgt
|
| 172 |
+
self.transformer = DiTTransformer2DModel(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
**build_dit_config(
|
| 174 |
depth=depth,
|
| 175 |
hidden_size=hidden_size,
|
|
|
|
| 189 |
nn.init.normal_(self.t_tgt_embedder.mlp[2].weight, std=0.02)
|
| 190 |
|
| 191 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 192 |
+
if self.use_t_tgt and not any(key.startswith("t_tgt_embedder.") for key in state_dict):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
strict = False
|
| 194 |
+
return super().load_state_dict(_wrap_dit_state_dict(state_dict), strict=strict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
def forward(self, x, y, t_src=None, t_tgt=None):
|
| 197 |
if self.use_t_src and t_src is not None:
|
|
|
|
| 225 |
):
|
| 226 |
super().__init__()
|
| 227 |
self.repeat = repeat
|
| 228 |
+
self.transformer = DiTTransformer2DModel(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
**build_dit_config(
|
| 230 |
depth=depth,
|
| 231 |
hidden_size=hidden_size,
|
|
|
|
| 241 |
self.in_channels = in_channels
|
| 242 |
|
| 243 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 244 |
+
return super().load_state_dict(_wrap_dit_state_dict(state_dict), strict=strict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
def forward(self, x, y, *args, **kwargs):
|
| 247 |
hidden_states = self.transformer.pos_embed(x)
|
|
|
|
| 285 |
return split_output_channels(output, self.in_channels)
|
| 286 |
|
| 287 |
|
| 288 |
+
class AFMGenerator2DModel(_AFMCheckpointMixin, Generator, ConfigMixin):
|
| 289 |
config_name = "config.json"
|
| 290 |
|
| 291 |
@register_to_config
|
|
|
|
| 326 |
)
|
| 327 |
|
| 328 |
|
| 329 |
+
class AFMGeneratorDeep2DModel(_AFMCheckpointMixin, GeneratorDeep, ConfigMixin):
|
| 330 |
config_name = "config.json"
|
| 331 |
|
| 332 |
@register_to_config
|
AFM-XL-2-1NFE-guided/generator/__pycache__/modeling_afm.cpython-312.pyc
CHANGED
|
Binary files a/AFM-XL-2-1NFE-guided/generator/__pycache__/modeling_afm.cpython-312.pyc and b/AFM-XL-2-1NFE-guided/generator/__pycache__/modeling_afm.cpython-312.pyc differ
|
|
|
AFM-XL-2-1NFE-guided/generator/config.json
CHANGED
|
@@ -1,6 +1,22 @@
|
|
| 1 |
{
|
| 2 |
-
"_class_name": "
|
| 3 |
"_diffusers_version": "0.36.0",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
"model_type": "AFM-XL/2",
|
| 5 |
"architecture": "standard",
|
| 6 |
"repeat": 1,
|
|
@@ -12,9 +28,7 @@
|
|
| 12 |
"class_dropout_prob": 0.0,
|
| 13 |
"input_size": 32,
|
| 14 |
"num_classes": 1000,
|
| 15 |
-
"in_channels": 4,
|
| 16 |
"depth": 28,
|
| 17 |
"hidden_size": 1152,
|
| 18 |
-
"patch_size": 2,
|
| 19 |
"num_heads": 16
|
| 20 |
}
|
|
|
|
| 1 |
{
|
| 2 |
+
"_class_name": "DiTTransformer2DModel",
|
| 3 |
"_diffusers_version": "0.36.0",
|
| 4 |
+
"sample_size": 32,
|
| 5 |
+
"num_layers": 28,
|
| 6 |
+
"num_attention_heads": 16,
|
| 7 |
+
"attention_head_dim": 72,
|
| 8 |
+
"in_channels": 4,
|
| 9 |
+
"out_channels": 4,
|
| 10 |
+
"patch_size": 2,
|
| 11 |
+
"attention_bias": true,
|
| 12 |
+
"activation_fn": "gelu-approximate",
|
| 13 |
+
"num_embeds_ada_norm": 1000,
|
| 14 |
+
"norm_type": "ada_norm_zero",
|
| 15 |
+
"norm_elementwise_affine": false,
|
| 16 |
+
"dropout": 0.0,
|
| 17 |
+
"norm_num_groups": 32,
|
| 18 |
+
"norm_eps": 1e-05,
|
| 19 |
+
"upcast_attention": false,
|
| 20 |
"model_type": "AFM-XL/2",
|
| 21 |
"architecture": "standard",
|
| 22 |
"repeat": 1,
|
|
|
|
| 28 |
"class_dropout_prob": 0.0,
|
| 29 |
"input_size": 32,
|
| 30 |
"num_classes": 1000,
|
|
|
|
| 31 |
"depth": 28,
|
| 32 |
"hidden_size": 1152,
|
|
|
|
| 33 |
"num_heads": 16
|
| 34 |
}
|
AFM-XL-2-1NFE-guided/generator/diffusion_pytorch_model.safetensors
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e8c3fc7246258cac7b4b16b8c95e5fa08a64a2f853e17689783a46cc4bccad9b
|
| 3 |
+
size 2999296584
|
AFM-XL-2-1NFE-guided/generator/modeling_afm.py
CHANGED
|
@@ -2,7 +2,6 @@
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
-
import copy
|
| 6 |
import math
|
| 7 |
from typing import Any, Dict, Optional
|
| 8 |
|
|
@@ -10,15 +9,10 @@ import torch
|
|
| 10 |
import torch.nn.functional as F
|
| 11 |
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
| 12 |
from diffusers.models.modeling_utils import ModelMixin
|
|
|
|
| 13 |
from torch import nn
|
| 14 |
|
| 15 |
|
| 16 |
-
def get_dit_transformer_class():
|
| 17 |
-
from diffusers.models.transformers.dit_transformer_2d import DiTTransformer2DModel
|
| 18 |
-
|
| 19 |
-
return DiTTransformer2DModel
|
| 20 |
-
|
| 21 |
-
|
| 22 |
def build_dit_config(
|
| 23 |
*,
|
| 24 |
depth: int = 28,
|
|
@@ -54,110 +48,15 @@ def build_dit_config(
|
|
| 54 |
}
|
| 55 |
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
if "t_embedder.mlp.0.weight" in converted:
|
| 64 |
-
timestep_weights = {
|
| 65 |
-
"linear_1.weight": converted.pop("t_embedder.mlp.0.weight"),
|
| 66 |
-
"linear_1.bias": converted.pop("t_embedder.mlp.0.bias"),
|
| 67 |
-
"linear_2.weight": converted.pop("t_embedder.mlp.2.weight"),
|
| 68 |
-
"linear_2.bias": converted.pop("t_embedder.mlp.2.bias"),
|
| 69 |
-
}
|
| 70 |
-
else:
|
| 71 |
-
timestep_weights = None
|
| 72 |
-
class_embedding = converted.pop("y_embedder.embedding_table.weight")
|
| 73 |
-
if class_embedding.shape[0] == 1000:
|
| 74 |
-
null_class = torch.zeros(1, class_embedding.shape[1], dtype=class_embedding.dtype)
|
| 75 |
-
class_embedding = torch.cat([class_embedding, null_class], dim=0)
|
| 76 |
-
|
| 77 |
-
for block_idx in range(depth):
|
| 78 |
-
if timestep_weights is not None:
|
| 79 |
-
for key, tensor in timestep_weights.items():
|
| 80 |
-
converted[f"transformer_blocks.{block_idx}.norm1.emb.timestep_embedder.{key}"] = tensor.clone()
|
| 81 |
-
converted[f"transformer_blocks.{block_idx}.norm1.emb.class_embedder.embedding_table.weight"] = (
|
| 82 |
-
class_embedding.clone()
|
| 83 |
-
)
|
| 84 |
-
converted[f"transformer_blocks.{block_idx}.norm1.linear.weight"] = converted[
|
| 85 |
-
f"blocks.{block_idx}.adaLN_modulation.1.weight"
|
| 86 |
-
]
|
| 87 |
-
converted[f"transformer_blocks.{block_idx}.norm1.linear.bias"] = converted[
|
| 88 |
-
f"blocks.{block_idx}.adaLN_modulation.1.bias"
|
| 89 |
-
]
|
| 90 |
-
|
| 91 |
-
q, k, v = torch.chunk(converted[f"blocks.{block_idx}.attn.qkv.weight"], 3, dim=0)
|
| 92 |
-
q_bias, k_bias, v_bias = torch.chunk(converted[f"blocks.{block_idx}.attn.qkv.bias"], 3, dim=0)
|
| 93 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_q.weight"] = q
|
| 94 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_q.bias"] = q_bias
|
| 95 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_k.weight"] = k
|
| 96 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_k.bias"] = k_bias
|
| 97 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_v.weight"] = v
|
| 98 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_v.bias"] = v_bias
|
| 99 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_out.0.weight"] = converted[
|
| 100 |
-
f"blocks.{block_idx}.attn.proj.weight"
|
| 101 |
-
]
|
| 102 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_out.0.bias"] = converted[
|
| 103 |
-
f"blocks.{block_idx}.attn.proj.bias"
|
| 104 |
-
]
|
| 105 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.0.proj.weight"] = converted[
|
| 106 |
-
f"blocks.{block_idx}.mlp.fc1.weight"
|
| 107 |
-
]
|
| 108 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.0.proj.bias"] = converted[
|
| 109 |
-
f"blocks.{block_idx}.mlp.fc1.bias"
|
| 110 |
-
]
|
| 111 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.2.weight"] = converted[
|
| 112 |
-
f"blocks.{block_idx}.mlp.fc2.weight"
|
| 113 |
-
]
|
| 114 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.2.bias"] = converted[
|
| 115 |
-
f"blocks.{block_idx}.mlp.fc2.bias"
|
| 116 |
-
]
|
| 117 |
-
|
| 118 |
-
for suffix in (
|
| 119 |
-
"attn.qkv.weight",
|
| 120 |
-
"attn.qkv.bias",
|
| 121 |
-
"attn.proj.weight",
|
| 122 |
-
"attn.proj.bias",
|
| 123 |
-
"mlp.fc1.weight",
|
| 124 |
-
"mlp.fc1.bias",
|
| 125 |
-
"mlp.fc2.weight",
|
| 126 |
-
"mlp.fc2.bias",
|
| 127 |
-
"adaLN_modulation.1.weight",
|
| 128 |
-
"adaLN_modulation.1.bias",
|
| 129 |
-
):
|
| 130 |
-
converted.pop(f"blocks.{block_idx}.{suffix}", None)
|
| 131 |
-
|
| 132 |
-
converted["proj_out_1.weight"] = converted.pop("final_layer.adaLN_modulation.1.weight")
|
| 133 |
-
converted["proj_out_1.bias"] = converted.pop("final_layer.adaLN_modulation.1.bias")
|
| 134 |
-
converted["proj_out_2.weight"] = converted.pop("final_layer.linear.weight")
|
| 135 |
-
converted["proj_out_2.bias"] = converted.pop("final_layer.linear.bias")
|
| 136 |
-
|
| 137 |
-
converted.pop("pos_embed", None)
|
| 138 |
-
for block_idx in range(depth):
|
| 139 |
-
for suffix in ("norm1.weight", "norm1.bias", "norm2.weight", "norm2.bias"):
|
| 140 |
-
converted.pop(f"blocks.{block_idx}.{suffix}", None)
|
| 141 |
-
|
| 142 |
-
return {key: tensor.detach().clone().contiguous() for key, tensor in converted.items()}
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
def load_legacy_dit_state_dict(
|
| 146 |
-
state_dict: Dict[str, torch.Tensor],
|
| 147 |
-
depth: int,
|
| 148 |
-
patch_size: int,
|
| 149 |
-
hidden_size: int,
|
| 150 |
-
num_heads: int,
|
| 151 |
-
) -> Dict[str, torch.Tensor]:
|
| 152 |
-
if "transformer_blocks.0.attn1.to_q.weight" in state_dict:
|
| 153 |
-
return state_dict
|
| 154 |
-
if any(key.startswith("blocks.") for key in state_dict):
|
| 155 |
-
return _convert_legacy_dit_state_dict(state_dict, depth)
|
| 156 |
-
return state_dict
|
| 157 |
|
| 158 |
|
| 159 |
def forward_dit_output(
|
| 160 |
-
transformer,
|
| 161 |
hidden_states: torch.Tensor,
|
| 162 |
timestep: Optional[torch.Tensor],
|
| 163 |
class_labels: torch.Tensor,
|
|
@@ -207,23 +106,6 @@ def forward_dit_output(
|
|
| 207 |
)
|
| 208 |
|
| 209 |
|
| 210 |
-
def remap_transformer_state_dict(
|
| 211 |
-
state_dict: Dict[str, torch.Tensor],
|
| 212 |
-
legacy_kwargs: Dict[str, int],
|
| 213 |
-
extra_prefixes: tuple[str, ...] = (),
|
| 214 |
-
) -> Dict[str, torch.Tensor]:
|
| 215 |
-
state_dict = load_legacy_dit_state_dict(state_dict, **legacy_kwargs)
|
| 216 |
-
remapped: Dict[str, torch.Tensor] = {}
|
| 217 |
-
for key, value in state_dict.items():
|
| 218 |
-
if any(key.startswith(prefix) for prefix in extra_prefixes):
|
| 219 |
-
remapped[key] = value
|
| 220 |
-
elif key.startswith("transformer."):
|
| 221 |
-
remapped[key] = value
|
| 222 |
-
else:
|
| 223 |
-
remapped[f"transformer.{key}"] = value
|
| 224 |
-
return remapped
|
| 225 |
-
|
| 226 |
-
|
| 227 |
def split_output_channels(output: torch.Tensor, in_channels: int) -> torch.Tensor:
|
| 228 |
if output.shape[1] == in_channels:
|
| 229 |
return output
|
|
@@ -259,6 +141,14 @@ class TimestepEmbedder(nn.Module):
|
|
| 259 |
return self.mlp(emb.to(dtype=weight_dtype))
|
| 260 |
|
| 261 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
class Generator(nn.Module):
|
| 263 |
def __init__(
|
| 264 |
self,
|
|
@@ -279,14 +169,7 @@ class Generator(nn.Module):
|
|
| 279 |
super().__init__()
|
| 280 |
self.use_t_src = use_t_src
|
| 281 |
self.use_t_tgt = use_t_tgt
|
| 282 |
-
self.
|
| 283 |
-
"depth": depth,
|
| 284 |
-
"hidden_size": hidden_size,
|
| 285 |
-
"patch_size": patch_size,
|
| 286 |
-
"num_heads": num_heads,
|
| 287 |
-
}
|
| 288 |
-
dit_cls = get_dit_transformer_class()
|
| 289 |
-
self.transformer = dit_cls(
|
| 290 |
**build_dit_config(
|
| 291 |
depth=depth,
|
| 292 |
hidden_size=hidden_size,
|
|
@@ -306,20 +189,9 @@ class Generator(nn.Module):
|
|
| 306 |
nn.init.normal_(self.t_tgt_embedder.mlp[2].weight, std=0.02)
|
| 307 |
|
| 308 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 309 |
-
|
| 310 |
-
has_extra = any(key.startswith(extra) for key in state_dict)
|
| 311 |
-
has_t_embedder = any("t_embedder" in key for key in state_dict)
|
| 312 |
-
remapped = remap_transformer_state_dict(state_dict, self._legacy_kwargs, extra_prefixes=extra)
|
| 313 |
-
if self.use_t_tgt and not has_extra:
|
| 314 |
-
strict = False
|
| 315 |
-
if not has_t_embedder:
|
| 316 |
strict = False
|
| 317 |
-
|
| 318 |
-
if not has_t_embedder:
|
| 319 |
-
for name, param in self.named_parameters():
|
| 320 |
-
if "timestep_embedder" in name:
|
| 321 |
-
param.data.zero_()
|
| 322 |
-
return out
|
| 323 |
|
| 324 |
def forward(self, x, y, t_src=None, t_tgt=None):
|
| 325 |
if self.use_t_src and t_src is not None:
|
|
@@ -353,14 +225,7 @@ class GeneratorDeep(nn.Module):
|
|
| 353 |
):
|
| 354 |
super().__init__()
|
| 355 |
self.repeat = repeat
|
| 356 |
-
self.
|
| 357 |
-
"depth": depth,
|
| 358 |
-
"hidden_size": hidden_size,
|
| 359 |
-
"patch_size": patch_size,
|
| 360 |
-
"num_heads": num_heads,
|
| 361 |
-
}
|
| 362 |
-
dit_cls = get_dit_transformer_class()
|
| 363 |
-
self.transformer = dit_cls(
|
| 364 |
**build_dit_config(
|
| 365 |
depth=depth,
|
| 366 |
hidden_size=hidden_size,
|
|
@@ -376,16 +241,7 @@ class GeneratorDeep(nn.Module):
|
|
| 376 |
self.in_channels = in_channels
|
| 377 |
|
| 378 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 379 |
-
|
| 380 |
-
has_t_embedder = any("t_embedder" in key for key in state_dict)
|
| 381 |
-
if not has_t_embedder:
|
| 382 |
-
strict = False
|
| 383 |
-
out = super().load_state_dict(remapped, strict=strict)
|
| 384 |
-
if not has_t_embedder:
|
| 385 |
-
for name, param in self.named_parameters():
|
| 386 |
-
if "timestep_embedder" in name:
|
| 387 |
-
param.data.zero_()
|
| 388 |
-
return out
|
| 389 |
|
| 390 |
def forward(self, x, y, *args, **kwargs):
|
| 391 |
hidden_states = self.transformer.pos_embed(x)
|
|
@@ -429,7 +285,7 @@ class GeneratorDeep(nn.Module):
|
|
| 429 |
return split_output_channels(output, self.in_channels)
|
| 430 |
|
| 431 |
|
| 432 |
-
class AFMGenerator2DModel(
|
| 433 |
config_name = "config.json"
|
| 434 |
|
| 435 |
@register_to_config
|
|
@@ -470,7 +326,7 @@ class AFMGenerator2DModel(Generator, ModelMixin, ConfigMixin):
|
|
| 470 |
)
|
| 471 |
|
| 472 |
|
| 473 |
-
class AFMGeneratorDeep2DModel(
|
| 474 |
config_name = "config.json"
|
| 475 |
|
| 476 |
@register_to_config
|
|
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
|
|
|
| 5 |
import math
|
| 6 |
from typing import Any, Dict, Optional
|
| 7 |
|
|
|
|
| 9 |
import torch.nn.functional as F
|
| 10 |
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
| 11 |
from diffusers.models.modeling_utils import ModelMixin
|
| 12 |
+
from diffusers.models.transformers.dit_transformer_2d import DiTTransformer2DModel
|
| 13 |
from torch import nn
|
| 14 |
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def build_dit_config(
|
| 17 |
*,
|
| 18 |
depth: int = 28,
|
|
|
|
| 48 |
}
|
| 49 |
|
| 50 |
|
| 51 |
+
_PREFIX_KEYS = ("transformer.", "t_tgt_embedder.")
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def _wrap_dit_state_dict(state_dict: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]:
|
| 55 |
+
return {k if k.startswith(_PREFIX_KEYS) else f"transformer.{k}": v for k, v in state_dict.items()}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
def forward_dit_output(
|
| 59 |
+
transformer: DiTTransformer2DModel,
|
| 60 |
hidden_states: torch.Tensor,
|
| 61 |
timestep: Optional[torch.Tensor],
|
| 62 |
class_labels: torch.Tensor,
|
|
|
|
| 106 |
)
|
| 107 |
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
def split_output_channels(output: torch.Tensor, in_channels: int) -> torch.Tensor:
|
| 110 |
if output.shape[1] == in_channels:
|
| 111 |
return output
|
|
|
|
| 141 |
return self.mlp(emb.to(dtype=weight_dtype))
|
| 142 |
|
| 143 |
|
| 144 |
+
class _AFMCheckpointMixin(ModelMixin):
|
| 145 |
+
def _fix_state_dict_keys_on_load(self, state_dict):
|
| 146 |
+
wrapped = _wrap_dit_state_dict(state_dict)
|
| 147 |
+
state_dict.clear()
|
| 148 |
+
state_dict.update(wrapped)
|
| 149 |
+
super()._fix_state_dict_keys_on_load(state_dict)
|
| 150 |
+
|
| 151 |
+
|
| 152 |
class Generator(nn.Module):
|
| 153 |
def __init__(
|
| 154 |
self,
|
|
|
|
| 169 |
super().__init__()
|
| 170 |
self.use_t_src = use_t_src
|
| 171 |
self.use_t_tgt = use_t_tgt
|
| 172 |
+
self.transformer = DiTTransformer2DModel(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
**build_dit_config(
|
| 174 |
depth=depth,
|
| 175 |
hidden_size=hidden_size,
|
|
|
|
| 189 |
nn.init.normal_(self.t_tgt_embedder.mlp[2].weight, std=0.02)
|
| 190 |
|
| 191 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 192 |
+
if self.use_t_tgt and not any(key.startswith("t_tgt_embedder.") for key in state_dict):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
strict = False
|
| 194 |
+
return super().load_state_dict(_wrap_dit_state_dict(state_dict), strict=strict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
def forward(self, x, y, t_src=None, t_tgt=None):
|
| 197 |
if self.use_t_src and t_src is not None:
|
|
|
|
| 225 |
):
|
| 226 |
super().__init__()
|
| 227 |
self.repeat = repeat
|
| 228 |
+
self.transformer = DiTTransformer2DModel(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
**build_dit_config(
|
| 230 |
depth=depth,
|
| 231 |
hidden_size=hidden_size,
|
|
|
|
| 241 |
self.in_channels = in_channels
|
| 242 |
|
| 243 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 244 |
+
return super().load_state_dict(_wrap_dit_state_dict(state_dict), strict=strict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
def forward(self, x, y, *args, **kwargs):
|
| 247 |
hidden_states = self.transformer.pos_embed(x)
|
|
|
|
| 285 |
return split_output_channels(output, self.in_channels)
|
| 286 |
|
| 287 |
|
| 288 |
+
class AFMGenerator2DModel(_AFMCheckpointMixin, Generator, ConfigMixin):
|
| 289 |
config_name = "config.json"
|
| 290 |
|
| 291 |
@register_to_config
|
|
|
|
| 326 |
)
|
| 327 |
|
| 328 |
|
| 329 |
+
class AFMGeneratorDeep2DModel(_AFMCheckpointMixin, GeneratorDeep, ConfigMixin):
|
| 330 |
config_name = "config.json"
|
| 331 |
|
| 332 |
@register_to_config
|
AFM-XL-2-1NFE-noguide/generator/config.json
CHANGED
|
@@ -1,6 +1,22 @@
|
|
| 1 |
{
|
| 2 |
-
"_class_name": "
|
| 3 |
"_diffusers_version": "0.36.0",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
"model_type": "AFM-XL/2",
|
| 5 |
"architecture": "standard",
|
| 6 |
"repeat": 1,
|
|
@@ -12,9 +28,7 @@
|
|
| 12 |
"class_dropout_prob": 0.0,
|
| 13 |
"input_size": 32,
|
| 14 |
"num_classes": 1000,
|
| 15 |
-
"in_channels": 4,
|
| 16 |
"depth": 28,
|
| 17 |
"hidden_size": 1152,
|
| 18 |
-
"patch_size": 2,
|
| 19 |
"num_heads": 16
|
| 20 |
}
|
|
|
|
| 1 |
{
|
| 2 |
+
"_class_name": "DiTTransformer2DModel",
|
| 3 |
"_diffusers_version": "0.36.0",
|
| 4 |
+
"sample_size": 32,
|
| 5 |
+
"num_layers": 28,
|
| 6 |
+
"num_attention_heads": 16,
|
| 7 |
+
"attention_head_dim": 72,
|
| 8 |
+
"in_channels": 4,
|
| 9 |
+
"out_channels": 4,
|
| 10 |
+
"patch_size": 2,
|
| 11 |
+
"attention_bias": true,
|
| 12 |
+
"activation_fn": "gelu-approximate",
|
| 13 |
+
"num_embeds_ada_norm": 1000,
|
| 14 |
+
"norm_type": "ada_norm_zero",
|
| 15 |
+
"norm_elementwise_affine": false,
|
| 16 |
+
"dropout": 0.0,
|
| 17 |
+
"norm_num_groups": 32,
|
| 18 |
+
"norm_eps": 1e-05,
|
| 19 |
+
"upcast_attention": false,
|
| 20 |
"model_type": "AFM-XL/2",
|
| 21 |
"architecture": "standard",
|
| 22 |
"repeat": 1,
|
|
|
|
| 28 |
"class_dropout_prob": 0.0,
|
| 29 |
"input_size": 32,
|
| 30 |
"num_classes": 1000,
|
|
|
|
| 31 |
"depth": 28,
|
| 32 |
"hidden_size": 1152,
|
|
|
|
| 33 |
"num_heads": 16
|
| 34 |
}
|
AFM-XL-2-1NFE-noguide/generator/diffusion_pytorch_model.safetensors
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1582a0f05105962729018de245acd95d74651ab3b5bdec9c0aed52ec6c7bf5cd
|
| 3 |
+
size 2999296584
|
AFM-XL-2-1NFE-noguide/generator/modeling_afm.py
CHANGED
|
@@ -2,7 +2,6 @@
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
-
import copy
|
| 6 |
import math
|
| 7 |
from typing import Any, Dict, Optional
|
| 8 |
|
|
@@ -10,15 +9,10 @@ import torch
|
|
| 10 |
import torch.nn.functional as F
|
| 11 |
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
| 12 |
from diffusers.models.modeling_utils import ModelMixin
|
|
|
|
| 13 |
from torch import nn
|
| 14 |
|
| 15 |
|
| 16 |
-
def get_dit_transformer_class():
|
| 17 |
-
from diffusers.models.transformers.dit_transformer_2d import DiTTransformer2DModel
|
| 18 |
-
|
| 19 |
-
return DiTTransformer2DModel
|
| 20 |
-
|
| 21 |
-
|
| 22 |
def build_dit_config(
|
| 23 |
*,
|
| 24 |
depth: int = 28,
|
|
@@ -54,110 +48,15 @@ def build_dit_config(
|
|
| 54 |
}
|
| 55 |
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
if "t_embedder.mlp.0.weight" in converted:
|
| 64 |
-
timestep_weights = {
|
| 65 |
-
"linear_1.weight": converted.pop("t_embedder.mlp.0.weight"),
|
| 66 |
-
"linear_1.bias": converted.pop("t_embedder.mlp.0.bias"),
|
| 67 |
-
"linear_2.weight": converted.pop("t_embedder.mlp.2.weight"),
|
| 68 |
-
"linear_2.bias": converted.pop("t_embedder.mlp.2.bias"),
|
| 69 |
-
}
|
| 70 |
-
else:
|
| 71 |
-
timestep_weights = None
|
| 72 |
-
class_embedding = converted.pop("y_embedder.embedding_table.weight")
|
| 73 |
-
if class_embedding.shape[0] == 1000:
|
| 74 |
-
null_class = torch.zeros(1, class_embedding.shape[1], dtype=class_embedding.dtype)
|
| 75 |
-
class_embedding = torch.cat([class_embedding, null_class], dim=0)
|
| 76 |
-
|
| 77 |
-
for block_idx in range(depth):
|
| 78 |
-
if timestep_weights is not None:
|
| 79 |
-
for key, tensor in timestep_weights.items():
|
| 80 |
-
converted[f"transformer_blocks.{block_idx}.norm1.emb.timestep_embedder.{key}"] = tensor.clone()
|
| 81 |
-
converted[f"transformer_blocks.{block_idx}.norm1.emb.class_embedder.embedding_table.weight"] = (
|
| 82 |
-
class_embedding.clone()
|
| 83 |
-
)
|
| 84 |
-
converted[f"transformer_blocks.{block_idx}.norm1.linear.weight"] = converted[
|
| 85 |
-
f"blocks.{block_idx}.adaLN_modulation.1.weight"
|
| 86 |
-
]
|
| 87 |
-
converted[f"transformer_blocks.{block_idx}.norm1.linear.bias"] = converted[
|
| 88 |
-
f"blocks.{block_idx}.adaLN_modulation.1.bias"
|
| 89 |
-
]
|
| 90 |
-
|
| 91 |
-
q, k, v = torch.chunk(converted[f"blocks.{block_idx}.attn.qkv.weight"], 3, dim=0)
|
| 92 |
-
q_bias, k_bias, v_bias = torch.chunk(converted[f"blocks.{block_idx}.attn.qkv.bias"], 3, dim=0)
|
| 93 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_q.weight"] = q
|
| 94 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_q.bias"] = q_bias
|
| 95 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_k.weight"] = k
|
| 96 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_k.bias"] = k_bias
|
| 97 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_v.weight"] = v
|
| 98 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_v.bias"] = v_bias
|
| 99 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_out.0.weight"] = converted[
|
| 100 |
-
f"blocks.{block_idx}.attn.proj.weight"
|
| 101 |
-
]
|
| 102 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_out.0.bias"] = converted[
|
| 103 |
-
f"blocks.{block_idx}.attn.proj.bias"
|
| 104 |
-
]
|
| 105 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.0.proj.weight"] = converted[
|
| 106 |
-
f"blocks.{block_idx}.mlp.fc1.weight"
|
| 107 |
-
]
|
| 108 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.0.proj.bias"] = converted[
|
| 109 |
-
f"blocks.{block_idx}.mlp.fc1.bias"
|
| 110 |
-
]
|
| 111 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.2.weight"] = converted[
|
| 112 |
-
f"blocks.{block_idx}.mlp.fc2.weight"
|
| 113 |
-
]
|
| 114 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.2.bias"] = converted[
|
| 115 |
-
f"blocks.{block_idx}.mlp.fc2.bias"
|
| 116 |
-
]
|
| 117 |
-
|
| 118 |
-
for suffix in (
|
| 119 |
-
"attn.qkv.weight",
|
| 120 |
-
"attn.qkv.bias",
|
| 121 |
-
"attn.proj.weight",
|
| 122 |
-
"attn.proj.bias",
|
| 123 |
-
"mlp.fc1.weight",
|
| 124 |
-
"mlp.fc1.bias",
|
| 125 |
-
"mlp.fc2.weight",
|
| 126 |
-
"mlp.fc2.bias",
|
| 127 |
-
"adaLN_modulation.1.weight",
|
| 128 |
-
"adaLN_modulation.1.bias",
|
| 129 |
-
):
|
| 130 |
-
converted.pop(f"blocks.{block_idx}.{suffix}", None)
|
| 131 |
-
|
| 132 |
-
converted["proj_out_1.weight"] = converted.pop("final_layer.adaLN_modulation.1.weight")
|
| 133 |
-
converted["proj_out_1.bias"] = converted.pop("final_layer.adaLN_modulation.1.bias")
|
| 134 |
-
converted["proj_out_2.weight"] = converted.pop("final_layer.linear.weight")
|
| 135 |
-
converted["proj_out_2.bias"] = converted.pop("final_layer.linear.bias")
|
| 136 |
-
|
| 137 |
-
converted.pop("pos_embed", None)
|
| 138 |
-
for block_idx in range(depth):
|
| 139 |
-
for suffix in ("norm1.weight", "norm1.bias", "norm2.weight", "norm2.bias"):
|
| 140 |
-
converted.pop(f"blocks.{block_idx}.{suffix}", None)
|
| 141 |
-
|
| 142 |
-
return {key: tensor.detach().clone().contiguous() for key, tensor in converted.items()}
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
def load_legacy_dit_state_dict(
|
| 146 |
-
state_dict: Dict[str, torch.Tensor],
|
| 147 |
-
depth: int,
|
| 148 |
-
patch_size: int,
|
| 149 |
-
hidden_size: int,
|
| 150 |
-
num_heads: int,
|
| 151 |
-
) -> Dict[str, torch.Tensor]:
|
| 152 |
-
if "transformer_blocks.0.attn1.to_q.weight" in state_dict:
|
| 153 |
-
return state_dict
|
| 154 |
-
if any(key.startswith("blocks.") for key in state_dict):
|
| 155 |
-
return _convert_legacy_dit_state_dict(state_dict, depth)
|
| 156 |
-
return state_dict
|
| 157 |
|
| 158 |
|
| 159 |
def forward_dit_output(
|
| 160 |
-
transformer,
|
| 161 |
hidden_states: torch.Tensor,
|
| 162 |
timestep: Optional[torch.Tensor],
|
| 163 |
class_labels: torch.Tensor,
|
|
@@ -207,23 +106,6 @@ def forward_dit_output(
|
|
| 207 |
)
|
| 208 |
|
| 209 |
|
| 210 |
-
def remap_transformer_state_dict(
|
| 211 |
-
state_dict: Dict[str, torch.Tensor],
|
| 212 |
-
legacy_kwargs: Dict[str, int],
|
| 213 |
-
extra_prefixes: tuple[str, ...] = (),
|
| 214 |
-
) -> Dict[str, torch.Tensor]:
|
| 215 |
-
state_dict = load_legacy_dit_state_dict(state_dict, **legacy_kwargs)
|
| 216 |
-
remapped: Dict[str, torch.Tensor] = {}
|
| 217 |
-
for key, value in state_dict.items():
|
| 218 |
-
if any(key.startswith(prefix) for prefix in extra_prefixes):
|
| 219 |
-
remapped[key] = value
|
| 220 |
-
elif key.startswith("transformer."):
|
| 221 |
-
remapped[key] = value
|
| 222 |
-
else:
|
| 223 |
-
remapped[f"transformer.{key}"] = value
|
| 224 |
-
return remapped
|
| 225 |
-
|
| 226 |
-
|
| 227 |
def split_output_channels(output: torch.Tensor, in_channels: int) -> torch.Tensor:
|
| 228 |
if output.shape[1] == in_channels:
|
| 229 |
return output
|
|
@@ -259,6 +141,14 @@ class TimestepEmbedder(nn.Module):
|
|
| 259 |
return self.mlp(emb.to(dtype=weight_dtype))
|
| 260 |
|
| 261 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
class Generator(nn.Module):
|
| 263 |
def __init__(
|
| 264 |
self,
|
|
@@ -279,14 +169,7 @@ class Generator(nn.Module):
|
|
| 279 |
super().__init__()
|
| 280 |
self.use_t_src = use_t_src
|
| 281 |
self.use_t_tgt = use_t_tgt
|
| 282 |
-
self.
|
| 283 |
-
"depth": depth,
|
| 284 |
-
"hidden_size": hidden_size,
|
| 285 |
-
"patch_size": patch_size,
|
| 286 |
-
"num_heads": num_heads,
|
| 287 |
-
}
|
| 288 |
-
dit_cls = get_dit_transformer_class()
|
| 289 |
-
self.transformer = dit_cls(
|
| 290 |
**build_dit_config(
|
| 291 |
depth=depth,
|
| 292 |
hidden_size=hidden_size,
|
|
@@ -306,20 +189,9 @@ class Generator(nn.Module):
|
|
| 306 |
nn.init.normal_(self.t_tgt_embedder.mlp[2].weight, std=0.02)
|
| 307 |
|
| 308 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 309 |
-
|
| 310 |
-
has_extra = any(key.startswith(extra) for key in state_dict)
|
| 311 |
-
has_t_embedder = any("t_embedder" in key for key in state_dict)
|
| 312 |
-
remapped = remap_transformer_state_dict(state_dict, self._legacy_kwargs, extra_prefixes=extra)
|
| 313 |
-
if self.use_t_tgt and not has_extra:
|
| 314 |
-
strict = False
|
| 315 |
-
if not has_t_embedder:
|
| 316 |
strict = False
|
| 317 |
-
|
| 318 |
-
if not has_t_embedder:
|
| 319 |
-
for name, param in self.named_parameters():
|
| 320 |
-
if "timestep_embedder" in name:
|
| 321 |
-
param.data.zero_()
|
| 322 |
-
return out
|
| 323 |
|
| 324 |
def forward(self, x, y, t_src=None, t_tgt=None):
|
| 325 |
if self.use_t_src and t_src is not None:
|
|
@@ -353,14 +225,7 @@ class GeneratorDeep(nn.Module):
|
|
| 353 |
):
|
| 354 |
super().__init__()
|
| 355 |
self.repeat = repeat
|
| 356 |
-
self.
|
| 357 |
-
"depth": depth,
|
| 358 |
-
"hidden_size": hidden_size,
|
| 359 |
-
"patch_size": patch_size,
|
| 360 |
-
"num_heads": num_heads,
|
| 361 |
-
}
|
| 362 |
-
dit_cls = get_dit_transformer_class()
|
| 363 |
-
self.transformer = dit_cls(
|
| 364 |
**build_dit_config(
|
| 365 |
depth=depth,
|
| 366 |
hidden_size=hidden_size,
|
|
@@ -376,16 +241,7 @@ class GeneratorDeep(nn.Module):
|
|
| 376 |
self.in_channels = in_channels
|
| 377 |
|
| 378 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 379 |
-
|
| 380 |
-
has_t_embedder = any("t_embedder" in key for key in state_dict)
|
| 381 |
-
if not has_t_embedder:
|
| 382 |
-
strict = False
|
| 383 |
-
out = super().load_state_dict(remapped, strict=strict)
|
| 384 |
-
if not has_t_embedder:
|
| 385 |
-
for name, param in self.named_parameters():
|
| 386 |
-
if "timestep_embedder" in name:
|
| 387 |
-
param.data.zero_()
|
| 388 |
-
return out
|
| 389 |
|
| 390 |
def forward(self, x, y, *args, **kwargs):
|
| 391 |
hidden_states = self.transformer.pos_embed(x)
|
|
@@ -429,7 +285,7 @@ class GeneratorDeep(nn.Module):
|
|
| 429 |
return split_output_channels(output, self.in_channels)
|
| 430 |
|
| 431 |
|
| 432 |
-
class AFMGenerator2DModel(
|
| 433 |
config_name = "config.json"
|
| 434 |
|
| 435 |
@register_to_config
|
|
@@ -470,7 +326,7 @@ class AFMGenerator2DModel(Generator, ModelMixin, ConfigMixin):
|
|
| 470 |
)
|
| 471 |
|
| 472 |
|
| 473 |
-
class AFMGeneratorDeep2DModel(
|
| 474 |
config_name = "config.json"
|
| 475 |
|
| 476 |
@register_to_config
|
|
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
|
|
|
| 5 |
import math
|
| 6 |
from typing import Any, Dict, Optional
|
| 7 |
|
|
|
|
| 9 |
import torch.nn.functional as F
|
| 10 |
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
| 11 |
from diffusers.models.modeling_utils import ModelMixin
|
| 12 |
+
from diffusers.models.transformers.dit_transformer_2d import DiTTransformer2DModel
|
| 13 |
from torch import nn
|
| 14 |
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def build_dit_config(
|
| 17 |
*,
|
| 18 |
depth: int = 28,
|
|
|
|
| 48 |
}
|
| 49 |
|
| 50 |
|
| 51 |
+
_PREFIX_KEYS = ("transformer.", "t_tgt_embedder.")
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def _wrap_dit_state_dict(state_dict: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]:
|
| 55 |
+
return {k if k.startswith(_PREFIX_KEYS) else f"transformer.{k}": v for k, v in state_dict.items()}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
def forward_dit_output(
|
| 59 |
+
transformer: DiTTransformer2DModel,
|
| 60 |
hidden_states: torch.Tensor,
|
| 61 |
timestep: Optional[torch.Tensor],
|
| 62 |
class_labels: torch.Tensor,
|
|
|
|
| 106 |
)
|
| 107 |
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
def split_output_channels(output: torch.Tensor, in_channels: int) -> torch.Tensor:
|
| 110 |
if output.shape[1] == in_channels:
|
| 111 |
return output
|
|
|
|
| 141 |
return self.mlp(emb.to(dtype=weight_dtype))
|
| 142 |
|
| 143 |
|
| 144 |
+
class _AFMCheckpointMixin(ModelMixin):
|
| 145 |
+
def _fix_state_dict_keys_on_load(self, state_dict):
|
| 146 |
+
wrapped = _wrap_dit_state_dict(state_dict)
|
| 147 |
+
state_dict.clear()
|
| 148 |
+
state_dict.update(wrapped)
|
| 149 |
+
super()._fix_state_dict_keys_on_load(state_dict)
|
| 150 |
+
|
| 151 |
+
|
| 152 |
class Generator(nn.Module):
|
| 153 |
def __init__(
|
| 154 |
self,
|
|
|
|
| 169 |
super().__init__()
|
| 170 |
self.use_t_src = use_t_src
|
| 171 |
self.use_t_tgt = use_t_tgt
|
| 172 |
+
self.transformer = DiTTransformer2DModel(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
**build_dit_config(
|
| 174 |
depth=depth,
|
| 175 |
hidden_size=hidden_size,
|
|
|
|
| 189 |
nn.init.normal_(self.t_tgt_embedder.mlp[2].weight, std=0.02)
|
| 190 |
|
| 191 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 192 |
+
if self.use_t_tgt and not any(key.startswith("t_tgt_embedder.") for key in state_dict):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
strict = False
|
| 194 |
+
return super().load_state_dict(_wrap_dit_state_dict(state_dict), strict=strict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
def forward(self, x, y, t_src=None, t_tgt=None):
|
| 197 |
if self.use_t_src and t_src is not None:
|
|
|
|
| 225 |
):
|
| 226 |
super().__init__()
|
| 227 |
self.repeat = repeat
|
| 228 |
+
self.transformer = DiTTransformer2DModel(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
**build_dit_config(
|
| 230 |
depth=depth,
|
| 231 |
hidden_size=hidden_size,
|
|
|
|
| 241 |
self.in_channels = in_channels
|
| 242 |
|
| 243 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 244 |
+
return super().load_state_dict(_wrap_dit_state_dict(state_dict), strict=strict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
def forward(self, x, y, *args, **kwargs):
|
| 247 |
hidden_states = self.transformer.pos_embed(x)
|
|
|
|
| 285 |
return split_output_channels(output, self.in_channels)
|
| 286 |
|
| 287 |
|
| 288 |
+
class AFMGenerator2DModel(_AFMCheckpointMixin, Generator, ConfigMixin):
|
| 289 |
config_name = "config.json"
|
| 290 |
|
| 291 |
@register_to_config
|
|
|
|
| 326 |
)
|
| 327 |
|
| 328 |
|
| 329 |
+
class AFMGeneratorDeep2DModel(_AFMCheckpointMixin, GeneratorDeep, ConfigMixin):
|
| 330 |
config_name = "config.json"
|
| 331 |
|
| 332 |
@register_to_config
|
AFM-XL-2-2NFE-guided/generator/config.json
CHANGED
|
@@ -1,6 +1,22 @@
|
|
| 1 |
{
|
| 2 |
-
"_class_name": "
|
| 3 |
"_diffusers_version": "0.36.0",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
"model_type": "AFM-XL/2",
|
| 5 |
"architecture": "standard",
|
| 6 |
"repeat": 1,
|
|
@@ -12,9 +28,7 @@
|
|
| 12 |
"class_dropout_prob": 0.0,
|
| 13 |
"input_size": 32,
|
| 14 |
"num_classes": 1000,
|
| 15 |
-
"in_channels": 4,
|
| 16 |
"depth": 28,
|
| 17 |
"hidden_size": 1152,
|
| 18 |
-
"patch_size": 2,
|
| 19 |
"num_heads": 16
|
| 20 |
}
|
|
|
|
| 1 |
{
|
| 2 |
+
"_class_name": "DiTTransformer2DModel",
|
| 3 |
"_diffusers_version": "0.36.0",
|
| 4 |
+
"sample_size": 32,
|
| 5 |
+
"num_layers": 28,
|
| 6 |
+
"num_attention_heads": 16,
|
| 7 |
+
"attention_head_dim": 72,
|
| 8 |
+
"in_channels": 4,
|
| 9 |
+
"out_channels": 4,
|
| 10 |
+
"patch_size": 2,
|
| 11 |
+
"attention_bias": true,
|
| 12 |
+
"activation_fn": "gelu-approximate",
|
| 13 |
+
"num_embeds_ada_norm": 1000,
|
| 14 |
+
"norm_type": "ada_norm_zero",
|
| 15 |
+
"norm_elementwise_affine": false,
|
| 16 |
+
"dropout": 0.0,
|
| 17 |
+
"norm_num_groups": 32,
|
| 18 |
+
"norm_eps": 1e-05,
|
| 19 |
+
"upcast_attention": false,
|
| 20 |
"model_type": "AFM-XL/2",
|
| 21 |
"architecture": "standard",
|
| 22 |
"repeat": 1,
|
|
|
|
| 28 |
"class_dropout_prob": 0.0,
|
| 29 |
"input_size": 32,
|
| 30 |
"num_classes": 1000,
|
|
|
|
| 31 |
"depth": 28,
|
| 32 |
"hidden_size": 1152,
|
|
|
|
| 33 |
"num_heads": 16
|
| 34 |
}
|
AFM-XL-2-2NFE-guided/generator/diffusion_pytorch_model.safetensors
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d5b5c2e9fceff4d97eaad3e150878d765b4121b44b0c782c7c62145f2e2219bb
|
| 3 |
+
size 2999296584
|
AFM-XL-2-2NFE-guided/generator/modeling_afm.py
CHANGED
|
@@ -2,7 +2,6 @@
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
-
import copy
|
| 6 |
import math
|
| 7 |
from typing import Any, Dict, Optional
|
| 8 |
|
|
@@ -10,15 +9,10 @@ import torch
|
|
| 10 |
import torch.nn.functional as F
|
| 11 |
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
| 12 |
from diffusers.models.modeling_utils import ModelMixin
|
|
|
|
| 13 |
from torch import nn
|
| 14 |
|
| 15 |
|
| 16 |
-
def get_dit_transformer_class():
|
| 17 |
-
from diffusers.models.transformers.dit_transformer_2d import DiTTransformer2DModel
|
| 18 |
-
|
| 19 |
-
return DiTTransformer2DModel
|
| 20 |
-
|
| 21 |
-
|
| 22 |
def build_dit_config(
|
| 23 |
*,
|
| 24 |
depth: int = 28,
|
|
@@ -54,110 +48,15 @@ def build_dit_config(
|
|
| 54 |
}
|
| 55 |
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
if "t_embedder.mlp.0.weight" in converted:
|
| 64 |
-
timestep_weights = {
|
| 65 |
-
"linear_1.weight": converted.pop("t_embedder.mlp.0.weight"),
|
| 66 |
-
"linear_1.bias": converted.pop("t_embedder.mlp.0.bias"),
|
| 67 |
-
"linear_2.weight": converted.pop("t_embedder.mlp.2.weight"),
|
| 68 |
-
"linear_2.bias": converted.pop("t_embedder.mlp.2.bias"),
|
| 69 |
-
}
|
| 70 |
-
else:
|
| 71 |
-
timestep_weights = None
|
| 72 |
-
class_embedding = converted.pop("y_embedder.embedding_table.weight")
|
| 73 |
-
if class_embedding.shape[0] == 1000:
|
| 74 |
-
null_class = torch.zeros(1, class_embedding.shape[1], dtype=class_embedding.dtype)
|
| 75 |
-
class_embedding = torch.cat([class_embedding, null_class], dim=0)
|
| 76 |
-
|
| 77 |
-
for block_idx in range(depth):
|
| 78 |
-
if timestep_weights is not None:
|
| 79 |
-
for key, tensor in timestep_weights.items():
|
| 80 |
-
converted[f"transformer_blocks.{block_idx}.norm1.emb.timestep_embedder.{key}"] = tensor.clone()
|
| 81 |
-
converted[f"transformer_blocks.{block_idx}.norm1.emb.class_embedder.embedding_table.weight"] = (
|
| 82 |
-
class_embedding.clone()
|
| 83 |
-
)
|
| 84 |
-
converted[f"transformer_blocks.{block_idx}.norm1.linear.weight"] = converted[
|
| 85 |
-
f"blocks.{block_idx}.adaLN_modulation.1.weight"
|
| 86 |
-
]
|
| 87 |
-
converted[f"transformer_blocks.{block_idx}.norm1.linear.bias"] = converted[
|
| 88 |
-
f"blocks.{block_idx}.adaLN_modulation.1.bias"
|
| 89 |
-
]
|
| 90 |
-
|
| 91 |
-
q, k, v = torch.chunk(converted[f"blocks.{block_idx}.attn.qkv.weight"], 3, dim=0)
|
| 92 |
-
q_bias, k_bias, v_bias = torch.chunk(converted[f"blocks.{block_idx}.attn.qkv.bias"], 3, dim=0)
|
| 93 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_q.weight"] = q
|
| 94 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_q.bias"] = q_bias
|
| 95 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_k.weight"] = k
|
| 96 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_k.bias"] = k_bias
|
| 97 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_v.weight"] = v
|
| 98 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_v.bias"] = v_bias
|
| 99 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_out.0.weight"] = converted[
|
| 100 |
-
f"blocks.{block_idx}.attn.proj.weight"
|
| 101 |
-
]
|
| 102 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_out.0.bias"] = converted[
|
| 103 |
-
f"blocks.{block_idx}.attn.proj.bias"
|
| 104 |
-
]
|
| 105 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.0.proj.weight"] = converted[
|
| 106 |
-
f"blocks.{block_idx}.mlp.fc1.weight"
|
| 107 |
-
]
|
| 108 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.0.proj.bias"] = converted[
|
| 109 |
-
f"blocks.{block_idx}.mlp.fc1.bias"
|
| 110 |
-
]
|
| 111 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.2.weight"] = converted[
|
| 112 |
-
f"blocks.{block_idx}.mlp.fc2.weight"
|
| 113 |
-
]
|
| 114 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.2.bias"] = converted[
|
| 115 |
-
f"blocks.{block_idx}.mlp.fc2.bias"
|
| 116 |
-
]
|
| 117 |
-
|
| 118 |
-
for suffix in (
|
| 119 |
-
"attn.qkv.weight",
|
| 120 |
-
"attn.qkv.bias",
|
| 121 |
-
"attn.proj.weight",
|
| 122 |
-
"attn.proj.bias",
|
| 123 |
-
"mlp.fc1.weight",
|
| 124 |
-
"mlp.fc1.bias",
|
| 125 |
-
"mlp.fc2.weight",
|
| 126 |
-
"mlp.fc2.bias",
|
| 127 |
-
"adaLN_modulation.1.weight",
|
| 128 |
-
"adaLN_modulation.1.bias",
|
| 129 |
-
):
|
| 130 |
-
converted.pop(f"blocks.{block_idx}.{suffix}", None)
|
| 131 |
-
|
| 132 |
-
converted["proj_out_1.weight"] = converted.pop("final_layer.adaLN_modulation.1.weight")
|
| 133 |
-
converted["proj_out_1.bias"] = converted.pop("final_layer.adaLN_modulation.1.bias")
|
| 134 |
-
converted["proj_out_2.weight"] = converted.pop("final_layer.linear.weight")
|
| 135 |
-
converted["proj_out_2.bias"] = converted.pop("final_layer.linear.bias")
|
| 136 |
-
|
| 137 |
-
converted.pop("pos_embed", None)
|
| 138 |
-
for block_idx in range(depth):
|
| 139 |
-
for suffix in ("norm1.weight", "norm1.bias", "norm2.weight", "norm2.bias"):
|
| 140 |
-
converted.pop(f"blocks.{block_idx}.{suffix}", None)
|
| 141 |
-
|
| 142 |
-
return {key: tensor.detach().clone().contiguous() for key, tensor in converted.items()}
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
def load_legacy_dit_state_dict(
|
| 146 |
-
state_dict: Dict[str, torch.Tensor],
|
| 147 |
-
depth: int,
|
| 148 |
-
patch_size: int,
|
| 149 |
-
hidden_size: int,
|
| 150 |
-
num_heads: int,
|
| 151 |
-
) -> Dict[str, torch.Tensor]:
|
| 152 |
-
if "transformer_blocks.0.attn1.to_q.weight" in state_dict:
|
| 153 |
-
return state_dict
|
| 154 |
-
if any(key.startswith("blocks.") for key in state_dict):
|
| 155 |
-
return _convert_legacy_dit_state_dict(state_dict, depth)
|
| 156 |
-
return state_dict
|
| 157 |
|
| 158 |
|
| 159 |
def forward_dit_output(
|
| 160 |
-
transformer,
|
| 161 |
hidden_states: torch.Tensor,
|
| 162 |
timestep: Optional[torch.Tensor],
|
| 163 |
class_labels: torch.Tensor,
|
|
@@ -207,23 +106,6 @@ def forward_dit_output(
|
|
| 207 |
)
|
| 208 |
|
| 209 |
|
| 210 |
-
def remap_transformer_state_dict(
|
| 211 |
-
state_dict: Dict[str, torch.Tensor],
|
| 212 |
-
legacy_kwargs: Dict[str, int],
|
| 213 |
-
extra_prefixes: tuple[str, ...] = (),
|
| 214 |
-
) -> Dict[str, torch.Tensor]:
|
| 215 |
-
state_dict = load_legacy_dit_state_dict(state_dict, **legacy_kwargs)
|
| 216 |
-
remapped: Dict[str, torch.Tensor] = {}
|
| 217 |
-
for key, value in state_dict.items():
|
| 218 |
-
if any(key.startswith(prefix) for prefix in extra_prefixes):
|
| 219 |
-
remapped[key] = value
|
| 220 |
-
elif key.startswith("transformer."):
|
| 221 |
-
remapped[key] = value
|
| 222 |
-
else:
|
| 223 |
-
remapped[f"transformer.{key}"] = value
|
| 224 |
-
return remapped
|
| 225 |
-
|
| 226 |
-
|
| 227 |
def split_output_channels(output: torch.Tensor, in_channels: int) -> torch.Tensor:
|
| 228 |
if output.shape[1] == in_channels:
|
| 229 |
return output
|
|
@@ -259,6 +141,14 @@ class TimestepEmbedder(nn.Module):
|
|
| 259 |
return self.mlp(emb.to(dtype=weight_dtype))
|
| 260 |
|
| 261 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
class Generator(nn.Module):
|
| 263 |
def __init__(
|
| 264 |
self,
|
|
@@ -279,14 +169,7 @@ class Generator(nn.Module):
|
|
| 279 |
super().__init__()
|
| 280 |
self.use_t_src = use_t_src
|
| 281 |
self.use_t_tgt = use_t_tgt
|
| 282 |
-
self.
|
| 283 |
-
"depth": depth,
|
| 284 |
-
"hidden_size": hidden_size,
|
| 285 |
-
"patch_size": patch_size,
|
| 286 |
-
"num_heads": num_heads,
|
| 287 |
-
}
|
| 288 |
-
dit_cls = get_dit_transformer_class()
|
| 289 |
-
self.transformer = dit_cls(
|
| 290 |
**build_dit_config(
|
| 291 |
depth=depth,
|
| 292 |
hidden_size=hidden_size,
|
|
@@ -306,20 +189,9 @@ class Generator(nn.Module):
|
|
| 306 |
nn.init.normal_(self.t_tgt_embedder.mlp[2].weight, std=0.02)
|
| 307 |
|
| 308 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 309 |
-
|
| 310 |
-
has_extra = any(key.startswith(extra) for key in state_dict)
|
| 311 |
-
has_t_embedder = any("t_embedder" in key for key in state_dict)
|
| 312 |
-
remapped = remap_transformer_state_dict(state_dict, self._legacy_kwargs, extra_prefixes=extra)
|
| 313 |
-
if self.use_t_tgt and not has_extra:
|
| 314 |
-
strict = False
|
| 315 |
-
if not has_t_embedder:
|
| 316 |
strict = False
|
| 317 |
-
|
| 318 |
-
if not has_t_embedder:
|
| 319 |
-
for name, param in self.named_parameters():
|
| 320 |
-
if "timestep_embedder" in name:
|
| 321 |
-
param.data.zero_()
|
| 322 |
-
return out
|
| 323 |
|
| 324 |
def forward(self, x, y, t_src=None, t_tgt=None):
|
| 325 |
if self.use_t_src and t_src is not None:
|
|
@@ -353,14 +225,7 @@ class GeneratorDeep(nn.Module):
|
|
| 353 |
):
|
| 354 |
super().__init__()
|
| 355 |
self.repeat = repeat
|
| 356 |
-
self.
|
| 357 |
-
"depth": depth,
|
| 358 |
-
"hidden_size": hidden_size,
|
| 359 |
-
"patch_size": patch_size,
|
| 360 |
-
"num_heads": num_heads,
|
| 361 |
-
}
|
| 362 |
-
dit_cls = get_dit_transformer_class()
|
| 363 |
-
self.transformer = dit_cls(
|
| 364 |
**build_dit_config(
|
| 365 |
depth=depth,
|
| 366 |
hidden_size=hidden_size,
|
|
@@ -376,16 +241,7 @@ class GeneratorDeep(nn.Module):
|
|
| 376 |
self.in_channels = in_channels
|
| 377 |
|
| 378 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 379 |
-
|
| 380 |
-
has_t_embedder = any("t_embedder" in key for key in state_dict)
|
| 381 |
-
if not has_t_embedder:
|
| 382 |
-
strict = False
|
| 383 |
-
out = super().load_state_dict(remapped, strict=strict)
|
| 384 |
-
if not has_t_embedder:
|
| 385 |
-
for name, param in self.named_parameters():
|
| 386 |
-
if "timestep_embedder" in name:
|
| 387 |
-
param.data.zero_()
|
| 388 |
-
return out
|
| 389 |
|
| 390 |
def forward(self, x, y, *args, **kwargs):
|
| 391 |
hidden_states = self.transformer.pos_embed(x)
|
|
@@ -429,7 +285,7 @@ class GeneratorDeep(nn.Module):
|
|
| 429 |
return split_output_channels(output, self.in_channels)
|
| 430 |
|
| 431 |
|
| 432 |
-
class AFMGenerator2DModel(
|
| 433 |
config_name = "config.json"
|
| 434 |
|
| 435 |
@register_to_config
|
|
@@ -470,7 +326,7 @@ class AFMGenerator2DModel(Generator, ModelMixin, ConfigMixin):
|
|
| 470 |
)
|
| 471 |
|
| 472 |
|
| 473 |
-
class AFMGeneratorDeep2DModel(
|
| 474 |
config_name = "config.json"
|
| 475 |
|
| 476 |
@register_to_config
|
|
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
|
|
|
| 5 |
import math
|
| 6 |
from typing import Any, Dict, Optional
|
| 7 |
|
|
|
|
| 9 |
import torch.nn.functional as F
|
| 10 |
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
| 11 |
from diffusers.models.modeling_utils import ModelMixin
|
| 12 |
+
from diffusers.models.transformers.dit_transformer_2d import DiTTransformer2DModel
|
| 13 |
from torch import nn
|
| 14 |
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def build_dit_config(
|
| 17 |
*,
|
| 18 |
depth: int = 28,
|
|
|
|
| 48 |
}
|
| 49 |
|
| 50 |
|
| 51 |
+
_PREFIX_KEYS = ("transformer.", "t_tgt_embedder.")
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def _wrap_dit_state_dict(state_dict: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]:
|
| 55 |
+
return {k if k.startswith(_PREFIX_KEYS) else f"transformer.{k}": v for k, v in state_dict.items()}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
def forward_dit_output(
|
| 59 |
+
transformer: DiTTransformer2DModel,
|
| 60 |
hidden_states: torch.Tensor,
|
| 61 |
timestep: Optional[torch.Tensor],
|
| 62 |
class_labels: torch.Tensor,
|
|
|
|
| 106 |
)
|
| 107 |
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
def split_output_channels(output: torch.Tensor, in_channels: int) -> torch.Tensor:
|
| 110 |
if output.shape[1] == in_channels:
|
| 111 |
return output
|
|
|
|
| 141 |
return self.mlp(emb.to(dtype=weight_dtype))
|
| 142 |
|
| 143 |
|
| 144 |
+
class _AFMCheckpointMixin(ModelMixin):
|
| 145 |
+
def _fix_state_dict_keys_on_load(self, state_dict):
|
| 146 |
+
wrapped = _wrap_dit_state_dict(state_dict)
|
| 147 |
+
state_dict.clear()
|
| 148 |
+
state_dict.update(wrapped)
|
| 149 |
+
super()._fix_state_dict_keys_on_load(state_dict)
|
| 150 |
+
|
| 151 |
+
|
| 152 |
class Generator(nn.Module):
|
| 153 |
def __init__(
|
| 154 |
self,
|
|
|
|
| 169 |
super().__init__()
|
| 170 |
self.use_t_src = use_t_src
|
| 171 |
self.use_t_tgt = use_t_tgt
|
| 172 |
+
self.transformer = DiTTransformer2DModel(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
**build_dit_config(
|
| 174 |
depth=depth,
|
| 175 |
hidden_size=hidden_size,
|
|
|
|
| 189 |
nn.init.normal_(self.t_tgt_embedder.mlp[2].weight, std=0.02)
|
| 190 |
|
| 191 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 192 |
+
if self.use_t_tgt and not any(key.startswith("t_tgt_embedder.") for key in state_dict):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
strict = False
|
| 194 |
+
return super().load_state_dict(_wrap_dit_state_dict(state_dict), strict=strict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
def forward(self, x, y, t_src=None, t_tgt=None):
|
| 197 |
if self.use_t_src and t_src is not None:
|
|
|
|
| 225 |
):
|
| 226 |
super().__init__()
|
| 227 |
self.repeat = repeat
|
| 228 |
+
self.transformer = DiTTransformer2DModel(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
**build_dit_config(
|
| 230 |
depth=depth,
|
| 231 |
hidden_size=hidden_size,
|
|
|
|
| 241 |
self.in_channels = in_channels
|
| 242 |
|
| 243 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 244 |
+
return super().load_state_dict(_wrap_dit_state_dict(state_dict), strict=strict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
def forward(self, x, y, *args, **kwargs):
|
| 247 |
hidden_states = self.transformer.pos_embed(x)
|
|
|
|
| 285 |
return split_output_channels(output, self.in_channels)
|
| 286 |
|
| 287 |
|
| 288 |
+
class AFMGenerator2DModel(_AFMCheckpointMixin, Generator, ConfigMixin):
|
| 289 |
config_name = "config.json"
|
| 290 |
|
| 291 |
@register_to_config
|
|
|
|
| 326 |
)
|
| 327 |
|
| 328 |
|
| 329 |
+
class AFMGeneratorDeep2DModel(_AFMCheckpointMixin, GeneratorDeep, ConfigMixin):
|
| 330 |
config_name = "config.json"
|
| 331 |
|
| 332 |
@register_to_config
|
AFM-XL-2-2NFE-noguide/generator/config.json
CHANGED
|
@@ -1,6 +1,22 @@
|
|
| 1 |
{
|
| 2 |
-
"_class_name": "
|
| 3 |
"_diffusers_version": "0.36.0",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
"model_type": "AFM-XL/2",
|
| 5 |
"architecture": "standard",
|
| 6 |
"repeat": 1,
|
|
@@ -12,9 +28,7 @@
|
|
| 12 |
"class_dropout_prob": 0.0,
|
| 13 |
"input_size": 32,
|
| 14 |
"num_classes": 1000,
|
| 15 |
-
"in_channels": 4,
|
| 16 |
"depth": 28,
|
| 17 |
"hidden_size": 1152,
|
| 18 |
-
"patch_size": 2,
|
| 19 |
"num_heads": 16
|
| 20 |
}
|
|
|
|
| 1 |
{
|
| 2 |
+
"_class_name": "DiTTransformer2DModel",
|
| 3 |
"_diffusers_version": "0.36.0",
|
| 4 |
+
"sample_size": 32,
|
| 5 |
+
"num_layers": 28,
|
| 6 |
+
"num_attention_heads": 16,
|
| 7 |
+
"attention_head_dim": 72,
|
| 8 |
+
"in_channels": 4,
|
| 9 |
+
"out_channels": 4,
|
| 10 |
+
"patch_size": 2,
|
| 11 |
+
"attention_bias": true,
|
| 12 |
+
"activation_fn": "gelu-approximate",
|
| 13 |
+
"num_embeds_ada_norm": 1000,
|
| 14 |
+
"norm_type": "ada_norm_zero",
|
| 15 |
+
"norm_elementwise_affine": false,
|
| 16 |
+
"dropout": 0.0,
|
| 17 |
+
"norm_num_groups": 32,
|
| 18 |
+
"norm_eps": 1e-05,
|
| 19 |
+
"upcast_attention": false,
|
| 20 |
"model_type": "AFM-XL/2",
|
| 21 |
"architecture": "standard",
|
| 22 |
"repeat": 1,
|
|
|
|
| 28 |
"class_dropout_prob": 0.0,
|
| 29 |
"input_size": 32,
|
| 30 |
"num_classes": 1000,
|
|
|
|
| 31 |
"depth": 28,
|
| 32 |
"hidden_size": 1152,
|
|
|
|
| 33 |
"num_heads": 16
|
| 34 |
}
|
AFM-XL-2-2NFE-noguide/generator/diffusion_pytorch_model.safetensors
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d553e9ca93352c17ecbeb52136e74c721a4d6832368507e483a1bea30c5d75ff
|
| 3 |
+
size 2999296584
|
AFM-XL-2-2NFE-noguide/generator/modeling_afm.py
CHANGED
|
@@ -2,7 +2,6 @@
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
-
import copy
|
| 6 |
import math
|
| 7 |
from typing import Any, Dict, Optional
|
| 8 |
|
|
@@ -10,15 +9,10 @@ import torch
|
|
| 10 |
import torch.nn.functional as F
|
| 11 |
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
| 12 |
from diffusers.models.modeling_utils import ModelMixin
|
|
|
|
| 13 |
from torch import nn
|
| 14 |
|
| 15 |
|
| 16 |
-
def get_dit_transformer_class():
|
| 17 |
-
from diffusers.models.transformers.dit_transformer_2d import DiTTransformer2DModel
|
| 18 |
-
|
| 19 |
-
return DiTTransformer2DModel
|
| 20 |
-
|
| 21 |
-
|
| 22 |
def build_dit_config(
|
| 23 |
*,
|
| 24 |
depth: int = 28,
|
|
@@ -54,110 +48,15 @@ def build_dit_config(
|
|
| 54 |
}
|
| 55 |
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
if "t_embedder.mlp.0.weight" in converted:
|
| 64 |
-
timestep_weights = {
|
| 65 |
-
"linear_1.weight": converted.pop("t_embedder.mlp.0.weight"),
|
| 66 |
-
"linear_1.bias": converted.pop("t_embedder.mlp.0.bias"),
|
| 67 |
-
"linear_2.weight": converted.pop("t_embedder.mlp.2.weight"),
|
| 68 |
-
"linear_2.bias": converted.pop("t_embedder.mlp.2.bias"),
|
| 69 |
-
}
|
| 70 |
-
else:
|
| 71 |
-
timestep_weights = None
|
| 72 |
-
class_embedding = converted.pop("y_embedder.embedding_table.weight")
|
| 73 |
-
if class_embedding.shape[0] == 1000:
|
| 74 |
-
null_class = torch.zeros(1, class_embedding.shape[1], dtype=class_embedding.dtype)
|
| 75 |
-
class_embedding = torch.cat([class_embedding, null_class], dim=0)
|
| 76 |
-
|
| 77 |
-
for block_idx in range(depth):
|
| 78 |
-
if timestep_weights is not None:
|
| 79 |
-
for key, tensor in timestep_weights.items():
|
| 80 |
-
converted[f"transformer_blocks.{block_idx}.norm1.emb.timestep_embedder.{key}"] = tensor.clone()
|
| 81 |
-
converted[f"transformer_blocks.{block_idx}.norm1.emb.class_embedder.embedding_table.weight"] = (
|
| 82 |
-
class_embedding.clone()
|
| 83 |
-
)
|
| 84 |
-
converted[f"transformer_blocks.{block_idx}.norm1.linear.weight"] = converted[
|
| 85 |
-
f"blocks.{block_idx}.adaLN_modulation.1.weight"
|
| 86 |
-
]
|
| 87 |
-
converted[f"transformer_blocks.{block_idx}.norm1.linear.bias"] = converted[
|
| 88 |
-
f"blocks.{block_idx}.adaLN_modulation.1.bias"
|
| 89 |
-
]
|
| 90 |
-
|
| 91 |
-
q, k, v = torch.chunk(converted[f"blocks.{block_idx}.attn.qkv.weight"], 3, dim=0)
|
| 92 |
-
q_bias, k_bias, v_bias = torch.chunk(converted[f"blocks.{block_idx}.attn.qkv.bias"], 3, dim=0)
|
| 93 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_q.weight"] = q
|
| 94 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_q.bias"] = q_bias
|
| 95 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_k.weight"] = k
|
| 96 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_k.bias"] = k_bias
|
| 97 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_v.weight"] = v
|
| 98 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_v.bias"] = v_bias
|
| 99 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_out.0.weight"] = converted[
|
| 100 |
-
f"blocks.{block_idx}.attn.proj.weight"
|
| 101 |
-
]
|
| 102 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_out.0.bias"] = converted[
|
| 103 |
-
f"blocks.{block_idx}.attn.proj.bias"
|
| 104 |
-
]
|
| 105 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.0.proj.weight"] = converted[
|
| 106 |
-
f"blocks.{block_idx}.mlp.fc1.weight"
|
| 107 |
-
]
|
| 108 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.0.proj.bias"] = converted[
|
| 109 |
-
f"blocks.{block_idx}.mlp.fc1.bias"
|
| 110 |
-
]
|
| 111 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.2.weight"] = converted[
|
| 112 |
-
f"blocks.{block_idx}.mlp.fc2.weight"
|
| 113 |
-
]
|
| 114 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.2.bias"] = converted[
|
| 115 |
-
f"blocks.{block_idx}.mlp.fc2.bias"
|
| 116 |
-
]
|
| 117 |
-
|
| 118 |
-
for suffix in (
|
| 119 |
-
"attn.qkv.weight",
|
| 120 |
-
"attn.qkv.bias",
|
| 121 |
-
"attn.proj.weight",
|
| 122 |
-
"attn.proj.bias",
|
| 123 |
-
"mlp.fc1.weight",
|
| 124 |
-
"mlp.fc1.bias",
|
| 125 |
-
"mlp.fc2.weight",
|
| 126 |
-
"mlp.fc2.bias",
|
| 127 |
-
"adaLN_modulation.1.weight",
|
| 128 |
-
"adaLN_modulation.1.bias",
|
| 129 |
-
):
|
| 130 |
-
converted.pop(f"blocks.{block_idx}.{suffix}", None)
|
| 131 |
-
|
| 132 |
-
converted["proj_out_1.weight"] = converted.pop("final_layer.adaLN_modulation.1.weight")
|
| 133 |
-
converted["proj_out_1.bias"] = converted.pop("final_layer.adaLN_modulation.1.bias")
|
| 134 |
-
converted["proj_out_2.weight"] = converted.pop("final_layer.linear.weight")
|
| 135 |
-
converted["proj_out_2.bias"] = converted.pop("final_layer.linear.bias")
|
| 136 |
-
|
| 137 |
-
converted.pop("pos_embed", None)
|
| 138 |
-
for block_idx in range(depth):
|
| 139 |
-
for suffix in ("norm1.weight", "norm1.bias", "norm2.weight", "norm2.bias"):
|
| 140 |
-
converted.pop(f"blocks.{block_idx}.{suffix}", None)
|
| 141 |
-
|
| 142 |
-
return {key: tensor.detach().clone().contiguous() for key, tensor in converted.items()}
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
def load_legacy_dit_state_dict(
|
| 146 |
-
state_dict: Dict[str, torch.Tensor],
|
| 147 |
-
depth: int,
|
| 148 |
-
patch_size: int,
|
| 149 |
-
hidden_size: int,
|
| 150 |
-
num_heads: int,
|
| 151 |
-
) -> Dict[str, torch.Tensor]:
|
| 152 |
-
if "transformer_blocks.0.attn1.to_q.weight" in state_dict:
|
| 153 |
-
return state_dict
|
| 154 |
-
if any(key.startswith("blocks.") for key in state_dict):
|
| 155 |
-
return _convert_legacy_dit_state_dict(state_dict, depth)
|
| 156 |
-
return state_dict
|
| 157 |
|
| 158 |
|
| 159 |
def forward_dit_output(
|
| 160 |
-
transformer,
|
| 161 |
hidden_states: torch.Tensor,
|
| 162 |
timestep: Optional[torch.Tensor],
|
| 163 |
class_labels: torch.Tensor,
|
|
@@ -207,23 +106,6 @@ def forward_dit_output(
|
|
| 207 |
)
|
| 208 |
|
| 209 |
|
| 210 |
-
def remap_transformer_state_dict(
|
| 211 |
-
state_dict: Dict[str, torch.Tensor],
|
| 212 |
-
legacy_kwargs: Dict[str, int],
|
| 213 |
-
extra_prefixes: tuple[str, ...] = (),
|
| 214 |
-
) -> Dict[str, torch.Tensor]:
|
| 215 |
-
state_dict = load_legacy_dit_state_dict(state_dict, **legacy_kwargs)
|
| 216 |
-
remapped: Dict[str, torch.Tensor] = {}
|
| 217 |
-
for key, value in state_dict.items():
|
| 218 |
-
if any(key.startswith(prefix) for prefix in extra_prefixes):
|
| 219 |
-
remapped[key] = value
|
| 220 |
-
elif key.startswith("transformer."):
|
| 221 |
-
remapped[key] = value
|
| 222 |
-
else:
|
| 223 |
-
remapped[f"transformer.{key}"] = value
|
| 224 |
-
return remapped
|
| 225 |
-
|
| 226 |
-
|
| 227 |
def split_output_channels(output: torch.Tensor, in_channels: int) -> torch.Tensor:
|
| 228 |
if output.shape[1] == in_channels:
|
| 229 |
return output
|
|
@@ -259,6 +141,14 @@ class TimestepEmbedder(nn.Module):
|
|
| 259 |
return self.mlp(emb.to(dtype=weight_dtype))
|
| 260 |
|
| 261 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
class Generator(nn.Module):
|
| 263 |
def __init__(
|
| 264 |
self,
|
|
@@ -279,14 +169,7 @@ class Generator(nn.Module):
|
|
| 279 |
super().__init__()
|
| 280 |
self.use_t_src = use_t_src
|
| 281 |
self.use_t_tgt = use_t_tgt
|
| 282 |
-
self.
|
| 283 |
-
"depth": depth,
|
| 284 |
-
"hidden_size": hidden_size,
|
| 285 |
-
"patch_size": patch_size,
|
| 286 |
-
"num_heads": num_heads,
|
| 287 |
-
}
|
| 288 |
-
dit_cls = get_dit_transformer_class()
|
| 289 |
-
self.transformer = dit_cls(
|
| 290 |
**build_dit_config(
|
| 291 |
depth=depth,
|
| 292 |
hidden_size=hidden_size,
|
|
@@ -306,20 +189,9 @@ class Generator(nn.Module):
|
|
| 306 |
nn.init.normal_(self.t_tgt_embedder.mlp[2].weight, std=0.02)
|
| 307 |
|
| 308 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 309 |
-
|
| 310 |
-
has_extra = any(key.startswith(extra) for key in state_dict)
|
| 311 |
-
has_t_embedder = any("t_embedder" in key for key in state_dict)
|
| 312 |
-
remapped = remap_transformer_state_dict(state_dict, self._legacy_kwargs, extra_prefixes=extra)
|
| 313 |
-
if self.use_t_tgt and not has_extra:
|
| 314 |
-
strict = False
|
| 315 |
-
if not has_t_embedder:
|
| 316 |
strict = False
|
| 317 |
-
|
| 318 |
-
if not has_t_embedder:
|
| 319 |
-
for name, param in self.named_parameters():
|
| 320 |
-
if "timestep_embedder" in name:
|
| 321 |
-
param.data.zero_()
|
| 322 |
-
return out
|
| 323 |
|
| 324 |
def forward(self, x, y, t_src=None, t_tgt=None):
|
| 325 |
if self.use_t_src and t_src is not None:
|
|
@@ -353,14 +225,7 @@ class GeneratorDeep(nn.Module):
|
|
| 353 |
):
|
| 354 |
super().__init__()
|
| 355 |
self.repeat = repeat
|
| 356 |
-
self.
|
| 357 |
-
"depth": depth,
|
| 358 |
-
"hidden_size": hidden_size,
|
| 359 |
-
"patch_size": patch_size,
|
| 360 |
-
"num_heads": num_heads,
|
| 361 |
-
}
|
| 362 |
-
dit_cls = get_dit_transformer_class()
|
| 363 |
-
self.transformer = dit_cls(
|
| 364 |
**build_dit_config(
|
| 365 |
depth=depth,
|
| 366 |
hidden_size=hidden_size,
|
|
@@ -376,16 +241,7 @@ class GeneratorDeep(nn.Module):
|
|
| 376 |
self.in_channels = in_channels
|
| 377 |
|
| 378 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 379 |
-
|
| 380 |
-
has_t_embedder = any("t_embedder" in key for key in state_dict)
|
| 381 |
-
if not has_t_embedder:
|
| 382 |
-
strict = False
|
| 383 |
-
out = super().load_state_dict(remapped, strict=strict)
|
| 384 |
-
if not has_t_embedder:
|
| 385 |
-
for name, param in self.named_parameters():
|
| 386 |
-
if "timestep_embedder" in name:
|
| 387 |
-
param.data.zero_()
|
| 388 |
-
return out
|
| 389 |
|
| 390 |
def forward(self, x, y, *args, **kwargs):
|
| 391 |
hidden_states = self.transformer.pos_embed(x)
|
|
@@ -429,7 +285,7 @@ class GeneratorDeep(nn.Module):
|
|
| 429 |
return split_output_channels(output, self.in_channels)
|
| 430 |
|
| 431 |
|
| 432 |
-
class AFMGenerator2DModel(
|
| 433 |
config_name = "config.json"
|
| 434 |
|
| 435 |
@register_to_config
|
|
@@ -470,7 +326,7 @@ class AFMGenerator2DModel(Generator, ModelMixin, ConfigMixin):
|
|
| 470 |
)
|
| 471 |
|
| 472 |
|
| 473 |
-
class AFMGeneratorDeep2DModel(
|
| 474 |
config_name = "config.json"
|
| 475 |
|
| 476 |
@register_to_config
|
|
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
|
|
|
| 5 |
import math
|
| 6 |
from typing import Any, Dict, Optional
|
| 7 |
|
|
|
|
| 9 |
import torch.nn.functional as F
|
| 10 |
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
| 11 |
from diffusers.models.modeling_utils import ModelMixin
|
| 12 |
+
from diffusers.models.transformers.dit_transformer_2d import DiTTransformer2DModel
|
| 13 |
from torch import nn
|
| 14 |
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def build_dit_config(
|
| 17 |
*,
|
| 18 |
depth: int = 28,
|
|
|
|
| 48 |
}
|
| 49 |
|
| 50 |
|
| 51 |
+
_PREFIX_KEYS = ("transformer.", "t_tgt_embedder.")
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def _wrap_dit_state_dict(state_dict: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]:
|
| 55 |
+
return {k if k.startswith(_PREFIX_KEYS) else f"transformer.{k}": v for k, v in state_dict.items()}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
def forward_dit_output(
|
| 59 |
+
transformer: DiTTransformer2DModel,
|
| 60 |
hidden_states: torch.Tensor,
|
| 61 |
timestep: Optional[torch.Tensor],
|
| 62 |
class_labels: torch.Tensor,
|
|
|
|
| 106 |
)
|
| 107 |
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
def split_output_channels(output: torch.Tensor, in_channels: int) -> torch.Tensor:
|
| 110 |
if output.shape[1] == in_channels:
|
| 111 |
return output
|
|
|
|
| 141 |
return self.mlp(emb.to(dtype=weight_dtype))
|
| 142 |
|
| 143 |
|
| 144 |
+
class _AFMCheckpointMixin(ModelMixin):
|
| 145 |
+
def _fix_state_dict_keys_on_load(self, state_dict):
|
| 146 |
+
wrapped = _wrap_dit_state_dict(state_dict)
|
| 147 |
+
state_dict.clear()
|
| 148 |
+
state_dict.update(wrapped)
|
| 149 |
+
super()._fix_state_dict_keys_on_load(state_dict)
|
| 150 |
+
|
| 151 |
+
|
| 152 |
class Generator(nn.Module):
|
| 153 |
def __init__(
|
| 154 |
self,
|
|
|
|
| 169 |
super().__init__()
|
| 170 |
self.use_t_src = use_t_src
|
| 171 |
self.use_t_tgt = use_t_tgt
|
| 172 |
+
self.transformer = DiTTransformer2DModel(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
**build_dit_config(
|
| 174 |
depth=depth,
|
| 175 |
hidden_size=hidden_size,
|
|
|
|
| 189 |
nn.init.normal_(self.t_tgt_embedder.mlp[2].weight, std=0.02)
|
| 190 |
|
| 191 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 192 |
+
if self.use_t_tgt and not any(key.startswith("t_tgt_embedder.") for key in state_dict):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
strict = False
|
| 194 |
+
return super().load_state_dict(_wrap_dit_state_dict(state_dict), strict=strict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
def forward(self, x, y, t_src=None, t_tgt=None):
|
| 197 |
if self.use_t_src and t_src is not None:
|
|
|
|
| 225 |
):
|
| 226 |
super().__init__()
|
| 227 |
self.repeat = repeat
|
| 228 |
+
self.transformer = DiTTransformer2DModel(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
**build_dit_config(
|
| 230 |
depth=depth,
|
| 231 |
hidden_size=hidden_size,
|
|
|
|
| 241 |
self.in_channels = in_channels
|
| 242 |
|
| 243 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 244 |
+
return super().load_state_dict(_wrap_dit_state_dict(state_dict), strict=strict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
def forward(self, x, y, *args, **kwargs):
|
| 247 |
hidden_states = self.transformer.pos_embed(x)
|
|
|
|
| 285 |
return split_output_channels(output, self.in_channels)
|
| 286 |
|
| 287 |
|
| 288 |
+
class AFMGenerator2DModel(_AFMCheckpointMixin, Generator, ConfigMixin):
|
| 289 |
config_name = "config.json"
|
| 290 |
|
| 291 |
@register_to_config
|
|
|
|
| 326 |
)
|
| 327 |
|
| 328 |
|
| 329 |
+
class AFMGeneratorDeep2DModel(_AFMCheckpointMixin, GeneratorDeep, ConfigMixin):
|
| 330 |
config_name = "config.json"
|
| 331 |
|
| 332 |
@register_to_config
|
AFM-XL-2-4NFE-guided/generator/config.json
CHANGED
|
@@ -1,6 +1,22 @@
|
|
| 1 |
{
|
| 2 |
-
"_class_name": "
|
| 3 |
"_diffusers_version": "0.36.0",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
"model_type": "AFM-XL/2",
|
| 5 |
"architecture": "standard",
|
| 6 |
"repeat": 1,
|
|
@@ -12,9 +28,7 @@
|
|
| 12 |
"class_dropout_prob": 0.0,
|
| 13 |
"input_size": 32,
|
| 14 |
"num_classes": 1000,
|
| 15 |
-
"in_channels": 4,
|
| 16 |
"depth": 28,
|
| 17 |
"hidden_size": 1152,
|
| 18 |
-
"patch_size": 2,
|
| 19 |
"num_heads": 16
|
| 20 |
}
|
|
|
|
| 1 |
{
|
| 2 |
+
"_class_name": "DiTTransformer2DModel",
|
| 3 |
"_diffusers_version": "0.36.0",
|
| 4 |
+
"sample_size": 32,
|
| 5 |
+
"num_layers": 28,
|
| 6 |
+
"num_attention_heads": 16,
|
| 7 |
+
"attention_head_dim": 72,
|
| 8 |
+
"in_channels": 4,
|
| 9 |
+
"out_channels": 4,
|
| 10 |
+
"patch_size": 2,
|
| 11 |
+
"attention_bias": true,
|
| 12 |
+
"activation_fn": "gelu-approximate",
|
| 13 |
+
"num_embeds_ada_norm": 1000,
|
| 14 |
+
"norm_type": "ada_norm_zero",
|
| 15 |
+
"norm_elementwise_affine": false,
|
| 16 |
+
"dropout": 0.0,
|
| 17 |
+
"norm_num_groups": 32,
|
| 18 |
+
"norm_eps": 1e-05,
|
| 19 |
+
"upcast_attention": false,
|
| 20 |
"model_type": "AFM-XL/2",
|
| 21 |
"architecture": "standard",
|
| 22 |
"repeat": 1,
|
|
|
|
| 28 |
"class_dropout_prob": 0.0,
|
| 29 |
"input_size": 32,
|
| 30 |
"num_classes": 1000,
|
|
|
|
| 31 |
"depth": 28,
|
| 32 |
"hidden_size": 1152,
|
|
|
|
| 33 |
"num_heads": 16
|
| 34 |
}
|
AFM-XL-2-4NFE-guided/generator/diffusion_pytorch_model.safetensors
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4ebc0085e11680404c6745acd138f1f15e13a73b8e0d6c06a3bf6b1972d60206
|
| 3 |
+
size 2999296584
|
AFM-XL-2-4NFE-guided/generator/modeling_afm.py
CHANGED
|
@@ -2,7 +2,6 @@
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
-
import copy
|
| 6 |
import math
|
| 7 |
from typing import Any, Dict, Optional
|
| 8 |
|
|
@@ -10,15 +9,10 @@ import torch
|
|
| 10 |
import torch.nn.functional as F
|
| 11 |
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
| 12 |
from diffusers.models.modeling_utils import ModelMixin
|
|
|
|
| 13 |
from torch import nn
|
| 14 |
|
| 15 |
|
| 16 |
-
def get_dit_transformer_class():
|
| 17 |
-
from diffusers.models.transformers.dit_transformer_2d import DiTTransformer2DModel
|
| 18 |
-
|
| 19 |
-
return DiTTransformer2DModel
|
| 20 |
-
|
| 21 |
-
|
| 22 |
def build_dit_config(
|
| 23 |
*,
|
| 24 |
depth: int = 28,
|
|
@@ -54,110 +48,15 @@ def build_dit_config(
|
|
| 54 |
}
|
| 55 |
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
if "t_embedder.mlp.0.weight" in converted:
|
| 64 |
-
timestep_weights = {
|
| 65 |
-
"linear_1.weight": converted.pop("t_embedder.mlp.0.weight"),
|
| 66 |
-
"linear_1.bias": converted.pop("t_embedder.mlp.0.bias"),
|
| 67 |
-
"linear_2.weight": converted.pop("t_embedder.mlp.2.weight"),
|
| 68 |
-
"linear_2.bias": converted.pop("t_embedder.mlp.2.bias"),
|
| 69 |
-
}
|
| 70 |
-
else:
|
| 71 |
-
timestep_weights = None
|
| 72 |
-
class_embedding = converted.pop("y_embedder.embedding_table.weight")
|
| 73 |
-
if class_embedding.shape[0] == 1000:
|
| 74 |
-
null_class = torch.zeros(1, class_embedding.shape[1], dtype=class_embedding.dtype)
|
| 75 |
-
class_embedding = torch.cat([class_embedding, null_class], dim=0)
|
| 76 |
-
|
| 77 |
-
for block_idx in range(depth):
|
| 78 |
-
if timestep_weights is not None:
|
| 79 |
-
for key, tensor in timestep_weights.items():
|
| 80 |
-
converted[f"transformer_blocks.{block_idx}.norm1.emb.timestep_embedder.{key}"] = tensor.clone()
|
| 81 |
-
converted[f"transformer_blocks.{block_idx}.norm1.emb.class_embedder.embedding_table.weight"] = (
|
| 82 |
-
class_embedding.clone()
|
| 83 |
-
)
|
| 84 |
-
converted[f"transformer_blocks.{block_idx}.norm1.linear.weight"] = converted[
|
| 85 |
-
f"blocks.{block_idx}.adaLN_modulation.1.weight"
|
| 86 |
-
]
|
| 87 |
-
converted[f"transformer_blocks.{block_idx}.norm1.linear.bias"] = converted[
|
| 88 |
-
f"blocks.{block_idx}.adaLN_modulation.1.bias"
|
| 89 |
-
]
|
| 90 |
-
|
| 91 |
-
q, k, v = torch.chunk(converted[f"blocks.{block_idx}.attn.qkv.weight"], 3, dim=0)
|
| 92 |
-
q_bias, k_bias, v_bias = torch.chunk(converted[f"blocks.{block_idx}.attn.qkv.bias"], 3, dim=0)
|
| 93 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_q.weight"] = q
|
| 94 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_q.bias"] = q_bias
|
| 95 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_k.weight"] = k
|
| 96 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_k.bias"] = k_bias
|
| 97 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_v.weight"] = v
|
| 98 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_v.bias"] = v_bias
|
| 99 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_out.0.weight"] = converted[
|
| 100 |
-
f"blocks.{block_idx}.attn.proj.weight"
|
| 101 |
-
]
|
| 102 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_out.0.bias"] = converted[
|
| 103 |
-
f"blocks.{block_idx}.attn.proj.bias"
|
| 104 |
-
]
|
| 105 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.0.proj.weight"] = converted[
|
| 106 |
-
f"blocks.{block_idx}.mlp.fc1.weight"
|
| 107 |
-
]
|
| 108 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.0.proj.bias"] = converted[
|
| 109 |
-
f"blocks.{block_idx}.mlp.fc1.bias"
|
| 110 |
-
]
|
| 111 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.2.weight"] = converted[
|
| 112 |
-
f"blocks.{block_idx}.mlp.fc2.weight"
|
| 113 |
-
]
|
| 114 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.2.bias"] = converted[
|
| 115 |
-
f"blocks.{block_idx}.mlp.fc2.bias"
|
| 116 |
-
]
|
| 117 |
-
|
| 118 |
-
for suffix in (
|
| 119 |
-
"attn.qkv.weight",
|
| 120 |
-
"attn.qkv.bias",
|
| 121 |
-
"attn.proj.weight",
|
| 122 |
-
"attn.proj.bias",
|
| 123 |
-
"mlp.fc1.weight",
|
| 124 |
-
"mlp.fc1.bias",
|
| 125 |
-
"mlp.fc2.weight",
|
| 126 |
-
"mlp.fc2.bias",
|
| 127 |
-
"adaLN_modulation.1.weight",
|
| 128 |
-
"adaLN_modulation.1.bias",
|
| 129 |
-
):
|
| 130 |
-
converted.pop(f"blocks.{block_idx}.{suffix}", None)
|
| 131 |
-
|
| 132 |
-
converted["proj_out_1.weight"] = converted.pop("final_layer.adaLN_modulation.1.weight")
|
| 133 |
-
converted["proj_out_1.bias"] = converted.pop("final_layer.adaLN_modulation.1.bias")
|
| 134 |
-
converted["proj_out_2.weight"] = converted.pop("final_layer.linear.weight")
|
| 135 |
-
converted["proj_out_2.bias"] = converted.pop("final_layer.linear.bias")
|
| 136 |
-
|
| 137 |
-
converted.pop("pos_embed", None)
|
| 138 |
-
for block_idx in range(depth):
|
| 139 |
-
for suffix in ("norm1.weight", "norm1.bias", "norm2.weight", "norm2.bias"):
|
| 140 |
-
converted.pop(f"blocks.{block_idx}.{suffix}", None)
|
| 141 |
-
|
| 142 |
-
return {key: tensor.detach().clone().contiguous() for key, tensor in converted.items()}
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
def load_legacy_dit_state_dict(
|
| 146 |
-
state_dict: Dict[str, torch.Tensor],
|
| 147 |
-
depth: int,
|
| 148 |
-
patch_size: int,
|
| 149 |
-
hidden_size: int,
|
| 150 |
-
num_heads: int,
|
| 151 |
-
) -> Dict[str, torch.Tensor]:
|
| 152 |
-
if "transformer_blocks.0.attn1.to_q.weight" in state_dict:
|
| 153 |
-
return state_dict
|
| 154 |
-
if any(key.startswith("blocks.") for key in state_dict):
|
| 155 |
-
return _convert_legacy_dit_state_dict(state_dict, depth)
|
| 156 |
-
return state_dict
|
| 157 |
|
| 158 |
|
| 159 |
def forward_dit_output(
|
| 160 |
-
transformer,
|
| 161 |
hidden_states: torch.Tensor,
|
| 162 |
timestep: Optional[torch.Tensor],
|
| 163 |
class_labels: torch.Tensor,
|
|
@@ -207,23 +106,6 @@ def forward_dit_output(
|
|
| 207 |
)
|
| 208 |
|
| 209 |
|
| 210 |
-
def remap_transformer_state_dict(
|
| 211 |
-
state_dict: Dict[str, torch.Tensor],
|
| 212 |
-
legacy_kwargs: Dict[str, int],
|
| 213 |
-
extra_prefixes: tuple[str, ...] = (),
|
| 214 |
-
) -> Dict[str, torch.Tensor]:
|
| 215 |
-
state_dict = load_legacy_dit_state_dict(state_dict, **legacy_kwargs)
|
| 216 |
-
remapped: Dict[str, torch.Tensor] = {}
|
| 217 |
-
for key, value in state_dict.items():
|
| 218 |
-
if any(key.startswith(prefix) for prefix in extra_prefixes):
|
| 219 |
-
remapped[key] = value
|
| 220 |
-
elif key.startswith("transformer."):
|
| 221 |
-
remapped[key] = value
|
| 222 |
-
else:
|
| 223 |
-
remapped[f"transformer.{key}"] = value
|
| 224 |
-
return remapped
|
| 225 |
-
|
| 226 |
-
|
| 227 |
def split_output_channels(output: torch.Tensor, in_channels: int) -> torch.Tensor:
|
| 228 |
if output.shape[1] == in_channels:
|
| 229 |
return output
|
|
@@ -259,6 +141,14 @@ class TimestepEmbedder(nn.Module):
|
|
| 259 |
return self.mlp(emb.to(dtype=weight_dtype))
|
| 260 |
|
| 261 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
class Generator(nn.Module):
|
| 263 |
def __init__(
|
| 264 |
self,
|
|
@@ -279,14 +169,7 @@ class Generator(nn.Module):
|
|
| 279 |
super().__init__()
|
| 280 |
self.use_t_src = use_t_src
|
| 281 |
self.use_t_tgt = use_t_tgt
|
| 282 |
-
self.
|
| 283 |
-
"depth": depth,
|
| 284 |
-
"hidden_size": hidden_size,
|
| 285 |
-
"patch_size": patch_size,
|
| 286 |
-
"num_heads": num_heads,
|
| 287 |
-
}
|
| 288 |
-
dit_cls = get_dit_transformer_class()
|
| 289 |
-
self.transformer = dit_cls(
|
| 290 |
**build_dit_config(
|
| 291 |
depth=depth,
|
| 292 |
hidden_size=hidden_size,
|
|
@@ -306,20 +189,9 @@ class Generator(nn.Module):
|
|
| 306 |
nn.init.normal_(self.t_tgt_embedder.mlp[2].weight, std=0.02)
|
| 307 |
|
| 308 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 309 |
-
|
| 310 |
-
has_extra = any(key.startswith(extra) for key in state_dict)
|
| 311 |
-
has_t_embedder = any("t_embedder" in key for key in state_dict)
|
| 312 |
-
remapped = remap_transformer_state_dict(state_dict, self._legacy_kwargs, extra_prefixes=extra)
|
| 313 |
-
if self.use_t_tgt and not has_extra:
|
| 314 |
-
strict = False
|
| 315 |
-
if not has_t_embedder:
|
| 316 |
strict = False
|
| 317 |
-
|
| 318 |
-
if not has_t_embedder:
|
| 319 |
-
for name, param in self.named_parameters():
|
| 320 |
-
if "timestep_embedder" in name:
|
| 321 |
-
param.data.zero_()
|
| 322 |
-
return out
|
| 323 |
|
| 324 |
def forward(self, x, y, t_src=None, t_tgt=None):
|
| 325 |
if self.use_t_src and t_src is not None:
|
|
@@ -353,14 +225,7 @@ class GeneratorDeep(nn.Module):
|
|
| 353 |
):
|
| 354 |
super().__init__()
|
| 355 |
self.repeat = repeat
|
| 356 |
-
self.
|
| 357 |
-
"depth": depth,
|
| 358 |
-
"hidden_size": hidden_size,
|
| 359 |
-
"patch_size": patch_size,
|
| 360 |
-
"num_heads": num_heads,
|
| 361 |
-
}
|
| 362 |
-
dit_cls = get_dit_transformer_class()
|
| 363 |
-
self.transformer = dit_cls(
|
| 364 |
**build_dit_config(
|
| 365 |
depth=depth,
|
| 366 |
hidden_size=hidden_size,
|
|
@@ -376,16 +241,7 @@ class GeneratorDeep(nn.Module):
|
|
| 376 |
self.in_channels = in_channels
|
| 377 |
|
| 378 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 379 |
-
|
| 380 |
-
has_t_embedder = any("t_embedder" in key for key in state_dict)
|
| 381 |
-
if not has_t_embedder:
|
| 382 |
-
strict = False
|
| 383 |
-
out = super().load_state_dict(remapped, strict=strict)
|
| 384 |
-
if not has_t_embedder:
|
| 385 |
-
for name, param in self.named_parameters():
|
| 386 |
-
if "timestep_embedder" in name:
|
| 387 |
-
param.data.zero_()
|
| 388 |
-
return out
|
| 389 |
|
| 390 |
def forward(self, x, y, *args, **kwargs):
|
| 391 |
hidden_states = self.transformer.pos_embed(x)
|
|
@@ -429,7 +285,7 @@ class GeneratorDeep(nn.Module):
|
|
| 429 |
return split_output_channels(output, self.in_channels)
|
| 430 |
|
| 431 |
|
| 432 |
-
class AFMGenerator2DModel(
|
| 433 |
config_name = "config.json"
|
| 434 |
|
| 435 |
@register_to_config
|
|
@@ -470,7 +326,7 @@ class AFMGenerator2DModel(Generator, ModelMixin, ConfigMixin):
|
|
| 470 |
)
|
| 471 |
|
| 472 |
|
| 473 |
-
class AFMGeneratorDeep2DModel(
|
| 474 |
config_name = "config.json"
|
| 475 |
|
| 476 |
@register_to_config
|
|
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
|
|
|
| 5 |
import math
|
| 6 |
from typing import Any, Dict, Optional
|
| 7 |
|
|
|
|
| 9 |
import torch.nn.functional as F
|
| 10 |
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
| 11 |
from diffusers.models.modeling_utils import ModelMixin
|
| 12 |
+
from diffusers.models.transformers.dit_transformer_2d import DiTTransformer2DModel
|
| 13 |
from torch import nn
|
| 14 |
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def build_dit_config(
|
| 17 |
*,
|
| 18 |
depth: int = 28,
|
|
|
|
| 48 |
}
|
| 49 |
|
| 50 |
|
| 51 |
+
_PREFIX_KEYS = ("transformer.", "t_tgt_embedder.")
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def _wrap_dit_state_dict(state_dict: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]:
|
| 55 |
+
return {k if k.startswith(_PREFIX_KEYS) else f"transformer.{k}": v for k, v in state_dict.items()}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
def forward_dit_output(
|
| 59 |
+
transformer: DiTTransformer2DModel,
|
| 60 |
hidden_states: torch.Tensor,
|
| 61 |
timestep: Optional[torch.Tensor],
|
| 62 |
class_labels: torch.Tensor,
|
|
|
|
| 106 |
)
|
| 107 |
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
def split_output_channels(output: torch.Tensor, in_channels: int) -> torch.Tensor:
|
| 110 |
if output.shape[1] == in_channels:
|
| 111 |
return output
|
|
|
|
| 141 |
return self.mlp(emb.to(dtype=weight_dtype))
|
| 142 |
|
| 143 |
|
| 144 |
+
class _AFMCheckpointMixin(ModelMixin):
|
| 145 |
+
def _fix_state_dict_keys_on_load(self, state_dict):
|
| 146 |
+
wrapped = _wrap_dit_state_dict(state_dict)
|
| 147 |
+
state_dict.clear()
|
| 148 |
+
state_dict.update(wrapped)
|
| 149 |
+
super()._fix_state_dict_keys_on_load(state_dict)
|
| 150 |
+
|
| 151 |
+
|
| 152 |
class Generator(nn.Module):
|
| 153 |
def __init__(
|
| 154 |
self,
|
|
|
|
| 169 |
super().__init__()
|
| 170 |
self.use_t_src = use_t_src
|
| 171 |
self.use_t_tgt = use_t_tgt
|
| 172 |
+
self.transformer = DiTTransformer2DModel(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
**build_dit_config(
|
| 174 |
depth=depth,
|
| 175 |
hidden_size=hidden_size,
|
|
|
|
| 189 |
nn.init.normal_(self.t_tgt_embedder.mlp[2].weight, std=0.02)
|
| 190 |
|
| 191 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 192 |
+
if self.use_t_tgt and not any(key.startswith("t_tgt_embedder.") for key in state_dict):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
strict = False
|
| 194 |
+
return super().load_state_dict(_wrap_dit_state_dict(state_dict), strict=strict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
def forward(self, x, y, t_src=None, t_tgt=None):
|
| 197 |
if self.use_t_src and t_src is not None:
|
|
|
|
| 225 |
):
|
| 226 |
super().__init__()
|
| 227 |
self.repeat = repeat
|
| 228 |
+
self.transformer = DiTTransformer2DModel(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
**build_dit_config(
|
| 230 |
depth=depth,
|
| 231 |
hidden_size=hidden_size,
|
|
|
|
| 241 |
self.in_channels = in_channels
|
| 242 |
|
| 243 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 244 |
+
return super().load_state_dict(_wrap_dit_state_dict(state_dict), strict=strict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
def forward(self, x, y, *args, **kwargs):
|
| 247 |
hidden_states = self.transformer.pos_embed(x)
|
|
|
|
| 285 |
return split_output_channels(output, self.in_channels)
|
| 286 |
|
| 287 |
|
| 288 |
+
class AFMGenerator2DModel(_AFMCheckpointMixin, Generator, ConfigMixin):
|
| 289 |
config_name = "config.json"
|
| 290 |
|
| 291 |
@register_to_config
|
|
|
|
| 326 |
)
|
| 327 |
|
| 328 |
|
| 329 |
+
class AFMGeneratorDeep2DModel(_AFMCheckpointMixin, GeneratorDeep, ConfigMixin):
|
| 330 |
config_name = "config.json"
|
| 331 |
|
| 332 |
@register_to_config
|
AFM-XL-2-56layer-1NFE-guided/generator/config.json
CHANGED
|
@@ -1,6 +1,22 @@
|
|
| 1 |
{
|
| 2 |
-
"_class_name": "
|
| 3 |
"_diffusers_version": "0.36.0",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
"model_type": "AFM-XL/2",
|
| 5 |
"architecture": "deep",
|
| 6 |
"repeat": 2,
|
|
@@ -12,9 +28,7 @@
|
|
| 12 |
"class_dropout_prob": 0.0,
|
| 13 |
"input_size": 32,
|
| 14 |
"num_classes": 1000,
|
| 15 |
-
"in_channels": 4,
|
| 16 |
"depth": 28,
|
| 17 |
"hidden_size": 1152,
|
| 18 |
-
"patch_size": 2,
|
| 19 |
"num_heads": 16
|
| 20 |
}
|
|
|
|
| 1 |
{
|
| 2 |
+
"_class_name": "DiTTransformer2DModel",
|
| 3 |
"_diffusers_version": "0.36.0",
|
| 4 |
+
"sample_size": 32,
|
| 5 |
+
"num_layers": 28,
|
| 6 |
+
"num_attention_heads": 16,
|
| 7 |
+
"attention_head_dim": 72,
|
| 8 |
+
"in_channels": 4,
|
| 9 |
+
"out_channels": 4,
|
| 10 |
+
"patch_size": 2,
|
| 11 |
+
"attention_bias": true,
|
| 12 |
+
"activation_fn": "gelu-approximate",
|
| 13 |
+
"num_embeds_ada_norm": 1000,
|
| 14 |
+
"norm_type": "ada_norm_zero",
|
| 15 |
+
"norm_elementwise_affine": false,
|
| 16 |
+
"dropout": 0.0,
|
| 17 |
+
"norm_num_groups": 32,
|
| 18 |
+
"norm_eps": 1e-05,
|
| 19 |
+
"upcast_attention": false,
|
| 20 |
"model_type": "AFM-XL/2",
|
| 21 |
"architecture": "deep",
|
| 22 |
"repeat": 2,
|
|
|
|
| 28 |
"class_dropout_prob": 0.0,
|
| 29 |
"input_size": 32,
|
| 30 |
"num_classes": 1000,
|
|
|
|
| 31 |
"depth": 28,
|
| 32 |
"hidden_size": 1152,
|
|
|
|
| 33 |
"num_heads": 16
|
| 34 |
}
|
AFM-XL-2-56layer-1NFE-guided/generator/diffusion_pytorch_model.safetensors
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:39b4886846d53e4e664aa1eb70af1da6ad4a87c3f0f09d37f8e81b596ea319ce
|
| 3 |
+
size 2999296584
|
AFM-XL-2-56layer-1NFE-guided/generator/modeling_afm.py
CHANGED
|
@@ -2,7 +2,6 @@
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
-
import copy
|
| 6 |
import math
|
| 7 |
from typing import Any, Dict, Optional
|
| 8 |
|
|
@@ -10,15 +9,10 @@ import torch
|
|
| 10 |
import torch.nn.functional as F
|
| 11 |
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
| 12 |
from diffusers.models.modeling_utils import ModelMixin
|
|
|
|
| 13 |
from torch import nn
|
| 14 |
|
| 15 |
|
| 16 |
-
def get_dit_transformer_class():
|
| 17 |
-
from diffusers.models.transformers.dit_transformer_2d import DiTTransformer2DModel
|
| 18 |
-
|
| 19 |
-
return DiTTransformer2DModel
|
| 20 |
-
|
| 21 |
-
|
| 22 |
def build_dit_config(
|
| 23 |
*,
|
| 24 |
depth: int = 28,
|
|
@@ -54,110 +48,15 @@ def build_dit_config(
|
|
| 54 |
}
|
| 55 |
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
if "t_embedder.mlp.0.weight" in converted:
|
| 64 |
-
timestep_weights = {
|
| 65 |
-
"linear_1.weight": converted.pop("t_embedder.mlp.0.weight"),
|
| 66 |
-
"linear_1.bias": converted.pop("t_embedder.mlp.0.bias"),
|
| 67 |
-
"linear_2.weight": converted.pop("t_embedder.mlp.2.weight"),
|
| 68 |
-
"linear_2.bias": converted.pop("t_embedder.mlp.2.bias"),
|
| 69 |
-
}
|
| 70 |
-
else:
|
| 71 |
-
timestep_weights = None
|
| 72 |
-
class_embedding = converted.pop("y_embedder.embedding_table.weight")
|
| 73 |
-
if class_embedding.shape[0] == 1000:
|
| 74 |
-
null_class = torch.zeros(1, class_embedding.shape[1], dtype=class_embedding.dtype)
|
| 75 |
-
class_embedding = torch.cat([class_embedding, null_class], dim=0)
|
| 76 |
-
|
| 77 |
-
for block_idx in range(depth):
|
| 78 |
-
if timestep_weights is not None:
|
| 79 |
-
for key, tensor in timestep_weights.items():
|
| 80 |
-
converted[f"transformer_blocks.{block_idx}.norm1.emb.timestep_embedder.{key}"] = tensor.clone()
|
| 81 |
-
converted[f"transformer_blocks.{block_idx}.norm1.emb.class_embedder.embedding_table.weight"] = (
|
| 82 |
-
class_embedding.clone()
|
| 83 |
-
)
|
| 84 |
-
converted[f"transformer_blocks.{block_idx}.norm1.linear.weight"] = converted[
|
| 85 |
-
f"blocks.{block_idx}.adaLN_modulation.1.weight"
|
| 86 |
-
]
|
| 87 |
-
converted[f"transformer_blocks.{block_idx}.norm1.linear.bias"] = converted[
|
| 88 |
-
f"blocks.{block_idx}.adaLN_modulation.1.bias"
|
| 89 |
-
]
|
| 90 |
-
|
| 91 |
-
q, k, v = torch.chunk(converted[f"blocks.{block_idx}.attn.qkv.weight"], 3, dim=0)
|
| 92 |
-
q_bias, k_bias, v_bias = torch.chunk(converted[f"blocks.{block_idx}.attn.qkv.bias"], 3, dim=0)
|
| 93 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_q.weight"] = q
|
| 94 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_q.bias"] = q_bias
|
| 95 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_k.weight"] = k
|
| 96 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_k.bias"] = k_bias
|
| 97 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_v.weight"] = v
|
| 98 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_v.bias"] = v_bias
|
| 99 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_out.0.weight"] = converted[
|
| 100 |
-
f"blocks.{block_idx}.attn.proj.weight"
|
| 101 |
-
]
|
| 102 |
-
converted[f"transformer_blocks.{block_idx}.attn1.to_out.0.bias"] = converted[
|
| 103 |
-
f"blocks.{block_idx}.attn.proj.bias"
|
| 104 |
-
]
|
| 105 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.0.proj.weight"] = converted[
|
| 106 |
-
f"blocks.{block_idx}.mlp.fc1.weight"
|
| 107 |
-
]
|
| 108 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.0.proj.bias"] = converted[
|
| 109 |
-
f"blocks.{block_idx}.mlp.fc1.bias"
|
| 110 |
-
]
|
| 111 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.2.weight"] = converted[
|
| 112 |
-
f"blocks.{block_idx}.mlp.fc2.weight"
|
| 113 |
-
]
|
| 114 |
-
converted[f"transformer_blocks.{block_idx}.ff.net.2.bias"] = converted[
|
| 115 |
-
f"blocks.{block_idx}.mlp.fc2.bias"
|
| 116 |
-
]
|
| 117 |
-
|
| 118 |
-
for suffix in (
|
| 119 |
-
"attn.qkv.weight",
|
| 120 |
-
"attn.qkv.bias",
|
| 121 |
-
"attn.proj.weight",
|
| 122 |
-
"attn.proj.bias",
|
| 123 |
-
"mlp.fc1.weight",
|
| 124 |
-
"mlp.fc1.bias",
|
| 125 |
-
"mlp.fc2.weight",
|
| 126 |
-
"mlp.fc2.bias",
|
| 127 |
-
"adaLN_modulation.1.weight",
|
| 128 |
-
"adaLN_modulation.1.bias",
|
| 129 |
-
):
|
| 130 |
-
converted.pop(f"blocks.{block_idx}.{suffix}", None)
|
| 131 |
-
|
| 132 |
-
converted["proj_out_1.weight"] = converted.pop("final_layer.adaLN_modulation.1.weight")
|
| 133 |
-
converted["proj_out_1.bias"] = converted.pop("final_layer.adaLN_modulation.1.bias")
|
| 134 |
-
converted["proj_out_2.weight"] = converted.pop("final_layer.linear.weight")
|
| 135 |
-
converted["proj_out_2.bias"] = converted.pop("final_layer.linear.bias")
|
| 136 |
-
|
| 137 |
-
converted.pop("pos_embed", None)
|
| 138 |
-
for block_idx in range(depth):
|
| 139 |
-
for suffix in ("norm1.weight", "norm1.bias", "norm2.weight", "norm2.bias"):
|
| 140 |
-
converted.pop(f"blocks.{block_idx}.{suffix}", None)
|
| 141 |
-
|
| 142 |
-
return {key: tensor.detach().clone().contiguous() for key, tensor in converted.items()}
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
def load_legacy_dit_state_dict(
|
| 146 |
-
state_dict: Dict[str, torch.Tensor],
|
| 147 |
-
depth: int,
|
| 148 |
-
patch_size: int,
|
| 149 |
-
hidden_size: int,
|
| 150 |
-
num_heads: int,
|
| 151 |
-
) -> Dict[str, torch.Tensor]:
|
| 152 |
-
if "transformer_blocks.0.attn1.to_q.weight" in state_dict:
|
| 153 |
-
return state_dict
|
| 154 |
-
if any(key.startswith("blocks.") for key in state_dict):
|
| 155 |
-
return _convert_legacy_dit_state_dict(state_dict, depth)
|
| 156 |
-
return state_dict
|
| 157 |
|
| 158 |
|
| 159 |
def forward_dit_output(
|
| 160 |
-
transformer,
|
| 161 |
hidden_states: torch.Tensor,
|
| 162 |
timestep: Optional[torch.Tensor],
|
| 163 |
class_labels: torch.Tensor,
|
|
@@ -207,23 +106,6 @@ def forward_dit_output(
|
|
| 207 |
)
|
| 208 |
|
| 209 |
|
| 210 |
-
def remap_transformer_state_dict(
|
| 211 |
-
state_dict: Dict[str, torch.Tensor],
|
| 212 |
-
legacy_kwargs: Dict[str, int],
|
| 213 |
-
extra_prefixes: tuple[str, ...] = (),
|
| 214 |
-
) -> Dict[str, torch.Tensor]:
|
| 215 |
-
state_dict = load_legacy_dit_state_dict(state_dict, **legacy_kwargs)
|
| 216 |
-
remapped: Dict[str, torch.Tensor] = {}
|
| 217 |
-
for key, value in state_dict.items():
|
| 218 |
-
if any(key.startswith(prefix) for prefix in extra_prefixes):
|
| 219 |
-
remapped[key] = value
|
| 220 |
-
elif key.startswith("transformer."):
|
| 221 |
-
remapped[key] = value
|
| 222 |
-
else:
|
| 223 |
-
remapped[f"transformer.{key}"] = value
|
| 224 |
-
return remapped
|
| 225 |
-
|
| 226 |
-
|
| 227 |
def split_output_channels(output: torch.Tensor, in_channels: int) -> torch.Tensor:
|
| 228 |
if output.shape[1] == in_channels:
|
| 229 |
return output
|
|
@@ -259,6 +141,14 @@ class TimestepEmbedder(nn.Module):
|
|
| 259 |
return self.mlp(emb.to(dtype=weight_dtype))
|
| 260 |
|
| 261 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
class Generator(nn.Module):
|
| 263 |
def __init__(
|
| 264 |
self,
|
|
@@ -279,14 +169,7 @@ class Generator(nn.Module):
|
|
| 279 |
super().__init__()
|
| 280 |
self.use_t_src = use_t_src
|
| 281 |
self.use_t_tgt = use_t_tgt
|
| 282 |
-
self.
|
| 283 |
-
"depth": depth,
|
| 284 |
-
"hidden_size": hidden_size,
|
| 285 |
-
"patch_size": patch_size,
|
| 286 |
-
"num_heads": num_heads,
|
| 287 |
-
}
|
| 288 |
-
dit_cls = get_dit_transformer_class()
|
| 289 |
-
self.transformer = dit_cls(
|
| 290 |
**build_dit_config(
|
| 291 |
depth=depth,
|
| 292 |
hidden_size=hidden_size,
|
|
@@ -306,20 +189,9 @@ class Generator(nn.Module):
|
|
| 306 |
nn.init.normal_(self.t_tgt_embedder.mlp[2].weight, std=0.02)
|
| 307 |
|
| 308 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 309 |
-
|
| 310 |
-
has_extra = any(key.startswith(extra) for key in state_dict)
|
| 311 |
-
has_t_embedder = any("t_embedder" in key for key in state_dict)
|
| 312 |
-
remapped = remap_transformer_state_dict(state_dict, self._legacy_kwargs, extra_prefixes=extra)
|
| 313 |
-
if self.use_t_tgt and not has_extra:
|
| 314 |
-
strict = False
|
| 315 |
-
if not has_t_embedder:
|
| 316 |
strict = False
|
| 317 |
-
|
| 318 |
-
if not has_t_embedder:
|
| 319 |
-
for name, param in self.named_parameters():
|
| 320 |
-
if "timestep_embedder" in name:
|
| 321 |
-
param.data.zero_()
|
| 322 |
-
return out
|
| 323 |
|
| 324 |
def forward(self, x, y, t_src=None, t_tgt=None):
|
| 325 |
if self.use_t_src and t_src is not None:
|
|
@@ -353,14 +225,7 @@ class GeneratorDeep(nn.Module):
|
|
| 353 |
):
|
| 354 |
super().__init__()
|
| 355 |
self.repeat = repeat
|
| 356 |
-
self.
|
| 357 |
-
"depth": depth,
|
| 358 |
-
"hidden_size": hidden_size,
|
| 359 |
-
"patch_size": patch_size,
|
| 360 |
-
"num_heads": num_heads,
|
| 361 |
-
}
|
| 362 |
-
dit_cls = get_dit_transformer_class()
|
| 363 |
-
self.transformer = dit_cls(
|
| 364 |
**build_dit_config(
|
| 365 |
depth=depth,
|
| 366 |
hidden_size=hidden_size,
|
|
@@ -376,16 +241,7 @@ class GeneratorDeep(nn.Module):
|
|
| 376 |
self.in_channels = in_channels
|
| 377 |
|
| 378 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 379 |
-
|
| 380 |
-
has_t_embedder = any("t_embedder" in key for key in state_dict)
|
| 381 |
-
if not has_t_embedder:
|
| 382 |
-
strict = False
|
| 383 |
-
out = super().load_state_dict(remapped, strict=strict)
|
| 384 |
-
if not has_t_embedder:
|
| 385 |
-
for name, param in self.named_parameters():
|
| 386 |
-
if "timestep_embedder" in name:
|
| 387 |
-
param.data.zero_()
|
| 388 |
-
return out
|
| 389 |
|
| 390 |
def forward(self, x, y, *args, **kwargs):
|
| 391 |
hidden_states = self.transformer.pos_embed(x)
|
|
@@ -429,7 +285,7 @@ class GeneratorDeep(nn.Module):
|
|
| 429 |
return split_output_channels(output, self.in_channels)
|
| 430 |
|
| 431 |
|
| 432 |
-
class AFMGenerator2DModel(
|
| 433 |
config_name = "config.json"
|
| 434 |
|
| 435 |
@register_to_config
|
|
@@ -470,7 +326,7 @@ class AFMGenerator2DModel(Generator, ModelMixin, ConfigMixin):
|
|
| 470 |
)
|
| 471 |
|
| 472 |
|
| 473 |
-
class AFMGeneratorDeep2DModel(
|
| 474 |
config_name = "config.json"
|
| 475 |
|
| 476 |
@register_to_config
|
|
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
|
|
|
| 5 |
import math
|
| 6 |
from typing import Any, Dict, Optional
|
| 7 |
|
|
|
|
| 9 |
import torch.nn.functional as F
|
| 10 |
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
| 11 |
from diffusers.models.modeling_utils import ModelMixin
|
| 12 |
+
from diffusers.models.transformers.dit_transformer_2d import DiTTransformer2DModel
|
| 13 |
from torch import nn
|
| 14 |
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def build_dit_config(
|
| 17 |
*,
|
| 18 |
depth: int = 28,
|
|
|
|
| 48 |
}
|
| 49 |
|
| 50 |
|
| 51 |
+
_PREFIX_KEYS = ("transformer.", "t_tgt_embedder.")
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def _wrap_dit_state_dict(state_dict: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]:
|
| 55 |
+
return {k if k.startswith(_PREFIX_KEYS) else f"transformer.{k}": v for k, v in state_dict.items()}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
def forward_dit_output(
|
| 59 |
+
transformer: DiTTransformer2DModel,
|
| 60 |
hidden_states: torch.Tensor,
|
| 61 |
timestep: Optional[torch.Tensor],
|
| 62 |
class_labels: torch.Tensor,
|
|
|
|
| 106 |
)
|
| 107 |
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
def split_output_channels(output: torch.Tensor, in_channels: int) -> torch.Tensor:
|
| 110 |
if output.shape[1] == in_channels:
|
| 111 |
return output
|
|
|
|
| 141 |
return self.mlp(emb.to(dtype=weight_dtype))
|
| 142 |
|
| 143 |
|
| 144 |
+
class _AFMCheckpointMixin(ModelMixin):
|
| 145 |
+
def _fix_state_dict_keys_on_load(self, state_dict):
|
| 146 |
+
wrapped = _wrap_dit_state_dict(state_dict)
|
| 147 |
+
state_dict.clear()
|
| 148 |
+
state_dict.update(wrapped)
|
| 149 |
+
super()._fix_state_dict_keys_on_load(state_dict)
|
| 150 |
+
|
| 151 |
+
|
| 152 |
class Generator(nn.Module):
|
| 153 |
def __init__(
|
| 154 |
self,
|
|
|
|
| 169 |
super().__init__()
|
| 170 |
self.use_t_src = use_t_src
|
| 171 |
self.use_t_tgt = use_t_tgt
|
| 172 |
+
self.transformer = DiTTransformer2DModel(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
**build_dit_config(
|
| 174 |
depth=depth,
|
| 175 |
hidden_size=hidden_size,
|
|
|
|
| 189 |
nn.init.normal_(self.t_tgt_embedder.mlp[2].weight, std=0.02)
|
| 190 |
|
| 191 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 192 |
+
if self.use_t_tgt and not any(key.startswith("t_tgt_embedder.") for key in state_dict):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
strict = False
|
| 194 |
+
return super().load_state_dict(_wrap_dit_state_dict(state_dict), strict=strict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
def forward(self, x, y, t_src=None, t_tgt=None):
|
| 197 |
if self.use_t_src and t_src is not None:
|
|
|
|
| 225 |
):
|
| 226 |
super().__init__()
|
| 227 |
self.repeat = repeat
|
| 228 |
+
self.transformer = DiTTransformer2DModel(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
**build_dit_config(
|
| 230 |
depth=depth,
|
| 231 |
hidden_size=hidden_size,
|
|
|
|
| 241 |
self.in_channels = in_channels
|
| 242 |
|
| 243 |
def load_state_dict(self, state_dict, strict: bool = True):
|
| 244 |
+
return super().load_state_dict(_wrap_dit_state_dict(state_dict), strict=strict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
def forward(self, x, y, *args, **kwargs):
|
| 247 |
hidden_states = self.transformer.pos_embed(x)
|
|
|
|
| 285 |
return split_output_channels(output, self.in_channels)
|
| 286 |
|
| 287 |
|
| 288 |
+
class AFMGenerator2DModel(_AFMCheckpointMixin, Generator, ConfigMixin):
|
| 289 |
config_name = "config.json"
|
| 290 |
|
| 291 |
@register_to_config
|
|
|
|
| 326 |
)
|
| 327 |
|
| 328 |
|
| 329 |
+
class AFMGeneratorDeep2DModel(_AFMCheckpointMixin, GeneratorDeep, ConfigMixin):
|
| 330 |
config_name = "config.json"
|
| 331 |
|
| 332 |
@register_to_config
|