fix(near): defer renderer/tone-mapper init to first GPU ensure
Browse files- app: unify ensure_near/geometry with CPU preload helpers; fix generate_mesh; restore preload in _near_launch
- pipeline: comment out from_pretrained renderer+tone_mapper setup (initialized in ensure_near_on_cuda)
Made-with: Cursor
app.py
CHANGED
|
@@ -14,7 +14,6 @@ import os
|
|
| 14 |
import shutil
|
| 15 |
import sys
|
| 16 |
import threading
|
| 17 |
-
import time
|
| 18 |
from pathlib import Path
|
| 19 |
from typing import Any, Dict, Optional
|
| 20 |
|
|
@@ -143,23 +142,30 @@ def run_model_cpu_preload_blocking() -> None:
|
|
| 143 |
|
| 144 |
|
| 145 |
def ensure_near_on_cuda() -> None:
|
|
|
|
| 146 |
global _NEAR_ON_CUDA
|
| 147 |
with _MODEL_LOCK:
|
| 148 |
_ensure_near_loaded_on_cpu_locked()
|
|
|
|
| 149 |
if torch.cuda.is_available() and not _NEAR_ON_CUDA:
|
| 150 |
-
assert PIPELINE is not None
|
| 151 |
PIPELINE.to("cuda")
|
| 152 |
_NEAR_ON_CUDA = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
|
| 155 |
def ensure_geometry_on_cuda() -> None:
|
| 156 |
global _GEOMETRY_ON_CUDA
|
| 157 |
with _MODEL_LOCK:
|
| 158 |
_ensure_geometry_loaded_on_cpu_locked()
|
|
|
|
| 159 |
if torch.cuda.is_available() and not _GEOMETRY_ON_CUDA:
|
| 160 |
-
assert GEOMETRY_PIPELINE is not None
|
| 161 |
GEOMETRY_PIPELINE.to("cuda")
|
| 162 |
_GEOMETRY_ON_CUDA = True
|
|
|
|
| 163 |
|
| 164 |
|
| 165 |
def _try_release_cuda_memory() -> None:
|
|
|
|
| 14 |
import shutil
|
| 15 |
import sys
|
| 16 |
import threading
|
|
|
|
| 17 |
from pathlib import Path
|
| 18 |
from typing import Any, Dict, Optional
|
| 19 |
|
|
|
|
| 142 |
|
| 143 |
|
| 144 |
def ensure_near_on_cuda() -> None:
|
| 145 |
+
"""Load NeAR if needed (respects CPU preload), move to CUDA once, init renderer / tone mapper."""
|
| 146 |
global _NEAR_ON_CUDA
|
| 147 |
with _MODEL_LOCK:
|
| 148 |
_ensure_near_loaded_on_cpu_locked()
|
| 149 |
+
assert PIPELINE is not None
|
| 150 |
if torch.cuda.is_available() and not _NEAR_ON_CUDA:
|
|
|
|
| 151 |
PIPELINE.to("cuda")
|
| 152 |
_NEAR_ON_CUDA = True
|
| 153 |
+
if torch.cuda.is_available():
|
| 154 |
+
if PIPELINE.renderer is None:
|
| 155 |
+
PIPELINE.setup_renderer()
|
| 156 |
+
if PIPELINE.tone_mapper is None:
|
| 157 |
+
PIPELINE.setup_tone_mapper("AgX")
|
| 158 |
|
| 159 |
|
| 160 |
def ensure_geometry_on_cuda() -> None:
|
| 161 |
global _GEOMETRY_ON_CUDA
|
| 162 |
with _MODEL_LOCK:
|
| 163 |
_ensure_geometry_loaded_on_cpu_locked()
|
| 164 |
+
assert GEOMETRY_PIPELINE is not None
|
| 165 |
if torch.cuda.is_available() and not _GEOMETRY_ON_CUDA:
|
|
|
|
| 166 |
GEOMETRY_PIPELINE.to("cuda")
|
| 167 |
_GEOMETRY_ON_CUDA = True
|
| 168 |
+
print("[NeAR] Hunyuan geometry pipeline on CUDA.", flush=True)
|
| 169 |
|
| 170 |
|
| 171 |
def _try_release_cuda_memory() -> None:
|
trellis/pipelines/near_image_to_relightable_3d.py
CHANGED
|
@@ -116,12 +116,12 @@ class NeARImageToRelightable3DPipeline(Pipeline):
|
|
| 116 |
t_stage = time.time()
|
| 117 |
new_pipeline.hdri_processor = HDRI_Preprocessor(envmap_h=512, envmap_w=1024)
|
| 118 |
print(f"[NeAR] timing near.from_pretrained.hdri_processor: {time.time() - t_stage:.1f}s", flush=True)
|
| 119 |
-
t_stage = time.time()
|
| 120 |
-
new_pipeline.setup_renderer()
|
| 121 |
-
print(f"[NeAR] timing near.from_pretrained.renderer: {time.time() - t_stage:.1f}s", flush=True)
|
| 122 |
-
t_stage = time.time()
|
| 123 |
-
new_pipeline.setup_tone_mapper("AgX")
|
| 124 |
-
print(f"[NeAR] timing near.from_pretrained.tone_mapper: {time.time() - t_stage:.1f}s", flush=True)
|
| 125 |
print(f"[NeAR] timing near.from_pretrained.total: {time.time() - t0:.1f}s", flush=True)
|
| 126 |
return new_pipeline
|
| 127 |
|
|
|
|
| 116 |
t_stage = time.time()
|
| 117 |
new_pipeline.hdri_processor = HDRI_Preprocessor(envmap_h=512, envmap_w=1024)
|
| 118 |
print(f"[NeAR] timing near.from_pretrained.hdri_processor: {time.time() - t_stage:.1f}s", flush=True)
|
| 119 |
+
# t_stage = time.time()
|
| 120 |
+
# new_pipeline.setup_renderer()
|
| 121 |
+
# print(f"[NeAR] timing near.from_pretrained.renderer: {time.time() - t_stage:.1f}s", flush=True)
|
| 122 |
+
# t_stage = time.time()
|
| 123 |
+
# new_pipeline.setup_tone_mapper("AgX")
|
| 124 |
+
# print(f"[NeAR] timing near.from_pretrained.tone_mapper: {time.time() - t_stage:.1f}s", flush=True)
|
| 125 |
print(f"[NeAR] timing near.from_pretrained.total: {time.time() - t0:.1f}s", flush=True)
|
| 126 |
return new_pipeline
|
| 127 |
|