Skip to content

Inkling 2: merged 16-bit save and GGUF export - #19016

Merged
danielhanchen merged 10 commits into
mainfrom
inkling2-merged-export
Nov 23, 2026
Merged

danielhanchen merged 10 commits into
mainfrom
inkling2-merged-export

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Inkling 2 merged 16-bit save and GGUF export were refused with NotImplementedError (added on purpose in #18812). This adds a dedicated merge so both work.

Why the generic merge cannot work

The released checkpoint uses Inkling's own key names (attn.wq_du, controllers.*) and stores the routed experts as NVFP4 / MXFP8. The generic merge re-reads the original shards, cannot place any LoRA weight on those keys, and would write the base model back unchanged.

What this does

save_inkling2_merged_16bit (in unsloth/models/inkling2.py), used for save_method="merged_16bit" and therefore by save_pretrained_gguf:

  • walks the original shards and maps each key to its transformers name with the model's own conversion mapping;
  • dequantizes the experts with transformers' Inkling2LoopDequantizeExperts;
  • adds the LoRA deltas in float32 (gate_up deltas re-interleaved to the checkpoint's layout);
  • writes the checkpoint back in its original layout with bfloat16 experts and no quantization config, so transformers reloads it and llama.cpp's Inkling 2 converter reads it.

merged_4bit still refuses.

Tests

  • tests/test_inkling2_support.py: updated plus a merge round-trip test; 21 pass on CPU.
  • Tiny fake model on CPU: merged weights equal base plus adapter (max diff 0.016 against an adapter effect of 2.7); llama.cpp and transformers give the same 12 greedy tokens.
  • Tiny fake model, Unsloth 4-bit, Colab T4: merged weights match base plus the saved adapter (0.049 against 1.60, 100% argmax agreement); a q8_0 GGUF export loads in llama.cpp.
  • Real thinkingmachines/Inkling-2-Mini (20.19 GB, needed because merge and GGUF output cannot be judged on random weights), Colab A100 40 GB, 10 training steps: merged save 1116 tensors / 52.78 GB with LoRA targets changed and norms unchanged; q4_k_m GGUF 17.48 GB plus a 0.64 GB image projector, export 1274 s; llama.cpp (CPU build) generates 6.2 tok/s and answers the fibonacci prompt correctly.

Not covered

  • Kaggle 2x T4 cannot hold the GGUF export: it needs about 150 GB of scratch disk and Kaggle's disk went read-only during the second 53 GB write. The notebook points GGUF export to a Colab A100.
  • llama.cpp ran on its CPU build only.

The generic merge re-reads the released checkpoint, whose keys are Inkling's
own (attn.wq_du, controllers.*) and whose experts are NVFP4 / MXFP8, so it
placed none of the LoRA weights and the export was refused.

save_inkling2_merged_16bit walks the original shards, maps each key to its
transformers name with the model's own conversion mapping, dequantizes the
experts with transformers' Inkling2LoopDequantizeExperts, adds the LoRA deltas
in float32 (gate_up deltas re-interleaved to the checkpoint's layout) and
writes the checkpoint back in its original layout with bfloat16 experts and no
quantization config. transformers reloads it, and llama.cpp's Inkling 2
converter reads it, so save_pretrained_gguf works too. merged_4bit still
refuses.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 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-23T15:36:52.665311Z 72d96ea 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 chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9c0a8fba00

ℹ️ 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".

Comment thread unsloth/models/inkling2.py Resolved
Comment thread unsloth/models/inkling2.py Resolved
Comment thread unsloth/models/inkling2.py Resolved
Measure linear and embedding deltas against the original checkpoint for
PiSSA / OLoRA style inits (same conversion as the generic merge), and
subtract the initial snapshot from expert deltas. Collect embedding LoRA
factors from lora_embedding_A / lora_embedding_B instead of skipping them.
Write live biases trained through bias="all" / "lora_only" instead of
copying the checkpoint's.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 068fed1775

ℹ️ 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".

Comment thread unsloth/models/inkling2.py Resolved
Comment thread unsloth/save.py Resolved
LoRA trainable_token_indices keeps the trained embedding rows in PEFT's
TrainableTokensLayer, so the Inkling 2 writer copied the original rows.
Replace them from each active adapter's trainable_tokens_delta, as
merge_and_unload does, and refuse LoRA plus trainable tokens on one weight.

save_pretrained_merged now forwards dtype to the Inkling 2 writer:
float32 writes every floating tensor (experts included) in float32 without
rounding the merged deltas; None keeps the previous output; other dtypes
are refused.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cb2f65817b

ℹ️ 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".

Comment thread unsloth/models/inkling2.py Resolved
A merged Inkling 2 export always writes numbered shards plus an index. When the
target directory already held model.safetensors from an earlier unsharded save,
transformers loaded that file ahead of the index and silently reloaded the old
weights. After the new shards and index are in place, drop model.safetensors,
pytorch_model.bin, its index and numbered shards the new index does not list.
Write the index through a temp file, and refuse to export into the source
checkpoint directory since the writer reads from it.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 38490fb980

ℹ️ 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".

Comment thread unsloth/models/inkling2.py Resolved
If the target directory previously held a LoRA save, adapter_config.json and
adapter_model.safetensors (or adapter_model.bin) stayed next to the merged
shards, and transformers auto-loaded that adapter onto the merged model,
applying the LoRA twice. Drop those files in the same post-write cleanup that
removes stale weights, after the new shards and index are in place.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fc62a59851

ℹ️ 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".

Comment thread unsloth/models/inkling2.py Resolved
create_repo(private = True) leaves an existing public repo public, so the
direct upload in save_inkling2_merged_16bit could publish the merged weights.
Check visibility with _refuse_private_push_to_public_repo right after
create_repo, before merging or uploading, as the staged Hub export does, and
upload to the repo id create_repo resolved.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2f104a5ec1

ℹ️ 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".

Comment thread unsloth/models/inkling2.py Resolved
upload_folder only adds files, so an existing Hub repo kept an older
model.safetensors, pytorch_model.bin or LoRA adapter that would shadow
the merged shards on reload. List the repo and delete those files in the
same commit, matching what the local save already does.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e966ade4a5

ℹ️ 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".

Comment thread unsloth/save.py Resolved
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 72d96ea0a1

ℹ️ 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
danielhanchen merged commit c69ffd7 into main Nov 23, 2026
@danielhanchen
danielhanchen deleted the inkling2-merged-export branch November 23, 2026 17:24
danielhanchen added a commit that referenced this pull request Nov 23, 2026
…le helper (#19133)

## Problem

Two tests are broken on `main`.

1.
**`tests/test_source_read_encoding.py::test_checked_in_file_reads_name_an_encoding`**
is red in `Repo tests (CPU, rest-2)` for every PR based on `main`, for
example #19122:

   ```
Left contains one more item: 'tests/test_inkling2_support.py:633:
read_text()'
   ```

#19016 added `(_ROOT / "unsloth" / "save.py").read_text()`. It reads a
checked-in file with the platform default encoding, which breaks on
Windows as soon as `save.py` gains a non-ASCII byte. The guard is right
and the new line is wrong.

2.
**`tests/test_inkling2_support.py::test_fastmodel_keeps_inkling2_eager_unless_told_otherwise`**
fails wherever the file runs:

   ```
assert re.search('if os\\.environ\\.get\\("UNSLOTH_COMPILE_DISABLE",
"0"\\) == "0": ...', body)
   AssertionError: None
   ```

It still pins the inline assignment that #19073 replaced with the shared
`_set_partial_compile_disable()` helper. CI does not show it yet: the
file skips below transformers 5.24, and the HF=latest legs are held
below 5.24. It fails locally on 5.24.0, so it would turn red as soon as
that cap lifts.

## Fix

Tests only, in `tests/test_inkling2_support.py`:

- Both reads of checked-in source pass `encoding = "utf-8"`:
  - the `save.py` read the guard flags;
- the loader source read through `__file__`, which the guard does not
see.
- The eager-compile test now pins the current shape:
  - the Inkling 2 branch calls `_set_partial_compile_disable()`;
  - the helper sets `partial` from unset, `0` or `partial`;
- the helper keeps a full disable (`1`, `true`) as `1`, so an explicit
user choice is still left alone.

## Verification

| Check | Result |
| --- | --- |
| `test_source_read_encoding.py`, `main` | 1 failed |
| `test_source_read_encoding.py`, this branch | 16 passed |
| `test_inkling2_support.py`, transformers 5.24.0, `main` | 1 failed, 41
passed |
| `test_inkling2_support.py`, transformers 5.24.0, this branch | 42
passed |
| `test_inkling2_support.py`, transformers 5.17 | module skipped as
before |

Sabotage, each with only the product line changed:

- Drop the helper call from the Inkling 2 branch in `loader.py`: the
test fails on the call check.
- Make the helper always set `partial`: the test fails with `assert
'partial' == '1'` for a full disable.
danielhanchen added a commit that referenced this pull request Nov 23, 2026
…19135)

## Problem


`tests/test_runtime_text_encoding.py::test_shipping_code_names_an_encoding`
is red on `main` in `Repo tests (CPU, rest)`, for example on #19122 and
#19127:

```
4 text read/write call sites in shipping code let the operator's locale decide the encoding
  'unsloth/models/inkling2.py:364: open()'
  'unsloth/models/inkling2.py:532: open()'
  'unsloth/models/inkling2.py:537: open()'
  'unsloth/models/inkling2.py:545: open()'
```

## Cause

The guard is right; the new code is wrong.

#19016 (Inkling 2 merged 16-bit save and GGUF export) added four
text-mode `open()` calls with no encoding:

- reading `model.safetensors.index.json` (`index_path`);
- writing the merged index;
- reading `config.json`;
- writing `config.json`.

On Windows, Python uses the locale code page there. A config or index
with a non-ASCII byte then fails to load, or is written in a codec the
next reader does not expect. The guard has been in the repo since
October; the lines all come from `c69ffd79be`.

## Fix

Pass `encoding = "utf-8"` on all four calls in
`unsloth/models/inkling2.py`. Nothing else changes: the JSON written is
the same on a UTF-8 host.

## Verification

- `tests/test_runtime_text_encoding.py`: 9 passed (`main`: 1 failed,
listing exactly these four sites).
- `tests/test_inkling2_support.py` on transformers 5.24.0, including the
merged-save tests: 42 passed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant