Skip to content

Commit

Permalink
feat(api): add support for PyTorch 2.0 (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Apr 9, 2023
1 parent a291dc8 commit 34f1973
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions api/onnx_web/convert/diffusion/diffusers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from logging import getLogger
from os import mkdir, path
from packaging import version
from pathlib import Path
from shutil import rmtree
from typing import Dict, Tuple
Expand All @@ -22,6 +23,7 @@
OnnxStableDiffusionPipeline,
StableDiffusionPipeline,
)
from diffusers.models.cross_attention import CrossAttnProcessor
from onnx import load_model, save_model
from onnx.shape_inference import infer_shapes_path
from onnxruntime.transformers.float16 import convert_float_to_float16
Expand All @@ -36,6 +38,8 @@

logger = getLogger(__name__)

is_torch_2_0 = version.parse(version.parse(torch.__version__).base_version) >= version.parse("2.0")


def onnx_export(
model,
Expand Down Expand Up @@ -168,6 +172,9 @@ def convert_diffusion_diffusers(
device=ctx.training_device, dtype=torch.bool
)

if is_torch_2_0:
pipeline.unet.set_attn_processor(CrossAttnProcessor())

unet_in_channels = pipeline.unet.config.in_channels
unet_sample_size = pipeline.unet.config.sample_size
unet_path = output_path / "unet" / ONNX_MODEL
Expand Down

0 comments on commit 34f1973

Please sign in to comment.