Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure Temporary Files (input_embeddings.pt or output_embeddings.pt) Are Saved in Relative Directory #1482

Open
wants to merge 3 commits into
base: nightly
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions unsloth/models/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,8 +735,25 @@ def _prepare_backend(
# Offloading to disk for modules (lm_head, embed_tokens)
import pickle

def is_writeable(path):
"""Check if the given path is writeable."""
try:
testfile = os.path.join(path, ".write_test")
os.makedirs(path, exist_ok=True)
with open(testfile, "w") as f:
f.write("")
os.remove(testfile)
return True
except (OSError, IOError):
return False
pass


def offload_to_disk(W, model, name, temporary_location : str = "_unsloth_temporary_saved_buffers"):
file_location = os.path.join(temporary_location, model.config._name_or_path)
if not is_writeable(file_location):
file_location = os.path.join(temporary_location, os.path.basename(model.config._name_or_path))
pass
if not os.path.exists(file_location):
os.makedirs(file_location)
pass
Expand Down