Skip to content

Commit

Permalink
fix: handle sampling init failure
Browse files Browse the repository at this point in the history
  • Loading branch information
jhen0409 committed Oct 20, 2023
1 parent ddd1b68 commit 43b1d71
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
6 changes: 5 additions & 1 deletion android/src/main/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,11 @@ Java_com_rnllama_LlamaContext_doCompletion(
env->ReleaseStringUTFChars(stop_str, stop_chars);
}

llama->initSampling();
if (!llama->initSampling()) {
auto result = createWriteableMap(env);
putString(env, result, "error", "Failed to initialize sampling");
return reinterpret_cast<jobject>(result);
}
llama->loadPrompt();
llama->beginCompletion();

Expand Down
3 changes: 2 additions & 1 deletion cpp/rn-llama.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,12 @@ struct llama_rn_context
params.sparams.n_prev = n_ctx;
}

void initSampling() {
bool initSampling() {
if (ctx_sampling != nullptr) {
llama_sampling_free(ctx_sampling);
}
ctx_sampling = llama_sampling_init(params.sparams);
return ctx_sampling != nullptr;
}

bool loadModel(gpt_params &params_)
Expand Down
4 changes: 3 additions & 1 deletion ios/RNLlamaContext.mm
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ - (NSDictionary *)completion:(NSDictionary *)params
}
}

llama->initSampling();
if (!llama->initSampling()) {
@throw [NSException exceptionWithName:@"LlamaException" reason:@"Failed to initialize sampling" userInfo:nil];
}
llama->loadPrompt();
llama->beginCompletion();

Expand Down

0 comments on commit 43b1d71

Please sign in to comment.