amisima commited on
Commit
33ab1ac
·
verified ·
1 Parent(s): 2baf2c2

Fix Hunyuan Foley GPU memory exhaustion

Browse files

Load Foley models on demand and bound visual encoder batches while preserving original uploaded video packets and existing sound prompt tools.

Files changed (1) hide show
  1. app.py +56 -9
app.py CHANGED
@@ -4010,9 +4010,58 @@ def _unwrap_visual_encoder(model_dict):
4010
  return
4011
 
4012
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4013
  def _hunyuan_duration(video_path, prompt, negative, steps, guidance, model_path,
4014
  config_path, out_path):
4015
- """What to book for Hunyuan: four models onto the card, then the denoise."""
4016
  return int(min(330, 150 + float(steps or 25) * 2.2))
4017
 
4018
 
@@ -4024,9 +4073,9 @@ def _hunyuan_run(video_path, prompt, negative, steps, guidance, model_path, conf
4024
  _foley_lib_dir()
4025
  _restore_transformers_helpers()
4026
  try:
4027
- # Four separate models are about to be moved onto the card one after another, and
4028
- # the last of them is where it ran out of room. Hand back whatever is still held
4029
- # from the video pass first.
4030
  try:
4031
  gc.collect()
4032
  if torch.cuda.is_available():
@@ -4038,11 +4087,9 @@ def _hunyuan_run(video_path, prompt, negative, steps, guidance, model_path, conf
4038
  from hunyuanvideo_foley.utils.feature_utils import feature_process
4039
 
4040
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
4041
- model_dict, cfg = load_model(model_path, config_path, device)
4042
- try:
4043
- _unwrap_visual_encoder(model_dict)
4044
- except Exception as wrap_error: # noqa: BLE001
4045
- print(f"[sound] could not wrap the visual encoder: {wrap_error}", flush=True)
4046
 
4047
  visual_feats, text_feats, audio_len = feature_process(video_path, prompt, model_dict, cfg)
4048
  audio, sample_rate = denoise_process(
 
4010
  return
4011
 
4012
 
4013
+ class _HunyuanEncoderProxy(_TensorOutputProxy):
4014
+ """Bound encoder activation memory while retaining every frame/segment."""
4015
+
4016
+ def __init__(self, inner, batch_size):
4017
+ super().__init__(inner)
4018
+ object.__setattr__(self, "_batch_size", int(batch_size))
4019
+
4020
+ def __call__(self, inputs, *args, **kwargs):
4021
+ import torch
4022
+
4023
+ inner = object.__getattribute__(self, "_inner")
4024
+ batch_size = object.__getattribute__(self, "_batch_size")
4025
+ return torch.cat([
4026
+ _tensor_from_model_output(inner(inputs[start:start + batch_size], *args, **kwargs))
4027
+ for start in range(0, inputs.shape[0], batch_size)
4028
+ ], dim=0)
4029
+
4030
+ def get_image_features(self, pixel_values, *args, **kwargs):
4031
+ import torch
4032
+
4033
+ inner = object.__getattribute__(self, "_inner")
4034
+ batch_size = object.__getattribute__(self, "_batch_size")
4035
+ return torch.cat([
4036
+ _tensor_from_model_output(inner.get_image_features(
4037
+ *args, pixel_values=pixel_values[start:start + batch_size], **kwargs))
4038
+ for start in range(0, pixel_values.shape[0], batch_size)
4039
+ ], dim=0)
4040
+
4041
+
4042
+ class _HunyuanOffloadProxy:
4043
+ """Keep lazy model ownership with Hunyuan's offload manager.
4044
+
4045
+ Encoder wrappers are temporary: this object never retains a loaded encoder,
4046
+ so release_feature_models() can free its weights before denoising starts.
4047
+ """
4048
+
4049
+ def __init__(self, inner):
4050
+ self._inner = inner
4051
+
4052
+ def __getattr__(self, name):
4053
+ value = getattr(self._inner, name)
4054
+ if name == "siglip2_model":
4055
+ return _HunyuanEncoderProxy(value, batch_size=4)
4056
+ if name == "syncformer_model":
4057
+ return _HunyuanEncoderProxy(value, batch_size=1)
4058
+ return value
4059
+
4060
+
4061
+
4062
  def _hunyuan_duration(video_path, prompt, negative, steps, guidance, model_path,
4063
  config_path, out_path):
4064
+ """What to book for Hunyuan: on-demand model loading, features and denoising."""
4065
  return int(min(330, 150 + float(steps or 25) * 2.2))
4066
 
4067
 
 
4073
  _foley_lib_dir()
4074
  _restore_transformers_helpers()
4075
  try:
4076
+ # Release unused allocations from the video pass before loading Foley's
4077
+ # feature encoders on demand. Its offload manager releases those encoders
4078
+ # before the denoiser is loaded.
4079
  try:
4080
  gc.collect()
4081
  if torch.cuda.is_available():
 
4087
  from hunyuanvideo_foley.utils.feature_utils import feature_process
4088
 
4089
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
4090
+ model_dict, cfg = load_model(model_path, config_path, device, enable_offload=True)
4091
+ model_dict = _HunyuanOffloadProxy(model_dict)
4092
+ print("[sound] Hunyuan offload enabled; encoder batches: SigLIP2=4, Synchformer=1", flush=True)
 
 
4093
 
4094
  visual_feats, text_feats, audio_len = feature_process(video_path, prompt, model_dict, cfg)
4095
  audio, sample_rate = denoise_process(