Skip to content

Commit 661bede

Browse files
committed
optimize tokenize method
1 parent b95a4cc commit 661bede

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

gpttype_adapter.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -338,34 +338,32 @@ static std::string FileFormatTokenizeID(int id, FileFormat file_format)
338338
}
339339
}
340340

341-
static std::vector<int> TokenizeString(const std::string & str_to_tokenize, FileFormat file_format)
341+
static void TokenizeString(const std::string & str_to_tokenize, std::vector<int> & output_tokens, FileFormat file_format)
342342
{
343-
std::vector<int> tokvec;
344343
if (file_format == FileFormat::GGML || file_format == FileFormat::GGHF || file_format == FileFormat::GGJT || file_format == FileFormat::GGJT_2 || file_format == FileFormat::GGJT_3 || file_format == FileFormat::GGUF_LLAMA)
345344
{
346345
if(file_format == FileFormat::GGHF || file_format == FileFormat::GGJT || file_format == FileFormat::GGJT_2 )
347346
{
348-
tokvec = ::llama_v2_tokenize(llama_ctx_v2, str_to_tokenize, true);
347+
output_tokens = ::llama_v2_tokenize(llama_ctx_v2, str_to_tokenize, true);
349348
}
350349
else if (file_format == FileFormat::GGML)
351350
{
352-
tokvec = ::legacy_llama_v2_tokenize(llama_ctx_v2, str_to_tokenize, true);
351+
output_tokens = ::legacy_llama_v2_tokenize(llama_ctx_v2, str_to_tokenize, true);
353352
}
354353
else if (file_format == FileFormat::GGJT_3)
355354
{
356-
tokvec = ::llama_v3_tokenize(llama_ctx_v3, str_to_tokenize, true);
355+
output_tokens = ::llama_v3_tokenize(llama_ctx_v3, str_to_tokenize, true);
357356
}
358357
else
359358
{
360-
tokvec = ::llama_tokenize(llama_ctx_v4, str_to_tokenize, true);
359+
output_tokens = ::llama_tokenize(llama_ctx_v4, str_to_tokenize, true);
361360
}
362361
}
363362
else
364363
{
365364
// tokenize the prompt
366-
tokvec = ::gpt_tokenize(vocab, str_to_tokenize);
365+
output_tokens = ::gpt_tokenize(vocab, str_to_tokenize);
367366
}
368-
return tokvec;
369367
}
370368

371369
static std::string RemoveBell(const std::string & input) //removes the bell character
@@ -1001,7 +999,8 @@ int gpttype_token_count(const std::string & input)
1001999
{
10021000
printf("\nFileFormat: %d, Tokenizing: %s",file_format ,input.c_str());
10031001
}
1004-
auto toks = TokenizeString(input, file_format);
1002+
std::vector<int> toks;
1003+
TokenizeString(input, toks, file_format);
10051004
int tokcount = toks.size();
10061005
if(debugmode==1)
10071006
{
@@ -1063,7 +1062,8 @@ generation_outputs gpttype_generate(const generation_inputs inputs, generation_o
10631062
}
10641063

10651064
// tokenize the prompt
1066-
std::vector<int> embd_inp = TokenizeString(params.prompt, file_format);
1065+
std::vector<int> embd_inp;
1066+
TokenizeString(params.prompt, embd_inp, file_format);
10671067

10681068
//truncate to front of the prompt if its too long
10691069
int32_t nctx = params.n_ctx;

0 commit comments

Comments
 (0)