-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllama_runtime.cpp
More file actions
55 lines (44 loc) · 1.15 KB
/
Copy pathllama_runtime.cpp
File metadata and controls
55 lines (44 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "llama_runtime.h"
#include "llama.h"
#include <clocale>
#include <cstdio>
#include <mutex>
#include <thread>
namespace vox::llama {
namespace {
void quiet_llama_log(ggml_log_level level, const char * text, void *) {
if (level == GGML_LOG_LEVEL_ERROR && text != nullptr) {
fputs(text, stderr);
}
}
std::mutex & runtime_mutex() {
static std::mutex value;
return value;
}
int & runtime_ref_count() {
static int value = 0;
return value;
}
} // namespace
LlamaRuntime::LlamaRuntime() {
std::lock_guard<std::mutex> lock(runtime_mutex());
if (runtime_ref_count() == 0) {
std::setlocale(LC_NUMERIC, "C");
llama_log_set(quiet_llama_log, nullptr);
llama_backend_init();
ggml_backend_load_all();
}
++runtime_ref_count();
}
LlamaRuntime::~LlamaRuntime() {
std::lock_guard<std::mutex> lock(runtime_mutex());
--runtime_ref_count();
if (runtime_ref_count() == 0) {
llama_backend_free();
}
}
int default_thread_count() {
const unsigned int count = std::thread::hardware_concurrency();
return count == 0 ? 4 : static_cast<int>(count);
}
} // namespace vox::llama