Spaces:
Running
on
Zero
Running
on
Zero
Commit
Β·
d3c4f78
1
Parent(s):
dfcfa0d
yes
Browse files- __pycache__/two_stream_shunt_adapter.cpython-310.pyc +0 -0
- app.py +107 -104
__pycache__/two_stream_shunt_adapter.cpython-310.pyc
CHANGED
Binary files a/__pycache__/two_stream_shunt_adapter.cpython-310.pyc and b/__pycache__/two_stream_shunt_adapter.cpython-310.pyc differ
|
|
app.py
CHANGED
@@ -131,123 +131,126 @@ def encode_sdxl_prompt(prompt, negative_prompt=""):
|
|
131 |
|
132 |
# βββ Inference ββββββββββββββββββββββββββββββββββββββββββββ
|
133 |
|
134 |
-
|
135 |
-
@torch.no_grad()
|
136 |
@spaces.GPU
|
137 |
-
def infer(
|
138 |
-
|
139 |
-
|
140 |
-
|
|
|
|
|
|
|
|
|
|
|
141 |
global t5_tok, t5_mod, pipe
|
142 |
device = torch.device("cuda")
|
143 |
dtype = torch.float16
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
t5_tok
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
pipe
|
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 |
-
clip_l_mod
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
clip_g_mod
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
return (
|
242 |
image,
|
243 |
plot_heat(delta_l_final.squeeze().cpu().numpy(), "Ξ CLIP-L"),
|
244 |
-
plot_heat(gate_l_scaled.squeeze().cpu().numpy(), "Gate CLIP-L"),
|
245 |
plot_heat(delta_g_final.squeeze().cpu().numpy(), "Ξ CLIP-G"),
|
246 |
plot_heat(gate_g_scaled.squeeze().cpu().numpy(), "Gate CLIP-G"),
|
247 |
f"g_pred_l: {g_pred_l.mean().item():.3f}, Ο_l: {tau_l.mean().item():.3f}",
|
248 |
f"g_pred_g: {g_pred_g.mean().item():.3f}, Ο_g: {tau_g.mean().item():.3f}"
|
249 |
)
|
250 |
|
|
|
251 |
# βββ Gradio Interface βββββββββββββββββββββββββββββββββββββββββ
|
252 |
with gr.Blocks(title="SDXL Dual Shunt Adapter", theme=gr.themes.Soft()) as demo:
|
253 |
gr.Markdown("# π§ SDXL Dual Shunt Adapter β’ T5βCLIP Enhancement")
|
|
|
131 |
|
132 |
# βββ Inference ββββββββββββββββββββββββββββββββββββββββββββ
|
133 |
|
|
|
|
|
134 |
@spaces.GPU
|
135 |
+
def infer(
|
136 |
+
prompt, negative_prompt, adapter_l_file, adapter_g_file,
|
137 |
+
strength, noise, gate_prob, use_anchor,
|
138 |
+
steps, cfg_scale, scheduler_name,
|
139 |
+
width, height, seed
|
140 |
+
):
|
141 |
+
import torch
|
142 |
+
import numpy as np
|
143 |
+
|
144 |
global t5_tok, t5_mod, pipe
|
145 |
device = torch.device("cuda")
|
146 |
dtype = torch.float16
|
147 |
+
|
148 |
+
with torch.no_grad():
|
149 |
+
# Initialize tokenizer and model
|
150 |
+
if t5_tok is None:
|
151 |
+
t5_tok = T5Tokenizer.from_pretrained("google/flan-t5-base")
|
152 |
+
t5_mod = T5EncoderModel.from_pretrained("google/flan-t5-base").to(device).eval()
|
153 |
+
|
154 |
+
if pipe is None:
|
155 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
156 |
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
157 |
+
torch_dtype=dtype,
|
158 |
+
variant="fp16",
|
159 |
+
use_safetensors=True
|
160 |
+
).to(device)
|
161 |
+
|
162 |
+
# Reproducibility
|
163 |
+
if seed != -1:
|
164 |
+
torch.manual_seed(seed)
|
165 |
+
np.random.seed(seed)
|
166 |
+
|
167 |
+
# Scheduler
|
168 |
+
if scheduler_name in SCHEDULERS:
|
169 |
+
pipe.scheduler = SCHEDULERS[scheduler_name].from_config(pipe.scheduler.config)
|
170 |
+
|
171 |
+
# T5 embeddings
|
172 |
+
t5_ids = t5_tok(
|
173 |
+
prompt, return_tensors="pt",
|
174 |
+
padding="max_length", max_length=77, truncation=True
|
175 |
+
).input_ids.to(device)
|
176 |
+
t5_seq = t5_mod(t5_ids).last_hidden_state
|
177 |
+
|
178 |
+
# CLIP embeddings
|
179 |
+
clip_embeds = encode_sdxl_prompt(prompt, negative_prompt)
|
180 |
+
|
181 |
+
# Debug shapes
|
182 |
+
print(f"T5 seq shape: {t5_seq.shape}")
|
183 |
+
print(f"CLIP-L shape: {clip_embeds['clip_l'].shape}")
|
184 |
+
print(f"CLIP-G shape: {clip_embeds['clip_g'].shape}")
|
185 |
+
|
186 |
+
# Load adapters
|
187 |
+
adapter_l = load_adapter(repo_l, adapter_l_file, config_l).to(device) if adapter_l_file else None
|
188 |
+
adapter_g = load_adapter(repo_g, adapter_g_file, config_g).to(device) if adapter_g_file else None
|
189 |
+
|
190 |
+
# ---- Adapter L ----
|
191 |
+
if adapter_l:
|
192 |
+
anchor_l, delta_l, log_sigma_l, attn_l1, attn_l2, tau_l, g_pred_l, gate_l = adapter_l(t5_seq, clip_embeds["clip_l"])
|
193 |
+
gate_l_scaled = gate_l * gate_prob
|
194 |
+
delta_l_final = delta_l * strength * gate_l_scaled
|
195 |
+
clip_l_mod = clip_embeds["clip_l"] + delta_l_final
|
196 |
+
if use_anchor:
|
197 |
+
clip_l_mod = clip_l_mod * (1 - gate_l_scaled) + anchor_l * gate_l_scaled
|
198 |
+
if noise > 0:
|
199 |
+
clip_l_mod += torch.randn_like(clip_l_mod) * noise
|
200 |
+
else:
|
201 |
+
clip_l_mod = clip_embeds["clip_l"]
|
202 |
+
delta_l_final = torch.zeros_like(clip_l_mod)
|
203 |
+
gate_l_scaled = torch.zeros_like(clip_l_mod)
|
204 |
+
g_pred_l = torch.tensor(0.0)
|
205 |
+
tau_l = torch.tensor(0.0)
|
206 |
+
|
207 |
+
# ---- Adapter G ----
|
208 |
+
if adapter_g:
|
209 |
+
anchor_g, delta_g, log_sigma_g, attn_g1, attn_g2, tau_g, g_pred_g, gate_g = adapter_g(t5_seq, clip_embeds["clip_g"])
|
210 |
+
gate_g_scaled = gate_g * gate_prob
|
211 |
+
delta_g_final = delta_g * strength * gate_g_scaled
|
212 |
+
clip_g_mod = clip_embeds["clip_g"] + delta_g_final
|
213 |
+
if use_anchor:
|
214 |
+
clip_g_mod = clip_g_mod * (1 - gate_g_scaled) + anchor_g * gate_g_scaled
|
215 |
+
if noise > 0:
|
216 |
+
clip_g_mod += torch.randn_like(clip_g_mod) * noise
|
217 |
+
else:
|
218 |
+
clip_g_mod = clip_embeds["clip_g"]
|
219 |
+
delta_g_final = torch.zeros_like(clip_g_mod)
|
220 |
+
gate_g_scaled = torch.zeros_like(clip_g_mod)
|
221 |
+
g_pred_g = torch.tensor(0.0)
|
222 |
+
tau_g = torch.tensor(0.0)
|
223 |
+
|
224 |
+
# ---- Combine embeddings ----
|
225 |
+
prompt_embeds = torch.cat([clip_l_mod, clip_g_mod], dim=-1).to(dtype)
|
226 |
+
neg_embeds = torch.cat([clip_embeds["neg_clip_l"], clip_embeds["neg_clip_g"]], dim=-1).to(dtype)
|
227 |
+
|
228 |
+
# ---- Generate image ----
|
229 |
+
generator = torch.Generator(device=device).manual_seed(seed) if seed != -1 else None
|
230 |
+
image = pipe(
|
231 |
+
prompt_embeds=prompt_embeds,
|
232 |
+
pooled_prompt_embeds=clip_embeds["pooled"],
|
233 |
+
negative_prompt_embeds=neg_embeds,
|
234 |
+
negative_pooled_prompt_embeds=clip_embeds["neg_pooled"],
|
235 |
+
num_inference_steps=steps,
|
236 |
+
guidance_scale=cfg_scale,
|
237 |
+
width=width,
|
238 |
+
height=height,
|
239 |
+
num_images_per_prompt=1,
|
240 |
+
generator=generator,
|
241 |
+
).images[0]
|
242 |
+
|
|
|
243 |
return (
|
244 |
image,
|
245 |
plot_heat(delta_l_final.squeeze().cpu().numpy(), "Ξ CLIP-L"),
|
246 |
+
plot_heat(gate_l_scaled.squeeze().cpu().numpy(), "Gate CLIP-L"),
|
247 |
plot_heat(delta_g_final.squeeze().cpu().numpy(), "Ξ CLIP-G"),
|
248 |
plot_heat(gate_g_scaled.squeeze().cpu().numpy(), "Gate CLIP-G"),
|
249 |
f"g_pred_l: {g_pred_l.mean().item():.3f}, Ο_l: {tau_l.mean().item():.3f}",
|
250 |
f"g_pred_g: {g_pred_g.mean().item():.3f}, Ο_g: {tau_g.mean().item():.3f}"
|
251 |
)
|
252 |
|
253 |
+
|
254 |
# βββ Gradio Interface βββββββββββββββββββββββββββββββββββββββββ
|
255 |
with gr.Blocks(title="SDXL Dual Shunt Adapter", theme=gr.themes.Soft()) as demo:
|
256 |
gr.Markdown("# π§ SDXL Dual Shunt Adapter β’ T5βCLIP Enhancement")
|