@seanhogg/builderforce-memory-engine 2026.7.14 → 2026.9.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (186) hide show
  1. package/README.md +14 -0
  2. package/dist/bench/adaptation.d.ts +57 -0
  3. package/dist/bench/adaptation.d.ts.map +1 -0
  4. package/dist/bench/adaptation.js +167 -0
  5. package/dist/bench/adaptation.js.map +1 -0
  6. package/dist/bench/harness.d.ts +2 -0
  7. package/dist/bench/harness.d.ts.map +1 -1
  8. package/dist/bench/harness.js +1 -1
  9. package/dist/bench/harness.js.map +1 -1
  10. package/dist/bench/index.d.ts +5 -3
  11. package/dist/bench/index.d.ts.map +1 -1
  12. package/dist/bench/index.js +4 -2
  13. package/dist/bench/index.js.map +1 -1
  14. package/dist/bench/types.d.ts +88 -0
  15. package/dist/bench/types.d.ts.map +1 -1
  16. package/dist/evermind/adapt.d.ts +40 -0
  17. package/dist/evermind/adapt.d.ts.map +1 -0
  18. package/dist/evermind/adapt.js +25 -0
  19. package/dist/evermind/adapt.js.map +1 -0
  20. package/dist/evermind/delta_wire.d.ts +43 -0
  21. package/dist/evermind/delta_wire.d.ts.map +1 -0
  22. package/dist/evermind/delta_wire.js +94 -0
  23. package/dist/evermind/delta_wire.js.map +1 -0
  24. package/dist/evermind/eval.d.ts +31 -0
  25. package/dist/evermind/eval.d.ts.map +1 -0
  26. package/dist/evermind/eval.js +36 -0
  27. package/dist/evermind/eval.js.map +1 -0
  28. package/dist/evermind/index.d.ts +16 -0
  29. package/dist/evermind/index.d.ts.map +1 -0
  30. package/dist/evermind/index.js +11 -0
  31. package/dist/evermind/index.js.map +1 -0
  32. package/dist/evermind/merge.d.ts +21 -0
  33. package/dist/evermind/merge.d.ts.map +1 -0
  34. package/dist/evermind/merge.js +79 -0
  35. package/dist/evermind/merge.js.map +1 -0
  36. package/dist/{moe/moe_package.d.ts → evermind/package.d.ts} +55 -5
  37. package/dist/evermind/package.d.ts.map +1 -0
  38. package/dist/{moe/moe_package.js → evermind/package.js} +118 -19
  39. package/dist/evermind/package.js.map +1 -0
  40. package/dist/import/foreign/adapter.d.ts +59 -0
  41. package/dist/import/foreign/adapter.d.ts.map +1 -0
  42. package/dist/import/foreign/adapter.js +76 -0
  43. package/dist/import/foreign/adapter.js.map +1 -0
  44. package/dist/import/foreign/apply.d.ts +23 -0
  45. package/dist/import/foreign/apply.d.ts.map +1 -0
  46. package/dist/import/foreign/apply.js +43 -0
  47. package/dist/import/foreign/apply.js.map +1 -0
  48. package/dist/import/foreign/codestral_mamba.d.ts +57 -0
  49. package/dist/import/foreign/codestral_mamba.d.ts.map +1 -0
  50. package/dist/import/foreign/codestral_mamba.js +174 -0
  51. package/dist/import/foreign/codestral_mamba.js.map +1 -0
  52. package/dist/import/foreign/falcon_mamba.d.ts +51 -0
  53. package/dist/import/foreign/falcon_mamba.d.ts.map +1 -0
  54. package/dist/import/foreign/falcon_mamba.js +131 -0
  55. package/dist/import/foreign/falcon_mamba.js.map +1 -0
  56. package/dist/import/foreign/index.d.ts +21 -0
  57. package/dist/import/foreign/index.d.ts.map +1 -0
  58. package/dist/import/foreign/index.js +18 -0
  59. package/dist/import/foreign/index.js.map +1 -0
  60. package/dist/import/foreign/plan.d.ts +122 -0
  61. package/dist/import/foreign/plan.d.ts.map +1 -0
  62. package/dist/import/foreign/plan.js +171 -0
  63. package/dist/import/foreign/plan.js.map +1 -0
  64. package/dist/import/foreign/registry.d.ts +35 -0
  65. package/dist/import/foreign/registry.d.ts.map +1 -0
  66. package/dist/import/foreign/registry.js +89 -0
  67. package/dist/import/foreign/registry.js.map +1 -0
  68. package/dist/import/index.d.ts +7 -0
  69. package/dist/import/index.d.ts.map +1 -1
  70. package/dist/import/index.js +7 -0
  71. package/dist/import/index.js.map +1 -1
  72. package/dist/index.d.ts +25 -12
  73. package/dist/index.d.ts.map +1 -1
  74. package/dist/index.js +21 -9
  75. package/dist/index.js.map +1 -1
  76. package/dist/kernels/selective_scan.d.ts.map +1 -1
  77. package/dist/kernels/selective_scan.js +15 -9
  78. package/dist/kernels/selective_scan.js.map +1 -1
  79. package/dist/kernels/slice.d.ts +26 -0
  80. package/dist/kernels/slice.d.ts.map +1 -0
  81. package/dist/kernels/slice.js +52 -0
  82. package/dist/kernels/slice.js.map +1 -0
  83. package/dist/kernels/weight_update.d.ts.map +1 -1
  84. package/dist/kernels/weight_update.js +30 -4
  85. package/dist/kernels/weight_update.js.map +1 -1
  86. package/dist/lm/evermind_lm.d.ts +110 -1
  87. package/dist/lm/evermind_lm.d.ts.map +1 -1
  88. package/dist/lm/evermind_lm.js +220 -35
  89. package/dist/lm/evermind_lm.js.map +1 -1
  90. package/dist/lm/index.d.ts +4 -2
  91. package/dist/lm/index.d.ts.map +1 -1
  92. package/dist/lm/index.js +3 -1
  93. package/dist/lm/index.js.map +1 -1
  94. package/dist/lm/text_embedder.d.ts +55 -0
  95. package/dist/lm/text_embedder.d.ts.map +1 -0
  96. package/dist/lm/text_embedder.js +62 -0
  97. package/dist/lm/text_embedder.js.map +1 -0
  98. package/dist/model/attention_block.d.ts.map +1 -1
  99. package/dist/model/attention_block.js +9 -5
  100. package/dist/model/attention_block.js.map +1 -1
  101. package/dist/model/mamba1_block.d.ts.map +1 -1
  102. package/dist/model/mamba1_block.js +28 -12
  103. package/dist/model/mamba1_block.js.map +1 -1
  104. package/dist/model/mamba2_block.d.ts.map +1 -1
  105. package/dist/model/mamba2_block.js +14 -9
  106. package/dist/model/mamba2_block.js.map +1 -1
  107. package/dist/model/mamba3_block.d.ts.map +1 -1
  108. package/dist/model/mamba3_block.js +14 -9
  109. package/dist/model/mamba3_block.js.map +1 -1
  110. package/dist/moe/index.d.ts +0 -2
  111. package/dist/moe/index.d.ts.map +1 -1
  112. package/dist/moe/index.js +0 -1
  113. package/dist/moe/index.js.map +1 -1
  114. package/dist/optim/adamw.d.ts +32 -0
  115. package/dist/optim/adamw.d.ts.map +1 -1
  116. package/dist/optim/adamw.js +34 -0
  117. package/dist/optim/adamw.js.map +1 -1
  118. package/dist/tokenizer/bpe.d.ts +28 -0
  119. package/dist/tokenizer/bpe.d.ts.map +1 -1
  120. package/dist/tokenizer/bpe.js +47 -0
  121. package/dist/tokenizer/bpe.js.map +1 -1
  122. package/dist/training/cpu_ops.d.ts +44 -0
  123. package/dist/training/cpu_ops.d.ts.map +1 -0
  124. package/dist/training/cpu_ops.js +111 -0
  125. package/dist/training/cpu_ops.js.map +1 -0
  126. package/dist/training/mamba1_cpu.d.ts +109 -0
  127. package/dist/training/mamba1_cpu.d.ts.map +1 -0
  128. package/dist/training/mamba1_cpu.js +316 -0
  129. package/dist/training/mamba1_cpu.js.map +1 -0
  130. package/dist/training/model_cpu.d.ts +100 -0
  131. package/dist/training/model_cpu.d.ts.map +1 -0
  132. package/dist/training/model_cpu.js +252 -0
  133. package/dist/training/model_cpu.js.map +1 -0
  134. package/dist/training/trainer.d.ts +156 -16
  135. package/dist/training/trainer.d.ts.map +1 -1
  136. package/dist/training/trainer.js +327 -101
  137. package/dist/training/trainer.js.map +1 -1
  138. package/dist/utils/base64.d.ts +12 -0
  139. package/dist/utils/base64.d.ts.map +1 -0
  140. package/dist/utils/base64.js +27 -0
  141. package/dist/utils/base64.js.map +1 -0
  142. package/package.json +3 -1
  143. package/src/bench/adaptation.ts +180 -0
  144. package/src/bench/harness.ts +1 -1
  145. package/src/bench/index.ts +13 -2
  146. package/src/bench/types.ts +93 -0
  147. package/src/evermind/adapt.ts +61 -0
  148. package/src/evermind/delta_wire.ts +114 -0
  149. package/src/evermind/eval.ts +62 -0
  150. package/src/evermind/index.ts +33 -0
  151. package/src/evermind/merge.ts +106 -0
  152. package/src/{moe/moe_package.ts → evermind/package.ts} +148 -17
  153. package/src/import/foreign/adapter.ts +106 -0
  154. package/src/import/foreign/apply.ts +54 -0
  155. package/src/import/foreign/codestral_mamba.ts +224 -0
  156. package/src/import/foreign/falcon_mamba.ts +173 -0
  157. package/src/import/foreign/index.ts +37 -0
  158. package/src/import/foreign/plan.ts +294 -0
  159. package/src/import/foreign/registry.ts +126 -0
  160. package/src/import/index.ts +30 -0
  161. package/src/index.ts +98 -13
  162. package/src/kernels/selective_scan.ts +15 -9
  163. package/src/kernels/slice.ts +64 -0
  164. package/src/kernels/weight_update.ts +30 -4
  165. package/src/lm/evermind_lm.ts +244 -34
  166. package/src/lm/index.ts +6 -2
  167. package/src/lm/text_embedder.ts +84 -0
  168. package/src/model/attention_block.ts +9 -5
  169. package/src/model/mamba1_block.ts +28 -12
  170. package/src/model/mamba2_block.ts +14 -9
  171. package/src/model/mamba3_block.ts +14 -9
  172. package/src/moe/index.ts +0 -9
  173. package/src/optim/adamw.ts +60 -0
  174. package/src/tokenizer/bpe.ts +55 -0
  175. package/src/training/cpu_ops.ts +127 -0
  176. package/src/training/mamba1_cpu.ts +421 -0
  177. package/src/training/model_cpu.ts +360 -0
  178. package/src/training/trainer.ts +397 -154
  179. package/src/utils/base64.ts +28 -0
  180. package/dist/model/mamba_block.d.ts +0 -64
  181. package/dist/model/mamba_block.d.ts.map +0 -1
  182. package/dist/model/mamba_block.js +0 -303
  183. package/dist/model/mamba_block.js.map +0 -1
  184. package/dist/moe/moe_package.d.ts.map +0 -1
  185. package/dist/moe/moe_package.js.map +0 -1
  186. package/src/model/mamba_block.ts +0 -401
@@ -0,0 +1,174 @@
1
+ /**
2
+ * import/foreign/codestral_mamba.ts — weight port for Codestral-Mamba
3
+ * (`mistralai/Mamba-Codestral-7B-v0.1`, `Mamba2ForCausalLM`,
4
+ * `model_type: "mamba2"`, Mamba-2 / SSD).
5
+ *
6
+ * Source layout confirmed against the repo's published HF `config.json`, its
7
+ * `model.safetensors.index.json` (579 tensors = 9 per layer × 64 + 3 globals),
8
+ * the header of `consolidated.safetensors`, and
9
+ * `transformers/models/mamba2/modeling_mamba2.py`:
10
+ *
11
+ * backbone.embeddings.weight [vocab, d_model]
12
+ * backbone.layers.{N}.norm.weight [d_model]
13
+ * backbone.layers.{N}.mixer.in_proj.weight [2*d_inner + 2*n_groups*d_state + n_heads, d_model]
14
+ * backbone.layers.{N}.mixer.conv1d.weight [conv_dim, 1, d_conv]
15
+ * backbone.layers.{N}.mixer.conv1d.bias [conv_dim]
16
+ * backbone.layers.{N}.mixer.dt_bias [n_heads]
17
+ * backbone.layers.{N}.mixer.A_log [n_heads]
18
+ * backbone.layers.{N}.mixer.D [n_heads]
19
+ * backbone.layers.{N}.mixer.norm.weight [d_inner]
20
+ * backbone.layers.{N}.mixer.out_proj.weight [d_model, d_inner]
21
+ * backbone.norm_f.weight [d_model]
22
+ * lm_head.weight [vocab, d_model]
23
+ *
24
+ * where `conv_dim = d_inner + 2 * n_groups * d_state`. Mamba-2 has no `x_proj`
25
+ * and no `dt_proj`: B, C and dt all come out of the fused `in_proj`.
26
+ *
27
+ * Three reconciliations against `Mamba2Block`:
28
+ *
29
+ * 1. **`in_proj` row order.** Upstream splits
30
+ * `[gate (d_inner), x|B|C (conv_dim), dt (n_heads)]`; this engine's fused
31
+ * projection is `[x (d_inner), B (n_groups*d_state), C (n_groups*d_state),
32
+ * dt (n_heads)]` — the SAME tail, minus the leading gate rows. So the port
33
+ * is one contiguous row slice starting at `d_inner`, and the gate rows are
34
+ * declared as a discard (see 3).
35
+ * 2. **`A_log` convention.** Upstream uses `A = -exp(A_log)`; `ssd.ts` uses
36
+ * `A = -softplus(A_log)`. The port re-expresses the same `A` as
37
+ * `softplus⁻¹(exp(A_log))` so decay rates are preserved exactly, rather than
38
+ * copying the raw value and silently changing every head's decay.
39
+ * 3. **The gate branch.** `Mamba2Block` has no `z` gate: it RMS-norms the scan
40
+ * output and projects out, where upstream computes
41
+ * `norm(y * silu(z))`. The `d_inner × d_model` gate rows therefore have no
42
+ * target. That is a REAL loss of capability, not a formatting detail, and it
43
+ * is reported as a `PortDiscard` on every layer so a caller sees it.
44
+ *
45
+ * Also mapped without transform: `mixer.norm.weight` → the block's inner
46
+ * `normWeight` (applied ungated here, per 3), `backbone.layers.N.norm.weight` →
47
+ * `preNormWeight`, `conv1d.{weight,bias}` (depthwise, taps reversed) → the fused
48
+ * `[x, B, C]` conv, and `D` / `dt_bias` per head. Linear weights are
49
+ * `[out, in]` row-major on both sides, so no transposes.
50
+ *
51
+ * `tie_word_embeddings: false`, so `lm_head.weight` is a real stored tensor with
52
+ * no target here (`HybridMambaModel` ties its head to the embedding) — it is
53
+ * reported as unmapped.
54
+ */
55
+ import { expandFactor, firstInt, readInnerSize, requireInt, } from "./adapter.js";
56
+ const ID = "codestral_mamba";
57
+ /** `backbone.embeddings` (HF) vs `backbone.embedding` (Mistral-native). */
58
+ const EMBEDDING_NAMES = ["backbone.embeddings.weight", "backbone.embedding.weight"];
59
+ function rules(a) {
60
+ const { vocabSize: V, dModel: E, numLayers: L, dInner: D, dState: N, dConv: K, nHeads: H, nGroups: G } = a;
61
+ const convDim = D + 2 * G * N;
62
+ /** Rows this engine keeps out of the fused projection: [x, B, C, dt]. */
63
+ const keptRows = convDim + H;
64
+ const out = [
65
+ { target: "embedding", shape: [V, E], from: EMBEDDING_NAMES, op: { kind: "copy" } },
66
+ ];
67
+ for (let i = 0; i < L; i++) {
68
+ const m = `backbone.layers.${i}.mixer`;
69
+ const t = `layer${i}`;
70
+ out.push({
71
+ target: `${t}.wInProj`,
72
+ shape: [keptRows, E],
73
+ from: [`${m}.in_proj.weight`],
74
+ // Drop the leading gate rows; keep [x, B, C, dt] verbatim.
75
+ op: { kind: "rowSlice", start: D, count: keptRows },
76
+ note: "upstream rows are [gate, x, B, C, dt]; this block has no gate branch",
77
+ }, {
78
+ target: `${t}.wConv`,
79
+ shape: [convDim, K],
80
+ from: [`${m}.conv1d.weight`],
81
+ op: { kind: "convTaps" },
82
+ note: "depthwise [conv_dim, 1, d_conv] → [conv_dim, d_conv] with the tap axis reversed",
83
+ }, { target: `${t}.bConv`, shape: [convDim], from: [`${m}.conv1d.bias`], op: { kind: "copy" } }, {
84
+ target: `${t}.A_log`,
85
+ shape: [H],
86
+ from: [`${m}.A_log`],
87
+ op: { kind: "negExpToSoftplusInv" },
88
+ note: "upstream A = -exp(A_log); this engine's SSD kernel uses A = -softplus(A_log)",
89
+ }, { target: `${t}.dt_bias`, shape: [H], from: [`${m}.dt_bias`], op: { kind: "copy" } }, { target: `${t}.D_vec`, shape: [H], from: [`${m}.D`], op: { kind: "copy" } }, { target: `${t}.wOutProj`, shape: [E, D], from: [`${m}.out_proj.weight`], op: { kind: "copy" } }, {
90
+ target: `${t}.normWeight`,
91
+ shape: [D],
92
+ from: [`${m}.norm.weight`],
93
+ op: { kind: "copy" },
94
+ note: "upstream this gain belongs to a GATED RMSNorm; applied ungated here",
95
+ }, {
96
+ target: `${t}.preNormWeight`,
97
+ shape: [E],
98
+ from: [`backbone.layers.${i}.norm.weight`],
99
+ op: { kind: "copy" },
100
+ });
101
+ }
102
+ out.push({ target: "final_norm", shape: [E], from: ["backbone.norm_f.weight"], op: { kind: "copy" } });
103
+ return out;
104
+ }
105
+ function discards(a) {
106
+ const out = [];
107
+ for (let i = 0; i < a.numLayers; i++) {
108
+ out.push({
109
+ source: `backbone.layers.${i}.mixer.in_proj.weight`,
110
+ elements: a.dInner * a.dModel,
111
+ reason: "the leading d_inner gate (z) rows — Mamba2Block computes norm(y) rather than " +
112
+ "norm(y * silu(z)), so it has no gate branch to receive them",
113
+ });
114
+ }
115
+ return out;
116
+ }
117
+ export const codestralMambaAdapter = {
118
+ id: ID,
119
+ label: "Codestral-Mamba (Mamba-2 / SSD)",
120
+ modelTypes: ["mamba2"],
121
+ // `MambaCodestralForCausalLM` is not a transformers class — the published
122
+ // config says `Mamba2ForCausalLM` — but it is a common mis-citation, so it is
123
+ // accepted here rather than sending a correct checkpoint down the error path.
124
+ architectures: ["Mamba2ForCausalLM", "MambaCodestralForCausalLM"],
125
+ describe(config) {
126
+ const dModel = requireInt(config, "hidden_size");
127
+ const numLayers = requireInt(config, "num_hidden_layers");
128
+ const vocabSize = requireInt(config, "vocab_size");
129
+ const dState = requireInt(config, "state_size");
130
+ const dConv = requireInt(config, "conv_kernel");
131
+ const nHeads = requireInt(config, "num_heads");
132
+ const nGroups = firstInt(config, ["n_groups"]) ?? 1;
133
+ const chunkLen = firstInt(config, ["chunk_size"]) ?? 256;
134
+ const dInner = readInnerSize(config, dModel);
135
+ const expand = expandFactor(dInner, dModel, "Codestral-Mamba");
136
+ // Upstream's own invariant (`hidden_size * expand == num_heads * head_dim`);
137
+ // Mamba2Block enforces the same divisibility, so check it here where the
138
+ // message can name the config fields that disagree.
139
+ const headDim = firstInt(config, ["head_dim"]);
140
+ if (dInner % nHeads !== 0) {
141
+ throw new Error(`import/foreign: Codestral-Mamba d_inner ${dInner} is not divisible by num_heads ${nHeads}`);
142
+ }
143
+ if (headDim !== undefined && headDim * nHeads !== dInner) {
144
+ throw new Error(`import/foreign: Codestral-Mamba config is inconsistent — head_dim ${headDim} × ` +
145
+ `num_heads ${nHeads} = ${headDim * nHeads}, but d_inner is ${dInner}`);
146
+ }
147
+ const layers = Array.from({ length: numLayers }, () => ({ type: "mamba2" }));
148
+ const modelConfig = {
149
+ vocabSize,
150
+ dModel,
151
+ numLayers,
152
+ layers,
153
+ dState,
154
+ dConv,
155
+ expand,
156
+ nHeads,
157
+ nGroups,
158
+ chunkLen,
159
+ };
160
+ const geometry = {
161
+ vocabSize,
162
+ dModel,
163
+ numLayers,
164
+ dInner,
165
+ dState,
166
+ dConv,
167
+ nHeads,
168
+ nGroups,
169
+ chunkLen,
170
+ };
171
+ return { modelConfig, plan: { adapter: ID, rules: rules(geometry), discards: discards(geometry) } };
172
+ },
173
+ };
174
+ //# sourceMappingURL=codestral_mamba.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"codestral_mamba.js","sourceRoot":"","sources":["../../../src/import/foreign/codestral_mamba.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqDG;AAIH,OAAO,EACL,YAAY,EACZ,QAAQ,EACR,aAAa,EACb,UAA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C;QACJ,CAAC;QAED,MAAM,MAAM,GAAgB,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,QAAiB,EAAE,CAAC,CAAC,CAAC;QACnG,MAAM,WAAW,GAA2B;YAC1C,SAAS;YACT,MAAM;YACN,SAAS;YACT,MAAM;YACN,MAAM;YACN,KAAK;YACL,MAAM;YACN,MAAM;YACN,OAAO;YACP,QAAQ;SACT,CAAC;QAEF,MAAM,QAAQ,GAAmB;YAC/B,SAAS;YACT,MAAM;YACN,SAAS;YACT,MAAM;YACN,MAAM;YACN,KAAK;YACL,MAAM;YACN,OAAO;YACP,QAAQ;SACT,CAAC;QAEF,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,CAAC,QAAQ,CAAC,EAAE,QAAQ,EAAE,QAAQ,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;IACtG,CAAC;CACF,CAAC"}
@@ -0,0 +1,51 @@
1
+ /**
2
+ * import/foreign/falcon_mamba.ts — weight port for Falcon-Mamba
3
+ * (`FalconMambaForCausalLM`, `model_type: "falcon_mamba"`, Mamba-1 / S6).
4
+ *
5
+ * Source layout confirmed against `tiiuae/falcon-mamba-7b`'s published
6
+ * `config.json`, `model.safetensors.index.json` (643 tensors = 10 per layer × 64
7
+ * + 3 globals) and the safetensors header of shard 1, plus
8
+ * `transformers/models/falcon_mamba/modeling_falcon_mamba.py`:
9
+ *
10
+ * backbone.embeddings.weight [vocab, d_model]
11
+ * backbone.layers.{N}.norm.weight [d_model]
12
+ * backbone.layers.{N}.mixer.in_proj.weight [2*d_inner, d_model]
13
+ * backbone.layers.{N}.mixer.conv1d.weight [d_inner, 1, d_conv]
14
+ * backbone.layers.{N}.mixer.conv1d.bias [d_inner]
15
+ * backbone.layers.{N}.mixer.x_proj.weight [dt_rank + 2*d_state, d_inner]
16
+ * backbone.layers.{N}.mixer.dt_proj.weight [d_inner, dt_rank]
17
+ * backbone.layers.{N}.mixer.dt_proj.bias [d_inner]
18
+ * backbone.layers.{N}.mixer.A_log [d_inner, d_state]
19
+ * backbone.layers.{N}.mixer.D [d_inner]
20
+ * backbone.layers.{N}.mixer.out_proj.weight [d_model, d_inner]
21
+ * backbone.norm_f.weight [d_model]
22
+ * lm_head.weight [vocab, d_model]
23
+ *
24
+ * Conventions that line up with `Mamba1Block` as-is, so most rules are copies:
25
+ * • `in_proj` rows are `[x, gate]` (`hidden_states, gate = …chunk(2, dim=1)`),
26
+ * and `Mamba1Block.forward` also takes the first half as `x` and the second
27
+ * as the gate `z`.
28
+ * • `x_proj` rows are `[dt, B, C]`, matching the block's split order.
29
+ * • `A = -exp(A_log)` in both — `selective_scan.ts` computes
30
+ * `a_cont = -exp(clamp(a_log, -10, 5))`.
31
+ * • `dt_proj.bias` is added inside `softplus(dt + bias)` upstream; here it is
32
+ * the `dt_proj` linear's bias and the scan applies `softplus` afterwards —
33
+ * algebraically the same value.
34
+ * • Linear weights are `[out, in]` row-major on both sides (no transpose).
35
+ *
36
+ * Fidelity caveats recorded on the plan rather than hidden:
37
+ * • `use_bias: false` upstream, so `in_proj` / `x_proj` / `out_proj` ship no
38
+ * bias; this engine's blocks always own one, and they are filled with zeros
39
+ * (an exact no-op, not an approximation).
40
+ * • Falcon-Mamba RMS-normalises `dt`, `B` and `C` inside the mixer. Those
41
+ * norms are `FalconMambaWeightlessRMSNorm` with `persistent=False` buffers,
42
+ * so they contribute NO tensors to the checkpoint — nothing is dropped from
43
+ * the file — but this engine's S6 scan has no such normalisation step, so a
44
+ * ported model is not numerically identical to the original.
45
+ * • `tie_word_embeddings: false`, so `lm_head.weight` is a real stored tensor.
46
+ * `HybridMambaModel` always ties its LM head to the embedding table, so that
47
+ * tensor has no target and is reported as unmapped.
48
+ */
49
+ import { type ForeignMambaAdapter } from "./adapter.js";
50
+ export declare const falconMambaAdapter: ForeignMambaAdapter;
51
+ //# sourceMappingURL=falcon_mamba.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"falcon_mamba.d.ts","sourceRoot":"","sources":["../../../src/import/foreign/falcon_mamba.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AAIH,OAAO,EAML,KAAK,mBAAmB,EAEzB,MAAM,cAAc,CAAC;AA0EtB,eAAO,MAAM,kBAAkB,EAAE,mBAuChC,CAAC"}
@@ -0,0 +1,131 @@
1
+ /**
2
+ * import/foreign/falcon_mamba.ts — weight port for Falcon-Mamba
3
+ * (`FalconMambaForCausalLM`, `model_type: "falcon_mamba"`, Mamba-1 / S6).
4
+ *
5
+ * Source layout confirmed against `tiiuae/falcon-mamba-7b`'s published
6
+ * `config.json`, `model.safetensors.index.json` (643 tensors = 10 per layer × 64
7
+ * + 3 globals) and the safetensors header of shard 1, plus
8
+ * `transformers/models/falcon_mamba/modeling_falcon_mamba.py`:
9
+ *
10
+ * backbone.embeddings.weight [vocab, d_model]
11
+ * backbone.layers.{N}.norm.weight [d_model]
12
+ * backbone.layers.{N}.mixer.in_proj.weight [2*d_inner, d_model]
13
+ * backbone.layers.{N}.mixer.conv1d.weight [d_inner, 1, d_conv]
14
+ * backbone.layers.{N}.mixer.conv1d.bias [d_inner]
15
+ * backbone.layers.{N}.mixer.x_proj.weight [dt_rank + 2*d_state, d_inner]
16
+ * backbone.layers.{N}.mixer.dt_proj.weight [d_inner, dt_rank]
17
+ * backbone.layers.{N}.mixer.dt_proj.bias [d_inner]
18
+ * backbone.layers.{N}.mixer.A_log [d_inner, d_state]
19
+ * backbone.layers.{N}.mixer.D [d_inner]
20
+ * backbone.layers.{N}.mixer.out_proj.weight [d_model, d_inner]
21
+ * backbone.norm_f.weight [d_model]
22
+ * lm_head.weight [vocab, d_model]
23
+ *
24
+ * Conventions that line up with `Mamba1Block` as-is, so most rules are copies:
25
+ * • `in_proj` rows are `[x, gate]` (`hidden_states, gate = …chunk(2, dim=1)`),
26
+ * and `Mamba1Block.forward` also takes the first half as `x` and the second
27
+ * as the gate `z`.
28
+ * • `x_proj` rows are `[dt, B, C]`, matching the block's split order.
29
+ * • `A = -exp(A_log)` in both — `selective_scan.ts` computes
30
+ * `a_cont = -exp(clamp(a_log, -10, 5))`.
31
+ * • `dt_proj.bias` is added inside `softplus(dt + bias)` upstream; here it is
32
+ * the `dt_proj` linear's bias and the scan applies `softplus` afterwards —
33
+ * algebraically the same value.
34
+ * • Linear weights are `[out, in]` row-major on both sides (no transpose).
35
+ *
36
+ * Fidelity caveats recorded on the plan rather than hidden:
37
+ * • `use_bias: false` upstream, so `in_proj` / `x_proj` / `out_proj` ship no
38
+ * bias; this engine's blocks always own one, and they are filled with zeros
39
+ * (an exact no-op, not an approximation).
40
+ * • Falcon-Mamba RMS-normalises `dt`, `B` and `C` inside the mixer. Those
41
+ * norms are `FalconMambaWeightlessRMSNorm` with `persistent=False` buffers,
42
+ * so they contribute NO tensors to the checkpoint — nothing is dropped from
43
+ * the file — but this engine's S6 scan has no such normalisation step, so a
44
+ * ported model is not numerically identical to the original.
45
+ * • `tie_word_embeddings: false`, so `lm_head.weight` is a real stored tensor.
46
+ * `HybridMambaModel` always ties its LM head to the embedding table, so that
47
+ * tensor has no target and is reported as unmapped.
48
+ */
49
+ import { expandFactor, readInnerSize, readTimeStepRank, requireInt, } from "./adapter.js";
50
+ const ID = "falcon_mamba";
51
+ /** `backbone.embeddings` (HF) vs `backbone.embedding` (Mamba-SSM native). */
52
+ const EMBEDDING_NAMES = ["backbone.embeddings.weight", "backbone.embedding.weight"];
53
+ function rules(a) {
54
+ const { vocabSize: V, dModel: E, numLayers: L, dInner: D, dState: N, dConv: K, dtRank: R } = a;
55
+ const out = [
56
+ { target: "embedding", shape: [V, E], from: EMBEDDING_NAMES, op: { kind: "copy" } },
57
+ ];
58
+ for (let i = 0; i < L; i++) {
59
+ const m = `backbone.layers.${i}.mixer`;
60
+ const t = `layer${i}`;
61
+ out.push({ target: `${t}.wInProj`, shape: [2 * D, E], from: [`${m}.in_proj.weight`], op: { kind: "copy" } }, {
62
+ target: `${t}.bInProj`,
63
+ shape: [2 * D],
64
+ from: [],
65
+ op: { kind: "fill", value: 0 },
66
+ note: "Falcon-Mamba sets use_bias=false — in_proj ships no bias; zeros are exact",
67
+ }, {
68
+ target: `${t}.wConv`,
69
+ shape: [D, K],
70
+ from: [`${m}.conv1d.weight`],
71
+ op: { kind: "convTaps" },
72
+ note: "depthwise [d_inner, 1, d_conv] → [d_inner, d_conv] with the tap axis reversed",
73
+ }, { target: `${t}.bConv`, shape: [D], from: [`${m}.conv1d.bias`], op: { kind: "copy" } }, { target: `${t}.wXProj`, shape: [R + 2 * N, D], from: [`${m}.x_proj.weight`], op: { kind: "copy" } }, {
74
+ target: `${t}.bXProj`,
75
+ shape: [R + 2 * N],
76
+ from: [],
77
+ op: { kind: "fill", value: 0 },
78
+ note: "x_proj is bias-free upstream; zeros are exact",
79
+ }, { target: `${t}.wDtProj`, shape: [D, R], from: [`${m}.dt_proj.weight`], op: { kind: "copy" } }, { target: `${t}.bDtProj`, shape: [D], from: [`${m}.dt_proj.bias`], op: { kind: "copy" } }, { target: `${t}.A_log`, shape: [D, N], from: [`${m}.A_log`], op: { kind: "copy" } }, { target: `${t}.D_vec`, shape: [D], from: [`${m}.D`], op: { kind: "copy" } }, { target: `${t}.wOutProj`, shape: [E, D], from: [`${m}.out_proj.weight`], op: { kind: "copy" } }, {
80
+ target: `${t}.bOutProj`,
81
+ shape: [E],
82
+ from: [],
83
+ op: { kind: "fill", value: 0 },
84
+ note: "out_proj is bias-free upstream; zeros are exact",
85
+ }, {
86
+ target: `${t}.normWeight`,
87
+ shape: [E],
88
+ from: [`backbone.layers.${i}.norm.weight`],
89
+ op: { kind: "copy" },
90
+ });
91
+ }
92
+ out.push({ target: "final_norm", shape: [E], from: ["backbone.norm_f.weight"], op: { kind: "copy" } });
93
+ return out;
94
+ }
95
+ export const falconMambaAdapter = {
96
+ id: ID,
97
+ label: "Falcon-Mamba (Mamba-1 / S6)",
98
+ modelTypes: ["falcon_mamba"],
99
+ architectures: ["FalconMambaForCausalLM"],
100
+ describe(config) {
101
+ const dModel = requireInt(config, "hidden_size");
102
+ const numLayers = requireInt(config, "num_hidden_layers");
103
+ const vocabSize = requireInt(config, "vocab_size");
104
+ const dState = requireInt(config, "state_size");
105
+ const dConv = requireInt(config, "conv_kernel");
106
+ const dInner = readInnerSize(config, dModel);
107
+ const expand = expandFactor(dInner, dModel, "Falcon-Mamba");
108
+ const dtRank = readTimeStepRank(config, dModel);
109
+ const modelConfig = {
110
+ vocabSize,
111
+ dModel,
112
+ numLayers,
113
+ dState,
114
+ dConv,
115
+ expand,
116
+ // Every layer is a Mamba-1 mixer (the model's default schedule), but the
117
+ // dt rank and conv bias are per-block settings the shorthand can't carry.
118
+ defaultMamba1: { dtRank, biasConv: true },
119
+ };
120
+ const plan = {
121
+ adapter: ID,
122
+ rules: rules({ vocabSize, dModel, numLayers, dInner, dState, dConv, dtRank }),
123
+ // Nothing is partially consumed: every Falcon-Mamba tensor this plan
124
+ // touches is taken whole. `lm_head.weight` is untouched entirely and
125
+ // surfaces through `unmappedSources`.
126
+ discards: [],
127
+ };
128
+ return { modelConfig, plan };
129
+ },
130
+ };
131
+ //# sourceMappingURL=falcon_mamba.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"falcon_mamba.js","sourceRoot":"","sources":["../../../src/import/foreign/falcon_mamba.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AAIH,OAAO,EACL,YAAY,EACZ,aAAa,EACb,gBAAgB,EAChB,UAAU,GAIX,MAAM,cAAc,CAAC;AAEtB,MAAM,EAAE,GAAG,cAAc,CAAC;AAE1B,6EAA6E;AAC7E,MAAM,eAAe,GAAG,CAAC,4BAA4B,EAAE,2BAA2B,CAAC,CAAC;AAEpF,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M,MAAM,GAAG,UAAU,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;QACjD,MAAM,SAAS,GAAG,UAAU,CAAC,MAAM,EAAE,mBAAmB,CAAC,CAAC;QAC1D,MAAM,SAAS,GAAG,UAAU,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;QACnD,MAAM,MAAM,GAAG,UAAU,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;QAChD,MAAM,KAAK,GAAG,UAAU,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;QAChD,MAAM,MAAM,GAAG,aAAa,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC7C,MAAM,MAAM,GAAG,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,cAAc,CAAC,CAAC;QAC5D,MAAM,MAAM,GAAG,gBAAgB,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAEhD,MAAM,WAAW,GAA2B;YAC1C,SAAS;YACT,MAAM;YACN,SAAS;YACT,MAAM;YACN,KAAK;YACL,MAAM;YACN,yEAAyE;YACzE,0EAA0E;YAC1E,aAAa,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE;SAC1C,CAAC;QAEF,MAAM,IAAI,GAAa;YACrB,OAAO,EAAE,EAAE;YACX,KAAK,EAAE,KAAK,CAAC,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;YAC7E,qEAAqE;YACrE,qEAAqE;YACrE,sCAAsC;YACtC,QAAQ,EAAE,EAAE;SACb,CAAC;QAEF,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC;IAC/B,CAAC;CACF,CAAC"}
@@ -0,0 +1,21 @@
1
+ /**
2
+ * import/foreign/index.ts — the foreign-checkpoint weight-port surface.
3
+ *
4
+ * Warm-start this engine's `HybridMambaModel` from a published Mamba checkpoint:
5
+ * pass the checkpoint's `config.json` and its tensors, get back the model config
6
+ * to build plus a name-keyed set of weights, together with an explicit account of
7
+ * anything that did not map cleanly.
8
+ *
9
+ * Supported: Falcon-Mamba (Mamba-1) and Codestral-Mamba (Mamba-2). Transformer
10
+ * checkpoints are NOT portable at any tensor naming — distillation is the only
11
+ * route, and {@link foreignMambaAdapterFor} says so when it rejects one.
12
+ */
13
+ export { FOREIGN_MAMBA_ADAPTERS, foreignMambaAdapterFor, portForeignMamba, portForeignMambaSafetensors, } from "./registry.js";
14
+ export type { PortedCheckpoint } from "./registry.js";
15
+ export { applyPortedWeights } from "./apply.js";
16
+ export { executePortPlan, normaliseSourceName } from "./plan.js";
17
+ export type { PortDiscard, PortOp, PortPlan, PortRule, PortedTensors, SynthesisedTarget, } from "./plan.js";
18
+ export type { ForeignConfig, ForeignMambaAdapter, PortTarget } from "./adapter.js";
19
+ export { falconMambaAdapter } from "./falcon_mamba.js";
20
+ export { codestralMambaAdapter } from "./codestral_mamba.js";
21
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/import/foreign/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EACL,sBAAsB,EACtB,sBAAsB,EACtB,gBAAgB,EAChB,2BAA2B,GAC5B,MAAM,eAAe,CAAC;AACvB,YAAY,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAEtD,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAEhD,OAAO,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AACjE,YAAY,EACV,WAAW,EACX,MAAM,EACN,QAAQ,EACR,QAAQ,EACR,aAAa,EACb,iBAAiB,GAClB,MAAM,WAAW,CAAC;AAEnB,YAAY,EAAE,aAAa,EAAE,mBAAmB,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAEnF,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AACvD,OAAO,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAC"}
@@ -0,0 +1,18 @@
1
+ /**
2
+ * import/foreign/index.ts — the foreign-checkpoint weight-port surface.
3
+ *
4
+ * Warm-start this engine's `HybridMambaModel` from a published Mamba checkpoint:
5
+ * pass the checkpoint's `config.json` and its tensors, get back the model config
6
+ * to build plus a name-keyed set of weights, together with an explicit account of
7
+ * anything that did not map cleanly.
8
+ *
9
+ * Supported: Falcon-Mamba (Mamba-1) and Codestral-Mamba (Mamba-2). Transformer
10
+ * checkpoints are NOT portable at any tensor naming — distillation is the only
11
+ * route, and {@link foreignMambaAdapterFor} says so when it rejects one.
12
+ */
13
+ export { FOREIGN_MAMBA_ADAPTERS, foreignMambaAdapterFor, portForeignMamba, portForeignMambaSafetensors, } from "./registry.js";
14
+ export { applyPortedWeights } from "./apply.js";
15
+ export { executePortPlan, normaliseSourceName } from "./plan.js";
16
+ export { falconMambaAdapter } from "./falcon_mamba.js";
17
+ export { codestralMambaAdapter } from "./codestral_mamba.js";
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/import/foreign/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EACL,sBAAsB,EACtB,sBAAsB,EACtB,gBAAgB,EAChB,2BAA2B,GAC5B,MAAM,eAAe,CAAC;AAGvB,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAEhD,OAAO,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AAYjE,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AACvD,OAAO,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAC"}
@@ -0,0 +1,122 @@
1
+ /**
2
+ * import/foreign/plan.ts — the ONE executor for a foreign-checkpoint weight port.
3
+ *
4
+ * A port is expressed declaratively: every target parameter of this engine's
5
+ * {@link HybridMambaModel} gets exactly one {@link PortRule} saying which source
6
+ * tensor feeds it and how the bytes are reshaped. Architecture adapters
7
+ * (`falcon_mamba.ts`, `codestral_mamba.ts`) only *describe* their rules — the
8
+ * matching, validation and transform logic lives here once, so a new
9
+ * architecture never re-implements it.
10
+ *
11
+ * The contract that makes a port trustworthy: NOTHING is silently dropped.
12
+ * • a target with no source in the checkpoint is a `fill` rule and is reported
13
+ * in {@link PortedCheckpoint.synthesisedTargets};
14
+ * • a source tensor no rule consumed is reported in
15
+ * {@link PortedCheckpoint.unmappedSources};
16
+ * • a source tensor only PARTLY consumed (e.g. Mamba-2's gate rows, which this
17
+ * engine's block has no branch for) is declared as a {@link PortDiscard};
18
+ * • a shape that doesn't line up throws, naming the tensor and both shapes.
19
+ */
20
+ import type { NamedTensor } from "../../export/tensors.js";
21
+ /**
22
+ * How one source tensor becomes one target buffer.
23
+ *
24
+ * • `copy` — element-for-element; source and target shapes must
25
+ * agree once size-1 axes are squeezed out. Linear
26
+ * weights need no transpose: HF stores
27
+ * `[out_features, in_features]` row-major and this
28
+ * engine's LINEAR_FORWARD_WGSL reads `W` as `(N, K)`
29
+ * = `[out, in]` row-major — the same layout.
30
+ * • `convTaps` — a depthwise conv kernel `[C, 1, K]` (PyTorch
31
+ * `nn.Conv1d(groups=C)`) → `[C, K]` with the taps
32
+ * REVERSED. PyTorch pads left by `K-1` and cross-
33
+ * correlates: `y[t] = Σ_j w[j]·x[t + j - (K-1)]`,
34
+ * while conv1d.ts computes
35
+ * `y[t] = Σ_k w[k]·x[t - k]`. Substituting
36
+ * `k = K-1-j` shows the two agree only when the tap
37
+ * axis is reversed.
38
+ * • `rowSlice` — keep rows `[start, start+count)` of a rank-2
39
+ * source. Used for Mamba-2's fused `in_proj`.
40
+ * • `fill` — no source: fill the target with a constant.
41
+ * • `negExpToSoftplusInv` — `A_log` convention change. HF Mamba-2 uses
42
+ * `A = -exp(A_log)`; this engine's SSD kernel uses
43
+ * `A = -softplus(A_log)`. Re-express the same `A`:
44
+ * `target = softplus⁻¹(exp(source))`.
45
+ */
46
+ export type PortOp = {
47
+ kind: "copy";
48
+ } | {
49
+ kind: "convTaps";
50
+ } | {
51
+ kind: "rowSlice";
52
+ start: number;
53
+ count: number;
54
+ } | {
55
+ kind: "fill";
56
+ value: number;
57
+ } | {
58
+ kind: "negExpToSoftplusInv";
59
+ };
60
+ /** How ONE target parameter of this engine is produced. */
61
+ export interface PortRule {
62
+ /** Target parameter name, exactly as `HybridMambaModel.parameters()` names it. */
63
+ target: string;
64
+ /** Expected target shape (row-major), for validation and reporting. */
65
+ shape: number[];
66
+ /**
67
+ * Candidate source tensor names, in preference order — the first one present
68
+ * in the checkpoint is used. Empty means the target is synthesised (`fill`),
69
+ * because this architecture has no such tensor.
70
+ */
71
+ from: string[];
72
+ op: PortOp;
73
+ /** Why this isn't a plain copy / why there is no source. Required for `fill`. */
74
+ note?: string;
75
+ }
76
+ /** A source tensor deliberately consumed only in part. */
77
+ export interface PortDiscard {
78
+ source: string;
79
+ /** How many source elements are left behind. */
80
+ elements: number;
81
+ reason: string;
82
+ }
83
+ /** The complete, architecture-specific description of a port. */
84
+ export interface PortPlan {
85
+ /** Adapter id, e.g. `falcon_mamba`. */
86
+ adapter: string;
87
+ rules: PortRule[];
88
+ discards: PortDiscard[];
89
+ }
90
+ /** A target that no source tensor feeds, and the constant used instead. */
91
+ export interface SynthesisedTarget {
92
+ target: string;
93
+ value: number;
94
+ reason: string;
95
+ }
96
+ /** The result of running a {@link PortPlan} over a checkpoint's tensors. */
97
+ export interface PortedTensors {
98
+ adapter: string;
99
+ /** Target parameter name → buffer, sized exactly as the model expects. */
100
+ weights: Map<string, Float32Array>;
101
+ /** Checkpoint tensors that no rule consumed (e.g. an untied `lm_head.weight`). */
102
+ unmappedSources: string[];
103
+ /** Targets this architecture has no tensor for. */
104
+ synthesisedTargets: SynthesisedTarget[];
105
+ /** Source tensors consumed only in part. */
106
+ discardedSources: PortDiscard[];
107
+ }
108
+ /**
109
+ * Mistral publishes the same Mamba-2 weights twice: the HF conversion
110
+ * (`backbone.…`) and its native `consolidated.safetensors`, whose names carry a
111
+ * `model.` prefix. Strip it so one rule table serves both. The
112
+ * `embedding`/`embeddings` spelling difference is handled by the rules' `from`
113
+ * alternatives, not here.
114
+ */
115
+ export declare function normaliseSourceName(name: string): string;
116
+ /**
117
+ * Execute a plan against a checkpoint's tensors. Every rule must resolve; the
118
+ * first failure throws naming the offending tensor. Sources no rule touched are
119
+ * returned rather than ignored.
120
+ */
121
+ export declare function executePortPlan(plan: PortPlan, tensors: NamedTensor[]): PortedTensors;
122
+ //# sourceMappingURL=plan.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plan.d.ts","sourceRoot":"","sources":["../../../src/import/foreign/plan.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAI3D;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,MAAM,MAAM,GACd;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,GAChB;IAAE,IAAI,EAAE,UAAU,CAAA;CAAE,GACpB;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAClD;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAC/B;IAAE,IAAI,EAAE,qBAAqB,CAAA;CAAE,CAAC;AAIpC,2DAA2D;AAC3D,MAAM,WAAW,QAAQ;IACvB,kFAAkF;IAClF,MAAM,EAAE,MAAM,CAAC;IACf,uEAAuE;IACvE,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB;;;;OAIG;IACH,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,EAAE,EAAE,MAAM,CAAC;IACX,iFAAiF;IACjF,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,0DAA0D;AAC1D,MAA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