From 21db1bec935784a28ec562711767d4f8cdafda10 Mon Sep 17 00:00:00 2001 From: Dan Saunders Date: Fri, 19 Dec 2025 22:09:16 -0500 Subject: [PATCH 1/5] Fix VLM DDP checkpointing (#3751) --- unsloth/models/vision.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/unsloth/models/vision.py b/unsloth/models/vision.py index ed19f587cf..138b1b633a 100644 --- a/unsloth/models/vision.py +++ b/unsloth/models/vision.py @@ -32,6 +32,13 @@ from ..kernels import ( from ._utils import __version__, importlib_version, _prepare_model_for_qat from ._utils import * from ..save import patch_saving_functions +from ..models.loader_utils import is_distributed +from unsloth_zoo.gradient_checkpointing import ( + unpatch_unsloth_gradient_checkpointing, + unpatch_unsloth_smart_gradient_checkpointing, +) +import torch.utils.checkpoint as torch_checkpoint +import transformers.modeling_utils as hf_modeling_utils from peft import LoraConfig, TaskType, get_peft_model as _get_peft_model from peft import PeftModelForCausalLM from transformers import set_seed as transformers_set_seed @@ -1086,10 +1093,27 @@ class FastBaseModel: # Use bfloat16 precision for full finetuning float32_mixed_precision = False + # VLMs can hit DDP "marked ready twice" with re-entrant checkpointing. + # See: https://github.com/unslothai/unsloth/issues/3713. + use_reentrant = not is_distributed() + if not use_reentrant: + # Under DDP, avoid the offloaded/re-entrant checkpoint patch. + unpatch_unsloth_gradient_checkpointing() + unpatch_unsloth_smart_gradient_checkpointing() + # Force native checkpoint to default to non-reentrant for downstream calls. + _orig_checkpoint = torch_checkpoint.checkpoint + + def _nonre_checkpoint(function, *args, **kwargs): + kwargs["use_reentrant"] = False + return _orig_checkpoint(function, *args, **kwargs) + + torch_checkpoint.checkpoint = _nonre_checkpoint + hf_modeling_utils.checkpoint = _nonre_checkpoint + model = prepare_model_for_training( model, use_gradient_checkpointing = use_gradient_checkpointing, - use_reentrant = True, + use_reentrant = use_reentrant, full_finetuning = full_finetuning, train_layernorms = full_finetuning, train_embedding = full_finetuning, From a73853ed9b730c2f01fbcfba92dd9624487055a1 Mon Sep 17 00:00:00 2001 From: Strahinja Stamenkovic Date: Sat, 20 Dec 2025 04:22:56 +0100 Subject: [PATCH 2/5] Enable 4-bit quantization on AMD Radeon GPUs (#3748) * Enable 4-bit quant on Radeon * Fix table centering * Update comments for clarity * Handle failure to import Bitsandbytes * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update device_type.py * Apply suggestion from @danielhanchen * Update device_type.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Daniel Han --- unsloth/device_type.py | 52 ++++++++++++++++++++++++++++++---------- unsloth/models/loader.py | 8 +++---- 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/unsloth/device_type.py b/unsloth/device_type.py index adc09b05df..68038de679 100644 --- a/unsloth/device_type.py +++ b/unsloth/device_type.py @@ -25,7 +25,6 @@ __all__ = [ import torch import functools from unsloth_zoo.utils import Version -import inspect @functools.cache @@ -78,21 +77,50 @@ def get_device_count(): 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 :( +# 4-bit quantization requires a block size of 64 +# this is not supported on AMD Instinct GPUs currently +# | Device Type | Warp Size | Block Size | +# |-----------------|-----------|------------| +# | CUDA | 32 | 64 | +# | Radeon (Navi) | 32 | 64 | +# | Instinct (MI) | 64 | 128 | +# +# Since bitsandbytes 0.49.0, pre-quantized models with 64 blockwise now works +# on Radeon GPUs, but not Instinct MI300x for eg [WIP] +# See https://github.com/bitsandbytes-foundation/bitsandbytes/pull/1748 + ALLOW_PREQUANTIZED_MODELS: bool = True # HSA_STATUS_ERROR_EXCEPTION checks - sometimes AMD fails for BnB ALLOW_BITSANDBYTES: 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 import bitsandbytes - - ALLOW_BITSANDBYTES = Version(bitsandbytes.__version__) > Version("0.48.2.dev0") except: - pass + print( + "Unsloth: `bitsandbytes` is not installed - 4bit QLoRA unallowed, but 16bit and full finetuning works." + ) + ALLOW_PREQUANTIZED_MODELS = False + ALLOW_BITSANDBYTES = False + if ALLOW_BITSANDBYTES: + ALLOW_BITSANDBYTES = Version(bitsandbytes.__version__) > Version("0.48.2.dev0") + if Version(bitsandbytes.__version__) > Version("0.49.0"): + try: + # Pre-quantized bitsandbytes models use blocksize 64, so we need to check the GPU + from bitsandbytes.cextension import ROCM_WARP_SIZE_64 + + ALLOW_PREQUANTIZED_MODELS = not ROCM_WARP_SIZE_64 + except Exception as e: + print( + "Unsloth: Checking `from bitsandbytes.cextension import ROCM_WARP_SIZE_64` had error = \n" + f"{str(e)}\n" + "4bit QLoRA disabled for now, but 16bit and full finetuning works." + ) + ALLOW_PREQUANTIZED_MODELS = False + ALLOW_BITSANDBYTES = False + elif ALLOW_BITSANDBYTES: + from bitsandbytes.nn.modules import Params4bit + + if "blocksize = 64 if not HIP_ENVIRONMENT else 128" in inspect.getsource( + Params4bit + ): + ALLOW_PREQUANTIZED_MODELS = False diff --git a/unsloth/models/loader.py b/unsloth/models/loader.py index 6148b1783e..a1e5756060 100644 --- a/unsloth/models/loader.py +++ b/unsloth/models/loader.py @@ -249,7 +249,7 @@ class FastLanguageModel(FastLlamaModel): model_name = new_model_name # Check if pre-quantized models are allowed - # For eg AMD GPUs need blocksize = 128, but our pre-quants are blocksize = 64 + # For eg AMD Instinct GPUs need blocksize = 128, but our pre-quants are blocksize = 64 if not ALLOW_PREQUANTIZED_MODELS and model_name.lower().endswith( ("-unsloth-bnb-4bit", "-bnb-4bit") ): @@ -383,7 +383,7 @@ class FastLanguageModel(FastLlamaModel): 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 + # For eg AMD Instinct GPUs need blocksize = 128, but our pre-quants are blocksize = 64 if not ALLOW_PREQUANTIZED_MODELS and model_name.lower().endswith( ("-unsloth-bnb-4bit", "-bnb-4bit") ): @@ -790,7 +790,7 @@ class FastModel(FastBaseModel): model_name = new_model_name # Check if pre-quantized models are allowed - # For eg AMD GPUs need blocksize = 128, but our pre-quants are blocksize = 64 + # For eg AMD Instinct GPUs need blocksize = 128, but our pre-quants are blocksize = 64 if not ALLOW_PREQUANTIZED_MODELS and model_name.lower().endswith( ("-unsloth-bnb-4bit", "-bnb-4bit") ): @@ -1056,7 +1056,7 @@ class FastModel(FastBaseModel): 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 + # For eg AMD Instinct GPUs need blocksize = 128, but our pre-quants are blocksize = 64 if not ALLOW_PREQUANTIZED_MODELS and model_name.lower().endswith( ("-unsloth-bnb-4bit", "-bnb-4bit") ): From 2fdf84096cc02528fc22eacc4a476a60c8c95064 Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Fri, 19 Dec 2025 19:24:49 -0800 Subject: [PATCH 3/5] Update _utils.py --- unsloth/models/_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.py index 17d0ab9631..d769f815bd 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.12.7" +__version__ = "2025.12.8" __all__ = [ "SUPPORTS_BFLOAT16", From cc519c332a0d4af281a0ab78b5580c29974edfc3 Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Fri, 19 Dec 2025 19:35:41 -0800 Subject: [PATCH 4/5] Nightly (#3753) * Update _utils.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * [FIX] [Transformers] VLM input embeds fix for gradients (#3715) * Fix get_input_embeds call for VLMs * patch input_require_grads instead * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * cleanup old patch * cleanup old patch * cleanup * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Apply suggestion from @danielhanchen * use logger instead of prints * Move unsloth present set * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Daniel Han * Update rope_embedding.py * Fixes * Update _utils.py * Update import_fixes.py * Update rl_replacements.py * fix_openenv_no_vllm * Fix * Update __init__.py * Update __init__.py * Update __init__.py * Update import_fixes.py * Update import_fixes.py * Update import_fixes.py * logger * Update __init__.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update __init__.py * Update import_fixes.py * Update __init__.py * Update import_fixes.py * Update import_fixes.py * Update import_fixes.py * Update import_fixes.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update import_fixes.py * Update unsloth/import_fixes.py Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Update save.py * [fbgemm] Silence tma fbgemm (#3735) * Silence fbgemm TMA print Also safer .push_to_hub * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * Update loader.py * Update save.py * Update save.py * Update _utils.py * Update _utils.py * Diffusers warnings * Update pyproject.toml * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * [hf_hub] Token login (#3739) * login on token * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * cleanup old code * safer imports * cleanup * Return token after login * correct return types * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Apply suggestion from @danielhanchen * add back imports * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * finish return token --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Daniel Han * Do not overwrite slots (#3752) * Do not overwrite slots * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Datta Nimmaturi Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- unsloth/import_fixes.py | 2 -- unsloth/models/_utils.py | 21 +++++++++++++++++++++ unsloth/models/llama.py | 3 +-- unsloth/models/loader.py | 23 +++-------------------- unsloth/models/vision.py | 3 +-- 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/unsloth/import_fixes.py b/unsloth/import_fixes.py index f3aae7f523..efc7a7f4cd 100644 --- a/unsloth/import_fixes.py +++ b/unsloth/import_fixes.py @@ -72,8 +72,6 @@ class HideLoggingMessage(logging.Filter): class HidePrintMessage: - __slots__ = ("_original_stream", "_hidden_texts") - def __init__(self, original_stream): self._original_stream = original_stream self._hidden_texts = [] diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.py index d769f815bd..9929277719 100644 --- a/unsloth/models/_utils.py +++ b/unsloth/models/_utils.py @@ -72,6 +72,7 @@ __all__ = [ "patch_hf_quantizer", "verify_fp8_support_if_applicable", "_get_inference_mode_context_manager", + "hf_login", ] import torch @@ -2344,3 +2345,23 @@ def _get_inference_mode_context_manager(model: torch.nn.Module): return torch.no_grad() else: return torch.inference_mode() + + +def hf_login(token: Optional[str] = None) -> Optional[str]: + if token is None: + try: + from huggingface_hub import get_token + + token = get_token() + if token is None: + return None + except: + return None + try: + from huggingface_hub import login + + login(token = token) + return token + except Exception as e: + logger.info(f"Failed to login to huggingface using token with error: {e}") + return token diff --git a/unsloth/models/llama.py b/unsloth/models/llama.py index 4c9337ccf9..1d7695b9aa 100644 --- a/unsloth/models/llama.py +++ b/unsloth/models/llama.py @@ -2130,8 +2130,7 @@ class FastLlamaModel: "Unsloth: `unsloth_vllm_standby` is True, but environment variable `UNSLOTH_VLLM_STANDBY` is not set to 1!" ) - if token is None: - token = get_token() + token = hf_login(token) if model_patcher is None: model_patcher = FastLlamaModel SUPPORTS_BFLOAT16 = is_bfloat16_supported() diff --git a/unsloth/models/loader.py b/unsloth/models/loader.py index a1e5756060..fdfc313732 100644 --- a/unsloth/models/loader.py +++ b/unsloth/models/loader.py @@ -20,6 +20,7 @@ from ._utils import ( HAS_FLASH_ATTENTION_SOFTCAPPING, USE_MODELSCOPE, get_transformers_model_type, + hf_login, ) from .granite import FastGraniteModel from .llama import FastLlamaModel, logger @@ -151,15 +152,7 @@ class FastLanguageModel(FastLlamaModel): **kwargs, ): # Login to allow private models - if token is None: - token = get_token() - if token is not None: - try: - from huggingface_hub import login - - login(token = token) - except: - pass + token = hf_login(token) if load_in_8bit or full_finetuning or qat_scheme is not None: return FastModel.from_pretrained( model_name = model_name, @@ -195,8 +188,6 @@ class FastLanguageModel(FastLlamaModel): **kwargs, ) - if token is None: - token = get_token() if isinstance(dtype, str) and dtype in ["float16", "bfloat16"]: dtype = getattr(torch, dtype) assert ( @@ -687,16 +678,8 @@ class FastModel(FastBaseModel): *args, **kwargs, ): - if token is None: - token = get_token() # Login to allow private models - if token is not None: - try: - from huggingface_hub import login - - login(token = token) - except: - pass + token = hf_login(token) if whisper_language is not None: assert type(whisper_language) is str if whisper_task is not None: diff --git a/unsloth/models/vision.py b/unsloth/models/vision.py index 138b1b633a..b78b190bcb 100644 --- a/unsloth/models/vision.py +++ b/unsloth/models/vision.py @@ -397,8 +397,7 @@ class FastBaseModel: "Unsloth: WARNING `trust_remote_code` is True.\n" "Are you certain you want to do remote code execution?" ) - if token is None: - token = get_token() + token = hf_login(token) SUPPORTS_BFLOAT16 = is_bfloat16_supported() if DEVICE_TYPE == "cuda": From 6bc5bb7404a4218e01b0101981f36509a12c333f Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Fri, 19 Dec 2025 19:37:49 -0800 Subject: [PATCH 5/5] Update loader.py --- unsloth/models/loader.py | 1 + 1 file changed, 1 insertion(+) diff --git a/unsloth/models/loader.py b/unsloth/models/loader.py index fdfc313732..91016a13ba 100644 --- a/unsloth/models/loader.py +++ b/unsloth/models/loader.py @@ -184,6 +184,7 @@ class FastLanguageModel(FastLlamaModel): disable_log_stats = disable_log_stats, qat_scheme = qat_scheme, load_in_fp8 = load_in_fp8, + unsloth_tiled_mlp = unsloth_tiled_mlp, *args, **kwargs, )