Add d32 64ep LightningDiT inference weights and sampling option
Browse files- README.md +4 -1
- configs/d32-64ep.json +100 -0
- inference.py +10 -6
- lightningdit/lightningdit-f16d32-64ep.safetensors +3 -0
README.md
CHANGED
|
@@ -15,7 +15,7 @@ Matched ImageNet-256 DM-VAE tokenizers and LightningDiT-XL/1 generators.
|
|
| 15 |
|
| 16 |
| Variant | VAE | LightningDiT |
|
| 17 |
| --- | --- | --- |
|
| 18 |
-
| f16d32 | [50ep](vae/dmvae-f16d32-50ep.safetensors) | [400ep](lightningdit/lightningdit-f16d32-400ep.safetensors) |
|
| 19 |
| f16d64 | [50ep](vae/dmvae-f16d64-50ep.safetensors) | [400ep](lightningdit/lightningdit-f16d64-400ep.safetensors) |
|
| 20 |
|
| 21 |
Use each generator with the tokenizer and channel normalization in its matching `configs/d32.json` or `configs/d64.json`. Weights retain their original tensor precision.
|
|
@@ -29,6 +29,7 @@ git clone https://github.com/ustcchx/DM-VAE.git
|
|
| 29 |
cd DM-VAE
|
| 30 |
python -m pip install -e ".[hub]" safetensors
|
| 31 |
hf download Davidwang215/dmvae --local-dir weights/dmvae
|
|
|
|
| 32 |
python weights/dmvae/inference.py --variant d32 --classes 207 --device cuda --precision bf16 --output samples/d32
|
| 33 |
python weights/dmvae/inference.py --variant d64 --classes 207 --device cuda --precision bf16 --output samples/d64
|
| 34 |
```
|
|
@@ -44,3 +45,5 @@ sys.path.insert(0, "weights/dmvae")
|
|
| 44 |
from inference import load_pair
|
| 45 |
vae, dit, means, scales = load_pair("weights/dmvae", "d32", "cuda", torch.bfloat16)
|
| 46 |
```
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
| Variant | VAE | LightningDiT |
|
| 17 |
| --- | --- | --- |
|
| 18 |
+
| f16d32 | [50ep](vae/dmvae-f16d32-50ep.safetensors) | [64ep](lightningdit/lightningdit-f16d32-64ep.safetensors), [400ep](lightningdit/lightningdit-f16d32-400ep.safetensors) |
|
| 19 |
| f16d64 | [50ep](vae/dmvae-f16d64-50ep.safetensors) | [400ep](lightningdit/lightningdit-f16d64-400ep.safetensors) |
|
| 20 |
|
| 21 |
Use each generator with the tokenizer and channel normalization in its matching `configs/d32.json` or `configs/d64.json`. Weights retain their original tensor precision.
|
|
|
|
| 29 |
cd DM-VAE
|
| 30 |
python -m pip install -e ".[hub]" safetensors
|
| 31 |
hf download Davidwang215/dmvae --local-dir weights/dmvae
|
| 32 |
+
python weights/dmvae/inference.py --variant d32 --dit-epochs 64 --classes 207 --device cuda --precision bf16 --output samples/d32-64ep
|
| 33 |
python weights/dmvae/inference.py --variant d32 --classes 207 --device cuda --precision bf16 --output samples/d32
|
| 34 |
python weights/dmvae/inference.py --variant d64 --classes 207 --device cuda --precision bf16 --output samples/d64
|
| 35 |
```
|
|
|
|
| 45 |
from inference import load_pair
|
| 46 |
vae, dit, means, scales = load_pair("weights/dmvae", "d32", "cuda", torch.bfloat16)
|
| 47 |
```
|
| 48 |
+
|
| 49 |
+
Select the f16d32 64ep generator with `--dit-epochs 64`. The default is 400ep. Both f16d32 generators use the same f16d32 50ep VAE and channel normalization.
|
configs/d32-64ep.json
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"format": "safetensors",
|
| 3 |
+
"image_size": 256,
|
| 4 |
+
"variant": "d32",
|
| 5 |
+
"vae": {
|
| 6 |
+
"file": "vae/dmvae-f16d32-50ep.safetensors",
|
| 7 |
+
"sha256": "03f037b19412efb8406034a45a72fd9e7a70dcbb4f0de19c0a9fab67cc64a722",
|
| 8 |
+
"architecture": {
|
| 9 |
+
"z_channels_high": 32,
|
| 10 |
+
"z_channels_low": 16,
|
| 11 |
+
"legacy_bottleneck": false
|
| 12 |
+
}
|
| 13 |
+
},
|
| 14 |
+
"dit": {
|
| 15 |
+
"file": "lightningdit/lightningdit-f16d32-64ep.safetensors",
|
| 16 |
+
"sha256": "7c9c925c4d7980524092ec4593a4a7ecde7fab5e3422801651fa5b616dac28d3",
|
| 17 |
+
"architecture": {
|
| 18 |
+
"latent_shape": [
|
| 19 |
+
32,
|
| 20 |
+
16,
|
| 21 |
+
16
|
| 22 |
+
],
|
| 23 |
+
"z_channels_low": 16,
|
| 24 |
+
"depth": 28,
|
| 25 |
+
"hidden_size": 1152,
|
| 26 |
+
"num_heads": 16,
|
| 27 |
+
"latent_patch_size": 1,
|
| 28 |
+
"num_classes": 1000,
|
| 29 |
+
"align_layer": 7
|
| 30 |
+
}
|
| 31 |
+
},
|
| 32 |
+
"channel_means": [
|
| 33 |
+
0.11889809370040894,
|
| 34 |
+
-0.0498211495578289,
|
| 35 |
+
0.07956922799348831,
|
| 36 |
+
0.1282467246055603,
|
| 37 |
+
-0.09310000389814377,
|
| 38 |
+
0.01919279806315899,
|
| 39 |
+
0.008933479897677898,
|
| 40 |
+
0.06343873590230942,
|
| 41 |
+
0.09063224494457245,
|
| 42 |
+
-0.09089301526546478,
|
| 43 |
+
0.034680724143981934,
|
| 44 |
+
-0.07357332110404968,
|
| 45 |
+
-0.12892358005046844,
|
| 46 |
+
0.06941217929124832,
|
| 47 |
+
-0.15016569197177887,
|
| 48 |
+
-0.11732754856348038,
|
| 49 |
+
-0.08246354013681412,
|
| 50 |
+
-0.012567241676151752,
|
| 51 |
+
-0.1367921233177185,
|
| 52 |
+
0.13331466913223267,
|
| 53 |
+
0.027383580803871155,
|
| 54 |
+
-0.027444366365671158,
|
| 55 |
+
0.09910911321640015,
|
| 56 |
+
-0.11326712369918823,
|
| 57 |
+
0.023853497579693794,
|
| 58 |
+
0.1297440379858017,
|
| 59 |
+
0.07543385028839111,
|
| 60 |
+
-0.13411222398281097,
|
| 61 |
+
0.09224484860897064,
|
| 62 |
+
0.08815101534128189,
|
| 63 |
+
0.0031031614635139704,
|
| 64 |
+
0.17456728219985962
|
| 65 |
+
],
|
| 66 |
+
"channel_std": [
|
| 67 |
+
1.2717455625534058,
|
| 68 |
+
1.044906497001648,
|
| 69 |
+
1.8927958011627197,
|
| 70 |
+
1.4252386093139648,
|
| 71 |
+
1.8472360372543335,
|
| 72 |
+
1.0113850831985474,
|
| 73 |
+
1.8590723276138306,
|
| 74 |
+
2.1371302604675293,
|
| 75 |
+
2.2889883518218994,
|
| 76 |
+
1.6720170974731445,
|
| 77 |
+
1.6339408159255981,
|
| 78 |
+
2.2134768962860107,
|
| 79 |
+
1.4047157764434814,
|
| 80 |
+
1.0915695428848267,
|
| 81 |
+
1.2994840145111084,
|
| 82 |
+
2.1737470626831055,
|
| 83 |
+
1.340841293334961,
|
| 84 |
+
1.715541958808899,
|
| 85 |
+
1.5402436256408691,
|
| 86 |
+
2.1781232357025146,
|
| 87 |
+
0.714764416217804,
|
| 88 |
+
1.110756516456604,
|
| 89 |
+
1.363089680671692,
|
| 90 |
+
1.2082487344741821,
|
| 91 |
+
1.1547572612762451,
|
| 92 |
+
1.41787588596344,
|
| 93 |
+
1.297340989112854,
|
| 94 |
+
1.2110263109207153,
|
| 95 |
+
1.4920814037322998,
|
| 96 |
+
1.2209465503692627,
|
| 97 |
+
1.41843843460083,
|
| 98 |
+
2.409214735031128
|
| 99 |
+
]
|
| 100 |
+
}
|
inference.py
CHANGED
|
@@ -17,7 +17,7 @@ def digest(path):
|
|
| 17 |
return h.hexdigest()
|
| 18 |
|
| 19 |
|
| 20 |
-
def load_pair(directory, variant='d32', device='cpu', dtype=torch.float32):
|
| 21 |
"""Return VAE, DiT, and the channel normalization for one matched pair."""
|
| 22 |
from dmvae.autoencoder import DMVAE
|
| 23 |
from generation.model import LatentDiT
|
|
@@ -25,8 +25,11 @@ def load_pair(directory, variant='d32', device='cpu', dtype=torch.float32):
|
|
| 25 |
|
| 26 |
if variant not in ('d32', 'd64'):
|
| 27 |
raise ValueError('variant must be d32 or d64')
|
|
|
|
|
|
|
| 28 |
root = Path(directory)
|
| 29 |
-
|
|
|
|
| 30 |
loaded = {}
|
| 31 |
for component in ('vae', 'dit'):
|
| 32 |
spec = config[component]
|
|
@@ -48,16 +51,16 @@ def load_pair(directory, variant='d32', device='cpu', dtype=torch.float32):
|
|
| 48 |
|
| 49 |
|
| 50 |
@torch.no_grad()
|
| 51 |
-
def generate(directory, variant='d32', classes=(207,), seed=1234, steps=250, device='cpu', dtype=torch.float32, cfg=False):
|
| 52 |
from generation.sampling import euler_sample, RECIPES
|
| 53 |
|
| 54 |
-
vae, dit, means, scales = load_pair(directory, variant, device, dtype)
|
| 55 |
labels = torch.tensor(classes, device=device, dtype=torch.long)
|
| 56 |
if labels.numel() == 0 or ((labels < 0) | (labels >= 1000)).any():
|
| 57 |
raise ValueError('Provide ImageNet class indices in [0, 999]')
|
| 58 |
generator = torch.Generator(device=device).manual_seed(seed)
|
| 59 |
noise = torch.randn((len(labels), vae.z_channels_high, 16, 16), generator=generator, device=device, dtype=dtype)
|
| 60 |
-
recipe = ('d64-' if variant == 'd64' else '') + '
|
| 61 |
autocast = torch.autocast(torch.device(device).type, dtype=dtype) if dtype == torch.bfloat16 else nullcontext()
|
| 62 |
with autocast:
|
| 63 |
latent = euler_sample(dit, vae, noise, labels, means, scales, *RECIPES[recipe], steps=steps)
|
|
@@ -73,6 +76,7 @@ def main():
|
|
| 73 |
p = argparse.ArgumentParser(description=__doc__)
|
| 74 |
p.add_argument('--directory', type=Path, default=Path(__file__).resolve().parent)
|
| 75 |
p.add_argument('--variant', choices=('d32', 'd64'), default='d32')
|
|
|
|
| 76 |
p.add_argument('--classes', type=int, nargs='+', default=[207])
|
| 77 |
p.add_argument('--seed', type=int, default=1234)
|
| 78 |
p.add_argument('--steps', type=int, default=250, help='Euler sampling steps')
|
|
@@ -82,7 +86,7 @@ def main():
|
|
| 82 |
p.add_argument('--output', type=Path, default=Path('samples'))
|
| 83 |
args = p.parse_args()
|
| 84 |
dtype = torch.bfloat16 if args.precision == 'bf16' else torch.float32
|
| 85 |
-
images = generate(args.directory, args.variant, args.classes, args.seed, args.steps, args.device, dtype, args.cfg)
|
| 86 |
args.output.mkdir(parents=True, exist_ok=True)
|
| 87 |
for index, image in enumerate(images):
|
| 88 |
rgb = image.add(1).mul(127.5).round().to(torch.uint8).permute(1, 2, 0).cpu().numpy()
|
|
|
|
| 17 |
return h.hexdigest()
|
| 18 |
|
| 19 |
|
| 20 |
+
def load_pair(directory, variant='d32', device='cpu', dtype=torch.float32, dit_epochs=400):
|
| 21 |
"""Return VAE, DiT, and the channel normalization for one matched pair."""
|
| 22 |
from dmvae.autoencoder import DMVAE
|
| 23 |
from generation.model import LatentDiT
|
|
|
|
| 25 |
|
| 26 |
if variant not in ('d32', 'd64'):
|
| 27 |
raise ValueError('variant must be d32 or d64')
|
| 28 |
+
if dit_epochs not in (64, 400) or (dit_epochs == 64 and variant != 'd32'):
|
| 29 |
+
raise ValueError('64ep is available for d32; 400ep is available for d32 and d64')
|
| 30 |
root = Path(directory)
|
| 31 |
+
config_name = variant + ('-64ep' if dit_epochs == 64 else '') + '.json'
|
| 32 |
+
config = json.loads((root / 'configs' / config_name).read_text())
|
| 33 |
loaded = {}
|
| 34 |
for component in ('vae', 'dit'):
|
| 35 |
spec = config[component]
|
|
|
|
| 51 |
|
| 52 |
|
| 53 |
@torch.no_grad()
|
| 54 |
+
def generate(directory, variant='d32', classes=(207,), seed=1234, steps=250, device='cpu', dtype=torch.float32, cfg=False, dit_epochs=400):
|
| 55 |
from generation.sampling import euler_sample, RECIPES
|
| 56 |
|
| 57 |
+
vae, dit, means, scales = load_pair(directory, variant, device, dtype, dit_epochs=dit_epochs)
|
| 58 |
labels = torch.tensor(classes, device=device, dtype=torch.long)
|
| 59 |
if labels.numel() == 0 or ((labels < 0) | (labels >= 1000)).any():
|
| 60 |
raise ValueError('Provide ImageNet class indices in [0, 999]')
|
| 61 |
generator = torch.Generator(device=device).manual_seed(seed)
|
| 62 |
noise = torch.randn((len(labels), vae.z_channels_high, 16, 16), generator=generator, device=device, dtype=dtype)
|
| 63 |
+
recipe = ('d64-' if variant == 'd64' else '') + f'{dit_epochs}ep-ig' + ('-cfg' if cfg else '')
|
| 64 |
autocast = torch.autocast(torch.device(device).type, dtype=dtype) if dtype == torch.bfloat16 else nullcontext()
|
| 65 |
with autocast:
|
| 66 |
latent = euler_sample(dit, vae, noise, labels, means, scales, *RECIPES[recipe], steps=steps)
|
|
|
|
| 76 |
p = argparse.ArgumentParser(description=__doc__)
|
| 77 |
p.add_argument('--directory', type=Path, default=Path(__file__).resolve().parent)
|
| 78 |
p.add_argument('--variant', choices=('d32', 'd64'), default='d32')
|
| 79 |
+
p.add_argument('--dit-epochs', type=int, choices=(64, 400), default=400)
|
| 80 |
p.add_argument('--classes', type=int, nargs='+', default=[207])
|
| 81 |
p.add_argument('--seed', type=int, default=1234)
|
| 82 |
p.add_argument('--steps', type=int, default=250, help='Euler sampling steps')
|
|
|
|
| 86 |
p.add_argument('--output', type=Path, default=Path('samples'))
|
| 87 |
args = p.parse_args()
|
| 88 |
dtype = torch.bfloat16 if args.precision == 'bf16' else torch.float32
|
| 89 |
+
images = generate(args.directory, args.variant, args.classes, args.seed, args.steps, args.device, dtype, args.cfg, dit_epochs=args.dit_epochs)
|
| 90 |
args.output.mkdir(parents=True, exist_ok=True)
|
| 91 |
for index, image in enumerate(images):
|
| 92 |
rgb = image.add(1).mul(127.5).round().to(torch.uint8).permute(1, 2, 0).cpu().numpy()
|
lightningdit/lightningdit-f16d32-64ep.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7c9c925c4d7980524092ec4593a4a7ecde7fab5e3422801651fa5b616dac28d3
|
| 3 |
+
size 2706627304
|