Skip to content

Commit 628bc0a

Browse files
committed
Move MOSS token row builder into codec runtime
1 parent b0c05e4 commit 628bc0a

17 files changed

Lines changed: 186 additions & 187 deletions

File tree

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,6 @@ audiocpp_add_model(moss
801801
src/models/moss/moss_tts_nano/prompt_builder.cpp
802802
src/models/moss/moss_tts_nano/session.cpp
803803
src/models/moss/moss_tts_nano/tokenizer_text.cpp
804-
src/models/moss/shared/token_rows.cpp
805804
src/models/moss/moss_tts_local/depth_transformer.cpp
806805
src/models/moss/moss_tts_local/generator.cpp
807806
src/models/moss/moss_tts_local/loader.cpp

include/engine/community_models/moss_voicegen/session.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "engine/framework/codecs/moss_audio_tokenizer_codec_runtime.h"
1010
#include "engine/framework/modules/multi_codebook_embedding.h"
1111
#include "engine/framework/runtime/session_base.h"
12-
#include "engine/models/moss/shared/token_rows.h"
1312

1413
#include <cstddef>
1514
#include <memory>

include/engine/community_models/moss_voicegen/tokenizer_text.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

33
#include "engine/community_models/moss_voicegen/assets.h"
4-
#include "engine/models/moss/shared/token_rows.h"
4+
#include "engine/framework/codecs/moss_audio_tokenizer_codec_runtime.h"
55

66
#include <memory>
77
#include <optional>
@@ -23,7 +23,7 @@ class MossVoiceGenTextProcessor {
2323

2424
// `instruction` describes the speaker to design. `language` must be the full language
2525
// name the model was trained on ("English", not "en"); an empty value renders "None".
26-
moss::TokenRows build_generation_prefix(
26+
engine::codecs::MossTokenRows build_generation_prefix(
2727
const std::string & text,
2828
const std::optional<std::string> & instruction,
2929
const std::optional<std::string> & language) const;

include/engine/framework/codecs/moss_audio_tokenizer_codec_runtime.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,27 @@ struct MossAudioTokenizerCodes {
5858
std::vector<std::vector<int32_t>> codebooks;
5959
};
6060

61+
struct MossTokenRows {
62+
std::vector<int32_t> text_tokens;
63+
std::vector<int32_t> audio_codes;
64+
};
65+
66+
class MossTokenRowBuilder {
67+
public:
68+
MossTokenRowBuilder(int64_t num_codebooks, int32_t audio_pad_token_id);
69+
70+
void push_text_token(int32_t token_id);
71+
void push_text_tokens(const std::vector<int32_t> & token_ids);
72+
void push_audio_row(int32_t text_slot_token_id, const int32_t * codes, int64_t num_codebooks);
73+
void push_audio_row(int32_t text_slot_token_id, const std::vector<std::vector<int32_t>> & codes, int64_t frame);
74+
MossTokenRows finish();
75+
76+
private:
77+
int64_t num_codebooks_ = 0;
78+
int32_t audio_pad_token_id_ = 0;
79+
MossTokenRows rows_;
80+
};
81+
6182
struct MossAudioTokenizerCodecRuntimeOptions {
6283
size_t weight_context_bytes = 256ull * 1024ull * 1024ull;
6384
size_t encoder_graph_arena_bytes = 2048ull * 1024ull * 1024ull;

include/engine/models/moss/moss_tts_local/generator.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include "engine/models/moss/moss_tts_local/backbone.h"
55
#include "engine/models/moss/moss_tts_local/depth_transformer.h"
66
#include "engine/framework/modules/multi_codebook_embedding.h"
7-
#include "engine/models/moss/shared/token_rows.h"
87
#include "engine/framework/sampling/torch_random.h"
98

109
#include <cstddef>

include/engine/models/moss/moss_tts_local/tokenizer_text.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include "engine/models/moss/shared/token_rows.h"
3+
#include "engine/framework/codecs/moss_audio_tokenizer_codec_runtime.h"
44
#include "engine/models/moss/moss_tts_local/assets.h"
55

66
#include <cstdint>
@@ -14,7 +14,7 @@ namespace engine::models::moss_tts_local {
1414
// Decoder input for a generation request: the text channel (input_ids[..., 0]) plus the
1515
// n_vq audio channels flattened row-major as [seq, n_vq] (input_ids[..., 1:]). Every audio
1616
// slot of the prompt carries audio_pad_token_id, matching MossTTSLocalProcessor._build_text_rows.
17-
using MossGenerationPrefix = moss::TokenRows;
17+
using MossGenerationPrefix = engine::codecs::MossTokenRows;
1818

1919
// Reproduces the direct-generation branch of MossTTSLocalProcessor: it renders the
2020
// <user_inst> template, byte-level BPE encodes each piece with the Qwen tokenizer, and

include/engine/models/moss/shared/token_rows.h

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/community_models/moss_voicegen/tokenizer_text.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ MossVoiceGenTextProcessor::MossVoiceGenTextProcessor(std::shared_ptr<const MossV
8585

8686
MossVoiceGenTextProcessor::~MossVoiceGenTextProcessor() = default;
8787

88-
moss::TokenRows MossVoiceGenTextProcessor::build_generation_prefix(
88+
engine::codecs::MossTokenRows MossVoiceGenTextProcessor::build_generation_prefix(
8989
const std::string & text,
9090
const std::optional<std::string> & instruction,
9191
const std::optional<std::string> & language) const {
9292
const auto & config = impl_->assets->config;
93-
moss::TokenRowBuilder builder(config.num_codebooks, static_cast<int32_t>(config.audio_pad_code));
93+
engine::codecs::MossTokenRowBuilder builder(config.num_codebooks, static_cast<int32_t>(config.audio_pad_code));
9494

9595
// Render the whole turn as one string and encode it in a single pass, the way the
9696
// reference processor does. Encoding the fragments separately would split merges

src/framework/codecs/moss_audio_tokenizer_codec_runtime.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,64 @@ inline std::vector<float> causal_context_mask_window(
353353

354354
namespace engine::codecs {
355355

356+
MossTokenRowBuilder::MossTokenRowBuilder(int64_t num_codebooks, int32_t audio_pad_token_id)
357+
: num_codebooks_(num_codebooks),
358+
audio_pad_token_id_(audio_pad_token_id) {
359+
if (num_codebooks_ <= 0) {
360+
throw std::runtime_error("MOSS token row builder requires a positive codebook count");
361+
}
362+
}
363+
364+
void MossTokenRowBuilder::push_text_token(int32_t token_id) {
365+
rows_.text_tokens.push_back(token_id);
366+
rows_.audio_codes.insert(rows_.audio_codes.end(), static_cast<size_t>(num_codebooks_), audio_pad_token_id_);
367+
}
368+
369+
void MossTokenRowBuilder::push_text_tokens(const std::vector<int32_t> & token_ids) {
370+
for (const int32_t token_id : token_ids) {
371+
push_text_token(token_id);
372+
}
373+
}
374+
375+
void MossTokenRowBuilder::push_audio_row(int32_t text_slot_token_id, const int32_t * codes, int64_t num_codebooks) {
376+
if (num_codebooks != num_codebooks_) {
377+
throw std::runtime_error("MOSS audio row codebook count mismatch");
378+
}
379+
if (codes == nullptr) {
380+
throw std::runtime_error("MOSS audio row codes are missing");
381+
}
382+
rows_.text_tokens.push_back(text_slot_token_id);
383+
rows_.audio_codes.insert(rows_.audio_codes.end(), codes, codes + num_codebooks);
384+
}
385+
386+
void MossTokenRowBuilder::push_audio_row(
387+
int32_t text_slot_token_id,
388+
const std::vector<std::vector<int32_t>> & codes,
389+
int64_t frame) {
390+
if (static_cast<int64_t>(codes.size()) != num_codebooks_) {
391+
throw std::runtime_error("MOSS audio row codebook count mismatch");
392+
}
393+
rows_.text_tokens.push_back(text_slot_token_id);
394+
for (int64_t codebook = 0; codebook < num_codebooks_; ++codebook) {
395+
const auto & channel = codes[static_cast<size_t>(codebook)];
396+
if (frame < 0 || static_cast<size_t>(frame) >= channel.size()) {
397+
throw std::runtime_error("MOSS audio row frame index is out of range");
398+
}
399+
rows_.audio_codes.push_back(channel[static_cast<size_t>(frame)]);
400+
}
401+
}
402+
403+
MossTokenRows MossTokenRowBuilder::finish() {
404+
if (rows_.text_tokens.empty()) {
405+
throw std::runtime_error("MOSS token rows must not be empty");
406+
}
407+
if (static_cast<int64_t>(rows_.audio_codes.size()) !=
408+
static_cast<int64_t>(rows_.text_tokens.size()) * num_codebooks_) {
409+
throw std::runtime_error("MOSS token rows audio code shape mismatch");
410+
}
411+
return std::move(rows_);
412+
}
413+
356414
// Dequantizes MOSS-Audio-Tokenizer-v2 codes (RLFQ) into the codec's continuous
357415
// latent, i.e. the input to the codec decoder stack. Codes are the
358416
// [num_quantizers, steps] matrix produced by generation; the returned latent is

src/models/moss/moss_tts_local/session.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ uint64_t mix_reference_audio_key(uint64_t key, uint64_t value) {
3939
return key;
4040
}
4141

42-
uint64_t prefix_hash(const moss::TokenRows & prefix, int64_t num_codebooks) {
42+
uint64_t prefix_hash(const engine::codecs::MossTokenRows & prefix, int64_t num_codebooks) {
4343
uint64_t key = 1469598103934665603ull;
4444
for (size_t row = 0; row < prefix.text_tokens.size(); ++row) {
4545
key = mix_reference_audio_key(key, static_cast<uint32_t>(prefix.text_tokens[row]));
@@ -53,7 +53,7 @@ uint64_t prefix_hash(const moss::TokenRows & prefix, int64_t num_codebooks) {
5353
return key;
5454
}
5555

56-
int64_t prefix_audio_nonpad_count(const moss::TokenRows & prefix, int32_t audio_pad_token_id) {
56+
int64_t prefix_audio_nonpad_count(const engine::codecs::MossTokenRows & prefix, int32_t audio_pad_token_id) {
5757
int64_t count = 0;
5858
for (const int32_t code : prefix.audio_codes) {
5959
if (code != audio_pad_token_id) {

0 commit comments

Comments
 (0)