Skip to content

Commit 08c4256

Browse files
committed
run model in Java thread for debugging
1 parent 913d201 commit 08c4256

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

src/main/cpp/jllama.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -454,22 +454,9 @@ JNIEXPORT void JNICALL Java_de_kherud_llama_LlamaModel_loadModel(JNIEnv *env, jo
454454
std::placeholders::_1, std::placeholders::_2,
455455
std::placeholders::_3));
456456

457-
std::thread t([ctx_server]() {
458-
JNIEnv *env;
459-
jint res = g_vm->GetEnv((void **)&env, JNI_VERSION_1_6);
460-
if (res == JNI_EDETACHED)
461-
{
462-
res = g_vm->AttachCurrentThread((void **)&env, nullptr);
463-
if (res != JNI_OK)
464-
{
465-
throw std::runtime_error("Failed to attach thread to JVM");
466-
}
467-
}
468-
ctx_server->queue_tasks.start_loop();
469-
});
470-
t.detach();
471-
472457
env->SetLongField(obj, f_model_pointer, reinterpret_cast<jlong>(ctx_server));
458+
459+
ctx_server->queue_tasks.start_loop();
473460
}
474461

475462
JNIEXPORT jint JNICALL Java_de_kherud_llama_LlamaModel_requestCompletion(JNIEnv *env, jobject obj, jstring jparams)

src/main/java/de/kherud/llama/LlamaModel.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class LlamaModel implements AutoCloseable {
2828

2929
@Native
3030
private long ctx;
31+
private final Thread modelThread;
3132

3233
/**
3334
* Load with the given {@link ModelParameters}. Make sure to either set
@@ -41,7 +42,14 @@ public class LlamaModel implements AutoCloseable {
4142
* @throws LlamaException if no model could be loaded from the given file path
4243
*/
4344
public LlamaModel(ModelParameters parameters) {
44-
loadModel(parameters.toString());
45+
this.modelThread = new Thread(() -> loadModel(parameters.toString()));
46+
this.modelThread.start();
47+
try {
48+
Thread.sleep(30000);
49+
}
50+
catch (InterruptedException e) {
51+
throw new RuntimeException(e);
52+
}
4553
}
4654

4755
/**

0 commit comments

Comments
 (0)