Spaces:
Running on Zero
Running on Zero
Build and download pipe models without GPU
Browse files- src/smc/inference.py +32 -16
src/smc/inference.py
CHANGED
|
@@ -22,7 +22,10 @@ from typing import List
|
|
| 22 |
MIN_GPU_DURATION = 60
|
| 23 |
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
def build_pipe(device):
|
| 28 |
model_path = "Collov-Labs/Monetico"
|
|
@@ -38,13 +41,8 @@ def build_pipe(device):
|
|
| 38 |
device=device,
|
| 39 |
)
|
| 40 |
pipe = Pipeline(vq_model, tokenizer=tokenizer, text_encoder=text_encoder, transformer=model, scheduler=scheduler_new)
|
| 41 |
-
pipe.to(device)
|
| 42 |
return pipe
|
| 43 |
|
| 44 |
-
def build_pipe_threadsafe(device):
|
| 45 |
-
with model_load_lock:
|
| 46 |
-
return build_pipe(device)
|
| 47 |
-
|
| 48 |
@dataclass
|
| 49 |
class InferenceOutput:
|
| 50 |
images: List[Image.Image]
|
|
@@ -61,16 +59,26 @@ class PretrainedInferenceConfig:
|
|
| 61 |
steps: int = 48
|
| 62 |
num_batches: int = 4
|
| 63 |
|
| 64 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
step_duration = 1.0
|
| 66 |
-
total_duration = math.ceil(step_duration * config.steps)
|
| 67 |
return max(total_duration, MIN_GPU_DURATION)
|
| 68 |
|
| 69 |
@spaces.GPU(duration=_get_pretrained_duration)
|
| 70 |
-
def
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
| 72 |
reward_bias = 5.0
|
| 73 |
-
|
|
|
|
| 74 |
image_reward_fn = lambda images: reward_fn(
|
| 75 |
images,
|
| 76 |
[config.prompt] * len(images)
|
|
@@ -142,18 +150,26 @@ def _get_batch_size_based_on_gpu_mem_smc_grad(device, phi):
|
|
| 142 |
batch_p = 1
|
| 143 |
return batch_p
|
| 144 |
|
| 145 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
step_duration = 5.0
|
| 147 |
-
total_duration = math.ceil(step_duration * config.steps)
|
| 148 |
return max(total_duration, MIN_GPU_DURATION)
|
| 149 |
|
| 150 |
@spaces.GPU(duration=_get_smc_grad_duration)
|
| 151 |
-
def
|
| 152 |
if isinstance(device, str):
|
| 153 |
device = torch.device(device)
|
| 154 |
-
|
|
|
|
| 155 |
reward_bias = 5.0
|
| 156 |
-
|
|
|
|
| 157 |
image_reward_fn = lambda images: reward_fn(
|
| 158 |
images,
|
| 159 |
[config.prompt] * len(images)
|
|
|
|
| 22 |
MIN_GPU_DURATION = 60
|
| 23 |
|
| 24 |
|
| 25 |
+
pipe_build_lock = threading.Lock()
|
| 26 |
+
pipe_load_lock = threading.Lock()
|
| 27 |
+
reward_model_load_lock = threading.Lock()
|
| 28 |
+
|
| 29 |
|
| 30 |
def build_pipe(device):
|
| 31 |
model_path = "Collov-Labs/Monetico"
|
|
|
|
| 41 |
device=device,
|
| 42 |
)
|
| 43 |
pipe = Pipeline(vq_model, tokenizer=tokenizer, text_encoder=text_encoder, transformer=model, scheduler=scheduler_new)
|
|
|
|
| 44 |
return pipe
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
@dataclass
|
| 47 |
class InferenceOutput:
|
| 48 |
images: List[Image.Image]
|
|
|
|
| 59 |
steps: int = 48
|
| 60 |
num_batches: int = 4
|
| 61 |
|
| 62 |
+
def infer_pretrained(config: PretrainedInferenceConfig, device='cpu'):
|
| 63 |
+
with pipe_build_lock:
|
| 64 |
+
pipe = build_pipe(device)
|
| 65 |
+
return infer_pretrained_with_pipe(config, pipe, device=device)
|
| 66 |
+
|
| 67 |
+
def _get_pretrained_duration(config: PretrainedInferenceConfig, pipe: Pipeline, device='cpu') -> int:
|
| 68 |
+
setup_duration = 30.0
|
| 69 |
step_duration = 1.0
|
| 70 |
+
total_duration = math.ceil(setup_duration + step_duration * config.steps)
|
| 71 |
return max(total_duration, MIN_GPU_DURATION)
|
| 72 |
|
| 73 |
@spaces.GPU(duration=_get_pretrained_duration)
|
| 74 |
+
def infer_pretrained_with_pipe(config: PretrainedInferenceConfig, pipe: Pipeline, device='cpu'):
|
| 75 |
+
if isinstance(device, str):
|
| 76 |
+
device = torch.device(device)
|
| 77 |
+
with pipe_load_lock:
|
| 78 |
+
pipe = pipe.to(device)
|
| 79 |
reward_bias = 5.0
|
| 80 |
+
with reward_model_load_lock:
|
| 81 |
+
reward_fn, reward_name = rewards.ImageReward_Fk_Steering(device=device, bias=reward_bias), "image_reward_plus_5"
|
| 82 |
image_reward_fn = lambda images: reward_fn(
|
| 83 |
images,
|
| 84 |
[config.prompt] * len(images)
|
|
|
|
| 150 |
batch_p = 1
|
| 151 |
return batch_p
|
| 152 |
|
| 153 |
+
def infer_smc_grad(config: SMCGradInferenceConfig, device='cpu'):
|
| 154 |
+
with pipe_build_lock:
|
| 155 |
+
pipe = build_pipe(device)
|
| 156 |
+
return infer_smc_grad_with_pipe(config, pipe, device=device)
|
| 157 |
+
|
| 158 |
+
def _get_smc_grad_duration(config: SMCGradInferenceConfig, pipe: Pipeline, device='cpu') -> int:
|
| 159 |
+
setup_duration = 30.0
|
| 160 |
step_duration = 5.0
|
| 161 |
+
total_duration = math.ceil(setup_duration + step_duration * config.steps)
|
| 162 |
return max(total_duration, MIN_GPU_DURATION)
|
| 163 |
|
| 164 |
@spaces.GPU(duration=_get_smc_grad_duration)
|
| 165 |
+
def infer_smc_grad_with_pipe(config: SMCGradInferenceConfig, pipe: Pipeline, device='cpu'):
|
| 166 |
if isinstance(device, str):
|
| 167 |
device = torch.device(device)
|
| 168 |
+
with pipe_load_lock:
|
| 169 |
+
pipe = pipe.to(device)
|
| 170 |
reward_bias = 5.0
|
| 171 |
+
with reward_model_load_lock:
|
| 172 |
+
reward_fn, reward_name = rewards.ImageReward_Fk_Steering(device=device, bias=reward_bias), "image_reward_plus_5"
|
| 173 |
image_reward_fn = lambda images: reward_fn(
|
| 174 |
images,
|
| 175 |
[config.prompt] * len(images)
|