Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Fix transformers 4.57.1 (#3473)

Daniel Hancb8484eaf134
diff --git a/pyproject.toml b/pyproject.toml
index 2f812c769f4..6409d74c056 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -39,11 +39,10 @@ triton = [
"triton>=3.0.0 ; ('linux' in sys_platform)",
"triton-windows ; (sys_platform == 'win32') and (platform_machine == 'AMD64' or platform_machine == 'x86_64')",
]
-huggingface = [
- "unsloth_zoo>=2025.10.4",
+huggingfacenotorch = [
+ "unsloth_zoo>=2025.10.5",
"wheel>=0.42.0",
"packaging",
- "torchvision",
"numpy",
"tqdm",
"psutil",
@@ -58,6 +57,10 @@ huggingface = [
"diffusers",
"transformers>=4.51.3,!=4.52.0,!=4.52.1,!=4.52.2,!=4.52.3,!=4.53.0,!=4.54.0,!=4.55.0,!=4.55.1,<=4.56.2",
"trl>=0.7.9,!=0.9.0,!=0.9.1,!=0.9.2,!=0.9.3,!=0.15.0,!=0.19.0,<=0.23.0",
+]
+huggingface = [
+ "unsloth[huggingfacenotorch]",
+ "torchvision",
"unsloth[triton]",
]
windows = [
@@ -458,7 +461,7 @@ colab-ampere-torch220 = [
"flash-attn>=2.6.3 ; ('linux' in sys_platform)",
]
colab-new = [
- "unsloth_zoo>=2025.10.4",
+ "unsloth_zoo>=2025.10.5",
"packaging",
"tyro",
"transformers>=4.51.3,!=4.52.0,!=4.52.1,!=4.52.2,!=4.52.3,!=4.53.0,!=4.54.0,!=4.55.0,!=4.55.1,<=4.56.2",
@@ -740,7 +743,12 @@ intel-gpu-torch270 = [
"torch @ https://download.pytorch.org/whl/xpu/torch-2.7.0%2Bxpu-cp312-cp312-linux_x86_64.whl#sha256=c806d44aa2ca5d225629f6fbc6c994d5deaac2d2cde449195bc8e3522ddd219a ; ('linux' in sys_platform) and python_version == '3.12' and (platform_machine == 'AMD64' or platform_machine == 'x86_64')",
"torch @ https://download.pytorch.org/whl/xpu/torch-2.7.0%2Bxpu-cp313-cp313-linux_x86_64.whl#sha256=25d8277b7f01d42e2e014ccbab57a2692b6ec4eff8dcf894eda1b297407cf97a ; ('linux' in sys_platform) and python_version == '3.13' and (platform_machine == 'AMD64' or platform_machine == 'x86_64')",
]
-
+amd = [
+ "unsloth[huggingfacenotorch]",
+ "bitsandbytes @ https://github.com/bitsandbytes-foundation/bitsandbytes/releases/download/continuous-release_main/bitsandbytes-1.33.7.preview-py3-none-manylinux_2_24_x86_64.whl ; ('linux' in sys_platform) and (platform_machine == 'AMD64' or platform_machine == 'x86_64')",
+ "bitsandbytes @ https://github.com/bitsandbytes-foundation/bitsandbytes/releases/download/continuous-release_main/bitsandbytes-1.33.7.preview-py3-none-win_amd64.whl ; (sys_platform == 'win32') and (platform_machine == 'AMD64' or platform_machine == 'x86_64')",
+ "bitsandbytes @ https://github.com/bitsandbytes-foundation/bitsandbytes/releases/download/continuous-release_main/bitsandbytes-1.33.7.preview-py3-none-manylinux_2_24_aarch64.whl ; ('linux' in sys_platform) and (platform_machine == 'aarch64')",
+]
[project.urls]
homepage = "http://www.unsloth.ai"
documentation = "https://github.com/unslothai/unsloth"
diff --git a/unsloth/__init__.py b/unsloth/__init__.py
index 99d651ae5f4..b336ad03fec 100644
--- a/unsloth/__init__.py
+++ b/unsloth/__init__.py
@@ -69,49 +69,14 @@ except Exception as exception:
raise exception
pass
-def is_hip():
- return bool(getattr(getattr(torch, "version", None), "hip", None))
-pass
-
-def get_device_type():
- if hasattr(torch, "cuda") and torch.cuda.is_available():
- if is_hip():
- return "hip"
- return "cuda"
- elif hasattr(torch, "xpu") and torch.xpu.is_available():
- return "xpu"
- # Check torch.accelerator
- if hasattr(torch, "accelerator"):
- if not torch.accelerator.is_available():
- raise NotImplementedError("Unsloth cannot find any torch accelerator? You need a GPU.")
- accelerator = str(torch.accelerator.current_accelerator())
- if accelerator in ("cuda", "xpu", "hip"):
- raise RuntimeError(
- f"Unsloth: Weirdly `torch.cuda.is_available()`, `torch.xpu.is_available()` and `is_hip` all failed.\n"\
- f"But `torch.accelerator.current_accelerator()` works with it being = `{accelerator}`\n"\
- f"Please reinstall torch - it's most likely broken :("
- )
- raise NotImplementedError("Unsloth currently only works on NVIDIA, AMD and Intel GPUs.")
-pass
-DEVICE_TYPE : str = get_device_type()
-# HIP fails for autocast and other torch functions. Use CUDA instead
-DEVICE_TYPE_TORCH = DEVICE_TYPE
-if DEVICE_TYPE_TORCH == "hip": DEVICE_TYPE_TORCH = "cuda"
-
-def get_device_count():
- if DEVICE_TYPE in ("cuda", "hip"):
- return torch.cuda.device_count()
- elif DEVICE_TYPE == "xpu":
- return torch.xpu.device_count()
- else:
- return 1
-pass
-
-DEVICE_COUNT : int = get_device_count()
-
+from .device_type import (
+ is_hip,
+ get_device_type,
+ DEVICE_TYPE,
+ DEVICE_TYPE_TORCH,
+ DEVICE_COUNT,
+ ALLOW_PREQUANTIZED_MODELS,
+)
# Reduce VRAM usage by reducing fragmentation
# And optimize pinning of memory
# TODO(billishyahao): need to add hip related optimization...
@@ -201,7 +166,10 @@ if DEVICE_TYPE == "cuda":
else: from triton.common.build import libcuda_dirs
# Try loading bitsandbytes and triton
- import bitsandbytes as bnb
+ try:
+ import bitsandbytes as bnb
+ except:
+ print("Unsloth: `bitsandbytes` is not installed - 4bit QLoRA unallowed, but 16bit and full finetuning works!")
try:
cdequantize_blockwise_fp32 = bnb.functional.lib.cdequantize_blockwise_fp32
libcuda_dirs()
diff --git a/unsloth/_amd_install.sh b/unsloth/_amd_install.sh
new file mode 100644
index 00000000000..b83dbd2ad08
--- /dev/null
+++ b/unsloth/_amd_install.sh
@@ -0,0 +1,32 @@
+#!/usr/bin/env bash
+# _amd_install.sh
+# Non-interactive installer: build tools, PyTorch (ROCm 6.4), bitsandbytes (HIP), and Unsloth from source.
+# Usage:
+# bash _amd_install.sh
+#
+
+set -euo pipefail
+export DEBIAN_FRONTEND=noninteractive
+
+apt-get update
+apt-get install -y --no-install-recommends build-essential cmake git
+
+pip install \
+ torch==2.8.0 torchvision torchaudio torchao==0.13.0 xformers \
+ --index-url https://download.pytorch.org/whl/rocm6.4
+
+WORKDIR="$(pwd)"
+TMPDIR="$(mktemp -d)"
+cd "$TMPDIR"
+git clone https://github.com/bitsandbytes-foundation/bitsandbytes.git
+cd bitsandbytes
+arch
+cmake -DCOMPUTE_BACKEND=hip -S .
+make -j"$(nproc)"
+pip install .
+cd "$WORKDIR"
+rm -rf "$TMPDIR"
+
+pip install --no-deps unsloth unsloth-zoo
+pip install "unsloth_zoo[base] @ git+https://github.com/unslothai/unsloth-zoo"
+pip install "unsloth[base] @ git+https://github.com/unslothai/unsloth"
diff --git a/unsloth/device_type.py b/unsloth/device_type.py
new file mode 100644
index 00000000000..547750019a9
--- /dev/null
+++ b/unsloth/device_type.py
@@ -0,0 +1,80 @@
+# 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.
+
+__all__ = [
+ "is_hip",
+ "get_device_type",
+ "DEVICE_TYPE",
+ "DEVICE_TYPE_TORCH",
+ "DEVICE_COUNT",
+ "ALLOW_PREQUANTIZED_MODELS",
+]
+
+import torch
+import functools
+
+def is_hip():
+ return bool(getattr(getattr(torch, "version", None), "hip", None))
+pass
+
+def get_device_type():
+ if hasattr(torch, "cuda") and torch.cuda.is_available():
+ if is_hip():
+ return "hip"
+ return "cuda"
+ elif hasattr(torch, "xpu") and torch.xpu.is_available():
+ return "xpu"
+ # Check torch.accelerator
+ if hasattr(torch, "accelerator"):
+ if not torch.accelerator.is_available():
+ raise NotImplementedError("Unsloth cannot find any torch accelerator? You need a GPU.")
+ accelerator = str(torch.accelerator.current_accelerator())
+ if accelerator in ("cuda", "xpu", "hip"):
+ raise RuntimeError(
+ f"Unsloth: Weirdly `torch.cuda.is_available()`, `torch.xpu.is_available()` and `is_hip` all failed.\n"\
+ f"But `torch.accelerator.current_accelerator()` works with it being = `{accelerator}`\n"\
+ f"Please reinstall torch - it's most likely broken :("
+ )
+ raise NotImplementedError("Unsloth currently only works on NVIDIA, AMD and Intel GPUs.")
+pass
+DEVICE_TYPE : str = get_device_type()
+# HIP fails for autocast and other torch functions. Use CUDA instead
+DEVICE_TYPE_TORCH = DEVICE_TYPE
+if DEVICE_TYPE_TORCH == "hip": DEVICE_TYPE_TORCH = "cuda"
+
+def get_device_count():
+ if DEVICE_TYPE in ("cuda", "hip"):
+ return torch.cuda.device_count()
+ elif DEVICE_TYPE == "xpu":
+ return torch.xpu.device_count()
+ else:
+ return 1
+pass
+
+DEVICE_COUNT : int = get_device_count()
+
+# Check blocksize for 4bit -> 64 for CUDA, 128 for AMD
+# If AMD, we cannot load pre-quantized models for now :(
+ALLOW_PREQUANTIZED_MODELS : bool = True
+if DEVICE_TYPE == "hip":
+ try:
+ from bitsandbytes.nn.modules import Params4bit
+ if "blocksize = 64 if not HIP_ENVIRONMENT else 128" in inspect.getsource(Params4bit):
+ ALLOW_PREQUANTIZED_MODELS = False
+ except:
+ pass
+pass
diff --git a/unsloth/kernels/utils.py b/unsloth/kernels/utils.py
index 9a46d0d5d72..16fc6942309 100644
--- a/unsloth/kernels/utils.py
+++ b/unsloth/kernels/utils.py
@@ -19,7 +19,14 @@ next_power_of_2 = triton.next_power_of_2
import functools
from typing import Optional
-from .. import DEVICE_TYPE, DEVICE_COUNT
+from ..device_type import (
+ is_hip,
+ get_device_type,
+ DEVICE_TYPE,
+ DEVICE_TYPE_TORCH,
+ DEVICE_COUNT,
+ ALLOW_PREQUANTIZED_MODELS,
+)
from .fp8 import weight_dequant, fp8_linear
# torch.cuda.amp.custom_fwd is deprecated >= 2.4
diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.py
index beefada7bf0..e713c8efff6 100644
--- a/unsloth/models/_utils.py
+++ b/unsloth/models/_utils.py
@@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
-__version__ = "2025.10.4"
+__version__ = "2025.10.5"
__all__ = [
"SUPPORTS_BFLOAT16",
@@ -87,7 +87,14 @@ import functools
import warnings, subprocess, re, inspect, psutil, os, math
from unsloth_zoo.utils import Version
from importlib.metadata import version as importlib_version
-from unsloth import DEVICE_TYPE, DEVICE_COUNT, DEVICE_TYPE_TORCH
+from ..device_type import (
+ is_hip,
+ get_device_type,
+ DEVICE_TYPE,
+ DEVICE_TYPE_TORCH,
+ DEVICE_COUNT,
+ ALLOW_PREQUANTIZED_MODELS,
+)
from unsloth_zoo.log import logger
from unsloth_zoo.tokenizer_utils import (
patch_tokenizer as _patch_tokenizer,
@@ -1331,6 +1338,7 @@ pass
def patch_gradient_accumulation_fix(Trainer):
# Fixes gradient accumulation
+ # Fixes Output 0 of UnslothFusedLossBackward is a view and is being modified inplace.
import inspect
if hasattr(Trainer, "get_batch_samples"):
if Trainer.get_batch_samples.__name__ == "_unsloth_get_batch_samples": return
@@ -1346,6 +1354,32 @@ def patch_gradient_accumulation_fix(Trainer):
# Also fix passing in num_items_in_batch
if not hasattr(Trainer, "_old_compute_loss"):
+
+ # Fix transformers 4.57.0 causing `Output 0 of UnslothFusedLossBackward is a view and is being modified inplace.`
+ function = inspect.getsource(Trainer.compute_loss)
+ if "loss *=" in function or "loss*=" in function:
+ where = function.find("def")
+ function = function.split("\n")
+ function = "\n".join(x[where:] for x in function)
+
+ # Import all variables that need importing
+ import transformers.trainer
+ items_in_trainer = dir(transformers.trainer)
+ good_items = []
+ for item in items_in_trainer:
+ if item in function: good_items.append(item)
+ pass
+ exec("from transformers.trainer import (" + ", ".join(x for x in good_items) + ")", globals())
+
+ # Replace loss*= with loss = loss *
+ function = re.sub(
+ r"loss[\s]{0,}\*\=",
+ "loss = loss *",
+ function,
+ )
+ exec(function, globals())
+ Trainer.compute_loss = compute_loss
+ pass
Trainer._old_compute_loss = Trainer.compute_loss
Trainer.compute_loss = _unsloth_pre_compute_loss
pass
diff --git a/unsloth/models/llama.py b/unsloth/models/llama.py
index 596042288dc..535537a3a16 100644
--- a/unsloth/models/llama.py
+++ b/unsloth/models/llama.py
@@ -27,7 +27,14 @@ from transformers import __version__ as transformers_version
from unsloth_zoo.utils import Version, _get_dtype
from unsloth_zoo.hf_utils import dtype_from_config, add_dtype_kwargs, fix_lora_auto_mapping
from unsloth_zoo.peft_utils import SKIP_QUANTIZATION_MODULES
-from unsloth import DEVICE_TYPE, DEVICE_COUNT, DEVICE_TYPE_TORCH
+from ..device_type import (
+ is_hip,
+ get_device_type,
+ DEVICE_TYPE,
+ DEVICE_TYPE_TORCH,
+ DEVICE_COUNT,
+ ALLOW_PREQUANTIZED_MODELS,
+)
transformers_version = Version(transformers_version)
# Transformers moved rotary embeddings out of all attention layers
diff --git a/unsloth/models/loader.py b/unsloth/models/loader.py
index 356bca8a295..5d1896fd5bb 100644
--- a/unsloth/models/loader.py
+++ b/unsloth/models/loader.py
@@ -45,6 +45,14 @@ except:
pass
from huggingface_hub import HfFileSystem
import importlib.util
+from ..device_type import (
+ is_hip,
+ get_device_type,
+ DEVICE_TYPE,
+ DEVICE_TYPE_TORCH,
+ DEVICE_COUNT,
+ ALLOW_PREQUANTIZED_MODELS,
+)
# https://github.com/huggingface/transformers/pull/26037 allows 4 bit loading!
from unsloth_zoo.utils import Version, _get_dtype
@@ -195,6 +203,12 @@ class FastLanguageModel(FastLlamaModel):
old_model_name = model_name
if not use_exact_model_name:
model_name = get_model_name(model_name, load_in_4bit)
+ # Check if pre-quantized models are allowed
+ # For eg AMD GPUs need blocksize = 128, but our pre-quants are blocksize = 64
+ if not ALLOW_PREQUANTIZED_MODELS and model_name.endswith(("-unsloth-bnb-4bit", "-bnb-4bit")):
+ model_name = model_name.removesuffix("-unsloth-bnb-4bit")
+ model_name = model_name.removesuffix("-bnb-4bit")
+ pass
if USE_MODELSCOPE and not os.path.exists(model_name):
from modelscope import snapshot_download
@@ -306,6 +320,12 @@ class FastLanguageModel(FastLlamaModel):
model_name = peft_config.base_model_name_or_path
if not use_exact_model_name:
model_name = get_model_name(model_name, load_in_4bit)
+ # Check if pre-quantized models are allowed
+ # For eg AMD GPUs need blocksize = 128, but our pre-quants are blocksize = 64
+ if not ALLOW_PREQUANTIZED_MODELS and model_name.endswith(("-unsloth-bnb-4bit", "-bnb-4bit")):
+ model_name = model_name.removesuffix("-unsloth-bnb-4bit")
+ model_name = model_name.removesuffix("-bnb-4bit")
+ pass
model_config = AutoConfig.from_pretrained(
model_name,
token = token,
@@ -618,6 +638,12 @@ class FastModel(FastBaseModel):
old_model_name = model_name
if not use_exact_model_name:
model_name = get_model_name(model_name, load_in_4bit)
+ # Check if pre-quantized models are allowed
+ # For eg AMD GPUs need blocksize = 128, but our pre-quants are blocksize = 64
+ if not ALLOW_PREQUANTIZED_MODELS and model_name.endswith(("-unsloth-bnb-4bit", "-bnb-4bit")):
+ model_name = model_name.removesuffix("-unsloth-bnb-4bit")
+ model_name = model_name.removesuffix("-bnb-4bit")
+ pass
# Check modelscope
if USE_MODELSCOPE and not os.path.exists(model_name):
@@ -833,7 +859,12 @@ class FastModel(FastBaseModel):
model_name = peft_config.base_model_name_or_path
if not use_exact_model_name:
model_name = get_model_name(model_name, load_in_4bit)
-
+ # Check if pre-quantized models are allowed
+ # For eg AMD GPUs need blocksize = 128, but our pre-quants are blocksize = 64
+ if not ALLOW_PREQUANTIZED_MODELS and model_name.endswith(("-unsloth-bnb-4bit", "-bnb-4bit")):
+ model_name = model_name.removesuffix("-unsloth-bnb-4bit")
+ model_name = model_name.removesuffix("-bnb-4bit")
+ pass
model_config = AutoConfig.from_pretrained(
model_name,
token = token,
diff --git a/unsloth/models/rl_replacements.py b/unsloth/models/rl_replacements.py
index a207514e729..7ecffdf63d5 100644
--- a/unsloth/models/rl_replacements.py
+++ b/unsloth/models/rl_replacements.py
@@ -26,7 +26,14 @@ import torch
import inspect
from collections import defaultdict
from unsloth_zoo.rl_replacements import RL_REPLACEMENTS, left_pack_padding
-from unsloth import DEVICE_TYPE
+from ..device_type import (
+ is_hip,
+ get_device_type,
+ DEVICE_TYPE,
+ DEVICE_TYPE_TORCH,
+ DEVICE_COUNT,
+ ALLOW_PREQUANTIZED_MODELS,
+)
import textwrap
RL_EXTRA_ARGS = defaultdict(list)
diff --git a/unsloth/models/vision.py b/unsloth/models/vision.py
index b2704876e31..f2bd7c306b0 100644
--- a/unsloth/models/vision.py
+++ b/unsloth/models/vision.py
@@ -71,7 +71,14 @@ except:
# Old HF Hub versions <= 0.0.25
from huggingface_hub.utils._token import get_token
pass
-from unsloth import DEVICE_TYPE, DEVICE_COUNT, DEVICE_TYPE_TORCH
+from ..device_type import (
+ is_hip,
+ get_device_type,
+ DEVICE_TYPE,
+ DEVICE_TYPE_TORCH,
+ DEVICE_COUNT,
+ ALLOW_PREQUANTIZED_MODELS,
+)
__all__ = [
"FastBaseModel",