{"record":{"id":"fb6f7cdd03e77c7b","repo":"Comfy-Org/ComfyUI","slug":"attention-function-name-not-found","errorCode":null,"errorMessage":"Attention function {name} not found.","messagePattern":"Attention function (.+?) not found\\.","errorType":"exception","errorClass":"KeyError","httpStatus":null,"severity":"error","filePath":"comfy/ldm/modules/attention.py","lineNumber":69,"sourceCode":"        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\")\n        exit(-1)\n\nCOMFY_KITCHEN_INT8_ATTENTION_IS_AVAILABLE = comfy_kitchen.int8_attention_is_available()\n\nREGISTERED_ATTENTION_FUNCTIONS = {}\ndef register_attention_function(name: str, func: Callable):\n    # avoid replacing existing functions\n    if name not in REGISTERED_ATTENTION_FUNCTIONS:\n        REGISTERED_ATTENTION_FUNCTIONS[name] = func\n    else:\n        logging.warning(f\"Attention function {name} already registered, skipping registration.\")\n\ndef get_attention_function(name: str, default: Any=...) -> Union[Callable, None]:\n    if name == \"optimized\":\n        return optimized_attention\n    elif name not in REGISTERED_ATTENTION_FUNCTIONS:\n        if default is ...:\n            raise KeyError(f\"Attention function {name} not found.\")\n        else:\n            return default\n    return REGISTERED_ATTENTION_FUNCTIONS[name]\n\nfrom comfy.cli_args import args\nimport comfy.ops\nops = comfy.ops.disable_weight_init\n\nFORCE_UPCAST_ATTENTION_DTYPE = model_management.force_upcast_attention_dtype()\n\ndef get_attn_precision(attn_precision, current_dtype):\n    if args.dont_upcast_attention:\n        return None\n\n    if FORCE_UPCAST_ATTENTION_DTYPE is not None and current_dtype in FORCE_UPCAST_ATTENTION_DTYPE:\n        return FORCE_UPCAST_ATTENTION_DTYPE[current_dtype]\n    return attn_precision\n","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/Comfy-Org/ComfyUI/blob/1c6d8d45b3693bfbb32385b410d813a7fd6be216/comfy/ldm/modules/attention.py#L51-L87","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use name='optimized' for the standard optimized attention path","Register your custom function first: register_attention_function('myname', my_fn)","Pass a default (e.g. default=None or default=optimized_attention) if an unknown name should degrade gracefully"],"exampleFix":"# before\nfn = get_attention_function('pytorch')  # KeyError\n# after\nfn = get_attention_function('pytorch', default=optimized_attention)","handlingStrategy":"validation","validationCode":"from comfy.ldm.modules.attention import REGISTERED_ATTENTION_FUNCTIONS, optimized_attention\nname = 'my_attn'\nfn = optimized_attention if name == 'optimized' else REGISTERED_ATTENTION_FUNCTIONS.get(name)\nif fn is None:\n    raise ValueError(f'attention function {name!r} not registered; call register_attention_function first')","typeGuard":"def has_attention_function(name: str) -> bool:\n    from comfy.ldm.modules.attention import REGISTERED_ATTENTION_FUNCTIONS\n    return name == 'optimized' or name in REGISTERED_ATTENTION_FUNCTIONS","tryCatchPattern":"try:\n    fn = get_attention_function(name)\nexcept KeyError:\n    fn = optimized_attention  # or register the custom function and retry","preventionTips":["Register custom attention functions at extension load time, before any model runs","Pass a default to get_attention_function when graceful degradation is acceptable"],"tags":["attention","registry","dispatch","config"],"backgroundTag":null,"analyzedSha":"1c6d8d45b3693bfbb32385b410d813a7fd6be216","analyzedAt":"2026-08-14T19:37:18.893Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}