Skip to content

Commit

Permalink
deprecated onediffx.lora (#1179)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **Deprecation**
	- Added deprecation warnings for several LoRA-related functions.
	- Marked existing LoRA tests as skipped due to deprecation.

- **Testing**
- Introduced a new test for evaluating numerical stability of model
parameters during adapter loading and deletion.

- **Warning Management**
- Enhanced warning system to ensure deprecated functionality is clearly
communicated to users.

- **Workflow Enhancements**
	- Improved visibility into ComfyUI service status during tests.
- Updated volume mappings in the Docker Compose configuration for the
testing service.

- **Dependency Updates**
- Upgraded the `diffusers` package version in both `requirements.txt`
and `setup.py`.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Shenghang Tsai <[email protected]>
  • Loading branch information
marigoold and jackalcooper authored Jan 12, 2025
1 parent 1be9b27 commit 61b7cca
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 9 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ jobs:
run: |
docker exec -w /app/ComfyUI -d ${{ env.CONTAINER_NAME }} sh -c "python3 /app/ComfyUI/main.py --gpu-only --disable-cuda-malloc --port 8188 --extra-model-paths-config /src/onediff/tests/comfyui/extra_model_paths.yaml > /app/ComfyUI/onediff_comfyui.log 2>&1"
sleep 30
# print to check if comfy is launched successfully
- run: docker exec ${{ env.CONTAINER_NAME }} ps aux
- run: docker exec -w /src/onediff/onediff_comfy_nodes/benchmarks ${{ env.CONTAINER_NAME }} bash scripts/install_env.sh /app/ComfyUI
Expand Down Expand Up @@ -361,7 +363,7 @@ jobs:
- if: matrix.test-suite == 'diffusers_examples'
run: docker exec -w /src/onediff/onediff_diffusers_extensions ${{ env.CONTAINER_NAME }} bash examples/unet_save_and_load.sh --model_id=/share_nfs/hf_models/stable-diffusion-xl-base-1.0
- if: matrix.test-suite == 'diffusers_examples'
run: docker exec ${{ env.CONTAINER_NAME }} python3 -m pip install --user scikit-image "numpy<2" "diffusers==0.23" peft pytest "huggingface_hub<=0.25.0"
run: docker exec ${{ env.CONTAINER_NAME }} python3 -m pip install --user scikit-image "numpy<2" "diffusers==0.23" peft==0.6.0 pytest "huggingface_hub<=0.25.0"
- if: matrix.test-suite == 'diffusers_examples'
run: docker exec -e ONEFLOW_MLIR_ENABLE_INFERENCE_OPTIMIZATION=0 ${{ env.CONTAINER_NAME }} python3 -m pytest -v onediff_diffusers_extensions/tests/test_lora.py --disable-warnings

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
},
"43": {
"inputs": {
"image": "control/sdxl-unet-control-lora-speedup.png",
"image": "sdxl-unet-control-lora-speedup.png",
"upload": "image"
},
"class_type": "LoadImage",
Expand Down
9 changes: 6 additions & 3 deletions onediff_comfy_nodes/modules/oneflow/onediff_controlnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,12 @@ class control_lora_ops(ControlLoraOps, comfy.ops.manual_cast):
sd = diffusion_model.state_dict()
cm = self.control_model.state_dict()
for k in sd:
weight = comfy.model_management.resolve_lowvram_weight(
sd[k], diffusion_model, k
)
# comfy.model_management.resolve_lowvram_weight(weight, model, key) always returns weight
# and is removed now
# weight = comfy.model_management.resolve_lowvram_weight(
# sd[k], diffusion_model, k
# )
weight = sd[k]
try:
set_attr_of(self.control_model, k, weight)
except Exception as e:
Expand Down
2 changes: 1 addition & 1 deletion onediff_diffusers_extensions/examples/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pip install -r requirements.txt
transformers==4.27.1
diffusers[torch]==0.19.3
diffusers[torch]==0.27.0
onediff
chardet
opencv-python==4.8.0.74
24 changes: 24 additions & 0 deletions onediff_diffusers_extensions/onediffx/lora/lora.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import functools
import warnings
from collections import OrderedDict
from pathlib import Path
Expand Down Expand Up @@ -37,10 +38,28 @@ class OneDiffXWarning(Warning):


warnings.filterwarnings("always", category=OneDiffXWarning)
warnings.filterwarnings("always", category=DeprecationWarning)


def deprecated():
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
warnings.warn(
f"function {func.__name__} of onediffx.lora is deprecated",
category=DeprecationWarning,
)
return func(*args, **kwargs)

return wrapper

return decorator


USE_PEFT_BACKEND = False


@deprecated()
def load_and_fuse_lora(
pipeline: LoraLoaderMixin,
pretrained_model_name_or_path_or_dict: Union[str, Path, Dict[str, torch.Tensor]],
Expand All @@ -63,6 +82,7 @@ def load_and_fuse_lora(
)


@deprecated()
def load_lora_and_optionally_fuse(
pipeline: LoraLoaderMixin,
pretrained_model_name_or_path_or_dict: Union[str, Path, Dict[str, torch.Tensor]],
Expand Down Expand Up @@ -186,6 +206,7 @@ def load_lora_and_optionally_fuse(
)


@deprecated()
def unfuse_lora(pipeline: LoraLoaderMixin):
def _unfuse_lora_apply(m: torch.nn.Module):
if isinstance(m, (torch.nn.Linear, PatchedLoraProjection, torch.nn.Conv2d)):
Expand All @@ -205,6 +226,7 @@ def _unfuse_lora_apply(m: torch.nn.Module):
pipeline.text_encoder_2.apply(_unfuse_lora_apply)


@deprecated()
def set_and_fuse_adapters(
pipeline: LoraLoaderMixin,
adapter_names: Optional[Union[List[str], str]] = None,
Expand Down Expand Up @@ -248,6 +270,7 @@ def set_adapters_apply(m):
pipeline.text_encoder_2.apply(set_adapters_apply)


@deprecated()
def delete_adapters(
self, adapter_names: Union[List[str], str] = None, safe_delete=True
):
Expand Down Expand Up @@ -276,6 +299,7 @@ def delete_adapters_apply(m):
self.text_encoder_2.apply(delete_adapters_apply)


@deprecated()
def get_active_adapters(self) -> List[str]:
if hasattr(self, "_adapter_names"):
return list(self._active_adapter_names.keys())
Expand Down
7 changes: 7 additions & 0 deletions onediff_diffusers_extensions/tests/test_lora.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
)

image_file_prefix = "/share_nfs/onediff_ci/diffusers/images/1.0"
if not Path(image_file_prefix).exists():
Path(image_file_prefix).mkdir(parents=True)


@pytest.fixture
Expand Down Expand Up @@ -163,6 +165,7 @@ def preload_multi_loras(pipe, loras):
unfuse_lora(pipe)


@pytest.mark.skip(reason="onediffx.lora is deprecated")
def test_lora_loading(pipe, get_loras):
pipe.unet = oneflow_compile(pipe.unet)
pipe(
Expand Down Expand Up @@ -193,6 +196,7 @@ def test_lora_loading(pipe, get_loras):
assert ssim > 0.92, f"LoRA {name} ssim too low"


@pytest.mark.skip(reason="onediffx.lora is deprecated")
def test_multi_lora_loading(pipe, get_multi_loras, get_loras):
pipe.unet = oneflow_compile(pipe.unet)
multi_loras = get_multi_loras()
Expand Down Expand Up @@ -226,6 +230,7 @@ def test_multi_lora_loading(pipe, get_multi_loras, get_loras):
assert ssim > 0.92, f"LoRA {names} ssim too low"


@pytest.mark.skip(reason="onediffx.lora is deprecated")
def test_get_active_adapters(pipe, get_multi_loras, get_loras):
multi_loras = get_multi_loras()
preload_multi_loras(pipe, get_loras())
Expand All @@ -236,6 +241,7 @@ def test_get_active_adapters(pipe, get_multi_loras, get_loras):
assert set(active_adapters) == set(names)


@pytest.mark.skip(reason="onediffx.lora is deprecated")
def test_delete_adapters(pipe, get_multi_loras, get_loras):
multi_loras = get_multi_loras()
for names, _ in multi_loras.items():
Expand All @@ -250,6 +256,7 @@ def test_delete_adapters(pipe, get_multi_loras, get_loras):
assert set(active_adapters) == set(names) - set(names_to_delete)


@pytest.mark.skip(reason="onediffx.lora is deprecated")
def test_lora_numerical_stability():
original_pipe = get_pipe("sd1.5")
pipe = get_pipe("sd1.5")
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
python_requires=">=3.8.0",
install_requires=[
"transformers>=4.27.1",
"diffusers>=0.19.3",
"diffusers>=0.27.0",
"accelerate",
"torch",
],
Expand Down
3 changes: 2 additions & 1 deletion tests/comfy-docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ services:
- ${PWD}/${COMFYUI_SRC_DIR}:/app/ComfyUI
- /share_nfs/hf_models/comfyui_resources/custom_nodes/ComfyUI_IPAdapter_plus:/app/ComfyUI/custom_nodes/ComfyUI_IPAdapter_plus
- /share_nfs/hf_models/comfyui_resources/input/input_image_vermeer.png:/app/ComfyUI/input/input_image_vermeer.png:ro
- /share_nfs/hf_models/comfyui_resources/input/sdxl-unet-control-lora-speedup.png:/app/ComfyUI/input/sdxl-unet-control-lora-speedup.png:ro
- /share_nfs/hf_models/comfyui_resources/input/a_car.png:/app/ComfyUI/input/a_car.png:ro
- ${PWD}/onediff_comfy_nodes:/app/ComfyUI/custom_nodes/onediff_comfy_nodes
- ${SDXL_BASE}:/app/ComfyUI/models/checkpoints/sd_xl_base_1.0.safetensors:ro
- ${UNET_INT8}:/app/ComfyUI/models/unet_int8/unet_int8:ro
- ${CONTROL_LORA_OPENPOSEXL2_RANK256}:/app/ComfyUI/models/controlnet/control-lora-openposeXL2-rank256.safetensors:ro
- $PWD/tests/comfyui/workflows/input/control:/app/ComfyUI/input/control:ro
# - $PWD/tests/comfyui/workflows/input/control:/app/ComfyUI/input/control:ro
- $PWD:/src/onediff
working_dir: /src/onediff
restart: "no"
Expand Down
2 changes: 1 addition & 1 deletion tests/comfyui/workflows/sdxl-control-lora-speedup.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
"Node name for S&R": "LoadImage"
},
"widgets_values": [
"control/sdxl-unet-control-lora-speedup.png",
"sdxl-unet-control-lora-speedup.png",
"image"
]
},
Expand Down

0 comments on commit 61b7cca

Please sign in to comment.