{"record":{"id":"be90502c6295c434","repo":"huggingface/transformers","slug":"this-pytree-flattening-function-should-only-be-app","errorCode":null,"errorMessage":"This pytree flattening function should only be applied to DynamicCache","messagePattern":"This pytree flattening function should only be applied to DynamicCache","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/transformers/integrations/executorch.py","lineNumber":1115,"sourceCode":"            flatten_with_keys_fn=lambda dynamic_cache: torch.utils._pytree._dict_flatten_with_keys(\n                _get_cache_dict(dynamic_cache)\n            ),\n        )\n        # TODO (tmanlaibaatar) This won't be needed in torch 2.7.\n        torch.fx._pytree.register_pytree_flatten_spec(\n            DynamicCache,\n            lambda cache, spec: torch.fx._pytree._dict_flatten_spec(_get_cache_dict(cache), spec),\n        )\n    # Catching this in case there are multiple runs for some test runs\n    except ValueError as e:\n        if \"already registered as pytree node\" not in str(e):\n            raise\n\n\ndef _get_cache_dict(cache: DynamicCache):\n    \"\"\"Convert cache to dictionary format for pytree operations.\"\"\"\n    if any(not isinstance(layer, (DynamicLayer, DynamicSlidingWindowLayer)) for layer in cache.layers):\n        raise RuntimeError(\"This pytree flattening function should only be applied to DynamicCache\")\n\n    if not is_torch_greater_or_equal_than_2_6:\n        logging.warning(\"DynamicCache + torch.export is tested on torch 2.6.0+ and may not work on earlier versions.\")\n\n    return {\n        \"key_cache\": [layer.keys for layer in cache.layers if layer.keys is not None],\n        \"value_cache\": [layer.values for layer in cache.layers if layer.values is not None],\n    }\n\n\ndef _unflatten_dynamic_cache(values, context: torch.utils._pytree.Context):\n    dictionary = torch.utils._pytree._dict_unflatten(values, context)\n    cache = DynamicCache()\n    # Reconstruct layers from keys and values lists\n    key_list = dictionary.get(\"key_cache\", [])\n    value_list = dictionary.get(\"value_cache\", [])\n    for idx in range(max(len(key_list), len(value_list))):\n        key = key_list[idx] if idx < len(key_list) else None","sourceCodeStart":1097,"sourceCodeEnd":1133,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/integrations/executorch.py#L1097-L1133","documentation":"Error \"This pytree flattening function should only be applied to DynamicCache\" thrown in huggingface/transformers.","triggerScenarios":"Raised in the ExecuTorch pytree flattening helper when applied to an object that is not a DynamicCache.","commonSituations":"Registering or invoking the cache flatten function on StaticCache or arbitrary objects.","solutions":["Apply this flattening function only to DynamicCache instances.","Register/use a different flatten function for other cache types."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}