-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpixart.py
More file actions
227 lines (198 loc) · 7.91 KB
/
Copy pathpixart.py
File metadata and controls
227 lines (198 loc) · 7.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
"""Strict local PixArt-Sigma generation and ROCm benchmarking."""
from __future__ import annotations
from functools import lru_cache
import json
from pathlib import Path
import subprocess
import sys
import threading
from time import perf_counter
from typing import Any
MODEL_ID = "PixArt-alpha/PixArt-Sigma-XL-2-1024-MS"
MODEL_CARD_PARAMETER_COUNT = 600_000_000
MODEL_LICENSE = "CreativeML Open RAIL++-M"
class RocmUnavailableError(RuntimeError):
"""Raised instead of silently falling back to CPU or an unsupported GPU runtime."""
def _torch() -> Any:
try:
import torch
except ImportError as exc:
raise RocmUnavailableError(
"PyTorch is not installed. Install a ROCm PyTorch wheel on a supported AMD/Linux host first."
) from exc
return torch
def runtime_info(*, require_rocm: bool = False) -> dict[str, Any]:
try:
torch = _torch()
except RocmUnavailableError as exc:
if require_rocm:
raise
return {"ready": False, "reason": str(exc)}
gpu_available = bool(torch.cuda.is_available())
hip_version = getattr(torch.version, "hip", None)
is_rocm = gpu_available and bool(hip_version)
info: dict[str, Any] = {
"ready": is_rocm,
"rocm": is_rocm,
"torch_version": torch.__version__,
"rocm_version": hip_version,
"cuda_version": getattr(torch.version, "cuda", None),
"gpu_available": gpu_available,
"device_count": torch.cuda.device_count() if gpu_available else 0,
}
if gpu_available:
properties = torch.cuda.get_device_properties(0)
info.update(
{
"device": torch.cuda.get_device_name(0),
"total_vram_mib": round(properties.total_memory / (1024**2), 2),
}
)
if not info["ready"]:
info["reason"] = "PixArt generation requires an AMD ROCm PyTorch runtime with a detected ROCm/HIP device."
if require_rocm and not info["ready"]:
raise RocmUnavailableError(info["reason"])
return info
def _disable_broken_apex_t5_layernorm() -> None:
"""Block Apex's T5 LayerNorm patch before transformers can apply it.
The Radeon Cloud image preinstalls an Apex build whose FusedRMSNorm assumes
float32 input. transformers auto-swaps T5's LayerNorm for that fused kernel
whenever apex is importable, which is what previously crashed PixArt's T5 text
encoder in float16 with "expected scalar type Float but found Half" (see
RADEON_CLOUD_TROUBLESHOOTING.md). Poisoning sys.modules makes any `import apex...`
raise ImportError so transformers falls back to its native, dtype-safe LayerNorm,
letting the pipeline run in the float16 the PixArt-Sigma model card specifies
instead of the slower/heavier float32 fallback.
"""
for name in [mod for mod in sys.modules if mod == "apex" or mod.startswith("apex.")]:
del sys.modules[name]
sys.modules["apex"] = None # type: ignore[assignment]
_GPU_LOCK = threading.Lock() # Serializes generation; concurrent jobs on one GPU risk OOM/thrash.
@lru_cache(maxsize=2)
def _load_pipeline(model_id: str, compile_transformer: bool) -> Any:
torch = _torch()
runtime_info(require_rocm=True)
_disable_broken_apex_t5_layernorm()
try:
from diffusers import PixArtSigmaPipeline
except ImportError as exc:
raise RocmUnavailableError(
"diffusers is not installed. Install requirements after the ROCm PyTorch wheel."
) from exc
# Every request generates at a fixed 1024x1024 shape, so MIOpen can safely
# autotune conv/attention algorithms once and reuse them for the rest of the process.
torch.backends.cudnn.benchmark = True
pipeline = PixArtSigmaPipeline.from_pretrained(
model_id,
torch_dtype=torch.float16,
use_safetensors=True,
)
pipeline.to("cuda") # PyTorch addresses ROCm/HIP GPUs through this same device string.
pipeline.vae.enable_slicing() # PixArtSigmaPipeline has no enable_vae_slicing() convenience wrapper.
pipeline.vae.to(memory_format=torch.channels_last) # VAE decode is conv-heavy; helps MIOpen throughput.
if compile_transformer:
pipeline.transformer = torch.compile(pipeline.transformer, mode="reduce-overhead", fullgraph=True)
return pipeline
def _parameter_counts(pipeline: Any) -> dict[str, int]:
counts: dict[str, int] = {}
for component_name in ("transformer", "text_encoder", "vae"):
component = getattr(pipeline, component_name, None)
if component is not None and hasattr(component, "parameters"):
counts[component_name] = sum(int(parameter.numel()) for parameter in component.parameters())
counts["loaded_total"] = sum(counts.values())
return counts
def _percentile(values: list[float], percentile: float) -> float:
ordered = sorted(values)
if len(ordered) == 1:
return ordered[0]
position = (len(ordered) - 1) * percentile
lower = int(position)
upper = min(lower + 1, len(ordered) - 1)
weight = position - lower
return ordered[lower] * (1.0 - weight) + ordered[upper] * weight
def _run_pipeline(
pipeline: Any,
*,
prompt: str,
height: int,
width: int,
inference_steps: int,
guidance_scale: float,
seed: int,
device: str = "cuda",
) -> Any:
torch = _torch()
generator = torch.Generator(device=device).manual_seed(seed)
with torch.inference_mode():
return pipeline(
prompt=prompt,
height=height,
width=width,
num_inference_steps=inference_steps,
guidance_scale=guidance_scale,
generator=generator,
).images[0]
def generate_hero_image(
*,
prompt: str,
output_path: Path,
seed: int,
height: int = 1024,
width: int = 1024,
inference_steps: int = 20,
guidance_scale: float = 4.5,
compile_transformer: bool = False,
) -> dict[str, Any]:
if height % 8 or width % 8:
raise ValueError("PixArt image width and height must be multiples of 8.")
pipeline = _load_pipeline(MODEL_ID, compile_transformer)
torch = _torch()
with _GPU_LOCK:
torch.cuda.synchronize()
started = perf_counter()
image = _run_pipeline(
pipeline,
prompt=prompt,
height=height,
width=width,
inference_steps=inference_steps,
guidance_scale=guidance_scale,
seed=seed,
)
torch.cuda.synchronize()
elapsed_ms = (perf_counter() - started) * 1000.0
output_path.parent.mkdir(parents=True, exist_ok=True)
image.save(output_path)
return {
"model_id": MODEL_ID,
"model_card_parameter_count": MODEL_CARD_PARAMETER_COUNT,
"license": MODEL_LICENSE,
"runtime": runtime_info(require_rocm=True),
"parameter_counts": _parameter_counts(pipeline),
"image_path": str(output_path),
"width": width,
"height": height,
"inference_steps": inference_steps,
"guidance_scale": guidance_scale,
"seed": seed,
"latency_ms": round(elapsed_ms, 2),
"compiled_transformer": compile_transformer,
"image_label": "AI-generated representative image; not a factual depiction of the venue or event.",
}
def _rocm_smi_snapshot() -> dict[str, Any]:
try:
completed = subprocess.run(
["rocm-smi", "--showproductname", "--showmeminfo", "vram", "--json"],
check=False,
capture_output=True,
text=True,
timeout=10,
)
except (FileNotFoundError, subprocess.TimeoutExpired) as exc:
return {"available": False, "reason": str(exc)}
if completed.returncode != 0:
return {"available": False, "reason": completed.stderr.strip() or "rocm-smi failed"}
try:
return {"available": True, "data": json.loads(completed.stdout)}
except json.JSONDecodeError:
return {"available": True, "raw": completed.stdout.strip()}