Davidwang215 commited on
Commit
d302357
·
verified ·
1 Parent(s): 4919e39

Add matched DM-VAE 50ep and LightningDiT 400ep inference weights

Browse files
README.md CHANGED
@@ -1,3 +1,46 @@
1
  ---
2
  license: apache-2.0
 
 
 
 
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: apache-2.0
3
+ library_name: pytorch
4
+ pipeline_tag: unconditional-image-generation
5
+ tags:
6
+ - image-generation
7
+ - vae
8
+ - diffusion
9
+ - safetensors
10
  ---
11
+
12
+ # DM-VAE inference weights
13
+
14
+ 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.
22
+
23
+ ## Inference
24
+
25
+ Install the [DM-VAE inference code](https://github.com/ustcchx/DM-VAE) and download this repository:
26
+
27
+ ```bash
28
+ 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
+ ```
35
+
36
+ The loader verifies the SHA-256 of both weight files and loads each architecture strictly. The JSON files provide the matching architecture and latent-channel statistics. `inference.py` supports IG_TS sampling and optional interval CFG with `--cfg`.
37
+
38
+ For direct use in Python:
39
+
40
+ ```python
41
+ import sys
42
+ import torch
43
+ 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
+ ```
configs/d32.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-400ep.safetensors",
16
+ "sha256": "fd109b8e44feb12e4b5132e935f86922d232e661ba27d51355f13d9a89e1116c",
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
+ }
configs/d64.json ADDED
@@ -0,0 +1,164 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "format": "safetensors",
3
+ "image_size": 256,
4
+ "variant": "d64",
5
+ "vae": {
6
+ "file": "vae/dmvae-f16d64-50ep.safetensors",
7
+ "sha256": "f66e53a0409abfaa8150d1eccae204d193a33c897d89c8995d7f63b50aee2a03",
8
+ "architecture": {
9
+ "z_channels_high": 64,
10
+ "z_channels_low": 32,
11
+ "legacy_bottleneck": false
12
+ }
13
+ },
14
+ "dit": {
15
+ "file": "lightningdit/lightningdit-f16d64-400ep.safetensors",
16
+ "sha256": "cd40ead370ea6ce3f705a5daac80b6ccac3b1cec17914b1d0cb54fab7f94ad56",
17
+ "architecture": {
18
+ "latent_shape": [
19
+ 64,
20
+ 16,
21
+ 16
22
+ ],
23
+ "z_channels_low": 32,
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.028341079130768776,
34
+ -0.09949994087219238,
35
+ -0.03160687908530235,
36
+ -0.11842840909957886,
37
+ -0.11622001975774765,
38
+ -0.07433467358350754,
39
+ 0.055694159120321274,
40
+ -0.0011214334517717361,
41
+ -0.16256959736347198,
42
+ 0.07401937246322632,
43
+ 0.27884745597839355,
44
+ -0.19724160432815552,
45
+ 0.1270962655544281,
46
+ 0.06486223638057709,
47
+ -0.04896196350455284,
48
+ 0.08737696707248688,
49
+ 0.13284222781658173,
50
+ -0.026954934000968933,
51
+ -0.011626940220594406,
52
+ -0.058410365134477615,
53
+ -0.15890443325042725,
54
+ 0.045279789716005325,
55
+ -0.18521016836166382,
56
+ -0.0017506134463474154,
57
+ -0.1476009488105774,
58
+ -0.10280190408229828,
59
+ -0.015811553224921227,
60
+ -0.07009363174438477,
61
+ 0.018663663417100906,
62
+ -0.12013091892004013,
63
+ -0.1705775260925293,
64
+ -0.08432281017303467,
65
+ -0.10709524154663086,
66
+ -0.1219983920454979,
67
+ -0.09910440444946289,
68
+ -0.019219089299440384,
69
+ -0.0013863262720406055,
70
+ -0.2393021434545517,
71
+ -0.15514062345027924,
72
+ 0.01716453582048416,
73
+ -0.13546082377433777,
74
+ -0.11723585426807404,
75
+ -0.006290379911661148,
76
+ -0.02350301295518875,
77
+ 0.03779488429427147,
78
+ 0.09345638006925583,
79
+ 0.07353519648313522,
80
+ -0.20218366384506226,
81
+ -0.37422478199005127,
82
+ -0.1737336814403534,
83
+ -0.08720355480909348,
84
+ 0.018317075446248055,
85
+ 0.11454770714044571,
86
+ 0.016878275200724602,
87
+ -0.01129418145865202,
88
+ 0.08460887521505356,
89
+ 0.1117834597826004,
90
+ -0.0214847419410944,
91
+ 0.06342624127864838,
92
+ -0.1730186939239502,
93
+ -0.010302874259650707,
94
+ -0.11116563528776169,
95
+ -0.034337736666202545,
96
+ 0.07831554859876633
97
+ ],
98
+ "channel_std": [
99
+ 0.9022582769393921,
100
+ 0.889069139957428,
101
+ 3.505369186401367,
102
+ 1.0721676349639893,
103
+ 1.4140844345092773,
104
+ 0.8453800678253174,
105
+ 0.987554132938385,
106
+ 0.8046386241912842,
107
+ 1.0005619525909424,
108
+ 0.958489716053009,
109
+ 1.9488601684570312,
110
+ 1.0733662843704224,
111
+ 0.9201922416687012,
112
+ 1.2917345762252808,
113
+ 1.1065564155578613,
114
+ 2.8435702323913574,
115
+ 1.1295506954193115,
116
+ 0.8714216947555542,
117
+ 0.972705602645874,
118
+ 0.9654010534286499,
119
+ 1.0596938133239746,
120
+ 0.6941062211990356,
121
+ 0.892557680606842,
122
+ 2.817473888397217,
123
+ 1.6147539615631104,
124
+ 1.2007898092269897,
125
+ 1.0996588468551636,
126
+ 1.0066673755645752,
127
+ 0.8409624695777893,
128
+ 1.7884219884872437,
129
+ 1.0664924383163452,
130
+ 0.747014582157135,
131
+ 1.2121851444244385,
132
+ 1.0160459280014038,
133
+ 1.015558123588562,
134
+ 0.9143518209457397,
135
+ 0.6672655940055847,
136
+ 1.5574101209640503,
137
+ 1.047904133796692,
138
+ 1.9895871877670288,
139
+ 2.371877670288086,
140
+ 1.737741231918335,
141
+ 0.7887402772903442,
142
+ 0.9496433734893799,
143
+ 0.7692137360572815,
144
+ 0.9032579660415649,
145
+ 1.0647116899490356,
146
+ 0.9167023301124573,
147
+ 1.7463006973266602,
148
+ 1.5600683689117432,
149
+ 3.154749870300293,
150
+ 0.9171758890151978,
151
+ 0.9908086061477661,
152
+ 0.7302135229110718,
153
+ 0.7804285883903503,
154
+ 1.2411710023880005,
155
+ 1.9832549095153809,
156
+ 1.3594287633895874,
157
+ 0.8476155996322632,
158
+ 2.2670445442199707,
159
+ 2.206840753555298,
160
+ 1.001891016960144,
161
+ 1.5537095069885254,
162
+ 1.1557731628417969
163
+ ]
164
+ }
inference.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Load the matched DM-VAE / LightningDiT inference tensors and generate images."""
2
+ import argparse
3
+ from contextlib import nullcontext
4
+ import hashlib
5
+ import json
6
+ from pathlib import Path
7
+
8
+ import torch
9
+ from safetensors.torch import load_file
10
+
11
+
12
+ def digest(path):
13
+ h = hashlib.sha256()
14
+ with Path(path).open('rb') as f:
15
+ for block in iter(lambda: f.read(8 * 1024 * 1024), b''):
16
+ h.update(block)
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
24
+ from generation.model_d64 import LatentDiT64
25
+
26
+ if variant not in ('d32', 'd64'):
27
+ raise ValueError('variant must be d32 or d64')
28
+ root = Path(directory)
29
+ config = json.loads((root / 'configs' / (variant + '.json')).read_text())
30
+ loaded = {}
31
+ for component in ('vae', 'dit'):
32
+ spec = config[component]
33
+ path = root / spec['file']
34
+ if digest(path) != spec['sha256']:
35
+ raise ValueError(component + ' SHA-256 mismatch')
36
+ loaded[component] = load_file(str(path), device='cpu')
37
+ with torch.device('meta'):
38
+ vae = DMVAE(**config['vae']['architecture'])
39
+ dit_cls = LatentDiT64 if variant == 'd64' else LatentDiT
40
+ dit = dit_cls(**config['dit']['architecture'])
41
+ vae.load_state_dict(loaded.pop('vae'), strict=True, assign=True)
42
+ dit.load_state_dict(loaded.pop('dit'), strict=True, assign=True)
43
+ vae = vae.eval().requires_grad_(False).to(device=device, dtype=dtype)
44
+ dit = dit.eval().requires_grad_(False).to(device=device, dtype=dtype)
45
+ means = torch.tensor(config['channel_means'], device=device, dtype=dtype).view(1, -1, 1, 1)
46
+ scales = torch.tensor(config['channel_std'], dtype=torch.float64).reciprocal().to(device=device, dtype=dtype).view(1, -1, 1, 1)
47
+ return vae, dit, means, scales
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 '') + '400ep-ig' + ('-cfg' if cfg 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)
64
+ images = vae.decode(latent / scales + means)
65
+ if not torch.isfinite(images).all():
66
+ raise FloatingPointError('Non-finite generated image')
67
+ return images.float().clamp(-1, 1)
68
+
69
+
70
+ def main():
71
+ from PIL import Image
72
+
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')
79
+ p.add_argument('--device', default='cuda' if torch.cuda.is_available() else 'cpu')
80
+ p.add_argument('--precision', choices=('fp32', 'bf16'), default='fp32')
81
+ p.add_argument('--cfg', action='store_true')
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()
89
+ Image.fromarray(rgb).save(args.output / f'{args.variant}-{index:04d}.png')
90
+
91
+
92
+ if __name__ == '__main__':
93
+ main()
lightningdit/lightningdit-f16d32-400ep.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fd109b8e44feb12e4b5132e935f86922d232e661ba27d51355f13d9a89e1116c
3
+ size 2706627304
lightningdit/lightningdit-f16d64-400ep.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:cd40ead370ea6ce3f705a5daac80b6ccac3b1cec17914b1d0cb54fab7f94ad56
3
+ size 2706996136
vae/dmvae-f16d32-50ep.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:03f037b19412efb8406034a45a72fd9e7a70dcbb4f0de19c0a9fab67cc64a722
3
+ size 308960920
vae/dmvae-f16d64-50ep.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f66e53a0409abfaa8150d1eccae204d193a33c897d89c8995d7f63b50aee2a03
3
+ size 310955368