Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions llama-cpp-bindings-sys/wrapper_chat_apply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
#include "llama.cpp/common/chat.h"
#include "llama.cpp/common/json.h"
#include "llama.cpp/include/llama.h"
#include "wrapper_report_current_exception.h"
#include "wrapper_utils.h"

#include <cstddef>
#include <exception>
#include <gsl/span>
#include <new>
#include <string>
#include <utility>

Expand Down Expand Up @@ -86,19 +85,11 @@ extern "C" auto llama_rs_apply_chat_template(
}

return LLAMA_RS_APPLY_CHAT_TEMPLATE_OK;
} catch (const std::bad_alloc &) {
return LLAMA_RS_APPLY_CHAT_TEMPLATE_LLAMA_CPP_OUT_OF_MEMORY;
} catch (const std::exception & ex) {
*out_error = llama_rs_dup_string(std::string(ex.what()));
if (*out_error == nullptr) {
return LLAMA_RS_APPLY_CHAT_TEMPLATE_ERROR_STRING_ALLOCATION_FAILED;
}
return LLAMA_RS_APPLY_CHAT_TEMPLATE_LLAMA_CPP_THREW_CXX_EXCEPTION;
} catch (...) {
*out_error = llama_rs_dup_string(std::string("unknown c++ exception"));
if (*out_error == nullptr) {
return LLAMA_RS_APPLY_CHAT_TEMPLATE_ERROR_STRING_ALLOCATION_FAILED;
}
return LLAMA_RS_APPLY_CHAT_TEMPLATE_LLAMA_CPP_THREW_CXX_EXCEPTION;
return llama_rs_report_current_exception(
out_error,
LLAMA_RS_APPLY_CHAT_TEMPLATE_LLAMA_CPP_OUT_OF_MEMORY,
LLAMA_RS_APPLY_CHAT_TEMPLATE_LLAMA_CPP_THREW_CXX_EXCEPTION,
LLAMA_RS_APPLY_CHAT_TEMPLATE_ERROR_STRING_ALLOCATION_FAILED);
}
}
262 changes: 61 additions & 201 deletions llama-cpp-bindings-sys/wrapper_chat_parse.cpp

Large diffs are not rendered by default.

289 changes: 82 additions & 207 deletions llama-cpp-bindings-sys/wrapper_common.cpp

Large diffs are not rendered by default.

64 changes: 61 additions & 3 deletions llama-cpp-bindings-sys/wrapper_context.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
#include "wrapper_context.h"

#include <cstddef>
#include <cstdint>

#include "llama.cpp/include/llama.h"
#include "llama.cpp/src/llama-context.h"
#include "llama.cpp/src/llama-ext.h"
#include "llama.cpp/src/llama-memory.h"
#include "llama.cpp/src/llama-model.h"
#include "wrapper_report_current_exception.h"

extern "C" auto llama_rs_context_decodes_batches_in_one_micro_batch(const struct llama_context * ctx) -> bool {
return llama_get_memory(ctx) == nullptr || !ctx->get_cparams().causal_attn;
}

extern "C" auto llama_rs_context_embedding_row_length(const struct llama_context * ctx) -> size_t {
return ctx->get_model().hparams.n_embd_out();
extern "C" auto llama_rs_context_embeddings_enabled(const struct llama_context * ctx) -> bool {
return ctx->get_cparams().embeddings;
}

extern "C" auto llama_rs_context_embeddings_nextn_enabled(const struct llama_context * ctx) -> bool {
return ctx->get_cparams().embeddings_nextn;
}

extern "C" auto llama_rs_context_embeddings_nextn_masked(const struct llama_context * ctx) -> bool {
return ctx->get_cparams().embeddings_nextn_masked;
}

extern "C" void llama_rs_set_embeddings_nextn(struct llama_context * ctx, bool value, bool masked) {
Expand All @@ -24,6 +33,55 @@ extern "C" auto llama_rs_get_embeddings_nextn_ith(struct llama_context * ctx, in
return llama_get_embeddings_nextn_ith(ctx, token_index);
}

extern "C" auto llama_rs_context_graph_produces_nextn_hidden_state(
struct llama_context * ctx,
bool * out_produces_nextn_hidden_state,
char ** out_error) -> llama_rs_context_nextn_hidden_state_probe_status {
if (out_produces_nextn_hidden_state != nullptr) {
*out_produces_nextn_hidden_state = false;
}
if (out_error != nullptr) {
*out_error = nullptr;
}
if (ctx == nullptr) {
return LLAMA_RS_CONTEXT_NEXTN_HIDDEN_STATE_PROBE_NULL_CTX_ARG;
}
if (out_produces_nextn_hidden_state == nullptr) {
return LLAMA_RS_CONTEXT_NEXTN_HIDDEN_STATE_PROBE_NULL_OUT_PRODUCES_NEXTN_HIDDEN_STATE_ARG;
}
if (out_error == nullptr) {
return LLAMA_RS_CONTEXT_NEXTN_HIDDEN_STATE_PROBE_NULL_OUT_ERROR_ARG;
}
try {
constexpr uint32_t smallest_ubatch_tokens = 1;
constexpr uint32_t smallest_ubatch_sequences = 1;
constexpr uint32_t smallest_ubatch_outputs = 1;
constexpr bool split_graph_without_allocating_buffers = true;

llama_memory_context_ptr memory_context;
llama_memory_t memory = llama_get_memory(ctx);
if (memory != nullptr) {
memory_context = memory->init_full();
}
if (ctx->graph_reserve(
smallest_ubatch_tokens,
smallest_ubatch_sequences,
smallest_ubatch_outputs,
memory_context.get(),
split_graph_without_allocating_buffers) == nullptr) {
return LLAMA_RS_CONTEXT_NEXTN_HIDDEN_STATE_PROBE_LLAMA_CPP_FAILED_TO_RESERVE_GRAPH;
}
*out_produces_nextn_hidden_state = ctx->get_gf_res_reserve()->get_h_nextn() != nullptr;
return LLAMA_RS_CONTEXT_NEXTN_HIDDEN_STATE_PROBE_OK;
} catch (...) {
return llama_rs_report_current_exception(
out_error,
LLAMA_RS_CONTEXT_NEXTN_HIDDEN_STATE_PROBE_LLAMA_CPP_OUT_OF_MEMORY,
LLAMA_RS_CONTEXT_NEXTN_HIDDEN_STATE_PROBE_LLAMA_CPP_THREW_CXX_EXCEPTION,
LLAMA_RS_CONTEXT_NEXTN_HIDDEN_STATE_PROBE_ERROR_STRING_ALLOCATION_FAILED);
}
}

extern "C" auto llama_rs_model_causal_attn(const struct llama_model * model) -> bool {
return model->hparams.causal_attn;
}
23 changes: 21 additions & 2 deletions llama-cpp-bindings-sys/wrapper_context.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#ifdef __cplusplus
Expand All @@ -11,14 +10,34 @@ extern "C" {
struct llama_context;
struct llama_model;

typedef enum llama_rs_context_nextn_hidden_state_probe_status {
LLAMA_RS_CONTEXT_NEXTN_HIDDEN_STATE_PROBE_OK = 0,
LLAMA_RS_CONTEXT_NEXTN_HIDDEN_STATE_PROBE_NULL_CTX_ARG,
LLAMA_RS_CONTEXT_NEXTN_HIDDEN_STATE_PROBE_NULL_OUT_PRODUCES_NEXTN_HIDDEN_STATE_ARG,
LLAMA_RS_CONTEXT_NEXTN_HIDDEN_STATE_PROBE_NULL_OUT_ERROR_ARG,
LLAMA_RS_CONTEXT_NEXTN_HIDDEN_STATE_PROBE_LLAMA_CPP_FAILED_TO_RESERVE_GRAPH,
LLAMA_RS_CONTEXT_NEXTN_HIDDEN_STATE_PROBE_ERROR_STRING_ALLOCATION_FAILED,
LLAMA_RS_CONTEXT_NEXTN_HIDDEN_STATE_PROBE_LLAMA_CPP_OUT_OF_MEMORY,
LLAMA_RS_CONTEXT_NEXTN_HIDDEN_STATE_PROBE_LLAMA_CPP_THREW_CXX_EXCEPTION,
} llama_rs_context_nextn_hidden_state_probe_status;

bool llama_rs_context_decodes_batches_in_one_micro_batch(const struct llama_context * ctx);

size_t llama_rs_context_embedding_row_length(const struct llama_context * ctx);
bool llama_rs_context_embeddings_enabled(const struct llama_context * ctx);

bool llama_rs_context_embeddings_nextn_enabled(const struct llama_context * ctx);

bool llama_rs_context_embeddings_nextn_masked(const struct llama_context * ctx);

void llama_rs_set_embeddings_nextn(struct llama_context * ctx, bool value, bool masked);

float * llama_rs_get_embeddings_nextn_ith(struct llama_context * ctx, int32_t token_index);

llama_rs_context_nextn_hidden_state_probe_status llama_rs_context_graph_produces_nextn_hidden_state(
struct llama_context * ctx,
bool * out_produces_nextn_hidden_state,
char ** out_error);

bool llama_rs_model_causal_attn(const struct llama_model * model);

#ifdef __cplusplus
Expand Down
22 changes: 6 additions & 16 deletions llama-cpp-bindings-sys/wrapper_fit.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#include "wrapper_fit.h"
#include "llama.h"
#include "ggml.h"
#include "wrapper_utils.h"
#include "wrapper_report_current_exception.h"

#include <cstddef>
#include <cstdint>
#include <exception>
#include <new>

#include "llama.cpp/common/fit.h"

Expand Down Expand Up @@ -77,19 +75,11 @@ extern "C" auto llama_rs_fit_params(
}
*out_unrecognized_status_code = static_cast<int32_t>(status);
return LLAMA_RS_FIT_PARAMS_LLAMA_CPP_RETURNED_UNRECOGNIZED_STATUS_CODE;
} catch (const std::bad_alloc &) {
return LLAMA_RS_FIT_PARAMS_LLAMA_CPP_OUT_OF_MEMORY;
} catch (const std::exception & err) {
*out_error = llama_rs_dup_string(err.what());
if (*out_error == nullptr) {
return LLAMA_RS_FIT_PARAMS_ERROR_STRING_ALLOCATION_FAILED;
}
return LLAMA_RS_FIT_PARAMS_LLAMA_CPP_THREW_CXX_EXCEPTION;
} catch (...) {
*out_error = llama_rs_dup_string("unknown c++ exception");
if (*out_error == nullptr) {
return LLAMA_RS_FIT_PARAMS_ERROR_STRING_ALLOCATION_FAILED;
}
return LLAMA_RS_FIT_PARAMS_LLAMA_CPP_THREW_CXX_EXCEPTION;
return llama_rs_report_current_exception(
out_error,
LLAMA_RS_FIT_PARAMS_LLAMA_CPP_OUT_OF_MEMORY,
LLAMA_RS_FIT_PARAMS_LLAMA_CPP_THREW_CXX_EXCEPTION,
LLAMA_RS_FIT_PARAMS_ERROR_STRING_ALLOCATION_FAILED);
}
}
22 changes: 6 additions & 16 deletions llama-cpp-bindings-sys/wrapper_gbnf.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#include "wrapper_gbnf.h"
#include "wrapper_utils.h"
#include "wrapper_report_current_exception.h"

#include "llama.cpp/src/llama-grammar.h"

#include <exception>
#include <new>

extern "C" auto llama_rs_validate_gbnf(
const struct llama_vocab * vocab,
Expand Down Expand Up @@ -48,19 +46,11 @@ extern "C" auto llama_rs_validate_gbnf(
llama_grammar_free_impl(grammar);

return LLAMA_RS_GBNF_VALIDATION_OK;
} catch (const std::bad_alloc &) {
return LLAMA_RS_GBNF_VALIDATION_LLAMA_CPP_OUT_OF_MEMORY;
} catch (const std::exception & err) {
*out_error = llama_rs_dup_string(err.what());
if (*out_error == nullptr) {
return LLAMA_RS_GBNF_VALIDATION_ERROR_STRING_ALLOCATION_FAILED;
}
return LLAMA_RS_GBNF_VALIDATION_THREW_CXX_EXCEPTION;
} catch (...) {
*out_error = llama_rs_dup_string("unknown c++ exception");
if (*out_error == nullptr) {
return LLAMA_RS_GBNF_VALIDATION_ERROR_STRING_ALLOCATION_FAILED;
}
return LLAMA_RS_GBNF_VALIDATION_THREW_CXX_EXCEPTION;
return llama_rs_report_current_exception(
out_error,
LLAMA_RS_GBNF_VALIDATION_LLAMA_CPP_OUT_OF_MEMORY,
LLAMA_RS_GBNF_VALIDATION_THREW_CXX_EXCEPTION,
LLAMA_RS_GBNF_VALIDATION_ERROR_STRING_ALLOCATION_FAILED);
}
}
Loading
Loading