Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to convert a special input? #23042

Open
Lenan22 opened this issue Dec 6, 2024 · 3 comments
Open

how to convert a special input? #23042

Lenan22 opened this issue Dec 6, 2024 · 3 comments
Labels
feature request request for unsupported feature or enhancement

Comments

@Lenan22
Copy link

Lenan22 commented Dec 6, 2024

Describe the feature request

how to process the cfg_scale?

Describe scenario use case

vid: This is a torch tensor with the size [91200, 33].
timestep: A tensor with a single float value, like [988.] or [1000.], on ‘npu:1’ with dtype torch.bfloat16.
cfg_scale: This can be a tensor like [2.] or it can be None.

vid = torch.randn(91200, 33)
timestep = torch.tensor([988.])
cfg_scale = torch.tensor([2.]) or None

dynamic_axes = {
'vid': {0: 'batch_size'}, # The first dimension of vid is dynamic
'timestep': {0: 'batch_size'}, # The first dimension of timestep is dynamic
'cfg_scale': ???
'output': {0: 'batch_size'} # The first dimension of output is dynamic
}

torch.onnx.export(
model,
(vid, timestep, cfg_scale),
"your_model.onnx",
opset_version=17,
do_constant_folding=True,
input_names=['vid', 'timestep', 'cfg_scale'],
output_names=['output'],
dynamic_axes=dynamic_axes
)

how to process the cfg_scale?

@Lenan22 Lenan22 added the feature request request for unsupported feature or enhancement label Dec 6, 2024
@tianleiwu
Copy link
Contributor

stable diffusion uses scale <= 1.0 to do classifier free guidance, and the only change is the text embedding batch size. The unet onnx model is still the same.

If you want to convert the guidance code [like https://github.com/microsoft/onnxruntime/blob/6d9636f07cccdb6e4ac453087ad54c3bc9854d50/onnxruntime/python/tools/transformers/models/stable_diffusion/pipeline_stable_diffusion.py#L458-L460] to onnx model, you can generate two models: one with cfg_scale > 1; another for cfg_scale <= 1.0. Another approach is to use If onnx operator: https://onnx.ai/onnx/operators/onnx__If.html.

@Lenan22
Copy link
Author

Lenan22 commented Dec 10, 2024

"Use the ONNX If operator: https://onnx.ai/onnx/operators/onnx__If.html.

Are there any related examples? "

@tianleiwu
Copy link
Contributor

"Use the ONNX If operator: https://onnx.ai/onnx/operators/onnx__If.html.

Are there any related examples? "

Here is an example:
https://github.com/huggingface/optimum/blob/4a7cb298140ee9bed968d98a780a950d15bb2935/optimum/onnx/graph_transformations.py#L287-L306

It uses "If" to combine two graphs (one graph for decoder with kv cache input, and another graph for decoder without kv cache input).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request request for unsupported feature or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants