Update all files for BitDance-ImageNet-diffusers
Browse files
BitDance_L_1x/transformer/modeling_transformer.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import json
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
import torch
|
| 7 |
+
from safetensors.torch import load_file as load_safetensors
|
| 8 |
+
|
| 9 |
+
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
| 10 |
+
from diffusers.models.modeling_utils import ModelMixin
|
| 11 |
+
|
| 12 |
+
# NOTE: Diffusers dynamic module loader only copies directly-referenced relative imports.
|
| 13 |
+
# These guarded imports are intentionally never executed, but they force dependent files
|
| 14 |
+
# (and their siblings) to be copied into the dynamic module cache.
|
| 15 |
+
if False: # pragma: no cover
|
| 16 |
+
from .model import BitDance_B as _BD_B_STD
|
| 17 |
+
from .model import BitDance_H as _BD_H_STD
|
| 18 |
+
from .model import BitDance_L as _BD_L_STD
|
| 19 |
+
from .model_parallel import BitDance_B as _BD_B_PAR
|
| 20 |
+
from .model_parallel import BitDance_H as _BD_H_PAR
|
| 21 |
+
from .model_parallel import BitDance_L as _BD_L_PAR
|
| 22 |
+
from .diff_head import DiffHead as _DiffHead
|
| 23 |
+
from .diff_head_parallel import DiffHead as _DiffHeadParallel
|
| 24 |
+
from .layers import TransformerBlock as _TB
|
| 25 |
+
from .layers_parallel import TransformerBlock as _TBP
|
| 26 |
+
from .qae import VQModel as _VQ
|
| 27 |
+
from .gfq import GFQ as _GFQ
|
| 28 |
+
from .sampling import euler_maruyama as _EM
|
| 29 |
+
from .sampling_parallel import euler_maruyama as _EMP
|
| 30 |
+
from .utils import patchify_raster as _PR
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class BitDanceImageNetTransformer(ModelMixin, ConfigMixin):
|
| 34 |
+
@register_to_config
|
| 35 |
+
def __init__(
|
| 36 |
+
self,
|
| 37 |
+
architecture: str,
|
| 38 |
+
parallel_num: int,
|
| 39 |
+
resolution: int,
|
| 40 |
+
down_size: int,
|
| 41 |
+
latent_dim: int,
|
| 42 |
+
num_classes: int,
|
| 43 |
+
runtime_impl: str,
|
| 44 |
+
parallel_mode: str = "patch",
|
| 45 |
+
time_schedule: str = "logit_normal",
|
| 46 |
+
time_shift: float = 1.0,
|
| 47 |
+
p_std: float = 1.0,
|
| 48 |
+
p_mean: float = 0.0,
|
| 49 |
+
):
|
| 50 |
+
super().__init__()
|
| 51 |
+
|
| 52 |
+
kwargs = dict(
|
| 53 |
+
resolution=resolution,
|
| 54 |
+
down_size=down_size,
|
| 55 |
+
patch_size=1,
|
| 56 |
+
latent_dim=latent_dim,
|
| 57 |
+
diff_batch_mul=4,
|
| 58 |
+
cls_token_num=64,
|
| 59 |
+
num_classes=num_classes,
|
| 60 |
+
grad_checkpointing=False,
|
| 61 |
+
trained_vae="",
|
| 62 |
+
drop_rate=0.0,
|
| 63 |
+
perturb_schedule="constant",
|
| 64 |
+
perturb_rate=0.0,
|
| 65 |
+
perturb_rate_max=0.3,
|
| 66 |
+
time_schedule=time_schedule,
|
| 67 |
+
time_shift=time_shift,
|
| 68 |
+
P_std=p_std,
|
| 69 |
+
P_mean=p_mean,
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
if runtime_impl == "model_parallel.py" or parallel_num > 1:
|
| 73 |
+
from .model_parallel import BitDance_B, BitDance_H, BitDance_L
|
| 74 |
+
|
| 75 |
+
ctors = {"BitDance-B": BitDance_B, "BitDance-L": BitDance_L, "BitDance-H": BitDance_H}
|
| 76 |
+
kwargs.update(parallel_num=parallel_num, parallel_mode=parallel_mode)
|
| 77 |
+
else:
|
| 78 |
+
from .model import BitDance_B, BitDance_H, BitDance_L
|
| 79 |
+
|
| 80 |
+
ctors = {"BitDance-B": BitDance_B, "BitDance-L": BitDance_L, "BitDance-H": BitDance_H}
|
| 81 |
+
|
| 82 |
+
self.runtime_model = ctors[architecture](**kwargs)
|
| 83 |
+
|
| 84 |
+
@classmethod
|
| 85 |
+
def from_pretrained(cls, pretrained_model_name_or_path: str, *args, **kwargs):
|
| 86 |
+
del args, kwargs
|
| 87 |
+
model_dir = Path(pretrained_model_name_or_path)
|
| 88 |
+
config = json.loads((model_dir / "config.json").read_text(encoding="utf-8"))
|
| 89 |
+
model = cls(
|
| 90 |
+
architecture=config["architecture"],
|
| 91 |
+
parallel_num=int(config["parallel_num"]),
|
| 92 |
+
resolution=int(config["resolution"]),
|
| 93 |
+
down_size=int(config["down_size"]),
|
| 94 |
+
latent_dim=int(config["latent_dim"]),
|
| 95 |
+
num_classes=int(config["num_classes"]),
|
| 96 |
+
runtime_impl=config["runtime_impl"],
|
| 97 |
+
parallel_mode=config.get("parallel_mode", "patch"),
|
| 98 |
+
time_schedule=config.get("time_schedule", "logit_normal"),
|
| 99 |
+
time_shift=float(config.get("time_shift", 1.0)),
|
| 100 |
+
p_std=float(config.get("p_std", 1.0)),
|
| 101 |
+
p_mean=float(config.get("p_mean", 0.0)),
|
| 102 |
+
)
|
| 103 |
+
state = load_safetensors(model_dir / "diffusion_pytorch_model.safetensors")
|
| 104 |
+
model.runtime_model.load_state_dict(state, strict=True)
|
| 105 |
+
model.eval()
|
| 106 |
+
return model
|
| 107 |
+
|
| 108 |
+
@torch.no_grad()
|
| 109 |
+
def sample(
|
| 110 |
+
self,
|
| 111 |
+
class_ids: torch.Tensor,
|
| 112 |
+
sample_steps: int = 100,
|
| 113 |
+
cfg_scale: float = 4.6,
|
| 114 |
+
chunk_size: int = 0,
|
| 115 |
+
) -> torch.Tensor:
|
| 116 |
+
return self.runtime_model.sample(
|
| 117 |
+
cond=class_ids,
|
| 118 |
+
sample_steps=sample_steps,
|
| 119 |
+
cfg_scale=cfg_scale,
|
| 120 |
+
chunk_size=chunk_size,
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
def forward(self, *args, **kwargs):
|
| 124 |
+
return self.runtime_model(*args, **kwargs)
|