Archived
fishomni: voice cloning -- Fish S2's codec encoder (strided causal convolutions, a windowed transformer, two downsamplings and the residual quantiser) over a request's reference clips, their frames embedded as made frames are, the tower teacher-forced with them as fish-speech's prefill does, and the window of main picks seeded from the prompt; the reference prompt with each transcript tagged as the prompt builder tags it
This commit is contained in:
1 parent
bdc5c5b063
commit
871aa7dd97
3 files changed
+689
-101
No files matched your search
+637
-99
@@ -26,17 +26,24 @@
|
||||
* Codec: Fish S2's modified DAC decoder: ten codebooks of dimension 8 (one of 4096, nine of
|
||||
* 1024), each projected to 1024 and summed; an 8-layer transformer (16 heads of 64, a causal
|
||||
* window of 128 frames); two 2x upsamplings with ConvNeXt blocks; four transposed convolutions
|
||||
* (8, 8, 4, 2) with snake residual units; 2048 samples a frame.
|
||||
* (8, 8, 4, 2) with snake residual units; 2048 samples a frame. Its encoder, for a reference
|
||||
* clip, mirrors it: four blocks of snake residual units and strided causal convolutions (2, 4,
|
||||
* 8, 8) with a 4-layer transformer (window 512 rows) after the last, two 2x downsamplings with
|
||||
* ConvNeXt blocks, an 8-layer transformer (window 128 frames), and a residual quantiser,
|
||||
* nearest code by cosine, the semantic codebook first.
|
||||
*
|
||||
* ============================== THE PASSES ==============================
|
||||
*
|
||||
* The core's audio generator in its chunked form, one branch a request (no guidance):
|
||||
*
|
||||
* PROMPT the backbone over the whole prompt; the last row's hidden state into the lane; the
|
||||
* tower teacher-forced over every row. A prompt is text, so every row's label is text
|
||||
* and every layer's code embedding is zero; the tower still runs, because its attention
|
||||
* reads those rows' keys and values from every later frame. The last row's tower pass is
|
||||
* the first frame's, and FRAME 0 overwrites it.
|
||||
* tower teacher-forced over every row. A text prompt's labels are all text, so every
|
||||
* layer's code embedding is zero; the tower still runs, because its attention reads
|
||||
* those rows' keys and values from every later frame. With references the codec's
|
||||
* encoder first turns their clips into frames, each a prompt row embedded as a made
|
||||
* frame is; a row before a frame is labelled with it, a frame's row carries its unseen
|
||||
* codes, and the window of main picks starts with the prompt's last frames. The last
|
||||
* row's tower pass is the first frame's, and FRAME 0 overwrites it.
|
||||
* FRAME frame j of every lane in flight. The main token is drawn from the hidden state of the
|
||||
* row before the frame's (the prompt's last row for frame 0) over 4097 candidates (the
|
||||
* end of speech and the 4096 semantic tokens), with Fish's repetition-aware choice
|
||||
@@ -72,9 +79,14 @@ using namespace rad::arch;
|
||||
|
||||
/* ================================================================== the model's constants */
|
||||
constexpr int64_t kMaxLanes = 4;
|
||||
/* References (voice cloning): at most 8 clips of 30 s together. fish-speech's server takes each
|
||||
* up to 60 s; here the encoder's first stages, 64 and 128 channels at the samples' rate, hold
|
||||
* about 170 MB a buffer at 30 s. A clip's frames are prompt rows. */
|
||||
constexpr int64_t kMaxRefFrames = 646;
|
||||
constexpr int32_t kMaxRefs = 8;
|
||||
/* The longest prompt and speech. fish-speech's default request is 2048 frames (95 s); a plain
|
||||
* prompt is a few dozen rows a sentence. */
|
||||
constexpr int64_t kMaxPrompt = 2048, kMaxFrames = 2048;
|
||||
* prompt is a few dozen rows a sentence, and the references add a row a frame. */
|
||||
constexpr int64_t kMaxPrompt = 3072, kMaxFrames = 2048;
|
||||
/* A frame: ten codes, code 0 of 4096 (the semantic band), codes 1..9 drawn from heads of 4096 and
|
||||
* read by the codec through 1024-entry codebooks. */
|
||||
constexpr int64_t kCodes = 10, kVocab = 4096, kResVocab = 1024;
|
||||
@@ -101,6 +113,7 @@ static const double kRasTemp = 1.0, kRasTopP = 0.95;
|
||||
* reach back about nine frames through the convolutions after the transformer (kConvCtx, rounded
|
||||
* up), whose rows reach back kAttnWin - 1 frames. The dequantiser is per frame. */
|
||||
constexpr int64_t kSamplesPerFrame = 2048, kSampleRate = 44100;
|
||||
constexpr int64_t kMaxRefSamples = kMaxRefFrames * kSamplesPerFrame; /* 30.0 s */
|
||||
constexpr int64_t kVocChunk = 16, kConvCtx = 12, kAttnWin = 128;
|
||||
constexpr int64_t kVocContext = (kAttnWin - 1) + kConvCtx;
|
||||
constexpr int64_t kConvFrames = kConvCtx + kVocChunk;
|
||||
@@ -108,6 +121,12 @@ constexpr int64_t kCbDim = 8, kLat = 1024, kDec = 1536;
|
||||
/* The codec transformer: 8 layers of 1024, 16 heads of 64, a SwiGLU of 3072. */
|
||||
constexpr int64_t kCtLayers = 8, kCtHeads = 16, kCtHeadDim = 64, kCtFf = 3072;
|
||||
static const double kCtTheta = 10000.0, kCtEps = 1e-5;
|
||||
/* The encoder (modded_dac.py's Encoder): four blocks, each three residual units and a strided
|
||||
* causal conv (kernel two strides), a 4-layer transformer over a window of 512 rows after the
|
||||
* last; the quantiser then halves the rate twice to the frames (rvq.py). Its rows are 1, 2, 8, 64
|
||||
* and 512 samples, then 1024 and 2048. */
|
||||
static const int64_t kEncStride[4] = { 2, 4, 8, 8 };
|
||||
constexpr int64_t kEncWin = 512, kEncLayers = 4;
|
||||
/* The codec's sequence rates, rows a frame: the transformer's frames, the convolutions' frames,
|
||||
* the two upsamplings', and each decoder block's output (8, 8, 4, 2 times). */
|
||||
constexpr int kRates = 8;
|
||||
@@ -120,6 +139,15 @@ static const int64_t kRate[kRates] = { 1, 1, 2, 4, 32, 256, 1024, 2048 };
|
||||
static const char* kPlain =
|
||||
"<|im_start|>system\nconvert the provided text to speech<|im_end|>\n"
|
||||
"<|im_start|>user\n{input}<|im_end|>\n<|im_start|>assistant\n<|voice|>";
|
||||
/* With references: the system turn's instruction for them, then each one's numbered header, its
|
||||
* transcript and its frames (the core tokenises the instruction apart from the first header, as
|
||||
* fish-speech does: joined, ":\n\n" is one token), then the user turn as kPlain's. The
|
||||
* transcript's speaker tag comes first in it, from text_prepare. */
|
||||
static const char* kRefPlain =
|
||||
"<|im_start|>system\nconvert the provided text to speech reference to the following:"
|
||||
"{references}<|im_end|>\n<|im_start|>user\n{input}<|im_end|>\n<|im_start|>assistant\n"
|
||||
"<|voice|>";
|
||||
static const char* kRefItem = "\n\n[Reference {index}]\nText: {text}\nSpeech: {audio}";
|
||||
|
||||
/* Weight groups (movement units): the backbone's layers are 0..39 (the blocks' own), the tower's
|
||||
* 40..52 like their KV layers, then the frame's tables and the codec. */
|
||||
@@ -147,6 +175,9 @@ struct TowerLayer {
|
||||
rad_op op_attn = 0, op_kv = 0, op_sig = 0, op_mul = 0, op_o = 0, op_ar1 = 0, op_add1 = 0;
|
||||
rad_op op_fn = 0, op_gu = 0, op_act = 0, op_down = 0, op_ar2 = 0, op_add2 = 0, op_fin = 0;
|
||||
rad_op op_head = 0, op_samp = 0;
|
||||
/* PROMPT with references: the label embedding over the prompt's rows (and layer 0's unseen
|
||||
* codes), and its norm. */
|
||||
rad_op op_pemb = 0, op_peadd = 0, op_pen = 0;
|
||||
};
|
||||
|
||||
/* The codec transformer's layer: fused q|k|v with rotary at the frames' positions, causal
|
||||
@@ -180,6 +211,27 @@ struct VBlock {
|
||||
rad_op op_s = 0, op_t = 0;
|
||||
VRes res[3];
|
||||
};
|
||||
/* An encoder block: three residual units, a snake, the strided conv. */
|
||||
struct EBlock {
|
||||
VRes res[3];
|
||||
rad_weight a = 0;
|
||||
rad_op op_s = 0;
|
||||
VConv down{};
|
||||
};
|
||||
/* The quantiser's 2x downsampling: a strided causal conv (plain weights) and a ConvNeXt block. */
|
||||
struct EDown {
|
||||
VConv c{};
|
||||
rad_weight w_dw = 0, b_dw = 0, w_ln = 0, b_ln = 0, w_p1 = 0, b_p1 = 0, w_p2 = 0, b_p2 = 0;
|
||||
rad_weight w_g = 0;
|
||||
rad_op op_dw = 0, op_ln = 0, op_p1 = 0, op_gelu = 0, op_p2 = 0, op_g = 0, op_add = 0;
|
||||
};
|
||||
/* A residual quantiser step's weights, f32: the in-projection, the codebook, the out-projection. */
|
||||
struct EQuant {
|
||||
rad_weight in_w = 0, in_b = 0, cb = 0, out_w = 0, out_b = 0;
|
||||
rad_op op = 0;
|
||||
};
|
||||
/* A codec transformer's working set: its residual stream and scratch. */
|
||||
struct CtSet { rad_buf x = 0, h = 0, qkv = 0, attn = 0, o = 0, gu = 0, ff = 0; };
|
||||
|
||||
struct Model {
|
||||
Names nm{""};
|
||||
@@ -247,8 +299,29 @@ struct Model {
|
||||
rad_buf b_ux[3] = {}, b_ua = 0, b_ub = 0, b_uh = 0, b_ug2 = 0;
|
||||
rad_buf b_vs[5] = {}, b_vt[5] = {}, b_va[5] = {}, b_vb[5] = {};
|
||||
rad_buf b_vout = 0, b_audio = 0;
|
||||
|
||||
/* ---- the codec's encoder (PROMPT with references): the clips in, their frames' codes out
|
||||
* into b_pc, the prompt rows' codes ([10, kMaxPrompt + 1] code-major, -2 and -1 at a token's
|
||||
* row), from which the rows' embeddings and the tower's labels are read */
|
||||
rad_op op_ecast = 0, op_epos = 0, op_e5 = 0, op_etn = 0, op_pqn = 0;
|
||||
VConv e0{}, e6{};
|
||||
EBlock eb[4];
|
||||
std::vector<CodecLayer> et, pq;
|
||||
rad_weight w_etn = 0, w_e5 = 0, w_pqn = 0;
|
||||
EDown ed[2];
|
||||
EQuant eq[kCodes];
|
||||
rad_op op_pc0 = 0, op_pc1 = 0, op_pfb = 0, op_pufill = 0, op_pu = 0, op_ptefill = 0;
|
||||
rad_op op_rascopy = 0;
|
||||
rad_buf b_ein = 0, b_ex[4] = {}, b_et[4] = {}, b_ea[4] = {}, b_ex4 = 0, b_et4 = 0, b_ea4 = 0;
|
||||
CtSet ecs{};
|
||||
rad_buf b_epos = 0, b_ed1 = 0, b_ez = 0, b_dua = 0, b_dub = 0, b_duh = 0, b_dug = 0;
|
||||
rad_buf b_pc = 0, b_pte = 0, b_pu = 0;
|
||||
};
|
||||
|
||||
/* The width of the prompt rows' code plane: every row and one past the last, a token's (the last
|
||||
* row's label). */
|
||||
constexpr int64_t kPcW = kMaxPrompt + 1;
|
||||
|
||||
static Model g_model[MAX_RANKS];
|
||||
|
||||
/* ================================================================== declare helpers */
|
||||
@@ -825,6 +898,28 @@ static int declare_tower(RadBuilder* b, const RadModelMeta* meta, Model& m) {
|
||||
m.b_tff = decl_b(b, nm.f("tower.ff"), RAD_BF16, { T, ff });
|
||||
m.b_tho = decl_b(b, nm.f("tower.out"), RAD_BF16, { T, D });
|
||||
m.b_hlog = decl_b(b, nm.f("tower.hlog"), RAD_F32, { S, kVocab });
|
||||
/* PROMPT with references: every row's label embedding, and its unseen codes (2..9 of the
|
||||
* row's own frame, zero at a token's row), summed in f32 and rounded once as op_u's. */
|
||||
m.b_pte = decl_b(b, nm.f("tower.pe"), RAD_BF16, { T, D });
|
||||
m.b_pu = decl_b(b, nm.f("tower.pu"), RAD_BF16, { T, D });
|
||||
m.op_ptefill = rw(b, RAD_OP(b, "fill",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, T), RAD_INT("n", D),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
RAD_NOWEIGHTS),
|
||||
{}, { m.b_pte });
|
||||
m.op_pufill = rw(b, RAD_OP(b, "fill",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, T), RAD_INT("n", D),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
RAD_NOWEIGHTS),
|
||||
{}, { m.b_pu });
|
||||
m.op_pu = rw(b, RAD_OP(b, "rvq_embed",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, T), RAD_INT("n_embd", D),
|
||||
RAD_INT("n_codes", kCodes - 2), RAD_INT("vocab", kVocab),
|
||||
RAD_INT("off0", 0), RAD_F64("scale", 1.0),
|
||||
RAD_STR("dtype", "bf16"), RAD_INT("round", 1)),
|
||||
RAD_WEIGHTS(m.w_cb, m.w_cb)),
|
||||
{ m.b_pc }, { m.b_pu });
|
||||
if (!m.op_ptefill || !m.op_pufill || !m.op_pu) return RAD_E_INVAL;
|
||||
|
||||
for (int i = 0; i < kTower; ++i) {
|
||||
TowerLayer& L = m.tw[i];
|
||||
@@ -897,6 +992,20 @@ static int declare_tower(RadBuilder* b, const RadModelMeta* meta, Model& m) {
|
||||
{ m.b_codes }, { m.b_te });
|
||||
if (i == 0) L.op_eadd = add_op(b, S, D, m.b_te, m.b_ug, m.b_te);
|
||||
L.op_en = norm_op(b, S, D, g.eps, 1.0, L.w_en, { m.b_te }, m.b_tcat);
|
||||
/* PROMPT with references: the label is the next row's frame, off the prompt rows' code
|
||||
* plane a row on. Code 0 there is the frame's own c0, so a by-candidate table reads it
|
||||
* one row on; a token row's code (-2 for code 0, -1 for the others) reads nothing, and
|
||||
* its embedding stays the zero the fill leaves. */
|
||||
L.op_pemb = rw(b, RAD_OP(b, "rvq_embed",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, T), RAD_INT("n_embd", D),
|
||||
RAD_INT("n_codes", 1), RAD_INT("vocab", kVocab),
|
||||
RAD_INT("off0", by_cand ? 1 : 0),
|
||||
RAD_F64("scale", 1.0), RAD_STR("dtype", "bf16")),
|
||||
RAD_WEIGHTS(L.w_emb, L.w_emb)),
|
||||
{ m.b_pc }, { m.b_pte });
|
||||
if (i == 0) L.op_peadd = add_op(b, T, D, m.b_pte, m.b_pu, m.b_pte);
|
||||
L.op_pen = norm_op(b, T, D, g.eps, 1.0, L.w_en, { m.b_pte }, m.b_tcat);
|
||||
if (!L.op_pemb || !L.op_pen || (i == 0 && !L.op_peadd)) return RAD_E_INVAL;
|
||||
/* Layer 0's hidden state is the backbone's: the prompt's rows (PROMPT) or the frame's
|
||||
* plane (FRAME); both stay live to here. */
|
||||
L.op_hn = i == 0 ? norm_op(b, T, D, g.eps, 1.0, L.w_hn, { m.a_h.x, m.b_plane }, m.b_tcat)
|
||||
@@ -1013,7 +1122,33 @@ static int declare_frame_tail(RadBuilder* b, Model& m) {
|
||||
RAD_STR("dtype", "bf16")),
|
||||
RAD_NOWEIGHTS),
|
||||
{ m.a_h.x }, { m.b_hid });
|
||||
return m.op_store && m.op_u && m.op_uscat && m.op_fb && m.op_fstore ? RAD_OK : RAD_E_INVAL;
|
||||
/* PROMPT with references: a frame's row embedded as a made frame's is, off the rows' code
|
||||
* plane (code 0 its own c0, so the candidate table is read one row on); a token's row, whose
|
||||
* code 0 is -2, keeps its token's embedding. */
|
||||
m.op_pfb = rw(b, RAD_OP(b, "rvq_embed",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, m.g.max_tok), RAD_INT("n_embd", D),
|
||||
RAD_INT("n_codes", 2), RAD_INT("vocab", kVocab),
|
||||
RAD_INT("off0", 1), RAD_F64("scale", 1.0 / std::sqrt(2.0)),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
RAD_WEIGHTS(m.w_cemb, m.w_cb)),
|
||||
{ m.b_pc }, { m.b_x });
|
||||
/* ... and the lane's window of main picks starts with the prompt's last 16 rows', a frame's
|
||||
* as its candidate (c0 + 1) and a token's as no pick (-2 + 1). */
|
||||
std::string src, one;
|
||||
for (int64_t i = 0; i < kRasWin; ++i) {
|
||||
src += (i ? " " : "") + std::to_string(i);
|
||||
one += i ? " 1" : "1";
|
||||
}
|
||||
m.op_rascopy = rw(b, RAD_OP(b, "int_affine",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, kRasWin), RAD_STR("src", src.c_str()),
|
||||
RAD_STR("mul", one.c_str()),
|
||||
RAD_STR("add", one.c_str())),
|
||||
RAD_NOWEIGHTS),
|
||||
{ m.b_pc }, { m.b_ras });
|
||||
return m.op_store && m.op_u && m.op_uscat && m.op_fb && m.op_fstore && m.op_pfb &&
|
||||
m.op_rascopy
|
||||
? RAD_OK
|
||||
: RAD_E_INVAL;
|
||||
}
|
||||
|
||||
/* ================================================================== the codec */
|
||||
@@ -1050,6 +1185,111 @@ static rad_op snake_op(RadBuilder* b, int64_t rows, int64_t n, rad_weight a, rad
|
||||
{ x }, { y });
|
||||
}
|
||||
|
||||
/* An encoder's causal conv over the pass's clips: their bounds in samples, an input row holding
|
||||
* `rate` of them (cu_div), `stride` input rows an output row. */
|
||||
static rad_op econv_op(RadBuilder* b, int64_t rows, int64_t cin, int64_t cout, int64_t k,
|
||||
int64_t dil, int64_t stride, int64_t rate, rad_weight w, rad_weight bias,
|
||||
rad_buf x, rad_buf res, rad_buf y) {
|
||||
const RadParam ps[] = { RAD_RANGE("M", 1, rows), RAD_INT("cin", cin), RAD_INT("cout", cout),
|
||||
RAD_INT("k", k), RAD_INT("dil", dil), RAD_STR("act", "none"),
|
||||
RAD_STR("dtype", "bf16"), RAD_INT("causal", 1),
|
||||
RAD_INT("cu_div", rate), RAD_INT("stride", stride) };
|
||||
const int np = stride > 1 ? 10 : 9;
|
||||
const rad_weight ws[] = { w, bias };
|
||||
return res ? rw(b, rad_decl_op(b, "conv1d", ps, np, ws, 2), { x, res }, { y })
|
||||
: rw(b, rad_decl_op(b, "conv1d", ps, np, ws, 2), { x }, { y });
|
||||
}
|
||||
|
||||
/* An encoder conv, weight-normalised as vconv's. */
|
||||
static int econv(RadBuilder* b, Names& nm, VConv& v, const char* dn, const char* src, int64_t cin,
|
||||
int64_t cout, int64_t k, int64_t dil, int64_t stride, int64_t rate, int64_t rows,
|
||||
rad_buf x, rad_buf res, rad_buf y) {
|
||||
const char* wn = nm.f("%s.weight", dn);
|
||||
RAD_ARCH_TRY(map_wnorm(b, wn, nm.ckpt("%s.parametrizations.weight.original0", src),
|
||||
nm.ckpt("%s.parametrizations.weight.original1", src)));
|
||||
v.w = decl_w(b, wn, RAD_BF16, { cout, cin, k }, RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE,
|
||||
grp_layer(kGrpCodec));
|
||||
v.b = wcopy(b, nm.f("%s.bias", dn), nm.ckpt("%s.bias", src), RAD_BF16, { cout }, kGrpCodec);
|
||||
if (!v.w || !v.b) return RAD_E_INVAL;
|
||||
v.op = econv_op(b, rows, cin, cout, k, dil, stride, rate, v.w, v.b, x, res, y);
|
||||
return v.op ? RAD_OK : RAD_E_INVAL;
|
||||
}
|
||||
|
||||
/* A codec transformer of `n` layers (modded_dac.py's WindowLimitedTransformer): plain-gain RMS
|
||||
* norms at eps 1e-5, fused q|k|v with adjacent-pair rotary over all 64 dims at the rows'
|
||||
* positions, causal attention over a window of `win` rows within each sequence, layer scales on
|
||||
* both residual branches, over `rows` rows of B.x in place. Layer l's weights are `dn`.l.* from
|
||||
* `src`.layers.l. The sequences are the buffer `cu`, or with none the pass's clips in samples,
|
||||
* `cdiv` of them a row; the positions the buffer `pos`, or with none the pass's own. */
|
||||
static int declare_ct(RadBuilder* b, Names& nm, std::vector<CodecLayer>& ls, const char* dn,
|
||||
const char* src, int n, int64_t rows, int64_t win, int64_t cdiv,
|
||||
const CtSet& B, rad_buf cu, rad_buf pos) {
|
||||
const int64_t qd = kCtHeads * kCtHeadDim;
|
||||
ls.assign((size_t)n, CodecLayer{});
|
||||
for (int l = 0; l < n; ++l) {
|
||||
CodecLayer& L = ls[(size_t)l];
|
||||
auto sr = [&](const char* x) { return nm.ckpt("%s.layers.%d.%s", src, l, x); };
|
||||
auto d = [&](const char* x) { return nm.f("%s.%d.%s", dn, l, x); };
|
||||
L.w_n1 = wcopy(b, d("n1.weight"), sr("attention_norm.weight"), RAD_F32, { kLat },
|
||||
kGrpCodec);
|
||||
L.w_n2 = wcopy(b, d("n2.weight"), sr("ffn_norm.weight"), RAD_F32, { kLat }, kGrpCodec);
|
||||
L.w_ls1 = wcopy(b, d("ls1.weight"), sr("attention_layer_scale.gamma"), RAD_F32,
|
||||
{ kLat }, kGrpCodec);
|
||||
L.w_ls2 = wcopy(b, d("ls2.weight"), sr("ffn_layer_scale.gamma"), RAD_F32, { kLat },
|
||||
kGrpCodec);
|
||||
L.w_qkv = wcopy(b, d("qkv.weight"), sr("attention.wqkv.weight"), RAD_BF16,
|
||||
{ 3 * qd, kLat }, kGrpCodec);
|
||||
L.w_o = wcopy(b, d("o.weight"), sr("attention.wo.weight"), RAD_BF16, { kLat, qd },
|
||||
kGrpCodec);
|
||||
if (map_concat(b, d("gate_up.weight"),
|
||||
{ sr("feed_forward.w1.weight"), sr("feed_forward.w3.weight") }) < 0)
|
||||
return RAD_E_INVAL;
|
||||
L.w_gu = decl_w(b, d("gate_up.weight"), RAD_BF16, { 2 * kCtFf, kLat },
|
||||
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(kGrpCodec));
|
||||
L.w_down = wcopy(b, d("down.weight"), sr("feed_forward.w2.weight"), RAD_BF16,
|
||||
{ kLat, kCtFf }, kGrpCodec);
|
||||
/* Plain-gain RMS norms at eps 1e-5 (the codec's own RMSNorm, not the backbone's). */
|
||||
L.op_n1 = norm_op(b, rows, kLat, kCtEps, 0.0, L.w_n1, { B.x }, B.h);
|
||||
L.op_qkv = gemm_op(b, rows, 3 * qd, kLat, L.w_qkv, B.h, B.qkv);
|
||||
/* Adjacent pairs over all 64 dims, positions the rows' own: a piece's frames as a whole
|
||||
* decode restarting at 0 would see them, a clip's rows in the pass (attention reads only
|
||||
* their differences). */
|
||||
L.op_rope = rw(b, RAD_OP(b, "rope",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, rows),
|
||||
RAD_INT("head_dim", kCtHeadDim),
|
||||
RAD_INT("n_head", kCtHeads),
|
||||
RAD_INT("n_head_kv", kCtHeads),
|
||||
RAD_F64("theta", kCtTheta), RAD_F64("scale", 1.0),
|
||||
RAD_STR("mode", "gptj")),
|
||||
RAD_NOWEIGHTS),
|
||||
{ B.qkv }, { B.qkv });
|
||||
const RadParam ap[] = { RAD_RANGE("M", 1, rows), RAD_INT("head_dim", kCtHeadDim),
|
||||
RAD_INT("gqa", 1), RAD_INT("causal", 1), RAD_STR("dtype", "bf16"),
|
||||
RAD_F64("scale", 1.0 / std::sqrt((double)kCtHeadDim)),
|
||||
RAD_INT("max_seqlen", rows), RAD_INT("window", win),
|
||||
RAD_INT("cu_div", cdiv) };
|
||||
L.op_attn = rw(b, rad_decl_op(b, "attn_dense", ap, cdiv > 1 ? 9 : 8, nullptr, 0),
|
||||
{ B.qkv }, { B.attn });
|
||||
L.op_o = gemm_op(b, rows, kLat, qd, L.w_o, B.attn, B.o);
|
||||
L.op_ls1 = mulw_op(b, rows, kLat, L.w_ls1, B.o, B.o);
|
||||
L.op_add1 = add_op(b, rows, kLat, B.x, B.o, B.x);
|
||||
L.op_n2 = norm_op(b, rows, kLat, kCtEps, 0.0, L.w_n2, { B.x }, B.h);
|
||||
L.op_gu = gemm_op(b, rows, 2 * kCtFf, kLat, L.w_gu, B.h, B.gu);
|
||||
L.op_act = rows_op(b, "silu_mul", rows, kCtFf, { B.gu }, { B.ff });
|
||||
L.op_down = gemm_op(b, rows, kLat, kCtFf, L.w_down, B.ff, B.o);
|
||||
L.op_ls2 = mulw_op(b, rows, kLat, L.w_ls2, B.o, B.o);
|
||||
L.op_add2 = add_op(b, rows, kLat, B.x, B.o, B.x);
|
||||
if (!L.w_n1 || !L.w_n2 || !L.w_ls1 || !L.w_ls2 || !L.w_qkv || !L.w_o || !L.w_gu ||
|
||||
!L.w_down || !L.op_n1 || !L.op_qkv || !L.op_rope || !L.op_attn || !L.op_o ||
|
||||
!L.op_ls1 || !L.op_add1 || !L.op_n2 || !L.op_gu || !L.op_act || !L.op_down ||
|
||||
!L.op_ls2 || !L.op_add2)
|
||||
return RAD_E_INVAL;
|
||||
if (cu) RAD_ARCH_TRY(rad_op_reads(b, L.op_attn, &cu, 1));
|
||||
if (pos) RAD_ARCH_TRY(rad_op_reads(b, L.op_rope, &pos, 1));
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
static int declare_codec(RadBuilder* b, Model& m) {
|
||||
Names& nm = m.nm;
|
||||
const int64_t NL = kVocContext + kVocChunk; /* frames through the transformer */
|
||||
@@ -1113,63 +1353,12 @@ static int declare_codec(RadBuilder* b, Model& m) {
|
||||
m.b_co = decl_b(b, nm.f("codec.o"), RAD_BF16, { NL, kLat });
|
||||
m.b_cgu = decl_b(b, nm.f("codec.gu"), RAD_BF16, { NL, 2 * kCtFf });
|
||||
m.b_cff = decl_b(b, nm.f("codec.ff"), RAD_BF16, { NL, kCtFf });
|
||||
m.ct.resize((size_t)kCtLayers);
|
||||
for (int l = 0; l < kCtLayers; ++l) {
|
||||
CodecLayer& L = m.ct[(size_t)l];
|
||||
auto src = [&](const char* s) { return nm.ckpt("%s.layers.%d.%s", pt, l, s); };
|
||||
auto dn = [&](const char* s) { return nm.f("codec.%d.%s", l, s); };
|
||||
L.w_n1 = wcopy(b, dn("n1.weight"), src("attention_norm.weight"), RAD_F32, { kLat },
|
||||
kGrpCodec);
|
||||
L.w_n2 = wcopy(b, dn("n2.weight"), src("ffn_norm.weight"), RAD_F32, { kLat }, kGrpCodec);
|
||||
L.w_ls1 = wcopy(b, dn("ls1.weight"), src("attention_layer_scale.gamma"), RAD_F32,
|
||||
{ kLat }, kGrpCodec);
|
||||
L.w_ls2 = wcopy(b, dn("ls2.weight"), src("ffn_layer_scale.gamma"), RAD_F32, { kLat },
|
||||
kGrpCodec);
|
||||
L.w_qkv = wcopy(b, dn("qkv.weight"), src("attention.wqkv.weight"), RAD_BF16,
|
||||
{ 3 * qd, kLat }, kGrpCodec);
|
||||
L.w_o = wcopy(b, dn("o.weight"), src("attention.wo.weight"), RAD_BF16, { kLat, qd },
|
||||
kGrpCodec);
|
||||
if (map_concat(b, dn("gate_up.weight"),
|
||||
{ src("feed_forward.w1.weight"), src("feed_forward.w3.weight") }) < 0)
|
||||
return RAD_E_INVAL;
|
||||
L.w_gu = decl_w(b, dn("gate_up.weight"), RAD_BF16, { 2 * kCtFf, kLat },
|
||||
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(kGrpCodec));
|
||||
L.w_down = wcopy(b, dn("down.weight"), src("feed_forward.w2.weight"), RAD_BF16,
|
||||
{ kLat, kCtFf }, kGrpCodec);
|
||||
/* Plain-gain RMS norms at eps 1e-5 (the codec's own RMSNorm, not the backbone's). */
|
||||
L.op_n1 = norm_op(b, NL, kLat, kCtEps, 0.0, L.w_n1, { m.b_cx }, m.b_ch);
|
||||
L.op_qkv = gemm_op(b, NL, 3 * qd, kLat, L.w_qkv, m.b_ch, m.b_cqkv);
|
||||
/* Adjacent pairs over all 64 dims, positions the frames' own in the piece, as a whole
|
||||
* decode restarting at 0 would see them. */
|
||||
L.op_rope = rw(b, RAD_OP(b, "rope",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, NL), RAD_INT("head_dim", kCtHeadDim),
|
||||
RAD_INT("n_head", kCtHeads),
|
||||
RAD_INT("n_head_kv", kCtHeads),
|
||||
RAD_F64("theta", kCtTheta), RAD_F64("scale", 1.0),
|
||||
RAD_STR("mode", "gptj")),
|
||||
RAD_NOWEIGHTS),
|
||||
{ m.b_cqkv }, { m.b_cqkv });
|
||||
L.op_attn = rw(b, RAD_OP(b, "attn_dense",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, NL), RAD_INT("head_dim", kCtHeadDim),
|
||||
RAD_INT("gqa", 1), RAD_INT("causal", 1),
|
||||
RAD_STR("dtype", "bf16"),
|
||||
RAD_F64("scale", 1.0 / std::sqrt((double)kCtHeadDim)),
|
||||
RAD_INT("max_seqlen", NL), RAD_INT("window", kAttnWin)),
|
||||
RAD_NOWEIGHTS),
|
||||
{ m.b_cqkv }, { m.b_cattn });
|
||||
L.op_o = gemm_op(b, NL, kLat, qd, L.w_o, m.b_cattn, m.b_co);
|
||||
L.op_ls1 = mulw_op(b, NL, kLat, L.w_ls1, m.b_co, m.b_co);
|
||||
L.op_add1 = add_op(b, NL, kLat, m.b_cx, m.b_co, m.b_cx);
|
||||
L.op_n2 = norm_op(b, NL, kLat, kCtEps, 0.0, L.w_n2, { m.b_cx }, m.b_ch);
|
||||
L.op_gu = gemm_op(b, NL, 2 * kCtFf, kLat, L.w_gu, m.b_ch, m.b_cgu);
|
||||
L.op_act = rows_op(b, "silu_mul", NL, kCtFf, { m.b_cgu }, { m.b_cff });
|
||||
L.op_down = gemm_op(b, NL, kLat, kCtFf, L.w_down, m.b_cff, m.b_co);
|
||||
L.op_ls2 = mulw_op(b, NL, kLat, L.w_ls2, m.b_co, m.b_co);
|
||||
L.op_add2 = add_op(b, NL, kLat, m.b_cx, m.b_co, m.b_cx);
|
||||
if (!L.w_n1 || !L.w_n2 || !L.w_ls1 || !L.w_ls2 || !L.w_qkv || !L.w_o || !L.w_gu ||
|
||||
!L.w_down || !L.op_rope || !L.op_attn || !L.op_ls1 || !L.op_ls2 || !L.op_act)
|
||||
return RAD_E_INVAL;
|
||||
RAD_ARCH_TRY(rad_op_reads(b, L.op_attn, &m.b_ccu[0], 1));
|
||||
{
|
||||
CtSet B;
|
||||
B.x = m.b_cx; B.h = m.b_ch; B.qkv = m.b_cqkv; B.attn = m.b_cattn; B.o = m.b_co;
|
||||
B.gu = m.b_cgu; B.ff = m.b_cff;
|
||||
RAD_ARCH_TRY(declare_ct(b, nm, m.ct, "codec", pt, (int)kCtLayers, NL, kAttnWin, 1, B,
|
||||
m.b_ccu[0], 0));
|
||||
}
|
||||
/* The final norm over only the frames the convolutions read: the last nc. */
|
||||
m.w_cn = wcopy(b, nm.f("codec.norm.weight"), nm.ckpt("%s.norm.weight", pt), RAD_F32, { kLat },
|
||||
@@ -1311,6 +1500,204 @@ static int declare_codec(RadBuilder* b, Model& m) {
|
||||
: RAD_E_INVAL;
|
||||
}
|
||||
|
||||
/* ================================================================== the codec's encoder */
|
||||
/* Voice cloning: the PROMPT pass's reference clips to the codec's ten codes a frame
|
||||
* (modded_dac.py's Encoder, then rvq.py's DownsampleResidualVectorQuantize), every clip at once,
|
||||
* each a sequence of the pass's bounds, which are in samples and read at each rate through cu_div.
|
||||
* In bf16, as fish-speech's server encodes a reference, every quantiser step in f32 (vq_encode)
|
||||
* into the prompt rows' code plane at the clip's rows.
|
||||
*
|
||||
* Declared first: the clips are encoded before anything else of the pass runs, so the encoder's
|
||||
* buffers are dead by the backbone's, and only the code plane, which the tower reads, lives on. */
|
||||
static int declare_encoder(RadBuilder* b, Model& m) {
|
||||
Names& nm = m.nm;
|
||||
const int64_t L = kMaxRefSamples, D = kLat, qd = kCtHeads * kCtHeadDim;
|
||||
const char* es = "codec.encoder.block";
|
||||
static const int64_t kDil[3] = { 1, 3, 9 };
|
||||
|
||||
m.b_ein = decl_b(b, nm.f("enc.in"), RAD_BF16, { L, 1 });
|
||||
m.op_ecast = rw(b, RAD_OP(b, "cast",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, L), RAD_INT("n", 1),
|
||||
RAD_STR("from", "f32"), RAD_STR("to", "bf16")),
|
||||
RAD_NOWEIGHTS),
|
||||
{}, { m.b_ein });
|
||||
int64_t rows = L, ch = 64, rate = 1;
|
||||
for (int s = 0; s < 4; ++s) {
|
||||
m.b_ex[s] = decl_b(b, nm.f("enc.x%d", s), RAD_BF16, { rows, ch });
|
||||
m.b_et[s] = decl_b(b, nm.f("enc.t%d", s), RAD_BF16, { rows, ch });
|
||||
m.b_ea[s] = decl_b(b, nm.f("enc.a%d", s), RAD_BF16, { rows, ch });
|
||||
rows /= kEncStride[s];
|
||||
ch *= 2;
|
||||
}
|
||||
const int64_t R4 = rows, R5 = R4 / 2, F = R5 / 2; /* 512, 1024 and 2048 samples a row */
|
||||
m.b_ex4 = decl_b(b, nm.f("enc.x4"), RAD_BF16, { R4, D });
|
||||
m.b_et4 = decl_b(b, nm.f("enc.t4"), RAD_BF16, { R4, D });
|
||||
m.b_ea4 = decl_b(b, nm.f("enc.a4"), RAD_BF16, { R4, D });
|
||||
RAD_ARCH_TRY(econv(b, nm, m.e0, "codec.enc.0", nm.ckpt("%s.0.conv", es), 1, 64, 7, 1, 1, 1, L,
|
||||
m.b_ein, 0, m.b_ex[0]));
|
||||
if (!m.op_ecast) return RAD_E_INVAL;
|
||||
|
||||
/* ---- four blocks: three residual units (dilations 1, 3, 9: x + conv2(snake2(conv1(snake1
|
||||
* (x))))), a snake and the strided conv into the next block's rows */
|
||||
rows = L;
|
||||
ch = 64;
|
||||
for (int s = 0; s < 4; ++s) {
|
||||
EBlock& B = m.eb[s];
|
||||
const char* bn = nm.f("codec.enc.b%d", s);
|
||||
const char* bs = nm.ckpt("%s.%d.block", es, s + 1);
|
||||
const rad_buf X = m.b_ex[s], T = m.b_et[s], A = m.b_ea[s];
|
||||
for (int r = 0; r < 3; ++r) {
|
||||
VRes& U = B.res[r];
|
||||
const char* un = nm.f("%s.res%d", bn, r);
|
||||
const char* ub = nm.ckpt("%s.%d.block", bs, r);
|
||||
U.a1 = valpha(b, nm, nm.f("%s.act1", un), nm.ckpt("%s.0", ub), ch);
|
||||
U.a2 = valpha(b, nm, nm.f("%s.act2", un), nm.ckpt("%s.2", ub), ch);
|
||||
U.op_s1 = snake_op(b, rows, ch, U.a1, X, T);
|
||||
RAD_ARCH_TRY(econv(b, nm, U.c1, nm.f("%s.conv1", un), nm.ckpt("%s.1.conv", ub), ch, ch,
|
||||
7, kDil[r], 1, rate, rows, T, 0, A));
|
||||
U.op_s2 = snake_op(b, rows, ch, U.a2, A, T);
|
||||
RAD_ARCH_TRY(econv(b, nm, U.c2, nm.f("%s.conv2", un), nm.ckpt("%s.3.conv", ub), ch, ch,
|
||||
1, 1, 1, rate, rows, T, X, X));
|
||||
if (!U.a1 || !U.a2 || !U.op_s1 || !U.op_s2) return RAD_E_INVAL;
|
||||
}
|
||||
B.a = valpha(b, nm, nm.f("%s.snake", bn), nm.ckpt("%s.3", bs), ch);
|
||||
B.op_s = snake_op(b, rows, ch, B.a, X, T);
|
||||
if (!B.a || !B.op_s) return RAD_E_INVAL;
|
||||
const int64_t st = kEncStride[s];
|
||||
RAD_ARCH_TRY(econv(b, nm, B.down, nm.f("%s.down", bn), nm.ckpt("%s.4.conv", bs), ch,
|
||||
2 * ch, 2 * st, 1, st, rate, rows, T, 0,
|
||||
s < 3 ? m.b_ex[s + 1] : m.b_ex4));
|
||||
rows /= st;
|
||||
ch *= 2;
|
||||
rate *= st;
|
||||
}
|
||||
|
||||
/* ---- the last block's transformer (window 512 rows) and its norm, a snake, a conv */
|
||||
m.ecs.x = m.b_ex4;
|
||||
m.ecs.h = decl_b(b, nm.f("enc.h"), RAD_BF16, { R4, D });
|
||||
m.ecs.qkv = decl_b(b, nm.f("enc.qkv"), RAD_BF16, { R4, 3 * qd });
|
||||
m.ecs.attn = decl_b(b, nm.f("enc.attn"), RAD_BF16, { R4, qd });
|
||||
m.ecs.o = decl_b(b, nm.f("enc.o"), RAD_BF16, { R4, D });
|
||||
m.ecs.gu = decl_b(b, nm.f("enc.gu"), RAD_BF16, { R4, 2 * kCtFf });
|
||||
m.ecs.ff = decl_b(b, nm.f("enc.ff"), RAD_BF16, { R4, kCtFf });
|
||||
m.b_epos = decl_b(b, nm.f("enc.pos"), RAD_I32, { R4 });
|
||||
m.op_epos = iota_op(b, R4, 0, 1, m.b_epos);
|
||||
const char* tsrc = nm.ckpt("%s.4.block.5", es);
|
||||
RAD_ARCH_TRY(declare_ct(b, nm, m.et, "codec.enc.t", tsrc, (int)kEncLayers, R4, kEncWin, 512,
|
||||
m.ecs, 0, m.b_epos));
|
||||
m.w_etn = wcopy(b, nm.f("codec.enc.t.norm.weight"), nm.ckpt("%s.norm.weight", tsrc), RAD_F32,
|
||||
{ D }, kGrpCodec);
|
||||
m.op_etn = norm_op(b, R4, D, kCtEps, 0.0, m.w_etn, { m.b_ex4 }, m.b_et4);
|
||||
m.w_e5 = valpha(b, nm, "codec.enc.snake_out", nm.ckpt("%s.5", es), D);
|
||||
m.op_e5 = snake_op(b, R4, D, m.w_e5, m.b_et4, m.b_ea4);
|
||||
RAD_ARCH_TRY(econv(b, nm, m.e6, "codec.enc.conv_out", nm.ckpt("%s.6.conv", es), D, D, 3, 1, 1,
|
||||
512, R4, m.b_ea4, 0, m.b_ex4));
|
||||
if (!m.op_epos || !m.w_etn || !m.op_etn || !m.w_e5 || !m.op_e5) return RAD_E_INVAL;
|
||||
|
||||
/* ---- the quantiser's two 2x downsamplings: a strided conv (plain weights), a ConvNeXt
|
||||
* block (causal depthwise conv, layer norm, GELU feed-forward, layer scale, residual) */
|
||||
m.b_ed1 = decl_b(b, nm.f("enc.d1"), RAD_BF16, { R5, D });
|
||||
m.b_ez = decl_b(b, nm.f("enc.z"), RAD_BF16, { F, D });
|
||||
m.b_dua = decl_b(b, nm.f("enc.dua"), RAD_BF16, { R5, D });
|
||||
m.b_dub = decl_b(b, nm.f("enc.dub"), RAD_BF16, { R5, D });
|
||||
m.b_duh = decl_b(b, nm.f("enc.duh"), RAD_BF16, { R5, 4 * D });
|
||||
m.b_dug = decl_b(b, nm.f("enc.dug"), RAD_BF16, { R5, 4 * D });
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
EDown& E = m.ed[i];
|
||||
const int64_t rin = i ? R5 : R4, rout = rin / 2, rr = i ? 1024 : 512;
|
||||
const rad_buf xin = i ? m.b_ed1 : m.b_ex4, y = i ? m.b_ez : m.b_ed1;
|
||||
const char* ds = nm.ckpt("codec.quantizer.downsample.%d", i);
|
||||
auto w = [&](const char* dn, const char* sr, uint32_t dt,
|
||||
std::initializer_list<int64_t> sh) {
|
||||
return wcopy(b, nm.f("codec.enc.down%d.%s", i, dn), nm.ckpt("%s.%s", ds, sr), dt, sh,
|
||||
kGrpCodec);
|
||||
};
|
||||
E.c.w = w("weight", "0.conv.weight", RAD_BF16, { D, D, 2 });
|
||||
E.c.b = w("bias", "0.conv.bias", RAD_BF16, { D });
|
||||
E.w_dw = w("dw.weight", "1.dwconv.conv.weight", RAD_BF16, { D, 1, 7 });
|
||||
E.b_dw = w("dw.bias", "1.dwconv.conv.bias", RAD_BF16, { D });
|
||||
E.w_ln = w("ln.weight", "1.norm.weight", RAD_F32, { D });
|
||||
E.b_ln = w("ln.bias", "1.norm.bias", RAD_F32, { D });
|
||||
E.w_p1 = w("p1.weight", "1.pwconv1.weight", RAD_BF16, { 4 * D, D });
|
||||
E.b_p1 = w("p1.bias", "1.pwconv1.bias", RAD_BF16, { 4 * D });
|
||||
E.w_p2 = w("p2.weight", "1.pwconv2.weight", RAD_BF16, { D, 4 * D });
|
||||
E.b_p2 = w("p2.bias", "1.pwconv2.bias", RAD_BF16, { D });
|
||||
E.w_g = w("gamma", "1.gamma", RAD_F32, { D });
|
||||
if (!E.c.w || !E.c.b || !E.w_dw || !E.b_dw || !E.w_ln || !E.b_ln || !E.w_p1 || !E.b_p1 ||
|
||||
!E.w_p2 || !E.b_p2 || !E.w_g)
|
||||
return RAD_E_INVAL;
|
||||
E.c.op = econv_op(b, rin, D, D, 2, 1, 2, rr, E.c.w, E.c.b, xin, 0, y);
|
||||
E.op_dw = rw(b, RAD_OP(b, "conv1d_dw",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, rout), RAD_INT("n", D), RAD_INT("k", 7),
|
||||
RAD_INT("cu_div", 2 * rr), RAD_STR("dtype", "bf16")),
|
||||
RAD_WEIGHTS(E.w_dw, E.b_dw)),
|
||||
{ y }, { m.b_dua });
|
||||
E.op_ln = rw(b, RAD_OP(b, "layernorm",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, rout), RAD_INT("n", D),
|
||||
RAD_F64("eps", 1e-6), RAD_STR("dtype", "bf16")),
|
||||
RAD_WEIGHTS(E.w_ln, E.b_ln)),
|
||||
{ m.b_dua }, { m.b_dub });
|
||||
E.op_p1 = gemm_bias_op(b, rout, 4 * D, D, E.w_p1, E.b_p1, m.b_dub, m.b_duh);
|
||||
E.op_gelu = rows_op(b, "gelu", rout, 4 * D, { m.b_duh }, { m.b_dug });
|
||||
E.op_p2 = gemm_bias_op(b, rout, D, 4 * D, E.w_p2, E.b_p2, m.b_dug, m.b_dua);
|
||||
E.op_g = mulw_op(b, rout, D, E.w_g, m.b_dua, m.b_dua);
|
||||
E.op_add = add_op(b, rout, D, y, m.b_dua, y);
|
||||
if (!E.c.op || !E.op_dw || !E.op_ln || !E.op_p1 || !E.op_gelu || !E.op_p2 || !E.op_g ||
|
||||
!E.op_add)
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
|
||||
/* ---- the pre-quantiser transformer (8 layers, window 128 frames) and its norm, over the
|
||||
* encoder's working set */
|
||||
const char* psrc = "codec.quantizer.pre_module";
|
||||
CtSet P = m.ecs;
|
||||
P.x = m.b_ez;
|
||||
RAD_ARCH_TRY(declare_ct(b, nm, m.pq, "codec.enc.pre", nm.ckpt("%s", psrc), (int)kCtLayers, F,
|
||||
kAttnWin, kSamplesPerFrame, P, 0, m.b_epos));
|
||||
m.w_pqn = wcopy(b, nm.f("codec.enc.pre.norm.weight"), nm.ckpt("%s.norm.weight", psrc),
|
||||
RAD_F32, { D }, kGrpCodec);
|
||||
m.op_pqn = norm_op(b, F, D, kCtEps, 0.0, m.w_pqn, { m.b_ez }, m.b_ez);
|
||||
if (!m.w_pqn || !m.op_pqn) return RAD_E_INVAL;
|
||||
|
||||
/* ---- the prompt rows' codes: -2 for code 0 and -1 for the others at every row, then each
|
||||
* clip's frames at their rows, the semantic step first and the nine residual ones after it,
|
||||
* every step quantising what the last left of z */
|
||||
m.b_pc = decl_b(b, nm.f("enc.codes"), RAD_I32, { kCodes, kPcW });
|
||||
m.op_pc0 = iota_op(b, kPcW, -2, 0, m.b_pc);
|
||||
m.op_pc1 = iota_op(b, (kCodes - 1) * kPcW, -1, 0, m.b_pc);
|
||||
for (int k = 0; k < kCodes; ++k) {
|
||||
EQuant& Q = m.eq[k];
|
||||
const char* qs = k == 0 ? nm.ckpt("codec.quantizer.semantic_quantizer.quantizers.0")
|
||||
: nm.ckpt("codec.quantizer.quantizer.quantizers.%d", k - 1);
|
||||
const int64_t bins = k == 0 ? kVocab : kResVocab;
|
||||
const char* iw = nm.f("codec.enc.vq%d.in.weight", k);
|
||||
const char* ow = nm.f("codec.enc.vq%d.out.weight", k);
|
||||
RAD_ARCH_TRY(map_wnorm(b, iw, nm.ckpt("%s.in_proj.weight_g", qs),
|
||||
nm.ckpt("%s.in_proj.weight_v", qs)));
|
||||
RAD_ARCH_TRY(map_wnorm(b, ow, nm.ckpt("%s.out_proj.weight_g", qs),
|
||||
nm.ckpt("%s.out_proj.weight_v", qs)));
|
||||
Q.in_w = decl_w(b, iw, RAD_F32, { kCbDim, D, 1 }, RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE,
|
||||
grp_layer(kGrpCodec));
|
||||
Q.in_b = wcopy(b, nm.f("codec.enc.vq%d.in.bias", k), nm.ckpt("%s.in_proj.bias", qs),
|
||||
RAD_F32, { kCbDim }, kGrpCodec);
|
||||
Q.cb = wcopy(b, nm.f("codec.enc.vq%d.codebook", k), nm.ckpt("%s.codebook.weight", qs),
|
||||
RAD_F32, { bins, kCbDim }, kGrpCodec);
|
||||
Q.out_w = decl_w(b, ow, RAD_F32, { D, kCbDim, 1 }, RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE,
|
||||
grp_layer(kGrpCodec));
|
||||
Q.out_b = wcopy(b, nm.f("codec.enc.vq%d.out.bias", k), nm.ckpt("%s.out_proj.bias", qs),
|
||||
RAD_F32, { D }, kGrpCodec);
|
||||
if (!Q.in_w || !Q.in_b || !Q.cb || !Q.out_w || !Q.out_b) return RAD_E_INVAL;
|
||||
Q.op = rw(b, RAD_OP(b, "vq_encode",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, F), RAD_INT("n", D),
|
||||
RAD_INT("dim", kCbDim), RAD_INT("bins", bins),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
RAD_WEIGHTS(Q.in_w, Q.in_b, Q.cb, Q.out_w, Q.out_b)),
|
||||
{ m.b_ez }, { m.b_ez, m.b_pc });
|
||||
if (!Q.op) return RAD_E_INVAL;
|
||||
}
|
||||
return m.op_pc0 && m.op_pc1 ? RAD_OK : RAD_E_INVAL;
|
||||
}
|
||||
|
||||
/* ================================================================== declare */
|
||||
static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* ctx) {
|
||||
if (!b || !meta || !ctx) return RAD_E_INVAL;
|
||||
@@ -1332,11 +1719,14 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
|
||||
/* Declared in the order the passes issue them where it matters for liveness: the backbone
|
||||
* first (PROMPT runs it before everything else), then the frame head, the tower, the frame's
|
||||
* tail, and the codec. FRAME runs the head and the tower before the backbone; the buffers
|
||||
* that cross that seam (`x`, `h`, the codes, the plane) are named by the ops on both sides,
|
||||
* so their live ranges span it. */
|
||||
/* Declared in the order the passes issue them where it matters for liveness: the codec's
|
||||
* encoder first (PROMPT runs it before everything else when the request has references), the
|
||||
* backbone, then the frame head, the tower, the frame's tail, and the codec. The rows' code
|
||||
* plane the encoder leaves is named by the tower's ops too, so it lives across the backbone.
|
||||
* FRAME runs the head and the tower before the backbone; the buffers that cross that seam
|
||||
* (`x`, `h`, the codes, the plane) are named by the ops on both sides, so their live ranges
|
||||
* span it. */
|
||||
RAD_ARCH_TRY(declare_encoder(b, m));
|
||||
RAD_ARCH_TRY(declare_trunk(b, meta, ctx, m));
|
||||
RAD_ARCH_TRY(declare_frame_head(b, m));
|
||||
RAD_ARCH_TRY(declare_tower(b, meta, m));
|
||||
@@ -1367,6 +1757,12 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
d.voc_context = kVocContext;
|
||||
d.branches = 1;
|
||||
d.input_special = 1;
|
||||
d.tmpl_ref = kRefPlain;
|
||||
d.ref_item = kRefItem;
|
||||
d.ref_rate = kSampleRate;
|
||||
d.ref_hop = kSamplesPerFrame;
|
||||
d.ref_max_samples = kMaxRefSamples;
|
||||
d.ref_max = kMaxRefs;
|
||||
RAD_ARCH_TRY(rad_declare_audio_generator(b, &d));
|
||||
}
|
||||
rad_note(b, "rank %d/%d: drama-3 -- backbone %lld layers (%lld heads, %lld KV a rank; %lld "
|
||||
@@ -1386,6 +1782,107 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
}
|
||||
|
||||
/* ================================================================== step */
|
||||
/* A codec transformer (declare_ct) over R rows of B.x. */
|
||||
static void step_ct(RadCtx* c, const std::vector<CodecLayer>& ls, int64_t R, const CtSet& B,
|
||||
RadOperand pos, RadOperand cu) {
|
||||
const int64_t qd = kCtHeads * kCtHeadDim;
|
||||
for (const CodecLayer& Ly : ls) {
|
||||
RAD_ISSUE_N(c, Ly.op_n1, R, brows(B.x, R), RAD_W(Ly.w_n1), brows(B.h, R));
|
||||
RAD_ISSUE_N(c, Ly.op_qkv, R, brows(B.h, R), RAD_W(Ly.w_qkv), brows(B.qkv, R));
|
||||
RAD_ISSUE_N(c, Ly.op_rope, R, brows(B.qkv, R), pos);
|
||||
RAD_ISSUE_N(c, Ly.op_attn, R, bcol(B.qkv, 0, qd, R), bcol(B.qkv, qd, qd, R),
|
||||
bcol(B.qkv, 2 * qd, qd, R), cu, brows(B.attn, R));
|
||||
RAD_ISSUE_N(c, Ly.op_o, R, brows(B.attn, R), RAD_W(Ly.w_o), brows(B.o, R));
|
||||
RAD_ISSUE_N(c, Ly.op_ls1, R, brows(B.o, R), RAD_W(Ly.w_ls1), brows(B.o, R));
|
||||
RAD_ISSUE_N(c, Ly.op_add1, R, brows(B.x, R), brows(B.o, R), brows(B.x, R));
|
||||
RAD_ISSUE_N(c, Ly.op_n2, R, brows(B.x, R), RAD_W(Ly.w_n2), brows(B.h, R));
|
||||
RAD_ISSUE_N(c, Ly.op_gu, R, brows(B.h, R), RAD_W(Ly.w_gu), brows(B.gu, R));
|
||||
RAD_ISSUE_N(c, Ly.op_act, R, brows(B.gu, R), brows(B.ff, R));
|
||||
RAD_ISSUE_N(c, Ly.op_down, R, brows(B.ff, R), RAD_W(Ly.w_down), brows(B.o, R));
|
||||
RAD_ISSUE_N(c, Ly.op_ls2, R, brows(B.o, R), RAD_W(Ly.w_ls2), brows(B.o, R));
|
||||
RAD_ISSUE_N(c, Ly.op_add2, R, brows(B.x, R), brows(B.o, R), brows(B.x, R));
|
||||
}
|
||||
}
|
||||
|
||||
/* A PROMPT pass's clips through the encoder (declare_encoder), their frames' codes into the
|
||||
* prompt rows' code plane at the rows that name them. */
|
||||
static void step_encode(RadCtx* c, const RadBatch* batch, const Model& m) {
|
||||
const RadAudioGenBatch* A = batch->agen_b;
|
||||
const int64_t L = A->ref_samples, D = kLat;
|
||||
const RadOperand cu = praw(A->ref_cu, RAD_I32, A->n_ref + 1);
|
||||
auto conv = [&](const VConv& v, rad_buf x, rad_buf res, rad_buf y, int64_t R, int64_t Ry) {
|
||||
RAD_ISSUE_N(c, v.op, R, brows(x, R), RAD_W(v.w), cu, res ? brows(res, R) : RAD_NONE,
|
||||
brows(y, Ry), RAD_W(v.b));
|
||||
};
|
||||
RAD_ISSUE_N(c, m.op_ecast, L, praw(A->ref_audio, RAD_F32, L), brows(m.b_ein, L));
|
||||
conv(m.e0, m.b_ein, 0, m.b_ex[0], L, L);
|
||||
int64_t R = L;
|
||||
for (int s = 0; s < 4; ++s) {
|
||||
const EBlock& B = m.eb[s];
|
||||
const rad_buf X = m.b_ex[s], T = m.b_et[s], Ab = m.b_ea[s];
|
||||
for (const VRes& U : B.res) {
|
||||
RAD_ISSUE_N(c, U.op_s1, R, brows(X, R), RAD_W(U.a1), brows(T, R), RAD_NONE);
|
||||
conv(U.c1, T, 0, Ab, R, R);
|
||||
RAD_ISSUE_N(c, U.op_s2, R, brows(Ab, R), RAD_W(U.a2), brows(T, R), RAD_NONE);
|
||||
conv(U.c2, T, X, X, R, R);
|
||||
}
|
||||
RAD_ISSUE_N(c, B.op_s, R, brows(X, R), RAD_W(B.a), brows(T, R), RAD_NONE);
|
||||
conv(B.down, T, 0, s < 3 ? m.b_ex[s + 1] : m.b_ex4, R, R / kEncStride[s]);
|
||||
R /= kEncStride[s];
|
||||
}
|
||||
/* The last block's transformer, at the clips' rows of 512 samples; the snake and the conv. */
|
||||
RAD_ISSUE_N(c, m.op_epos, R, brows(m.b_epos, R));
|
||||
step_ct(c, m.et, R, m.ecs, brows(m.b_epos, R), cu);
|
||||
RAD_ISSUE_N(c, m.op_etn, R, brows(m.b_ex4, R), RAD_W(m.w_etn), brows(m.b_et4, R));
|
||||
RAD_ISSUE_N(c, m.op_e5, R, brows(m.b_et4, R), RAD_W(m.w_e5), brows(m.b_ea4, R), RAD_NONE);
|
||||
conv(m.e6, m.b_ea4, 0, m.b_ex4, R, R);
|
||||
/* The quantiser's two downsamplings. */
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
const EDown& E = m.ed[i];
|
||||
const rad_buf xin = i ? m.b_ed1 : m.b_ex4, y = i ? m.b_ez : m.b_ed1;
|
||||
const int64_t ro = R / 2;
|
||||
conv(E.c, xin, 0, y, R, ro);
|
||||
RAD_ISSUE_N(c, E.op_dw, ro, brows(y, ro), RAD_W(E.w_dw), cu, brows(m.b_dua, ro),
|
||||
RAD_W(E.b_dw));
|
||||
RAD_ISSUE_N(c, E.op_ln, ro, brows(m.b_dua, ro), RAD_W(E.w_ln), RAD_W(E.b_ln),
|
||||
brows(m.b_dub, ro));
|
||||
RAD_ISSUE_N(c, E.op_p1, ro, brows(m.b_dub, ro), RAD_W(E.w_p1), RAD_W(E.b_p1), RAD_NONE,
|
||||
brows(m.b_duh, ro));
|
||||
RAD_ISSUE_N(c, E.op_gelu, ro, brows(m.b_duh, ro), brows(m.b_dug, ro));
|
||||
RAD_ISSUE_N(c, E.op_p2, ro, brows(m.b_dug, ro), RAD_W(E.w_p2), RAD_W(E.b_p2), RAD_NONE,
|
||||
brows(m.b_dua, ro));
|
||||
RAD_ISSUE_N(c, E.op_g, ro, brows(m.b_dua, ro), RAD_W(E.w_g), brows(m.b_dua, ro));
|
||||
RAD_ISSUE_N(c, E.op_add, ro, brows(y, ro), brows(m.b_dua, ro), brows(y, ro));
|
||||
R = ro;
|
||||
}
|
||||
/* The pre-quantiser transformer and its norm at the frames: z. */
|
||||
const int64_t F = R;
|
||||
CtSet P = m.ecs;
|
||||
P.x = m.b_ez;
|
||||
RAD_ISSUE_N(c, m.op_epos, F, brows(m.b_epos, F));
|
||||
step_ct(c, m.pq, F, P, brows(m.b_epos, F), cu);
|
||||
RAD_ISSUE_N(c, m.op_pqn, F, brows(m.b_ez, F), RAD_W(m.w_pqn), brows(m.b_ez, F));
|
||||
|
||||
/* The rows' codes: a token's everywhere, then each run of rows naming consecutive frames
|
||||
* quantised step by step into its columns. */
|
||||
RAD_ISSUE_N(c, m.op_pc0, kPcW, brow_slice(m.b_pc, 0, 1, kPcW));
|
||||
RAD_ISSUE_N(c, m.op_pc1, (kCodes - 1) * kPcW, brow_slice(m.b_pc, 1, kCodes - 1, kPcW));
|
||||
const int64_t T = batch->n_tok;
|
||||
for (int64_t t = 0; t < T;) {
|
||||
const int32_t f = A->ref_frame_h[t];
|
||||
if (f < 0 || f >= F) { ++t; continue; }
|
||||
int64_t n = 1;
|
||||
while (t + n < T && A->ref_frame_h[t + n] == f + n && f + n < F) ++n;
|
||||
for (int k = 0; k < kCodes; ++k) {
|
||||
const EQuant& Q = m.eq[k];
|
||||
RAD_ISSUE_N(c, Q.op, n, brow_slice(m.b_ez, f, n, D), RAD_W(Q.in_w), RAD_W(Q.in_b),
|
||||
RAD_W(Q.cb), RAD_W(Q.out_w), RAD_W(Q.out_b),
|
||||
bcol_at(m.b_pc, k, kPcW, t, n, 1));
|
||||
}
|
||||
t += n;
|
||||
}
|
||||
}
|
||||
|
||||
static void step_trunk(RadCtx* c, const RadBatch* batch, const Model& m) {
|
||||
const int64_t T = batch->n_tok;
|
||||
if (m.op_rope_cs && T <= qk_fuse_rows(m.g) && !rope_mixed(batch))
|
||||
@@ -1407,6 +1904,7 @@ struct TowerAt {
|
||||
rad_buf h0 = 0;
|
||||
bool frame = false;
|
||||
RadOperand seed{}, fidx{};
|
||||
bool labels = false; /* PROMPT with references: labels off the rows' code plane */
|
||||
};
|
||||
|
||||
static void step_tower(RadCtx* c, const Model& m, const TowerAt& at) {
|
||||
@@ -1429,6 +1927,17 @@ static void step_tower(RadCtx* c, const Model& m, const TowerAt& at) {
|
||||
RAD_ISSUE_N(c, L.op_eadd, R, brows(m.b_te, R), brows(m.b_ug, R), brows(m.b_te, R));
|
||||
RAD_ISSUE_N(c, L.op_en, R, brows(m.b_te, R), RAD_W(L.w_en), bcol(m.b_tcat, 0, D, R));
|
||||
}
|
||||
if (at.labels) {
|
||||
/* Row r's label: the code plane's column r + 1. */
|
||||
RAD_ISSUE_N(c, m.op_ptefill, R, brows(m.b_pte, R));
|
||||
RAD_ISSUE_N(c, L.op_pemb, R, bcol_at(m.b_pc, k < 0 ? 0 : k, kPcW, 1, R, 1),
|
||||
RAD_W(L.w_emb), RAD_W(L.w_emb), brows(m.b_pte, R));
|
||||
if (L.op_peadd)
|
||||
RAD_ISSUE_N(c, L.op_peadd, R, brows(m.b_pte, R), brows(m.b_pu, R),
|
||||
brows(m.b_pte, R));
|
||||
RAD_ISSUE_N(c, L.op_pen, R, brows(m.b_pte, R), RAD_W(L.w_en),
|
||||
bcol(m.b_tcat, 0, D, R));
|
||||
}
|
||||
RAD_ISSUE_N(c, L.op_hn, R, brows(i == 0 ? at.h0 : m.b_tho, R), RAD_W(L.w_hn),
|
||||
bcol(m.b_tcat, D, D, R));
|
||||
RAD_ISSUE_N(c, L.op_eh, R, brows(m.b_tcat, R), RAD_W(L.w_eh), brows(m.b_tm, R));
|
||||
@@ -1471,9 +1980,21 @@ static void step_tower(RadCtx* c, const Model& m, const TowerAt& at) {
|
||||
static void step_prompt(RadCtx* c, const RadBatch* batch, const Model& m) {
|
||||
const RadAudioGenBatch* A = batch->agen_b;
|
||||
const int64_t T = batch->n_tok, D = m.g.n_embd;
|
||||
/* References: their clips encoded into the rows' code plane first. */
|
||||
const bool refs = A->n_ref > 0;
|
||||
if (refs) step_encode(c, batch, m);
|
||||
RAD_ISSUE_N(c, m.op_embed, T, praw(batch->token_ids, RAD_I32, T), RAD_W(m.w_tok),
|
||||
brows(m.b_x, T));
|
||||
if (m.op_embed_ar) RAD_ISSUE_N(c, m.op_embed_ar, T * D, brows(m.b_x, T), RAD_NONE);
|
||||
RadOperand sec1 = RAD_W(m.w_cb), sec2 = RAD_W(m.w_cb), sec3 = RAD_W(m.w_cb);
|
||||
sec1.rows = kVocab;
|
||||
sec2.offset = kVocab * D;
|
||||
sec2.rows = kVocab;
|
||||
sec3.offset = 2 * kVocab * D;
|
||||
sec3.rows = (kCodes - 3) * kVocab;
|
||||
if (refs)
|
||||
RAD_ISSUE_N(c, m.op_pfb, T, bcol_at(m.b_pc, 0, kPcW, 0, T, 2), RAD_W(m.w_cemb), sec1,
|
||||
brows(m.b_x, T));
|
||||
step_trunk(c, batch, m);
|
||||
|
||||
/* The lane: the prompt's last hidden state, an empty window, no unseen codes. */
|
||||
@@ -1486,12 +2007,24 @@ static void step_prompt(RadCtx* c, const RadBatch* batch, const Model& m) {
|
||||
{
|
||||
RadOperand w = brow_slice(m.b_ras, lane, 1, kRasWin);
|
||||
RAD_ISSUE_N(c, m.op_rasinit, kRasWin, w);
|
||||
/* A reference's frames among the last rows are picks the window has seen. */
|
||||
if (refs && T >= kRasWin)
|
||||
RAD_ISSUE_N(c, m.op_rascopy, kRasWin,
|
||||
bcol_at(m.b_pc, 0, kPcW, T - kRasWin, kRasWin, 1), w);
|
||||
}
|
||||
|
||||
/* The tower over every prompt row: the label of each is text, so every code embedding is 0.
|
||||
* The last row's pass is FRAME 0's, which writes its cache rows again. */
|
||||
/* The tower over every prompt row. A text prompt's labels are all text, so every code
|
||||
* embedding is 0; with references a row before a frame is labelled with it, and a frame's row
|
||||
* also carries its unseen codes (tower.py, prefill). The last row's pass is FRAME 0's, which
|
||||
* writes its cache rows again. */
|
||||
const RadKVGroupBatch* kvb = kv_batch(batch, m.kv_full);
|
||||
RAD_ISSUE_N(c, m.op_efill, T, bcol(m.b_tcat, 0, D, T));
|
||||
if (!refs) {
|
||||
RAD_ISSUE_N(c, m.op_efill, T, bcol(m.b_tcat, 0, D, T));
|
||||
} else {
|
||||
RAD_ISSUE_N(c, m.op_pufill, T, brows(m.b_pu, T));
|
||||
RAD_ISSUE_N(c, m.op_pu, T, bcol_at(m.b_pc, 2, kPcW, 0, T, kCodes - 2), sec2, sec3,
|
||||
brows(m.b_pu, T));
|
||||
}
|
||||
TowerAt at{};
|
||||
at.R = T;
|
||||
at.max_q = batch->max_q_len;
|
||||
@@ -1501,6 +2034,7 @@ static void step_prompt(RadCtx* c, const RadBatch* batch, const Model& m) {
|
||||
at.used = praw(kvb->seqused, RAD_I32, batch->n_seq);
|
||||
at.cu = praw(batch->cu_seqlens, RAD_I32, batch->n_seq + 1);
|
||||
at.h0 = m.a_h.x;
|
||||
at.labels = refs;
|
||||
step_tower(c, m, at);
|
||||
}
|
||||
|
||||
@@ -1566,7 +2100,6 @@ static void step_frame(RadCtx* c, const RadBatch* batch, const Model& m) {
|
||||
static void step_vocode(RadCtx* c, const RadBatch* batch, const Model& m) {
|
||||
const RadAudioGenBatch* A = batch->agen_b;
|
||||
const int64_t nl = A->voc_latents, n = nl - A->voc_left, L = A->voc_lane;
|
||||
const int64_t qd = kCtHeads * kCtHeadDim;
|
||||
/* The convolutions run over the last kConvFrames frames, or all of them when fewer. */
|
||||
const int64_t nc = std::min<int64_t>(nl, kConvFrames), c0 = nl - nc;
|
||||
const RadOperand nlen = praw(batch->cu_seqlens + 1, RAD_I32, 1);
|
||||
@@ -1587,23 +2120,11 @@ static void step_vocode(RadCtx* c, const RadBatch* batch, const Model& m) {
|
||||
}
|
||||
|
||||
/* 3. the transformer over every frame of the pass */
|
||||
const RadOperand pos = praw(batch->positions, RAD_I32, nl);
|
||||
const RadOperand cu = brows(m.b_ccu[0], 2);
|
||||
for (const CodecLayer& Ly : m.ct) {
|
||||
RAD_ISSUE_N(c, Ly.op_n1, nl, brows(m.b_cx, nl), RAD_W(Ly.w_n1), brows(m.b_ch, nl));
|
||||
RAD_ISSUE_N(c, Ly.op_qkv, nl, brows(m.b_ch, nl), RAD_W(Ly.w_qkv), brows(m.b_cqkv, nl));
|
||||
RAD_ISSUE_N(c, Ly.op_rope, nl, brows(m.b_cqkv, nl), pos);
|
||||
RAD_ISSUE_N(c, Ly.op_attn, nl, bcol(m.b_cqkv, 0, qd, nl), bcol(m.b_cqkv, qd, qd, nl),
|
||||
bcol(m.b_cqkv, 2 * qd, qd, nl), cu, brows(m.b_cattn, nl));
|
||||
RAD_ISSUE_N(c, Ly.op_o, nl, brows(m.b_cattn, nl), RAD_W(Ly.w_o), brows(m.b_co, nl));
|
||||
RAD_ISSUE_N(c, Ly.op_ls1, nl, brows(m.b_co, nl), RAD_W(Ly.w_ls1), brows(m.b_co, nl));
|
||||
RAD_ISSUE_N(c, Ly.op_add1, nl, brows(m.b_cx, nl), brows(m.b_co, nl), brows(m.b_cx, nl));
|
||||
RAD_ISSUE_N(c, Ly.op_n2, nl, brows(m.b_cx, nl), RAD_W(Ly.w_n2), brows(m.b_ch, nl));
|
||||
RAD_ISSUE_N(c, Ly.op_gu, nl, brows(m.b_ch, nl), RAD_W(Ly.w_gu), brows(m.b_cgu, nl));
|
||||
RAD_ISSUE_N(c, Ly.op_act, nl, brows(m.b_cgu, nl), brows(m.b_cff, nl));
|
||||
RAD_ISSUE_N(c, Ly.op_down, nl, brows(m.b_cff, nl), RAD_W(Ly.w_down), brows(m.b_co, nl));
|
||||
RAD_ISSUE_N(c, Ly.op_ls2, nl, brows(m.b_co, nl), RAD_W(Ly.w_ls2), brows(m.b_co, nl));
|
||||
RAD_ISSUE_N(c, Ly.op_add2, nl, brows(m.b_cx, nl), brows(m.b_co, nl), brows(m.b_cx, nl));
|
||||
{
|
||||
CtSet B;
|
||||
B.x = m.b_cx; B.h = m.b_ch; B.qkv = m.b_cqkv; B.attn = m.b_cattn; B.o = m.b_co;
|
||||
B.gu = m.b_cgu; B.ff = m.b_cff;
|
||||
step_ct(c, m.ct, nl, B, praw(batch->positions, RAD_I32, nl), brows(m.b_ccu[0], 2));
|
||||
}
|
||||
RAD_ISSUE_N(c, m.op_cn, nc, brow_slice(m.b_cx, c0, nc, kLat), RAD_W(m.w_cn),
|
||||
brows(m.b_ux[0], nc));
|
||||
@@ -1766,13 +2287,28 @@ static std::string speaker_turns(const std::string& r) {
|
||||
return outs;
|
||||
}
|
||||
|
||||
/* generate.py's validate_drama3_text: the input is not empty, holds none of the tokens only the
|
||||
* prompt builder writes, and names speakers only as <|speaker:0|> to <|speaker:15|>; then its
|
||||
* speaker turns are written as the reference's server writes them (speaker_turns). */
|
||||
/* generate.py's build_drama3_prompt for a reference's transcript: speaker 0's tag and a space in
|
||||
* front unless a tag leads after white space (with_speaker_tag), and white space ahead of a
|
||||
* leading tag moved behind it, so the tag follows "Text: " directly. `r` is validated already. */
|
||||
static std::string reference_text(const std::string& r) {
|
||||
const unsigned char* u = (const unsigned char*)r.data();
|
||||
size_t lead = 0;
|
||||
for (size_t w; lead < r.size() && (w = white_space_at(u + lead, r.size() - lead)) > 0;)
|
||||
lead += w;
|
||||
const size_t tag = speaker_tag_at(r.data() + lead, r.size() - lead);
|
||||
if (!tag) return "<|speaker:0|> " + r;
|
||||
return r.substr(lead, tag) + r.substr(0, lead) + r.substr(lead + tag);
|
||||
}
|
||||
|
||||
/* generate.py's validate_drama3_text: the input, or a reference's transcript, is not empty, holds
|
||||
* none of the tokens only the prompt builder writes, and names speakers only as <|speaker:0|> to
|
||||
* <|speaker:15|>; then the input's speaker turns are written as the reference's server writes
|
||||
* them (speaker_turns), and a transcript is tagged as its prompt builder tags it. */
|
||||
static int64_t text_prepare(int field, const char* in, char* out, int64_t cap) {
|
||||
if (!in) return RAD_E_INVAL;
|
||||
std::string r = in;
|
||||
if (field == RAD_TEXT_INPUT) {
|
||||
if (field == RAD_TEXT_INPUT || field == RAD_TEXT_REFERENCE) {
|
||||
const bool ref = field == RAD_TEXT_REFERENCE;
|
||||
const unsigned char* u = (const unsigned char*)r.data();
|
||||
const size_t n = r.size();
|
||||
size_t lead = 0;
|
||||
@@ -1783,21 +2319,23 @@ static int64_t text_prepare(int field, const char* in, char* out, int64_t cap) {
|
||||
if (w == 0) blank = false;
|
||||
else i += w;
|
||||
}
|
||||
if (blank) return refuse(out, cap, "the input is empty: there is nothing to speak");
|
||||
if (blank)
|
||||
return refuse(out, cap, ref ? "the reference's text is empty: it is what its clip says"
|
||||
: "the input is empty: there is nothing to speak");
|
||||
static const char* kStructural[] = { "<|im_start|>", "<|im_end|>", "<|voice|>",
|
||||
"<|text|>", "<|interleave|>", "<|semantic:" };
|
||||
for (const char* t : kStructural)
|
||||
if (r.find(t) != std::string::npos) {
|
||||
char why[128];
|
||||
std::snprintf(why, sizeof why, "%s is written by the prompt builder, not the input",
|
||||
t);
|
||||
std::snprintf(why, sizeof why, "%s is written by the prompt builder, not %s", t,
|
||||
ref ? "a reference's text" : "the input");
|
||||
return refuse(out, cap, why);
|
||||
}
|
||||
for (size_t at = r.find("<|speaker"); at != std::string::npos;
|
||||
at = r.find("<|speaker", at + 1))
|
||||
if (!speaker_tag_at(r.data() + at, n - at))
|
||||
return refuse(out, cap, "speaker tags are <|speaker:0|> to <|speaker:15|>");
|
||||
r = speaker_turns(r);
|
||||
r = ref ? reference_text(r) : speaker_turns(r);
|
||||
if (r.empty()) return refuse(out, cap, "the input is empty: there is nothing to speak");
|
||||
}
|
||||
if ((int64_t)r.size() < cap) {
|
||||
|
||||
@@ -7,8 +7,10 @@
|
||||
# The directory holds the model and codec.pth, its codec, which is read as checkpoint tensors
|
||||
# under `codec.` (core/format/torch_zip.h). The model is kept as stored. The codec ships f32:
|
||||
# its weight-normalised convolutions are folded here, and its linears, convolutions and
|
||||
# codebooks cast to bf16, the precision fish-speech decodes at, while its norms, layer scales,
|
||||
# ConvNeXt gains and snake parameters stay f32 -- read once a pass or less.
|
||||
# codebooks cast to bf16, the precision fish-speech decodes and encodes at, while its norms,
|
||||
# layer scales, ConvNeXt gains and snake parameters stay f32 -- read once a pass or less. The
|
||||
# encoder's residual quantiser (codec.enc.vq*) stays f32 too: each step is an argmax over
|
||||
# cosines, which f32 weights decide as the reference's f32 encoder does.
|
||||
|
||||
codec.*.n1.weight cast dtype=f32
|
||||
codec.*.n2.weight cast dtype=f32
|
||||
@@ -18,4 +20,8 @@ codec.norm.weight cast dtype=f32
|
||||
codec.up*.ln.* cast dtype=f32
|
||||
codec.up*.gamma cast dtype=f32
|
||||
codec.*.alpha cast dtype=f32
|
||||
codec.enc.*norm.weight cast dtype=f32
|
||||
codec.enc.down*.ln.* cast dtype=f32
|
||||
codec.enc.down*.gamma cast dtype=f32
|
||||
codec.enc.vq* cast dtype=f32
|
||||
codec.* cast dtype=bf16
|
||||
@@ -196,4 +196,48 @@ TEST(fishomni_writes_the_input_as_the_reference_server_does) {
|
||||
CHECK_EQ(std::string(buf), std::string(" calm "));
|
||||
}
|
||||
|
||||
/* A reference's transcript as generate.py's build_drama3_prompt writes it after "Text: ": speaker
|
||||
* 0's tag and a space before untagged text, which is otherwise kept as given; white space before a
|
||||
* leading tag moved behind it. The input's rules refuse the same tokens. */
|
||||
TEST(fishomni_tags_a_reference_transcript_as_the_prompt_builder_does) {
|
||||
Registry r;
|
||||
const int st = r.load_dir(dir_of("architectures"), {});
|
||||
if (st == RAD_E_IO && r.plugins().empty())
|
||||
ARTIFACT_MISSING("the architecture plugins under " + dir_of("architectures"));
|
||||
CHECK_OK(st);
|
||||
const Plugin* p = r.plugin("fishomni");
|
||||
REQUIRE(p != nullptr);
|
||||
REQUIRE(p->arch_text_prepare != nullptr);
|
||||
auto prep = [&](const char* in, std::string* out) {
|
||||
char buf[512] = {};
|
||||
const int64_t n = p->arch_text_prepare(RAD_TEXT_REFERENCE, in, buf, sizeof buf);
|
||||
*out = buf;
|
||||
return n;
|
||||
};
|
||||
const struct { const char* in; const char* want; } ok[] = {
|
||||
{ "We asked over twenty people.", "<|speaker:0|> We asked over twenty people." },
|
||||
{ " Two spaces kept. ", "<|speaker:0|> Two spaces kept. " },
|
||||
{ " <|speaker:1|>Hi there", "<|speaker:1|> Hi there" },
|
||||
{ "<|speaker:3|> Hello. <|speaker:4|> Hi.", "<|speaker:3|> Hello. <|speaker:4|> Hi." },
|
||||
};
|
||||
for (const auto& c : ok) {
|
||||
std::string got;
|
||||
const int64_t n = prep(c.in, &got);
|
||||
if (got != c.want) fprintf(stderr, " '%s' -> '%s'\n", c.in, got.c_str());
|
||||
CHECK_EQ(n, (int64_t)std::strlen(c.want));
|
||||
CHECK_EQ(got, std::string(c.want));
|
||||
}
|
||||
const struct { const char* in; const char* why; } refused[] = {
|
||||
{ " \n ", "empty" },
|
||||
{ "Then <|im_start|> came", "<|im_start|>" },
|
||||
{ "<|speaker:20|> hi", "speaker tags" },
|
||||
};
|
||||
for (const auto& c : refused) {
|
||||
std::string why;
|
||||
CHECK(prep(c.in, &why) < 0);
|
||||
if (why.find(c.why) == std::string::npos) fprintf(stderr, " '%s': %s\n", c.in, why.c_str());
|
||||
CHECK(why.find(c.why) != std::string::npos);
|
||||
}
|
||||
}
|
||||
|
||||
RAD_TEST_MAIN()
|
||||
Reference in new issue
Block a user