Saving pretrained to same directory as load

I’m trying to fine tune a mask2former model on a semantic segmentation dataset. The codebase is still experimental so I want to train it iteratively - e.g. load, train for 100 epochs, and save to the folder I loaded it from. Both from_pretrained and save_pretrained seem to be working as expected from local folders - however, if I load a model then save it to the same folder:

from transformers import Mask2FormerForUniversalSegmentation as Former

model = Former.from_pretrained("facebook/mask2former-swin-small-ade-semantic") #load from remote - works fine

model.save_pretrained("local_mask2former") #works fine

##later, could be in the same python instance or after the first has closed
model  = Former.from_pretrained("local_mask2former")

#ideally some finetuning here, but even with no code in between:

model.save_pretrained("local_mask2former") #raises SafeTensorError

Which on the last line raises the following error:

safetensors_rust.SafetensorError: Error while serializing: IoError(Os { code: 1224, kind: Uncategorized, message: "The requested operation cannot be performed on a file with a user-mapped section open." })

Same thing happens with maskformer and with AutoModelForCausalLM (random model I pulled up and random). It seems like the model retains some file handle/tie to the original folder from which it was loaded.

I have a workaround that involves first saving the model to some other temporary folder first, load the model AGAIN from there, then save it back to the original, which is a nightmare. This feels like a bug. If someone has a workaround, please let me know; otherwise, I’ll probably put this as a github issue

1 Like

There don’t seem to be any issues with Transoformers or safetensors. I think it would be better to raise an issue.
I think it was working before.

It’s not good that the file pointer isn’t being freed…

1 Like

ok, raised Loading and Saving Pretrained model to the same directory raises SafeTensorError: IOError · Issue #37713 · huggingface/transformers · GitHub

1 Like