(model_path: str, output_path: str, opset: int, fp16: bool = False)
| 66 | |
| 67 | @torch.no_grad() |
| 68 | def convert_models(model_path: str, output_path: str, opset: int, fp16: bool = False): |
| 69 | dtype = torch.float16 if fp16 else torch.float32 |
| 70 | if fp16 and torch.cuda.is_available(): |
| 71 | device = "cuda" |
| 72 | elif fp16 and not torch.cuda.is_available(): |
| 73 | raise ValueError("`float16` model export is only supported on GPUs with CUDA") |
| 74 | else: |
| 75 | device = "cpu" |
| 76 | output_path = Path(output_path) |
| 77 | |
| 78 | # VAE DECODER |
| 79 | vae_decoder = AutoencoderKL.from_pretrained(model_path + "/vae") |
| 80 | vae_latent_channels = vae_decoder.config.latent_channels |
| 81 | # forward only through the decoder part |
| 82 | vae_decoder.forward = vae_decoder.decode |
| 83 | onnx_export( |
| 84 | vae_decoder, |
| 85 | model_args=( |
| 86 | torch.randn(1, vae_latent_channels, 25, 25).to(device=device, dtype=dtype), |
| 87 | False, |
| 88 | ), |
| 89 | output_path=output_path / "vae_decoder" / "model.onnx", |
| 90 | ordered_input_names=["latent_sample", "return_dict"], |
| 91 | output_names=["sample"], |
| 92 | dynamic_axes={ |
| 93 | "latent_sample": {0: "batch", 1: "channels", 2: "height", 3: "width"}, |
| 94 | }, |
| 95 | opset=opset, |
| 96 | ) |
| 97 | del vae_decoder |
| 98 | |
| 99 | |
| 100 | if __name__ == "__main__": |
no test coverage detected