Update save.py

This commit is contained in:
Daniel Han-Chen 2024-03-19 02:52:23 +11:00
commit 2f357feece

View file

@ -32,6 +32,8 @@ __all__ = [
"patch_saving_functions",
]
# Check Kaggle
IS_A_KAGGLE_ENVIRONMENT = "KAGGLE_CONTAINER_NAME" in os.environ
LLAMA_WEIGHTS = (
"self_attn.q_proj", "self_attn.k_proj", "self_attn.v_proj", "self_attn.o_proj",
@ -433,7 +435,7 @@ def unsloth_save_model(
pass
# Check if Kaggle, since only 20GB of Disk space allowed.
if "KAGGLE_CONTAINER_NAME" in os.environ:
if IS_A_KAGGLE_ENVIRONMENT:
# We free up 4GB of space
logger.warning_once(
"Unsloth: Kaggle only allows 20GB of disk space. We need to delete the downloaded\n"\
@ -513,7 +515,7 @@ def unsloth_save_model(
# First check if we're pushing to an organization!
save_directory = save_pretrained_settings["save_directory"]
new_save_directory, new_username = _determine_username(save_directory, username)
new_save_directory, new_username = _determine_username(save_directory, username, token)
# Check if pushing to an organization
if save_pretrained_settings["push_to_hub"] and (username != new_username):
@ -696,7 +698,7 @@ def install_llama_cpp_blocking(use_cuda = True):
"pip install gguf protobuf",
]
if os.path.exists("llama.cpp"): return
for command in commands:
with subprocess.Popen(command, shell = True, stdout = subprocess.PIPE, bufsize = 1) as sp:
for line in sp.stdout:
@ -1029,7 +1031,7 @@ This {model_type} model was trained 2x faster with [Unsloth](https://github.com/
"""
def _determine_username(save_directory, old_username = None):
def _determine_username(save_directory, old_username, token):
username = ""
save_directory = save_directory.lstrip("./")
if "/" not in save_directory:
@ -1059,7 +1061,7 @@ def upload_to_huggingface(
old_username = None,
private = None,
):
save_directory, username = _determine_username(save_directory, old_username)
save_directory, username = _determine_username(save_directory, old_username, token)
from huggingface_hub import create_repo
try:
@ -1187,18 +1189,14 @@ def unsloth_save_pretrained_gguf(
# Non blocking install GGUF first
if not os.path.exists("llama.cpp"):
git_clone = install_llama_cpp_clone_non_blocking()
python_install = install_python_non_blocking(["gguf", "protobuf"])
git_clone.wait()
makefile = install_llama_cpp_make_non_blocking()
new_save_directory, old_username = unsloth_save_model(**arguments)
python_install.wait()
else:
try:
if IS_A_KAGGLE_ENVIRONMENT:
# Kaggle is weird - no blocking installs, and no CUDA?
python_install = install_python_non_blocking(["gguf", "protobuf"])
python_install.wait()
install_llama_cpp_blocking(use_cuda = False)
new_save_directory, old_username = unsloth_save_model(**arguments)
makefile = None
except:
# Retry by recloning llama.cpp
else:
git_clone = install_llama_cpp_clone_non_blocking()
python_install = install_python_non_blocking(["gguf", "protobuf"])
git_clone.wait()
@ -1206,6 +1204,27 @@ def unsloth_save_pretrained_gguf(
new_save_directory, old_username = unsloth_save_model(**arguments)
python_install.wait()
pass
else:
try:
new_save_directory, old_username = unsloth_save_model(**arguments)
makefile = None
except:
# Retry by recloning llama.cpp
if IS_A_KAGGLE_ENVIRONMENT:
# Kaggle is weird - no blocking installs, and no CUDA?
python_install = install_python_non_blocking(["gguf", "protobuf"])
python_install.wait()
install_llama_cpp_blocking(use_cuda = False)
new_save_directory, old_username = unsloth_save_model(**arguments)
else:
git_clone = install_llama_cpp_clone_non_blocking()
python_install = install_python_non_blocking(["gguf", "protobuf"])
git_clone.wait()
makefile = install_llama_cpp_make_non_blocking()
new_save_directory, old_username = unsloth_save_model(**arguments)
python_install.wait()
pass
pass
pass
for _ in range(3):
@ -1288,24 +1307,41 @@ def unsloth_push_to_hub_gguf(
# Non blocking install GGUF first
if not os.path.exists("llama.cpp"):
git_clone = install_llama_cpp_clone_non_blocking()
python_install = install_python_non_blocking(["gguf", "protobuf"])
git_clone.wait()
makefile = install_llama_cpp_make_non_blocking()
new_save_directory, old_username = unsloth_save_model(**arguments)
python_install.wait()
if IS_A_KAGGLE_ENVIRONMENT:
# Kaggle is weird - no blocking installs, and no CUDA?
python_install = install_python_non_blocking(["gguf", "protobuf"])
python_install.wait()
install_llama_cpp_blocking(use_cuda = False)
new_save_directory, old_username = unsloth_save_model(**arguments)
else:
git_clone = install_llama_cpp_clone_non_blocking()
python_install = install_python_non_blocking(["gguf", "protobuf"])
git_clone.wait()
makefile = install_llama_cpp_make_non_blocking()
new_save_directory, old_username = unsloth_save_model(**arguments)
python_install.wait()
pass
else:
try:
new_save_directory, old_username = unsloth_save_model(**arguments)
makefile = None
except:
# Retry by recloning llama.cpp
git_clone = install_llama_cpp_clone_non_blocking()
python_install = install_python_non_blocking(["gguf", "protobuf"])
git_clone.wait()
makefile = install_llama_cpp_make_non_blocking()
new_save_directory, old_username = unsloth_save_model(**arguments)
python_install.wait()
if IS_A_KAGGLE_ENVIRONMENT:
# Kaggle is weird - no blocking installs, and no CUDA?
python_install = install_python_non_blocking(["gguf", "protobuf"])
python_install.wait()
install_llama_cpp_blocking(use_cuda = False)
new_save_directory, old_username = unsloth_save_model(**arguments)
else:
git_clone = install_llama_cpp_clone_non_blocking()
python_install = install_python_non_blocking(["gguf", "protobuf"])
git_clone.wait()
makefile = install_llama_cpp_make_non_blocking()
new_save_directory, old_username = unsloth_save_model(**arguments)
python_install.wait()
pass
pass
pass