XLabs-AI / x-flux-comfyui

Apache License 2.0
725 stars 50 forks source link

Can't load a lora trained from civitai. #29

Open dchatel opened 3 weeks ago

dchatel commented 3 weeks ago

Using the lora workflow provided on x-flux-comfyui to load and use a flux lora trained on civitai, I get this error:

!!! Exception during processing !!! list index out of range
Traceback (most recent call last):
  File "G:\ai\ComfyUI\execution.py", line 316, in execute
    output_data, output_ui, has_subgraph = get_output_data(obj, input_data_all, execution_block_cb=execution_block_cb, pre_execute_cb=pre_execute_cb)
                                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "G:\ai\ComfyUI\execution.py", line 191, in get_output_data
    return_values = _map_node_over_list(obj, input_data_all, obj.FUNCTION, allow_interrupt=True, execution_block_cb=execution_block_cb, pre_execute_cb=pre_execute_cb)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "G:\ai\ComfyUI\execution.py", line 168, in _map_node_over_list
    process_inputs(input_dict, i)
  File "G:\ai\ComfyUI\execution.py", line 157, in process_inputs
    results.append(getattr(obj, func)(**inputs))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "G:\ai\ComfyUI\custom_nodes\x-flux-comfyui\nodes.py", line 117, in loadmodel
    checkpoint, lora_rank = load_flux_lora(os.path.join(dir_xlabs_loras, lora_name))
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "G:\ai\ComfyUI\custom_nodes\x-flux-comfyui\nodes.py", line 59, in load_flux_lora
    a1 = sorted(list(checkpoint[list(checkpoint.keys())[0]].shape))[0]
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range

This is due to the following: image Basically, that first tensor in the checkpoint is a scalar, so it has no dimension, thus you can't iterate over its dimensions (obviously).

But if I do a quick "fix" like this:

def load_flux_lora(path):
    if path is not None:
        if '.safetensors' in path:
            checkpoint = load_safetensors(path)
        else:
            checkpoint = torch.load(path, map_location='cpu')
    else:
        checkpoint = None
        print("Invalid path")
    if len(checkpoint[list(checkpoint.keys())[0]].shape) > 0:
        a1 = sorted(list(checkpoint[list(checkpoint.keys())[0]].shape))[0]
        a2 = sorted(list(checkpoint[list(checkpoint.keys())[1]].shape))[0]
        if a1==a2:
            return checkpoint, int(a1)
    return checkpoint, 16

Then I get the following error:

!!! Exception during processing !!! Error(s) in loading state_dict for DoubleStreamBlockLoraProcessor:
        Missing key(s) in state_dict: "qkv_lora1.down.weight", "qkv_lora1.up.weight", "proj_lora1.down.weight", "proj_lora1.up.weight", "qkv_lora2.down.weight", "qkv_lora2.up.weight", "proj_lora2.down.weight", "proj_lora2.up.weight".
Traceback (most recent call last):
  File "G:\ai\ComfyUI\execution.py", line 316, in execute
    output_data, output_ui, has_subgraph = get_output_data(obj, input_data_all, execution_block_cb=execution_block_cb, pre_execute_cb=pre_execute_cb)
                                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "G:\ai\ComfyUI\execution.py", line 191, in get_output_data
    return_values = _map_node_over_list(obj, input_data_all, obj.FUNCTION, allow_interrupt=True, execution_block_cb=execution_block_cb, pre_execute_cb=pre_execute_cb)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "G:\ai\ComfyUI\execution.py", line 168, in _map_node_over_list
    process_inputs(input_dict, i)
  File "G:\ai\ComfyUI\execution.py", line 157, in process_inputs
    results.append(getattr(obj, func)(**inputs))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "G:\ai\ComfyUI\custom_nodes\x-flux-comfyui\nodes.py", line 140, in loadmodel
    lora_attn_procs[name].load_state_dict(lora_state_dict)
  File "C:\Users\dchat\miniconda3\envs\comfy\Lib\site-packages\torch\nn\modules\module.py", line 2189, in load_state_dict
    raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(
RuntimeError: Error(s) in loading state_dict for DoubleStreamBlockLoraProcessor:
        Missing key(s) in state_dict: "qkv_lora1.down.weight", "qkv_lora1.up.weight", "proj_lora1.down.weight", "proj_lora1.up.weight", "qkv_lora2.down.weight", "qkv_lora2.up.weight", "proj_lora2.down.weight", "proj_lora2.up.weight".
Vovanm88 commented 3 weeks ago

Sooo, can you send me you LoRA so I do nodes compatible with this?

dchatel commented 3 weeks ago

Well, it's in the zip file I attached to the original message, under the link caption "a flux lora trained on civitai".

barnaclejive commented 2 weeks ago

I have the same error using a Lora I trained myself using ai-toolkit.

!!! Exception during processing !!! Error(s) in loading state_dict for DoubleStreamBlockLoraProcessor:
        Missing key(s) in state_dict: "qkv_lora1.down.weight", "qkv_lora1.up.weight", "proj_lora1.down.weight", "proj_lora1.up.weight", "qkv_lora2.down.weight", "qkv_lora2.up.weight", "proj_lora2.down.weight", "proj_lora2.up.weight".
Traceback (most recent call last):
  File "C:\Users\foo\ai\ComfyUI\execution.py", line 317, in execute
    output_data, output_ui, has_subgraph = get_output_data(obj, input_data_all, execution_block_cb=execution_block_cb, pre_execute_cb=pre_execute_cb)
  File "C:\Users\foo\ai\ComfyUI\execution.py", line 192, in get_output_data
    return_values = _map_node_over_list(obj, input_data_all, obj.FUNCTION, allow_interrupt=True, execution_block_cb=execution_block_cb, pre_execute_cb=pre_execute_cb)
  File "C:\Users\foo\ai\ComfyUI\execution.py", line 169, in _map_node_over_list
    process_inputs(input_dict, i)
  File "C:\Users\foo\ai\ComfyUI\execution.py", line 158, in process_inputs
    results.append(getattr(obj, func)(**inputs))
  File "C:\Users\foo\ai\ComfyUI\custom_nodes\x-flux-comfyui\nodes.py", line 153, in loadmodel
    lora_attn_procs[name].load_state_dict(lora_state_dict)
  File "C:\Users\foo\ai\ComfyUI\venv\lib\site-packages\torch\nn\modules\module.py", line 2215, in load_state_dict
    raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(
RuntimeError: Error(s) in loading state_dict for DoubleStreamBlockLoraProcessor:
        Missing key(s) in state_dict: "qkv_lora1.down.weight", "qkv_lora1.up.weight", "proj_lora1.down.weight", "proj_lora1.up.weight", "qkv_lora2.down.weight", "qkv_lora2.up.weight", "proj_lora2.down.weight", "proj_lora2.up.weight".
moebiussurfing commented 2 weeks ago

i have the same problem loading a LoRA with XFlux made locally by with the ostris ai-toolkit script:

Error occurred when executing FluxLoraLoader:

Error(s) in loading state_dict for DoubleStreamBlockLoraProcessor:
Missing key(s) in state_dict: "qkv_lora1.down.weight", "qkv_lora1.up.weight", "proj_lora1.down.weight", "proj_lora1.up.weight", "qkv_lora2.down.weight", "qkv_lora2.up.weight", "proj_lora2.down.weight", "proj_lora2.up.weight".

File "D:\_AI\ComfyUI_windows_portable\ComfyUI\execution.py", line 317, in execute
output_data, output_ui, has_subgraph = get_output_data(obj, input_data_all, execution_block_cb=execution_block_cb, pre_execute_cb=pre_execute_cb)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\_AI\ComfyUI_windows_portable\ComfyUI\execution.py", line 192, in get_output_data
return_values = _map_node_over_list(obj, input_data_all, obj.FUNCTION, allow_interrupt=True, execution_block_cb=execution_block_cb, pre_execute_cb=pre_execute_cb)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\_AI\ComfyUI_windows_portable\ComfyUI\execution.py", line 169, in _map_node_over_list
process_inputs(input_dict, i)
File "D:\_AI\ComfyUI_windows_portable\ComfyUI\execution.py", line 158, in process_inputs
results.append(getattr(obj, func)(**inputs))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\_AI\ComfyUI_windows_portable\ComfyUI\custom_nodes\x-flux-comfyui\nodes.py", line 153, in loadmodel
lora_attn_procs[name].load_state_dict(lora_state_dict)
File "D:\_AI\ComfyUI_windows_portable\python_embeded\Lib\site-packages\torch\nn\modules\module.py", line 2152, in load_state_dict
raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(
balala8 commented 2 weeks ago

I have the same error using a Lora I trained myself using ai-toolkit.

!!! Exception during processing !!! Error(s) in loading state_dict for DoubleStreamBlockLoraProcessor:
        Missing key(s) in state_dict: "qkv_lora1.down.weight", "qkv_lora1.up.weight", "proj_lora1.down.weight", "proj_lora1.up.weight", "qkv_lora2.down.weight", "qkv_lora2.up.weight", "proj_lora2.down.weight", "proj_lora2.up.weight".
Traceback (most recent call last):
  File "C:\Users\foo\ai\ComfyUI\execution.py", line 317, in execute
    output_data, output_ui, has_subgraph = get_output_data(obj, input_data_all, execution_block_cb=execution_block_cb, pre_execute_cb=pre_execute_cb)
  File "C:\Users\foo\ai\ComfyUI\execution.py", line 192, in get_output_data
    return_values = _map_node_over_list(obj, input_data_all, obj.FUNCTION, allow_interrupt=True, execution_block_cb=execution_block_cb, pre_execute_cb=pre_execute_cb)
  File "C:\Users\foo\ai\ComfyUI\execution.py", line 169, in _map_node_over_list
    process_inputs(input_dict, i)
  File "C:\Users\foo\ai\ComfyUI\execution.py", line 158, in process_inputs
    results.append(getattr(obj, func)(**inputs))
  File "C:\Users\foo\ai\ComfyUI\custom_nodes\x-flux-comfyui\nodes.py", line 153, in loadmodel
    lora_attn_procs[name].load_state_dict(lora_state_dict)
  File "C:\Users\foo\ai\ComfyUI\venv\lib\site-packages\torch\nn\modules\module.py", line 2215, in load_state_dict
    raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(
RuntimeError: Error(s) in loading state_dict for DoubleStreamBlockLoraProcessor:
        Missing key(s) in state_dict: "qkv_lora1.down.weight", "qkv_lora1.up.weight", "proj_lora1.down.weight", "proj_lora1.up.weight", "qkv_lora2.down.weight", "qkv_lora2.up.weight", "proj_lora2.down.weight", "proj_lora2.up.weight".

Using the normal Lora model loader will not report an error