Skip to content

Commit b6b3e02

Browse files
committed
parakeet : fix TDT decode by outputting raw logits from the joint graph
1 parent a722846 commit b6b3e02

2 files changed

Lines changed: 15 additions & 13 deletions

File tree

include/parakeet.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ extern "C" {
176176
// Token logits obtained from the last call to parakeet_full/parakeet_chunk
177177
// The logits for the last token are stored in the last row
178178
// Rows: n_tokens
179-
// Cols: n_vocab
179+
// Cols: n_vocab + 1 token logits (the blank token is at index n_vocab),
180+
// followed by n_tdt_durations duration logits
180181
PARAKEET_API float * parakeet_get_logits (struct parakeet_context * ctx);
181182
PARAKEET_API float * parakeet_get_logits_from_state(struct parakeet_state * state);
182183

src/parakeet.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2302,12 +2302,7 @@ static struct ggml_cgraph * parakeet_build_graph_joint(
23022302
ggml_set_output(logits);
23032303
ggml_set_name(logits, "logits");
23042304

2305-
struct ggml_tensor * probs = ggml_soft_max(ctx0, logits);
2306-
struct ggml_tensor * log_probs = ggml_log(ctx0, probs);
2307-
ggml_set_output(log_probs);
2308-
ggml_format_name(log_probs, "log_probs");
2309-
2310-
ggml_build_forward_expand(gf, log_probs);
2305+
ggml_build_forward_expand(gf, logits);
23112306

23122307
ggml_free(ctx0);
23132308

@@ -2473,19 +2468,25 @@ static parakeet_token_data create_token_data(
24732468
float token_logit,
24742469
int n_vocab_logits) {
24752470

2471+
float max_logit = token_logit;
2472+
for (int i = 0; i < n_vocab_logits; ++i) {
2473+
max_logit = std::max(max_logit, pstate.logits[i]);
2474+
}
2475+
24762476
float token_sum = 0.0f;
24772477
for (int i = 0; i < n_vocab_logits; ++i) {
2478-
token_sum += expf(pstate.logits[i]);
2478+
token_sum += expf(pstate.logits[i] - max_logit);
24792479
}
2480-
float token_p = expf(token_logit) / token_sum;
2480+
2481+
const float log_z = max_logit + logf(token_sum);
24812482

24822483
parakeet_token_data token_data;
24832484
token_data.id = token_id;
24842485
token_data.duration_idx = duration_idx;
24852486
token_data.duration_value = duration_value;
24862487
token_data.frame_index = frame_index;
2487-
token_data.p = token_p;
2488-
token_data.plog = token_logit;
2488+
token_data.p = expf(token_logit - log_z);
2489+
token_data.plog = token_logit - log_z;
24892490
token_data.t0 = frame_index * pctx.model.hparams.subsampling_factor;
24902491
token_data.t1 = (frame_index + duration_value) * pctx.model.hparams.subsampling_factor;
24912492
token_data.is_word_start = is_word_start_token(pctx.vocab, token_id);
@@ -2566,8 +2567,8 @@ static bool parakeet_decode(
25662567
// find the max index of the duration logits, and look up that index
25672568
// value in the tdt_durations array to get the actual duration value.
25682569
int best_duration_idx = 0;
2569-
float best_duration_logit = -1e10f;
2570-
for (int i = 0; i < n_tdt_durations; ++i) {
2570+
float best_duration_logit = pstate.logits[n_vocab_logits];
2571+
for (int i = 1; i < n_tdt_durations; ++i) {
25712572
if (pstate.logits[n_vocab_logits + i] > best_duration_logit) {
25722573
best_duration_logit = pstate.logits[n_vocab_logits + i];
25732574
best_duration_idx = i;

0 commit comments

Comments
 (0)