@tryhamster/gerbil 1.10.2 → 1.11.0
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.
- package/dist/{architectures-BHkqQ9xp.mjs → architectures-DmZMEFsA.mjs} +652 -9
- package/dist/architectures-DmZMEFsA.mjs.map +1 -0
- package/dist/cli.mjs +8 -8
- package/dist/cli.mjs.map +1 -1
- package/dist/frameworks/express.mjs +1 -1
- package/dist/frameworks/fastify.mjs +1 -1
- package/dist/frameworks/hono.mjs +1 -1
- package/dist/frameworks/next.d.mts +2 -2
- package/dist/frameworks/next.mjs +1 -1
- package/dist/frameworks/trpc.mjs +1 -1
- package/dist/{gerbil-PEAdJdsH.d.mts → gerbil-6E0XH_8s.d.mts} +2 -2
- package/dist/{gerbil-PEAdJdsH.d.mts.map → gerbil-6E0XH_8s.d.mts.map} +1 -1
- package/dist/gerbil-BY5EW-Jk.mjs +4 -0
- package/dist/{gerbil-sQ7eqzn3.mjs → gerbil-BpYemKEH.mjs} +5 -4
- package/dist/{gerbil-sQ7eqzn3.mjs.map → gerbil-BpYemKEH.mjs.map} +1 -1
- package/dist/gpu/hooks.d.mts +1 -1
- package/dist/gpu/index.d.mts +1 -1
- package/dist/gpu/index.mjs +3 -3
- package/dist/{gpu-Ps1eQaXG.mjs → gpu-CzgbyVeq.mjs} +15 -636
- package/dist/gpu-CzgbyVeq.mjs.map +1 -0
- package/dist/{index-CJux7zbV.d.mts → index-DyqcKFfQ.d.mts} +13 -7
- package/dist/{index-CJux7zbV.d.mts.map → index-DyqcKFfQ.d.mts.map} +1 -1
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +5 -5
- package/dist/integrations/ai-sdk.mjs +1 -1
- package/dist/integrations/langchain.mjs +1 -1
- package/dist/integrations/llamaindex.mjs +1 -1
- package/dist/integrations/mcp.d.mts +2 -2
- package/dist/integrations/mcp.mjs +4 -4
- package/dist/{mcp-BX-ryGGJ.mjs → mcp-DSpjqWZC.mjs} +3 -3
- package/dist/{mcp-BX-ryGGJ.mjs.map → mcp-DSpjqWZC.mjs.map} +1 -1
- package/dist/moonshine-stt-CcVt4Vdd.mjs +4 -0
- package/dist/{moonshine-stt-BKeD6OYF.mjs → moonshine-stt-CgDXoLFd.mjs} +2 -2
- package/dist/{moonshine-stt-BKeD6OYF.mjs.map → moonshine-stt-CgDXoLFd.mjs.map} +1 -1
- package/dist/{one-liner-m6NjYWMg.mjs → one-liner-BgNDxJSJ.mjs} +2 -2
- package/dist/{one-liner-m6NjYWMg.mjs.map → one-liner-BgNDxJSJ.mjs.map} +1 -1
- package/dist/repl-DSK2hDzu.mjs +9 -0
- package/dist/skills/index.d.mts +15 -15
- package/dist/skills/index.d.mts.map +1 -1
- package/dist/skills/index.mjs +3 -3
- package/dist/{skills-DZ5OITy0.mjs → skills-Cd75ZGew.mjs} +2 -2
- package/dist/{skills-DZ5OITy0.mjs.map → skills-Cd75ZGew.mjs.map} +1 -1
- package/dist/tune/index.mjs +1 -1
- package/package.json +1 -1
- package/dist/architectures-BHkqQ9xp.mjs.map +0 -1
- package/dist/gerbil-IqsK32DM.mjs +0 -4
- package/dist/gpu-Ps1eQaXG.mjs.map +0 -1
- package/dist/moonshine-stt-DKC0PHF_.mjs +0 -4
- package/dist/repl-hN4jSHrb.mjs +0 -9
|
@@ -3630,7 +3630,7 @@ function convOutLen(L, K, stride, pad = 0, dilation = 1) {
|
|
|
3630
3630
|
function moonshineEncoderFrames(nSamples) {
|
|
3631
3631
|
return convOutLen(convOutLen(convOutLen(nSamples, 127, 64), 7, 3), 3, 2);
|
|
3632
3632
|
}
|
|
3633
|
-
function baseConfig(d, layers) {
|
|
3633
|
+
function baseConfig$1(d, layers) {
|
|
3634
3634
|
return {
|
|
3635
3635
|
hidden_size: d.hidden_size,
|
|
3636
3636
|
num_layers: layers,
|
|
@@ -3649,7 +3649,7 @@ function baseConfig(d, layers) {
|
|
|
3649
3649
|
has_vision_tower: false
|
|
3650
3650
|
};
|
|
3651
3651
|
}
|
|
3652
|
-
const CAPS = {
|
|
3652
|
+
const CAPS$1 = {
|
|
3653
3653
|
text: true,
|
|
3654
3654
|
vision: false,
|
|
3655
3655
|
moe: false
|
|
@@ -3989,8 +3989,8 @@ function generateMoonshineEncoderGraph(raw, nSamples) {
|
|
|
3989
3989
|
}
|
|
3990
3990
|
return {
|
|
3991
3991
|
architecture: "MoonshineEncoder",
|
|
3992
|
-
config: baseConfig(d, d.enc_layers),
|
|
3993
|
-
capabilities: CAPS,
|
|
3992
|
+
config: baseConfig$1(d, d.enc_layers),
|
|
3993
|
+
capabilities: CAPS$1,
|
|
3994
3994
|
tensors,
|
|
3995
3995
|
nodes,
|
|
3996
3996
|
executionOrder,
|
|
@@ -4565,8 +4565,8 @@ function generateMoonshineDecoderGraph(raw, sEnc) {
|
|
|
4565
4565
|
});
|
|
4566
4566
|
return {
|
|
4567
4567
|
architecture: "MoonshineDecoder",
|
|
4568
|
-
config: baseConfig(d, d.dec_layers),
|
|
4569
|
-
capabilities: CAPS,
|
|
4568
|
+
config: baseConfig$1(d, d.dec_layers),
|
|
4569
|
+
capabilities: CAPS$1,
|
|
4570
4570
|
tensors,
|
|
4571
4571
|
nodes,
|
|
4572
4572
|
executionOrder,
|
|
@@ -4584,6 +4584,648 @@ function generateMoonshineGraph() {
|
|
|
4584
4584
|
throw new Error("Moonshine is an encoder-decoder STT model and is not loadable via the single-graph generate() path. Use the dedicated STT engine instead:\n import { MoonshineSTT } from '@tryhamster/gerbil';\n const stt = await MoonshineSTT.create();\n const { text } = await stt.transcribe(pcm16kMonoFloat32);");
|
|
4585
4585
|
}
|
|
4586
4586
|
|
|
4587
|
+
//#endregion
|
|
4588
|
+
//#region src/gpu/architectures/parler.ts
|
|
4589
|
+
const PARLER_SAMPLE_RATE = 44100;
|
|
4590
|
+
const PARLER_NUM_CODEBOOKS = 9;
|
|
4591
|
+
const PARLER_DECODER_RATES = [
|
|
4592
|
+
8,
|
|
4593
|
+
8,
|
|
4594
|
+
4,
|
|
4595
|
+
2
|
|
4596
|
+
];
|
|
4597
|
+
const PARLER_DAC_LATENT_DIM = 1024;
|
|
4598
|
+
const PARLER_DAC_DECODER_DIM = 1536;
|
|
4599
|
+
/** Decoder audio-vocab: 1024 codes + eos(1024)+bos(1025)+pad(1024); lm_head vocab. */
|
|
4600
|
+
const PARLER_AUDIO_VOCAB = 1088;
|
|
4601
|
+
const PARLER_BOS_TOKEN_ID = 1025;
|
|
4602
|
+
const PARLER_EOS_TOKEN_ID = 1024;
|
|
4603
|
+
const PARLER_PAD_TOKEN_ID = 1024;
|
|
4604
|
+
const CAPS = {
|
|
4605
|
+
text: true,
|
|
4606
|
+
vision: false,
|
|
4607
|
+
moe: false
|
|
4608
|
+
};
|
|
4609
|
+
/** Pull Parler dims from the (nested) HF config. */
|
|
4610
|
+
function parseParlerConfig(raw) {
|
|
4611
|
+
const te = raw.text_encoder ?? {};
|
|
4612
|
+
const dec = raw.decoder ?? {};
|
|
4613
|
+
const enc_heads = te.num_heads ?? 16;
|
|
4614
|
+
const enc_d_model = te.d_model ?? 1024;
|
|
4615
|
+
const dec_heads = dec.num_attention_heads ?? 16;
|
|
4616
|
+
return {
|
|
4617
|
+
enc_d_model,
|
|
4618
|
+
enc_layers: te.num_layers ?? 24,
|
|
4619
|
+
enc_heads,
|
|
4620
|
+
enc_d_kv: te.d_kv ?? Math.floor(enc_d_model / enc_heads),
|
|
4621
|
+
enc_d_ff: te.d_ff ?? 2816,
|
|
4622
|
+
enc_vocab: te.vocab_size ?? 32128,
|
|
4623
|
+
rel_num_buckets: te.relative_attention_num_buckets ?? 32,
|
|
4624
|
+
rel_max_distance: te.relative_attention_max_distance ?? 128,
|
|
4625
|
+
ln_eps: te.layer_norm_epsilon ?? 1e-6,
|
|
4626
|
+
dec_hidden: dec.hidden_size ?? 1024,
|
|
4627
|
+
dec_layers: dec.num_hidden_layers ?? 24,
|
|
4628
|
+
dec_heads,
|
|
4629
|
+
dec_kv_heads: dec.num_key_value_heads ?? dec_heads,
|
|
4630
|
+
dec_ffn: dec.ffn_dim ?? 4096,
|
|
4631
|
+
num_codebooks: dec.num_codebooks ?? PARLER_NUM_CODEBOOKS,
|
|
4632
|
+
audio_vocab: dec.vocab_size ?? PARLER_AUDIO_VOCAB,
|
|
4633
|
+
embed_vocab: (dec.vocab_size ?? PARLER_AUDIO_VOCAB) + 1,
|
|
4634
|
+
max_position: dec.max_position_embeddings ?? 4096,
|
|
4635
|
+
bos_token_id: dec.bos_token_id ?? PARLER_BOS_TOKEN_ID,
|
|
4636
|
+
eos_token_id: dec.eos_token_id ?? PARLER_EOS_TOKEN_ID,
|
|
4637
|
+
pad_token_id: dec.pad_token_id ?? PARLER_PAD_TOKEN_ID
|
|
4638
|
+
};
|
|
4639
|
+
}
|
|
4640
|
+
function baseConfig(c, hidden, heads, layers) {
|
|
4641
|
+
return {
|
|
4642
|
+
hidden_size: hidden,
|
|
4643
|
+
num_layers: layers,
|
|
4644
|
+
num_heads: heads,
|
|
4645
|
+
num_kv_heads: heads,
|
|
4646
|
+
head_dim: Math.floor(hidden / heads),
|
|
4647
|
+
intermediate_size: c.dec_ffn,
|
|
4648
|
+
vocab_size: c.audio_vocab,
|
|
4649
|
+
context_length: c.max_position,
|
|
4650
|
+
rms_norm_eps: c.ln_eps,
|
|
4651
|
+
norm_type: "layernorm",
|
|
4652
|
+
rope_base: 0,
|
|
4653
|
+
rope_dim: 0,
|
|
4654
|
+
kv_layout: "LHSd",
|
|
4655
|
+
is_moe: false,
|
|
4656
|
+
has_vision_tower: false
|
|
4657
|
+
};
|
|
4658
|
+
}
|
|
4659
|
+
/**
|
|
4660
|
+
* T5's bidirectional relative-position bucket (verbatim port of
|
|
4661
|
+
* transformers' T5Attention._relative_position_bucket, bidirectional=True).
|
|
4662
|
+
* Maps a signed relative position (key − query) to a bucket id in [0, num_buckets).
|
|
4663
|
+
*/
|
|
4664
|
+
function relativePositionBucket(relativePosition, numBuckets, maxDistance) {
|
|
4665
|
+
let bucket = 0;
|
|
4666
|
+
let n = relativePosition;
|
|
4667
|
+
const halfBuckets = Math.floor(numBuckets / 2);
|
|
4668
|
+
if (n > 0) bucket += halfBuckets;
|
|
4669
|
+
else n = -n;
|
|
4670
|
+
const maxExact = Math.floor(halfBuckets / 2);
|
|
4671
|
+
const isSmall = n < maxExact;
|
|
4672
|
+
let largeVal = maxExact + Math.floor(Math.log(n / maxExact) / Math.log(maxDistance / maxExact) * (halfBuckets - maxExact));
|
|
4673
|
+
largeVal = Math.min(largeVal, halfBuckets - 1);
|
|
4674
|
+
bucket += isSmall ? n : largeVal;
|
|
4675
|
+
return bucket;
|
|
4676
|
+
}
|
|
4677
|
+
/**
|
|
4678
|
+
* Build the dense per-head relative-position bias B[head, q, k] = bias_table[bucket(k-q), head]
|
|
4679
|
+
* for a length-S encoder sequence. `biasTable` is the learned
|
|
4680
|
+
* relative_attention_bias.weight, row-major [num_buckets, num_heads].
|
|
4681
|
+
* Returns a flat Float32Array of length num_heads*S*S (head-major, then q, then k).
|
|
4682
|
+
*/
|
|
4683
|
+
function buildT5RelativeBias(biasTable, numBuckets, maxDistance, numHeads, S) {
|
|
4684
|
+
const out = new Float32Array(numHeads * S * S);
|
|
4685
|
+
const buckets = new Int32Array(S * S);
|
|
4686
|
+
for (let q = 0; q < S; q++) for (let k = 0; k < S; k++) buckets[q * S + k] = relativePositionBucket(k - q, numBuckets, maxDistance);
|
|
4687
|
+
for (let h = 0; h < numHeads; h++) {
|
|
4688
|
+
const headBase = h * S * S;
|
|
4689
|
+
for (let q = 0; q < S; q++) for (let k = 0; k < S; k++) {
|
|
4690
|
+
const bucket = buckets[q * S + k];
|
|
4691
|
+
out[headBase + q * S + k] = biasTable[bucket * numHeads + h];
|
|
4692
|
+
}
|
|
4693
|
+
}
|
|
4694
|
+
return out;
|
|
4695
|
+
}
|
|
4696
|
+
/**
|
|
4697
|
+
* Build the T5 encoder graph for a concrete description length S. The relative
|
|
4698
|
+
* bias `t5_rel_bias` enters as a host-written activation [num_heads, S, S]. Like
|
|
4699
|
+
* Moonshine, the graph also pre-projects encoder_out through every decoder layer's
|
|
4700
|
+
* encoder_attn.k/v_proj → enc_k_layer{i}/enc_v_layer{i} (frozen cross-attn K/V).
|
|
4701
|
+
*/
|
|
4702
|
+
function generateParlerEncoderGraph(raw, sDesc) {
|
|
4703
|
+
const c = parseParlerConfig(raw);
|
|
4704
|
+
const H = c.enc_d_model;
|
|
4705
|
+
const innerDim = c.enc_heads * c.enc_d_kv;
|
|
4706
|
+
const tensors = {};
|
|
4707
|
+
const nodes = [];
|
|
4708
|
+
const executionOrder = [];
|
|
4709
|
+
const addTensor = (t) => {
|
|
4710
|
+
tensors[t.name] = t;
|
|
4711
|
+
};
|
|
4712
|
+
const addNode = (n) => {
|
|
4713
|
+
nodes.push(n);
|
|
4714
|
+
executionOrder.push(n.id);
|
|
4715
|
+
};
|
|
4716
|
+
const w = (name, shape) => addTensor({
|
|
4717
|
+
name,
|
|
4718
|
+
shape,
|
|
4719
|
+
dtype: "f32",
|
|
4720
|
+
storage: "constant",
|
|
4721
|
+
safetensorsKey: name
|
|
4722
|
+
});
|
|
4723
|
+
const act = (name, shape) => addTensor({
|
|
4724
|
+
name,
|
|
4725
|
+
shape,
|
|
4726
|
+
dtype: "f32",
|
|
4727
|
+
storage: "activation"
|
|
4728
|
+
});
|
|
4729
|
+
const linear = (id, inp, weight, out, K, N) => {
|
|
4730
|
+
w(weight, [N, K]);
|
|
4731
|
+
addNode({
|
|
4732
|
+
id,
|
|
4733
|
+
opType: "MatMul",
|
|
4734
|
+
inputs: [inp, weight],
|
|
4735
|
+
outputs: [out],
|
|
4736
|
+
attributes: {
|
|
4737
|
+
M_tensor: inp,
|
|
4738
|
+
K,
|
|
4739
|
+
N
|
|
4740
|
+
}
|
|
4741
|
+
});
|
|
4742
|
+
};
|
|
4743
|
+
const t5norm = (id, inp, weight, out) => {
|
|
4744
|
+
w(weight, [H]);
|
|
4745
|
+
act(out, [sDesc, H]);
|
|
4746
|
+
addNode({
|
|
4747
|
+
id,
|
|
4748
|
+
opType: "RMSNorm",
|
|
4749
|
+
inputs: [inp, weight],
|
|
4750
|
+
outputs: [out],
|
|
4751
|
+
attributes: {
|
|
4752
|
+
hidden_size: H,
|
|
4753
|
+
eps: c.ln_eps,
|
|
4754
|
+
seq_len_tensor: inp
|
|
4755
|
+
}
|
|
4756
|
+
});
|
|
4757
|
+
};
|
|
4758
|
+
addTensor({
|
|
4759
|
+
name: "input_ids",
|
|
4760
|
+
shape: ["T"],
|
|
4761
|
+
dtype: "u32",
|
|
4762
|
+
storage: "activation"
|
|
4763
|
+
});
|
|
4764
|
+
addTensor({
|
|
4765
|
+
name: "text_encoder.shared.weight",
|
|
4766
|
+
shape: [c.enc_vocab, H],
|
|
4767
|
+
dtype: "f32",
|
|
4768
|
+
storage: "constant",
|
|
4769
|
+
safetensorsKey: "text_encoder.shared.weight"
|
|
4770
|
+
});
|
|
4771
|
+
act("enc_embed", [sDesc, H]);
|
|
4772
|
+
addNode({
|
|
4773
|
+
id: "enc_embed",
|
|
4774
|
+
opType: "Embedding",
|
|
4775
|
+
inputs: ["input_ids", "text_encoder.shared.weight"],
|
|
4776
|
+
outputs: ["enc_embed"],
|
|
4777
|
+
attributes: {
|
|
4778
|
+
vocab_size: c.enc_vocab,
|
|
4779
|
+
hidden_size: H
|
|
4780
|
+
}
|
|
4781
|
+
});
|
|
4782
|
+
addTensor({
|
|
4783
|
+
name: "t5_rel_bias",
|
|
4784
|
+
shape: [
|
|
4785
|
+
c.enc_heads,
|
|
4786
|
+
sDesc,
|
|
4787
|
+
sDesc
|
|
4788
|
+
],
|
|
4789
|
+
dtype: "f32",
|
|
4790
|
+
storage: "activation"
|
|
4791
|
+
});
|
|
4792
|
+
let prev = "enc_embed";
|
|
4793
|
+
for (let i = 0; i < c.enc_layers; i++) {
|
|
4794
|
+
const p = `enc${i}`;
|
|
4795
|
+
const k = (s) => `text_encoder.encoder.block.${i}.${s}`;
|
|
4796
|
+
t5norm(`${p}_norm0`, prev, k("layer.0.layer_norm.weight"), `${p}_n0`);
|
|
4797
|
+
act(`${p}_q`, [sDesc, innerDim]);
|
|
4798
|
+
act(`${p}_k`, [sDesc, innerDim]);
|
|
4799
|
+
act(`${p}_v`, [sDesc, innerDim]);
|
|
4800
|
+
linear(`${p}_qp`, `${p}_n0`, k("layer.0.SelfAttention.q.weight"), `${p}_q`, H, innerDim);
|
|
4801
|
+
linear(`${p}_kp`, `${p}_n0`, k("layer.0.SelfAttention.k.weight"), `${p}_k`, H, innerDim);
|
|
4802
|
+
linear(`${p}_vp`, `${p}_n0`, k("layer.0.SelfAttention.v.weight"), `${p}_v`, H, innerDim);
|
|
4803
|
+
act(`${p}_attn`, [sDesc, innerDim]);
|
|
4804
|
+
addNode({
|
|
4805
|
+
id: `${p}_sa`,
|
|
4806
|
+
opType: "T5Attention",
|
|
4807
|
+
inputs: [
|
|
4808
|
+
`${p}_q`,
|
|
4809
|
+
`${p}_k`,
|
|
4810
|
+
`${p}_v`,
|
|
4811
|
+
"t5_rel_bias"
|
|
4812
|
+
],
|
|
4813
|
+
outputs: [`${p}_attn`],
|
|
4814
|
+
attributes: {
|
|
4815
|
+
num_heads: c.enc_heads,
|
|
4816
|
+
head_dim: c.enc_d_kv,
|
|
4817
|
+
S: sDesc
|
|
4818
|
+
}
|
|
4819
|
+
});
|
|
4820
|
+
act(`${p}_o`, [sDesc, H]);
|
|
4821
|
+
linear(`${p}_op`, `${p}_attn`, k("layer.0.SelfAttention.o.weight"), `${p}_o`, innerDim, H);
|
|
4822
|
+
act(`${p}_res0`, [sDesc, H]);
|
|
4823
|
+
addNode({
|
|
4824
|
+
id: `${p}_add0`,
|
|
4825
|
+
opType: "Add",
|
|
4826
|
+
inputs: [prev, `${p}_o`],
|
|
4827
|
+
outputs: [`${p}_res0`],
|
|
4828
|
+
attributes: {
|
|
4829
|
+
count_tensor: prev,
|
|
4830
|
+
hidden_size: H
|
|
4831
|
+
}
|
|
4832
|
+
});
|
|
4833
|
+
t5norm(`${p}_norm1`, `${p}_res0`, k("layer.1.layer_norm.weight"), `${p}_n1`);
|
|
4834
|
+
act(`${p}_wi0`, [sDesc, c.enc_d_ff]);
|
|
4835
|
+
act(`${p}_wi1`, [sDesc, c.enc_d_ff]);
|
|
4836
|
+
linear(`${p}_wi0p`, `${p}_n1`, k("layer.1.DenseReluDense.wi_0.weight"), `${p}_wi0`, H, c.enc_d_ff);
|
|
4837
|
+
linear(`${p}_wi1p`, `${p}_n1`, k("layer.1.DenseReluDense.wi_1.weight"), `${p}_wi1`, H, c.enc_d_ff);
|
|
4838
|
+
act(`${p}_gelu`, [sDesc, c.enc_d_ff]);
|
|
4839
|
+
addNode({
|
|
4840
|
+
id: `${p}_act`,
|
|
4841
|
+
opType: "GELU",
|
|
4842
|
+
inputs: [`${p}_wi0`],
|
|
4843
|
+
outputs: [`${p}_gelu`],
|
|
4844
|
+
attributes: { count_tensor: `${p}_wi0` }
|
|
4845
|
+
});
|
|
4846
|
+
act(`${p}_gated`, [sDesc, c.enc_d_ff]);
|
|
4847
|
+
addNode({
|
|
4848
|
+
id: `${p}_gate`,
|
|
4849
|
+
opType: "Mul",
|
|
4850
|
+
inputs: [`${p}_gelu`, `${p}_wi1`],
|
|
4851
|
+
outputs: [`${p}_gated`],
|
|
4852
|
+
attributes: {
|
|
4853
|
+
count_tensor: `${p}_gelu`,
|
|
4854
|
+
hidden_size: c.enc_d_ff
|
|
4855
|
+
}
|
|
4856
|
+
});
|
|
4857
|
+
act(`${p}_wo`, [sDesc, H]);
|
|
4858
|
+
linear(`${p}_wop`, `${p}_gated`, k("layer.1.DenseReluDense.wo.weight"), `${p}_wo`, c.enc_d_ff, H);
|
|
4859
|
+
act(`${p}_res1`, [sDesc, H]);
|
|
4860
|
+
addNode({
|
|
4861
|
+
id: `${p}_add1`,
|
|
4862
|
+
opType: "Add",
|
|
4863
|
+
inputs: [`${p}_res0`, `${p}_wo`],
|
|
4864
|
+
outputs: [`${p}_res1`],
|
|
4865
|
+
attributes: {
|
|
4866
|
+
count_tensor: `${p}_res0`,
|
|
4867
|
+
hidden_size: H
|
|
4868
|
+
}
|
|
4869
|
+
});
|
|
4870
|
+
prev = `${p}_res1`;
|
|
4871
|
+
}
|
|
4872
|
+
w("text_encoder.encoder.final_layer_norm.weight", [H]);
|
|
4873
|
+
act("encoder_out", [sDesc, H]);
|
|
4874
|
+
addNode({
|
|
4875
|
+
id: "enc_final_norm",
|
|
4876
|
+
opType: "RMSNorm",
|
|
4877
|
+
inputs: [prev, "text_encoder.encoder.final_layer_norm.weight"],
|
|
4878
|
+
outputs: ["encoder_out"],
|
|
4879
|
+
attributes: {
|
|
4880
|
+
hidden_size: H,
|
|
4881
|
+
eps: c.ln_eps,
|
|
4882
|
+
seq_len_tensor: prev
|
|
4883
|
+
}
|
|
4884
|
+
});
|
|
4885
|
+
const outputs = ["encoder_out"];
|
|
4886
|
+
for (let i = 0; i < c.dec_layers; i++) {
|
|
4887
|
+
const kw = `decoder.model.decoder.layers.${i}.encoder_attn.k_proj.weight`;
|
|
4888
|
+
const vw = `decoder.model.decoder.layers.${i}.encoder_attn.v_proj.weight`;
|
|
4889
|
+
w(kw, [H, H]);
|
|
4890
|
+
w(vw, [H, H]);
|
|
4891
|
+
const encK = `enc_k_layer${i}`;
|
|
4892
|
+
const encV = `enc_v_layer${i}`;
|
|
4893
|
+
act(encK, [sDesc, H]);
|
|
4894
|
+
act(encV, [sDesc, H]);
|
|
4895
|
+
addNode({
|
|
4896
|
+
id: `enc_kproj${i}`,
|
|
4897
|
+
opType: "MatMul",
|
|
4898
|
+
inputs: ["encoder_out", kw],
|
|
4899
|
+
outputs: [encK],
|
|
4900
|
+
attributes: {
|
|
4901
|
+
M_tensor: "encoder_out",
|
|
4902
|
+
K: H,
|
|
4903
|
+
N: H
|
|
4904
|
+
}
|
|
4905
|
+
});
|
|
4906
|
+
addNode({
|
|
4907
|
+
id: `enc_vproj${i}`,
|
|
4908
|
+
opType: "MatMul",
|
|
4909
|
+
inputs: ["encoder_out", vw],
|
|
4910
|
+
outputs: [encV],
|
|
4911
|
+
attributes: {
|
|
4912
|
+
M_tensor: "encoder_out",
|
|
4913
|
+
K: H,
|
|
4914
|
+
N: H
|
|
4915
|
+
}
|
|
4916
|
+
});
|
|
4917
|
+
outputs.push(encK, encV);
|
|
4918
|
+
}
|
|
4919
|
+
return {
|
|
4920
|
+
architecture: "ParlerTextEncoder",
|
|
4921
|
+
config: baseConfig(c, H, c.enc_heads, c.enc_layers),
|
|
4922
|
+
capabilities: CAPS,
|
|
4923
|
+
tensors,
|
|
4924
|
+
nodes,
|
|
4925
|
+
executionOrder,
|
|
4926
|
+
inputs: ["input_ids", "t5_rel_bias"],
|
|
4927
|
+
outputs
|
|
4928
|
+
};
|
|
4929
|
+
}
|
|
4930
|
+
/**
|
|
4931
|
+
* Build the Parler decoder graph for a single decode step (T=1). The host supplies:
|
|
4932
|
+
* - `dec_input_embed` [1, H]: the SUMMED 9-codebook embedding of the current step's
|
|
4933
|
+
* codes PLUS the sinusoidal position (computed host-side from embed_tokens +
|
|
4934
|
+
* embed_positions, since the 9-way embedding sum is cheap on the host and avoids
|
|
4935
|
+
* 9 Embedding ops + an Add tree per step).
|
|
4936
|
+
* - `enc_k_layer{i}` / `enc_v_layer{i}`: frozen cross-attn K/V from the encoder.
|
|
4937
|
+
* Output: 9 logit rows `logits_cb{c}` [1, audio_vocab] (one per codebook head).
|
|
4938
|
+
*
|
|
4939
|
+
* The prompt prefix (transcript) is handled by prefilling its embeddings through the
|
|
4940
|
+
* SAME graph before audio decode begins (the driver feeds the prompt rows first, with
|
|
4941
|
+
* their sinusoidal positions, so the self-attn KV-cache holds [prompt ++ audio]).
|
|
4942
|
+
*/
|
|
4943
|
+
function generateParlerDecoderGraph(raw, sEnc) {
|
|
4944
|
+
const c = parseParlerConfig(raw);
|
|
4945
|
+
const H = c.dec_hidden;
|
|
4946
|
+
const heads = c.dec_heads;
|
|
4947
|
+
const headDim = Math.floor(H / heads);
|
|
4948
|
+
const tensors = {};
|
|
4949
|
+
const nodes = [];
|
|
4950
|
+
const executionOrder = [];
|
|
4951
|
+
const addTensor = (t) => {
|
|
4952
|
+
tensors[t.name] = t;
|
|
4953
|
+
};
|
|
4954
|
+
const addNode = (n) => {
|
|
4955
|
+
nodes.push(n);
|
|
4956
|
+
executionOrder.push(n.id);
|
|
4957
|
+
};
|
|
4958
|
+
const w = (name, shape) => addTensor({
|
|
4959
|
+
name,
|
|
4960
|
+
shape,
|
|
4961
|
+
dtype: "f32",
|
|
4962
|
+
storage: "constant",
|
|
4963
|
+
safetensorsKey: name
|
|
4964
|
+
});
|
|
4965
|
+
const act = (name, shape) => addTensor({
|
|
4966
|
+
name,
|
|
4967
|
+
shape,
|
|
4968
|
+
dtype: "f32",
|
|
4969
|
+
storage: "activation"
|
|
4970
|
+
});
|
|
4971
|
+
const linear = (id, inp, weight, out, K, N) => {
|
|
4972
|
+
w(weight, [N, K]);
|
|
4973
|
+
addNode({
|
|
4974
|
+
id,
|
|
4975
|
+
opType: "MatMul",
|
|
4976
|
+
inputs: [inp, weight],
|
|
4977
|
+
outputs: [out],
|
|
4978
|
+
attributes: {
|
|
4979
|
+
M_tensor: inp,
|
|
4980
|
+
K,
|
|
4981
|
+
N
|
|
4982
|
+
}
|
|
4983
|
+
});
|
|
4984
|
+
};
|
|
4985
|
+
const layernorm = (id, inp, prefix, out) => {
|
|
4986
|
+
w(`${prefix}.weight`, [H]);
|
|
4987
|
+
w(`${prefix}.bias`, [H]);
|
|
4988
|
+
act(out, ["T", H]);
|
|
4989
|
+
addNode({
|
|
4990
|
+
id,
|
|
4991
|
+
opType: "LayerNorm",
|
|
4992
|
+
inputs: [
|
|
4993
|
+
inp,
|
|
4994
|
+
`${prefix}.weight`,
|
|
4995
|
+
`${prefix}.bias`
|
|
4996
|
+
],
|
|
4997
|
+
outputs: [out],
|
|
4998
|
+
attributes: {
|
|
4999
|
+
hidden_size: H,
|
|
5000
|
+
eps: c.ln_eps,
|
|
5001
|
+
seq_len_tensor: inp,
|
|
5002
|
+
has_bias: true
|
|
5003
|
+
}
|
|
5004
|
+
});
|
|
5005
|
+
};
|
|
5006
|
+
const inputs = ["dec_input_embed"];
|
|
5007
|
+
addTensor({
|
|
5008
|
+
name: "dec_input_embed",
|
|
5009
|
+
shape: ["T", H],
|
|
5010
|
+
dtype: "f32",
|
|
5011
|
+
storage: "activation"
|
|
5012
|
+
});
|
|
5013
|
+
let prev = "dec_input_embed";
|
|
5014
|
+
for (let i = 0; i < c.dec_layers; i++) {
|
|
5015
|
+
const p = `dec${i}`;
|
|
5016
|
+
const lk = `decoder.model.decoder.layers.${i}`;
|
|
5017
|
+
layernorm(`${p}_n1`, prev, `${lk}.self_attn_layer_norm`, `${p}_ln1`);
|
|
5018
|
+
act(`${p}_q`, ["T", H]);
|
|
5019
|
+
act(`${p}_k`, ["T", H]);
|
|
5020
|
+
act(`${p}_v`, ["T", H]);
|
|
5021
|
+
linear(`${p}_qp`, `${p}_ln1`, `${lk}.self_attn.q_proj.weight`, `${p}_q`, H, H);
|
|
5022
|
+
linear(`${p}_kp`, `${p}_ln1`, `${lk}.self_attn.k_proj.weight`, `${p}_k`, H, H);
|
|
5023
|
+
linear(`${p}_vp`, `${p}_ln1`, `${lk}.self_attn.v_proj.weight`, `${p}_v`, H, H);
|
|
5024
|
+
addTensor({
|
|
5025
|
+
name: `${p}_kcache`,
|
|
5026
|
+
shape: ["L_max", H],
|
|
5027
|
+
dtype: "f32",
|
|
5028
|
+
storage: "kv_cache"
|
|
5029
|
+
});
|
|
5030
|
+
addTensor({
|
|
5031
|
+
name: `${p}_vcache`,
|
|
5032
|
+
shape: ["L_max", H],
|
|
5033
|
+
dtype: "f32",
|
|
5034
|
+
storage: "kv_cache"
|
|
5035
|
+
});
|
|
5036
|
+
addNode({
|
|
5037
|
+
id: `${p}_kappend`,
|
|
5038
|
+
opType: "KVCacheAppend",
|
|
5039
|
+
inputs: [`${p}_k`],
|
|
5040
|
+
outputs: [`${p}_kcache`],
|
|
5041
|
+
attributes: {
|
|
5042
|
+
width: H,
|
|
5043
|
+
T_tensor: `${p}_k`
|
|
5044
|
+
}
|
|
5045
|
+
});
|
|
5046
|
+
addNode({
|
|
5047
|
+
id: `${p}_vappend`,
|
|
5048
|
+
opType: "KVCacheAppend",
|
|
5049
|
+
inputs: [`${p}_v`],
|
|
5050
|
+
outputs: [`${p}_vcache`],
|
|
5051
|
+
attributes: {
|
|
5052
|
+
width: H,
|
|
5053
|
+
T_tensor: `${p}_v`
|
|
5054
|
+
}
|
|
5055
|
+
});
|
|
5056
|
+
act(`${p}_sa`, ["T", H]);
|
|
5057
|
+
addNode({
|
|
5058
|
+
id: `${p}_self_attn`,
|
|
5059
|
+
opType: "Attention",
|
|
5060
|
+
inputs: [
|
|
5061
|
+
`${p}_q`,
|
|
5062
|
+
`${p}_kcache`,
|
|
5063
|
+
`${p}_vcache`
|
|
5064
|
+
],
|
|
5065
|
+
outputs: [`${p}_sa`],
|
|
5066
|
+
attributes: {
|
|
5067
|
+
hidden_size: H,
|
|
5068
|
+
num_q_heads: heads,
|
|
5069
|
+
num_kv_heads: c.dec_kv_heads,
|
|
5070
|
+
head_dim: headDim,
|
|
5071
|
+
causal: true,
|
|
5072
|
+
layer_index: i
|
|
5073
|
+
}
|
|
5074
|
+
});
|
|
5075
|
+
act(`${p}_sao`, ["T", H]);
|
|
5076
|
+
linear(`${p}_sap`, `${p}_sa`, `${lk}.self_attn.out_proj.weight`, `${p}_sao`, H, H);
|
|
5077
|
+
act(`${p}_res1`, ["T", H]);
|
|
5078
|
+
addNode({
|
|
5079
|
+
id: `${p}_add1`,
|
|
5080
|
+
opType: "Add",
|
|
5081
|
+
inputs: [prev, `${p}_sao`],
|
|
5082
|
+
outputs: [`${p}_res1`],
|
|
5083
|
+
attributes: {
|
|
5084
|
+
count_tensor: prev,
|
|
5085
|
+
hidden_size: H
|
|
5086
|
+
}
|
|
5087
|
+
});
|
|
5088
|
+
layernorm(`${p}_n2`, `${p}_res1`, `${lk}.encoder_attn_layer_norm`, `${p}_ln2`);
|
|
5089
|
+
act(`${p}_cq`, ["T", H]);
|
|
5090
|
+
linear(`${p}_cqp`, `${p}_ln2`, `${lk}.encoder_attn.q_proj.weight`, `${p}_cq`, H, H);
|
|
5091
|
+
const encK = `enc_k_layer${i}`;
|
|
5092
|
+
const encV = `enc_v_layer${i}`;
|
|
5093
|
+
act(encK, [sEnc, H]);
|
|
5094
|
+
act(encV, [sEnc, H]);
|
|
5095
|
+
inputs.push(encK, encV);
|
|
5096
|
+
act(`${p}_ca`, ["T", H]);
|
|
5097
|
+
addNode({
|
|
5098
|
+
id: `${p}_cross_attn`,
|
|
5099
|
+
opType: "CrossAttention",
|
|
5100
|
+
inputs: [
|
|
5101
|
+
`${p}_cq`,
|
|
5102
|
+
encK,
|
|
5103
|
+
encV
|
|
5104
|
+
],
|
|
5105
|
+
outputs: [`${p}_ca`],
|
|
5106
|
+
attributes: {
|
|
5107
|
+
num_q_heads: heads,
|
|
5108
|
+
num_kv_heads: c.dec_kv_heads,
|
|
5109
|
+
head_dim: headDim
|
|
5110
|
+
}
|
|
5111
|
+
});
|
|
5112
|
+
act(`${p}_cao`, ["T", H]);
|
|
5113
|
+
linear(`${p}_cap`, `${p}_ca`, `${lk}.encoder_attn.out_proj.weight`, `${p}_cao`, H, H);
|
|
5114
|
+
act(`${p}_res2`, ["T", H]);
|
|
5115
|
+
addNode({
|
|
5116
|
+
id: `${p}_add2`,
|
|
5117
|
+
opType: "Add",
|
|
5118
|
+
inputs: [`${p}_res1`, `${p}_cao`],
|
|
5119
|
+
outputs: [`${p}_res2`],
|
|
5120
|
+
attributes: {
|
|
5121
|
+
count_tensor: `${p}_res1`,
|
|
5122
|
+
hidden_size: H
|
|
5123
|
+
}
|
|
5124
|
+
});
|
|
5125
|
+
layernorm(`${p}_n3`, `${p}_res2`, `${lk}.final_layer_norm`, `${p}_ln3`);
|
|
5126
|
+
act(`${p}_fc1`, ["T", c.dec_ffn]);
|
|
5127
|
+
linear(`${p}_fc1p`, `${p}_ln3`, `${lk}.fc1.weight`, `${p}_fc1`, H, c.dec_ffn);
|
|
5128
|
+
act(`${p}_gelu`, ["T", c.dec_ffn]);
|
|
5129
|
+
addNode({
|
|
5130
|
+
id: `${p}_act`,
|
|
5131
|
+
opType: "GeluErf",
|
|
5132
|
+
inputs: [`${p}_fc1`],
|
|
5133
|
+
outputs: [`${p}_gelu`],
|
|
5134
|
+
attributes: { count_tensor: `${p}_fc1` }
|
|
5135
|
+
});
|
|
5136
|
+
act(`${p}_fc2`, ["T", H]);
|
|
5137
|
+
linear(`${p}_fc2p`, `${p}_gelu`, `${lk}.fc2.weight`, `${p}_fc2`, c.dec_ffn, H);
|
|
5138
|
+
act(`${p}_res3`, ["T", H]);
|
|
5139
|
+
addNode({
|
|
5140
|
+
id: `${p}_add3`,
|
|
5141
|
+
opType: "Add",
|
|
5142
|
+
inputs: [`${p}_res2`, `${p}_fc2`],
|
|
5143
|
+
outputs: [`${p}_res3`],
|
|
5144
|
+
attributes: {
|
|
5145
|
+
count_tensor: `${p}_res2`,
|
|
5146
|
+
hidden_size: H
|
|
5147
|
+
}
|
|
5148
|
+
});
|
|
5149
|
+
prev = `${p}_res3`;
|
|
5150
|
+
}
|
|
5151
|
+
layernorm("dec_final_norm", prev, "decoder.model.decoder.layer_norm", "dec_normed");
|
|
5152
|
+
act("dec_last", [1, H]);
|
|
5153
|
+
addNode({
|
|
5154
|
+
id: "slice_last",
|
|
5155
|
+
opType: "SliceLastRow",
|
|
5156
|
+
inputs: ["dec_normed"],
|
|
5157
|
+
outputs: ["dec_last"],
|
|
5158
|
+
attributes: { width: H }
|
|
5159
|
+
});
|
|
5160
|
+
const outputs = [];
|
|
5161
|
+
for (let cb = 0; cb < c.num_codebooks; cb++) {
|
|
5162
|
+
const headW = `decoder.lm_heads.${cb}.weight`;
|
|
5163
|
+
w(headW, [c.audio_vocab, H]);
|
|
5164
|
+
const logit = `logits_cb${cb}`;
|
|
5165
|
+
act(logit, [1, c.audio_vocab]);
|
|
5166
|
+
addNode({
|
|
5167
|
+
id: `lm_head${cb}`,
|
|
5168
|
+
opType: "MatMul",
|
|
5169
|
+
inputs: ["dec_last", headW],
|
|
5170
|
+
outputs: [logit],
|
|
5171
|
+
attributes: {
|
|
5172
|
+
M_tensor: "dec_last",
|
|
5173
|
+
K: H,
|
|
5174
|
+
N: c.audio_vocab
|
|
5175
|
+
}
|
|
5176
|
+
});
|
|
5177
|
+
outputs.push(logit);
|
|
5178
|
+
}
|
|
5179
|
+
return {
|
|
5180
|
+
architecture: "ParlerTTSDecoder",
|
|
5181
|
+
config: baseConfig(c, H, heads, c.dec_layers),
|
|
5182
|
+
capabilities: CAPS,
|
|
5183
|
+
tensors,
|
|
5184
|
+
nodes,
|
|
5185
|
+
executionOrder,
|
|
5186
|
+
inputs,
|
|
5187
|
+
outputs
|
|
5188
|
+
};
|
|
5189
|
+
}
|
|
5190
|
+
/**
|
|
5191
|
+
* Apply the Parler/MusicGen delay pattern to a [numCodebooks, T] code grid produced
|
|
5192
|
+
* by AR decode. During generation, codebook `i` is delayed by `i` steps: at decode
|
|
5193
|
+
* step t the model emits, for each codebook i, the code for ACOUSTIC frame (t − i).
|
|
5194
|
+
* To recover the aligned acoustic grid we simply read codebook i's stream starting
|
|
5195
|
+
* at offset i. This returns the de-delayed [numCodebooks, frames] grid (codebook-major)
|
|
5196
|
+
* ready for the DAC decoder, where frames = T − (numCodebooks − 1).
|
|
5197
|
+
*
|
|
5198
|
+
* `delayed` is codebook-major [numCodebooks, T] (delayed[i*T + t]).
|
|
5199
|
+
*/
|
|
5200
|
+
function revertDelayPattern(delayed, numCodebooks, T) {
|
|
5201
|
+
const frames = T - (numCodebooks - 1);
|
|
5202
|
+
if (frames <= 0) return {
|
|
5203
|
+
codes: new Uint32Array(0),
|
|
5204
|
+
frames: 0
|
|
5205
|
+
};
|
|
5206
|
+
const codes = new Uint32Array(numCodebooks * frames);
|
|
5207
|
+
for (let i = 0; i < numCodebooks; i++) for (let f = 0; f < frames; f++) codes[i * frames + f] = delayed[i * T + (f + i)];
|
|
5208
|
+
return {
|
|
5209
|
+
codes,
|
|
5210
|
+
frames
|
|
5211
|
+
};
|
|
5212
|
+
}
|
|
5213
|
+
/**
|
|
5214
|
+
* Single-graph architecture entry for `ParlerTTSForConditionalGeneration`.
|
|
5215
|
+
*
|
|
5216
|
+
* Registering this in the architecture registry makes the loader RECOGNIZE the
|
|
5217
|
+
* Parler checkpoint (so `isArchitectureSupported` is true and the error is precise)
|
|
5218
|
+
* rather than reporting "Unsupported model architecture". Parler is an
|
|
5219
|
+
* encoder-decoder codec-LM (Flan-T5 encoder + AR decoder + DAC codec) that cannot
|
|
5220
|
+
* be expressed as one text graph, so — exactly like Moonshine STT — it is not
|
|
5221
|
+
* loadable through the single-graph `generate()` path; callers use the dedicated
|
|
5222
|
+
* ParlerTTS engine (or `g.speak(text, { model, describeVoice })`), which drives the
|
|
5223
|
+
* encoder/decoder/DAC graphs directly.
|
|
5224
|
+
*/
|
|
5225
|
+
function generateParlerGraph() {
|
|
5226
|
+
throw new Error("Parler-TTS is an encoder-decoder (Flan-T5 + DAC) describe-your-voice TTS model and is not loadable via the single-graph generate() path. Use the dedicated TTS engine instead:\n import { ParlerTTS } from '@tryhamster/gerbil';\n const tts = await ParlerTTS.create();\n const { pcm, sampleRate } = await tts.speak('Hello.', { describeVoice: 'a warm female voice' });\nor, from the high-level Gerbil class:\n await g.speak('Hello.', { model: 'parler-tts/parler-tts-mini-v1', describeVoice: 'a warm female voice' });");
|
|
5227
|
+
}
|
|
5228
|
+
|
|
4587
5229
|
//#endregion
|
|
4588
5230
|
//#region src/gpu/architectures/qwen2.ts
|
|
4589
5231
|
function generateQwen2Graph(rawConfig, dtype, groupSizeOverride, _kvDtype, embedding) {
|
|
@@ -6097,7 +6739,8 @@ const ARCHITECTURES = {
|
|
|
6097
6739
|
Gemma3Model: generateGemma3EncoderGraph,
|
|
6098
6740
|
Gemma4ForConditionalGeneration: generateGemma4Graph,
|
|
6099
6741
|
MoonshineForConditionalGeneration: generateMoonshineGraph,
|
|
6100
|
-
KaniTTS2ForCausalLM: generateKaniTtsGraph
|
|
6742
|
+
KaniTTS2ForCausalLM: generateKaniTtsGraph,
|
|
6743
|
+
ParlerTTSForConditionalGeneration: generateParlerGraph
|
|
6101
6744
|
};
|
|
6102
6745
|
/**
|
|
6103
6746
|
* Look up and generate a ModelGraph for a given architecture string.
|
|
@@ -6123,5 +6766,5 @@ function isArchitectureSupported(name) {
|
|
|
6123
6766
|
}
|
|
6124
6767
|
|
|
6125
6768
|
//#endregion
|
|
6126
|
-
export {
|
|
6127
|
-
//# sourceMappingURL=architectures-
|
|
6769
|
+
export { kaniSinTensor as A, computeKaniPositions as C, kaniAttentionLayerIndices as D, generateNanoCodecDecoderGraph as E, CANONICAL_KEYS as F, DTYPE_BYTES as I, GEMMA4_VIS_KEYS as L, parseKaniConfig as M, DEFAULT_GROUP_SIZE as N, kaniCosTensor as O, quantizeInt4 as P, createDefaultHFKeyMapper as R, buildKaniLayerCosSin as S, generateKaniTtsGraph as T, moonshineEncoderFrames as _, PARLER_DAC_LATENT_DIM as a, KANI_START_OF_HUMAN as b, PARLER_SAMPLE_RATE as c, generateParlerEncoderGraph as d, parseParlerConfig as f, generateMoonshineEncoderGraph as g, generateMoonshineDecoderGraph as h, PARLER_DAC_DECODER_DIM as i, nanoCodecWeightMap as j, kaniLayerAlpha as k, buildT5RelativeBias as l, MOONSHINE_REMAINING_WORK as m, isArchitectureSupported as n, PARLER_DECODER_RATES as o, revertDelayPattern as p, PARLER_BOS_TOKEN_ID as r, PARLER_EOS_TOKEN_ID as s, generateGraph as t, generateParlerDecoderGraph as u, parseMoonshineConfig as v, foldNanoCodecWeightNorm as w, audioTokensToCodes as x, KANI_END_OF_HUMAN as y };
|
|
6770
|
+
//# sourceMappingURL=architectures-DmZMEFsA.mjs.map
|