saliacoel commited on
Commit
552a044
·
verified ·
1 Parent(s): 1df3f39

Upload Salia_Save_Checkpoint_And_Upload.py

Browse files
Files changed (1) hide show
  1. Salia_Save_Checkpoint_And_Upload.py +43 -4
Salia_Save_Checkpoint_And_Upload.py CHANGED
@@ -18,9 +18,12 @@ NODES
18
  Civitai base-model label yourself. Auto cannot identify SDXL derivatives.
19
  Add examples, review metadata/license, and publish through Civitai's UI.
20
 
21
- Both call the installed ComfyUI core checkpoint exporter. No tensor conversion
22
- or alternate checkpoint format is introduced. Numbered filenames, the merged
23
- model/CLIP, VAE, and core model metadata follow core Save Checkpoint behavior.
 
 
 
24
  API keys are removed from embedded prompt/workflow metadata. Keys entered into
25
  widgets can still be stored by ComfyUI when you save the workflow itself; leave
26
  the widgets empty and set HF_TOKEN / CIVITAI_API_KEY instead to avoid that.
@@ -145,6 +148,41 @@ def _safe_error(error, tokens=()):
145
  return message[:1600]
146
 
147
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
  def _save_local(model, clip, vae, filename_prefix, prompt, extra_pnginfo, token):
149
  if not str(filename_prefix).strip():
150
  raise ValueError("filename_prefix cannot be empty.")
@@ -175,7 +213,8 @@ def _save_local(model, clip, vae, filename_prefix, prompt, extra_pnginfo, token)
175
  _log(f"Saving checkpoint: {path}")
176
  _interrupt()
177
  core_save_checkpoint(
178
- model, clip=clip, vae=vae, filename_prefix=name,
 
179
  output_dir=stage, prompt=prompt, extra_pnginfo=extra_pnginfo,
180
  )
181
  exported = Path(stage) / path.name
 
18
  Civitai base-model label yourself. Auto cannot identify SDXL derivatives.
19
  Add examples, review metadata/license, and publish through Civitai's UI.
20
 
21
+ Both call the installed ComfyUI core checkpoint exporter. Numbered filenames,
22
+ the merged model/CLIP, VAE, tensor dtypes and core metadata follow core Save
23
+ Checkpoint behavior. ComfyUI's deferred CLIP weights are materialized on CPU
24
+ before core converts them to checkpoint keys, avoiding its dynamic-VRAM
25
+ FakeDevice allocation error. This uses additional system RAM for CLIP weights;
26
+ the UNet still follows core's normal export path. No core files are modified.
27
  API keys are removed from embedded prompt/workflow metadata. Keys entered into
28
  widgets can still be stored by ComfyUI when you save the workflow itself; leave
29
  the widgets empty and set HF_TOKEN / CIVITAI_API_KEY instead to avoid that.
 
148
  return message[:1600]
149
 
150
 
151
+ class _ClipForCheckpointSaving:
152
+ """Delegate CLIP loading; resolve deferred weights before core conversion.
153
+
154
+ ComfyUI's LazyCastingParam exposes FakeDevice("comfy-lazy-caster", 0).
155
+ Its .to("cpu") applies the patcher's LoRA/merge patches before returning a
156
+ real tensor. Merely copying the underlying parameter would lose patches.
157
+ Core's SDXL converter concatenates Q/K/V before safetensors can call .to(),
158
+ so those tensors must be materialized before handing the state to core.
159
+ The wrapper is local to this export; it does not mutate the original CLIP
160
+ or patch any global ComfyUI/PyTorch functions.
161
+ """
162
+
163
+ def __init__(self, clip):
164
+ self._clip = clip
165
+
166
+ def __getattr__(self, name):
167
+ return getattr(self._clip, name)
168
+
169
+ def state_dict_for_saving(self):
170
+ state = dict(self._clip.state_dict_for_saving())
171
+ resolved = 0
172
+ for name, value in state.items():
173
+ device = getattr(value, "device", None)
174
+ if getattr(device, "type", None) == "comfy-lazy-caster":
175
+ _interrupt()
176
+ # Use ComfyUI's own deferred patch application, retaining dtype.
177
+ state[name] = value.to("cpu")
178
+ if getattr(getattr(state[name], "device", None), "type", None) != "cpu":
179
+ raise RuntimeError(f"ComfyUI could not materialize CLIP weight {name!r} on CPU.")
180
+ resolved += 1
181
+ if resolved:
182
+ _log(f"Materialized {resolved} deferred CLIP tensors for checkpoint export.")
183
+ return state
184
+
185
+
186
  def _save_local(model, clip, vae, filename_prefix, prompt, extra_pnginfo, token):
187
  if not str(filename_prefix).strip():
188
  raise ValueError("filename_prefix cannot be empty.")
 
213
  _log(f"Saving checkpoint: {path}")
214
  _interrupt()
215
  core_save_checkpoint(
216
+ model, clip=_ClipForCheckpointSaving(clip) if clip is not None else None,
217
+ vae=vae, filename_prefix=name,
218
  output_dir=stage, prompt=prompt, extra_pnginfo=extra_pnginfo,
219
  )
220
  exported = Path(stage) / path.name