BiliSakura commited on
Commit
f5413aa
·
verified ·
1 Parent(s): 5df5042

Delete run_jit_diffusers_inference.py

Browse files
Files changed (1) hide show
  1. run_jit_diffusers_inference.py +0 -131
run_jit_diffusers_inference.py DELETED
@@ -1,131 +0,0 @@
1
- import argparse
2
- from pathlib import Path
3
- import sys
4
-
5
- import torch
6
-
7
- SCRIPT_DIR = Path(__file__).resolve().parent
8
- if str(SCRIPT_DIR) not in sys.path:
9
- sys.path.insert(0, str(SCRIPT_DIR))
10
-
11
- from jit_diffusers import JiTPipeline
12
-
13
-
14
- RECOMMENDED_CFG_BY_MODEL = {
15
- "JiT-B/16": 3.0,
16
- "JiT-L/16": 2.4,
17
- "JiT-H/16": 2.2,
18
- "JiT-B/32": 3.0,
19
- "JiT-L/32": 2.5,
20
- "JiT-H/32": 2.3,
21
- }
22
-
23
- RECOMMENDED_NOISE_BY_RESOLUTION = {
24
- 256: 1.0,
25
- 512: 2.0,
26
- }
27
-
28
-
29
- def parse_args() -> argparse.Namespace:
30
- parser = argparse.ArgumentParser(description="Run single-image JiT diffusers inference.")
31
- parser.add_argument("--model_path", type=str, required=True, help="Path to converted diffusers model directory.")
32
- parser.add_argument("--output_path", type=str, required=True, help="Path to save output PNG image.")
33
- parser.add_argument("--class_label", type=int, default=207, help="ImageNet class id for conditional generation.")
34
- parser.add_argument("--seed", type=int, default=42, help="Random seed.")
35
- parser.add_argument("--steps", type=int, default=50, help="Number of ODE sampling steps.")
36
- parser.add_argument(
37
- "--cfg",
38
- type=float,
39
- default=None,
40
- help="Classifier-free guidance scale. Defaults to paper recommendation for the loaded model.",
41
- )
42
- parser.add_argument("--interval_min", type=float, default=0.1, help="CFG interval min.")
43
- parser.add_argument("--interval_max", type=float, default=1.0, help="CFG interval max.")
44
- parser.add_argument(
45
- "--noise_scale",
46
- type=float,
47
- default=None,
48
- help="Initial Gaussian noise scale. Defaults to paper recommendation for the loaded resolution.",
49
- )
50
- parser.add_argument("--t_eps", type=float, default=5e-2, help="Small epsilon for timestep denominator.")
51
- parser.add_argument(
52
- "--device",
53
- type=str,
54
- default="auto",
55
- choices=["auto", "cuda", "cpu"],
56
- help="Inference device.",
57
- )
58
- parser.add_argument(
59
- "--dtype",
60
- type=str,
61
- default="bf16",
62
- choices=["bf16", "fp32"],
63
- help="Inference dtype. Defaults to bf16 on CUDA.",
64
- )
65
- parser.add_argument(
66
- "--solver",
67
- type=str,
68
- default="scheduler",
69
- choices=["scheduler", "heun", "euler"],
70
- help="Sampling solver. Use scheduler to keep pipeline default.",
71
- )
72
- return parser.parse_args()
73
-
74
-
75
- def resolve_device(name: str) -> torch.device:
76
- if name == "auto":
77
- return torch.device("cuda" if torch.cuda.is_available() else "cpu")
78
- return torch.device(name)
79
-
80
-
81
- def resolve_dtype(name: str, device: torch.device) -> torch.dtype:
82
- if name == "bf16":
83
- return torch.bfloat16 if device.type == "cuda" else torch.float32
84
- return torch.float32
85
-
86
-
87
- def resolve_generation_defaults(pipe: JiTPipeline, cfg: float | None, noise_scale: float | None) -> tuple[float, float]:
88
- model_type = str(getattr(pipe.transformer.config, "model_type", ""))
89
- sample_size = int(getattr(pipe.transformer.config, "sample_size", 256))
90
- resolved_cfg = cfg if cfg is not None else RECOMMENDED_CFG_BY_MODEL.get(model_type, 2.9)
91
- resolved_noise_scale = noise_scale if noise_scale is not None else RECOMMENDED_NOISE_BY_RESOLUTION.get(sample_size, 1.0)
92
- return resolved_cfg, resolved_noise_scale
93
-
94
-
95
- def main() -> None:
96
- args = parse_args()
97
- device = resolve_device(args.device)
98
- dtype = resolve_dtype(args.dtype, device)
99
- if device.type == "cuda":
100
- torch.set_float32_matmul_precision("high")
101
-
102
- pipe = JiTPipeline.from_pretrained(args.model_path).to(device)
103
- pipe.transformer = pipe.transformer.to(device=device, dtype=dtype)
104
- pipe.transformer.eval()
105
- sampling_method = None if args.solver == "scheduler" else args.solver
106
- cfg, noise_scale = resolve_generation_defaults(pipe, args.cfg, args.noise_scale)
107
-
108
- generator = torch.Generator(device=device).manual_seed(args.seed)
109
- output = pipe(
110
- class_labels=[args.class_label],
111
- num_inference_steps=args.steps,
112
- guidance_scale=cfg,
113
- guidance_interval_min=args.interval_min,
114
- guidance_interval_max=args.interval_max,
115
- noise_scale=noise_scale,
116
- t_eps=args.t_eps,
117
- sampling_method=sampling_method,
118
- generator=generator,
119
- output_type="pil",
120
- )
121
- image = output.images[0]
122
-
123
- output_path = Path(args.output_path)
124
- output_path.parent.mkdir(parents=True, exist_ok=True)
125
- image.save(output_path)
126
- print(f"Used sampling hyperparameters: cfg={cfg}, noise_scale={noise_scale}")
127
- print(f"Saved image to: {output_path}")
128
-
129
-
130
- if __name__ == "__main__":
131
- main()