You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Transformers can create and load python modules that break both of these conventions. This can cause unexpected behavior with code that uses the modules that transformers creates, such as creating, saving, and loading pyTorch traces from disk.
The module name is serialized inside the torch trace. When the trace is loaded again, it fails to parse because the module name of the class does not follow python conventions:
return torch.jit.load(model_path)
cpp_module = torch._C.import_ir_module(cu, str(f), map_location, _extra_files, _restore_shapes) # type: ignore[call-arg]
RuntimeError: expected newline but found 'ident' here:
Serialized File "code/__torch__.py", line 6
training : bool
_is_full_backward_hook : Optional[bool]
model : __torch__.transformers_modules.nomic-ai.nomic-bert-2048.40b98394640e630d5276807046089b233113aa87.modeling_hf_nomic_bert.NomicBertModel
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
def forward(self: __torch__.TransformerWrapper,
input_ids: Tensor,
Expected behavior
The module names created by transformers should be sanitized to follow python convention. I was able to solve this problem with a simple modification:
I am unsure if this is the best fix, or whether it would be considered safe, for the package as a whole, but this does fix the tracing issue I'm hitting:
kory
changed the title
Python code in HF repos can create unconventional python module names that break pyTorch tracing
Python code in HF repos can create unconventional python module names that break pytorch tracing
Jan 8, 2025
kory
changed the title
Python code in HF repos can create unconventional python module names that break pytorch tracing
Transformers can create unconventional python module names when loading certain repositories
Jan 8, 2025
This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.
Please note that issues that do not follow the contributing guidelines are likely to be ignored.
Hi @kory, sorry for missing this PR earlier! I think it got lost in the pile of notifications around Christmas.
It seems like a real issue, and your fix looks reasonable - can you open a PR with it so we can run some tests and review it? It's easier if we have something concrete to start with, even if we end up making changes
kory
linked a pull request
Feb 28, 2025
that will
close
this issue
System Info
transformers
version: 4.41.1Who can help?
@Rocketknight1 (maybe?)
Information
Python module names cannot typically:
Transformers can create and load python modules that break both of these conventions. This can cause unexpected behavior with code that uses the modules that transformers creates, such as creating, saving, and loading pyTorch traces from disk.
Tasks
Load a model from huggingface and trace it.
Reproduction
I try to load, trace, save to disk, and reload the model from this repo: https://huggingface.co/nomic-ai/nomic-bert-2048
The model is loaded in an unconventionally-named python module:
The module name is serialized inside the torch trace. When the trace is loaded again, it fails to parse because the module name of the class does not follow python conventions:
Expected behavior
The module names created by transformers should be sanitized to follow python convention. I was able to solve this problem with a simple modification:
kory@b3fde4f
I am unsure if this is the best fix, or whether it would be considered safe, for the package as a whole, but this does fix the tracing issue I'm hitting:
The text was updated successfully, but these errors were encountered: