{
"$type": "site.standard.document",
"bskyPostRef": {
"cid": "bafyreihduuo4h32ftby7b3xowredaqha42qiiopsmpmfpzzrdolasnw2sm",
"uri": "at://did:plc:pgryn3ephfd2xgft23qokfzt/app.bsky.feed.post/3mh7fcgnkyia2"
},
"path": "/t/importerror-for-function-find-pruneable-heads-and-indices/174317#post_2",
"publishedAt": "2026-03-16T15:10:23.000Z",
"site": "https://discuss.huggingface.co",
"tags": [
"Hugging Face",
"GitHub"
],
"textContent": "Oh… Seems completely removed…\n\n* * *\n\n`find_pruneable_heads_and_indices` is **not just in a different place in Transformers 5.3.0.dev0**. The evidence points to it being **removed from the current v5 public internals** , not relocated to another documented import path. In legacy v3 docs it lived under `transformers.modeling_utils`. In later v4 docs it lived under `transformers.pytorch_utils`. In current v5.3.0 docs, `prune_linear_layer` is still documented, but `find_pruneable_heads_and_indices` is no longer listed. The v4.57.1 source contains the function, while the v5.0.0 and current `main` `pytorch_utils.py` do not. (Hugging Face)\n\n## What changed\n\nThere is a clear timeline:\n\n * **v3.x** : import from `transformers.modeling_utils` was valid. (Hugging Face)\n * **v4.x** : the helper was documented and implemented under `transformers.pytorch_utils`. (Hugging Face)\n * **v5.x** : the helper is absent from both the current docs and the current `pytorch_utils.py` source, even though nearby helpers such as `apply_chunking_to_forward` and `prune_linear_layer` remain. (Hugging Face)\n\n\n\nThat is why your import fails. The library still has pruning-related helpers, but **this specific helper is gone**. (Hugging Face)\n\n## Why this is happening\n\nTransformers v5 is a major cleanup release. The migration guide explicitly warns that a lot of paths were removed or reworked, and that custom code or remote code can break because old internal import paths no longer exist. That warning is broader than your one symbol, but it matches your situation exactly: code imported a low-level internal helper, then v5 changed the internals. (GitHub)\n\nThis is also not an isolated one-off. There are recent downstream reports of the exact same `find_pruneable_heads_and_indices` import error on Transformers 5.2.0, and other projects have hit similar breakages with adjacent helpers such as `apply_chunking_to_forward` after upgrading Transformers. (GitHub)\n\n## The practical answer\n\n### Best answer for your own code\n\nDo **not** keep searching for a new official import path. I could not verify one in the current docs or source. The cleanest fix is to define the helper locally and import it from your own module instead. (Hugging Face)\n\nUse this compatibility helper:\n\n\n import torch\n\n\n def find_pruneable_heads_and_indices(\n heads: list[int],\n n_heads: int,\n head_size: int,\n already_pruned_heads: set[int],\n ) -> tuple[set[int], torch.LongTensor]:\n \"\"\"\n Finds the heads and the flattened indices to keep, taking already-pruned heads\n into account.\n\n Parameters\n ----------\n heads : list[int]\n Head indices requested for pruning.\n n_heads : int\n Total number of attention heads before this pruning step.\n head_size : int\n Size of each attention head.\n already_pruned_heads : set[int]\n Heads that were pruned in earlier steps.\n\n Returns\n -------\n tuple[set[int], torch.LongTensor]\n (new_heads_to_prune, flattened_indices_to_keep)\n \"\"\"\n mask = torch.ones(n_heads, head_size, dtype=torch.bool)\n\n heads = set(heads) - already_pruned_heads\n\n for head in heads:\n # Shift the head index left by however many smaller heads\n # were already removed earlier.\n shifted_head = head - sum(1 for h in already_pruned_heads if h < head)\n mask[shifted_head] = False\n\n index = torch.arange(n_heads * head_size)[mask.view(-1)].long()\n return heads, index\n\n\nThis matches the old library behavior documented in legacy versions and shown in the v4 source. (Hugging Face)\n\nThen replace:\n\n\n from transformers.pytorch_utils import find_pruneable_heads_and_indices\n\n\nwith:\n\n\n from my_compat import find_pruneable_heads_and_indices\n\n\n## If the failing import is inside a third-party package\n\nThen the package is assuming an older Transformers internal API.\n\nUse one of these two fixes:\n\n### Option A. Patch the package\n\nAdd a fallback import:\n\n\n try:\n from transformers.pytorch_utils import find_pruneable_heads_and_indices\n except ImportError:\n from .my_compat import find_pruneable_heads_and_indices\n\n\n### Option B. Pin Transformers to v4\n\nIf you do not want to patch the package, pin to a v4 release where the helper still exists.\n\n\n pip install \"transformers<5\"\n\n\nor a specific known-good release such as:\n\n\n pip install \"transformers==4.57.1\"\n\n\nThat version still contains the function in `transformers.pytorch_utils`. (GitHub)\n\n## If your real goal is “prune attention heads”\n\nIf you only need head-pruning behavior, importing this helper directly is usually the fragile path. The older docs show it was mainly an internal utility used by model implementations to support `prune_heads` logic. If your model class already exposes a supported pruning method, use that instead of importing the helper. In older model code, the helper is used under the hood inside model attention modules rather than being the main user-facing API. (Hugging Face)\n\n## Why “just downgrade” is not the only answer\n\nYou were right to suspect there should be a better way than blindly downgrading.\n\nThere are really **three** levels of fix:\n\n 1. **Local shim**\nBest when this is your code. Small. Stable. Future-proof.\n\n 2. **Patch the downstream package**\nBest when the error comes from external code you can edit.\n\n 3. **Pin to v4**\nBest when you need the old behavior unchanged and want the least effort.\n\n\n\n\nWhat is **not** a good use of time is hunting for a hidden v5 import path. The current docs and source do not support the idea that the function merely moved. (Hugging Face)\n\n## One subtle background detail\n\nA real-world compatibility commit from September 2025 shows another repo updating imports so this helper came from `transformers.pytorch_utils` instead of older locations, specifically to restore compatibility with newer v4-era Transformers. That fits the historical pattern:\n\n * old code imported from `modeling_utils`\n * later code imported from `pytorch_utils`\n * now in v5, the helper is gone entirely\n\n\n\nSo your failure is the next step in that evolution. (Hugging Face)\n\n## Bottom line\n\nYour import fails because `find_pruneable_heads_and_indices` appears to have been **removed in Transformers v5** , not moved to a new documented module. The best fix is to **vendor the old helper locally** or **pin to Transformers v4** if you need compatibility without patching. (GitHub)",
"title": "ImportError for function find_pruneable_heads_and_indices"
}