Skip to content

Fix dynamic module symlinked cache on trust_remote_code models - #46618

Merged
Rocketknight1 merged 1 commit into
huggingface:mainfrom
ldkhang1201:fix-dynamic-module-symlinked-cache
Jun 17, 2026
Merged

Fix dynamic module symlinked cache on trust_remote_code models#46618
Rocketknight1 merged 1 commit into
huggingface:mainfrom
ldkhang1201:fix-dynamic-module-symlinked-cache

Conversation

@ldkhang1201

Copy link
Copy Markdown
Contributor

What this fixes

#46617
Loading a trust_remote_code model from a local, symlinked cache crashes with:

FileNotFoundError: [Errno 2] No such file or directory:
'.../blobs/transformers_4_44_2__modeling_rope_utils.py'

A standard huggingface_hub cache stores each file twice:

models--org--repo/
  blobs/<sha>                  # real content, content-addressed, hash-named, no .py extension
  snapshots/<rev>/modeling.py  # symlink -> ../../blobs/<sha>

read: https://huggingface.co/docs/huggingface_hub/en/guides/manage-cache

_compute_local_source_files_hash did Path(resolved_module_file).resolve() before calling
get_relative_import_files. That follows the snapshot symlink into blobs/, and
get_relative_import_files then derives sibling import paths from Path(module_file).parent
(now blobs/). The named .py siblings only exist in snapshots/<rev>/, not in blobs/, so the
lookup raises FileNotFoundError.

Fix

  • Run relative-import discovery on the unresolved snapshot path, so traversal stays inside
    snapshots/<rev>/ where the named *.py symlinks live.
  • When building the hash key, canonicalize only the parent directory (to normalize symlinked
    path components such as macOS /var/private/var) and keep the file's logical name.
    read_bytes() still follows the symlink, so content is hashed correctly. For plain
    non-symlinked local dirs the produced hash is unchanged (no cache churn).

Tests

Added a regression test in tests/utils/test_dynamic_module_utils.py that build the real
blobs/ + snapshots/ symlink layout:

  • test_get_cached_module_file_local_handles_symlinked_hub_cache — loads a module with a transitive
    relative-import chain (A→B→C) from a symlinked cache; fails on main with the exact
    FileNotFoundError, passes with the fix, and asserts the transitive deps are copied with correct
    content.
$ python -m pytest tests/utils/test_dynamic_module_utils.py -q
16 passed

@Rocketknight1 Rocketknight1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this looks like it makes sense for symlinked remote code dirs!

@Rocketknight1
Rocketknight1 enabled auto-merge June 15, 2026 17:50
@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@ldkhang1201

Copy link
Copy Markdown
Contributor Author

@Rocketknight1 Hi, could you help me merge the PR. I see auto merge is enable but look like the check pr-ci / PR CI status pending is blocking the merge. How could i unblock that ?

@Rocketknight1

Copy link
Copy Markdown
Member

Working on it!

@Rocketknight1
Rocketknight1 force-pushed the fix-dynamic-module-symlinked-cache branch from b77a326 to 9320453 Compare June 16, 2026 13:12
@ldkhang1201

Copy link
Copy Markdown
Contributor Author

The check is failing because the test: tests/models/kosmos2_5/test_modeling_kosmos2_5.py::Kosmos2_5ModelTest::test_generate_with_cache_matches_no_cache which passed on my machine and in previous runs. @Rocketknight1 glad if you can help, or give me some resources to working on the problem

@Rocketknight1

Copy link
Copy Markdown
Member

This is just our CI, I'll try rebasing/retrying.

@Rocketknight1
Rocketknight1 force-pushed the fix-dynamic-module-symlinked-cache branch from 9320453 to 50b9e41 Compare June 17, 2026 12:04
@Rocketknight1
Rocketknight1 added this pull request to the merge queue Jun 17, 2026
@github-actions

Copy link
Copy Markdown
Contributor

CI Dashboard: View test results in Grafana

@github-actions

Copy link
Copy Markdown
Contributor

View the CircleCI Test Summary for this PR:

https://huggingface.co/spaces/transformers-community/circle-ci-viz?pr=46618&sha=50b9e4

Merged via the queue into huggingface:main with commit 27d8151 Jun 17, 2026
119 of 121 checks passed
@ldkhang1201

Copy link
Copy Markdown
Contributor Author

This is just our CI, I'll try rebasing/retrying.

Tysm for your work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants