From d823a30fa21219780a24e0643fe970bb871c5694 Mon Sep 17 00:00:00 2001 From: chengzeyi Date: Mon, 5 Feb 2024 14:14:09 +0800 Subject: [PATCH 1/5] fix compatibility with peft and diffusers 0.26.1 --- .../register_diffusers/__init__.py | 5 ++++- .../transform/builtin_transform.py | 8 ++++---- .../transform/custom_transform.py | 20 +++++++++++-------- .../infer_compiler/transform/manager.py | 2 +- src/onediff/infer_compiler/utils/log_utils.py | 2 +- 5 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/infer_compiler_registry/register_diffusers/__init__.py b/src/infer_compiler_registry/register_diffusers/__init__.py index 7bcbc7b1b..c1aad54e8 100644 --- a/src/infer_compiler_registry/register_diffusers/__init__.py +++ b/src/infer_compiler_registry/register_diffusers/__init__.py @@ -20,7 +20,10 @@ from diffusers.models.resnet import SpatioTemporalResBlock from diffusers.models.transformer_temporal import TransformerSpatioTemporalModel from diffusers.models.attention import TemporalBasicTransformerBlock - from diffusers.models.unet_spatio_temporal_condition import UNetSpatioTemporalConditionModel + if diffusers_version >= version.parse("0.26.00"): + from diffusers.models.unets.unet_spatio_temporal_condition import UNetSpatioTemporalConditionModel + else: + from diffusers.models.unet_spatio_temporal_condition import UNetSpatioTemporalConditionModel if diffusers_version >= version.parse("0.25.00"): from diffusers.models.autoencoders.autoencoder_kl_temporal_decoder import TemporalDecoder diff --git a/src/onediff/infer_compiler/transform/builtin_transform.py b/src/onediff/infer_compiler/transform/builtin_transform.py index 6be671d2f..3a6f13f82 100644 --- a/src/onediff/infer_compiler/transform/builtin_transform.py +++ b/src/onediff/infer_compiler/transform/builtin_transform.py @@ -2,6 +2,7 @@ import os import importlib import types +import inspect from functools import singledispatch, partial from collections import OrderedDict from collections.abc import Iterable @@ -149,9 +150,9 @@ def torch2oflow(mod, *args, **kwargs): def default_converter(obj, verbose=False, *, proxy_cls=None): - # Higher versions of diffusers might use torch.nn.modules.Linear - if obj is torch.nn.Linear: - return flow.nn.Linear + # Workaround for Linear and LoRACompatibleLinear. + if inspect.isclass(obj): + return obj if not is_need_mock(type(obj)): return obj @@ -199,7 +200,6 @@ def init(self): attr = getattr(proxy_md, k) try: self.__dict__[k] = torch2oflow(attr) - except Exception as e: logger.error(f"convert {type(attr)} failed: {e}") raise NotImplementedError(f"Unsupported type: {type(attr)}") diff --git a/src/onediff/infer_compiler/transform/custom_transform.py b/src/onediff/infer_compiler/transform/custom_transform.py index 32eb3a774..3d3f3d9d3 100644 --- a/src/onediff/infer_compiler/transform/custom_transform.py +++ b/src/onediff/infer_compiler/transform/custom_transform.py @@ -2,6 +2,7 @@ import inspect from pathlib import Path from typing import Callable, Dict, List, Optional, Union +import importlib.util from ..import_tools import import_module_from_path from .manager import transform_mgr from .builtin_transform import torch2oflow @@ -50,16 +51,19 @@ def import_module_safely(module_path, module_name): # compiler_registry_path registry_path = Path(__file__).parents[3] / "infer_compiler_registry" - import_module_safely(registry_path / "register_diffusers", "register_diffusers") + if importlib.util.find_spec("diffusers") is not None: + import_module_safely(registry_path / "register_diffusers", "register_diffusers") - import_module_safely( - registry_path / "register_onediff_quant", "register_onediff_quant" - ) + if importlib.util.find_spec("onediff_quant") is not None: + import_module_safely( + registry_path / "register_onediff_quant", "register_onediff_quant" + ) - import_module_safely( - registry_path / "register_diffusers_enterprise_lite", - "register_diffusers_enterprise_lite", - ) + if importlib.util.find_spec("diffusers_enterprise_lite") is not None: + import_module_safely( + registry_path / "register_diffusers_enterprise_lite", + "register_diffusers_enterprise_lite", + ) def ensure_list(obj): diff --git a/src/onediff/infer_compiler/transform/manager.py b/src/onediff/infer_compiler/transform/manager.py index 672e7fb49..03c2c8322 100644 --- a/src/onediff/infer_compiler/transform/manager.py +++ b/src/onediff/infer_compiler/transform/manager.py @@ -26,7 +26,7 @@ def __init__(self, debug_mode=False, tmp_dir="./output"): def _setup_logger(self): name = "ONEDIFF" - level = logging.DEBUG if self.debug_mode else logging.ERROR + level = logging.DEBUG if self.debug_mode else logging.WARNING logger.configure_logging(name=name, file_name=None, level=level, log_dir=None) self.logger = logger diff --git a/src/onediff/infer_compiler/utils/log_utils.py b/src/onediff/infer_compiler/utils/log_utils.py index cfb6ac42f..9e9892209 100644 --- a/src/onediff/infer_compiler/utils/log_utils.py +++ b/src/onediff/infer_compiler/utils/log_utils.py @@ -37,7 +37,7 @@ def configure_logging(self, name, level, log_dir=None, file_name=None): # Create a console formatter and add it to a console handler console_formatter = ColorFormatter( - fmt="%(levelname)s [%(asctime)s] - %(message)s", datefmt="%Y-%m-%d %H:%M:%S" + fmt="%(levelname)s [%(asctime)s] %(filename)s:%(lineno)d - %(message)s", datefmt="%Y-%m-%d %H:%M:%S" ) console_handler = logging.StreamHandler() console_handler.setFormatter(console_formatter) From c6dead6470d0ab464997e8b36abc64bce304642e Mon Sep 17 00:00:00 2001 From: chengzeyi Date: Mon, 5 Feb 2024 14:18:35 +0800 Subject: [PATCH 2/5] fix import order --- src/onediff/infer_compiler/transform/custom_transform.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/onediff/infer_compiler/transform/custom_transform.py b/src/onediff/infer_compiler/transform/custom_transform.py index 3d3f3d9d3..0d0e71f59 100644 --- a/src/onediff/infer_compiler/transform/custom_transform.py +++ b/src/onediff/infer_compiler/transform/custom_transform.py @@ -1,8 +1,8 @@ """A module for registering custom torch2oflow functions and classes.""" import inspect +import importlib.util from pathlib import Path from typing import Callable, Dict, List, Optional, Union -import importlib.util from ..import_tools import import_module_from_path from .manager import transform_mgr from .builtin_transform import torch2oflow From e81c9e80c53ca6193da7c316a7628c36f2ea0320 Mon Sep 17 00:00:00 2001 From: chengzeyi Date: Mon, 5 Feb 2024 19:14:51 +0800 Subject: [PATCH 3/5] remove workaround for Linear and LoRACompatibleLinear --- src/onediff/infer_compiler/transform/builtin_transform.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/onediff/infer_compiler/transform/builtin_transform.py b/src/onediff/infer_compiler/transform/builtin_transform.py index f479a0a9a..0043a7df1 100644 --- a/src/onediff/infer_compiler/transform/builtin_transform.py +++ b/src/onediff/infer_compiler/transform/builtin_transform.py @@ -161,10 +161,6 @@ def _(mod: type): def default_converter(obj, verbose=False, *, proxy_cls=None): - # Workaround for Linear and LoRACompatibleLinear. - if inspect.isclass(obj): - return obj - if not is_need_mock(type(obj)): return obj try: From 91545829b7a36e777f9c029ef32d73c8cdb9a782 Mon Sep 17 00:00:00 2001 From: C Date: Wed, 7 Feb 2024 10:27:20 +0800 Subject: [PATCH 4/5] Change maximum version requirement of diffusers for onediffx to 0.26.1 --- onediff_diffusers_extensions/setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/onediff_diffusers_extensions/setup.py b/onediff_diffusers_extensions/setup.py index a4a2202cb..c16edafea 100644 --- a/onediff_diffusers_extensions/setup.py +++ b/onediff_diffusers_extensions/setup.py @@ -12,7 +12,7 @@ python_requires=">=3.7.0", install_requires=[ "transformers>=4.27.1", - "diffusers>=0.24.0,<=0.25.1", + "diffusers>=0.24.0,<=0.26.1", "accelerate", "torch", "onefx", From 9ecb9fcf6a4fe6520157e7ade65a76088e626f48 Mon Sep 17 00:00:00 2001 From: C Date: Wed, 7 Feb 2024 10:47:43 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20setup.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- onediff_diffusers_extensions/setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/onediff_diffusers_extensions/setup.py b/onediff_diffusers_extensions/setup.py index c16edafea..168ee1dd0 100644 --- a/onediff_diffusers_extensions/setup.py +++ b/onediff_diffusers_extensions/setup.py @@ -12,7 +12,7 @@ python_requires=">=3.7.0", install_requires=[ "transformers>=4.27.1", - "diffusers>=0.24.0,<=0.26.1", + "diffusers>=0.24.0,<=0.26.2", "accelerate", "torch", "onefx",