Skip to content

Commit f6bae3b

Browse files
committed
qual(cmake): support Vulcan build
1 parent ba62acf commit f6bae3b

4 files changed

Lines changed: 52 additions & 30 deletions

File tree

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ set(CMAKE_CXX_STANDARD 17)
99

1010

1111
option(BUILD_SHARED_LIBS "Build using shared libraries" OFF)
12-
option(LLAMA_OPENBLAS_ON "llama: use OpenBLAS" OFF)
1312
option(RENDEZLLAMA_COVERAGE_ON "Enable coverage instrumentation" OFF)
1413

1514

Makefile

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11

2-
# No OpenBLAS by default. Override like: make LLAMA_OPENBLAS_ON=1
3-
LLAMA_OPENBLAS_ON = 0
4-
52
BUILD_DIR = bld
63
SOURCE_DIR = .
74

@@ -10,7 +7,15 @@ GODO = $(CMAKE) -E chdir
107

118
CMAKE_BUILD_TYPE = RelOnHost
129
CMAKE_BUILD_OPTIONS = -DCMAKE_BUILD_TYPE=$(CMAKE_BUILD_TYPE)
13-
CMAKE_BUILD_OPTIONS += -DLLAMA_OPENBLAS_ON:BOOL=$(LLAMA_OPENBLAS_ON)
10+
ifdef GGML_BLAS
11+
CMAKE_BUILD_OPTIONS += -DGGML_BLAS:BOOL=$(GGML_BLAS)
12+
endif
13+
ifdef GGML_OPENMP
14+
CMAKE_BUILD_OPTIONS += -DGGML_OPENMP:BOOL=$(GGML_OPENMP)
15+
endif
16+
ifdef GGML_VULKAN
17+
CMAKE_BUILD_OPTIONS += -DGGML_VULKAN:BOOL=$(GGML_VULKAN)
18+
endif
1419

1520

1621
.PHONY: default all cmake proj \

dep/cmake_fetchcontent/llama_cpp.cmake

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,8 @@ FetchContent_Declare(
44
GIT_TAG "d9c6ce46f747189cd6238ca7699253613f77c016"
55
)
66

7-
set(GGML_OPENMP FALSE CACHE BOOL "OpenMP off for compatibility.")
87
FetchContent_MakeAvailable(LlamaCpp)
98

109
set(LlamaCpp_SOURCE_DIR "${llamacpp_SOURCE_DIR}" PARENT_SCOPE)
1110
set(LlamaCpp_INCLUDE_DIRS "${llamacpp_SOURCE_DIR}/include" PARENT_SCOPE)
1211
set(LlamaCpp_LIBRARIES "$<TARGET_NAME:llama>" PARENT_SCOPE)
13-
14-
if(LLAMA_OPENBLAS_ON)
15-
find_package(OpenBLAS REQUIRED)
16-
target_compile_definitions(ggml PRIVATE "GGML_USE_OPENBLAS")
17-
target_include_directories(ggml PRIVATE ${OpenBLAS_INCLUDE_DIRS})
18-
target_link_libraries(ggml PUBLIC ${OpenBLAS_LIBRARIES})
19-
endif()

src/language/inference.cc

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
#include <algorithm>
44
#include <cassert>
55
#include <cstring>
6+
#include <stdexcept>
67
#include <thread>
8+
#include <vector>
79

810
#include <fildesh/fildesh.h>
911
#include <fildesh/ostream.hh>
@@ -122,6 +124,20 @@ rendezllama::make_llama_context(rendezllama::ChatOptions& opt)
122124
if (opt.context_token_limit == 0) {
123125
opt.context_token_limit = opt.model_token_limit;
124126
}
127+
float rope_freq_scale = llama_model_rope_freq_scale_train(model);
128+
if (rope_freq_scale <= 0.0) {
129+
rope_freq_scale = 1.0f;
130+
}
131+
while (
132+
(unsigned)(opt.model_token_limit / rope_freq_scale)
133+
<
134+
opt.context_token_limit)
135+
{
136+
rope_freq_scale /= 2;
137+
}
138+
llama_model_free(model);
139+
model = nullptr;
140+
125141

126142
model_params = llama_model_default_params();
127143
model_params.use_mlock = opt.mlock_on;
@@ -130,14 +146,33 @@ rendezllama::make_llama_context(rendezllama::ChatOptions& opt)
130146
llama_context_params ctx_params = llama_context_default_params();
131147
ctx_params.n_ctx = opt.context_token_limit;
132148
ctx_params.n_batch = opt.batch_count;
133-
ctx_params.rope_freq_scale = llama_model_rope_freq_scale_train(model);
134-
assert(ctx_params.rope_freq_scale > 0.0);
135-
while (
136-
(unsigned)(opt.model_token_limit / ctx_params.rope_freq_scale)
137-
<
138-
opt.context_token_limit)
139-
{
140-
ctx_params.rope_freq_scale /= 2;
149+
ctx_params.rope_freq_scale = rope_freq_scale;
150+
151+
std::vector<float> tensor_split(llama_max_devices());
152+
std::vector<llama_model_tensor_buft_override> tensor_buft_overrides(llama_max_tensor_buft_overrides());
153+
std::vector<size_t> margins(llama_max_devices(), 0);
154+
155+
// Auto-tune parameters if possible (and not manually overridden by user yet).
156+
// This helps avoid OOM crashes on Vulkan/GPU by fitting layers to available memory.
157+
auto status = llama_params_fit(
158+
opt.model_filename.c_str(),
159+
&model_params,
160+
&ctx_params,
161+
tensor_split.data(),
162+
tensor_buft_overrides.data(),
163+
margins.data(),
164+
/*n_ctx_min=*/0,
165+
GGML_LOG_LEVEL_ERROR);
166+
167+
if (status != 0) {
168+
fildesh_log_warning("llama_params_fit failed");
169+
}
170+
171+
model = llama_model_load_from_file(
172+
opt.model_filename.c_str(), model_params);
173+
if (!model) {
174+
fildesh_log_error("Failed to open model.");
175+
return std::make_tuple(nullptr, nullptr);
141176
}
142177

143178
struct llama_context* ctx = llama_init_from_model(model, ctx_params);
@@ -367,14 +402,6 @@ Inference::commit_to_context(
367402
opt.batch_count,
368403
chat_traj.token_count() - chat_traj.context_token_count_);
369404

370-
#if LLAMA_OPENBLAS_ON
371-
if (n < 32) {
372-
llama_set_n_threads(ctx, thread_count_, batch_thread_count_);
373-
}
374-
else {
375-
llama_set_n_threads(ctx, thread_count_, 1);
376-
}
377-
#endif
378405
chat_disp.show_new(chat_traj.context_token_count_ + n, chat_traj, vocabulary_);
379406

380407
if (!batch_.token || (unsigned)batch_.n_tokens < n) {
@@ -445,4 +472,3 @@ Inference::sample_to_trajectory(
445472
llama_sampler_accept(smpl_, chat_traj.token());
446473
token_count_ += 1;
447474
}
448-

0 commit comments

Comments
 (0)