@@ -3721,7 +3721,7 @@ int whisper_tokenize(struct whisper_context * ctx, const char * text, whisper_to
3721
3721
3722
3722
if (n_max_tokens < (int ) res.size ()) {
3723
3723
WHISPER_LOG_ERROR (" %s: too many resulting tokens: %d (max %d)\n " , __func__, (int ) res.size (), n_max_tokens);
3724
- return -1 ;
3724
+ return -( int ) res. size () ;
3725
3725
}
3726
3726
3727
3727
for (int i = 0 ; i < (int ) res.size (); i++) {
@@ -3731,6 +3731,10 @@ int whisper_tokenize(struct whisper_context * ctx, const char * text, whisper_to
3731
3731
return res.size ();
3732
3732
}
3733
3733
3734
+ int whisper_token_count (struct whisper_context * ctx, const char * text) {
3735
+ return -whisper_tokenize (ctx, text, NULL , 0 );
3736
+ }
3737
+
3734
3738
int whisper_lang_max_id () {
3735
3739
auto max_id = 0 ;
3736
3740
for (const auto & kv : g_lang) {
@@ -5313,7 +5317,12 @@ int whisper_full_with_state(
5313
5317
// initial prompt
5314
5318
if (!params.prompt_tokens && params.initial_prompt ) {
5315
5319
prompt_tokens.resize (1024 );
5316
- prompt_tokens.resize (whisper_tokenize (ctx, params.initial_prompt , prompt_tokens.data (), prompt_tokens.size ()));
5320
+ int n_needed = whisper_tokenize (ctx, params.initial_prompt , prompt_tokens.data (), prompt_tokens.size ());
5321
+ if (n_needed < 0 ) {
5322
+ prompt_tokens.resize (-n_needed);
5323
+ n_needed = whisper_tokenize (ctx, params.initial_prompt , prompt_tokens.data (), prompt_tokens.size ());
5324
+ }
5325
+ prompt_tokens.resize (n_needed);
5317
5326
params.prompt_tokens = prompt_tokens.data ();
5318
5327
params.prompt_n_tokens = prompt_tokens.size ();
5319
5328
}
0 commit comments