Skip to content

GGUF LoRA export: read quantization_method case insensitively - #19113

Merged
danielhanchen merged 4 commits into
unslothai:mainfrom
abhishekacharya200:fix/lora-gguf-outtype-case
Nov 24, 2026
Merged

danielhanchen merged 4 commits into
unslothai:mainfrom
abhishekacharya200:fix/lora-gguf-outtype-case

Conversation

@abhishekacharya200

@abhishekacharya200 abhishekacharya200 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

save_pretrained_gguf(save_method = "lora") and push_to_hub_gguf(save_method = "lora") compare quantization_method against the LoRA outtypes (f32, f16, bf16, q8_0, auto) as written. So quantization_method = "Q8_0" or "BF16" logs "LoRA GGUF export does not support quantization_method='Q8_0'; using outtype 'f16'" and writes an f16 adapter instead of the q8_0 or bf16 one that was asked for.

The merged GGUF path in the same functions lowercases each quant name before using it, so "Q8_0" works there. Only the LoRA branch is case sensitive.

Changes:

  • Move the duplicated outtype selection from both functions into one helper, _lora_gguf_outtype, next to _LORA_GGUF_OUTTYPES in unsloth/save.py.
  • The helper lowercases a string before the lookup. Everything else stays the same: a one item list is unwrapped, None and "fast_quantized" still give f16 with no warning, and an unsupported name such as q4_k_m still falls back to f16 with the warning.

Testing:

  • New tests/test_lora_gguf_outtype.py lifts the helper out of save.py with ast, the same way tests/test_gguf_model_basename.py does, so it runs without the GPU stack. It covers upper case names, a one item list or tuple, the valid lower case names, the f16 default and the warning fallback.
  • 16 passed with this change. On main the file fails (the helper does not exist there). Running the old inline logic on main gives Q8_0 -> f16 and BF16 -> f16, both with the warning.
  • ruff check is clean on the changed files.

Follow-up commit

save_pretrained_ggml / push_to_hub_ggml(outtype="Q8_0") go straight to the LoRA GGUF writer, which still raised ValueError: ... (got 'Q8_0'). The writer now lowercases outtype too, so every LoRA GGUF entry point accepts any case. Verified on a Colab L4 with a tiny Qwen3 LoRA: before, "Q8_0" / "BF16" produced an f16 file and save_pretrained_ggml(outtype="Q8_0") raised; after, they write -lora-q8_0.gguf (Q8_0 tensors) and -lora-bf16.gguf (BF16 tensors). Lowercase inputs are unchanged.

save_pretrained_gguf and push_to_hub_gguf with save_method="lora" matched
quantization_method against the LoRA outtypes as written, so "Q8_0" or
"BF16" fell back to f16 with a "does not support" warning. The merged
GGUF path already lowercases quant names.

Move the duplicated selection into _lora_gguf_outtype, lowercase string
input there, and add a test that lifts the helper with ast.
@danielhanchen danielhanchen self-assigned this Nov 24, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-24T12:57:55.165604Z 16b587e Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 16b587ecbc

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

danielhanchen added a commit that referenced this pull request Nov 24, 2026
## Problem

`studio/backend/tests/test_log_bounds.py` fails on main in the backend
`(Python 3.13, g-l)` shard. It also shows on staging runs, for example
for #19113:

```
TestFamiliesAreBounded::test_every_python_written_family_prunes
  these log families are written but never pruned: sglang (sglang-*.log)
TestFamiliesAreBounded::test_a_new_family_cannot_be_added_unnoticed
  new log families: sglang
```

## Cause

#19166 (per-attempt logs for managed SGLang) added the `sglang` family,
and its logs are pruned: `ManagedEngineLog.open` calls
`prune_log_dir(directory, f"{engine}-*.log", protect = path)` after
opening each attempt's file. The guard could not see that, for two
reasons:

- it only reads `run.py`, `log_retention.py`, `llama_cpp.py` and
`lemonade_server.py`;
- it looks for the literal glob `sglang-*.log`, which an f-string never
contains.

The reviewed list also did not name the new family. So this is a guard
gap, not unbounded logs.

## Fix

- `managed_engine_log.py`: prune with the `pattern` it already reads
from `MANAGED_ENGINE_LOG_FAMILIES`. It is the same value, but now the
family table is the one place the glob is defined.
- `test_log_bounds.py`:
- managed engine families are proved by running the code.
`test_a_managed_engine_log_prunes_its_family_on_open` is parametrised
over `MANAGED_ENGINE_LOG_FAMILIES`. It fills a temp studio home with
`DEFAULT_KEEP + 5` old logs and opens a new one, then checks:
    - exactly `DEFAULT_KEEP` remain;
    - the new handle is kept;
    - the oldest is gone.
- the source scan skips these families instead of grepping for their
glob;
- `sglang` joins the reviewed list. A future managed engine still fails
`test_a_new_family_cannot_be_added_unnoticed` until someone adds it
there, and the behavioral test covers it automatically.

## Verification

- `test_log_bounds.py` and `test_managed_engine_logs.py`: 17 passed. On
main, 2 fail.
- Sabotage:
- replacing the `prune_log_dir` call with `pass` fails
`test_a_managed_engine_log_prunes_its_family_on_open[sglang]`;
- adding a `vllm` entry to `MANAGED_ENGINE_LOG_FAMILIES` fails
`test_a_new_family_cannot_be_added_unnoticed`.
@danielhanchen
danielhanchen merged commit 55bafc9 into unslothai:main Nov 24, 2026
7 of 43 checks passed
danielhanchen added a commit that referenced this pull request Nov 24, 2026
## Problem

On runners with 16 GB of RAM, 35 llama.cpp placement tests fail across
`(Python 3.13, g-l)` and `(Python 3.13, m-r)`. The staging runs for
#19113, #19181 and #19244 all show them, for example:

```
test_llama_cpp_placement.py::test_the_moe_cache_stays_off[ram_cannot_pin_experts-]
  assert _load_mode(cmd) == "none"   (None == 'none')
Load mode: keeping mmap; pinning the host-side weights plus the 9.0 GB llama-server grows into
would hold more than 50% of the 15.3 GB of RAM, which the OS cannot page out.
```

They pass on larger runners and locally, so this only shows up on some
runners.

## Cause

#19231 (price llama-server host RAM growth before pinning a spilled
load) is correct. A pinned launch (`--load-mode none`) now also checks
that the pinned host-side weights plus llama-server's growth stay under
a safe share of total RAM. That total comes from
`_host_memory_capacity_mib()`, which reads the real `MemTotal` and the
cgroup limit.

The placement tests fake the GPUs and, where they care, the available
RAM. They never pin the total, so each launch decided against whatever
machine ran it. Reproduced locally by stubbing
`_total_system_memory_mib` to 15667 MiB: 35 failed, 770 passed in the
three files below.

## Fix

- `test_llama_cpp_placement.py` gets an autouse fixture,
`_pinned_host_ram`. It pins `LlamaCppBackend._total_system_memory_mib`
to 256 GiB and `_cgroup_memory_limit_mib` to `None`, so every launch
sees the same host on any runner.
- `test_lookup_table_fit.py` and `test_prompt_cache_sized_to_context.py`
launch through that file's helpers, so they import the fixture.
- Tests that are about host RAM still set their own total in the test
body, which overrides the fixture.

## Verification

- The three files on this branch: 805 passed.
- With the host forced to 15.3 GB (a plugin stubbing
`_total_system_memory_mib`): 805 passed on this branch, and 35 failed on
main.
- All 34 test files that import `test_llama_cpp_placement`: 2365 passed
and 24 skipped.
- One test failed:
`test_cached_gguf_routes.py::test_pipeline_class_guard_passes_every_shipped_family`.
- It fails identically on main, because this host has diffusers
0.41.0.dev0 and the test needs 0.42.0. It is unrelated to this change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants