Spaces:
Running on Zero
Running on Zero
Fix CUBLAS errors: enforce dtype consistency at model entry points
Browse files- Add dtype conversion at Flux model forward() entry point
- Add dtype conversion in LastLayer to match weight dtype
- Remove torch.compile (not compatible with ZeroGPU)
- This ensures all inputs match model weights dtype regardless of ZeroGPU auto-conversion
- app.py +1 -0
- src/flux/model.py +24 -0
- src/flux/modules/layers.py +11 -0
app.py
CHANGED
|
@@ -148,6 +148,7 @@ def init_generator():
|
|
| 148 |
use_4bit_quantization=False, # Full precision model
|
| 149 |
dtype="fp32", # Use fp32 to avoid CUBLAS errors on ZeroGPU
|
| 150 |
)
|
|
|
|
| 151 |
return generator
|
| 152 |
|
| 153 |
|
|
|
|
| 148 |
use_4bit_quantization=False, # Full precision model
|
| 149 |
dtype="fp32", # Use fp32 to avoid CUBLAS errors on ZeroGPU
|
| 150 |
)
|
| 151 |
+
|
| 152 |
return generator
|
| 153 |
|
| 154 |
|
src/flux/model.py
CHANGED
|
@@ -199,6 +199,30 @@ class Flux(nn.Module):
|
|
| 199 |
if img.ndim != 3 or txt.ndim != 3:
|
| 200 |
raise ValueError("Input img and txt tensors must have 3 dimensions.")
|
| 201 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
# running on sequences img
|
| 203 |
img = self.img_in(img)
|
| 204 |
if self.module_embeddings is not None:
|
|
|
|
| 199 |
if img.ndim != 3 or txt.ndim != 3:
|
| 200 |
raise ValueError("Input img and txt tensors must have 3 dimensions.")
|
| 201 |
|
| 202 |
+
# Get model's weight dtype and ensure all inputs match
|
| 203 |
+
# This prevents CUBLAS errors on ZeroGPU where tensors may be auto-converted
|
| 204 |
+
weight_dtype = self.img_in.weight.dtype
|
| 205 |
+
if img.dtype != weight_dtype:
|
| 206 |
+
img = img.to(weight_dtype)
|
| 207 |
+
if img_ids.dtype != weight_dtype:
|
| 208 |
+
img_ids = img_ids.to(weight_dtype)
|
| 209 |
+
if txt.dtype != weight_dtype:
|
| 210 |
+
txt = txt.to(weight_dtype)
|
| 211 |
+
if txt_ids.dtype != weight_dtype:
|
| 212 |
+
txt_ids = txt_ids.to(weight_dtype)
|
| 213 |
+
if y.dtype != weight_dtype:
|
| 214 |
+
y = y.to(weight_dtype)
|
| 215 |
+
if timesteps.dtype != weight_dtype:
|
| 216 |
+
timesteps = timesteps.to(weight_dtype)
|
| 217 |
+
if timesteps2 is not None and timesteps2.dtype != weight_dtype:
|
| 218 |
+
timesteps2 = timesteps2.to(weight_dtype)
|
| 219 |
+
if cond_txt_latent is not None and cond_txt_latent.dtype != weight_dtype:
|
| 220 |
+
cond_txt_latent = cond_txt_latent.to(weight_dtype)
|
| 221 |
+
if guidance is not None and guidance.dtype != weight_dtype:
|
| 222 |
+
guidance = guidance.to(weight_dtype)
|
| 223 |
+
if image_proj is not None and image_proj.dtype != weight_dtype:
|
| 224 |
+
image_proj = image_proj.to(weight_dtype)
|
| 225 |
+
|
| 226 |
# running on sequences img
|
| 227 |
img = self.img_in(img)
|
| 228 |
if self.module_embeddings is not None:
|
src/flux/modules/layers.py
CHANGED
|
@@ -625,6 +625,17 @@ class LastLayer(nn.Module):
|
|
| 625 |
self.adaLN_modulation = nn.Sequential(nn.SiLU(), nn.Linear(hidden_size, 2 * hidden_size, bias=True))
|
| 626 |
|
| 627 |
def forward(self, x: Tensor, vec: Tensor, vec2: Tensor) -> Tensor:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 628 |
shift, scale = self.adaLN_modulation(vec).chunk(2, dim=1)
|
| 629 |
shift = shift[:, None, :]
|
| 630 |
scale = scale[:, None, :]
|
|
|
|
| 625 |
self.adaLN_modulation = nn.Sequential(nn.SiLU(), nn.Linear(hidden_size, 2 * hidden_size, bias=True))
|
| 626 |
|
| 627 |
def forward(self, x: Tensor, vec: Tensor, vec2: Tensor) -> Tensor:
|
| 628 |
+
# Get weights dtype (should be fp32 if loaded correctly)
|
| 629 |
+
weight_dtype = self.linear.weight.dtype
|
| 630 |
+
|
| 631 |
+
# Ensure inputs match weight dtype to avoid CUBLAS errors on ZeroGPU
|
| 632 |
+
if x.dtype != weight_dtype:
|
| 633 |
+
x = x.to(weight_dtype)
|
| 634 |
+
if vec.dtype != weight_dtype:
|
| 635 |
+
vec = vec.to(weight_dtype)
|
| 636 |
+
if vec2 is not None and vec2.dtype != weight_dtype:
|
| 637 |
+
vec2 = vec2.to(weight_dtype)
|
| 638 |
+
|
| 639 |
shift, scale = self.adaLN_modulation(vec).chunk(2, dim=1)
|
| 640 |
shift = shift[:, None, :]
|
| 641 |
scale = scale[:, None, :]
|