Skip to content

Commit 3f96181

Browse files
authored
Merge b3583
b3583
2 parents 8408090 + 98a532d commit 3f96181

File tree

15 files changed

+57
-49
lines changed

15 files changed

+57
-49
lines changed

examples/export-lora/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ For example:
1717

1818
```bash
1919
./bin/llama-export-lora \
20-
-m open-llama-3b-v2-q8_0.gguf \
21-
-o open-llama-3b-v2-q8_0-english2tokipona-chat.gguf \
22-
--lora lora-open-llama-3b-v2-q8_0-english2tokipona-chat-LATEST.gguf
20+
-m open-llama-3b-v2.gguf \
21+
-o open-llama-3b-v2-english2tokipona-chat.gguf \
22+
--lora lora-open-llama-3b-v2-english2tokipona-chat-LATEST.gguf
2323
```
2424

2525
Multiple LORA adapters can be applied by passing multiple `--lora FNAME` or `--lora-scaled FNAME S` command line parameters:

examples/export-lora/export-lora.cpp

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010

1111
static bool g_verbose = false;
1212

13+
struct tensor_transformation {
14+
struct ggml_tensor * in;
15+
struct ggml_tensor * out;
16+
bool is_copy;
17+
};
18+
1319
static std::string get_kv_str(struct gguf_context * ctx_gguf, const std::string & key){
1420
int id = gguf_find_key(ctx_gguf, key.c_str());
1521
return id < 0 ? "" : std::string(gguf_get_val_str(ctx_gguf, id));
@@ -198,8 +204,7 @@ struct lora_merge_ctx {
198204
}
199205

200206
// mapping base tensor to out tensor (same shape with base, but different type)
201-
// if out_tensor == nullptr, we only copy it
202-
std::vector<std::pair<struct ggml_tensor *, struct ggml_tensor *>> base_to_out_tensors;
207+
std::vector<tensor_transformation> trans;
203208
for (auto & it : base_model.tensors) {
204209
bool t_a = true;
205210
bool t_b = true;
@@ -212,14 +217,22 @@ struct lora_merge_ctx {
212217
// only copy
213218
struct ggml_tensor * cpy_tensor = ggml_dup_tensor(ctx_out_ggml, base_tensor);
214219
ggml_set_name(cpy_tensor, base_tensor->name);
215-
base_to_out_tensors.push_back(std::make_pair(cpy_tensor, nullptr));
220+
trans.push_back({
221+
cpy_tensor,
222+
cpy_tensor,
223+
true,
224+
});
216225
gguf_add_tensor(ctx_out, cpy_tensor);
217226
} else if (t_a && t_b) {
218227
// need merging
219228
struct ggml_tensor * out_tensor = ggml_new_tensor(
220229
ctx_out_ggml, get_out_tensor_type(base_tensor), GGML_MAX_DIMS, base_tensor->ne);
221230
ggml_set_name(out_tensor, base_tensor->name);
222-
base_to_out_tensors.push_back(std::make_pair(base_tensor, out_tensor));
231+
trans.push_back({
232+
base_tensor,
233+
out_tensor,
234+
false,
235+
});
223236
gguf_add_tensor(ctx_out, out_tensor);
224237
} else {
225238
throw std::runtime_error("tensor " + it.first + " missing either lora_a or lora_b");
@@ -234,12 +247,12 @@ struct lora_merge_ctx {
234247

235248
// process base model tensors
236249
size_t n_merged = 0;
237-
for (auto & it : base_to_out_tensors) {
238-
if (it.second != nullptr) {
239-
merge_tensor(it.first, it.second);
250+
for (auto & it : trans) {
251+
if (!it.is_copy) {
252+
merge_tensor(it.in, it.out);
240253
n_merged++;
241254
} else {
242-
copy_tensor(it.first);
255+
copy_tensor(it.in);
243256
}
244257
}
245258

@@ -252,7 +265,7 @@ struct lora_merge_ctx {
252265
}
253266

254267
printf("%s : merged %ld tensors with lora adapters\n", __func__, n_merged);
255-
printf("%s : wrote %ld tensors to output file\n", __func__, base_to_out_tensors.size());
268+
printf("%s : wrote %ld tensors to output file\n", __func__, trans.size());
256269
}
257270

258271
void copy_tensor(struct ggml_tensor * base) {
@@ -285,6 +298,10 @@ struct lora_merge_ctx {
285298
for (size_t i = 0; i < adapters.size(); ++i) {
286299
auto t_a = adapters[i]->get_tensor(name_lora_a);
287300
auto t_b = adapters[i]->get_tensor(name_lora_b);
301+
// TODO: add support for quantized lora
302+
if (ggml_is_quantized(t_a->type) || ggml_is_quantized(t_b->type)) {
303+
throw std::runtime_error("quantized LoRA adapters is not supported, please retry with f16 or f32");
304+
}
288305
inp_a[i] = ggml_dup_tensor(ctx, t_a);
289306
inp_b[i] = ggml_dup_tensor(ctx, t_b);
290307
}

examples/server/server.cpp

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -754,13 +754,13 @@ struct server_context {
754754
default_generation_settings_for_props = get_formated_generation(slots.front());
755755
default_generation_settings_for_props["seed"] = -1;
756756

757-
// the update_slots() logic will always submit a maximum of n_batch tokens
757+
// the update_slots() logic will always submit a maximum of n_batch or n_parallel tokens
758758
// note that n_batch can be > n_ctx (e.g. for non-causal attention models such as BERT where the KV cache is not used)
759759
{
760760
const int32_t n_batch = llama_n_batch(ctx);
761761

762762
// only a single seq_id per token is needed
763-
batch = llama_batch_init(n_batch, 0, 1);
763+
batch = llama_batch_init(std::max(n_batch, params.n_parallel), 0, 1);
764764
}
765765

766766
metrics.init();
@@ -1137,28 +1137,19 @@ struct server_context {
11371137
if (!system_prompt.empty()) {
11381138
system_tokens = ::llama_tokenize(ctx, system_prompt, true);
11391139

1140-
llama_batch_clear(batch);
1140+
const int32_t n_batch = llama_n_batch(ctx);
1141+
const int32_t n_tokens_prompt = system_tokens.size();
11411142

1142-
for (int i = 0; i < (int)system_tokens.size(); ++i) {
1143-
llama_batch_add(batch, system_tokens[i], i, { 0 }, false);
1144-
}
1143+
for (int32_t i = 0; i < n_tokens_prompt; i += n_batch) {
1144+
const int32_t n_tokens = std::min(n_batch, n_tokens_prompt - i);
11451145

1146-
const int32_t n_batch = llama_n_batch(ctx);
1146+
llama_batch_clear(batch);
11471147

1148-
for (int32_t i = 0; i < batch.n_tokens; i += n_batch) {
1149-
const int32_t n_tokens = std::min(params.n_batch, batch.n_tokens - i);
1150-
llama_batch batch_view = {
1151-
n_tokens,
1152-
batch.token + i,
1153-
nullptr,
1154-
batch.pos + i,
1155-
batch.n_seq_id + i,
1156-
batch.seq_id + i,
1157-
batch.logits + i,
1158-
0, 0, 0, // unused
1159-
};
1148+
for (int32_t j = 0; j < n_tokens; ++j) {
1149+
llama_batch_add(batch, system_tokens[i + j], i + j, { 0 }, false);
1150+
}
11601151

1161-
if (llama_decode(ctx, batch_view) != 0) {
1152+
if (llama_decode(ctx, batch) != 0) {
11621153
LOG_ERROR("llama_decode() failed", {});
11631154
return;
11641155
}

ggml/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ option(GGML_CUDA_NO_VMM "ggml: do not try to use CUDA VMM"
129129
option(GGML_CUDA_FA_ALL_QUANTS "ggml: compile all quants for FlashAttention" OFF)
130130
option(GGML_CUDA_USE_GRAPHS "ggml: use CUDA graphs (llama.cpp only)" OFF)
131131

132-
option(GGML_CURL "ggml: use libcurl to download model from an URL" OFF)
133132
option(GGML_HIPBLAS "ggml: use hipBLAS" OFF)
134133
option(GGML_HIP_UMA "ggml: use HIP unified memory architecture" OFF)
135134
option(GGML_VULKAN "ggml: use Vulkan" OFF)

ggml/include/ggml.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@
244244
#define GGML_EXIT_SUCCESS 0
245245
#define GGML_EXIT_ABORTED 1
246246

247+
#define GGML_ROPE_TYPE_NEOX 2
248+
247249
#define GGUF_MAGIC "GGUF"
248250

249251
#define GGUF_VERSION 3
@@ -1453,8 +1455,8 @@ extern "C" {
14531455
struct ggml_tensor * b);
14541456

14551457
// rotary position embedding
1456-
// if mode & 1 == 1, skip n_past elements (NOT SUPPORTED)
1457-
// if mode & 2 == 1, GPT-NeoX style
1458+
// if (mode & 1) - skip n_past elements (NOT SUPPORTED)
1459+
// if (mode & GGML_ROPE_TYPE_NEOX) - GPT-NeoX style
14581460
//
14591461
// b is an int32 vector with size a->ne[2], it contains the positions
14601462
GGML_API struct ggml_tensor * ggml_rope(

ggml/src/ggml-cann/aclnn_ops.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2881,7 +2881,7 @@ void ggml_cann_rope(ggml_backend_cann_context& ctx, ggml_tensor* dst) {
28812881
ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast,
28822882
beta_slow, corr_dims);
28832883

2884-
const bool is_neox = mode & 2;
2884+
const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
28852885

28862886
// init cos/sin cache
28872887
ggml_cann_pool_alloc sin_allocator(

ggml/src/ggml-cuda/rope.cu

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ void ggml_cuda_op_rope(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
226226
memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
227227
memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
228228

229-
const bool is_neox = mode & 2;
229+
const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
230230

231231
const int32_t * pos = (const int32_t *) src1_d;
232232

ggml/src/ggml-metal.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2313,7 +2313,7 @@ static enum ggml_status ggml_metal_graph_compute(
23132313
memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
23142314
memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
23152315

2316-
const bool is_neox = mode & 2;
2316+
const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
23172317

23182318
id<MTLComputePipelineState> pipeline = nil;
23192319

ggml/src/ggml-sycl/rope.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ void ggml_sycl_op_rope(
226226
memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
227227
memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
228228

229-
const bool is_neox = mode & 2;
229+
const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
230230

231231
const int32_t * pos = (const int32_t *) src1_dd;
232232

ggml/src/ggml-vulkan.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4053,7 +4053,7 @@ static vk_pipeline ggml_vk_op_get_pipeline(ggml_backend_vk_context * ctx, const
40534053
case GGML_OP_ROPE:
40544054
{
40554055
const int mode = ((const int32_t *) dst->op_params)[2];
4056-
const bool is_neox = mode & 2;
4056+
const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
40574057

40584058
if (is_neox) {
40594059
if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {

0 commit comments

Comments
 (0)