Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/models/inkling.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ Add `--model UD-Q3_K_XL` or `--model UD-Q2_K_XL` for a smaller size.
| **UD-Q2_K_XL** | 1- to 2-bit experts: the smallest | 9.1 GB | about 13 GB |

The installer downloads the files under the repository's `Mainline_Support` folder, the layout llama.cpp runs
(`inkling2_loop`). The files at the top of the repository are another layout, which the engine refuses by name.
(`inkling2_loop`). The files at the top of the repository (architecture `inkling2`, for example UD-IQ3_XXS) are
Unsloth's layout of the same weights: the engine reads them too, with `--gguf-dir` or `--model` set to the file.

## How does it work?

Expand Down
15 changes: 6 additions & 9 deletions engine/src/poly/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1868,7 +1868,9 @@ std::unique_ptr<Engine> Engine::open(const Options& o) {
if (o.kv_format == KvFmt::kQ4_0 || o.kv_resident > 0)
throw std::runtime_error("--kv q4_0 and --kv-resident have no Metal kernels yet");
#endif
if (o.kv_format == KvFmt::kFp8 && name != "inkling2_loop")
// Inkling 2: the inkling2_loop files llama.cpp runs, or Unsloth's own layout of the same weights ("inkling2")
const bool inkling = name == "inkling2_loop" || name == "inkling2";
if (o.kv_format == KvFmt::kFp8 && !inkling)
throw std::runtime_error("--kv fp8 is Inkling 2's; this is a '" + name + "' model");
// int8 and int8k are Gemma 4's: their readers are not the arithmetic q8_0's are, so a model takes one
// family or the other rather than a choice between them.
Expand All @@ -1878,9 +1880,9 @@ std::unique_ptr<Engine> Engine::open(const Options& o) {
// moe() weighs the picks as a softmax of their scores, which is how only this architecture's router does
if (o.cache_bias > 0.f && !qwen35)
throw std::runtime_error("--cache-bias is supported for qwen35moe models; this is a '" + name + "' model");
if ((!o.attn_requant.empty() || o.loop_bf16) && name != "inkling2_loop")
if ((!o.attn_requant.empty() || o.loop_bf16) && !inkling)
throw std::runtime_error("--attn-requant and --loop-bf16 are Inkling 2's; this is a '" + name + "' model");
if (o.expert_gpu >= 0 && (name != "inkling2_loop" || !o.layer_split.empty()))
if (o.expert_gpu >= 0 && (!inkling || !o.layer_split.empty()))
throw std::runtime_error("--expert-gpu is Inkling 2's, on one card beside it (no --layer-split); this is a '" +
name + "' model");
if (!o.attn_requant.empty() && o.attn_requant != "q4_0" && o.attn_requant != "q5_0" && o.attn_requant != "iq4_nl")
Expand All @@ -1893,15 +1895,10 @@ std::unique_ptr<Engine> Engine::open(const Options& o) {
if (name == "lfm2moe") return std::make_unique<Lfm2Moe>();
if (name == "flint") return std::make_unique<Flint>();
if (name == "kolibri1") return std::make_unique<Kolibri1>();
if (name == "inkling2_loop") return std::make_unique<Inkling2>();
if (inkling) return std::make_unique<Inkling2>();
#if !defined(STRATA_GPU_METAL)
if (name == "laguna") return std::make_unique<Laguna>();
#endif
// Unsloth's own layout of the model (loop_kv_res_query and the like): no public description of it to follow
if (name == "inkling2")
throw std::runtime_error(o.model + " is an 'inkling2' file, the layout of Unsloth's own runtime; this engine "
"runs the 'inkling2_loop' files llama.cpp runs (the repository's Mainline_Support "
"folder)");
throw std::runtime_error(o.model + " is a '" + name + "' model, which this engine does not run");
};
int cards = 1, devices = 1;
Expand Down
96 changes: 94 additions & 2 deletions engine/src/poly/inkling2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,76 @@ namespace {
constexpr size_t kScoreFloats = size_t(16) << 20; // the attention's scores of the tokens it takes at a time
const char* const kCtrlName[] = {"q", "k", "v", "ffn"};

// Unsloth's top-level files (architecture "inkling2": unsloth/Inkling-2-Mini-GGUF outside its Mainline_Support folder)
// name the loop's parts otherwise. Byte for byte against the inkling2_loop files of the same weights: attn = q,
// mlp = ffn, and the loop's keys are num_loops, loop.core_start and loop.core_end.
const Names kUnslothTensors[] = {
{"loop_q_ctrl_down.weight", {"loop_attn_down.weight", nullptr}},
{"loop_q_ctrl_gain.weight", {"loop_attn_gain.weight", nullptr}},
{"loop_q_ctrl_forget.weight", {"loop_attn_forget.weight", nullptr}},
{"loop_k_ctrl_down.weight", {"loop_k_down.weight", nullptr}},
{"loop_k_ctrl_gain.weight", {"loop_k_gain.weight", nullptr}},
{"loop_k_ctrl_forget.weight", {"loop_k_forget.weight", nullptr}},
{"loop_v_ctrl_down.weight", {"loop_v_down.weight", nullptr}},
{"loop_v_ctrl_gain.weight", {"loop_v_gain.weight", nullptr}},
{"loop_v_ctrl_forget.weight", {"loop_v_forget.weight", nullptr}},
{"loop_ffn_ctrl_down.weight", {"loop_mlp_down.weight", nullptr}},
{"loop_ffn_ctrl_gain.weight", {"loop_mlp_gain.weight", nullptr}},
{"loop_ffn_ctrl_forget.weight", {"loop_mlp_forget.weight", nullptr}},
};
const Names kUnslothKeys[] = {
{"loop.start", {"loop.core_start", nullptr}},
{"loop.end", {"loop.core_end", nullptr}},
{"loop.count", {"num_loops", nullptr}},
};

} // namespace

const Names* Inkling2::tensor_alias(const char* name) const {
if (unsloth_)
for (const Names& n : kUnslothTensors)
if (std::strcmp(n.name, name) == 0) return &n;
return nullptr;
}

const Names* Inkling2::meta_alias(const char* key) const {
if (unsloth_)
for (const Names& n : kUnslothKeys)
if (std::strcmp(n.name, key) == 0) return &n;
return nullptr;
}

// A core layer's message queries, [n_embd, 4] for Q, K, V and the FFN. An inkling2_loop file has them as the layer's
// loop_msg_query; an Unsloth file has two tensors of the model, [n_embd, 2 * core] each: rows 2i and 2i + 1 of
// loop_res_query are core layer start + i's Q and FFN queries, rows 2i and 2i + 1 of loop_kv_res_query its K and V
// ones. The four rows go to the dense arena as upload() puts a tensor there, counted on the first pass of the load.
Dev Inkling2::msg_query(int il) {
const int64_t e = n_embd_;
if (!unsloth_) return upload(tensor(il, "loop_msg_query.weight"), blk(il, "loop_msg_query.weight"), e, kCtrl, kF32);
const TensorInfo* rq = gg_->find("loop_res_query.weight");
const TensorInfo* kq = gg_->find("loop_kv_res_query.weight");
const int64_t rows = 2 * (int64_t) (core_end_ - core_start_);
for (const TensorInfo* t : {rq, kq})
if (!t || t->type != kTypeF32 || t->shape.size() != 2 || (int64_t) t->shape[0] != e ||
(int64_t) t->shape[1] != rows)
throw std::runtime_error(std::string(t ? t->name + " is " + shape_of(*t) : "loop_res_query.weight or "
"loop_kv_res_query.weight is missing") +
", expected F32 [" + std::to_string(e) + ", " + std::to_string(rows) + "]");
const size_t bytes = (size_t) kCtrl * (size_t) e * sizeof(float);
const Dev d{dense_ ? dense_ + dense_at_ : nullptr, kTypeF32, (int) e, kCtrl};
dense_at_ += pad(bytes) + kTail;
if (!dense_) return d;
if (dense_at_ > dense_bytes_) throw std::logic_error("the dense arena is too small for " + blk(il, "loop_msg_query"));
const float* r = (const float*) home(*rq).data(*rq);
const float* k = (const float*) home(*kq).data(*kq);
const size_t i = (size_t) (il - core_start_), row = (size_t) e;
const float* from[kCtrl] = {r + 2 * i * row, k + 2 * i * row, k + (2 * i + 1) * row, r + (2 * i + 1) * row};
std::vector<float> q((size_t) kCtrl * row);
for (int c = 0; c < kCtrl; ++c) std::memcpy(q.data() + (size_t) c * row, from[c], row * sizeof(float));
h2d(d.p, q.data(), bytes);
return d;
}

void Inkling2::begin_pass() {
blocks_.assign(layers_.size(), Block{});
tok_norm_ = out_query_ = Dev{};
Expand All @@ -38,7 +106,17 @@ void Inkling2::load_geometry() {
need(opt_.batch == 0, "Inkling 2 runs one sequence (--batch is not supported for it yet)");
need(opt_.layer_split.empty(), "Inkling 2 runs on one card (--layer-split is not supported for it yet)");
need(opt_.kv_format == KvFmt::kFp16 || opt_.kv_format == KvFmt::kFp8, "Inkling 2's attention caches are fp16 or fp8");
unsloth_ = gg_->get("general.architecture")->s == "inkling2";
load_shape();
if (unsloth_) {
// block_count counts the applications (48), the layers before the core, the core's passes and the layers
// after it; the file has tensors for the 24 layers of weights
const int passes = shape_i("loop.count"), core = shape_i("loop.end") - shape_i("loop.start");
need(passes >= 2 && core > 0 && n_layer_ > (passes - 1) * core, "bad loop");
n_layer_ -= (passes - 1) * core;
need(tensor(n_layer_ - 1, "ffn_norm.weight") && !tensor(n_layer_, "ffn_norm.weight"),
"an inkling2 file's layers are not block_count less the core's later passes");
}
auto find = [&](const char* key) { return meta_in(*gg_, key); };
auto first_u = [&](const char* key) { // a number, or a list's first where every layer has the same
const MetaValue* v = find(key);
Expand Down Expand Up @@ -113,7 +191,21 @@ void Inkling2::load_geometry() {
for (int p = 0; p < (il >= core_start_ && il < core_end_ ? n_pass_ : 1); ++p)
slots_[(size_t) slot_of(il, p)].il = il;
swa_.assign((size_t) n_layer_, false);
for (int il = 0; il < n_layer_; ++il) swa_[(size_t) il] = pattern->items[(size_t) il].u != 0;
// an Unsloth file lists the applications (48 for Inkling 2 Mini), so a layer's value is its first application's;
// index il there would be right only while the pattern's period divides the loop's length
for (int il = 0; il < n_layer_; ++il) {
const size_t v = unsloth_ ? (size_t) visit_of(il, 0) : (size_t) il;
need(v < pattern->items.size(), "attention.sliding_window_pattern has no value for every layer");
swa_[(size_t) il] = pattern->items[v].u != 0;
}
// and every other application must be its layer's
if (unsloth_)
for (int il = 0; il < n_layer_; ++il)
for (int p = 0; p < (il >= core_start_ && il < core_end_ ? n_pass_ : 1); ++p) {
const size_t v = (size_t) visit_of(il, p);
need(v < pattern->items.size() && (pattern->items[v].u != 0) == swa_[(size_t) il],
"attention.sliding_window_pattern differs between a layer's applications");
}
if (!opt_.draft_model.empty()) ds_open();
if (opt_.window == 0) opt_.window = ds_file_ ? ds_block_ : 1;
}
Expand Down Expand Up @@ -501,7 +593,7 @@ void Inkling2::load_layer(int il) {
B.ffn_query = f32("ffn_res_query.weight", e);
B.ffn_norm = f32("ffn_norm.weight", e);
if (il >= core_start_ && il < core_end_) {
B.msg_query = f32("loop_msg_query.weight", e, kCtrl);
B.msg_query = msg_query(il);
// a `fix` takes upload() past the file's F32 to the matrices' BF16
auto ctl = [&](const std::string& n, int64_t a, int64_t b) {
return ctrl_bf16_ ? upload(tensor(il, n.c_str()), blk(il, n.c_str()), a, b, kFloatMat,
Expand Down
7 changes: 6 additions & 1 deletion engine/src/poly/inkling2.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// src/poly/inkling2.hpp - Thinking Machines' Inkling 2 with a looped core (llama.cpp architecture "inkling2_loop"):
// Inkling 2 Mini.
// Inkling 2 Mini. Unsloth's top-level files (architecture "inkling2") are the same weights under other names; the
// engine reads them through tensor_alias(), meta_alias() and msg_query() (inkling2.cpp).
//
// 24 layers of weights make 48 layer applications: the layers before the core run once, the core's run
// loop.count times, each pass from the output of the layers before it, and the layers after it once. A layer of a
Expand Down Expand Up @@ -31,6 +32,8 @@ class Inkling2 final : public Engine {
protected:
void load_geometry() override;
void load_layer(int il) override;
const Names* tensor_alias(const char* name) const override;
const Names* meta_alias(const char* key) const override;
void begin_pass() override;
size_t recurrent_floats() const override;
void save_layer(int il, int64_t pos, uint8_t* dst) override;
Expand Down Expand Up @@ -133,6 +136,7 @@ class Inkling2 final : public Engine {
return il < core_start_ ? il : il < core_end_ ? core_start_ + pass * core + il - core_start_
: il + (n_pass_ - 1) * core;
}
Dev msg_query(int il);
void ds_open();
void ds_load();
void ds_commit(int n);
Expand All @@ -152,6 +156,7 @@ class Inkling2 final : public Engine {
float qk_clip_ = 0, rel_clip_ = 0, log_alpha_ = 0, log_floor_ = 1, softcap_ = 0;
float w_scale_ = 0;
uint32_t attn_type_ = 0; ///< --attn-requant's type for the attention's matrices; 0: the file's
bool unsloth_ = false; ///< an "inkling2" file: block_count counts applications, the loop's names are Unsloth's
bool ctrl_bf16_ = false; ///< --loop-bf16: the loop controllers' matrices in BF16
bool kv8_ = false; ///< the attention caches in FP8 E4M3 (load_geometry()); else fp16
size_t kv_row() const { return kv8_ ? ink_kv8_row(n_kv_, head_dim_) : (size_t) n_kv_ * head_dim_ * sizeof(uint16_t); }
Expand Down
9 changes: 6 additions & 3 deletions terminal/data/gguf_fingerprints.json
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,8 @@
"inkling/UD-Q4_K_XL": {
"engine": "strata-poly",
"archs": [
"inkling2_loop"
"inkling2_loop",
"inkling2"
],
"files": [
"Inkling-2-Mini-UD-Q4_K_XL.gguf"
Expand All @@ -898,7 +899,8 @@
"inkling/UD-Q3_K_XL": {
"engine": "strata-poly",
"archs": [
"inkling2_loop"
"inkling2_loop",
"inkling2"
],
"files": [
"Inkling-2-Mini-UD-Q3_K_XL.gguf"
Expand All @@ -914,7 +916,8 @@
"inkling/UD-Q2_K_XL": {
"engine": "strata-poly",
"archs": [
"inkling2_loop"
"inkling2_loop",
"inkling2"
],
"files": [
"Inkling-2-Mini-UD-Q2_K_XL.gguf"
Expand Down
1 change: 1 addition & 0 deletions terminal/serve/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"kolibri1": "kolibri1",
"laguna": "laguna",
"inkling2_loop": "inkling2_loop",
"inkling2": "inkling2_loop", # Unsloth's top-level files: the same engine (inkling2.cpp)
}
# a routed expert's tensors: they stay in the file and are held in VRAM as far as it has room
EXPERT = re.compile(r"\.ffn_(gate|up|down|gate_up)_exps\.weight$")
Expand Down
6 changes: 3 additions & 3 deletions terminal/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -927,8 +927,8 @@ def ms_meta(repo: str, path: str):
# Inkling 2 Mini (llama.cpp's inkling2_loop): 24 layers of weights run as 48, the middle twelve three times, so a
# context token is the rows of the eight full-attention applications ("row_bytes"); the forty window ones keep
# rings. The files are the repository's Mainline_Support ones, the layout llama.cpp runs: the repository names them
# .gguf_file ("remote_suffix") and its top-level files are another layout (architecture inkling2), which the
# engine refuses. The engine keeps one sequence's state for it, so it answers one
# .gguf_file ("remote_suffix"). Its top-level files are Unsloth's layout (architecture inkling2), which the
# engine also reads: a --gguf-dir file of either is taken. The engine keeps one sequence's state for it, so it answers one
# request at a time ("one_request": setup writes "parallel": 1). "vram_beside_gb" is the peak VRAM measured on an RTX 5090 with no expert on the card, a 10,081-token
# prompt read 1,024 tokens at a time at a 32K context (2,916 MiB), plus the rows of the context's other tokens.
"inkling": {"title": "Inkling 2 Mini", "by": "Thinking Machines; quantized by Unsloth",
Expand Down Expand Up @@ -4371,7 +4371,7 @@ def sha256_matches(s: Path, size: int, sha: str) -> bool:
# code by; the families not listed are qwen35moe
POLY_GGUF_ARCHS = {"glm": ("glm5-next", "glm5next"), "deepseek": ("deepseek4",), "lfm25": ("lfm2moe",),
"gemma4": ("gemma4",), "flint": ("flint",), "kolibri": ("kolibri1",), "clover": ("clover_moe",),
"inkling": ("inkling2_loop",)}
"inkling": ("inkling2_loop", "inkling2")}


def gguf_arch(s: Path) -> str:
Expand Down
5 changes: 3 additions & 2 deletions terminal/tools/test_setup_inkling.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ def test_the_files(self):
self.assertEqual(d["ram_gb"], round(setup.poly_ram_need(d, 8.0)), size)

def test_the_engine_s_name_for_its_files(self):
# the repository's top-level files are another layout (general.architecture inkling2), which is not taken
self.assertEqual(setup.POLY_GGUF_ARCHS["inkling"], ("inkling2_loop",))
# the repository's top-level files are Unsloth's layout (general.architecture inkling2), which the engine
# reads too (inkling2.cpp): a --gguf-dir file of either is taken
self.assertEqual(setup.POLY_GGUF_ARCHS["inkling"], ("inkling2_loop", "inkling2"))


class Main(unittest.TestCase):
Expand Down
Loading