Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions packages/transformers/src/generation/logits_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,6 @@ export class WhisperTimeStampLogitsProcessor extends LogitsProcessor {
// suppress <|notimestamps|> which is handled by without_timestamps
batch_logits_data[this.no_timestamps_token_id] = -Infinity;

if (input_ids[i].length === this.begin_index) {
batch_logits_data.subarray(0, this.timestamp_begin).fill(-Infinity);
continue;
}

// timestamps have to appear in pairs, except directly before eos_token; mask logits accordingly
const seq = input_ids[i].slice(this.begin_index);
const last_was_timestamp = seq.length >= 1 && seq[seq.length - 1] >= this.timestamp_begin;
Expand All @@ -330,9 +325,13 @@ export class WhisperTimeStampLogitsProcessor extends LogitsProcessor {
}

// apply the `max_initial_timestamp` option
if (input_ids[i].length === this.begin_index && this.max_initial_timestamp_index !== null) {
const last_allowed = this.timestamp_begin + this.max_initial_timestamp_index;
batch_logits_data.subarray(last_allowed + 1).fill(-Infinity);
if (input_ids[i].length === this.begin_index) {
batch_logits_data.subarray(0, this.timestamp_begin).fill(-Infinity);

if (this.max_initial_timestamp_index != null) {
const last_allowed = this.timestamp_begin + this.max_initial_timestamp_index;
batch_logits_data.subarray(last_allowed + 1).fill(-Infinity);
}
}

// if sum of probability over timestamps is above any other token, sample timestamp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ export class WhisperGenerationConfig extends GenerationConfig {

/**
* Used to set the maximum value of the initial timestamp. This is used to prevent the model from
* predicting timestamps that are too far in the future.
* @type {number}
* predicting timestamps that are too far in the future. If `null`, no restriction is applied.
* @type {number|null}
*/
max_initial_timestamp_index = 1;
max_initial_timestamp_index = null;
}

/**
Expand Down
52 changes: 52 additions & 0 deletions packages/transformers/tests/utils/logits_process.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import {
// Pipelines
pipeline,
TextGenerationPipeline,

// Logits processors
WhisperTimeStampLogitsProcessor,

// Other
Tensor,
} from "../../src/transformers.js";

import { init } from "../init.js";
Expand Down Expand Up @@ -110,3 +116,49 @@ describe("Logits Processors", () => {
}, MAX_MODEL_DISPOSE_TIME);
});
});

describe("WhisperTimeStampLogitsProcessor", () => {
// Small synthetic vocabulary: [0..8] text tokens, 9 = <|endoftext|>, 10 = <|notimestamps|>, [11..18] timestamp tokens
const eos_token_id = 9;
const no_timestamps_token_id = 10;
const timestamp_begin = no_timestamps_token_id + 1;
const vocab_size = 19;
const init_tokens = [1, 2, 3]; // begin_index = 3

const create_logits = () => new Tensor("float32", new Float32Array(vocab_size).fill(0), [1, vocab_size]);
const finite_ids = (data) => Array.from(data, (x, i) => (Number.isFinite(x) ? i : -1)).filter((i) => i >= 0);

it("should apply `max_initial_timestamp_index` at the first decoding step", () => {
const max_initial_timestamp_index = 2;
const processor = new WhisperTimeStampLogitsProcessor({ eos_token_id, no_timestamps_token_id, max_initial_timestamp_index }, init_tokens);

const logits = processor([init_tokens.map(BigInt)], create_logits());

// Only the first `max_initial_timestamp_index + 1` timestamp tokens may be sampled at the first step
const last_allowed = timestamp_begin + max_initial_timestamp_index;
expect(finite_ids(logits.data)).toEqual([timestamp_begin, timestamp_begin + 1, last_allowed]);
});

it("should not restrict the initial timestamp when `max_initial_timestamp_index` is unset", () => {
const processor = new WhisperTimeStampLogitsProcessor({ eos_token_id, no_timestamps_token_id, max_initial_timestamp_index: null }, init_tokens);

const logits = processor([init_tokens.map(BigInt)], create_logits());

// All timestamp tokens remain available (text tokens are still suppressed at the first step)
const all_timestamps = Array.from({ length: vocab_size - timestamp_begin }, (_, i) => timestamp_begin + i);
expect(finite_ids(logits.data)).toEqual(all_timestamps);
});

it("should only apply `max_initial_timestamp_index` at the first decoding step", () => {
const max_initial_timestamp_index = 2;
const processor = new WhisperTimeStampLogitsProcessor({ eos_token_id, no_timestamps_token_id, max_initial_timestamp_index }, init_tokens);

// One text token has already been sampled after the prompt
const logits = processor([[...init_tokens, 5].map(BigInt)], create_logits());

const last_allowed = timestamp_begin + max_initial_timestamp_index;
for (let i = last_allowed + 1; i < vocab_size; ++i) {
expect(logits.data[i]).toBe(0);
}
});
});