xinjjj commited on
Commit
0cb8a49
·
1 Parent(s): 13c7678
Files changed (2) hide show
  1. common.py +103 -35
  2. embodied_gen/models/text_model.py +10 -3
common.py CHANGED
@@ -170,6 +170,25 @@ def _diag_pipeline(name: str, pipeline: object) -> None:
170
  )
171
 
172
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
  def _install_zerogpu_diagnostics() -> None:
174
  try:
175
  from spaces.zero import client as zero_client
@@ -296,30 +315,32 @@ elif os.getenv("GRADIO_APP").startswith("textto3d"):
296
  elapsed_seconds=round(time.monotonic() - _pipeline_init_started, 3),
297
  )
298
  text_model_dir = "weights/Kolors"
299
- _pipeline_img_ip_started = time.monotonic()
300
- _diag("IMPORT_MODEL_START", name="PIPELINE_IMG_IP", model="Kolors-IP")
301
- PIPELINE_IMG_IP = build_text2img_ip_pipeline(text_model_dir, ref_scale=0.3)
302
  _diag(
303
- "IMPORT_MODEL_READY",
304
- name="PIPELINE_IMG_IP",
305
- model_type=type(PIPELINE_IMG_IP).__name__,
306
- elapsed_seconds=round(time.monotonic() - _pipeline_img_ip_started, 3),
307
  )
308
- _pipeline_img_started = time.monotonic()
309
- _diag("IMPORT_MODEL_START", name="PIPELINE_IMG", model="Kolors")
310
- PIPELINE_IMG = build_text2img_pipeline(text_model_dir)
 
 
311
  _diag(
312
  "IMPORT_MODEL_READY",
313
  name="PIPELINE_IMG",
314
  model_type=type(PIPELINE_IMG).__name__,
 
315
  elapsed_seconds=round(time.monotonic() - _pipeline_img_started, 3),
316
  )
317
  _diag(
318
  "IMPORT_MODELS_COMPLETE",
319
- global_cuda_pipeline_count=3,
 
320
  note=(
321
- "Sam3D plus two full Kolors pipelines are resident before "
322
- "ZeroGPU dispatch"
323
  ),
324
  )
325
  SEG_CHECKER = ImageSegChecker(GPT_CLIENT)
@@ -818,6 +839,8 @@ def text2image_fn(
818
  n_sample: int = 3,
819
  req: gr.Request = None,
820
  ) -> list[str]:
 
 
821
  started = time.monotonic()
822
  _diag(
823
  "WORKER_ENTER",
@@ -848,32 +871,77 @@ def text2image_fn(
848
  os.makedirs(output_root, exist_ok=True)
849
  _diag("OUTPUT_DIRECTORY_READY", output_root=output_root)
850
 
851
- pipeline_name = "PIPELINE_IMG" if ip_image is None else "PIPELINE_IMG_IP"
852
- pipeline = PIPELINE_IMG if ip_image is None else PIPELINE_IMG_IP
853
- _diag_pipeline(pipeline_name, pipeline)
854
- if ip_image is not None:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
855
  _diag("IP_ADAPTER_SCALE_START", scale=ip_adapt_scale)
856
  pipeline.set_ip_adapter_scale([ip_adapt_scale])
857
  _diag("IP_ADAPTER_SCALE_READY", scale=ip_adapt_scale)
 
858
 
859
- generation_started = time.monotonic()
860
- _diag("TEXT2IMAGE_GENERATION_START", pipeline=pipeline_name)
861
- images = text2img_gen(
862
- prompt=prompt,
863
- n_sample=n_sample,
864
- guidance_scale=guidance_scale,
865
- pipeline=pipeline,
866
- ip_image=ip_image,
867
- image_wh=image_wh,
868
- infer_step=infer_step,
869
- seed=seed,
870
- )
871
- _diag(
872
- "TEXT2IMAGE_GENERATION_READY",
873
- image_count=len(images),
874
- elapsed_seconds=round(time.monotonic() - generation_started, 3),
875
- )
876
- _diag_cuda("after_text2image_generation")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
877
 
878
  for idx, image in enumerate(images):
879
  preprocess_started = time.monotonic()
 
170
  )
171
 
172
 
173
+ def _move_pipeline(pipeline: object, device: str) -> None:
174
+ started = time.monotonic()
175
+ _diag(
176
+ "PIPELINE_MOVE_START",
177
+ pipeline_type=type(pipeline).__name__,
178
+ target_device=device,
179
+ )
180
+ pipeline.to(device)
181
+ image_encoder = getattr(pipeline, "image_encoder", None)
182
+ if image_encoder is not None:
183
+ image_encoder.to(device)
184
+ _diag(
185
+ "PIPELINE_MOVE_READY",
186
+ pipeline_type=type(pipeline).__name__,
187
+ target_device=device,
188
+ elapsed_seconds=round(time.monotonic() - started, 3),
189
+ )
190
+
191
+
192
  def _install_zerogpu_diagnostics() -> None:
193
  try:
194
  from spaces.zero import client as zero_client
 
315
  elapsed_seconds=round(time.monotonic() - _pipeline_init_started, 3),
316
  )
317
  text_model_dir = "weights/Kolors"
318
+ _pipeline_img_started = time.monotonic()
 
 
319
  _diag(
320
+ "IMPORT_MODEL_START",
321
+ name="PIPELINE_IMG",
322
+ model="Kolors",
323
+ device="cpu",
324
  )
325
+ PIPELINE_IMG = build_text2img_pipeline(
326
+ text_model_dir,
327
+ device="cpu",
328
+ )
329
+ _PIPELINE_IMG_IP = None
330
  _diag(
331
  "IMPORT_MODEL_READY",
332
  name="PIPELINE_IMG",
333
  model_type=type(PIPELINE_IMG).__name__,
334
+ device=str(getattr(PIPELINE_IMG, "device", None)),
335
  elapsed_seconds=round(time.monotonic() - _pipeline_img_started, 3),
336
  )
337
  _diag(
338
  "IMPORT_MODELS_COMPLETE",
339
+ global_cuda_pipeline_count=1,
340
+ global_cpu_pipeline_count=1,
341
  note=(
342
+ "Only Sam3D is CUDA-resident; the standard Kolors pipeline "
343
+ "stays on CPU and IP-Adapter is loaded only when requested"
344
  ),
345
  )
346
  SEG_CHECKER = ImageSegChecker(GPT_CLIENT)
 
839
  n_sample: int = 3,
840
  req: gr.Request = None,
841
  ) -> list[str]:
842
+ global _PIPELINE_IMG_IP
843
+
844
  started = time.monotonic()
845
  _diag(
846
  "WORKER_ENTER",
 
871
  os.makedirs(output_root, exist_ok=True)
872
  _diag("OUTPUT_DIRECTORY_READY", output_root=output_root)
873
 
874
+ if ip_image is None:
875
+ pipeline_name = "PIPELINE_IMG"
876
+ pipeline = PIPELINE_IMG
877
+ else:
878
+ if _PIPELINE_IMG_IP is None:
879
+ ip_pipeline_started = time.monotonic()
880
+ _diag(
881
+ "IP_PIPELINE_INIT_START",
882
+ model="Kolors-IP",
883
+ device="cpu",
884
+ )
885
+ _PIPELINE_IMG_IP = build_text2img_ip_pipeline(
886
+ "weights/Kolors",
887
+ ref_scale=ip_adapt_scale,
888
+ device="cpu",
889
+ )
890
+ _diag(
891
+ "IP_PIPELINE_INIT_READY",
892
+ model_type=type(_PIPELINE_IMG_IP).__name__,
893
+ elapsed_seconds=round(
894
+ time.monotonic() - ip_pipeline_started, 3
895
+ ),
896
+ )
897
+ pipeline_name = "PIPELINE_IMG_IP"
898
+ pipeline = _PIPELINE_IMG_IP
899
  _diag("IP_ADAPTER_SCALE_START", scale=ip_adapt_scale)
900
  pipeline.set_ip_adapter_scale([ip_adapt_scale])
901
  _diag("IP_ADAPTER_SCALE_READY", scale=ip_adapt_scale)
902
+ _diag_pipeline(pipeline_name, pipeline)
903
 
904
+ pipeline_move_attempted = False
905
+ try:
906
+ pipeline_move_attempted = True
907
+ _move_pipeline(pipeline, "cuda")
908
+ _diag_pipeline(pipeline_name, pipeline)
909
+ _diag_cuda("after_kolors_move_to_cuda")
910
+
911
+ generation_started = time.monotonic()
912
+ _diag("TEXT2IMAGE_GENERATION_START", pipeline=pipeline_name)
913
+ images = text2img_gen(
914
+ prompt=prompt,
915
+ n_sample=n_sample,
916
+ guidance_scale=guidance_scale,
917
+ pipeline=pipeline,
918
+ ip_image=ip_image,
919
+ image_wh=image_wh,
920
+ infer_step=infer_step,
921
+ seed=seed,
922
+ )
923
+ _diag(
924
+ "TEXT2IMAGE_GENERATION_READY",
925
+ image_count=len(images),
926
+ elapsed_seconds=round(
927
+ time.monotonic() - generation_started, 3
928
+ ),
929
+ )
930
+ _diag_cuda("after_text2image_generation")
931
+ finally:
932
+ if pipeline_move_attempted:
933
+ try:
934
+ _move_pipeline(pipeline, "cpu")
935
+ except Exception as exc:
936
+ _diag(
937
+ "PIPELINE_MOVE_CPU_EXCEPTION",
938
+ exception_type=type(exc).__name__,
939
+ exception=str(exc)[:2000],
940
+ )
941
+ gc.collect()
942
+ if torch.cuda.is_available():
943
+ torch.cuda.empty_cache()
944
+ _diag_cuda("after_kolors_move_to_cpu")
945
 
946
  for idx, image in enumerate(images):
947
  preprocess_started = time.monotonic()
embodied_gen/models/text_model.py CHANGED
@@ -59,11 +59,15 @@ PROMPT_APPEND = (
59
  PROMPT_KAPPEND = "Single {object}, in the center of the image, white background, 3D style, best quality"
60
 
61
 
62
- def download_kolors_weights(local_dir: str = "weights/Kolors") -> None:
 
 
 
63
  """Downloads Kolors model weights from HuggingFace.
64
 
65
  Args:
66
- local_dir (str, optional): Local directory to store weights.
 
67
  """
68
  logger.info(f"Download kolors weights from huggingface...")
69
  os.makedirs(local_dir, exist_ok=True)
@@ -79,6 +83,9 @@ def download_kolors_weights(local_dir: str = "weights/Kolors") -> None:
79
  check=True,
80
  )
81
 
 
 
 
82
  ip_adapter_path = f"{local_dir}/../Kolors-IP-Adapter-Plus"
83
  subprocess.run(
84
  [
@@ -114,7 +121,7 @@ def build_text2img_ip_pipeline(
114
  pipe = build_text2img_ip_pipeline("weights/Kolors", ref_scale=0.3)
115
  ```
116
  """
117
- download_kolors_weights(ckpt_dir)
118
 
119
  text_encoder = ChatGLMModel.from_pretrained(
120
  f"{ckpt_dir}/text_encoder", torch_dtype=torch.float16
 
59
  PROMPT_KAPPEND = "Single {object}, in the center of the image, white background, 3D style, best quality"
60
 
61
 
62
+ def download_kolors_weights(
63
+ local_dir: str = "weights/Kolors",
64
+ include_ip_adapter: bool = False,
65
+ ) -> None:
66
  """Downloads Kolors model weights from HuggingFace.
67
 
68
  Args:
69
+ local_dir: Local directory to store weights.
70
+ include_ip_adapter: Whether to also download IP-Adapter weights.
71
  """
72
  logger.info(f"Download kolors weights from huggingface...")
73
  os.makedirs(local_dir, exist_ok=True)
 
83
  check=True,
84
  )
85
 
86
+ if not include_ip_adapter:
87
+ return
88
+
89
  ip_adapter_path = f"{local_dir}/../Kolors-IP-Adapter-Plus"
90
  subprocess.run(
91
  [
 
121
  pipe = build_text2img_ip_pipeline("weights/Kolors", ref_scale=0.3)
122
  ```
123
  """
124
+ download_kolors_weights(ckpt_dir, include_ip_adapter=True)
125
 
126
  text_encoder = ChatGLMModel.from_pretrained(
127
  f"{ckpt_dir}/text_encoder", torch_dtype=torch.float16