From d247bc5a9cd624abe38fee7ac80ec03fda764845 Mon Sep 17 00:00:00 2001 From: Robin Huang Date: Thu, 12 Sep 2024 14:52:06 +0900 Subject: [PATCH 1/4] Expand variables in base_path for extra_config_paths.yaml. (#4893) * Expand variables in base_path for extra_config_paths.yaml. * Fix comments. --- tests-unit/utils/extra_config_test.py | 55 +++++++++++++++++++++++++++ utils/extra_config.py | 2 +- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/tests-unit/utils/extra_config_test.py b/tests-unit/utils/extra_config_test.py index f56dd3e2e..06349560d 100644 --- a/tests-unit/utils/extra_config_test.py +++ b/tests-unit/utils/extra_config_test.py @@ -19,6 +19,24 @@ def mock_yaml_content(): def mock_expanded_home(): return '/home/user' +@pytest.fixture +def yaml_config_with_appdata(): + return """ + test_config: + base_path: '%APPDATA%/ComfyUI' + checkpoints: 'models/checkpoints' + """ + +@pytest.fixture +def mock_yaml_content_appdata(yaml_config_with_appdata): + return yaml.safe_load(yaml_config_with_appdata) + +@pytest.fixture +def mock_expandvars_appdata(): + mock = Mock() + mock.side_effect = lambda path: path.replace('%APPDATA%', 'C:/Users/TestUser/AppData/Roaming') + return mock + @pytest.fixture def mock_add_model_folder_path(): return Mock() @@ -67,3 +85,40 @@ def test_load_extra_model_paths_expands_userpath( # Check if open was called with the correct file path mock_file.assert_called_once_with(dummy_yaml_file_name, 'r') + +@patch('builtins.open', new_callable=mock_open) +def test_load_extra_model_paths_expands_appdata( + mock_file, + monkeypatch, + mock_add_model_folder_path, + mock_expandvars_appdata, + yaml_config_with_appdata, + mock_yaml_content_appdata +): + # Set the mock_file to return yaml with appdata as a variable + mock_file.return_value.read.return_value = yaml_config_with_appdata + + # Attach mocks + monkeypatch.setattr(folder_paths, 'add_model_folder_path', mock_add_model_folder_path) + monkeypatch.setattr(os.path, 'expandvars', mock_expandvars_appdata) + monkeypatch.setattr(yaml, 'safe_load', Mock(return_value=mock_yaml_content_appdata)) + + # Mock expanduser to do nothing (since we're not testing it here) + monkeypatch.setattr(os.path, 'expanduser', lambda x: x) + + dummy_yaml_file_name = 'dummy_path.yaml' + load_extra_path_config(dummy_yaml_file_name) + + expected_base_path = 'C:/Users/TestUser/AppData/Roaming/ComfyUI' + expected_calls = [ + ('checkpoints', os.path.join(expected_base_path, 'models/checkpoints')), + ] + + assert mock_add_model_folder_path.call_count == len(expected_calls) + + # Check the base path variable was expanded + for actual_call, expected_call in zip(mock_add_model_folder_path.call_args_list, expected_calls): + assert actual_call.args == expected_call + + # Verify that expandvars was called + assert mock_expandvars_appdata.called diff --git a/utils/extra_config.py b/utils/extra_config.py index 23c2d791c..7ac3650ac 100644 --- a/utils/extra_config.py +++ b/utils/extra_config.py @@ -13,7 +13,7 @@ def load_extra_path_config(yaml_path): base_path = None if "base_path" in conf: base_path = conf.pop("base_path") - base_path = os.path.expanduser(base_path) + base_path = os.path.expandvars(os.path.expanduser(base_path)) for x in conf: for y in conf[x].split("\n"): if len(y) == 0: From 9d720187f181bc5a74b7bd36b8ad6276b1336000 Mon Sep 17 00:00:00 2001 From: comfyanonymous Date: Thu, 12 Sep 2024 03:56:52 -0400 Subject: [PATCH 2/4] types -> comfy_types to fix import issue. --- comfy/{types.py => comfy_types.py} | 0 comfy/model_patcher.py | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename comfy/{types.py => comfy_types.py} (100%) diff --git a/comfy/types.py b/comfy/comfy_types.py similarity index 100% rename from comfy/types.py rename to comfy/comfy_types.py diff --git a/comfy/model_patcher.py b/comfy/model_patcher.py index 3f5d90273..a89ad4fd7 100644 --- a/comfy/model_patcher.py +++ b/comfy/model_patcher.py @@ -28,7 +28,7 @@ import comfy.utils import comfy.float import comfy.model_management import comfy.lora -from comfy.types import UnetWrapperFunction +from comfy.comfy_types import UnetWrapperFunction def string_to_seed(data): crc = 0xFFFFFFFF From 405b529545f56658960a28e0c73e2a9c64ce2376 Mon Sep 17 00:00:00 2001 From: Yoland Yan <4950057+yoland68@users.noreply.github.com> Date: Thu, 12 Sep 2024 17:53:08 +0900 Subject: [PATCH 3/4] Minor: update tests-unit README.md (#4896) --- tests-unit/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests-unit/README.md b/tests-unit/README.md index 94abd9853..81692b8f1 100644 --- a/tests-unit/README.md +++ b/tests-unit/README.md @@ -2,7 +2,7 @@ ## Install test dependencies -`pip install -r tests-units/requirements.txt` +`pip install -r tests-unit/requirements.txt` ## Run tests -`pytest tests-units/` +`pytest tests-unit/` From d0b7ab88ba0f1cb4ab16e0425f5229e60c934536 Mon Sep 17 00:00:00 2001 From: comfyanonymous Date: Thu, 12 Sep 2024 05:23:32 -0400 Subject: [PATCH 4/4] Add a simple experimental TorchCompileModel node. It probably only works on Linux. For maximum speed on Flux with Nvidia 40 series/ada and newer try using this node with fp8_e4m3fn and the --fast argument. --- comfy_extras/nodes_torch_compile.py | 21 +++++++++++++++++++++ nodes.py | 1 + 2 files changed, 22 insertions(+) create mode 100644 comfy_extras/nodes_torch_compile.py diff --git a/comfy_extras/nodes_torch_compile.py b/comfy_extras/nodes_torch_compile.py new file mode 100644 index 000000000..1d914fa93 --- /dev/null +++ b/comfy_extras/nodes_torch_compile.py @@ -0,0 +1,21 @@ +import torch + +class TorchCompileModel: + @classmethod + def INPUT_TYPES(s): + return {"required": { "model": ("MODEL",), + }} + RETURN_TYPES = ("MODEL",) + FUNCTION = "patch" + + CATEGORY = "_for_testing" + EXPERIMENTAL = True + + def patch(self, model): + m = model.clone() + m.add_object_patch("diffusion_model", torch.compile(model=m.get_model_object("diffusion_model"))) + return (m, ) + +NODE_CLASS_MAPPINGS = { + "TorchCompileModel": TorchCompileModel, +} diff --git a/nodes.py b/nodes.py index bbe73282a..1f14aaf11 100644 --- a/nodes.py +++ b/nodes.py @@ -2102,6 +2102,7 @@ def init_builtin_extra_nodes(): "nodes_hunyuan.py", "nodes_flux.py", "nodes_lora_extract.py", + "nodes_torch_compile.py", ] import_failed = []