llama-cpp-bindings-sys 0.34.0

Low level bindings to llama.cpp
Documentation
#include "wrapper_common.h"
#include "wrapper_fault_injection.h"

#include <cstdlib>
#include <cstdint>
#include <cstring>
#include <memory>
#include <regex>
#include <stdexcept>
#include <string>

#include <common/json-schema-to-grammar.h>
#include <common/json.h>
#include <ggml-backend.h>
#include <llama.h>
#include "wrapper_report_current_exception.h"
#include "wrapper_utils.h"

extern "C" auto llama_rs_json_schema_to_grammar(
    const char * schema_json,
    char ** out_grammar,
    char ** out_error) -> llama_rs_json_schema_to_grammar_status {
    *out_grammar = nullptr;
    *out_error = nullptr;

    try {
        const auto schema = common_json::parse(schema_json);
        const auto grammar = json_schema_to_grammar(schema);
        *out_grammar = llama_rs_dup_string(grammar);
        if (*out_grammar == nullptr) {
            return LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_ERROR_STRING_ALLOCATION_FAILED;
        }
        return LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_OK;
    } catch (const common_json_error & err) {
        return llama_rs_report_exception_message(
            out_error,
            err.what(),
            LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_INVALID_JSON,
            LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_ERROR_STRING_ALLOCATION_FAILED);
    } catch (const std::invalid_argument & err) {
        return llama_rs_report_exception_message(
            out_error,
            err.what(),
            LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_INVALID_SCHEMA,
            LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_ERROR_STRING_ALLOCATION_FAILED);
    } catch (...) {
        return llama_rs_report_current_exception(
            out_error,
            LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_LLAMA_CPP_OUT_OF_MEMORY,
            LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_LLAMA_CPP_THREW_CXX_EXCEPTION,
            LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_ERROR_STRING_ALLOCATION_FAILED);
    }
}

extern "C" auto llama_rs_string_dup(const char * value) -> char * {
    return llama_rs_dup_string(value);
}

extern "C" void llama_rs_string_free(char * ptr) {
    const std::unique_ptr<char[]> reclaimed(ptr);
}

extern "C" auto llama_rs_sampler_init_grammar(
    const struct llama_vocab * vocab,
    const char * grammar_str,
    const char * grammar_root,
    struct llama_sampler ** out_sampler,
    char ** out_error) -> llama_rs_sampler_init_grammar_status {
    *out_sampler = nullptr;
    *out_error = nullptr;
    try {
        llama_rs_throw_if_fault_injected(LLAMA_RS_FAULT_POINT_SAMPLER_INIT_GRAMMAR_THROWS);
        *out_sampler = llama_rs_fault_injected(LLAMA_RS_FAULT_POINT_SAMPLER_INIT_GRAMMAR_RETURNS_NULL)
                           ? nullptr
                           : llama_sampler_init_grammar(vocab, grammar_str, grammar_root);
        if (*out_sampler == nullptr) {
            return LLAMA_RS_SAMPLER_INIT_GRAMMAR_LLAMA_CPP_RETURNED_NULL;
        }
        return LLAMA_RS_SAMPLER_INIT_GRAMMAR_OK;
    } catch (...) {
        return llama_rs_report_current_exception(
            out_error,
            LLAMA_RS_SAMPLER_INIT_GRAMMAR_LLAMA_CPP_OUT_OF_MEMORY,
            LLAMA_RS_SAMPLER_INIT_GRAMMAR_LLAMA_CPP_THREW_CXX_EXCEPTION,
            LLAMA_RS_SAMPLER_INIT_GRAMMAR_ERROR_STRING_ALLOCATION_FAILED);
    }
}

extern "C" auto llama_rs_sampler_init_grammar_lazy_patterns(
    const struct llama_vocab * vocab,
    const char * grammar_str,
    const char * grammar_root,
    const char ** trigger_patterns,
    size_t num_trigger_patterns,
    const llama_token * trigger_tokens,
    size_t num_trigger_tokens,
    struct llama_sampler ** out_sampler,
    char ** out_error) -> llama_rs_sampler_init_grammar_lazy_patterns_status {
    *out_sampler = nullptr;
    *out_error = nullptr;
    try {
        llama_rs_throw_if_fault_injected(LLAMA_RS_FAULT_POINT_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_THROWS);
        *out_sampler = llama_sampler_init_grammar_lazy_patterns(
            vocab,
            grammar_str,
            grammar_root,
            trigger_patterns,
            num_trigger_patterns,
            trigger_tokens,
            num_trigger_tokens);
        if (*out_sampler == nullptr) {
            return LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_LLAMA_CPP_RETURNED_NULL;
        }
        return LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_OK;
    } catch (const std::regex_error & err) {
        return llama_rs_report_exception_message(
            out_error,
            err.what(),
            LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_INVALID_TRIGGER_PATTERN,
            LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_ERROR_STRING_ALLOCATION_FAILED);
    } catch (...) {
        return llama_rs_report_current_exception(
            out_error,
            LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_LLAMA_CPP_OUT_OF_MEMORY,
            LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_LLAMA_CPP_THREW_CXX_EXCEPTION,
            LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_ERROR_STRING_ALLOCATION_FAILED);
    }
}

extern "C" auto llama_rs_memory_seq_pos_max(
    llama_memory_t mem,
    llama_seq_id seq_id,
    llama_pos * out_position,
    char ** out_error) -> llama_rs_memory_seq_pos_max_status {
    *out_position = -1;
    *out_error = nullptr;
    try {
        llama_rs_throw_if_fault_injected(LLAMA_RS_FAULT_POINT_MEMORY_SEQ_POS_MAX_THROWS);

        *out_position = llama_memory_seq_pos_max(mem, seq_id);
        return LLAMA_RS_MEMORY_SEQ_POS_MAX_OK;
    } catch (...) {
        return llama_rs_report_current_exception(
            out_error,
            LLAMA_RS_MEMORY_SEQ_POS_MAX_LLAMA_CPP_OUT_OF_MEMORY,
            LLAMA_RS_MEMORY_SEQ_POS_MAX_LLAMA_CPP_THREW_CXX_EXCEPTION,
            LLAMA_RS_MEMORY_SEQ_POS_MAX_ERROR_STRING_ALLOCATION_FAILED);
    }
}

extern "C" auto llama_rs_memory_seq_add(
    llama_memory_t mem,
    llama_seq_id seq_id,
    llama_pos pos_start,
    llama_pos pos_end,
    llama_pos shift,
    char ** out_error) -> llama_rs_memory_seq_add_status {
    *out_error = nullptr;
    try {
        llama_memory_seq_add(mem, seq_id, pos_start, pos_end, shift);
        return LLAMA_RS_MEMORY_SEQ_ADD_OK;
    } catch (...) {
        return llama_rs_report_current_exception(
            out_error,
            LLAMA_RS_MEMORY_SEQ_ADD_LLAMA_CPP_OUT_OF_MEMORY,
            LLAMA_RS_MEMORY_SEQ_ADD_LLAMA_CPP_THREW_CXX_EXCEPTION,
            LLAMA_RS_MEMORY_SEQ_ADD_ERROR_STRING_ALLOCATION_FAILED);
    }
}

extern "C" auto llama_rs_memory_seq_div(
    llama_memory_t mem,
    llama_seq_id seq_id,
    llama_pos pos_start,
    llama_pos pos_end,
    int divisor,
    char ** out_error) -> llama_rs_memory_seq_div_status {
    *out_error = nullptr;
    try {
        llama_memory_seq_div(mem, seq_id, pos_start, pos_end, divisor);
        return LLAMA_RS_MEMORY_SEQ_DIV_OK;
    } catch (...) {
        return llama_rs_report_current_exception(
            out_error,
            LLAMA_RS_MEMORY_SEQ_DIV_LLAMA_CPP_OUT_OF_MEMORY,
            LLAMA_RS_MEMORY_SEQ_DIV_LLAMA_CPP_THREW_CXX_EXCEPTION,
            LLAMA_RS_MEMORY_SEQ_DIV_ERROR_STRING_ALLOCATION_FAILED);
    }
}

extern "C" auto llama_rs_sampler_sample(
    struct llama_sampler * sampler,
    struct llama_context * ctx,
    int32_t idx,
    llama_token * out_token,
    char ** out_error) -> llama_rs_sampler_sample_status {
    *out_error = nullptr;
    try {
        *out_token = llama_sampler_sample(sampler, ctx, idx);
        return LLAMA_RS_SAMPLER_SAMPLE_OK;
    } catch (...) {
        return llama_rs_report_current_exception(
            out_error,
            LLAMA_RS_SAMPLER_SAMPLE_LLAMA_CPP_OUT_OF_MEMORY,
            LLAMA_RS_SAMPLER_SAMPLE_LLAMA_CPP_THREW_CXX_EXCEPTION,
            LLAMA_RS_SAMPLER_SAMPLE_ERROR_STRING_ALLOCATION_FAILED);
    }
}

extern "C" auto llama_rs_sampler_accept(
    struct llama_sampler * sampler,
    llama_token token,
    char ** out_error) -> llama_rs_sampler_accept_status {
    *out_error = nullptr;
    try {
        llama_rs_throw_if_fault_injected(LLAMA_RS_FAULT_POINT_SAMPLER_ACCEPT_THROWS);
        llama_sampler_accept(sampler, token);
        return LLAMA_RS_SAMPLER_ACCEPT_OK;
    } catch (...) {
        return llama_rs_report_current_exception(
            out_error,
            LLAMA_RS_SAMPLER_ACCEPT_LLAMA_CPP_OUT_OF_MEMORY,
            LLAMA_RS_SAMPLER_ACCEPT_LLAMA_CPP_THREW_CXX_EXCEPTION,
            LLAMA_RS_SAMPLER_ACCEPT_ERROR_STRING_ALLOCATION_FAILED);
    }
}

extern "C" auto llama_rs_load_model_from_file(
    const char * path,
    struct llama_model_params params,
    struct llama_model ** out_model,
    char ** out_error) -> llama_rs_load_model_from_file_status {
    *out_model = nullptr;
    *out_error = nullptr;
    try {
        *out_model = llama_model_load_from_file(path, params);
        if (*out_model == nullptr) {
            return LLAMA_RS_LOAD_MODEL_FROM_FILE_LLAMA_CPP_RETURNED_NULL;
        }
        return LLAMA_RS_LOAD_MODEL_FROM_FILE_OK;
    } catch (...) {
        return llama_rs_report_current_exception(
            out_error,
            LLAMA_RS_LOAD_MODEL_FROM_FILE_LLAMA_CPP_OUT_OF_MEMORY,
            LLAMA_RS_LOAD_MODEL_FROM_FILE_LLAMA_CPP_THREW_CXX_EXCEPTION,
            LLAMA_RS_LOAD_MODEL_FROM_FILE_ERROR_STRING_ALLOCATION_FAILED);
    }
}

extern "C" auto llama_rs_adapter_lora_init(
    struct llama_model * model,
    const char * path_lora,
    struct llama_adapter_lora ** out_adapter,
    char ** out_error) -> llama_rs_adapter_lora_init_status {
    *out_adapter = nullptr;
    *out_error = nullptr;
    try {
        llama_rs_throw_if_fault_injected(LLAMA_RS_FAULT_POINT_ADAPTER_LORA_INIT_THROWS);
        *out_adapter = llama_adapter_lora_init(model, path_lora);
        if (*out_adapter == nullptr) {
            return LLAMA_RS_ADAPTER_LORA_INIT_LLAMA_CPP_RETURNED_NULL;
        }
        return LLAMA_RS_ADAPTER_LORA_INIT_OK;
    } catch (...) {
        return llama_rs_report_current_exception(
            out_error,
            LLAMA_RS_ADAPTER_LORA_INIT_LLAMA_CPP_OUT_OF_MEMORY,
            LLAMA_RS_ADAPTER_LORA_INIT_LLAMA_CPP_THREW_CXX_EXCEPTION,
            LLAMA_RS_ADAPTER_LORA_INIT_ERROR_STRING_ALLOCATION_FAILED);
    }
}

extern "C" auto llama_rs_new_context_with_model(
    struct llama_model * model,
    struct llama_context_params params,
    struct llama_context ** out_ctx,
    char ** out_error) -> llama_rs_new_context_with_model_status {
    *out_ctx = nullptr;
    *out_error = nullptr;
    try {
        *out_ctx = llama_init_from_model(model, params);
        if (*out_ctx == nullptr) {
            return LLAMA_RS_NEW_CONTEXT_WITH_MODEL_LLAMA_CPP_RETURNED_NULL;
        }
        return LLAMA_RS_NEW_CONTEXT_WITH_MODEL_OK;
    } catch (...) {
        return llama_rs_report_current_exception(
            out_error,
            LLAMA_RS_NEW_CONTEXT_WITH_MODEL_LLAMA_CPP_OUT_OF_MEMORY,
            LLAMA_RS_NEW_CONTEXT_WITH_MODEL_LLAMA_CPP_THREW_CXX_EXCEPTION,
            LLAMA_RS_NEW_CONTEXT_WITH_MODEL_ERROR_STRING_ALLOCATION_FAILED);
    }
}

extern "C" auto llama_rs_process(
    struct llama_context * ctx,
    enum llama_process_type process_type,
    struct llama_batch_ext * batch,
    int32_t * out_llama_cpp_return_code,
    char ** out_error) -> llama_rs_process_status {
    *out_error = nullptr;
    *out_llama_cpp_return_code = 0;
    try {
        *out_llama_cpp_return_code = llama_process(ctx, process_type, batch);
        return LLAMA_RS_PROCESS_OK;
    } catch (...) {
        return llama_rs_report_current_exception(
            out_error,
            LLAMA_RS_PROCESS_LLAMA_CPP_OUT_OF_MEMORY,
            LLAMA_RS_PROCESS_LLAMA_CPP_THREW_CXX_EXCEPTION,
            LLAMA_RS_PROCESS_ERROR_STRING_ALLOCATION_FAILED);
    }
}

extern "C" auto llama_rs_tokenize(
    const struct llama_vocab * vocab,
    const char * text,
    int32_t text_len,
    llama_token * tokens,
    int32_t n_tokens_max,
    bool add_special,
    bool parse_special,
    int32_t * out_returned_count,
    char ** out_error) -> llama_rs_tokenize_status {
    *out_error = nullptr;
    *out_returned_count = 0;
    try {
        llama_rs_throw_if_fault_injected(LLAMA_RS_FAULT_POINT_TOKENIZE_THROWS);

        int32_t const count = llama_tokenize(
            vocab, text, text_len, tokens, n_tokens_max, add_special, parse_special);
        *out_returned_count = count;
        return LLAMA_RS_TOKENIZE_OK;
    } catch (...) {
        return llama_rs_report_current_exception(
            out_error,
            LLAMA_RS_TOKENIZE_LLAMA_CPP_OUT_OF_MEMORY,
            LLAMA_RS_TOKENIZE_LLAMA_CPP_THREW_CXX_EXCEPTION,
            LLAMA_RS_TOKENIZE_ERROR_STRING_ALLOCATION_FAILED);
    }
}

extern "C" auto llama_rs_sampler_apply(
    struct llama_sampler * sampler,
    struct llama_token_data_array * data_array,
    char ** out_error) -> llama_rs_sampler_apply_status {
    *out_error = nullptr;
    try {
        llama_rs_throw_if_fault_injected(LLAMA_RS_FAULT_POINT_SAMPLER_APPLY_THROWS);
        llama_sampler_apply(sampler, data_array);
        return LLAMA_RS_SAMPLER_APPLY_OK;
    } catch (...) {
        return llama_rs_report_current_exception(
            out_error,
            LLAMA_RS_SAMPLER_APPLY_LLAMA_CPP_OUT_OF_MEMORY,
            LLAMA_RS_SAMPLER_APPLY_LLAMA_CPP_THREW_CXX_EXCEPTION,
            LLAMA_RS_SAMPLER_APPLY_ERROR_STRING_ALLOCATION_FAILED);
    }
}

extern "C" auto llama_rs_token_to_piece(
    const struct llama_vocab * vocab,
    llama_token token,
    char * buf,
    int32_t length,
    int32_t lstrip,
    bool special) -> int32_t {
    if (llama_rs_fault_injected(LLAMA_RS_FAULT_POINT_TOKEN_TO_PIECE_FAILS) ||
        (special && llama_rs_fault_injected(LLAMA_RS_FAULT_POINT_SPECIAL_TOKEN_TO_PIECE_FAILS))) {
        return -(length + 1);
    }

    return llama_token_to_piece(vocab, token, buf, length, lstrip, special);
}

extern "C" auto llama_rs_backend_device_text(const char * text) -> const char * {
    if (llama_rs_fault_injected(LLAMA_RS_FAULT_POINT_BACKEND_DEVICE_TEXT_NULL)) {
        return nullptr;
    }

    return text;
}

extern "C" auto llama_rs_backend_device_type(ggml_backend_dev_t dev) -> uint32_t {
    if (llama_rs_fault_injected(LLAMA_RS_FAULT_POINT_BACKEND_DEVICE_TYPE_UNKNOWN)) {
        return GGML_BACKEND_DEVICE_TYPE_META + 1;
    }

    return ggml_backend_dev_type(dev);
}