Closed mszulc913 closed 2 weeks ago
Hi @mszulc913, thanks for opening this issue!
I'm able to replicate the issue.
The model checkpoint didn't have a safetensors weight associated with it. Which is merged in with this commit.
However, the issue still persists :(
It seems like this is an issue when loading as safetensors on the fly.
If instead I save out the model locally from pytorch.bin
and save out as safetensors, I'm able to load without any issue:
from transformers import LayoutLMModel
model = LayoutLMModel.from_pretrained("microsoft/layoutlm-base-uncased", use_safetensors=False)
model.save_pretrained("test-layoutlm-base-uncased") # Saves out the model as safetensors
# Loads from safetensors automatically
model = LayoutLMModel.from_pretrained("test-layoutlm-base-uncased")
cc @LysandreJik @Narsil As you both probably have the best knowledge of this code
cc @Rocketknight1 as you've been looking into the safetensors conversion recently
In SFconvertbot's convert.py
file, the loading of weights happens with -
loaded = torch.load(pt_filename, map_location="cpu", weights_only=True)
, which does not maps the layers correctly (the 'keys' in the weights dictionary are different). This is causing the issue.
If we load the weights using -
from transformers import LayoutLMModel
model = LayoutLMModel.from_pretrained("microsoft/layoutlm-base-uncased", use_safetensors=False)
loaded = {f"layoutlm.{k}":v.data for k, v in model.named_parameters()}
, then the weights are loaded with correct mappings.
This is either Pytorch's issue with load()
function, or the implementation issue with SFconvertbot.
If this issue needs to be fixed somewhere, I can take it up.
Also, I created a PR in microsoft/layoutlm-base-uncased with updated SafeTensors.
@RVV-karma Thanks for looking into this and for fixing the weights upstream ❤️
@Rocketknight1 has been working with safetensor weight loading and the bot recently, so will be able to advise on the best approach here to address for future models.
I actually haven't touched the bot, so I'm not sure how to push a fix to it! @Narsil do you know where it runs?
The bot runs here @Rocketknight1 if you want to open a PR: https://huggingface.co/spaces/safetensors/convert
code is here https://huggingface.co/spaces/safetensors/convert/blob/main/convert.py
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.
System Info
transformers
version: 4.38.1Who can help?
No response
Information
Tasks
examples
folder (such as GLUE/SQuAD, ...)Reproduction
Running the following:
results in:
Note that this is also the default behavior if a user has
safetensors
installed and doesn't provideuse_safetensors
.The following works as expected (without safetensors):
Expected behavior
Embeddings' weights should be correctly loaded.