How can I export svd onnx model from ComfyUI?
- Dominant language
- Python
- Stars
- 133k
- Forks
- 15.7k
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 155
Description
I hope to export all onnx of svd.
I tried to export in the source while the program run, but got multiple problems.
for example:
```python
# nodes.py
class VAEDecode:
@classmethod
def INPUT_TYPES(s):
return {"required": { "samples": ("LATENT", ), "vae": ("VAE", )}}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "decode"
CATEGORY = "latent"
def decode(self, vae, samples):
# start to export onnx
traced_model = torch.jit.trace(vae.decode, (samples["samples"],))
torch.onnx.export(traced_model, samples["samples"], "vae_decoder.onnx")
# end
return (vae.decode(samples["samples"]), )
```
the error:
```
/data2/hurrayzhu/code/ComfyUI/comfy/model_management.py:379: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
extra_mem = max(inference_memory, memory_required)
/data2/hurrayzhu/code/ComfyUI/comfy/model_management.py:357: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
if get_free_memory(device) > memory_required:
/data2/hurrayzhu/code/ComfyUI/comfy/sd.py:287: TracerWarning: Converting a tensor to a Python integer might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
batch_number = int(free_memory / memory_used)
/data2/hurrayzhu/code/ComfyUI/comfy/sd.py:291: TracerWarning: torch.tensor results are registered as constants in the trace. You can safely ignore this warning if you use this function to create tensors out of constant variables that would be the same every time you call this function. In any other case, this might cause the trace to be incorrect.
pixel_samples = torch.empty((samples_in.shape[0], 3, torch.tensor(samples_in.shape[2] * self.upscale_ratio).round(), torch.tensor(samples_in.shape[3] * self.upscale_ratio).round()), device=self.output_device)
/data2/hurrayzhu/code/ComfyUI/comfy/sd.py:291: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
pixel_samples = torch.empty((samples_in.shape[0], 3, torch.tensor(samples_in.shape[2] * self.upscale_ratio).round(), torch.tensor(samples_in.shape[3] * self.upscale_ratio).round()), device=self.output_device)
!!! Exception during processing !!!
Traceback (most recent call last):
File "/data2/hurrayzhu/code/ComfyUI/execution.py", line 151, in recursive_execute
output_data, output_ui = get_output_data(obj, input_data_all)
File "/data2/hurrayzhu/code/ComfyUI/execution.py", line 81, in get_output_data
return_values = map_node_over_list(obj, input_data_all, obj.FUNCTION, allow_interrupt=True)
File "/data2/hurrayzhu/code/ComfyUI/execution.py", line 74, in map_node_over_list
results.append(getattr(obj, func)(**slice_dict(input_data_all, i)))
File "/data2/hurrayzhu/code/ComfyUI/nodes.py", line 289, in decode
traced_model = torch.jit.trace(vae.decode, (samples["samples"],))
File "/usr/local/lib/python3.10/dist-packages/torch/jit/_trace.py", line 874, in trace
traced = torch._C._create_function_from_trace(
File "/data2/hurrayzhu/code/ComfyUI/comfy/sd.py", line 294, in decode
pixel_samples[x:x+batch_number] = self.process_output(self.first_stage_model.decode(samples).to(self.output_device).float())
File "/data2/hurrayzhu/code/ComfyUI/comfy/ldm/models/autoencoder.py", line 139, in decode
x = self.decoder(z, **kwargs)
File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1511, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1520, in _call_impl
return forward_call(*args, **kwargs)
File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1501, in _slow_forward
result = self.forward(*input, **kwargs)
File "/data2/hurrayzhu/code/ComfyUI/comfy/ldm/modules/diffusionmodules/model.py", line 625, in forward
h = self.conv_in(z)
File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1511, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1520, in _call_impl
return forward_call(*args, **kwargs)
File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1501, in _slow_forward
result = self.forward(*input, **kwargs)
File "/data2/hurrayzhu/code/ComfyUI/comfy/ops.py", line 60, in forward
return super().forward(*args, **kwargs)
File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/conv.py", line 460, in forward
return self._conv_forward(input, self.weight, self.bias)
File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/conv.py", line 456, in _conv_forward
return F.conv2d(input, weight, bias, self.stride,
RuntimeError: Cannot insert a Tensor that requires grad as a constant. Consider making it a parameter or input, or detaching the gradient
Tensor:
(1,1,.,.) =
-0.0012 0.0014 -0.0074
0.0659 0.1797 0.0505
-0.0913 -0.1318 -0.0464
(2,1,.,.) =
0.01 *
1.7456 0.3708 2.4048
-1.9287 1.9409 -0.9033
3.5156 -6.4941 1.0193
(3,1,.,.) =
0.01 *
0.7416 -0.0492 6.0791
-1.2878 -3.2959 2.7588
-0.3693 2.3560 -1.8921
```
I tried to remove all the grad, but still had the same problem:
```python
class VAEDecode:
@classmethod
def INPUT_TYPES(s):
return {"required": { "samples": ("LATENT", ), "vae": ("VAE", )}}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "decode"
CATEGORY = "latent"
def decode(self, vae, samples):
# start to export onnx
def new_decode(sample):
with torch.no_grad():
return vae.decode(sample)
samples_no_grad = samples["samples"].detach()
traced_model = torch.jit.trace(new_decode, (samples_no_grad,))
torch.onnx.export(traced_model, samples["samples"], "vae_decoder.onnx")
# end
return (vae.decode(samples["samples"]), )
```
Can anyone help?
Contributor guide
Assessment
This issue has not been assessed yet.