@@ -224,17 +224,6 @@ class AddonModelLoadLoraWorker : public Napi::AsyncWorker {
224
224
};
225
225
226
226
AddonModel::AddonModel (const Napi::CallbackInfo& info) : Napi::ObjectWrap<AddonModel>(info) {
227
- loadedModelSize = 0 ;
228
- hasAddonExportsRef = false ;
229
- modelLoaded = false ;
230
- abortModelLoad = false ;
231
- model_load_stopped = false ;
232
- rawModelLoadPercentage = 0 ;
233
- modelLoadPercentage = 0 ;
234
- onLoadProgressEventCallbackSet = false ;
235
- hasLoadAbortSignal = false ;
236
- disposed = false ;
237
-
238
227
data = new AddonModelData ();
239
228
model_params = llama_model_default_params ();
240
229
@@ -456,18 +445,19 @@ Napi::Value AddonModel::Detokenize(const Napi::CallbackInfo& info) {
456
445
? info[1 ].As <Napi::Boolean>().Value ()
457
446
: false ;
458
447
459
- std::vector< char > result ( 8 , 0 ) ;
460
- const int n_length = llama_detokenize (model, (llama_token*)tokens. Data (), tokens.ElementLength (), result. data (), result. size (), false , decodeSpecialTokens );
448
+ std::string result;
449
+ result. resize ( std::max (result. capacity (), tokens.ElementLength ()) );
461
450
462
- if (n_length < 0 ) {
463
- result.resize (-n_length);
464
- int check = llama_detokenize (model, (llama_token*)tokens.Data (), tokens.ElementLength (), result.data (), result.size (), false , decodeSpecialTokens);
465
- GGML_ASSERT (check == -n_length);
466
- } else {
467
- result.resize (n_length);
451
+ int n_chars = llama_detokenize (model, (llama_token*)tokens.Data (), tokens.ElementLength (), &result[0 ], result.size (), false , decodeSpecialTokens);
452
+ if (n_chars < 0 ) {
453
+ result.resize (-n_chars);
454
+ n_chars = llama_detokenize (model, (llama_token*)tokens.Data (), tokens.ElementLength (), &result[0 ], result.size (), false , decodeSpecialTokens);
455
+ GGML_ASSERT (n_chars <= result.size ()); // whitespace trimming is performed after per-token detokenization
468
456
}
469
457
470
- return Napi::String::New (info.Env (), result.data (), result.size ());
458
+ result.resize (n_chars);
459
+
460
+ return Napi::String::New (info.Env (), result);
471
461
}
472
462
473
463
Napi::Value AddonModel::GetTrainContextSize (const Napi::CallbackInfo& info) {
0 commit comments