transformers - 💡(How to fix) Fix from_pretrained loads wrong custom module after save_pretrained [3 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
huggingface/transformers#45698Fetched 2026-04-30 06:18:24
View on GitHub
Comments
3
Participants
2
Timeline
8
Reactions
0
Author
Timeline (top)
commented ×3mentioned ×2subscribed ×2labeled ×1

Root Cause

Note: these setup commands print <MAGIC> because they instantiate the local source file before patching the saved custom_model.py.

Fix Action

Fix / Workaround

Note: these setup commands print <MAGIC> because they instantiate the local source file before patching the saved custom_model.py.

Code Example

python main.py save --path=pretrained_a --magic="Magic A"
python main.py save --path=pretrained_b --magic="Magic B"

---

python main.py load-and-load --path-a=pretrained_a --path-b=pretrained_b

---

Load "pretrained_a"
Model says:  Magic A
Source path: HF_MODULES_CACHE/transformers_modules/pretrained_a/custom_model.py
Source says: Magic A
Load "pretrained_b"
Model says:  Magic B
Source path: HF_MODULES_CACHE/transformers_modules/pretrained_b/custom_model.py
Source says: Magic B

---

python main.py save-and-load --save-path=pretrained_a --save-magic="Magic A" --load-path=pretrained_b

---

Save "pretrained_a" with "Magic A"
Model says:  <MAGIC>
Source path: custom_model.py
Source says: <MAGIC>
Load "pretrained_b"
Model says:  <MAGIC>
Source path: custom_model.py
Source says: <MAGIC>
RAW_BUFFERClick to expand / collapse

System Info

Transformers version: 5.7.0.dev0 Python version: 3.13.3 Platform: Linux-6.17.0-20-generic-x86_64-with-glibc2.39 Machine: x86_64

Note, running transformers env fails with: NameError: name 'CompletionCreateParamsStreaming' is not defined

Who can help?

@CyrilVallez (model loading)

Information

  • The official example scripts
  • My own modified scripts

Tasks

  • An officially supported task in the examples folder (such as GLUE/SQuAD, ...)
  • My own task or dataset (give details below)

Reproduction

With the attached main.py and custom_model.py:

1. Create two local saved models

python main.py save --path=pretrained_a --magic="Magic A"
python main.py save --path=pretrained_b --magic="Magic B"

Note: these setup commands print <MAGIC> because they instantiate the local source file before patching the saved custom_model.py.

2. Control case: loading both models works

python main.py load-and-load --path-a=pretrained_a --path-b=pretrained_b

This correctly prints Magic A from pretrained_a and Magic B from pretrained_b:

Load "pretrained_a"
Model says:  Magic A
Source path: HF_MODULES_CACHE/transformers_modules/pretrained_a/custom_model.py
Source says: Magic A
Load "pretrained_b"
Model says:  Magic B
Source path: HF_MODULES_CACHE/transformers_modules/pretrained_b/custom_model.py
Source says: Magic B

3. Failing case: saving first changes the later load

python main.py save-and-load --save-path=pretrained_a --save-magic="Magic A" --load-path=pretrained_b
Save "pretrained_a" with "Magic A"
Model says:  <MAGIC>
Source path: custom_model.py
Source says: <MAGIC>
Load "pretrained_b"
Model says:  <MAGIC>
Source path: custom_model.py
Source says: <MAGIC>

Actual result: pretrained_b is loaded from local custom_model.py and prints <MAGIC>.

custom_model.py main.py

Expected behavior

Expected result: pretrained_b should be loaded from HF_MODULES_CACHE/transformers_modules/pretrained_b/custom_model.py and print Magic B, like it does in the control case.

extent analysis

TL;DR

The issue can be fixed by ensuring that the custom_model.py file is not modified during the saving process, allowing the subsequent load to use the correct cached version.

Guidance

  • The problem seems to be related to the way the custom_model.py file is being modified and cached during the saving process, causing the later load to use the local version instead of the cached one.
  • To verify this, check the HF_MODULES_CACHE directory to see if the custom_model.py file is being updated correctly for each saved model.
  • One possible workaround is to ensure that the custom_model.py file is not imported or modified during the saving process, allowing the subsequent load to use the correct cached version.
  • Another possible solution is to use a different approach to save and load the models, avoiding the use of the custom_model.py file altogether.

Example

No code snippet is provided as the issue is more related to the workflow and caching mechanism rather than a specific code error.

Notes

The issue seems to be specific to the custom implementation of saving and loading models, and the provided information does not give a clear indication of the root cause. Further investigation into the caching mechanism and the saving process is needed to provide a more accurate solution.

Recommendation

Apply workaround: Avoid modifying the custom_model.py file during the saving process to ensure that the subsequent load uses the correct cached version. This can be done by reorganizing the code to separate the saving and loading processes, or by using a different approach to save and load the models.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

Expected result: pretrained_b should be loaded from HF_MODULES_CACHE/transformers_modules/pretrained_b/custom_model.py and print Magic B, like it does in the control case.

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING