Spaces:
Runtime error
Runtime error
optimization
Browse files
app.py
CHANGED
|
@@ -20,8 +20,22 @@ print(f"GPU count: {torch.cuda.device_count()}")
|
|
| 20 |
if torch.cuda.is_available():
|
| 21 |
print(f"Current device: {torch.cuda.current_device()}")
|
| 22 |
print(f"Device name: {torch.cuda.get_device_name(0)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
print("="*50)
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 26 |
|
| 27 |
print(f"{datetime.datetime.now()} Загрузка модели FLUX.1-dev")
|
|
@@ -43,6 +57,8 @@ print(f"{datetime.datetime.now()} Загрузка LoRA успешно заве
|
|
| 43 |
|
| 44 |
pipe.fuse_lora(lora_scale=1.0)
|
| 45 |
pipe.to(device)
|
|
|
|
|
|
|
| 46 |
pipe.enable_model_cpu_offload() # Выгрузка неиспользуемых компонентов
|
| 47 |
|
| 48 |
# print(f"{datetime.datetime.now()} Загрузка модели stabilityai/stable-diffusion-x4-upscaler")
|
|
@@ -58,6 +74,8 @@ print(f"{datetime.datetime.now()} Загрузка модели briaai/RMBG-1.4"
|
|
| 58 |
bg_remover = pipeline("image-segmentation", "briaai/RMBG-1.4", trust_remote_code=True )
|
| 59 |
print(f"{datetime.datetime.now()} Загрузка модели briaai/RMBG-1.4 успешно завершена")
|
| 60 |
|
|
|
|
|
|
|
| 61 |
@spaces.GPU()
|
| 62 |
def generate_image(object_name, remove_bg=True):
|
| 63 |
try:
|
|
@@ -73,17 +91,18 @@ def generate_image(object_name, remove_bg=True):
|
|
| 73 |
steps = os.getenv('STEPS') if os.getenv('STEPS') is not None else 10
|
| 74 |
print(f"Шаги: {steps}")
|
| 75 |
|
|
|
|
|
|
|
| 76 |
image = pipe(
|
| 77 |
prompt,
|
| 78 |
-
height=
|
| 79 |
-
width=
|
| 80 |
-
guidance_scale=
|
| 81 |
num_inference_steps=int(steps),
|
| 82 |
-
generator=torch.Generator(device).manual_seed(42)
|
|
|
|
| 83 |
).images[0]
|
| 84 |
|
| 85 |
-
torch.cuda.empty_cache()
|
| 86 |
-
|
| 87 |
# if upscale :
|
| 88 |
# torch.cuda.empty_cache()
|
| 89 |
# upscaled_image = upscaler_pipeline(
|
|
@@ -95,11 +114,12 @@ def generate_image(object_name, remove_bg=True):
|
|
| 95 |
# return upscaled_image
|
| 96 |
|
| 97 |
if remove_bg :
|
|
|
|
| 98 |
remove_bg_image = bg_remover(image)
|
| 99 |
-
|
| 100 |
return remove_bg_image
|
| 101 |
|
| 102 |
-
|
| 103 |
return image
|
| 104 |
|
| 105 |
except Exception as e:
|
|
|
|
| 20 |
if torch.cuda.is_available():
|
| 21 |
print(f"Current device: {torch.cuda.current_device()}")
|
| 22 |
print(f"Device name: {torch.cuda.get_device_name(0)}")
|
| 23 |
+
# Настройка PyTorch для A100
|
| 24 |
+
torch.backends.cuda.enable_flash_sdp(True) # Включение Flash Attention
|
| 25 |
+
torch.backends.cuda.enable_mem_efficient_sdp(True) # Экономия памяти
|
| 26 |
+
torch.set_float32_matmul_precision('high') # Оптимизация матричных операций
|
| 27 |
print("="*50)
|
| 28 |
|
| 29 |
+
def clear_cuda():
|
| 30 |
+
if torch.cuda.is_available():
|
| 31 |
+
print(f"Используется VRAM: {torch.cuda.memory_allocated() / 1024 ** 3:.2f} GB")
|
| 32 |
+
print(f"Доступно VRAM: {torch.cuda.memory_reserved() / 1024 ** 3:.2f} GB")
|
| 33 |
+
print(f"Очистка кеша CUDA...")
|
| 34 |
+
torch.cuda.empty_cache()
|
| 35 |
+
print(f"Очистка кеша CUDA завершена.")
|
| 36 |
+
print(f"Используется VRAM: {torch.cuda.memory_allocated() / 1024 ** 3:.2f} GB")
|
| 37 |
+
print(f"Доступно VRAM: {torch.cuda.memory_reserved() / 1024 ** 3:.2f} GB")
|
| 38 |
+
|
| 39 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 40 |
|
| 41 |
print(f"{datetime.datetime.now()} Загрузка модели FLUX.1-dev")
|
|
|
|
| 57 |
|
| 58 |
pipe.fuse_lora(lora_scale=1.0)
|
| 59 |
pipe.to(device)
|
| 60 |
+
|
| 61 |
+
pipe.enable_xformers_memory_efficient_attention() # Ускорение внимания
|
| 62 |
pipe.enable_model_cpu_offload() # Выгрузка неиспользуемых компонентов
|
| 63 |
|
| 64 |
# print(f"{datetime.datetime.now()} Загрузка модели stabilityai/stable-diffusion-x4-upscaler")
|
|
|
|
| 74 |
bg_remover = pipeline("image-segmentation", "briaai/RMBG-1.4", trust_remote_code=True )
|
| 75 |
print(f"{datetime.datetime.now()} Загрузка модели briaai/RMBG-1.4 успешно завершена")
|
| 76 |
|
| 77 |
+
clear_cuda()
|
| 78 |
+
|
| 79 |
@spaces.GPU()
|
| 80 |
def generate_image(object_name, remove_bg=True):
|
| 81 |
try:
|
|
|
|
| 91 |
steps = os.getenv('STEPS') if os.getenv('STEPS') is not None else 10
|
| 92 |
print(f"Шаги: {steps}")
|
| 93 |
|
| 94 |
+
clear_cuda()
|
| 95 |
+
|
| 96 |
image = pipe(
|
| 97 |
prompt,
|
| 98 |
+
height=768,
|
| 99 |
+
width=768,
|
| 100 |
+
guidance_scale=4.0,
|
| 101 |
num_inference_steps=int(steps),
|
| 102 |
+
generator=torch.Generator(device).manual_seed(42),
|
| 103 |
+
num_images_per_prompt=1
|
| 104 |
).images[0]
|
| 105 |
|
|
|
|
|
|
|
| 106 |
# if upscale :
|
| 107 |
# torch.cuda.empty_cache()
|
| 108 |
# upscaled_image = upscaler_pipeline(
|
|
|
|
| 114 |
# return upscaled_image
|
| 115 |
|
| 116 |
if remove_bg :
|
| 117 |
+
clear_cuda()
|
| 118 |
remove_bg_image = bg_remover(image)
|
| 119 |
+
clear_cuda()
|
| 120 |
return remove_bg_image
|
| 121 |
|
| 122 |
+
clear_cuda()
|
| 123 |
return image
|
| 124 |
|
| 125 |
except Exception as e:
|