MCPcopy Create free account
hub / github.com/YesianRohn/TextSSR / convert_models

Function convert_models

diffusers/scripts/convert_vae_diff_to_onnx.py:68–97  ·  view source on GitHub ↗
(model_path: str, output_path: str, opset: int, fp16: bool = False)

Source from the content-addressed store, hash-verified

66
67@torch.no_grad()
68def 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
100if __name__ == "__main__":

Callers 1

Calls 3

onnx_exportFunction · 0.70
from_pretrainedMethod · 0.45
toMethod · 0.45

Tested by

no test coverage detected