Skip to content

[Bug] Unsloth Studio: Local safetensors model loads in full precision (high VRAM) instead of 4-bit QLoRA, but loading via GGUF ID works correctly #5344

Closed

Description

@tiegangwang

Describe the bug
When I load a local safetensors model directly from disk using QLoRA (4-bit) method in Unsloth Studio, the model appears to load in full BF16/FP16 precision, consuming ~15.7 GB VRAM (near the original model size). This prevents training due to OOM. However, previously when I specified a GGUF model ID (e.g., from Hugging Face), Unsloth Studio automatically downloaded the corresponding safetensors and loaded it in 4‑bit correctly (low VRAM).

To Reproduce

Have a local folder containing a complete safetensors model (sharded .safetensors files, config.json, tokenizer files, etc.).

Open Unsloth Studio.

In the model selection, choose Local Model and point to that folder.

Set Method to QLoRA (4-bit).

Observe GPU memory usage after loading (e.g., via Task Manager or nvidia-smi).

Expected behavior
The model should be loaded in 4‑bit quantized form, using significantly less VRAM (like ~5-6 GB for a 7B model), allowing training with QLoRA.

Actual behavior
VRAM usage is ~15.7 GB (close to the BF16 model size), indicating 4‑bit loading did not take effect.

Screenshots / Logs

GPU memory usage: 15.7 GB / 23.9 GB (dedicated 7.2/8.0 GB already saturated)
Local folder contents: all necessary files present (config.json, model-*.safetensors, tokenizer.json, etc.)

Environment

Unsloth Studio version: (2026.5.2)

GPU: (e.g., RTX 4060Ti 8GB)

OS: Windows 11

Installation method: (e.g., direct download / winget / source)

Possible cause
When a GGUF ID is used, Unsloth Studio internally triggers a proper 4‑bit loading pipeline (e.g., load_in_4bit=True). When a local path is given, it may fall back to a simple from_pretrained without the 4‑bit flag.

Request
Please advise if there is a configuration file change or a hidden option to force 4‑bit loading for local safetensors, or fix this behavior so that QLoRA (4-bit) method always respects 4‑bit loading regardless of model source (local vs HF ID).

Activity

  1. rolandtannous commented on May 9, 2026

    @rolandtannous
    Contributor

    @tiegangwang what is the model if you don't mind or what is the base model in this case? (llama3.2 ? gemma3? etc)
    also GGUF cannot be trained so that is not relevant here.

  2. Michal-Zablocki commented on May 9, 2026

    @Michal-Zablocki

    Hi, I've been having a similar issue for the last few days on a similar setup - maybe I can post it here.

    I've been unable to load the full version of gemma-4-e2b in Unsloth Studio (2026.5.2 version as well), Windows 11 on my RTX 4060 (8GB VRAM) despite the tooltip suggesting only 6 GB should be enough for just loading the full model to chat - see the screen attached:

    Image

    I've been obtaining the error:

    ValueError: Some modules are dispatched on the CPU or the disk. Make sure you have enough GPU RAM to fit the quantized model. If you want to dispatch the model on the CPU or the disk while keeping these modules in 32-bit, you need to set llm_int8_enable_fp32_cpu_offload=True and pass a custom device_map to from_pretrained. Check https://huggingface.co/docs/transformers/main/en/main_classes/quantization#offload-between-cpu-and-gpu for more details.

    It took me a while to figure out what, well, essentially is written in the message; this is the code where I succeeded in running the model locally:

    import torch
    
    model_name = "unsloth/gemma-4-e2b-it"
    
    model, tokenizer = FastLanguageModel.from_pretrained(
        model_name=model_name,
        max_seq_length=512,
        load_in_4bit=True,
        full_finetuning=False,
        device_map="cuda:0",
    )
    
    print("Model loaded successfully!")
    

    dump from the output:

    🦥 Unsloth: Will patch your computer to enable 2x faster free finetuning.
    W0509 13:46:51.803000 17412 Lib\site-packages\torch\distributed\elastic\multiprocessing\redirects.py:29] NOTE: Redirects are currently not supported in Windows or MacOs.
    Unsloth: Your Flash Attention 2 installation seems to be broken. Using Xformers instead. No performance changes will be seen.
    🦥 Unsloth Zoo will now patch everything to make training faster!
    ==((====))==  Unsloth 2026.5.2: Fast Gemma4 patching. Transformers: 5.5.0.
       \\   /|    NVIDIA GeForce RTX 4060 Laptop GPU. Num GPUs = 1. Max memory: 7.996 GB. Platform: Windows.
    O^O/ \_/ \    Torch: 2.10.0+cu130. CUDA: 8.9. CUDA Toolkit: 13.0. Triton: 3.6.0
    \        /    Bfloat16 = TRUE. FA [Xformers = None. FA2 = False]
     "-____-"     Free license: http://github.com/unslothai/unsloth
    Unsloth: Fast downloading is enabled - ignore downloading bars which are red colored!
    Loading weights: 100%|███████████████████████████████████████████████████████████████████████| 2011/2011 [00:07<00:00, 270.66it/s]
    generation_config.json: 100%|████████████████████████████████████████████████████████████████████| 203/203 [00:00<00:00, 1.14MB/s]
    processor_config.json: 1.69kB [00:00, 7.45MB/s]
    chat_template.jinja: 16.8kB [00:00, 41.4MB/s]
    tokenizer_config.json: 19.9kB [00:00, 30.4MB/s]
    tokenizer.json: 100%|████████████████████████████████████████████████████████████████████████| 32.2M/32.2M [00:02<00:00, 10.7MB/s]
    Model loaded successfully!
    

    nvidia-smi:

    +-----------------------------------------------------------------------------------------+
    | NVIDIA-SMI 596.36                 Driver Version: 596.36         CUDA Version: 13.2     |
    +-----------------------------------------+------------------------+----------------------+
    

    In Cloud, I've been running into issues trying to fine-tune gemma-4-e4b version on this notebook - https://colab.research.google.com/github/unslothai/unsloth/blob/main/studio/Unsloth_Studio_Colab.ipynb

    It ran fine once when I trained it on a small dataset (yahma/alpaca-cleaned), when I try any bigger dataset around 1 GB - e.g. unsloth/LaTeX_OCR it gets over system RAM(?) and crashes with no specific error/no info in logs - see the screen below:

    Image

    Thanks for your help

  3. tiegangwang commented on May 10, 2026

    @tiegangwang
    Author

    @Michal-Zablocki thanks, i will try.

  4. DevInsilicon commented on May 13, 2026

    @DevInsilicon

    I've had a similar issue, but with the regular unsloth python api inside Jupyter notebooks. I am specifying to load in 4 bit with gemma-4-26B-A4B results in 50gb of vram usage which is eerily similar to the full precision model size which is 51gb. (It should be using ~16-20 gb in 4 bit), I specify to load in 4 bit and or 8 bit and it still uses 50gb of vram. This seems like a underlying issue where it is ignoring when you specify load in 4 bit / 8 bit. I'm currently experimenting with passing pre-quantized models to see if the issue resolves.
    If your wondering what I'm under its
    NVIDIA-SMI 595.58.03 Driver Version: 595.58.03 CUDA Version: 13.2
    Ubuntu 25
    torch 2.11.0
    unsloth 2026.5.2
    unsloth-zoo 2026.5.1
    With dual blackwell gpus.

    Edit: Using a pre-quantized model doesn't seem to fix the full precision loading issue.

  5. danielhanchen commented on May 15, 2026

    @danielhanchen
    Member

    Thanks all for the reports. Some notes after digging into this, plus two PRs.

    What I found

    The thread bundles two different mechanisms that happen to look the same on a VRAM meter.

    Mechanism A: Gemma-4 MoE fused experts are not bitsandbytes-quantizable.

    Reproduced on unsloth/gemma-4-26B-A4B-it (B200, transformers 5.5.0, torch 2.9.1, single GPU): load_in_4bit=True yields ~46 GB resident VRAM. Walking named_parameters() after the load shows the runtime BitsAndBytesConfig IS being applied to every nn.Linear (attention + MLP gates), but ~36 GB of expert weight lives in two fused 3D nn.Parameter tensors on each Gemma4TextExperts:

    28.36 GB   model.language_model.layers.N.experts.gate_up_proj   (128, 1408, 2816)  BF16
     4.25 GB   model.language_model.layers.N.experts.down_proj      (128, 2816, 704)   BF16
    

    That layout exists so torch._grouped_mm can dispatch one grouped matmul per layer. bitsandbytes.replace_with_bnb_linear only swaps nn.Linear instances, so the fused expert weights stay BF16 and dominate the footprint. This explains @DevInsilicon's "50 GB at 4-bit on dual Blackwell" report exactly.

    Mechanism B: CPU spillover via device_map="sequential" on tight-VRAM Windows.

    For @Michal-Zablocki (gemma-4-e2b-it on RTX 4060 Laptop 8 GB) and the original Studio report on RTX 4060 Ti 8 GB, I could not reproduce on Linux + B200 against byte-identical PyPI unsloth==2026.5.2 code (md5-verified). The reporter's "15.7 GB" is reading total system VRAM including Windows shared system memory (dedicated 7.2/8.0 GB already saturated matches that), and @Michal-Zablocki's explicit device_map="cuda:0" workaround is exactly the signature of accelerate auto-CPU-offloading a 4-bit model that does not fit in 8 GB dedicated VRAM. That is a separate class from Mechanism A.

    PRs

    #5430 adds a post-load guardrail in FastBaseModel.from_pretrained and FastLlamaModel.from_pretrained that catches both Mechanism A (partial bypass: bnb quantized nn.Linear but a large fraction of weight bytes live in non-nn.Linear Parameters outside the skip list) and the total-bypass class (no Linear4bit modules at all). Emits warnings.warn rather than raising so CPU / MLX / AMD-without-bnb backends are not broken. On 26B-A4B without the swap below, it prints:

    Unsloth: load_in_4bit=True is partially applied. bitsandbytes quantized
    ~0.77 GB of nn.Linear weights, but ~42.54 GB of non-nn.Linear floating
    Parameters were left unquantized (e.g. fused MoE expert tensors, custom
    Linear-like wrappers). Examples: model.language_model.layers.0.experts.
    gate_up_proj (torch.bfloat16, (128, 1408, 2816)), ...
    

    7 unit tests cover both branches plus the skip-list, full-finetuning, and no-quant cases.

    #5432 is the actual fix for Mechanism A. Opt-in via UNSLOTH_GEMMA4_MOE_4BIT=1. After the model loads, it walks every Gemma4TextExperts, slices each fused (num_experts, out, in) Parameter into num_experts individual bnb.nn.Linear4bit modules, and patches forward to dispatch per-expert. Trade-off is the loss of torch._grouped_mm throughput in exchange for the VRAM win:

    mode resident VRAM Linear4bit count forward fidelity
    BF16 (full_finetuning=True) 48.08 GB 0 reference
    load_in_4bit=True, no swap ~46 GB 206 partial-quant
    load_in_4bit=True, swap on 14.27 GB 7,886 cosine 0.994 vs BF16

    Default off until the matching per-expert LoRA path lands; that is the gating concern for flipping it on (training, not inference). The swap renames gate_up_proj to gate_up_proj_4bit to avoid colliding with the existing unsloth_zoo grouped-mm LoRA extractor.

    What is NOT in these PRs

    Mechanism B (Windows + 8 GB + accelerate CPU spillover) is not addressed. The right fix there is probably a Studio-side default of device_map="cuda:0" on single-GPU + low-VRAM, or a clearer error when accelerate would auto-CPU-offload a bnb-4bit model. Happy to take that as a separate issue if either of the original reporters can confirm that is what they hit; running print([type(m).__name__ for m in model.model.layers[0].modules()]) after load and checking whether Linear4bit is present will distinguish A from B quickly.

  6. texastoland commented on Aug 29, 2026

    @texastoland

    is this the same issue? I couldn't find it searching quantized image model:

    • system: mbp m1max 32gb
    • model: tried z-image-turbo and ltx2 both ud-q4 from unsloth
    • use case: replace comfy for "fun" testing multimedia models
    • expected: works or at least suggests how to fit them succesfully
    • actual: auto downloads full quant safetensors then reports not enough memory
  7. danielhanchen commented on Oct 11, 2026

    @danielhanchen
    Member

    Fixed! Sorry on the delay. Gemma-4 MoE models like 26B-A4B kept their expert weights in 16-bit because bitsandbytes only quantized regular linear layers; unslothai/unsloth-zoo#659 now quantizes the experts too, so gemma-4-26B-A4B-it with load_in_4bit=True loads at about 14 GB instead of about 46 GB (from a local folder or the Hub ID) and QLoRA trains. Local safetensors folders were already loaded in 4-bit the same way as Hub IDs; the high usage on 8 GB cards came from the model not fitting in dedicated VRAM and spilling over, which now raises a clear error pointing to offload_layers = "auto" instead of the transformers CPU offload message (#13052).
    To update: uv pip install --upgrade unsloth unsloth_zoo --force-reinstall --no-cache-dir (if it's not in the current release yet it'll be in the next one). Unsloth Desktop will have an update banner. For Studio, use unsloth studio update. For Docker, use docker exec <container> unsloth-studio-update. Reopen if it still happens.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions