Skip to content

Commit c150e1b

Browse files
committed
Add support for using a different base model
1 parent 57627f0 commit c150e1b

File tree

8 files changed

+148
-33
lines changed

8 files changed

+148
-33
lines changed

examples/common.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
146146
}
147147
params.lora_adapter = argv[i];
148148
params.use_mmap = false;
149+
} else if (arg == "--lora-base") {
150+
if (++i >= argc) {
151+
invalid_param = true;
152+
break;
153+
}
154+
params.lora_base = argv[i];
149155
} else if (arg == "-i" || arg == "--interactive") {
150156
params.interactive = true;
151157
} else if (arg == "--embedding") {
@@ -250,6 +256,7 @@ void gpt_print_usage(int /*argc*/, char ** argv, const gpt_params & params) {
250256
fprintf(stderr, " --mtest compute maximum memory usage\n");
251257
fprintf(stderr, " --verbose-prompt print prompt before generation\n");
252258
fprintf(stderr, " --lora FNAME apply LoRA adapter (implies --no-mmap)\n");
259+
fprintf(stderr, " --lora-base FNAME optional model to use as a base for the layers modified by the LoRA adapter\n");
253260
fprintf(stderr, " -m FNAME, --model FNAME\n");
254261
fprintf(stderr, " model path (default: %s)\n", params.model.c_str());
255262
fprintf(stderr, "\n");

examples/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ struct gpt_params {
3535
std::vector<std::string> antiprompt; // string upon seeing which more user input is prompted
3636

3737
std::string lora_adapter = ""; // lora adapter path
38+
std::string lora_base = ""; // base model path for the lora adapter
3839

3940
bool memory_f16 = true; // use f16 instead of f32 for memory kv
4041
bool random_prompt = false; // do not randomize prompt if none provided

examples/main/main.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ int main(int argc, char ** argv) {
115115
}
116116

117117
if (!params.lora_adapter.empty()) {
118-
int err = llama_apply_lora_from_file(ctx, params.lora_adapter.c_str(), params.n_threads);
118+
int err = llama_apply_lora_from_file(ctx,
119+
params.lora_adapter.c_str(),
120+
params.lora_base.empty() ? NULL : params.lora_base.c_str(),
121+
params.n_threads);
119122
if (err != 0) {
120123
fprintf(stderr, "%s: error: failed to apply lora adapter\n", __func__);
121124
return 1;

examples/perplexity/perplexity.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,10 @@ int main(int argc, char ** argv) {
135135
}
136136

137137
if (!params.lora_adapter.empty()) {
138-
int err = llama_apply_lora_from_file(ctx, params.lora_adapter.c_str(), params.n_threads);
138+
int err = llama_apply_lora_from_file(ctx,
139+
params.lora_adapter.c_str(),
140+
params.lora_base.empty() ? NULL : params.lora_base.c_str(),
141+
params.n_threads);
139142
if (err != 0) {
140143
fprintf(stderr, "%s: error: failed to apply lora adapter\n", __func__);
141144
return 1;

ggml.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5461,6 +5461,27 @@ static void ggml_compute_forward_dup_f16(
54615461
}
54625462
}
54635463
}
5464+
} else if (dst->type == GGML_TYPE_Q4_0 || dst->type == GGML_TYPE_Q4_1) {
5465+
quantize_row_q_t const quantize_row_q = quantize_fns[dst->type].quantize_row_q;
5466+
size_t id = 0;
5467+
uint8_t * dst_ptr = (uint8_t *) dst->data;
5468+
size_t dst_row_size = nb0 * (ne00 / GGML_BLCK_SIZE[dst->type]);
5469+
// todo: use work buffer
5470+
float * src0_f32 = (float *) alloca(ne00 * sizeof(float));
5471+
5472+
for (int i03 = 0; i03 < ne03; i03++) {
5473+
for (int i02 = 0; i02 < ne02; i02++) {
5474+
for (int i01 = 0; i01 < ne01; i01++) {
5475+
const ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
5476+
// convert to f32 and quantize
5477+
for (int i00 = 0; i00 < ne00; i00++) {
5478+
src0_f32[i00] = GGML_FP16_TO_FP32(src0_ptr[i00]);
5479+
}
5480+
quantize_row_q(src0_f32, dst_ptr + id, ne00);
5481+
id += dst_row_size;
5482+
}
5483+
}
5484+
}
54645485
} else {
54655486
GGML_ASSERT(false); // TODO: implement
54665487
}
@@ -5653,6 +5674,21 @@ static void ggml_compute_forward_dup_f32(
56535674
}
56545675
}
56555676
}
5677+
} else if (dst->type == GGML_TYPE_Q4_0 || dst->type == GGML_TYPE_Q4_1) {
5678+
quantize_row_q_t const quantize_row_q = quantize_fns[dst->type].quantize_row_q;
5679+
size_t id = 0;
5680+
uint8_t * dst_ptr = (uint8_t *) dst->data;
5681+
size_t dst_row_size = nb0 * (ne00 / GGML_BLCK_SIZE[dst->type]);
5682+
5683+
for (int i03 = 0; i03 < ne03; i03++) {
5684+
for (int i02 = 0; i02 < ne02; i02++) {
5685+
for (int i01 = 0; i01 < ne01; i01++) {
5686+
const float * src0_ptr = (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
5687+
quantize_row_q(src0_ptr, dst_ptr + id, ne00);
5688+
id += dst_row_size;
5689+
}
5690+
}
5691+
}
56565692
} else {
56575693
GGML_ASSERT(false); // TODO: implement
56585694
}

llama.cpp

Lines changed: 75 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Defines fileno on msys:
22
#ifndef _GNU_SOURCE
33
#define _GNU_SOURCE
4+
#include <cstdint>
5+
#include <cstdio>
46
#endif
57

68
#include "llama_util.h"
@@ -1759,8 +1761,7 @@ int llama_model_quantize(
17591761
}
17601762
}
17611763

1762-
int llama_apply_lora_from_file(struct llama_context * ctx, const char * path_lora, int n_threads) {
1763-
// TODO: refactor all of this after PR #801
1764+
int llama_apply_lora_from_file_internal(struct llama_context * ctx, const char * path_lora, const char * path_base_model, int n_threads) {
17641765
fprintf(stderr, "%s: applying lora adapter from '%s' - please wait ...\n", __func__, path_lora);
17651766

17661767
auto & model = ctx->model;
@@ -1801,13 +1802,13 @@ int llama_apply_lora_from_file(struct llama_context * ctx, const char * path_lor
18011802

18021803
// create a temporary ggml context to store the lora tensors
18031804
// todo: calculate size from biggest possible tensor
1804-
std::vector<uint8_t> buf(1024ull * 1024ull * 1024ull);
1805+
std::vector<uint8_t> lora_buf(1024ull * 1024ull * 1024ull);
18051806
struct ggml_init_params params;
1806-
params.mem_size = buf.size();
1807-
params.mem_buffer = buf.data();
1807+
params.mem_size = lora_buf.size();
1808+
params.mem_buffer = lora_buf.data();
18081809
params.no_alloc = false;
18091810

1810-
ggml_context* lora_ctx = ggml_init(params);
1811+
ggml_context * lora_ctx = ggml_init(params);
18111812
std::unordered_map<std::string, struct ggml_tensor *> lora_tensors;
18121813

18131814
// create a name -> tensor map of the model to accelerate lookups
@@ -1816,6 +1817,32 @@ int llama_apply_lora_from_file(struct llama_context * ctx, const char * path_lor
18161817
model_tensors.insert(kv);
18171818
}
18181819

1820+
1821+
// load base model
1822+
std::unique_ptr<llama_model_loader> model_loader;
1823+
ggml_context * base_ctx = NULL;
1824+
llama_buffer base_buf;
1825+
if (path_base_model) {
1826+
fprintf(stderr, "%s: loading base model from '%s'\n", __func__, path_base_model);
1827+
model_loader.reset(new llama_model_loader(path_base_model, /*use_mmap*/ true, /*vocab_only*/ false));
1828+
1829+
size_t ctx_size, mmapped_size;
1830+
model_loader->calc_sizes(&ctx_size, &mmapped_size);
1831+
base_buf.resize(ctx_size);
1832+
1833+
ggml_init_params base_params;
1834+
base_params.mem_size = base_buf.size;
1835+
base_params.mem_buffer = base_buf.addr;
1836+
base_params.no_alloc = model_loader->use_mmap;
1837+
1838+
base_ctx = ggml_init(base_params);
1839+
1840+
model_loader->ggml_ctx = base_ctx;
1841+
1842+
// maybe this should in llama_model_loader
1843+
model_loader->mapping.reset(new llama_mmap(&model_loader->file_loaders.at(0)->file, false));
1844+
}
1845+
18191846
fprintf(stderr, "%s: ", __func__);
18201847

18211848
// read tensors and apply
@@ -1892,13 +1919,31 @@ int llama_apply_lora_from_file(struct llama_context * ctx, const char * path_lor
18921919
if (lora_tensors.find(base_name + ".loraA") != lora_tensors.end() &&
18931920
lora_tensors.find(base_name + ".loraB") != lora_tensors.end()) {
18941921

1895-
ggml_tensor * tensor = model_tensors[base_name];
1922+
ggml_tensor * dest_t = model_tensors[base_name];
1923+
ggml_tensor * base_t;
1924+
if (model_loader) {
1925+
// load from base model
1926+
if (model_loader->tensors_map.name_to_idx.find(base_name) == model_loader->tensors_map.name_to_idx.end()) {
1927+
fprintf(stderr, "%s: error: tensor '%s' not found in base model\n", __func__, base_name.c_str());
1928+
return 1;
1929+
}
1930+
size_t idx = model_loader->tensors_map.name_to_idx[base_name];
1931+
llama_load_tensor & lt = model_loader->tensors_map.tensors[idx];
1932+
base_t = model_loader->get_tensor(base_name, { (uint32_t)dest_t->ne[0], (uint32_t)dest_t->ne[1] });
1933+
lt.data = (uint8_t *) lt.ggml_tensor->data;
1934+
model_loader->load_data_for(lt);
1935+
lt.ggml_tensor->data = lt.data;
1936+
}
1937+
else {
1938+
base_t = dest_t;
1939+
}
1940+
18961941
ggml_tensor * loraA = lora_tensors[base_name + ".loraA"];
18971942
ggml_tensor * loraB = lora_tensors[base_name + ".loraB"];
18981943

1899-
if (tensor->ne[0] != loraA->ne[1] || tensor->ne[1] != loraB->ne[1]) {
1944+
if (base_t->ne[0] != loraA->ne[1] || base_t->ne[1] != loraB->ne[1]) {
19001945
fprintf(stderr, "%s: incompatible tensor dimensions (%" PRId64 " and %" PRId64 ");"
1901-
" are you sure that this adapter is for this model?\n", __func__, tensor->ne[0], loraA->ne[1]);
1946+
" are you sure that this adapter is for this model?\n", __func__, base_t->ne[0], loraA->ne[1]);
19021947
return 1;
19031948
}
19041949

@@ -1910,14 +1955,14 @@ int llama_apply_lora_from_file(struct llama_context * ctx, const char * path_lor
19101955
BA = ggml_scale(lora_ctx, BA, scale_tensor);
19111956
}
19121957

1913-
//printf("%s: (B)(%d %d %d %d) x (A)(%d %d %d %d) => (BA)(%d %d %d %d) + (T)(%d %d %d %d)\n",
1914-
// base_name.c_str(),
1915-
// (int)loraB->ne[0], (int)loraB->ne[1], (int)loraB->ne[2], (int)loraB->ne[3],
1916-
// (int)loraA->ne[0], (int)loraA->ne[1], (int)loraA->ne[2], (int)loraA->ne[3],
1917-
// (int)BA->ne[0], (int)BA->ne[1], (int)BA->ne[2], (int)BA->ne[3],
1918-
// (int)tensor->ne[0], (int)tensor->ne[1], (int)tensor->ne[2], (int)tensor->ne[3]
1919-
//);
1920-
ggml_tensor * r = ggml_add_inplace(lora_ctx, tensor, BA);
1958+
ggml_tensor * r;
1959+
if (base_t == dest_t) {
1960+
r = ggml_add_inplace(lora_ctx, dest_t, BA);
1961+
}
1962+
else {
1963+
r = ggml_add(lora_ctx, base_t, BA);
1964+
r = ggml_cpy(lora_ctx, r, dest_t);
1965+
}
19211966

19221967
struct ggml_cgraph gf = ggml_build_forward(r);
19231968
gf.n_threads = n_threads;
@@ -1934,14 +1979,27 @@ int llama_apply_lora_from_file(struct llama_context * ctx, const char * path_lor
19341979
}
19351980
}
19361981

1982+
// TODO: this should be in a destructor, it will leak on failure
19371983
ggml_free(lora_ctx);
1984+
if (base_ctx) {
1985+
ggml_free(base_ctx);
1986+
}
19381987

19391988
const int64_t t_lora_us = ggml_time_us() - t_start_lora_us;
19401989
fprintf(stderr, " done (%.2f ms)\n", t_lora_us / 1000.0);
19411990

19421991
return 0;
19431992
}
19441993

1994+
int llama_apply_lora_from_file(struct llama_context * ctx, const char * path_lora, const char * path_base_model, int n_threads) {
1995+
try {
1996+
return llama_apply_lora_from_file_internal(ctx, path_lora, path_base_model, n_threads);
1997+
} catch (const std::string & err) {
1998+
fprintf(stderr, "%s: failed to apply lora adapter: %s\n", __func__, err.c_str());
1999+
return 1;
2000+
}
2001+
}
2002+
19452003
// Returns the KV cache that will contain the context for the
19462004
// ongoing prediction with the model.
19472005
const uint8_t * llama_get_kv_cache(struct llama_context * ctx) {

llama.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,15 @@ extern "C" {
9797
enum llama_ftype ftype);
9898

9999
// Apply a LoRA adapter to a loaded model
100-
// The model needs to be reloaded before applying a new adapter, otherwise
101-
// the adapter will the applied on top of the previous one
100+
// path_base_model is the path to a higher quality model to use as a base for
101+
// the layers modified by the adapter. Can be NULL to use the current loaded model.
102+
// The model needs to be reloaded before applying a new adapter, otherwise the adapter
103+
// will be applied on top of the previous one
102104
// Returns 0 on success
103105
LLAMA_API int llama_apply_lora_from_file(
104106
struct llama_context * ctx,
105107
const char * path_lora,
108+
const char * path_base_model,
106109
int n_threads);
107110

108111
// Returns the KV cache that will contain the context for the

llama_util.h

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ struct llama_mmap {
168168
#ifdef _POSIX_MAPPED_FILES
169169
static constexpr bool SUPPORTED = true;
170170

171-
llama_mmap(struct llama_file * file) {
171+
llama_mmap(struct llama_file * file, bool prefetch = true) {
172172
size = file->size;
173173
int fd = fileno(file->fp);
174174
int flags = MAP_SHARED;
@@ -181,10 +181,12 @@ struct llama_mmap {
181181
throw format("mmap failed: %s", strerror(errno));
182182
}
183183

184-
// Advise the kernel to preload the mapped memory
185-
if (madvise(addr, file->size, MADV_WILLNEED)) {
186-
fprintf(stderr, "warning: madvise(.., MADV_WILLNEED) failed: %s\n",
187-
strerror(errno));
184+
if (prefetch) {
185+
// Advise the kernel to preload the mapped memory
186+
if (madvise(addr, file->size, MADV_WILLNEED)) {
187+
fprintf(stderr, "warning: madvise(.., MADV_WILLNEED) failed: %s\n",
188+
strerror(errno));
189+
}
188190
}
189191
}
190192

@@ -216,13 +218,15 @@ struct llama_mmap {
216218
}
217219

218220
#if _WIN32_WINNT >= _WIN32_WINNT_WIN8
219-
// Advise the kernel to preload the mapped memory
220-
WIN32_MEMORY_RANGE_ENTRY range;
221-
range.VirtualAddress = addr;
222-
range.NumberOfBytes = (SIZE_T)size;
223-
if (!PrefetchVirtualMemory(GetCurrentProcess(), 1, &range, 0)) {
224-
fprintf(stderr, "warning: PrefetchVirtualMemory failed: %s\n",
225-
llama_format_win_err(GetLastError()).c_str());
221+
if (prefetch) {
222+
// Advise the kernel to preload the mapped memory
223+
WIN32_MEMORY_RANGE_ENTRY range;
224+
range.VirtualAddress = addr;
225+
range.NumberOfBytes = (SIZE_T)size;
226+
if (!PrefetchVirtualMemory(GetCurrentProcess(), 1, &range, 0)) {
227+
fprintf(stderr, "warning: PrefetchVirtualMemory failed: %s\n",
228+
llama_format_win_err(GetLastError()).c_str());
229+
}
226230
}
227231
#else
228232
#pragma message("warning: You are building for pre-Windows 8; prefetch not supported")

0 commit comments

Comments
 (0)