multimodalart HF Staff commited on
Commit
859958d
·
verified ·
1 Parent(s): fd8d900

Never .to() a torchao-quantised param (ZeroGPU empty_like on Float8Tensor)

Browse files
Files changed (1) hide show
  1. xvideo/models/loader.py +23 -6
xvideo/models/loader.py CHANGED
@@ -102,6 +102,20 @@ def _is_fp8_target(module: nn.Module, fqn: str) -> bool:
102
  return any(sub in fqn for sub in _FP8_TARGET_SUBSTRINGS)
103
 
104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  def build_dit(cfg: ExpConfig, dit_path: str, device: str = "cuda") -> nn.Module:
106
  dtype = PRECISION_TO_TYPE[cfg.dit_precision]
107
  params = dict(cfg.dit_arch_config.get("params", {}))
@@ -153,15 +167,18 @@ def build_dit(cfg: ExpConfig, dit_path: str, device: str = "cuda") -> nn.Module:
153
  _log(f" block {idx + 1}/{n_blocks}")
154
  _log(f"FP8 quantisation done in {time.time() - t0:.0f}s")
155
 
156
- # Anything still mmap-backed (patch embed, modulation, norms, proj_out) is
157
- # cloned so the 32.5 GB file can be released, then moved to the device.
158
- for name, param in model.named_parameters():
159
- if param.device.type == "cpu":
160
- param.data = param.data.clone()
 
 
 
 
161
  del state_dict
162
  gc.collect()
163
 
164
- model.to(device)
165
  if getattr(model.config, "causal", False):
166
  model.config.use_inference_kv_cache = True
167
  return model
 
102
  return any(sub in fqn for sub in _FP8_TARGET_SUBSTRINGS)
103
 
104
 
105
+ def _move_unquantised(model: nn.Module, device: str) -> None:
106
+ """Move every top-level child except ``double_blocks`` (already on device)."""
107
+ for name, child in model.named_children():
108
+ if name == "double_blocks":
109
+ continue
110
+ child.to(device)
111
+ for name, param in list(model.named_parameters(recurse=False)):
112
+ if param.device.type != torch.device(device).type:
113
+ setattr(model, name, nn.Parameter(param.data.to(device), requires_grad=False))
114
+ for name, buf in list(model.named_buffers(recurse=False)):
115
+ if buf is not None and buf.device.type != torch.device(device).type:
116
+ model.register_buffer(name, buf.to(device))
117
+
118
+
119
  def build_dit(cfg: ExpConfig, dit_path: str, device: str = "cuda") -> nn.Module:
120
  dtype = PRECISION_TO_TYPE[cfg.dit_precision]
121
  params = dict(cfg.dit_arch_config.get("params", {}))
 
167
  _log(f" block {idx + 1}/{n_blocks}")
168
  _log(f"FP8 quantisation done in {time.time() - t0:.0f}s")
169
 
170
+ # The blocks are already on the device. Everything still mmap-backed
171
+ # (patch embed, timestep/text projections, final layer) is moved here.
172
+ # A quantised parameter must NEVER be moved again: `Tensor.to` on a
173
+ # torchao `Float8Tensor` hits an unimplemented `aten.empty_like` inside
174
+ # the ZeroGPU tensor-tracking hook.
175
+ _move_unquantised(model, device)
176
+ else:
177
+ model.to(device)
178
+
179
  del state_dict
180
  gc.collect()
181
 
 
182
  if getattr(model.config, "causal", False):
183
  model.config.use_inference_kv_cache = True
184
  return model