Comfy-Org/ComfyUI · error · KeyError

Attention function {name} not found.

Error message

Attention function {name} not found.

What it means

Raised by get_attention_function when the requested attention function name is not 'optimized' and not present in the REGISTERED_ATTENTION_FUNCTIONS registry (populated via register_attention_function). The special sentinel default (...) means 'no default'; if you pass a concrete default the function returns it instead of raising, so the KeyError only fires for genuinely unknown names with no fallback.

Source

Thrown at comfy/ldm/modules/attention.py:69

        logging.error(f"\n\nTo use the `--use-flash-attention` feature, the `flash-attn` package must be installed first.\ncommand:\n\t{sys.executable} -m pip install flash-attn")
        exit(-1)

COMFY_KITCHEN_INT8_ATTENTION_IS_AVAILABLE = comfy_kitchen.int8_attention_is_available()

REGISTERED_ATTENTION_FUNCTIONS = {}
def register_attention_function(name: str, func: Callable):
    # avoid replacing existing functions
    if name not in REGISTERED_ATTENTION_FUNCTIONS:
        REGISTERED_ATTENTION_FUNCTIONS[name] = func
    else:
        logging.warning(f"Attention function {name} already registered, skipping registration.")

def get_attention_function(name: str, default: Any=...) -> Union[Callable, None]:
    if name == "optimized":
        return optimized_attention
    elif name not in REGISTERED_ATTENTION_FUNCTIONS:
        if default is ...:
            raise KeyError(f"Attention function {name} not found.")
        else:
            return default
    return REGISTERED_ATTENTION_FUNCTIONS[name]

from comfy.cli_args import args
import comfy.ops
ops = comfy.ops.disable_weight_init

FORCE_UPCAST_ATTENTION_DTYPE = model_management.force_upcast_attention_dtype()

def get_attn_precision(attn_precision, current_dtype):
    if args.dont_upcast_attention:
        return None

    if FORCE_UPCAST_ATTENTION_DTYPE is not None and current_dtype in FORCE_UPCAST_ATTENTION_DTYPE:
        return FORCE_UPCAST_ATTENTION_DTYPE[current_dtype]
    return attn_precision

View on GitHub (pinned to 1c6d8d45b3)

Solutions

  1. Use name='optimized' for the standard optimized attention path
  2. Register your custom function first: register_attention_function('myname', my_fn)
  3. Pass a default (e.g. default=None or default=optimized_attention) if an unknown name should degrade gracefully

Example fix

# before
fn = get_attention_function('pytorch')  # KeyError
# after
fn = get_attention_function('pytorch', default=optimized_attention)
Defensive patterns

Strategy: validation

Validate before calling

from comfy.ldm.modules.attention import REGISTERED_ATTENTION_FUNCTIONS, optimized_attention
name = 'my_attn'
fn = optimized_attention if name == 'optimized' else REGISTERED_ATTENTION_FUNCTIONS.get(name)
if fn is None:
    raise ValueError(f'attention function {name!r} not registered; call register_attention_function first')

Type guard

def has_attention_function(name: str) -> bool:
    from comfy.ldm.modules.attention import REGISTERED_ATTENTION_FUNCTIONS
    return name == 'optimized' or name in REGISTERED_ATTENTION_FUNCTIONS

Try / catch

try:
    fn = get_attention_function(name)
except KeyError:
    fn = optimized_attention  # or register the custom function and retry

Prevention

When it happens

Trigger: Calling get_attention_function('flash') (or any custom name) without first registering it via register_attention_function('flash', fn), and without supplying the default argument.

Common situations: Custom model code or extensions referencing an attention backend name that was never registered in this process; renaming a registered function; a dispatch string coming from a checkpoint config that this build doesn't register.

Related errors


AI-assisted analysis of Comfy-Org/ComfyUI@1c6d8d45b3 (2026-08-14). Data as JSON: /api/errors/fb6f7cdd03e77c7b. Report an issue: GitHub.