Skip to content

Commit a3ad9fc

Browse files
committed
Revert "run model in Java thread for debugging"
This reverts commit 08c4256.
1 parent 58b9889 commit a3ad9fc

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

src/main/cpp/jllama.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,22 @@ 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-
env->SetLongField(obj, f_model_pointer, reinterpret_cast<jlong>(ctx_server));
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();
458471

459-
ctx_server->queue_tasks.start_loop();
472+
env->SetLongField(obj, f_model_pointer, reinterpret_cast<jlong>(ctx_server));
460473
}
461474

462475
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: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public class LlamaModel implements AutoCloseable {
2828

2929
@Native
3030
private long ctx;
31-
private final Thread modelThread;
3231

3332
/**
3433
* Load with the given {@link ModelParameters}. Make sure to either set
@@ -42,14 +41,7 @@ public class LlamaModel implements AutoCloseable {
4241
* @throws LlamaException if no model could be loaded from the given file path
4342
*/
4443
public LlamaModel(ModelParameters parameters) {
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-
}
44+
loadModel(parameters.toString());
5345
}
5446

5547
/**

0 commit comments

Comments
 (0)