Repository: ggerganov/whisper.cpp
File: src/whisper.cpp (C++)

Code before:

        decoder.probs.resize   (ctx->vocab.n_vocab);
        decoder.logits.resize  (ctx->vocab.n_vocab);
        decoder.logprobs.resize(ctx->vocab.n_vocab);
        decoder.logits_id.reserve(ctx->model.hparams.n_vocab);

        decoder.rng = std::mt19937(j);
    }

    // the accumulated text context split into static (prompt_past0) and dynamic (prompt_past1)
    auto & prompt_past0 = state->prompt_past0;
    auto & prompt_past1 = state->prompt_past1;
    if (params.no_context) {
        prompt_past0.clear();
        prompt_past1.clear();
    }

    // prepare prompt
    {
        std::vector<whisper_token> prompt_tokens;
        if (!params.prompt_tokens && params.initial_prompt) {
            prompt_tokens.resize(1024);
            int n_needed = whisper_tokenize(ctx, params.initial_prompt, prompt_tokens.data(), prompt_tokens.size());
            if (n_needed < 0) {
                prompt_tokens.resize(-n_needed);
                n_needed = whisper_tokenize(ctx, params.initial_prompt, prompt_tokens.data(), prompt_tokens.size());
            }
            prompt_tokens.resize(n_needed);
            params.prompt_tokens   = prompt_tokens.data();
            params.prompt_n_tokens = prompt_tokens.size();
        }
        if (params.prompt_tokens && params.prompt_n_tokens > 0) {
            if (params.carry_initial_prompt) {
                if (prompt_past0.empty()) {
                    prompt_past0.insert(prompt_past0.end(), params.prompt_tokens, params.prompt_tokens + params.prompt_n_tokens);
                }
            } else {
                prompt_past1.insert(prompt_past1.end(), params.prompt_tokens, params.prompt_tokens + params.prompt_n_tokens);
            }
        }
    }

    // overwrite audio_ctx, max allowed is hparams.n_audio_ctx
    if (params.audio_ctx > whisper_n_audio_ctx(ctx)) {
        WHISPER_LOG_ERROR("%s: audio_ctx is larger than the maximum allowed (%d > %d)\n", __func__, params.audio_ctx, whisper_n_audio_ctx(ctx));
        return -5;
    }
    state->exp_n_audio_ctx = params.audio_ctx;

    // these tokens determine the task that will be performed
    std::vector<whisper_token> prompt_init = { whisper_token_sot(ctx), };

Review comment (suggestion):
Keep the comment:

```suggestion
        // tokenize the initial prompt
        if (!params.prompt_tokens && params.initial_prompt) {
```

Code after:

        decoder.probs.resize   (ctx->vocab.n_vocab);
        decoder.logits.resize  (ctx->vocab.n_vocab);
        decoder.logprobs.resize(ctx->vocab.n_vocab);
        decoder.logits_id.reserve(ctx->model.hparams.n_vocab);

        decoder.rng = std::mt19937(j);
    }

    // the accumulated text context split into static (prompt_past0) and dynamic (prompt_past1)
    auto & prompt_past0 = state->prompt_past0;
    auto & prompt_past1 = state->prompt_past1;
    if (params.no_context) {
        prompt_past0.clear();
        prompt_past1.clear();
    }

    // calculate the maximum context budget for prompt history
    const int max_prompt_ctx = std::min(params.n_max_text_ctx, whisper_n_text_ctx(ctx)/2);

    // prepare prompt
    {
        std::vector<whisper_token> prompt_tokens;

        // tokenize the initial prompt
        if (!params.prompt_tokens && params.initial_prompt) {
            prompt_tokens.resize(1024);
            int n_needed = whisper_tokenize(ctx, params.initial_prompt, prompt_tokens.data(), prompt_tokens.size());
            if (n_needed < 0) {
                prompt_tokens.resize(-n_needed);
                n_needed = whisper_tokenize(ctx, params.initial_prompt, prompt_tokens.data(), prompt_tokens.size());
            }
            prompt_tokens.resize(n_needed);
            params.prompt_tokens   = prompt_tokens.data();
            params.prompt_n_tokens = prompt_tokens.size();
        }
        if (params.prompt_tokens && params.prompt_n_tokens > 0) {
            if (params.carry_initial_prompt) {
                if (prompt_past0.empty()) {
                    const int max_tokens = std::max(1, max_prompt_ctx - 1);

                    if (params.prompt_n_tokens > max_tokens) {
                        WHISPER_LOG_WARN("%s: initial prompt is too long (%d tokens), will use only the last %d tokens\n",
                                        __func__, params.prompt_n_tokens, max_tokens);
                    }

                    const int n_tokens = std::min(params.prompt_n_tokens, max_tokens);
                    prompt_past0.assign(params.prompt_tokens + (params.prompt_n_tokens - n_tokens), params.prompt_tokens + params.prompt_n_tokens);
                }
            } else {
                for (int i = 0; i < params.prompt_n_tokens; ++i) {