Spaces:
Running on Zero
Running on Zero
File size: 12,575 Bytes
b701455 | 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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | import pytest
pytest.skip("moved to tests/e2e/test_core_functionalities.py (refactored)", allow_module_level=True)
# Add the project root to the Python path
project_root = Path(__file__).resolve().parent.parent
sys.path.append(str(project_root))
from src.user.pipeline import pipeline
def get_absolute_path(relative_path):
return os.path.join(project_root, relative_path)
def run_test(test_function, *args, **kwargs):
"""Decorator to time and print test information."""
print(f"\n--- Running test: {test_function.__name__} ---")
start_time = time.perf_counter()
try:
test_function(*args, **kwargs)
end_time = time.perf_counter()
print(f"--- Test {test_function.__name__} finished in {end_time - start_time:.2f} seconds ---")
except Exception as e:
end_time = time.perf_counter()
print(f"--- Test {test_function.__name__} failed after {end_time - start_time:.2f} seconds ---")
print(f"Error: {e}")
@pytest.mark.slow
def test_normal_pipeline():
"""Tests the default text-to-image pipeline."""
print("Testing normal pipeline with default settings...")
pipeline(
prompt="a beautiful landscape",
w=512,
h=512,
number=1,
batch=1,
)
@pytest.mark.slow
def test_samplers():
"""Tests all available samplers."""
samplers = ["euler", "euler_ancestral", "euler_cfgpp", "euler_ancestral_cfgpp", "dpmpp_2m_cfgpp", "dpmpp_sde_cfgpp"]
for sampler in samplers:
print(f"Testing sampler: {sampler}...")
pipeline(
prompt=f"a beautiful landscape using {sampler} sampler",
w=512,
h=512,
number=1,
batch=1,
sampler=sampler,
)
@pytest.mark.slow
def test_schedulers():
"""Tests all available schedulers."""
schedulers = ["normal", "karras", "simple", "beta", "ays", "ays_sd15", "ays_sdxl"]
for scheduler in schedulers:
print(f"Testing scheduler: {scheduler}...")
pipeline(
prompt=f"a beautiful landscape using {scheduler} scheduler",
w=512,
h=512,
number=1,
batch=1,
scheduler=scheduler,
)
@pytest.mark.slow
def test_optimizations():
"""Tests various optimizations."""
import time
def time_pipeline(description, **kwargs):
"""Helper to time a pipeline call."""
print(f"Testing {description}...")
start_time = time.perf_counter()
pipeline(
prompt="a beautiful landscape",
w=512,
h=512,
number=1,
batch=1,
**kwargs
)
end_time = time.perf_counter()
duration = end_time - start_time
print(f"{duration:.2f}")
return duration
# Baseline: no optimizations
baseline_time = time_pipeline("baseline (no optimizations)")
# Test Stable-Fast
stable_fast_time = time_pipeline("Stable-Fast optimization", stable_fast=True)
# Test multiscale diffusion
multiscale_time = time_pipeline("multiscale diffusion", enable_multiscale=True, multiscale_preset="performance")
# Test DeepCache
deepcache_time = time_pipeline("DeepCache", deepcache_enabled=True)
# Speed comparisons
print("\n--- Speed Comparison Results ---")
print(f"Baseline time: {baseline_time:.2f}s")
if stable_fast_time < baseline_time:
speedup = baseline_time / stable_fast_time
print(f"Stable-Fast: {stable_fast_time:.2f}s ({speedup:.2f}x speedup)")
else:
slowdown = stable_fast_time / baseline_time
print(f"Stable-Fast: {stable_fast_time:.2f}s ({slowdown:.2f}x slower)")
if multiscale_time < baseline_time:
speedup = baseline_time / multiscale_time
print(f"Multiscale: {multiscale_time:.2f}s ({speedup:.2f}x speedup)")
else:
slowdown = multiscale_time / baseline_time
print(f"Multiscale: {multiscale_time:.2f}s ({slowdown:.2f}x slower)")
if deepcache_time < baseline_time:
speedup = baseline_time / deepcache_time
print(f"DeepCache: {deepcache_time:.2f}s ({speedup:.2f}x speedup)")
else:
slowdown = deepcache_time / baseline_time
print(f"DeepCache: {deepcache_time:.2f}s ({slowdown:.2f}x slower)")
@pytest.mark.slow
def test_img2img():
"""Tests the img2img pipeline."""
print("Testing img2img pipeline...")
# Create a dummy image for testing
dummy_image_path = get_absolute_path("tests/dummy_image.png")
if not os.path.exists(dummy_image_path):
from PIL import Image
img = Image.new('RGB', (256, 256), color = 'red')
img.save(dummy_image_path)
pipeline(
prompt="a beautiful landscape",
w=512,
h=512,
number=1,
batch=1,
img2img=True,
img2img_image=dummy_image_path,
)
@pytest.mark.slow
def test_api_endpoints():
"""Tests the API endpoints."""
print("Testing API endpoints...")
server_process = None
try:
# Start the server
server_command = [sys.executable, get_absolute_path("server.py")]
server_process = subprocess.Popen(server_command)
print("Waiting for server to start...")
time.sleep(30) # Wait for the server to initialize
# Test health endpoint
print("Testing /health endpoint...")
response = requests.get("http://localhost:7861/health")
response.raise_for_status()
print("Health check passed.")
# Test generate endpoint
print("Testing /api/generate endpoint...")
payload = {
"prompt": "a beautiful landscape",
"width": 512,
"height": 512,
"steps": 1,
}
response = requests.post("http://localhost:7861/api/generate", json=payload)
response.raise_for_status()
print("Generate endpoint test passed.")
finally:
if server_process:
print("Shutting down server...")
server_process.terminate()
server_process.wait()
@pytest.mark.slow
def test_hires_fix():
"""Tests the hires_fix feature."""
print("Testing hires_fix...")
pipeline(
prompt="a beautiful landscape with hires_fix",
w=512,
h=512,
number=1,
batch=1,
hires_fix=True,
)
@pytest.mark.slow
def test_adetailer():
"""Tests the adetailer feature."""
print("Testing adetailer...")
pipeline(
prompt="a beautiful landscape with adetailer",
w=512,
h=512,
number=1,
batch=1,
adetailer=True,
)
@pytest.mark.slow
def test_enhance_prompt():
"""Tests the enhance_prompt feature."""
print("Testing enhance_prompt...")
pipeline(
prompt="a beautiful landscape with enhance_prompt",
w=512,
h=512,
number=1,
batch=1,
enhance_prompt=True,
)
@pytest.mark.slow
def test_reuse_seed():
"""Tests the reuse_seed feature."""
print("Testing reuse_seed...")
pipeline(
prompt="a beautiful landscape with reuse_seed",
w=512,
h=512,
number=1,
batch=1,
reuse_seed=True,
)
@pytest.mark.slow
def test_realistic_model():
"""Tests the realistic_model feature."""
print("Testing realistic_model...")
pipeline(
prompt="a beautiful landscape with realistic_model",
w=512,
h=512,
number=1,
batch=1,
realistic_model=True,
)
@pytest.mark.slow
def test_all_features():
"""Tests all features at once."""
print("Testing all features at once...")
pipeline(
prompt="a beautiful landscape with all features",
w=512,
h=512,
number=1,
batch=1,
hires_fix=True,
adetailer=True,
enhance_prompt=True,
reuse_seed=True,
realistic_model=True,
)
@pytest.mark.slow
def benchmark_optimizations():
"""Benchmarks optimizations with multiple runs for statistical analysis."""
import time
import statistics
def benchmark_pipeline(description, runs=3, **kwargs):
"""Benchmark a pipeline configuration with multiple runs."""
times = []
for i in range(runs):
print(f"Benchmarking {description} - run {i+1}/{runs}...")
start_time = time.perf_counter()
pipeline(
prompt="a beautiful landscape",
w=512,
h=512,
number=1,
batch=1,
**kwargs
)
end_time = time.perf_counter()
duration = end_time - start_time
times.append(duration)
print(f"{duration:.2f}")
avg_time = statistics.mean(times)
std_dev = statistics.stdev(times) if len(times) > 1 else 0
print(f"{avg_time:.2f}")
return avg_time, std_dev
print("=== Optimization Benchmark (3 runs each) ===")
# Baseline
baseline_avg, baseline_std = benchmark_pipeline("Baseline (no optimizations)")
# Optimizations
optimizations = [
("Stable-Fast", {"stable_fast": True}),
("Multiscale Performance", {"enable_multiscale": True, "multiscale_preset": "performance"}),
("DeepCache", {"deepcache_enabled": True}),
]
results = []
for name, kwargs in optimizations:
avg, std = benchmark_pipeline(name, **kwargs)
results.append((name, avg, std))
# Results summary
print("\n=== Benchmark Results Summary ===")
print(f"{'Optimization':<25} {'Avg Time (s)':<15} {'Std Dev':<10} {'Speedup':<10}")
print("-" * 60)
print(f"{'Baseline':<25} {baseline_avg:<15.2f} {baseline_std:<10.2f} {'1.00x':<10}")
for name, avg, std in results:
speedup = baseline_avg / avg
print(f"{name:<25} {avg:<15.2f} {std:<10.2f} {speedup:<10.2f}x")
print("\nNote: Lower time = better performance. Speedup > 1 means faster than baseline.")
def main():
parser = argparse.ArgumentParser(description="Run LightDiffusion-Next core functionality tests.")
parser.add_argument("--all", action="store_true", help="Run all tests.")
parser.add_argument("--normal", action="store_true", help="Run normal pipeline test.")
parser.add_argument("--samplers", action="store_true", help="Run samplers test.")
parser.add_argument("--schedulers", action="store_true", help="Run schedulers test.")
parser.add_argument("--optimizations", action="store_true", help="Run optimizations test.")
parser.add_argument("--img2img", action="store_true", help="Run img2img test.")
parser.add_argument("--api", action="store_true", help="Run API endpoints test.")
parser.add_argument("--hires_fix", action="store_true", help="Run hires_fix test.")
parser.add_argument("--adetailer", action="store_true", help="Run adetailer test.")
parser.add_argument("--enhance_prompt", action="store_true", help="Run enhance_prompt test.")
parser.add_argument("--reuse_seed", action="store_true", help="Run reuse_seed test.")
parser.add_argument("--realistic_model", action="store_true", help="Run realistic_model test.")
parser.add_argument("--all_features", action="store_true", help="Run all features test.")
parser.add_argument("--benchmark", action="store_true", help="Run optimization benchmark.")
args = parser.parse_args()
if args.all or args.normal:
run_test(test_normal_pipeline)
if args.all or args.samplers:
run_test(test_samplers)
if args.all or args.schedulers:
run_test(test_schedulers)
if args.all or args.optimizations:
run_test(test_optimizations)
if args.all or args.img2img:
run_test(test_img2img)
if args.all or args.api:
run_test(test_api_endpoints)
if args.all or args.hires_fix:
run_test(test_hires_fix)
if args.all or args.adetailer:
run_test(test_adetailer)
if args.all or args.enhance_prompt:
run_test(test_enhance_prompt)
if args.all or args.reuse_seed:
run_test(test_reuse_seed)
if args.all or args.realistic_model:
run_test(test_realistic_model)
if args.all or args.all_features:
run_test(test_all_features)
if args.benchmark:
run_test(benchmark_optimizations)
if not any(vars(args).values()):
print("No tests selected. Use --all to run all tests or select specific tests.")
if __name__ == "__main__":
main()
|