Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@ import torch
|
|
| 4 |
import os
|
| 5 |
from diffusers import FluxPipeline
|
| 6 |
from torchao.quantization import quantize_, Float8DynamicActivationFloat8WeightConfig
|
| 7 |
-
import numpy as np
|
| 8 |
from PIL import Image
|
| 9 |
import time
|
| 10 |
import gc
|
|
@@ -37,7 +36,7 @@ class OptimizedFluxPipeline:
|
|
| 37 |
|
| 38 |
print(f"Loading {MODEL_ID} on {DEVICE} with {DTYPE}...")
|
| 39 |
|
| 40 |
-
# Base pipeline loading
|
| 41 |
self.pipe = FluxPipeline.from_pretrained(
|
| 42 |
MODEL_ID,
|
| 43 |
torch_dtype=DTYPE,
|
|
@@ -53,13 +52,12 @@ class OptimizedFluxPipeline:
|
|
| 53 |
try:
|
| 54 |
print("Applying FP8 quantization for H200 acceleration...")
|
| 55 |
# Quantize transformer component (heaviest part)
|
| 56 |
-
from torchao.quantization import quantize_
|
| 57 |
quantize_(self.pipe.transformer, QUANTIZATION_CONFIG())
|
| 58 |
print("✅ Quantization applied successfully")
|
| 59 |
except Exception as e:
|
| 60 |
print(f"⚠️ Quantization failed: {e}. Proceeding without quantization.")
|
| 61 |
|
| 62 |
-
# Apply AOTI compilation if enabled
|
| 63 |
if USE_AOTI and DEVICE == "cuda":
|
| 64 |
try:
|
| 65 |
self._apply_aot_compilation()
|
|
@@ -73,29 +71,16 @@ class OptimizedFluxPipeline:
|
|
| 73 |
"""Apply Ahead-of-Time compilation for faster inference"""
|
| 74 |
print("Applying AOTI compilation for 1.3-1.8x speedup...")
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
config.fallback_random = True # Better compatibility
|
| 79 |
-
|
| 80 |
-
# Warmup run for compilation
|
| 81 |
-
with torch.no_grad():
|
| 82 |
-
# Create dummy input for compilation
|
| 83 |
-
dummy_prompt = "warmup"
|
| 84 |
-
_ = self.pipe(
|
| 85 |
-
dummy_prompt,
|
| 86 |
-
num_inference_steps=1,
|
| 87 |
-
height=256,
|
| 88 |
-
width=256
|
| 89 |
-
)
|
| 90 |
-
|
| 91 |
-
# Compile the transformer
|
| 92 |
self.pipe.transformer = torch.compile(
|
| 93 |
self.pipe.transformer,
|
| 94 |
mode="reduce-overhead",
|
| 95 |
fullgraph=False
|
| 96 |
)
|
| 97 |
-
|
| 98 |
-
|
|
|
|
| 99 |
|
| 100 |
def generate(self, prompt, negative_prompt="", guidance_scale=3.5, num_inference_steps=28, seed=None, progress=None):
|
| 101 |
"""Generate image from prompt with interactive parameters"""
|
|
@@ -344,7 +329,4 @@ if __name__ == "__main__":
|
|
| 344 |
if torch.cuda.is_available():
|
| 345 |
print(f"CUDA device: {torch.cuda.get_device_name(0)}")
|
| 346 |
|
| 347 |
-
# Load model on startup (optional - comment out to load on first request)
|
| 348 |
-
# pipeline.load()
|
| 349 |
-
|
| 350 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 4 |
import os
|
| 5 |
from diffusers import FluxPipeline
|
| 6 |
from torchao.quantization import quantize_, Float8DynamicActivationFloat8WeightConfig
|
|
|
|
| 7 |
from PIL import Image
|
| 8 |
import time
|
| 9 |
import gc
|
|
|
|
| 36 |
|
| 37 |
print(f"Loading {MODEL_ID} on {DEVICE} with {DTYPE}...")
|
| 38 |
|
| 39 |
+
# Base pipeline loading
|
| 40 |
self.pipe = FluxPipeline.from_pretrained(
|
| 41 |
MODEL_ID,
|
| 42 |
torch_dtype=DTYPE,
|
|
|
|
| 52 |
try:
|
| 53 |
print("Applying FP8 quantization for H200 acceleration...")
|
| 54 |
# Quantize transformer component (heaviest part)
|
|
|
|
| 55 |
quantize_(self.pipe.transformer, QUANTIZATION_CONFIG())
|
| 56 |
print("✅ Quantization applied successfully")
|
| 57 |
except Exception as e:
|
| 58 |
print(f"⚠️ Quantization failed: {e}. Proceeding without quantization.")
|
| 59 |
|
| 60 |
+
# Apply AOTI compilation if enabled
|
| 61 |
if USE_AOTI and DEVICE == "cuda":
|
| 62 |
try:
|
| 63 |
self._apply_aot_compilation()
|
|
|
|
| 71 |
"""Apply Ahead-of-Time compilation for faster inference"""
|
| 72 |
print("Applying AOTI compilation for 1.3-1.8x speedup...")
|
| 73 |
|
| 74 |
+
try:
|
| 75 |
+
# Simple compilation approach
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
self.pipe.transformer = torch.compile(
|
| 77 |
self.pipe.transformer,
|
| 78 |
mode="reduce-overhead",
|
| 79 |
fullgraph=False
|
| 80 |
)
|
| 81 |
+
print("✅ AOTI compilation complete!")
|
| 82 |
+
except Exception as e:
|
| 83 |
+
print(f"⚠️ Compilation failed: {e}")
|
| 84 |
|
| 85 |
def generate(self, prompt, negative_prompt="", guidance_scale=3.5, num_inference_steps=28, seed=None, progress=None):
|
| 86 |
"""Generate image from prompt with interactive parameters"""
|
|
|
|
| 329 |
if torch.cuda.is_available():
|
| 330 |
print(f"CUDA device: {torch.cuda.get_device_name(0)}")
|
| 331 |
|
|
|
|
|
|
|
|
|
|
| 332 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|