Launch comfyui in just one seconds

Launch comfyui in just one seconds

Originally, ComfyUI booted up quickly, but after installing various custom nodes, the launch time has progressively slowed down.

Import times for custom nodes:
   0.0 seconds: /home/wangzg/comfyflow/ComfyUI/custom_nodes/cg-use-everywhere
   0.0 seconds: /home/wangzg/comfyflow/ComfyUI/custom_nodes/ComfyUI_Noise
   0.0 seconds: /home/wangzg/comfyflow/ComfyUI/custom_nodes/ControlNet-LLLite-ComfyUI
   0.0 seconds: /home/wangzg/comfyflow/ComfyUI/custom_nodes/ComfyUi_NNLatentUpscale
   0.0 seconds: /home/wangzg/comfyflow/ComfyUI/custom_nodes/comfyui_controlnet_aux
   0.0 seconds: /home/wangzg/comfyflow/ComfyUI/custom_nodes/ComfyUi_PromptStylers
   0.0 seconds: /home/wangzg/comfyflow/ComfyUI/custom_nodes/sdxl_prompt_styler
   0.0 seconds: /home/wangzg/comfyflow/ComfyUI/custom_nodes/ComfyUI-WD14-Tagger
   0.0 seconds: /home/wangzg/comfyflow/ComfyUI/custom_nodes/ComfyUI_IPAdapter_plus
   0.0 seconds: /home/wangzg/comfyflow/ComfyUI/custom_nodes/comfyui-inpaint-nodes
   0.0 seconds: /home/wangzg/comfyflow/ComfyUI/custom_nodes/ComfyUI-Custom-Scripts
   0.0 seconds: /home/wangzg/comfyflow/ComfyUI/custom_nodes/ComfyUI_UltimateSDUpscale
   0.0 seconds: /home/wangzg/comfyflow/ComfyUI/custom_nodes/rgthree-comfy
   0.0 seconds: /home/wangzg/comfyflow/ComfyUI/custom_nodes/ComfyUI-Frame-Interpolation
   0.0 seconds: /home/wangzg/comfyflow/ComfyUI/custom_nodes/ComfyUI-Advanced-ControlNet
   0.0 seconds: /home/wangzg/comfyflow/ComfyUI/custom_nodes/ComfyUI-AnimateDiff-Evolved
   0.0 seconds: /home/wangzg/comfyflow/ComfyUI/custom_nodes/OneButtonPrompt
   0.0 seconds: /home/wangzg/comfyflow/ComfyUI/custom_nodes/ComfyUI_Comfyroll_CustomNodes
   0.0 seconds: /home/wangzg/comfyflow/ComfyUI/custom_nodes/efficiency-nodes-comfyui
   0.0 seconds: /home/wangzg/comfyflow/ComfyUI/custom_nodes/ComfyUI-Inspire-Pack
   0.0 seconds: /home/wangzg/comfyflow/ComfyUI/custom_nodes/ComfyUI-Allor
   0.0 seconds: /home/wangzg/comfyflow/ComfyUI/custom_nodes/comfyui-dynamicprompts
   0.1 seconds: /home/wangzg/comfyflow/ComfyUI/custom_nodes/ComfyUI-VideoHelperSuite
   0.1 seconds: /home/wangzg/comfyflow/ComfyUI/custom_nodes/ComfyUI_FizzNodes
   0.2 seconds: /home/wangzg/comfyflow/ComfyUI/custom_nodes/ComfyUI_LayerStyle
   0.3 seconds: /home/wangzg/comfyflow/ComfyUI/custom_nodes/ComfyUI-Image-Filters
   0.4 seconds: /home/wangzg/comfyflow/ComfyUI/custom_nodes/ComfyUI_InstantID
   0.6 seconds: /home/wangzg/comfyflow/ComfyUI/custom_nodes/was-node-suite-comfyui
   1.5 seconds: /home/wangzg/comfyflow/ComfyUI/custom_nodes/comfyui_segment_anything
   1.6 seconds: /home/wangzg/comfyflow/ComfyUI/custom_nodes/ComfyUI-Impact-Pack

Total import time: 4.8 seconds

How can I pinpoint where the slow loading times for the nodes are occurring?

use import_profiler package to find bottlenecks in import times, import profiler

A basic python import profiler to find bottlenecks in import times. While not often a problem, imports can be an issue for applications that need to start quickly, such as CLI tools. The goal of import profiler is to help find the bottlenecks when importing a given package.

modify comfyui/nodes file to print the impot time

  ...
        import import_profiler 
        with import_profiler.profile_import() as context:
            if os.path.isfile(module_path):
                module_spec = importlib.util.spec_from_file_location(module_name, module_path)
                module_dir = os.path.split(module_path)[0]
            else:
                module_spec = importlib.util.spec_from_file_location(module_name, os.path.join(module_path, "__init__.py"))
                module_dir = module_path

            module = importlib.util.module_from_spec(module_spec)
            sys.modules[module_name] = module
            module_spec.loader.exec_module(module)

        context.print_info(10)
  ...

Let's see where the loading time for the comfyui_segment_anything component is being spent, shall we?

  cumtime (ms)    intime (ms)  name
         188.1            0.3  node
         150.5            0.1  +sam_hq.build_sam_hq
         150.2            0.3  ++modeling.tiny_vit
         149.9            0.2  +++timm.models.layers
          91              0.2  ++++layers
          85.2            0.1  +++++classifier
          83.7            0.2  ++++++create_act
          81.3           81    +++++++activations_me
          58.6            0.4  ++++models
          17.6            0.2  +local_groundingdino.util.utils
          15.3            0.2  ++local_groundingdino.util.slconfig
          14.9            0.8  +++yapf.yapflib.yapf_api
          16.9            0.1  +local_groundingdino.models
          16.9            0.1  ++GroundingDINO
          16.8            0.1  +++groundingdino
          15.6            0    ++++local_groundingdino.util
          15.5            2.5  +++++transformers
          11.1            1.3  ++++++modeling_utils
        1284.6         1284.6  install

"install.py" file cost 1284 ms, Let's see why it's so slow.

It turns out that each time it starts up, it checks to see if the relevant dependencies are installed.

import sys
import os.path
import subprocess
 
custom_nodes_path = os.path.dirname(os.path.abspath(__file__))
 
def build_pip_install_cmds(args):
    if "python_embeded" in sys.executable or "python_embedded" in sys.executable:
        return [sys.executable, '-s', '-m', 'pip', 'install'] + args
    else:
        return [sys.executable, '-m', 'pip', 'install'] + args
 
def ensure_package():
    cmds = build_pip_install_cmds(['-r', 'requirements.txt'])
    subprocess.run(cmds, cwd=custom_nodes_path)
 
ensure_package()

If we don't need to check for installation every time it starts up, we can optimize the relevant files by making modifications.

import sys
import os.path
import subprocess
 
custom_nodes_path = os.path.dirname(os.path.abspath(__file__))
 
def build_pip_install_cmds(args):
    if "python_embeded" in sys.executable or "python_embedded" in sys.executable:
        return [sys.executable, '-s', '-m', 'pip', 'install'] + args
    else:
        return [sys.executable, '-m', 'pip', 'install'] + args
 
def ensure_package():
    cmds = build_pip_install_cmds(['-r', 'requirements.txt'])
    subprocess.run(cmds, cwd=custom_nodes_path)
 
if __name__ == "__main__":
    ensure_package()

"node.py" file took 188 milliseconds, with 155 milliseconds spent on importing sam_hq.build_sam_hq.

Is it possible to lazy-load the SAMModelLoader node in ComfyUI, so that the sam_hq.build_sam_hq is only loaded when SAMModelLoader is required, especially considering the extensive installation of nodes in ComfyUI where SAMModelLoader isn't needed on every startup?

def load_sam_model(model_name):
    
    from sam_hq.build_sam_hq import sam_model_registry
 
    sam_checkpoint_path = get_local_filepath(
        sam_model_list[model_name]["model_url"], sam_model_dir_name)
    model_file_name = os.path.basename(sam_checkpoint_path)
    model_type = model_file_name.split('.')[0]
    if 'hq' not in model_type and 'mobile' not in model_type:
        model_type = '_'.join(model_type.split('_')[:-1])
    sam = sam_model_registry[model_type](checkpoint=sam_checkpoint_path)
    sam_device = comfy.model_management.get_torch_device()
    sam.to(device=sam_device)
    sam.eval()
    sam.model_name = model_file_name
    return sam
 

By reducing unnecessary checks and implementing lazy loading, we've optimized the loading time of the comfyui_segment_anything node from 1.6 seconds to 0.1 seconds.

We can use this method to optimize the loading time of other nodes as well, thereby making the startup time of ComfyUI in just one second.

Import times for custom nodes:
   0.0 seconds: /workspace/ComfyUI/custom_nodes/cg-use-everywhere
   0.0 seconds: /workspace/ComfyUI/custom_nodes/ComfyUI_Noise
   0.0 seconds: /workspace/ComfyUI/custom_nodes/sdxl_prompt_styler
   0.0 seconds: /workspace/ComfyUI/custom_nodes/ComfyUi_NNLatentUpscale
   0.0 seconds: /workspace/ComfyUI/custom_nodes/ControlNet-LLLite-ComfyUI
   0.0 seconds: /workspace/ComfyUI/custom_nodes/ComfyUI-WD14-Tagger
   0.0 seconds: /workspace/ComfyUI/custom_nodes/ComfyUi_PromptStylers
   0.0 seconds: /workspace/ComfyUI/custom_nodes/ComfyUI_IPAdapter_plus
   0.0 seconds: /workspace/ComfyUI/custom_nodes/ComfyUI_UltimateSDUpscale
   0.0 seconds: /workspace/ComfyUI/custom_nodes/comfyui-inpaint-nodes
   0.0 seconds: /workspace/ComfyUI/custom_nodes/ComfyUI-Frame-Interpolation
   0.0 seconds: /workspace/ComfyUI/custom_nodes/OneButtonPrompt
   0.0 seconds: /workspace/ComfyUI/custom_nodes/ComfyUI-Custom-Scripts
   0.0 seconds: /workspace/ComfyUI/custom_nodes/rgthree-comfy
   0.0 seconds: /workspace/ComfyUI/custom_nodes/ComfyUI-Advanced-ControlNet
   0.0 seconds: /workspace/ComfyUI/custom_nodes/ComfyUI-Allor
   0.0 seconds: /workspace/ComfyUI/custom_nodes/efficiency-nodes-comfyui
   0.0 seconds: /workspace/ComfyUI/custom_nodes/ComfyUI-AnimateDiff-Evolved
   0.0 seconds: /workspace/ComfyUI/custom_nodes/ComfyUI_InstantID
   0.0 seconds: /workspace/ComfyUI/custom_nodes/ComfyUI_Comfyroll_CustomNodes
   0.0 seconds: /workspace/ComfyUI/custom_nodes/comfyui_controlnet_aux
   0.0 seconds: /workspace/ComfyUI/custom_nodes/ComfyUI_LayerStyle
   0.0 seconds: /workspace/ComfyUI/custom_nodes/comfyui-dynamicprompts
   0.0 seconds: /workspace/ComfyUI/custom_nodes/ComfyUI-Image-Filters
   0.0 seconds: /workspace/ComfyUI/custom_nodes/ComfyUI-Inspire-Pack
   0.1 seconds: /workspace/ComfyUI/custom_nodes/was-node-suite-comfyui
   0.1 seconds: /workspace/ComfyUI/custom_nodes/ComfyUI-VideoHelperSuite
   0.1 seconds: /workspace/ComfyUI/custom_nodes/ComfyUI_FizzNodes
   0.1 seconds: /workspace/ComfyUI/custom_nodes/comfyui_segment_anything
   0.6 seconds: /workspace/ComfyUI/custom_nodes/ComfyUI-Impact-Pack
Total import time: 1.0 seconds