Skip to content

Commit 28751d1

Browse files
committed
[aux] Memory example (embedding)
Adapt embedding example to showcase how to load from memory. Can be configured through environment variables.
1 parent 1f820b0 commit 28751d1

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

examples/embedding/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
set(TARGET llama-embedding)
22
add_executable(${TARGET} embedding.cpp)
33
install(TARGETS ${TARGET} RUNTIME)
4-
target_link_libraries(${TARGET} PRIVATE common llama ${CMAKE_THREAD_LIBS_INIT})
4+
target_link_libraries(${TARGET} PRIVATE common llama llama-common-test ${CMAKE_THREAD_LIBS_INIT})
55
target_compile_features(${TARGET} PRIVATE cxx_std_17)

examples/embedding/embedding.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1+
#include <algorithm>
2+
#include <chrono>
3+
#include <cstdint>
4+
#include <cstdlib>
5+
#include <ctime>
6+
#include <fstream>
7+
#include <thread>
8+
#include <vector>
9+
110
#include "arg.h"
211
#include "common.h"
12+
#include "llama-cpp.h"
313
#include "log.h"
4-
#include "llama.h"
5-
6-
#include <ctime>
7-
#include <algorithm>
814

915
#if defined(_MSC_VER)
1016
#pragma warning(disable: 4244 4267) // possible loss of data
1117
#endif
1218

19+
#include "load_into_memory.h"
20+
1321
static std::vector<std::string> split_lines(const std::string & s, const std::string & separator = "\n") {
1422
std::vector<std::string> lines;
1523
size_t start = 0;
@@ -94,7 +102,15 @@ int main(int argc, char ** argv) {
94102
llama_numa_init(params.numa);
95103

96104
// load the model
97-
common_init_result llama_init = common_init_from_params(params);
105+
common_init_result llama_init;
106+
if (memory_configuration_env_is_set()) {
107+
llama_model_params mparams = common_model_params_to_llama(params);
108+
common_init_result iparams;
109+
llama_model * model = load_model_from_memory_configuration(params.model.path.c_str(), mparams);
110+
llama_init = common_init_from_model_and_params(model, std::move(iparams), params);
111+
} else {
112+
llama_init = common_init_from_params(params);
113+
}
98114

99115
llama_model * model = llama_init.model.get();
100116
llama_context * ctx = llama_init.context.get();

0 commit comments

Comments
 (0)