Spaces:
Running
Running
last try, perplexity
Browse files- Dockerfile +12 -2
- Image-Morpher/main.py +14 -1
- memory_manager.py +19 -0
- requirements.txt +5 -2
Dockerfile
CHANGED
@@ -3,6 +3,9 @@ FROM nvidia/cuda:12.5.1-cudnn-devel-ubi9
|
|
3 |
|
4 |
# Set non-interactive mode
|
5 |
ENV DEBIAN_FRONTEND=noninteractive
|
|
|
|
|
|
|
6 |
|
7 |
# Update and install necessary system packages with microdnf
|
8 |
RUN microdnf update -y && \
|
@@ -33,8 +36,12 @@ RUN curl -L https://www.python.org/ftp/python/3.12.9/Python-3.12.9.tgz -o Python
|
|
33 |
# Upgrade pip to the latest version
|
34 |
RUN pip install --upgrade pip
|
35 |
|
36 |
-
# Install
|
37 |
-
RUN
|
|
|
|
|
|
|
|
|
38 |
|
39 |
# Set the working directory
|
40 |
WORKDIR /app
|
@@ -45,6 +52,9 @@ COPY requirements.txt .
|
|
45 |
# Install Python dependencies
|
46 |
RUN pip install -r requirements.txt
|
47 |
|
|
|
|
|
|
|
48 |
# Copy the rest of your repository into the container
|
49 |
COPY . .
|
50 |
|
|
|
3 |
|
4 |
# Set non-interactive mode
|
5 |
ENV DEBIAN_FRONTEND=noninteractive
|
6 |
+
# Add these environment variables early
|
7 |
+
ENV TF_FORCE_GPU_ALLOW_GROWTH=true
|
8 |
+
ENV MAX_MEMORY=0.95
|
9 |
|
10 |
# Update and install necessary system packages with microdnf
|
11 |
RUN microdnf update -y && \
|
|
|
36 |
# Upgrade pip to the latest version
|
37 |
RUN pip install --upgrade pip
|
38 |
|
39 |
+
# Install xformers dependencies
|
40 |
+
RUN microdnf install -y ninja-build && \
|
41 |
+
pip install -U xformers --index-url https://download.pytorch.org/whl/cu125
|
42 |
+
|
43 |
+
# Modify PyTorch installation
|
44 |
+
RUN pip install torch==2.3.0 torchvision==0.18.0 --index-url https://download.pytorch.org/whl/cu125
|
45 |
|
46 |
# Set the working directory
|
47 |
WORKDIR /app
|
|
|
52 |
# Install Python dependencies
|
53 |
RUN pip install -r requirements.txt
|
54 |
|
55 |
+
# Add memory management tools
|
56 |
+
RUN pip install nvidia-ml-py3 pynvml
|
57 |
+
|
58 |
# Copy the rest of your repository into the container
|
59 |
COPY . .
|
60 |
|
Image-Morpher/main.py
CHANGED
@@ -10,6 +10,7 @@ from model import DiffMorpherPipeline
|
|
10 |
import time
|
11 |
import logging
|
12 |
import gc
|
|
|
13 |
|
14 |
os.environ["HF_HOME"] = "/app/hf_cache"
|
15 |
os.environ["DIFFUSERS_CACHE"] = "/app/hf_cache"
|
@@ -117,7 +118,13 @@ pipeline = DiffMorpherPipeline.from_pretrained(args.model_path, torch_dtype=torc
|
|
117 |
pipeline.enable_vae_slicing()
|
118 |
pipeline.enable_attention_slicing()
|
119 |
|
120 |
-
pipeline.
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
|
122 |
# Add these AFTER device movement
|
123 |
torch.backends.cudnn.benchmark = True # finds efficient convolution algo by running short benchmark, minimal speed-up.
|
@@ -135,6 +142,12 @@ if args.use_lcm:
|
|
135 |
# set CFG (range allowed by legacy code: 0 to 1, 1 performs best)
|
136 |
args.guidance_scale = 1
|
137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
# Run the pipeline inference using existing parameters
|
139 |
images = pipeline(
|
140 |
img_path_0=args.image_path_0,
|
|
|
10 |
import time
|
11 |
import logging
|
12 |
import gc
|
13 |
+
from memory_manager import check_vram, limit_precision
|
14 |
|
15 |
os.environ["HF_HOME"] = "/app/hf_cache"
|
16 |
os.environ["DIFFUSERS_CACHE"] = "/app/hf_cache"
|
|
|
118 |
pipeline.enable_vae_slicing()
|
119 |
pipeline.enable_attention_slicing()
|
120 |
|
121 |
+
pipeline.enable_xformers_memory_efficient_attention()
|
122 |
+
|
123 |
+
# Replace manual cache clearing with automatic management
|
124 |
+
|
125 |
+
from accelerate import init_empty_weights
|
126 |
+
with init_empty_weights():
|
127 |
+
pipeline.to("cuda", dtype=torch.float32)
|
128 |
|
129 |
# Add these AFTER device movement
|
130 |
torch.backends.cudnn.benchmark = True # finds efficient convolution algo by running short benchmark, minimal speed-up.
|
|
|
142 |
# set CFG (range allowed by legacy code: 0 to 1, 1 performs best)
|
143 |
args.guidance_scale = 1
|
144 |
|
145 |
+
pipeline = limit_precision(pipeline)
|
146 |
+
|
147 |
+
# Add memory checks during execution
|
148 |
+
check_vram(0.85) # Check before critical operations
|
149 |
+
|
150 |
+
|
151 |
# Run the pipeline inference using existing parameters
|
152 |
images = pipeline(
|
153 |
img_path_0=args.image_path_0,
|
memory_manager.py
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import pynvml
|
3 |
+
import gc
|
4 |
+
|
5 |
+
def check_vram(threshold=0.9):
|
6 |
+
pynvml.nvmlInit()
|
7 |
+
handle = pynvml.nvmlDeviceGetHandleByIndex(0)
|
8 |
+
info = pynvml.nvmlDeviceGetMemoryInfo(handle)
|
9 |
+
used_percent = info.used / info.total
|
10 |
+
if used_percent > threshold:
|
11 |
+
torch.cuda.empty_cache()
|
12 |
+
gc.collect()
|
13 |
+
return used_percent
|
14 |
+
|
15 |
+
def limit_precision(model):
|
16 |
+
for param in model.parameters():
|
17 |
+
if param.dtype == torch.float32:
|
18 |
+
param.data = param.data.half()
|
19 |
+
return model
|
requirements.txt
CHANGED
@@ -10,10 +10,13 @@ Pillow==10.1.0
|
|
10 |
safetensors==0.4.0
|
11 |
tqdm==4.65.0
|
12 |
transformers==4.34.1
|
13 |
-
|
14 |
-
|
15 |
lpips
|
16 |
# peft
|
17 |
tensorflow==2.18.0
|
18 |
tensorflow_hub==0.16.1
|
19 |
opencv_python
|
|
|
|
|
|
|
|
10 |
safetensors==0.4.0
|
11 |
tqdm==4.65.0
|
12 |
transformers==4.34.1
|
13 |
+
torch
|
14 |
+
torchvision
|
15 |
lpips
|
16 |
# peft
|
17 |
tensorflow==2.18.0
|
18 |
tensorflow_hub==0.16.1
|
19 |
opencv_python
|
20 |
+
xformers
|
21 |
+
nvidia-ml-py3
|
22 |
+
pynvml
|