* use exact model name
* Update save.py
* Update _utils.py
* Update _utils.py
* Update _utils.py
* Update _utils.py
* print
* Update _utils.py
* Update _utils.py
* Update llama.py
* Update _utils.py
* Update vision.py
* Update _utils.py
* Update _utils.py
* Update _utils.py
* Update _utils.py
* Update _utils.py
* Update _utils.py
* Update _utils.py
* Update _utils.py
* Update loader.py
* accurate_accumulation
* Update loader.py
* Update loader.py
* Update _utils.py
* Update loader.py
* Update loader.py
* Update loader.py
* Update loader.py
* Update pyproject.toml
* Update __init__.py
* Update pyproject.toml
* Update __init__.py
* Update __init__.py
* Fix Triton heuristics
https://github.com/triton-lang/triton/issues/5224
* Update __init__.py
* Update __init__.py
* Update __init__.py
* Update __init__.py
* Xformers
* Update loader.py
* Update loader.py
* Rewind
* Update _utils.py
* Update _utils.py
* requires grad
* Update loader.py
* Update _utils.py
* Update loader.py
* changing model to base_model if peft model is already used
* Improve debugging experience (#1512)
* Create CONTRIBUTING.md (#1472)
Creating contributing guidelines
* Update CONTRIBUTING.md
improved sentence
* Improve logging control in `unsloth_compile_transformers` by conditionally redirecting stdout based on UNSLOTH_DISABLE_LOGGER environment variable
---------
Co-authored-by: Michael Han <107991372+shimmyshimmer@users.noreply.github.com>
Co-authored-by: Nino Risteski <95188570+NinoRisteski@users.noreply.github.com>
* Update loader.py
* Update llama.py
* Update llama.py
* Revert "Update llama.py"
This reverts commit b7ddf962d2.
* Update llama.py
* Update llama.py
* Update llama.py
* Update llama.py
* Update llama.py
* Update llama.py
* Update llama.py
* Update llama.py
* Update llama.py
* Update llama.py
* Update llama.py
* Update llama.py
* Update llama.py
* Auto change is_bfloat16_supported
* Update llama.py
* Force data-type
* Update llama.py
* All attention refactor fix (#1491)
* change initilization of n_heads, n_kv_heads, hidden_size in llama.py
* do the same for cohere, mistral, gemma2, granite
* do the same for flexattention,cohere, mistral, granite
* Update llama.py
* Update llama.py
* Update granite to work with latest post_patch methods (#1502)
* Update granite to work with latest post_patch methods
* Pass position_embeddings for granite even if transformers<4.47
* Update llama.py
---------
Co-authored-by: Daniel Han <danielhanchen@gmail.com>
* Minor fixes for granite models (#1503)
* Update granite.py
Grab residual multiplier directly from layer
* Update llama.py
Version should read >= 4.47.1 as that is the version requiring the changes
* Update granite.py
* Update llama.py
---------
Co-authored-by: Daniel Han <danielhanchen@gmail.com>
* support modelscope models and datasets (#1481)
* support modelscope
* change modelscope args
* remove useless import
* remove useless import
* fix
* wip
* fix
* remove useless code
* add readme
* add some comments
* change print to raise error
* update comment
* Update loader.py
---------
Co-authored-by: Daniel Han <danielhanchen@gmail.com>
* Merge branch 'main' into nightly
* Phi 4
* Update llama.py
* Torch.Cuda Is Available Condition and Warning (#1545)
* check for torch.cuda and triton if available
on my machine(mac m3) the cuda were not available
* Update pyproject.toml
* Update __init__.py
---------
Co-authored-by: Daniel Han <danielhanchen@gmail.com>
* Update mistral.py
* Update mistral.py
* Update _utils.py
* Update _utils.py
* Update _utils.py
* Update _utils.py
* Update _utils.py
* Fix
* Bug fixes
* Update mapper.py
* Add dropout to granite to match HF's implementation (#1557)
Signed-off-by: datta0 <venkatadattasainimmaturi@gmail.com>
* Update llama.py
* Update llama.py
* Bug fixes
* fix: flash_attn_detection_error (#1556)
* fix: flash_attn_detection_error
* Update _utils.py
---------
Co-authored-by: Daniel Han <danielhanchen@gmail.com>
* Update mapper.py
* Update gemma.py
* Update gemma.py
* Update gemma.py
* Update gemma.py
* dim fix
* Update _utils.py
* Torch 2.6 support
* Update llama.py
* Update llama.py
* Update llama.py
* Update llama.py
* Update llama.py
* Update llama.py
* Update llama.py
* Update llama.py
* Update llama.py
* Update llama.py
* Update llama.py
* Update llama.py
* Faster inference?
* Update llama.py
* Update llama.py
* Update utils.py
* Update llama.py
* Update llama.py
* Update utils.py
* Update utils.py
* Update utils.py
* Update utils.py
* Update utils.py
* Update utils.py
* Update utils.py
* Update utils.py
* Update utils.py
* Update utils.py
* Update utils.py
* Update utils.py
* Update utils.py
* Update mapper.py
* Fast Inference via vLLM
* Update llama.py
* Update llama.py
* Update utils.py
* Create rl.py
* PatchRL
* Update rl.py
* Update rl.py
* Update rl.py
* PatchRLStatistics
* Update rl.py
* Update rl.py
* Update rl.py
* Update utils.py
* Update utils.py
* Update rl.py
* Update rl.py
* Update rl.py
* Update rl.py
* Update rl.py
* Update rl.py
* Update rl.py
* Update rl.py
* Update rl.py
* Update rl.py
* Update rl.py
* Update rl.py
* Update rl.py
* Update rl.py
* Update rl.py
* RL metrics
* Update rl.py
* RL metrics
* Update __init__.py
* Update rl.py
* Update rl.py
* Update rl.py
* Update chat_templates.py
* Update mapper.py
* Fp8 cache
* Update llama.py
* Update llama.py
* Update rl.py
* Update rl.py
* Update rl.py
* Update rl.py
* Update rl.py
* Update rl.py
* Update rl.py
* Update rl.py
* Update rl.py
* Update rl.py
* Update rl.py
* Update rl.py
* Update rl.py
* Update rl.py
* Update rl.py
* Update rl.py
* Update rl.py
* Update __init__.py
* Update loader.py
---------
Signed-off-by: datta0 <venkatadattasainimmaturi@gmail.com>
Co-authored-by: Itsuro Tajima <tajima@georepublic.de>
Co-authored-by: Muhammad Osama <muhammadosama1994@gmail.com>
Co-authored-by: Edd <68678137+Erland366@users.noreply.github.com>
Co-authored-by: Michael Han <107991372+shimmyshimmer@users.noreply.github.com>
Co-authored-by: Nino Risteski <95188570+NinoRisteski@users.noreply.github.com>
Co-authored-by: Kareem <81531392+KareemMusleh@users.noreply.github.com>
Co-authored-by: Datta Nimmaturi <datta.nimmaturi@nutanix.com>
Co-authored-by: Z <coffeevampirebusiness@gmail.com>
Co-authored-by: tastelikefeet <58414341+tastelikefeet@users.noreply.github.com>
Co-authored-by: AminWhat <88392440+aminwhat@users.noreply.github.com>
Co-authored-by: Zhe Zhang <2631992879@qq.com>
219 lines
8.6 KiB
Python
219 lines
8.6 KiB
Python
# Copyright 2023-present Daniel Han-Chen & the Unsloth team. All rights reserved.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
import warnings, importlib, sys
|
|
from packaging.version import Version
|
|
import os, re, subprocess, inspect
|
|
import numpy as np
|
|
|
|
# Unsloth currently does not work on multi GPU setups - sadly we are a 2 brother team so
|
|
# enabling it will require much more work, so we have to prioritize. Please understand!
|
|
# We do have a beta version, which you can contact us about!
|
|
# Thank you for your understanding and we appreciate it immensely!
|
|
|
|
# Fixes https://github.com/unslothai/unsloth/issues/1266
|
|
os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python"
|
|
|
|
if "CUDA_VISIBLE_DEVICES" in os.environ:
|
|
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
|
|
devices = os.environ["CUDA_VISIBLE_DEVICES"]
|
|
# Check if there are multiple cuda devices set in env
|
|
if not devices.isdigit():
|
|
first_id = devices.split(",")[0]
|
|
warnings.warn(
|
|
f"Unsloth: 'CUDA_VISIBLE_DEVICES' is currently {devices} \n"\
|
|
"Unsloth currently does not support multi GPU setups - but we are working on it!\n"\
|
|
"Multiple CUDA devices detected but we require a single device.\n"\
|
|
f"We will override CUDA_VISIBLE_DEVICES to first device: {first_id}."
|
|
)
|
|
os.environ["CUDA_VISIBLE_DEVICES"] = str(first_id)
|
|
else:
|
|
# warnings.warn("Unsloth: 'CUDA_VISIBLE_DEVICES' is not set. We shall set it ourselves.")
|
|
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
|
|
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
|
|
pass
|
|
|
|
# Reduce VRAM usage by reducing fragmentation
|
|
# And optimize pinning of memory
|
|
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = \
|
|
"expandable_segments:True,"\
|
|
"roundup_power2_divisions:[32:256,64:128,256:64,>:32]"
|
|
|
|
# [TODO] Check why some GPUs don't work
|
|
# "pinned_use_cuda_host_register:True,"\
|
|
# "pinned_num_register_threads:8"
|
|
|
|
# Hugging Face Hub faster downloads
|
|
if "HF_HUB_ENABLE_HF_TRANSFER" not in os.environ:
|
|
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
|
|
pass
|
|
|
|
# Log Unsloth is being used
|
|
os.environ["UNSLOTH_IS_PRESENT"] = "1"
|
|
|
|
try:
|
|
import torch
|
|
except ModuleNotFoundError:
|
|
raise ImportError(
|
|
"Unsloth: Pytorch is not installed. Go to https://pytorch.org/.\n"\
|
|
"We have some installation instructions on our Github page."
|
|
)
|
|
except Exception as exception:
|
|
raise exception
|
|
pass
|
|
|
|
# We support Pytorch 2
|
|
# Fixes https://github.com/unslothai/unsloth/issues/38
|
|
torch_version = torch.__version__.split(".")
|
|
major_torch, minor_torch = torch_version[0], torch_version[1]
|
|
major_torch, minor_torch = int(major_torch), int(minor_torch)
|
|
if (major_torch < 2):
|
|
raise ImportError("Unsloth only supports Pytorch 2 for now. Please update your Pytorch to 2.1.\n"\
|
|
"We have some installation instructions on our Github page.")
|
|
elif (major_torch == 2) and (minor_torch < 2):
|
|
# Disable expandable_segments
|
|
del os.environ["PYTORCH_CUDA_ALLOC_CONF"]
|
|
pass
|
|
|
|
# First check if CUDA is available ie a NVIDIA GPU is seen
|
|
if not torch.cuda.is_available():
|
|
raise NotImplementedError("Unsloth: No NVIDIA GPU found? Unsloth currently only supports GPUs!")
|
|
|
|
# Fix Xformers performance issues since 0.0.25
|
|
import importlib.util
|
|
from pathlib import Path
|
|
from importlib.metadata import version as importlib_version
|
|
from packaging.version import Version
|
|
try:
|
|
xformers_version = importlib_version("xformers")
|
|
if Version(xformers_version) < Version("0.0.29"):
|
|
xformers_location = importlib.util.find_spec("xformers").origin
|
|
xformers_location = os.path.split(xformers_location)[0]
|
|
cutlass = Path(xformers_location) / "ops" / "fmha" / "cutlass.py"
|
|
|
|
if cutlass.exists():
|
|
with open(cutlass, "r+") as f:
|
|
text = f.read()
|
|
# See https://github.com/facebookresearch/xformers/issues/1176#issuecomment-2545829591
|
|
if "num_splits_key=-1," in text:
|
|
text = text.replace("num_splits_key=-1,", "num_splits_key=None,")
|
|
f.seek(0)
|
|
f.write(text)
|
|
f.truncate()
|
|
print("Unsloth: Patching Xformers to fix some performance issues.")
|
|
pass
|
|
pass
|
|
pass
|
|
pass
|
|
except:
|
|
pass
|
|
pass
|
|
|
|
# Torch 2.4 has including_emulation
|
|
major_version, minor_version = torch.cuda.get_device_capability()
|
|
SUPPORTS_BFLOAT16 = (major_version >= 8)
|
|
|
|
old_is_bf16_supported = torch.cuda.is_bf16_supported
|
|
if "including_emulation" in str(inspect.signature(old_is_bf16_supported)):
|
|
def is_bf16_supported(including_emulation = False):
|
|
return old_is_bf16_supported(including_emulation)
|
|
torch.cuda.is_bf16_supported = is_bf16_supported
|
|
else:
|
|
def is_bf16_supported(): return SUPPORTS_BFLOAT16
|
|
torch.cuda.is_bf16_supported = is_bf16_supported
|
|
pass
|
|
|
|
# For Gradio HF Spaces?
|
|
# if "SPACE_AUTHOR_NAME" not in os.environ and "SPACE_REPO_NAME" not in os.environ:
|
|
import triton
|
|
libcuda_dirs = lambda: None
|
|
if Version(triton.__version__) >= Version("3.0.0"):
|
|
try: from triton.backends.nvidia.driver import libcuda_dirs
|
|
except: pass
|
|
else: from triton.common.build import libcuda_dirs
|
|
|
|
# Try loading bitsandbytes and triton
|
|
import bitsandbytes as bnb
|
|
try:
|
|
cdequantize_blockwise_fp32 = bnb.functional.lib.cdequantize_blockwise_fp32
|
|
libcuda_dirs()
|
|
except:
|
|
warnings.warn(
|
|
"Unsloth: Running `ldconfig /usr/lib64-nvidia` to link CUDA."\
|
|
)
|
|
|
|
if os.path.exists("/usr/lib64-nvidia"):
|
|
os.system("ldconfig /usr/lib64-nvidia")
|
|
elif os.path.exists("/usr/local"):
|
|
# Sometimes bitsandbytes cannot be linked properly in Runpod for example
|
|
possible_cudas = subprocess.check_output(["ls", "-al", "/usr/local"]).decode("utf-8").split("\n")
|
|
find_cuda = re.compile(r"[\s](cuda\-[\d\.]{2,})$")
|
|
possible_cudas = [find_cuda.search(x) for x in possible_cudas]
|
|
possible_cudas = [x.group(1) for x in possible_cudas if x is not None]
|
|
|
|
# Try linking cuda folder, or everything in local
|
|
if len(possible_cudas) == 0:
|
|
os.system("ldconfig /usr/local/")
|
|
else:
|
|
find_number = re.compile(r"([\d\.]{2,})")
|
|
latest_cuda = np.argsort([float(find_number.search(x).group(1)) for x in possible_cudas])[::-1][0]
|
|
latest_cuda = possible_cudas[latest_cuda]
|
|
os.system(f"ldconfig /usr/local/{latest_cuda}")
|
|
pass
|
|
|
|
importlib.reload(bnb)
|
|
importlib.reload(triton)
|
|
try:
|
|
libcuda_dirs = lambda: None
|
|
if Version(triton.__version__) >= Version("3.0.0"):
|
|
try: from triton.backends.nvidia.driver import libcuda_dirs
|
|
except: pass
|
|
else: from triton.common.build import libcuda_dirs
|
|
cdequantize_blockwise_fp32 = bnb.functional.lib.cdequantize_blockwise_fp32
|
|
libcuda_dirs()
|
|
except:
|
|
warnings.warn(
|
|
"Unsloth: CUDA is not linked properly.\n"\
|
|
"Try running `python -m bitsandbytes` then `python -m xformers.info`\n"\
|
|
"We tried running `ldconfig /usr/lib64-nvidia` ourselves, but it didn't work.\n"\
|
|
"You need to run in your terminal `sudo ldconfig /usr/lib64-nvidia` yourself, then import Unsloth.\n"\
|
|
"Also try `sudo ldconfig /usr/local/cuda-xx.x` - find the latest cuda version.\n"\
|
|
"Unsloth will still run for now, but maybe it might crash - let's hope it works!"
|
|
)
|
|
pass
|
|
|
|
# Check for unsloth_zoo
|
|
try:
|
|
unsloth_zoo_version = importlib_version("unsloth_zoo")
|
|
if Version(unsloth_zoo_version) < Version("2025.2.1"):
|
|
try:
|
|
os.system("pip install --upgrade --no-cache-dir --no-deps unsloth_zoo")
|
|
except:
|
|
try:
|
|
os.system("pip install --upgrade --no-cache-dir --no-deps --user unsloth_zoo")
|
|
except:
|
|
raise ImportError("Unsloth: Please update unsloth_zoo via `pip install --upgrade --no-cache-dir --no-deps unsloth_zoo`")
|
|
import unsloth_zoo
|
|
except:
|
|
raise ImportError("Unsloth: Please install unsloth_zoo via `pip install unsloth_zoo`")
|
|
pass
|
|
|
|
from .models import *
|
|
from .save import *
|
|
from .chat_templates import *
|
|
from .tokenizer_utils import *
|
|
from .trainer import *
|
|
|
|
# Patch TRL trainers for backwards compatibility
|
|
_patch_trl_trainer()
|