diff --git a/Cargo.lock b/Cargo.lock index 5bbe679d6..e93573ccc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1173,6 +1173,7 @@ dependencies = [ "llama-cpp-ffi-status", "llama-cpp-gbnf", "llama-cpp-log-decoder", + "llama-cpp-wrapper-error-fixture", "llguidance", "log", "nom 8.0.0", @@ -1234,6 +1235,7 @@ name = "llama-cpp-ffi-status" version = "0.13.0" dependencies = [ "llama-cpp-bindings-sys", + "llama-cpp-wrapper-error-fixture", "thiserror", ] @@ -1272,6 +1274,13 @@ dependencies = [ "syn", ] +[[package]] +name = "llama-cpp-wrapper-error-fixture" +version = "0.13.0" +dependencies = [ + "llama-cpp-bindings-sys", +] + [[package]] name = "llama-cpp-wrapper-sources" version = "0.13.0" diff --git a/Cargo.toml b/Cargo.toml index c2003ebf5..bbc576aa6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ members = [ "llama-cpp-log-decoder", "llama-cpp-test-harness", "llama-cpp-test-harness-macros", + "llama-cpp-wrapper-error-fixture", "llama-cpp-wrapper-sources", ] @@ -42,6 +43,7 @@ llama-cpp-gbnf = { path = "llama-cpp-gbnf", version = "=0.13.0" } llama-cpp-log-decoder = { path = "llama-cpp-log-decoder", version = "=0.13.0" } llama-cpp-test-harness = { path = "llama-cpp-test-harness", version = "=0.13.0" } llama-cpp-test-harness-macros = { path = "llama-cpp-test-harness-macros", version = "=0.13.0" } +llama-cpp-wrapper-error-fixture = { path = "llama-cpp-wrapper-error-fixture" } llama-cpp-wrapper-sources = { path = "llama-cpp-wrapper-sources", version = "=0.13.0" } llguidance = "=1.7.0" log = "=0.4.29" diff --git a/llama-cpp-bindings-sys/Cargo.toml b/llama-cpp-bindings-sys/Cargo.toml index 1a275947e..4a2a0345f 100644 --- a/llama-cpp-bindings-sys/Cargo.toml +++ b/llama-cpp-bindings-sys/Cargo.toml @@ -37,6 +37,8 @@ include = [ "/llama.cpp/convert_hf_to_gguf.py", "/llama.cpp/common/build-info.cpp.in", + "/llama.cpp/ggml/src/ggml-version.h.in", + "/llama.cpp/src/llama-version.h.in", "/llama.cpp/include/llama.h", "/llama.cpp/include/llama-cpp.h", diff --git a/llama-cpp-bindings-sys/llama.cpp b/llama-cpp-bindings-sys/llama.cpp index c1d0e7a00..5266f24da 160000 --- a/llama-cpp-bindings-sys/llama.cpp +++ b/llama-cpp-bindings-sys/llama.cpp @@ -1 +1 @@ -Subproject commit c1d0e7a004015f23bc0233470b747b596f29b264 +Subproject commit 5266f24da75dc449bd56cbed7addb9c8e4a6a73e diff --git a/llama-cpp-bindings-sys/src/lib.rs b/llama-cpp-bindings-sys/src/lib.rs index 522d1ec68..0b6b4ba15 100644 --- a/llama-cpp-bindings-sys/src/lib.rs +++ b/llama-cpp-bindings-sys/src/lib.rs @@ -15,7 +15,7 @@ clippy::doc_markdown, clippy::pub_underscore_fields, clippy::use_self, - reason = "bindgen writes this file from the vendored headers; its shape is not ours to change" + reason = "bindgen writes this file from the llama.cpp headers; its shape is not ours to change" )] include!(concat!(env!("OUT_DIR"), "/bindings.rs")); diff --git a/llama-cpp-bindings-sys/wrapper_chat_apply.cpp b/llama-cpp-bindings-sys/wrapper_chat_apply.cpp index 5b822e8d9..48d4c1799 100644 --- a/llama-cpp-bindings-sys/wrapper_chat_apply.cpp +++ b/llama-cpp-bindings-sys/wrapper_chat_apply.cpp @@ -87,18 +87,18 @@ 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_VENDORED_OUT_OF_MEMORY; + 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_VENDORED_THREW_CXX_EXCEPTION; + 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_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_APPLY_CHAT_TEMPLATE_LLAMA_CPP_THREW_CXX_EXCEPTION; } } diff --git a/llama-cpp-bindings-sys/wrapper_chat_apply.h b/llama-cpp-bindings-sys/wrapper_chat_apply.h index 68f678a81..3add989a3 100644 --- a/llama-cpp-bindings-sys/wrapper_chat_apply.h +++ b/llama-cpp-bindings-sys/wrapper_chat_apply.h @@ -19,8 +19,8 @@ typedef enum llama_rs_apply_chat_template_status { LLAMA_RS_APPLY_CHAT_TEMPLATE_MODEL_HAS_NO_VOCAB, LLAMA_RS_APPLY_CHAT_TEMPLATE_TEMPLATE_APPLICATION_FAILED, LLAMA_RS_APPLY_CHAT_TEMPLATE_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_APPLY_CHAT_TEMPLATE_VENDORED_OUT_OF_MEMORY, - LLAMA_RS_APPLY_CHAT_TEMPLATE_VENDORED_THREW_CXX_EXCEPTION, + 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_status; llama_rs_apply_chat_template_status llama_rs_apply_chat_template( diff --git a/llama-cpp-bindings-sys/wrapper_chat_parse.cpp b/llama-cpp-bindings-sys/wrapper_chat_parse.cpp index bae6ba6b5..9525c9ccf 100644 --- a/llama-cpp-bindings-sys/wrapper_chat_parse.cpp +++ b/llama-cpp-bindings-sys/wrapper_chat_parse.cpp @@ -75,19 +75,19 @@ extern "C" auto llama_rs_chat_parser_create( return LLAMA_RS_CHAT_PARSER_CREATE_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_CHAT_PARSER_CREATE_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_CHAT_PARSER_CREATE_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_CHAT_PARSER_CREATE_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_CHAT_PARSER_CREATE_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_CHAT_PARSER_CREATE_LLAMA_CPP_THREW_CXX_EXCEPTION; } catch (...) { *out_error = llama_rs_dup_string(std::string("unknown c++ exception")); if (*out_error == nullptr) { return LLAMA_RS_CHAT_PARSER_CREATE_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_CHAT_PARSER_CREATE_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_CHAT_PARSER_CREATE_LLAMA_CPP_THREW_CXX_EXCEPTION; } } @@ -101,7 +101,7 @@ extern "C" auto llama_rs_chat_parser_free( const std::unique_ptr reclaimed(parser); return LLAMA_RS_CHAT_PARSER_FREE_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_CHAT_PARSER_FREE_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_CHAT_PARSER_FREE_LLAMA_CPP_OUT_OF_MEMORY; } catch (const std::exception & err) { if (out_error != nullptr) { *out_error = llama_rs_dup_string(err.what()); @@ -171,19 +171,19 @@ extern "C" auto llama_rs_parse_chat_message( return LLAMA_RS_PARSE_CHAT_MESSAGE_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_PARSE_CHAT_MESSAGE_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_PARSE_CHAT_MESSAGE_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_PARSE_CHAT_MESSAGE_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_PARSE_CHAT_MESSAGE_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_PARSE_CHAT_MESSAGE_LLAMA_CPP_THREW_CXX_EXCEPTION; } catch (...) { *out_error = llama_rs_dup_string(std::string("unknown c++ exception")); if (*out_error == nullptr) { return LLAMA_RS_PARSE_CHAT_MESSAGE_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_PARSE_CHAT_MESSAGE_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_PARSE_CHAT_MESSAGE_LLAMA_CPP_THREW_CXX_EXCEPTION; } } @@ -197,7 +197,7 @@ extern "C" auto llama_rs_parsed_chat_free( const std::unique_ptr reclaimed(handle); return LLAMA_RS_PARSED_CHAT_FREE_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_PARSED_CHAT_FREE_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_PARSED_CHAT_FREE_LLAMA_CPP_OUT_OF_MEMORY; } catch (const std::exception & err) { if (out_error != nullptr) { *out_error = llama_rs_dup_string(err.what()); @@ -237,7 +237,7 @@ extern "C" auto llama_rs_parsed_chat_tool_call_count( *out_count = handle->message.tool_calls.size(); return LLAMA_RS_PARSED_CHAT_TOOL_CALL_COUNT_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_PARSED_CHAT_TOOL_CALL_COUNT_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_PARSED_CHAT_TOOL_CALL_COUNT_LLAMA_CPP_OUT_OF_MEMORY; } catch (const std::exception & err) { if (out_error != nullptr) { *out_error = llama_rs_dup_string(err.what()); @@ -245,7 +245,7 @@ extern "C" auto llama_rs_parsed_chat_tool_call_count( return LLAMA_RS_PARSED_CHAT_TOOL_CALL_COUNT_ERROR_STRING_ALLOCATION_FAILED; } } - return LLAMA_RS_PARSED_CHAT_TOOL_CALL_COUNT_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_PARSED_CHAT_TOOL_CALL_COUNT_LLAMA_CPP_THREW_CXX_EXCEPTION; } catch (...) { if (out_error != nullptr) { *out_error = llama_rs_dup_string("unknown c++ exception"); @@ -253,7 +253,7 @@ extern "C" auto llama_rs_parsed_chat_tool_call_count( return LLAMA_RS_PARSED_CHAT_TOOL_CALL_COUNT_ERROR_STRING_ALLOCATION_FAILED; } } - return LLAMA_RS_PARSED_CHAT_TOOL_CALL_COUNT_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_PARSED_CHAT_TOOL_CALL_COUNT_LLAMA_CPP_THREW_CXX_EXCEPTION; } } @@ -285,7 +285,7 @@ extern "C" auto llama_rs_parsed_chat_tool_call_id( } return LLAMA_RS_PARSED_CHAT_TOOL_CALL_ID_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_PARSED_CHAT_TOOL_CALL_ID_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_PARSED_CHAT_TOOL_CALL_ID_LLAMA_CPP_OUT_OF_MEMORY; } catch (const std::exception & err) { if (out_error != nullptr) { *out_error = llama_rs_dup_string(err.what()); @@ -293,7 +293,7 @@ extern "C" auto llama_rs_parsed_chat_tool_call_id( return LLAMA_RS_PARSED_CHAT_TOOL_CALL_ID_ERROR_STRING_ALLOCATION_FAILED; } } - return LLAMA_RS_PARSED_CHAT_TOOL_CALL_ID_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_PARSED_CHAT_TOOL_CALL_ID_LLAMA_CPP_THREW_CXX_EXCEPTION; } catch (...) { if (out_error != nullptr) { *out_error = llama_rs_dup_string("unknown c++ exception"); @@ -301,7 +301,7 @@ extern "C" auto llama_rs_parsed_chat_tool_call_id( return LLAMA_RS_PARSED_CHAT_TOOL_CALL_ID_ERROR_STRING_ALLOCATION_FAILED; } } - return LLAMA_RS_PARSED_CHAT_TOOL_CALL_ID_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_PARSED_CHAT_TOOL_CALL_ID_LLAMA_CPP_THREW_CXX_EXCEPTION; } } @@ -333,7 +333,7 @@ extern "C" auto llama_rs_parsed_chat_tool_call_name( } return LLAMA_RS_PARSED_CHAT_TOOL_CALL_NAME_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_PARSED_CHAT_TOOL_CALL_NAME_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_PARSED_CHAT_TOOL_CALL_NAME_LLAMA_CPP_OUT_OF_MEMORY; } catch (const std::exception & err) { if (out_error != nullptr) { *out_error = llama_rs_dup_string(err.what()); @@ -341,7 +341,7 @@ extern "C" auto llama_rs_parsed_chat_tool_call_name( return LLAMA_RS_PARSED_CHAT_TOOL_CALL_NAME_ERROR_STRING_ALLOCATION_FAILED; } } - return LLAMA_RS_PARSED_CHAT_TOOL_CALL_NAME_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_PARSED_CHAT_TOOL_CALL_NAME_LLAMA_CPP_THREW_CXX_EXCEPTION; } catch (...) { if (out_error != nullptr) { *out_error = llama_rs_dup_string("unknown c++ exception"); @@ -349,7 +349,7 @@ extern "C" auto llama_rs_parsed_chat_tool_call_name( return LLAMA_RS_PARSED_CHAT_TOOL_CALL_NAME_ERROR_STRING_ALLOCATION_FAILED; } } - return LLAMA_RS_PARSED_CHAT_TOOL_CALL_NAME_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_PARSED_CHAT_TOOL_CALL_NAME_LLAMA_CPP_THREW_CXX_EXCEPTION; } } @@ -382,7 +382,7 @@ extern "C" auto llama_rs_parsed_chat_tool_call_arguments( } return LLAMA_RS_PARSED_CHAT_TOOL_CALL_ARGUMENTS_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_PARSED_CHAT_TOOL_CALL_ARGUMENTS_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_PARSED_CHAT_TOOL_CALL_ARGUMENTS_LLAMA_CPP_OUT_OF_MEMORY; } catch (const std::exception & err) { if (out_error != nullptr) { *out_error = llama_rs_dup_string(err.what()); @@ -390,7 +390,7 @@ extern "C" auto llama_rs_parsed_chat_tool_call_arguments( return LLAMA_RS_PARSED_CHAT_TOOL_CALL_ARGUMENTS_ERROR_STRING_ALLOCATION_FAILED; } } - return LLAMA_RS_PARSED_CHAT_TOOL_CALL_ARGUMENTS_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_PARSED_CHAT_TOOL_CALL_ARGUMENTS_LLAMA_CPP_THREW_CXX_EXCEPTION; } catch (...) { if (out_error != nullptr) { *out_error = llama_rs_dup_string("unknown c++ exception"); @@ -398,7 +398,7 @@ extern "C" auto llama_rs_parsed_chat_tool_call_arguments( return LLAMA_RS_PARSED_CHAT_TOOL_CALL_ARGUMENTS_ERROR_STRING_ALLOCATION_FAILED; } } - return LLAMA_RS_PARSED_CHAT_TOOL_CALL_ARGUMENTS_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_PARSED_CHAT_TOOL_CALL_ARGUMENTS_LLAMA_CPP_THREW_CXX_EXCEPTION; } } @@ -426,7 +426,7 @@ extern "C" auto llama_rs_parsed_chat_content( } return LLAMA_RS_PARSED_CHAT_CONTENT_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_PARSED_CHAT_CONTENT_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_PARSED_CHAT_CONTENT_LLAMA_CPP_OUT_OF_MEMORY; } catch (const std::exception & err) { if (out_error != nullptr) { *out_error = llama_rs_dup_string(err.what()); @@ -434,7 +434,7 @@ extern "C" auto llama_rs_parsed_chat_content( return LLAMA_RS_PARSED_CHAT_CONTENT_ERROR_STRING_ALLOCATION_FAILED; } } - return LLAMA_RS_PARSED_CHAT_CONTENT_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_PARSED_CHAT_CONTENT_LLAMA_CPP_THREW_CXX_EXCEPTION; } catch (...) { if (out_error != nullptr) { *out_error = llama_rs_dup_string("unknown c++ exception"); @@ -442,7 +442,7 @@ extern "C" auto llama_rs_parsed_chat_content( return LLAMA_RS_PARSED_CHAT_CONTENT_ERROR_STRING_ALLOCATION_FAILED; } } - return LLAMA_RS_PARSED_CHAT_CONTENT_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_PARSED_CHAT_CONTENT_LLAMA_CPP_THREW_CXX_EXCEPTION; } } @@ -470,7 +470,7 @@ extern "C" auto llama_rs_parsed_chat_reasoning_content( } return LLAMA_RS_PARSED_CHAT_REASONING_CONTENT_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_PARSED_CHAT_REASONING_CONTENT_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_PARSED_CHAT_REASONING_CONTENT_LLAMA_CPP_OUT_OF_MEMORY; } catch (const std::exception & err) { if (out_error != nullptr) { *out_error = llama_rs_dup_string(err.what()); @@ -478,7 +478,7 @@ extern "C" auto llama_rs_parsed_chat_reasoning_content( return LLAMA_RS_PARSED_CHAT_REASONING_CONTENT_ERROR_STRING_ALLOCATION_FAILED; } } - return LLAMA_RS_PARSED_CHAT_REASONING_CONTENT_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_PARSED_CHAT_REASONING_CONTENT_LLAMA_CPP_THREW_CXX_EXCEPTION; } catch (...) { if (out_error != nullptr) { *out_error = llama_rs_dup_string("unknown c++ exception"); @@ -486,6 +486,6 @@ extern "C" auto llama_rs_parsed_chat_reasoning_content( return LLAMA_RS_PARSED_CHAT_REASONING_CONTENT_ERROR_STRING_ALLOCATION_FAILED; } } - return LLAMA_RS_PARSED_CHAT_REASONING_CONTENT_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_PARSED_CHAT_REASONING_CONTENT_LLAMA_CPP_THREW_CXX_EXCEPTION; } } diff --git a/llama-cpp-bindings-sys/wrapper_chat_parse.h b/llama-cpp-bindings-sys/wrapper_chat_parse.h index aaf529e17..8c097f0bf 100644 --- a/llama-cpp-bindings-sys/wrapper_chat_parse.h +++ b/llama-cpp-bindings-sys/wrapper_chat_parse.h @@ -23,8 +23,8 @@ typedef enum llama_rs_chat_parser_create_status { LLAMA_RS_CHAT_PARSER_CREATE_MODEL_HAS_NO_CHAT_TEMPLATE, LLAMA_RS_CHAT_PARSER_CREATE_MODEL_HAS_NO_VOCAB, LLAMA_RS_CHAT_PARSER_CREATE_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_CHAT_PARSER_CREATE_VENDORED_OUT_OF_MEMORY, - LLAMA_RS_CHAT_PARSER_CREATE_VENDORED_THREW_CXX_EXCEPTION, + LLAMA_RS_CHAT_PARSER_CREATE_LLAMA_CPP_OUT_OF_MEMORY, + LLAMA_RS_CHAT_PARSER_CREATE_LLAMA_CPP_THREW_CXX_EXCEPTION, } llama_rs_chat_parser_create_status; llama_rs_chat_parser_create_status llama_rs_chat_parser_create( @@ -35,7 +35,7 @@ llama_rs_chat_parser_create_status llama_rs_chat_parser_create( typedef enum llama_rs_chat_parser_free_status { LLAMA_RS_CHAT_PARSER_FREE_OK = 0, LLAMA_RS_CHAT_PARSER_FREE_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_CHAT_PARSER_FREE_VENDORED_OUT_OF_MEMORY, + LLAMA_RS_CHAT_PARSER_FREE_LLAMA_CPP_OUT_OF_MEMORY, LLAMA_RS_CHAT_PARSER_FREE_DESTRUCTOR_THREW_CXX_EXCEPTION, } llama_rs_chat_parser_free_status; @@ -50,8 +50,8 @@ typedef enum llama_rs_parse_chat_message_status { LLAMA_RS_PARSE_CHAT_MESSAGE_NULL_OUT_HANDLE_ARG, LLAMA_RS_PARSE_CHAT_MESSAGE_NULL_OUT_ERROR_ARG, LLAMA_RS_PARSE_CHAT_MESSAGE_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_PARSE_CHAT_MESSAGE_VENDORED_OUT_OF_MEMORY, - LLAMA_RS_PARSE_CHAT_MESSAGE_VENDORED_THREW_CXX_EXCEPTION, + LLAMA_RS_PARSE_CHAT_MESSAGE_LLAMA_CPP_OUT_OF_MEMORY, + LLAMA_RS_PARSE_CHAT_MESSAGE_LLAMA_CPP_THREW_CXX_EXCEPTION, } llama_rs_parse_chat_message_status; llama_rs_parse_chat_message_status llama_rs_parse_chat_message( @@ -65,7 +65,7 @@ llama_rs_parse_chat_message_status llama_rs_parse_chat_message( typedef enum llama_rs_parsed_chat_free_status { LLAMA_RS_PARSED_CHAT_FREE_OK = 0, LLAMA_RS_PARSED_CHAT_FREE_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_PARSED_CHAT_FREE_VENDORED_OUT_OF_MEMORY, + LLAMA_RS_PARSED_CHAT_FREE_LLAMA_CPP_OUT_OF_MEMORY, LLAMA_RS_PARSED_CHAT_FREE_DESTRUCTOR_THREW_CXX_EXCEPTION, } llama_rs_parsed_chat_free_status; @@ -78,8 +78,8 @@ typedef enum llama_rs_parsed_chat_tool_call_count_status { LLAMA_RS_PARSED_CHAT_TOOL_CALL_COUNT_NULL_HANDLE_ARG, LLAMA_RS_PARSED_CHAT_TOOL_CALL_COUNT_NULL_OUT_COUNT_ARG, LLAMA_RS_PARSED_CHAT_TOOL_CALL_COUNT_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_PARSED_CHAT_TOOL_CALL_COUNT_VENDORED_OUT_OF_MEMORY, - LLAMA_RS_PARSED_CHAT_TOOL_CALL_COUNT_VENDORED_THREW_CXX_EXCEPTION, + LLAMA_RS_PARSED_CHAT_TOOL_CALL_COUNT_LLAMA_CPP_OUT_OF_MEMORY, + LLAMA_RS_PARSED_CHAT_TOOL_CALL_COUNT_LLAMA_CPP_THREW_CXX_EXCEPTION, } llama_rs_parsed_chat_tool_call_count_status; llama_rs_parsed_chat_tool_call_count_status llama_rs_parsed_chat_tool_call_count( @@ -93,8 +93,8 @@ typedef enum llama_rs_parsed_chat_tool_call_id_status { LLAMA_RS_PARSED_CHAT_TOOL_CALL_ID_NULL_OUT_STRING_ARG, LLAMA_RS_PARSED_CHAT_TOOL_CALL_ID_INDEX_OUT_OF_BOUNDS, LLAMA_RS_PARSED_CHAT_TOOL_CALL_ID_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_PARSED_CHAT_TOOL_CALL_ID_VENDORED_OUT_OF_MEMORY, - LLAMA_RS_PARSED_CHAT_TOOL_CALL_ID_VENDORED_THREW_CXX_EXCEPTION, + LLAMA_RS_PARSED_CHAT_TOOL_CALL_ID_LLAMA_CPP_OUT_OF_MEMORY, + LLAMA_RS_PARSED_CHAT_TOOL_CALL_ID_LLAMA_CPP_THREW_CXX_EXCEPTION, } llama_rs_parsed_chat_tool_call_id_status; llama_rs_parsed_chat_tool_call_id_status llama_rs_parsed_chat_tool_call_id( @@ -109,8 +109,8 @@ typedef enum llama_rs_parsed_chat_tool_call_name_status { LLAMA_RS_PARSED_CHAT_TOOL_CALL_NAME_NULL_OUT_STRING_ARG, LLAMA_RS_PARSED_CHAT_TOOL_CALL_NAME_INDEX_OUT_OF_BOUNDS, LLAMA_RS_PARSED_CHAT_TOOL_CALL_NAME_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_PARSED_CHAT_TOOL_CALL_NAME_VENDORED_OUT_OF_MEMORY, - LLAMA_RS_PARSED_CHAT_TOOL_CALL_NAME_VENDORED_THREW_CXX_EXCEPTION, + LLAMA_RS_PARSED_CHAT_TOOL_CALL_NAME_LLAMA_CPP_OUT_OF_MEMORY, + LLAMA_RS_PARSED_CHAT_TOOL_CALL_NAME_LLAMA_CPP_THREW_CXX_EXCEPTION, } llama_rs_parsed_chat_tool_call_name_status; llama_rs_parsed_chat_tool_call_name_status llama_rs_parsed_chat_tool_call_name( @@ -125,8 +125,8 @@ typedef enum llama_rs_parsed_chat_tool_call_arguments_status { LLAMA_RS_PARSED_CHAT_TOOL_CALL_ARGUMENTS_NULL_OUT_STRING_ARG, LLAMA_RS_PARSED_CHAT_TOOL_CALL_ARGUMENTS_INDEX_OUT_OF_BOUNDS, LLAMA_RS_PARSED_CHAT_TOOL_CALL_ARGUMENTS_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_PARSED_CHAT_TOOL_CALL_ARGUMENTS_VENDORED_OUT_OF_MEMORY, - LLAMA_RS_PARSED_CHAT_TOOL_CALL_ARGUMENTS_VENDORED_THREW_CXX_EXCEPTION, + LLAMA_RS_PARSED_CHAT_TOOL_CALL_ARGUMENTS_LLAMA_CPP_OUT_OF_MEMORY, + LLAMA_RS_PARSED_CHAT_TOOL_CALL_ARGUMENTS_LLAMA_CPP_THREW_CXX_EXCEPTION, } llama_rs_parsed_chat_tool_call_arguments_status; llama_rs_parsed_chat_tool_call_arguments_status llama_rs_parsed_chat_tool_call_arguments( @@ -140,8 +140,8 @@ typedef enum llama_rs_parsed_chat_content_status { LLAMA_RS_PARSED_CHAT_CONTENT_NULL_HANDLE_ARG, LLAMA_RS_PARSED_CHAT_CONTENT_NULL_OUT_STRING_ARG, LLAMA_RS_PARSED_CHAT_CONTENT_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_PARSED_CHAT_CONTENT_VENDORED_OUT_OF_MEMORY, - LLAMA_RS_PARSED_CHAT_CONTENT_VENDORED_THREW_CXX_EXCEPTION, + LLAMA_RS_PARSED_CHAT_CONTENT_LLAMA_CPP_OUT_OF_MEMORY, + LLAMA_RS_PARSED_CHAT_CONTENT_LLAMA_CPP_THREW_CXX_EXCEPTION, } llama_rs_parsed_chat_content_status; llama_rs_parsed_chat_content_status llama_rs_parsed_chat_content( @@ -154,8 +154,8 @@ typedef enum llama_rs_parsed_chat_reasoning_content_status { LLAMA_RS_PARSED_CHAT_REASONING_CONTENT_NULL_HANDLE_ARG, LLAMA_RS_PARSED_CHAT_REASONING_CONTENT_NULL_OUT_STRING_ARG, LLAMA_RS_PARSED_CHAT_REASONING_CONTENT_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_PARSED_CHAT_REASONING_CONTENT_VENDORED_OUT_OF_MEMORY, - LLAMA_RS_PARSED_CHAT_REASONING_CONTENT_VENDORED_THREW_CXX_EXCEPTION, + LLAMA_RS_PARSED_CHAT_REASONING_CONTENT_LLAMA_CPP_OUT_OF_MEMORY, + LLAMA_RS_PARSED_CHAT_REASONING_CONTENT_LLAMA_CPP_THREW_CXX_EXCEPTION, } llama_rs_parsed_chat_reasoning_content_status; llama_rs_parsed_chat_reasoning_content_status llama_rs_parsed_chat_reasoning_content( diff --git a/llama-cpp-bindings-sys/wrapper_common.cpp b/llama-cpp-bindings-sys/wrapper_common.cpp index 96b155088..5f43c3e9b 100644 --- a/llama-cpp-bindings-sys/wrapper_common.cpp +++ b/llama-cpp-bindings-sys/wrapper_common.cpp @@ -45,7 +45,7 @@ extern "C" auto llama_rs_json_schema_to_grammar( } return LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_LLAMA_CPP_OUT_OF_MEMORY; } catch (const std::invalid_argument & err) { *out_error = llama_rs_dup_string(err.what()); if (*out_error == nullptr) { @@ -57,13 +57,13 @@ extern "C" auto llama_rs_json_schema_to_grammar( if (*out_error == nullptr) { return LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_LLAMA_CPP_THREW_CXX_EXCEPTION; } catch (...) { *out_error = llama_rs_dup_string("unknown c++ exception"); if (*out_error == nullptr) { return LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_LLAMA_CPP_THREW_CXX_EXCEPTION; } } @@ -99,23 +99,23 @@ extern "C" auto llama_rs_sampler_init_grammar( try { *out_sampler = llama_sampler_init_grammar(vocab, grammar_str, grammar_root); if (*out_sampler == nullptr) { - return LLAMA_RS_SAMPLER_INIT_GRAMMAR_VENDORED_RETURNED_NULL; + return LLAMA_RS_SAMPLER_INIT_GRAMMAR_LLAMA_CPP_RETURNED_NULL; } return LLAMA_RS_SAMPLER_INIT_GRAMMAR_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_SAMPLER_INIT_GRAMMAR_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_SAMPLER_INIT_GRAMMAR_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_SAMPLER_INIT_GRAMMAR_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_SAMPLER_INIT_GRAMMAR_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_SAMPLER_INIT_GRAMMAR_LLAMA_CPP_THREW_CXX_EXCEPTION; } catch (...) { *out_error = llama_rs_dup_string("unknown c++ exception"); if (*out_error == nullptr) { return LLAMA_RS_SAMPLER_INIT_GRAMMAR_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_SAMPLER_INIT_GRAMMAR_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_SAMPLER_INIT_GRAMMAR_LLAMA_CPP_THREW_CXX_EXCEPTION; } } @@ -151,11 +151,11 @@ extern "C" auto llama_rs_sampler_init_grammar_lazy_patterns( trigger_tokens, num_trigger_tokens); if (*out_sampler == nullptr) { - return LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_VENDORED_RETURNED_NULL; + return LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_LLAMA_CPP_RETURNED_NULL; } return LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_LLAMA_CPP_OUT_OF_MEMORY; } catch (const std::regex_error & err) { *out_error = llama_rs_dup_string(err.what()); if (*out_error == nullptr) { @@ -167,13 +167,13 @@ extern "C" auto llama_rs_sampler_init_grammar_lazy_patterns( if (*out_error == nullptr) { return LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_LLAMA_CPP_THREW_CXX_EXCEPTION; } catch (...) { *out_error = llama_rs_dup_string("unknown c++ exception"); if (*out_error == nullptr) { return LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_LLAMA_CPP_THREW_CXX_EXCEPTION; } } @@ -210,32 +210,32 @@ extern "C" auto llama_rs_memory_seq_pos_max( *out_position = llama_memory_seq_pos_max(mem, seq_id); return LLAMA_RS_MEMORY_SEQ_POS_MAX_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_MEMORY_SEQ_POS_MAX_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_MEMORY_SEQ_POS_MAX_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_MEMORY_SEQ_POS_MAX_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_MEMORY_SEQ_POS_MAX_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_MEMORY_SEQ_POS_MAX_LLAMA_CPP_THREW_CXX_EXCEPTION; } catch (...) { *out_error = llama_rs_dup_string("unknown c++ exception"); if (*out_error == nullptr) { return LLAMA_RS_MEMORY_SEQ_POS_MAX_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_MEMORY_SEQ_POS_MAX_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_MEMORY_SEQ_POS_MAX_LLAMA_CPP_THREW_CXX_EXCEPTION; } } extern "C" auto llama_rs_encode( struct llama_context * ctx, struct llama_batch batch, - int32_t * out_vendored_return_code, + int32_t * out_llama_cpp_return_code, char ** out_error) -> llama_rs_encode_status { if (out_error != nullptr) { *out_error = nullptr; } - if (out_vendored_return_code != nullptr) { - *out_vendored_return_code = 0; + if (out_llama_cpp_return_code != nullptr) { + *out_llama_cpp_return_code = 0; } if (ctx == nullptr) { return LLAMA_RS_ENCODE_NULL_CTX_ARG; @@ -250,8 +250,8 @@ extern "C" auto llama_rs_encode( } int32_t const result = llama_encode(ctx, batch); if (result != 0) { - if (out_vendored_return_code != nullptr) { - *out_vendored_return_code = result; + if (out_llama_cpp_return_code != nullptr) { + *out_llama_cpp_return_code = result; } if (result == -2) { return LLAMA_RS_ENCODE_OUT_OF_MEMORY; @@ -259,11 +259,11 @@ extern "C" auto llama_rs_encode( if (result == -3) { return LLAMA_RS_ENCODE_COMPUTE_FAILED; } - return LLAMA_RS_ENCODE_VENDORED_RETURNED_NONZERO_CODE; + return LLAMA_RS_ENCODE_LLAMA_CPP_RETURNED_NONZERO_CODE; } return LLAMA_RS_ENCODE_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_ENCODE_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_ENCODE_LLAMA_CPP_OUT_OF_MEMORY; } catch (const std::exception & err) { if (out_error != nullptr) { *out_error = llama_rs_dup_string(err.what()); @@ -271,7 +271,7 @@ extern "C" auto llama_rs_encode( return LLAMA_RS_ENCODE_ERROR_STRING_ALLOCATION_FAILED; } } - return LLAMA_RS_ENCODE_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_ENCODE_LLAMA_CPP_THREW_CXX_EXCEPTION; } catch (...) { if (out_error != nullptr) { *out_error = llama_rs_dup_string("unknown c++ exception"); @@ -279,7 +279,7 @@ extern "C" auto llama_rs_encode( return LLAMA_RS_ENCODE_ERROR_STRING_ALLOCATION_FAILED; } } - return LLAMA_RS_ENCODE_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_ENCODE_LLAMA_CPP_THREW_CXX_EXCEPTION; } } @@ -312,7 +312,7 @@ extern "C" auto llama_rs_memory_seq_add( llama_memory_seq_add(mem, seq_id, pos_start, pos_end, shift); return LLAMA_RS_MEMORY_SEQ_ADD_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_MEMORY_SEQ_ADD_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_MEMORY_SEQ_ADD_LLAMA_CPP_OUT_OF_MEMORY; } catch (const std::exception & err) { if (out_error != nullptr) { *out_error = llama_rs_dup_string(err.what()); @@ -320,7 +320,7 @@ extern "C" auto llama_rs_memory_seq_add( return LLAMA_RS_MEMORY_SEQ_ADD_ERROR_STRING_ALLOCATION_FAILED; } } - return LLAMA_RS_MEMORY_SEQ_ADD_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_MEMORY_SEQ_ADD_LLAMA_CPP_THREW_CXX_EXCEPTION; } catch (...) { if (out_error != nullptr) { *out_error = llama_rs_dup_string("unknown c++ exception"); @@ -328,7 +328,7 @@ extern "C" auto llama_rs_memory_seq_add( return LLAMA_RS_MEMORY_SEQ_ADD_ERROR_STRING_ALLOCATION_FAILED; } } - return LLAMA_RS_MEMORY_SEQ_ADD_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_MEMORY_SEQ_ADD_LLAMA_CPP_THREW_CXX_EXCEPTION; } } @@ -361,7 +361,7 @@ extern "C" auto llama_rs_memory_seq_div( llama_memory_seq_div(mem, seq_id, pos_start, pos_end, divisor); return LLAMA_RS_MEMORY_SEQ_DIV_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_MEMORY_SEQ_DIV_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_MEMORY_SEQ_DIV_LLAMA_CPP_OUT_OF_MEMORY; } catch (const std::exception & err) { if (out_error != nullptr) { *out_error = llama_rs_dup_string(err.what()); @@ -369,7 +369,7 @@ extern "C" auto llama_rs_memory_seq_div( return LLAMA_RS_MEMORY_SEQ_DIV_ERROR_STRING_ALLOCATION_FAILED; } } - return LLAMA_RS_MEMORY_SEQ_DIV_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_MEMORY_SEQ_DIV_LLAMA_CPP_THREW_CXX_EXCEPTION; } catch (...) { if (out_error != nullptr) { *out_error = llama_rs_dup_string("unknown c++ exception"); @@ -377,7 +377,7 @@ extern "C" auto llama_rs_memory_seq_div( return LLAMA_RS_MEMORY_SEQ_DIV_ERROR_STRING_ALLOCATION_FAILED; } } - return LLAMA_RS_MEMORY_SEQ_DIV_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_MEMORY_SEQ_DIV_LLAMA_CPP_THREW_CXX_EXCEPTION; } } @@ -406,19 +406,19 @@ extern "C" auto llama_rs_sampler_sample( *out_token = llama_sampler_sample(sampler, ctx, idx); return LLAMA_RS_SAMPLER_SAMPLE_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_SAMPLER_SAMPLE_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_SAMPLER_SAMPLE_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_SAMPLER_SAMPLE_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_SAMPLER_SAMPLE_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_SAMPLER_SAMPLE_LLAMA_CPP_THREW_CXX_EXCEPTION; } catch (...) { *out_error = llama_rs_dup_string("unknown c++ exception"); if (*out_error == nullptr) { return LLAMA_RS_SAMPLER_SAMPLE_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_SAMPLER_SAMPLE_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_SAMPLER_SAMPLE_LLAMA_CPP_THREW_CXX_EXCEPTION; } } @@ -439,19 +439,19 @@ extern "C" auto llama_rs_sampler_accept( llama_sampler_accept(sampler, token); return LLAMA_RS_SAMPLER_ACCEPT_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_SAMPLER_ACCEPT_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_SAMPLER_ACCEPT_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_SAMPLER_ACCEPT_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_SAMPLER_ACCEPT_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_SAMPLER_ACCEPT_LLAMA_CPP_THREW_CXX_EXCEPTION; } catch (...) { *out_error = llama_rs_dup_string("unknown c++ exception"); if (*out_error == nullptr) { return LLAMA_RS_SAMPLER_ACCEPT_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_SAMPLER_ACCEPT_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_SAMPLER_ACCEPT_LLAMA_CPP_THREW_CXX_EXCEPTION; } } @@ -478,23 +478,23 @@ extern "C" auto llama_rs_load_model_from_file( try { *out_model = llama_model_load_from_file(path, params); if (*out_model == nullptr) { - return LLAMA_RS_LOAD_MODEL_FROM_FILE_VENDORED_RETURNED_NULL; + return LLAMA_RS_LOAD_MODEL_FROM_FILE_LLAMA_CPP_RETURNED_NULL; } return LLAMA_RS_LOAD_MODEL_FROM_FILE_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_LOAD_MODEL_FROM_FILE_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_LOAD_MODEL_FROM_FILE_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_LOAD_MODEL_FROM_FILE_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_LOAD_MODEL_FROM_FILE_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_LOAD_MODEL_FROM_FILE_LLAMA_CPP_THREW_CXX_EXCEPTION; } catch (...) { *out_error = llama_rs_dup_string("unknown c++ exception"); if (*out_error == nullptr) { return LLAMA_RS_LOAD_MODEL_FROM_FILE_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_LOAD_MODEL_FROM_FILE_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_LOAD_MODEL_FROM_FILE_LLAMA_CPP_THREW_CXX_EXCEPTION; } } @@ -521,36 +521,36 @@ extern "C" auto llama_rs_new_context_with_model( try { *out_ctx = llama_init_from_model(model, params); if (*out_ctx == nullptr) { - return LLAMA_RS_NEW_CONTEXT_WITH_MODEL_VENDORED_RETURNED_NULL; + return LLAMA_RS_NEW_CONTEXT_WITH_MODEL_LLAMA_CPP_RETURNED_NULL; } return LLAMA_RS_NEW_CONTEXT_WITH_MODEL_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_NEW_CONTEXT_WITH_MODEL_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_NEW_CONTEXT_WITH_MODEL_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_NEW_CONTEXT_WITH_MODEL_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_NEW_CONTEXT_WITH_MODEL_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_NEW_CONTEXT_WITH_MODEL_LLAMA_CPP_THREW_CXX_EXCEPTION; } catch (...) { *out_error = llama_rs_dup_string("unknown c++ exception"); if (*out_error == nullptr) { return LLAMA_RS_NEW_CONTEXT_WITH_MODEL_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_NEW_CONTEXT_WITH_MODEL_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_NEW_CONTEXT_WITH_MODEL_LLAMA_CPP_THREW_CXX_EXCEPTION; } } extern "C" auto llama_rs_decode( struct llama_context * ctx, struct llama_batch batch, - int32_t * out_vendored_return_code, + int32_t * out_llama_cpp_return_code, char ** out_error) -> llama_rs_decode_status { if (out_error != nullptr) { *out_error = nullptr; } - if (out_vendored_return_code != nullptr) { - *out_vendored_return_code = 0; + if (out_llama_cpp_return_code != nullptr) { + *out_llama_cpp_return_code = 0; } if (ctx == nullptr) { return LLAMA_RS_DECODE_NULL_CTX_ARG; @@ -561,8 +561,8 @@ extern "C" auto llama_rs_decode( try { int32_t const result = llama_decode(ctx, batch); if (result != 0) { - if (out_vendored_return_code != nullptr) { - *out_vendored_return_code = result; + if (out_llama_cpp_return_code != nullptr) { + *out_llama_cpp_return_code = result; } if (result == -2) { return LLAMA_RS_DECODE_OUT_OF_MEMORY; @@ -570,23 +570,23 @@ extern "C" auto llama_rs_decode( if (result == -3) { return LLAMA_RS_DECODE_COMPUTE_FAILED; } - return LLAMA_RS_DECODE_VENDORED_RETURNED_NONZERO_CODE; + return LLAMA_RS_DECODE_LLAMA_CPP_RETURNED_NONZERO_CODE; } return LLAMA_RS_DECODE_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_DECODE_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_DECODE_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_DECODE_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_DECODE_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_DECODE_LLAMA_CPP_THREW_CXX_EXCEPTION; } catch (...) { *out_error = llama_rs_dup_string("unknown c++ exception"); if (*out_error == nullptr) { return LLAMA_RS_DECODE_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_DECODE_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_DECODE_LLAMA_CPP_THREW_CXX_EXCEPTION; } } @@ -624,19 +624,19 @@ extern "C" auto llama_rs_tokenize( *out_returned_count = count; return LLAMA_RS_TOKENIZE_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_TOKENIZE_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_TOKENIZE_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_TOKENIZE_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_TOKENIZE_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_TOKENIZE_LLAMA_CPP_THREW_CXX_EXCEPTION; } catch (...) { *out_error = llama_rs_dup_string("unknown c++ exception"); if (*out_error == nullptr) { return LLAMA_RS_TOKENIZE_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_TOKENIZE_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_TOKENIZE_LLAMA_CPP_THREW_CXX_EXCEPTION; } } @@ -660,18 +660,18 @@ extern "C" auto llama_rs_sampler_apply( llama_sampler_apply(sampler, data_array); return LLAMA_RS_SAMPLER_APPLY_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_SAMPLER_APPLY_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_SAMPLER_APPLY_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_SAMPLER_APPLY_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_SAMPLER_APPLY_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_SAMPLER_APPLY_LLAMA_CPP_THREW_CXX_EXCEPTION; } catch (...) { *out_error = llama_rs_dup_string("unknown c++ exception"); if (*out_error == nullptr) { return LLAMA_RS_SAMPLER_APPLY_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_SAMPLER_APPLY_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_SAMPLER_APPLY_LLAMA_CPP_THREW_CXX_EXCEPTION; } } diff --git a/llama-cpp-bindings-sys/wrapper_common.h b/llama-cpp-bindings-sys/wrapper_common.h index 9a7750062..b9a7330d1 100644 --- a/llama-cpp-bindings-sys/wrapper_common.h +++ b/llama-cpp-bindings-sys/wrapper_common.h @@ -22,9 +22,9 @@ typedef enum llama_rs_json_schema_to_grammar_status { LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_NULL_OUT_GRAMMAR_ARG, LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_NULL_OUT_ERROR_ARG, LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_VENDORED_OUT_OF_MEMORY, + LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_LLAMA_CPP_OUT_OF_MEMORY, LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_INVALID_SCHEMA, - LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_VENDORED_THREW_CXX_EXCEPTION, + LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_LLAMA_CPP_THREW_CXX_EXCEPTION, } llama_rs_json_schema_to_grammar_status; llama_rs_json_schema_to_grammar_status llama_rs_json_schema_to_grammar( @@ -37,10 +37,10 @@ typedef enum llama_rs_sampler_init_grammar_status { LLAMA_RS_SAMPLER_INIT_GRAMMAR_OK = 0, LLAMA_RS_SAMPLER_INIT_GRAMMAR_NULL_OUT_SAMPLER_ARG, LLAMA_RS_SAMPLER_INIT_GRAMMAR_NULL_OUT_ERROR_ARG, - LLAMA_RS_SAMPLER_INIT_GRAMMAR_VENDORED_RETURNED_NULL, + LLAMA_RS_SAMPLER_INIT_GRAMMAR_LLAMA_CPP_RETURNED_NULL, LLAMA_RS_SAMPLER_INIT_GRAMMAR_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_SAMPLER_INIT_GRAMMAR_VENDORED_OUT_OF_MEMORY, - LLAMA_RS_SAMPLER_INIT_GRAMMAR_VENDORED_THREW_CXX_EXCEPTION, + 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_status; llama_rs_sampler_init_grammar_status llama_rs_sampler_init_grammar( @@ -54,11 +54,11 @@ typedef enum llama_rs_sampler_init_grammar_lazy_patterns_status { LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_OK = 0, LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_NULL_OUT_SAMPLER_ARG, LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_NULL_OUT_ERROR_ARG, - LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_VENDORED_RETURNED_NULL, + LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_LLAMA_CPP_RETURNED_NULL, LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_VENDORED_OUT_OF_MEMORY, + LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_LLAMA_CPP_OUT_OF_MEMORY, LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_INVALID_TRIGGER_PATTERN, - LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_VENDORED_THREW_CXX_EXCEPTION, + LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_LLAMA_CPP_THREW_CXX_EXCEPTION, } llama_rs_sampler_init_grammar_lazy_patterns_status; llama_rs_sampler_init_grammar_lazy_patterns_status llama_rs_sampler_init_grammar_lazy_patterns( @@ -77,8 +77,8 @@ typedef enum llama_rs_sampler_accept_status { LLAMA_RS_SAMPLER_ACCEPT_NULL_SAMPLER_ARG, LLAMA_RS_SAMPLER_ACCEPT_NULL_OUT_ERROR_ARG, LLAMA_RS_SAMPLER_ACCEPT_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_SAMPLER_ACCEPT_VENDORED_OUT_OF_MEMORY, - LLAMA_RS_SAMPLER_ACCEPT_VENDORED_THREW_CXX_EXCEPTION, + LLAMA_RS_SAMPLER_ACCEPT_LLAMA_CPP_OUT_OF_MEMORY, + LLAMA_RS_SAMPLER_ACCEPT_LLAMA_CPP_THREW_CXX_EXCEPTION, } llama_rs_sampler_accept_status; llama_rs_sampler_accept_status llama_rs_sampler_accept( @@ -93,8 +93,8 @@ typedef enum llama_rs_sampler_sample_status { LLAMA_RS_SAMPLER_SAMPLE_NULL_OUT_TOKEN_ARG, LLAMA_RS_SAMPLER_SAMPLE_NULL_OUT_ERROR_ARG, LLAMA_RS_SAMPLER_SAMPLE_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_SAMPLER_SAMPLE_VENDORED_OUT_OF_MEMORY, - LLAMA_RS_SAMPLER_SAMPLE_VENDORED_THREW_CXX_EXCEPTION, + LLAMA_RS_SAMPLER_SAMPLE_LLAMA_CPP_OUT_OF_MEMORY, + LLAMA_RS_SAMPLER_SAMPLE_LLAMA_CPP_THREW_CXX_EXCEPTION, } llama_rs_sampler_sample_status; llama_rs_sampler_sample_status llama_rs_sampler_sample( @@ -116,8 +116,8 @@ typedef enum llama_rs_memory_seq_pos_max_status { LLAMA_RS_MEMORY_SEQ_POS_MAX_NULL_MEM, LLAMA_RS_MEMORY_SEQ_POS_MAX_SEQ_ID_OUT_OF_RANGE, LLAMA_RS_MEMORY_SEQ_POS_MAX_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_MEMORY_SEQ_POS_MAX_VENDORED_OUT_OF_MEMORY, - LLAMA_RS_MEMORY_SEQ_POS_MAX_VENDORED_THREW_CXX_EXCEPTION, + 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_status; llama_rs_memory_seq_pos_max_status llama_rs_memory_seq_pos_max( @@ -131,18 +131,18 @@ typedef enum llama_rs_encode_status { LLAMA_RS_ENCODE_NULL_CTX_ARG, LLAMA_RS_ENCODE_NULL_MODEL, LLAMA_RS_ENCODE_MODEL_HAS_NO_ENCODER, - LLAMA_RS_ENCODE_VENDORED_RETURNED_NONZERO_CODE, + LLAMA_RS_ENCODE_LLAMA_CPP_RETURNED_NONZERO_CODE, LLAMA_RS_ENCODE_OUT_OF_MEMORY, LLAMA_RS_ENCODE_COMPUTE_FAILED, LLAMA_RS_ENCODE_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_ENCODE_VENDORED_OUT_OF_MEMORY, - LLAMA_RS_ENCODE_VENDORED_THREW_CXX_EXCEPTION, + LLAMA_RS_ENCODE_LLAMA_CPP_OUT_OF_MEMORY, + LLAMA_RS_ENCODE_LLAMA_CPP_THREW_CXX_EXCEPTION, } llama_rs_encode_status; llama_rs_encode_status llama_rs_encode( struct llama_context * ctx, struct llama_batch batch, - int32_t * out_vendored_return_code, + int32_t * out_llama_cpp_return_code, char ** out_error); typedef enum llama_rs_memory_seq_add_status { @@ -152,8 +152,8 @@ typedef enum llama_rs_memory_seq_add_status { LLAMA_RS_MEMORY_SEQ_ADD_INCOMPATIBLE_ROPE_TYPE, LLAMA_RS_MEMORY_SEQ_ADD_NULL_MEM, LLAMA_RS_MEMORY_SEQ_ADD_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_MEMORY_SEQ_ADD_VENDORED_OUT_OF_MEMORY, - LLAMA_RS_MEMORY_SEQ_ADD_VENDORED_THREW_CXX_EXCEPTION, + 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_status; llama_rs_memory_seq_add_status llama_rs_memory_seq_add( @@ -171,8 +171,8 @@ typedef enum llama_rs_memory_seq_div_status { LLAMA_RS_MEMORY_SEQ_DIV_INCOMPATIBLE_ROPE_TYPE, LLAMA_RS_MEMORY_SEQ_DIV_NULL_MEM, LLAMA_RS_MEMORY_SEQ_DIV_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_MEMORY_SEQ_DIV_VENDORED_OUT_OF_MEMORY, - LLAMA_RS_MEMORY_SEQ_DIV_VENDORED_THREW_CXX_EXCEPTION, + 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_status; llama_rs_memory_seq_div_status llama_rs_memory_seq_div( @@ -188,10 +188,10 @@ typedef enum llama_rs_load_model_from_file_status { LLAMA_RS_LOAD_MODEL_FROM_FILE_NULL_PATH_ARG, LLAMA_RS_LOAD_MODEL_FROM_FILE_NULL_OUT_MODEL_ARG, LLAMA_RS_LOAD_MODEL_FROM_FILE_NULL_OUT_ERROR_ARG, - LLAMA_RS_LOAD_MODEL_FROM_FILE_VENDORED_RETURNED_NULL, + LLAMA_RS_LOAD_MODEL_FROM_FILE_LLAMA_CPP_RETURNED_NULL, LLAMA_RS_LOAD_MODEL_FROM_FILE_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_LOAD_MODEL_FROM_FILE_VENDORED_OUT_OF_MEMORY, - LLAMA_RS_LOAD_MODEL_FROM_FILE_VENDORED_THREW_CXX_EXCEPTION, + 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_status; llama_rs_load_model_from_file_status llama_rs_load_model_from_file( @@ -205,10 +205,10 @@ typedef enum llama_rs_new_context_with_model_status { LLAMA_RS_NEW_CONTEXT_WITH_MODEL_NULL_MODEL_ARG, LLAMA_RS_NEW_CONTEXT_WITH_MODEL_NULL_OUT_CTX_ARG, LLAMA_RS_NEW_CONTEXT_WITH_MODEL_NULL_OUT_ERROR_ARG, - LLAMA_RS_NEW_CONTEXT_WITH_MODEL_VENDORED_RETURNED_NULL, + LLAMA_RS_NEW_CONTEXT_WITH_MODEL_LLAMA_CPP_RETURNED_NULL, LLAMA_RS_NEW_CONTEXT_WITH_MODEL_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_NEW_CONTEXT_WITH_MODEL_VENDORED_OUT_OF_MEMORY, - LLAMA_RS_NEW_CONTEXT_WITH_MODEL_VENDORED_THREW_CXX_EXCEPTION, + 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_status; llama_rs_new_context_with_model_status llama_rs_new_context_with_model( @@ -221,18 +221,18 @@ typedef enum llama_rs_decode_status { LLAMA_RS_DECODE_OK = 0, LLAMA_RS_DECODE_NULL_CTX_ARG, LLAMA_RS_DECODE_NULL_OUT_ERROR_ARG, - LLAMA_RS_DECODE_VENDORED_RETURNED_NONZERO_CODE, + LLAMA_RS_DECODE_LLAMA_CPP_RETURNED_NONZERO_CODE, LLAMA_RS_DECODE_OUT_OF_MEMORY, LLAMA_RS_DECODE_COMPUTE_FAILED, LLAMA_RS_DECODE_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_DECODE_VENDORED_OUT_OF_MEMORY, - LLAMA_RS_DECODE_VENDORED_THREW_CXX_EXCEPTION, + LLAMA_RS_DECODE_LLAMA_CPP_OUT_OF_MEMORY, + LLAMA_RS_DECODE_LLAMA_CPP_THREW_CXX_EXCEPTION, } llama_rs_decode_status; llama_rs_decode_status llama_rs_decode( struct llama_context * ctx, struct llama_batch batch, - int32_t * out_vendored_return_code, + int32_t * out_llama_cpp_return_code, char ** out_error); typedef enum llama_rs_tokenize_status { @@ -242,8 +242,8 @@ typedef enum llama_rs_tokenize_status { LLAMA_RS_TOKENIZE_NULL_OUT_RETURNED_COUNT_ARG, LLAMA_RS_TOKENIZE_NULL_OUT_ERROR_ARG, LLAMA_RS_TOKENIZE_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_TOKENIZE_VENDORED_OUT_OF_MEMORY, - LLAMA_RS_TOKENIZE_VENDORED_THREW_CXX_EXCEPTION, + LLAMA_RS_TOKENIZE_LLAMA_CPP_OUT_OF_MEMORY, + LLAMA_RS_TOKENIZE_LLAMA_CPP_THREW_CXX_EXCEPTION, } llama_rs_tokenize_status; llama_rs_tokenize_status llama_rs_tokenize( @@ -263,8 +263,8 @@ typedef enum llama_rs_sampler_apply_status { LLAMA_RS_SAMPLER_APPLY_NULL_DATA_ARRAY_ARG, LLAMA_RS_SAMPLER_APPLY_NULL_OUT_ERROR_ARG, LLAMA_RS_SAMPLER_APPLY_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_SAMPLER_APPLY_VENDORED_OUT_OF_MEMORY, - LLAMA_RS_SAMPLER_APPLY_VENDORED_THREW_CXX_EXCEPTION, + LLAMA_RS_SAMPLER_APPLY_LLAMA_CPP_OUT_OF_MEMORY, + LLAMA_RS_SAMPLER_APPLY_LLAMA_CPP_THREW_CXX_EXCEPTION, } llama_rs_sampler_apply_status; llama_rs_sampler_apply_status llama_rs_sampler_apply( diff --git a/llama-cpp-bindings-sys/wrapper_fit.cpp b/llama-cpp-bindings-sys/wrapper_fit.cpp index d9f0370e1..efd32ac88 100644 --- a/llama-cpp-bindings-sys/wrapper_fit.cpp +++ b/llama-cpp-bindings-sys/wrapper_fit.cpp @@ -56,40 +56,40 @@ extern "C" auto llama_rs_fit_params( } try { - common_fit_extra_model vendored_extra{}; + common_fit_extra_model llama_cpp_extra{}; if (extra != nullptr) { - vendored_extra.path_model = extra->path_model; - vendored_extra.mparams = extra->mparams; - vendored_extra.cparams = extra->cparams; - vendored_extra.shares_model = extra->shares_model; + llama_cpp_extra.path_model = extra->path_model; + llama_cpp_extra.mparams = extra->mparams; + llama_cpp_extra.cparams = extra->cparams; + llama_cpp_extra.shares_model = extra->shares_model; } const common_params_fit_status status = common_fit_params( path_model, mparams, cparams, tensor_split, tensor_buft_overrides, - margins, n_ctx_min, extra == nullptr ? nullptr : &vendored_extra, log_level); + margins, n_ctx_min, extra == nullptr ? nullptr : &llama_cpp_extra, log_level); switch (status) { case COMMON_PARAMS_FIT_STATUS_SUCCESS: return LLAMA_RS_FIT_PARAMS_OK; case COMMON_PARAMS_FIT_STATUS_FAILURE: - return LLAMA_RS_FIT_PARAMS_VENDORED_REPORTED_FAILURE; + return LLAMA_RS_FIT_PARAMS_LLAMA_CPP_REPORTED_FAILURE; case COMMON_PARAMS_FIT_STATUS_ERROR: - return LLAMA_RS_FIT_PARAMS_VENDORED_REPORTED_ERROR; + return LLAMA_RS_FIT_PARAMS_LLAMA_CPP_REPORTED_ERROR; } *out_unrecognized_status_code = static_cast(status); - return LLAMA_RS_FIT_PARAMS_VENDORED_RETURNED_UNRECOGNIZED_STATUS_CODE; + return LLAMA_RS_FIT_PARAMS_LLAMA_CPP_RETURNED_UNRECOGNIZED_STATUS_CODE; } catch (const std::bad_alloc &) { - return LLAMA_RS_FIT_PARAMS_VENDORED_OUT_OF_MEMORY; + 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_VENDORED_THREW_CXX_EXCEPTION; + 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_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_FIT_PARAMS_LLAMA_CPP_THREW_CXX_EXCEPTION; } } diff --git a/llama-cpp-bindings-sys/wrapper_fit.h b/llama-cpp-bindings-sys/wrapper_fit.h index d9e16200c..f9485cde6 100644 --- a/llama-cpp-bindings-sys/wrapper_fit.h +++ b/llama-cpp-bindings-sys/wrapper_fit.h @@ -18,12 +18,12 @@ typedef enum llama_rs_fit_params_status { LLAMA_RS_FIT_PARAMS_NULL_CPARAMS_ARG, LLAMA_RS_FIT_PARAMS_NULL_OUT_UNRECOGNIZED_STATUS_CODE_ARG, LLAMA_RS_FIT_PARAMS_NULL_OUT_ERROR_ARG, - LLAMA_RS_FIT_PARAMS_VENDORED_REPORTED_FAILURE, - LLAMA_RS_FIT_PARAMS_VENDORED_REPORTED_ERROR, - LLAMA_RS_FIT_PARAMS_VENDORED_RETURNED_UNRECOGNIZED_STATUS_CODE, + LLAMA_RS_FIT_PARAMS_LLAMA_CPP_REPORTED_FAILURE, + LLAMA_RS_FIT_PARAMS_LLAMA_CPP_REPORTED_ERROR, + LLAMA_RS_FIT_PARAMS_LLAMA_CPP_RETURNED_UNRECOGNIZED_STATUS_CODE, LLAMA_RS_FIT_PARAMS_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_FIT_PARAMS_VENDORED_OUT_OF_MEMORY, - LLAMA_RS_FIT_PARAMS_VENDORED_THREW_CXX_EXCEPTION, + LLAMA_RS_FIT_PARAMS_LLAMA_CPP_OUT_OF_MEMORY, + LLAMA_RS_FIT_PARAMS_LLAMA_CPP_THREW_CXX_EXCEPTION, LLAMA_RS_FIT_PARAMS_NULL_EXTRA_PATH_MODEL_ARG, LLAMA_RS_FIT_PARAMS_NULL_EXTRA_MPARAMS_ARG, LLAMA_RS_FIT_PARAMS_NULL_EXTRA_CPARAMS_ARG, diff --git a/llama-cpp-bindings-sys/wrapper_gbnf.cpp b/llama-cpp-bindings-sys/wrapper_gbnf.cpp index 2695e4721..e36a84eaf 100644 --- a/llama-cpp-bindings-sys/wrapper_gbnf.cpp +++ b/llama-cpp-bindings-sys/wrapper_gbnf.cpp @@ -48,7 +48,7 @@ extern "C" auto llama_rs_validate_gbnf( return LLAMA_RS_GBNF_VALIDATION_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_GBNF_VALIDATION_VENDORED_OUT_OF_MEMORY; + 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) { diff --git a/llama-cpp-bindings-sys/wrapper_gbnf.h b/llama-cpp-bindings-sys/wrapper_gbnf.h index f279ab170..20ecefab8 100644 --- a/llama-cpp-bindings-sys/wrapper_gbnf.h +++ b/llama-cpp-bindings-sys/wrapper_gbnf.h @@ -14,7 +14,7 @@ typedef enum llama_rs_gbnf_validation_status { LLAMA_RS_GBNF_VALIDATION_NULL_ROOT_ARG, LLAMA_RS_GBNF_VALIDATION_NULL_OUT_ERROR_ARG, LLAMA_RS_GBNF_VALIDATION_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_GBNF_VALIDATION_VENDORED_OUT_OF_MEMORY, + LLAMA_RS_GBNF_VALIDATION_LLAMA_CPP_OUT_OF_MEMORY, LLAMA_RS_GBNF_VALIDATION_THREW_CXX_EXCEPTION, } llama_rs_gbnf_validation_status; diff --git a/llama-cpp-bindings-sys/wrapper_mtmd.cpp b/llama-cpp-bindings-sys/wrapper_mtmd.cpp index 5a63565aa..367b0dfe2 100644 --- a/llama-cpp-bindings-sys/wrapper_mtmd.cpp +++ b/llama-cpp-bindings-sys/wrapper_mtmd.cpp @@ -33,12 +33,12 @@ extern "C" auto llama_rs_mtmd_init_from_file( try { struct mtmd_context * ctx = mtmd_init_from_file(mmproj_path, text_model, ctx_params); if (ctx == nullptr) { - return LLAMA_RS_MTMD_INIT_FROM_FILE_VENDORED_RETURNED_NULL; + return LLAMA_RS_MTMD_INIT_FROM_FILE_LLAMA_CPP_RETURNED_NULL; } *out_ctx = ctx; return LLAMA_RS_MTMD_INIT_FROM_FILE_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_MTMD_INIT_FROM_FILE_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_MTMD_INIT_FROM_FILE_LLAMA_CPP_OUT_OF_MEMORY; } catch (const std::exception & err) { if (out_error != nullptr) { *out_error = llama_rs_dup_string(err.what()); @@ -46,7 +46,7 @@ extern "C" auto llama_rs_mtmd_init_from_file( return LLAMA_RS_MTMD_INIT_FROM_FILE_ERROR_STRING_ALLOCATION_FAILED; } } - return LLAMA_RS_MTMD_INIT_FROM_FILE_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_MTMD_INIT_FROM_FILE_LLAMA_CPP_THREW_CXX_EXCEPTION; } catch (...) { if (out_error != nullptr) { *out_error = llama_rs_dup_string("unknown c++ exception"); @@ -54,12 +54,12 @@ extern "C" auto llama_rs_mtmd_init_from_file( return LLAMA_RS_MTMD_INIT_FROM_FILE_ERROR_STRING_ALLOCATION_FAILED; } } - return LLAMA_RS_MTMD_INIT_FROM_FILE_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_MTMD_INIT_FROM_FILE_LLAMA_CPP_THREW_CXX_EXCEPTION; } } extern "C" auto llama_rs_mtmd_bitmap_init_from_file( - struct mtmd_context * ctx, + const struct mtmd_context * ctx, const char * fname, struct mtmd_bitmap ** out_bitmap, char ** out_error) -> llama_rs_mtmd_bitmap_init_from_file_status { @@ -79,15 +79,15 @@ extern "C" auto llama_rs_mtmd_bitmap_init_from_file( try { struct mtmd_helper_bitmap_wrapper const bitmap_wrapper = - mtmd_helper_bitmap_init_from_file(ctx, fname, false); + mtmd_helper_bitmap_init_from_file(ctx, fname, false, mtmd_helper_init_opt_default()); struct mtmd_bitmap * bitmap = bitmap_wrapper.bitmap; if (bitmap == nullptr) { - return LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_VENDORED_RETURNED_NULL; + return LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_LLAMA_CPP_RETURNED_NULL; } *out_bitmap = bitmap; return LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_LLAMA_CPP_OUT_OF_MEMORY; } catch (const std::exception & err) { if (out_error != nullptr) { *out_error = llama_rs_dup_string(err.what()); @@ -95,7 +95,7 @@ extern "C" auto llama_rs_mtmd_bitmap_init_from_file( return LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_ERROR_STRING_ALLOCATION_FAILED; } } - return LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_LLAMA_CPP_THREW_CXX_EXCEPTION; } catch (...) { if (out_error != nullptr) { *out_error = llama_rs_dup_string("unknown c++ exception"); @@ -103,15 +103,65 @@ extern "C" auto llama_rs_mtmd_bitmap_init_from_file( return LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_ERROR_STRING_ALLOCATION_FAILED; } } - return LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_LLAMA_CPP_THREW_CXX_EXCEPTION; + } +} + +extern "C" auto llama_rs_mtmd_bitmap_init_from_buf( + const struct mtmd_context * ctx, + const unsigned char * buf, + size_t len, + struct mtmd_bitmap ** out_bitmap, + char ** out_error) -> llama_rs_mtmd_bitmap_init_from_buf_status { + if (out_error != nullptr) { + *out_error = nullptr; + } + if (out_bitmap == nullptr) { + return LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_NULL_OUT_BITMAP_ARG; + } + *out_bitmap = nullptr; + if (ctx == nullptr) { + return LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_NULL_CTX_ARG; + } + if (buf == nullptr) { + return LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_NULL_BUF_ARG; + } + + try { + struct mtmd_helper_bitmap_wrapper const bitmap_wrapper = + mtmd_helper_bitmap_init_from_buf(ctx, buf, len, false, mtmd_helper_init_opt_default()); + struct mtmd_bitmap * bitmap = bitmap_wrapper.bitmap; + if (bitmap == nullptr) { + return LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_LLAMA_CPP_RETURNED_NULL; + } + *out_bitmap = bitmap; + return LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_OK; + } catch (const std::bad_alloc &) { + return LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_LLAMA_CPP_OUT_OF_MEMORY; + } catch (const std::exception & err) { + if (out_error != nullptr) { + *out_error = llama_rs_dup_string(err.what()); + if (*out_error == nullptr) { + return LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_ERROR_STRING_ALLOCATION_FAILED; + } + } + return LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_LLAMA_CPP_THREW_CXX_EXCEPTION; + } catch (...) { + if (out_error != nullptr) { + *out_error = llama_rs_dup_string("unknown c++ exception"); + if (*out_error == nullptr) { + return LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_ERROR_STRING_ALLOCATION_FAILED; + } + } + return LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_LLAMA_CPP_THREW_CXX_EXCEPTION; } } extern "C" auto llama_rs_mtmd_tokenize( - struct mtmd_context * ctx, + const struct mtmd_context * ctx, struct mtmd_input_chunks * output, const struct mtmd_input_text * text, - const struct mtmd_bitmap ** bitmaps, + const struct mtmd_bitmap * const * bitmaps, size_t num_bitmaps, int32_t * out_undocumented_return_code, char ** out_error) -> llama_rs_mtmd_tokenize_status { @@ -140,17 +190,17 @@ extern "C" auto llama_rs_mtmd_tokenize( case 0: return LLAMA_RS_MTMD_TOKENIZE_OK; case 1: - return LLAMA_RS_MTMD_TOKENIZE_VENDORED_REPORTED_BITMAP_COUNT_DOES_NOT_MATCH_MARKER_COUNT; + return LLAMA_RS_MTMD_TOKENIZE_LLAMA_CPP_REPORTED_BITMAP_COUNT_DOES_NOT_MATCH_MARKER_COUNT; case 2: - return LLAMA_RS_MTMD_TOKENIZE_VENDORED_REPORTED_IMAGE_PREPROCESSING_ERROR; + return LLAMA_RS_MTMD_TOKENIZE_LLAMA_CPP_REPORTED_IMAGE_PREPROCESSING_ERROR; default: if (out_undocumented_return_code != nullptr) { *out_undocumented_return_code = result; } - return LLAMA_RS_MTMD_TOKENIZE_VENDORED_RETURNED_UNDOCUMENTED_NONZERO_CODE; + return LLAMA_RS_MTMD_TOKENIZE_LLAMA_CPP_RETURNED_UNDOCUMENTED_NONZERO_CODE; } } catch (const std::bad_alloc &) { - return LLAMA_RS_MTMD_TOKENIZE_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_MTMD_TOKENIZE_LLAMA_CPP_OUT_OF_MEMORY; } catch (const std::exception & err) { if (out_error != nullptr) { *out_error = llama_rs_dup_string(err.what()); @@ -158,7 +208,7 @@ extern "C" auto llama_rs_mtmd_tokenize( return LLAMA_RS_MTMD_TOKENIZE_ERROR_STRING_ALLOCATION_FAILED; } } - return LLAMA_RS_MTMD_TOKENIZE_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_MTMD_TOKENIZE_LLAMA_CPP_THREW_CXX_EXCEPTION; } catch (...) { if (out_error != nullptr) { *out_error = llama_rs_dup_string("unknown c++ exception"); @@ -166,20 +216,20 @@ extern "C" auto llama_rs_mtmd_tokenize( return LLAMA_RS_MTMD_TOKENIZE_ERROR_STRING_ALLOCATION_FAILED; } } - return LLAMA_RS_MTMD_TOKENIZE_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_MTMD_TOKENIZE_LLAMA_CPP_THREW_CXX_EXCEPTION; } } extern "C" auto llama_rs_mtmd_encode_chunk( struct mtmd_context * ctx, const struct mtmd_input_chunk * chunk, - int32_t * out_vendored_return_code, + int32_t * out_llama_cpp_return_code, char ** out_error) -> llama_rs_mtmd_encode_chunk_status { if (out_error != nullptr) { *out_error = nullptr; } - if (out_vendored_return_code != nullptr) { - *out_vendored_return_code = 0; + if (out_llama_cpp_return_code != nullptr) { + *out_llama_cpp_return_code = 0; } if (ctx == nullptr) { return LLAMA_RS_MTMD_ENCODE_CHUNK_NULL_CTX_ARG; @@ -191,14 +241,14 @@ extern "C" auto llama_rs_mtmd_encode_chunk( try { int32_t const result = mtmd_encode_chunk(ctx, chunk); if (result != 0) { - if (out_vendored_return_code != nullptr) { - *out_vendored_return_code = result; + if (out_llama_cpp_return_code != nullptr) { + *out_llama_cpp_return_code = result; } - return LLAMA_RS_MTMD_ENCODE_CHUNK_VENDORED_RETURNED_NONZERO_CODE; + return LLAMA_RS_MTMD_ENCODE_CHUNK_LLAMA_CPP_RETURNED_NONZERO_CODE; } return LLAMA_RS_MTMD_ENCODE_CHUNK_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_MTMD_ENCODE_CHUNK_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_MTMD_ENCODE_CHUNK_LLAMA_CPP_OUT_OF_MEMORY; } catch (const std::exception & err) { if (out_error != nullptr) { *out_error = llama_rs_dup_string(err.what()); @@ -206,7 +256,7 @@ extern "C" auto llama_rs_mtmd_encode_chunk( return LLAMA_RS_MTMD_ENCODE_CHUNK_ERROR_STRING_ALLOCATION_FAILED; } } - return LLAMA_RS_MTMD_ENCODE_CHUNK_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_MTMD_ENCODE_CHUNK_LLAMA_CPP_THREW_CXX_EXCEPTION; } catch (...) { if (out_error != nullptr) { *out_error = llama_rs_dup_string("unknown c++ exception"); @@ -214,7 +264,7 @@ extern "C" auto llama_rs_mtmd_encode_chunk( return LLAMA_RS_MTMD_ENCODE_CHUNK_ERROR_STRING_ALLOCATION_FAILED; } } - return LLAMA_RS_MTMD_ENCODE_CHUNK_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_MTMD_ENCODE_CHUNK_LLAMA_CPP_THREW_CXX_EXCEPTION; } } @@ -227,13 +277,13 @@ extern "C" auto llama_rs_mtmd_eval_chunk_single( int32_t n_batch, bool logits_last, llama_pos * out_new_n_past, - int32_t * out_vendored_return_code, + int32_t * out_llama_cpp_return_code, char ** out_error) -> llama_rs_mtmd_eval_chunk_single_status { if (out_error != nullptr) { *out_error = nullptr; } - if (out_vendored_return_code != nullptr) { - *out_vendored_return_code = 0; + if (out_llama_cpp_return_code != nullptr) { + *out_llama_cpp_return_code = 0; } if (ctx == nullptr) { return LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_NULL_MTMD_CTX_ARG; @@ -252,14 +302,14 @@ extern "C" auto llama_rs_mtmd_eval_chunk_single( int32_t const result = mtmd_helper_eval_chunk_single( ctx, lctx, chunk, n_past, seq_id, n_batch, logits_last, out_new_n_past); if (result != 0) { - if (out_vendored_return_code != nullptr) { - *out_vendored_return_code = result; + if (out_llama_cpp_return_code != nullptr) { + *out_llama_cpp_return_code = result; } - return LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_VENDORED_RETURNED_NONZERO_CODE; + return LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_LLAMA_CPP_RETURNED_NONZERO_CODE; } return LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_LLAMA_CPP_OUT_OF_MEMORY; } catch (const std::exception & err) { if (out_error != nullptr) { *out_error = llama_rs_dup_string(err.what()); @@ -267,7 +317,7 @@ extern "C" auto llama_rs_mtmd_eval_chunk_single( return LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_ERROR_STRING_ALLOCATION_FAILED; } } - return LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_LLAMA_CPP_THREW_CXX_EXCEPTION; } catch (...) { if (out_error != nullptr) { *out_error = llama_rs_dup_string("unknown c++ exception"); @@ -275,6 +325,6 @@ extern "C" auto llama_rs_mtmd_eval_chunk_single( return LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_ERROR_STRING_ALLOCATION_FAILED; } } - return LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_LLAMA_CPP_THREW_CXX_EXCEPTION; } } diff --git a/llama-cpp-bindings-sys/wrapper_mtmd.h b/llama-cpp-bindings-sys/wrapper_mtmd.h index 3935e12fd..108dcd271 100644 --- a/llama-cpp-bindings-sys/wrapper_mtmd.h +++ b/llama-cpp-bindings-sys/wrapper_mtmd.h @@ -16,10 +16,10 @@ typedef enum llama_rs_mtmd_init_from_file_status { LLAMA_RS_MTMD_INIT_FROM_FILE_NULL_MMPROJ_PATH_ARG, LLAMA_RS_MTMD_INIT_FROM_FILE_NULL_TEXT_MODEL_ARG, LLAMA_RS_MTMD_INIT_FROM_FILE_NULL_OUT_CTX_ARG, - LLAMA_RS_MTMD_INIT_FROM_FILE_VENDORED_RETURNED_NULL, + LLAMA_RS_MTMD_INIT_FROM_FILE_LLAMA_CPP_RETURNED_NULL, LLAMA_RS_MTMD_INIT_FROM_FILE_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_MTMD_INIT_FROM_FILE_VENDORED_OUT_OF_MEMORY, - LLAMA_RS_MTMD_INIT_FROM_FILE_VENDORED_THREW_CXX_EXCEPTION, + LLAMA_RS_MTMD_INIT_FROM_FILE_LLAMA_CPP_OUT_OF_MEMORY, + LLAMA_RS_MTMD_INIT_FROM_FILE_LLAMA_CPP_THREW_CXX_EXCEPTION, } llama_rs_mtmd_init_from_file_status; llama_rs_mtmd_init_from_file_status llama_rs_mtmd_init_from_file( @@ -34,37 +34,55 @@ typedef enum llama_rs_mtmd_bitmap_init_from_file_status { LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_NULL_CTX_ARG, LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_NULL_FNAME_ARG, LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_NULL_OUT_BITMAP_ARG, - LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_VENDORED_RETURNED_NULL, + LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_LLAMA_CPP_RETURNED_NULL, LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_VENDORED_OUT_OF_MEMORY, - LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_VENDORED_THREW_CXX_EXCEPTION, + LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_LLAMA_CPP_OUT_OF_MEMORY, + LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_LLAMA_CPP_THREW_CXX_EXCEPTION, } llama_rs_mtmd_bitmap_init_from_file_status; llama_rs_mtmd_bitmap_init_from_file_status llama_rs_mtmd_bitmap_init_from_file( - struct mtmd_context * ctx, + const struct mtmd_context * ctx, const char * fname, struct mtmd_bitmap ** out_bitmap, char ** out_error); +typedef enum llama_rs_mtmd_bitmap_init_from_buf_status { + LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_OK = 0, + LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_NULL_CTX_ARG, + LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_NULL_BUF_ARG, + LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_NULL_OUT_BITMAP_ARG, + LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_LLAMA_CPP_RETURNED_NULL, + LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_ERROR_STRING_ALLOCATION_FAILED, + LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_LLAMA_CPP_OUT_OF_MEMORY, + LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_LLAMA_CPP_THREW_CXX_EXCEPTION, +} llama_rs_mtmd_bitmap_init_from_buf_status; + +llama_rs_mtmd_bitmap_init_from_buf_status llama_rs_mtmd_bitmap_init_from_buf( + const struct mtmd_context * ctx, + const unsigned char * buf, + size_t len, + struct mtmd_bitmap ** out_bitmap, + char ** out_error); + typedef enum llama_rs_mtmd_tokenize_status { LLAMA_RS_MTMD_TOKENIZE_OK = 0, LLAMA_RS_MTMD_TOKENIZE_NULL_CTX_ARG, LLAMA_RS_MTMD_TOKENIZE_NULL_OUTPUT_ARG, LLAMA_RS_MTMD_TOKENIZE_NULL_TEXT_ARG, LLAMA_RS_MTMD_TOKENIZE_NULL_BITMAPS_ARG_WHEN_NUM_BITMAPS_NONZERO, - LLAMA_RS_MTMD_TOKENIZE_VENDORED_REPORTED_BITMAP_COUNT_DOES_NOT_MATCH_MARKER_COUNT, - LLAMA_RS_MTMD_TOKENIZE_VENDORED_REPORTED_IMAGE_PREPROCESSING_ERROR, - LLAMA_RS_MTMD_TOKENIZE_VENDORED_RETURNED_UNDOCUMENTED_NONZERO_CODE, + LLAMA_RS_MTMD_TOKENIZE_LLAMA_CPP_REPORTED_BITMAP_COUNT_DOES_NOT_MATCH_MARKER_COUNT, + LLAMA_RS_MTMD_TOKENIZE_LLAMA_CPP_REPORTED_IMAGE_PREPROCESSING_ERROR, + LLAMA_RS_MTMD_TOKENIZE_LLAMA_CPP_RETURNED_UNDOCUMENTED_NONZERO_CODE, LLAMA_RS_MTMD_TOKENIZE_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_MTMD_TOKENIZE_VENDORED_OUT_OF_MEMORY, - LLAMA_RS_MTMD_TOKENIZE_VENDORED_THREW_CXX_EXCEPTION, + LLAMA_RS_MTMD_TOKENIZE_LLAMA_CPP_OUT_OF_MEMORY, + LLAMA_RS_MTMD_TOKENIZE_LLAMA_CPP_THREW_CXX_EXCEPTION, } llama_rs_mtmd_tokenize_status; llama_rs_mtmd_tokenize_status llama_rs_mtmd_tokenize( - struct mtmd_context * ctx, + const struct mtmd_context * ctx, struct mtmd_input_chunks * output, const struct mtmd_input_text * text, - const struct mtmd_bitmap ** bitmaps, + const struct mtmd_bitmap * const * bitmaps, size_t num_bitmaps, int32_t * out_undocumented_return_code, char ** out_error); @@ -73,16 +91,16 @@ typedef enum llama_rs_mtmd_encode_chunk_status { LLAMA_RS_MTMD_ENCODE_CHUNK_OK = 0, LLAMA_RS_MTMD_ENCODE_CHUNK_NULL_CTX_ARG, LLAMA_RS_MTMD_ENCODE_CHUNK_NULL_CHUNK_ARG, - LLAMA_RS_MTMD_ENCODE_CHUNK_VENDORED_RETURNED_NONZERO_CODE, + LLAMA_RS_MTMD_ENCODE_CHUNK_LLAMA_CPP_RETURNED_NONZERO_CODE, LLAMA_RS_MTMD_ENCODE_CHUNK_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_MTMD_ENCODE_CHUNK_VENDORED_OUT_OF_MEMORY, - LLAMA_RS_MTMD_ENCODE_CHUNK_VENDORED_THREW_CXX_EXCEPTION, + LLAMA_RS_MTMD_ENCODE_CHUNK_LLAMA_CPP_OUT_OF_MEMORY, + LLAMA_RS_MTMD_ENCODE_CHUNK_LLAMA_CPP_THREW_CXX_EXCEPTION, } llama_rs_mtmd_encode_chunk_status; llama_rs_mtmd_encode_chunk_status llama_rs_mtmd_encode_chunk( struct mtmd_context * ctx, const struct mtmd_input_chunk * chunk, - int32_t * out_vendored_return_code, + int32_t * out_llama_cpp_return_code, char ** out_error); typedef enum llama_rs_mtmd_eval_chunk_single_status { @@ -91,10 +109,10 @@ typedef enum llama_rs_mtmd_eval_chunk_single_status { LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_NULL_LLAMA_CTX_ARG, LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_NULL_CHUNK_ARG, LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_NULL_OUT_NEW_N_PAST_ARG, - LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_VENDORED_RETURNED_NONZERO_CODE, + LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_LLAMA_CPP_RETURNED_NONZERO_CODE, LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_VENDORED_OUT_OF_MEMORY, - LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_VENDORED_THREW_CXX_EXCEPTION, + LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_LLAMA_CPP_OUT_OF_MEMORY, + LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_LLAMA_CPP_THREW_CXX_EXCEPTION, } llama_rs_mtmd_eval_chunk_single_status; llama_rs_mtmd_eval_chunk_single_status llama_rs_mtmd_eval_chunk_single( @@ -106,7 +124,7 @@ llama_rs_mtmd_eval_chunk_single_status llama_rs_mtmd_eval_chunk_single( int32_t n_batch, bool logits_last, llama_pos * out_new_n_past, - int32_t * out_vendored_return_code, + int32_t * out_llama_cpp_return_code, char ** out_error); #ifdef __cplusplus diff --git a/llama-cpp-bindings-sys/wrapper_reasoning.cpp b/llama-cpp-bindings-sys/wrapper_reasoning.cpp index 963301157..a5f847bc4 100644 --- a/llama-cpp-bindings-sys/wrapper_reasoning.cpp +++ b/llama-cpp-bindings-sys/wrapper_reasoning.cpp @@ -110,19 +110,19 @@ extern "C" auto llama_rs_detect_reasoning_markers( return LLAMA_RS_DETECT_REASONING_MARKERS_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_DETECT_REASONING_MARKERS_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_DETECT_REASONING_MARKERS_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_DETECT_REASONING_MARKERS_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_DETECT_REASONING_MARKERS_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_DETECT_REASONING_MARKERS_LLAMA_CPP_THREW_CXX_EXCEPTION; } catch (...) { *out_error = llama_rs_dup_string(std::string("unknown c++ exception")); if (*out_error == nullptr) { return LLAMA_RS_DETECT_REASONING_MARKERS_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_DETECT_REASONING_MARKERS_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_DETECT_REASONING_MARKERS_LLAMA_CPP_THREW_CXX_EXCEPTION; } } @@ -161,7 +161,7 @@ extern "C" auto llama_rs_reasoning_markers_free( const std::unique_ptr reclaimed(markers); return LLAMA_RS_REASONING_MARKERS_FREE_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_REASONING_MARKERS_FREE_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_REASONING_MARKERS_FREE_LLAMA_CPP_OUT_OF_MEMORY; } catch (const std::exception & err) { if (out_error != nullptr) { *out_error = llama_rs_dup_string(err.what()); diff --git a/llama-cpp-bindings-sys/wrapper_reasoning.h b/llama-cpp-bindings-sys/wrapper_reasoning.h index ff94141a3..9370c4323 100644 --- a/llama-cpp-bindings-sys/wrapper_reasoning.h +++ b/llama-cpp-bindings-sys/wrapper_reasoning.h @@ -17,8 +17,8 @@ typedef enum llama_rs_detect_reasoning_markers_status { LLAMA_RS_DETECT_REASONING_MARKERS_MODEL_HAS_NO_CHAT_TEMPLATE, LLAMA_RS_DETECT_REASONING_MARKERS_MODEL_HAS_NO_VOCAB, LLAMA_RS_DETECT_REASONING_MARKERS_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_DETECT_REASONING_MARKERS_VENDORED_OUT_OF_MEMORY, - LLAMA_RS_DETECT_REASONING_MARKERS_VENDORED_THREW_CXX_EXCEPTION, + LLAMA_RS_DETECT_REASONING_MARKERS_LLAMA_CPP_OUT_OF_MEMORY, + LLAMA_RS_DETECT_REASONING_MARKERS_LLAMA_CPP_THREW_CXX_EXCEPTION, } llama_rs_detect_reasoning_markers_status; typedef struct llama_rs_reasoning_markers llama_rs_reasoning_markers; @@ -36,7 +36,7 @@ const char * llama_rs_reasoning_markers_close_at( typedef enum llama_rs_reasoning_markers_free_status { LLAMA_RS_REASONING_MARKERS_FREE_OK = 0, LLAMA_RS_REASONING_MARKERS_FREE_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_REASONING_MARKERS_FREE_VENDORED_OUT_OF_MEMORY, + LLAMA_RS_REASONING_MARKERS_FREE_LLAMA_CPP_OUT_OF_MEMORY, LLAMA_RS_REASONING_MARKERS_FREE_DESTRUCTOR_THREW_CXX_EXCEPTION, } llama_rs_reasoning_markers_free_status; diff --git a/llama-cpp-bindings-sys/wrapper_state.cpp b/llama-cpp-bindings-sys/wrapper_state.cpp index f1e8e4bd5..f40919868 100644 --- a/llama-cpp-bindings-sys/wrapper_state.cpp +++ b/llama-cpp-bindings-sys/wrapper_state.cpp @@ -17,7 +17,7 @@ auto describe_failure(char ** out_error, const char * message) -> llama_rs_state return LLAMA_RS_STATE_DATA_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_STATE_DATA_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_STATE_DATA_LLAMA_CPP_THREW_CXX_EXCEPTION; } auto validate_arguments( @@ -67,7 +67,7 @@ extern "C" auto llama_rs_state_get_data( return LLAMA_RS_STATE_DATA_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_STATE_DATA_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_STATE_DATA_LLAMA_CPP_OUT_OF_MEMORY; } catch (const std::exception & err) { return describe_failure(out_error, err.what()); } catch (...) { @@ -99,7 +99,7 @@ extern "C" auto llama_rs_state_set_data( return LLAMA_RS_STATE_DATA_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_STATE_DATA_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_STATE_DATA_LLAMA_CPP_OUT_OF_MEMORY; } catch (const std::exception & err) { return describe_failure(out_error, err.what()); } catch (...) { @@ -133,7 +133,7 @@ extern "C" auto llama_rs_state_seq_get_data( return LLAMA_RS_STATE_DATA_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_STATE_DATA_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_STATE_DATA_LLAMA_CPP_OUT_OF_MEMORY; } catch (const std::exception & err) { return describe_failure(out_error, err.what()); } catch (...) { @@ -167,7 +167,7 @@ extern "C" auto llama_rs_state_seq_set_data( return LLAMA_RS_STATE_DATA_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_STATE_DATA_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_STATE_DATA_LLAMA_CPP_OUT_OF_MEMORY; } catch (const std::exception & err) { return describe_failure(out_error, err.what()); } catch (...) { diff --git a/llama-cpp-bindings-sys/wrapper_state.h b/llama-cpp-bindings-sys/wrapper_state.h index 1512bae87..4f76fe630 100644 --- a/llama-cpp-bindings-sys/wrapper_state.h +++ b/llama-cpp-bindings-sys/wrapper_state.h @@ -16,8 +16,8 @@ typedef enum llama_rs_state_data_status { LLAMA_RS_STATE_DATA_NULL_OUT_BYTE_COUNT_ARG, LLAMA_RS_STATE_DATA_NULL_OUT_ERROR_ARG, LLAMA_RS_STATE_DATA_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_STATE_DATA_VENDORED_OUT_OF_MEMORY, - LLAMA_RS_STATE_DATA_VENDORED_THREW_CXX_EXCEPTION, + LLAMA_RS_STATE_DATA_LLAMA_CPP_OUT_OF_MEMORY, + LLAMA_RS_STATE_DATA_LLAMA_CPP_THREW_CXX_EXCEPTION, } llama_rs_state_data_status; llama_rs_state_data_status llama_rs_state_get_data( diff --git a/llama-cpp-bindings-sys/wrapper_tool_calls.cpp b/llama-cpp-bindings-sys/wrapper_tool_calls.cpp index e8f0f00cb..e42c0e64a 100644 --- a/llama-cpp-bindings-sys/wrapper_tool_calls.cpp +++ b/llama-cpp-bindings-sys/wrapper_tool_calls.cpp @@ -160,19 +160,19 @@ extern "C" auto llama_rs_compute_tool_call_haystack( return LLAMA_RS_COMPUTE_TOOL_CALL_HAYSTACK_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_COMPUTE_TOOL_CALL_HAYSTACK_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_COMPUTE_TOOL_CALL_HAYSTACK_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_COMPUTE_TOOL_CALL_HAYSTACK_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_COMPUTE_TOOL_CALL_HAYSTACK_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_COMPUTE_TOOL_CALL_HAYSTACK_LLAMA_CPP_THREW_CXX_EXCEPTION; } catch (...) { *out_error = llama_rs_dup_string(std::string("unknown c++ exception")); if (*out_error == nullptr) { return LLAMA_RS_COMPUTE_TOOL_CALL_HAYSTACK_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_COMPUTE_TOOL_CALL_HAYSTACK_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_COMPUTE_TOOL_CALL_HAYSTACK_LLAMA_CPP_THREW_CXX_EXCEPTION; } } @@ -236,18 +236,18 @@ extern "C" auto llama_rs_diagnose_tool_call_synthetic_renders( return LLAMA_RS_DIAGNOSE_TOOL_CALL_SYNTHETIC_RENDERS_OK; } catch (const std::bad_alloc &) { - return LLAMA_RS_DIAGNOSE_TOOL_CALL_SYNTHETIC_RENDERS_VENDORED_OUT_OF_MEMORY; + return LLAMA_RS_DIAGNOSE_TOOL_CALL_SYNTHETIC_RENDERS_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_DIAGNOSE_TOOL_CALL_SYNTHETIC_RENDERS_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_DIAGNOSE_TOOL_CALL_SYNTHETIC_RENDERS_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_DIAGNOSE_TOOL_CALL_SYNTHETIC_RENDERS_LLAMA_CPP_THREW_CXX_EXCEPTION; } catch (...) { *out_error = llama_rs_dup_string(std::string("unknown c++ exception")); if (*out_error == nullptr) { return LLAMA_RS_DIAGNOSE_TOOL_CALL_SYNTHETIC_RENDERS_ERROR_STRING_ALLOCATION_FAILED; } - return LLAMA_RS_DIAGNOSE_TOOL_CALL_SYNTHETIC_RENDERS_VENDORED_THREW_CXX_EXCEPTION; + return LLAMA_RS_DIAGNOSE_TOOL_CALL_SYNTHETIC_RENDERS_LLAMA_CPP_THREW_CXX_EXCEPTION; } } diff --git a/llama-cpp-bindings-sys/wrapper_tool_calls.h b/llama-cpp-bindings-sys/wrapper_tool_calls.h index 45a18a448..fce2c366c 100644 --- a/llama-cpp-bindings-sys/wrapper_tool_calls.h +++ b/llama-cpp-bindings-sys/wrapper_tool_calls.h @@ -15,8 +15,8 @@ typedef enum llama_rs_compute_tool_call_haystack_status { LLAMA_RS_COMPUTE_TOOL_CALL_HAYSTACK_MODEL_HAS_NO_CHAT_TEMPLATE, LLAMA_RS_COMPUTE_TOOL_CALL_HAYSTACK_MODEL_HAS_NO_VOCAB, LLAMA_RS_COMPUTE_TOOL_CALL_HAYSTACK_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_COMPUTE_TOOL_CALL_HAYSTACK_VENDORED_OUT_OF_MEMORY, - LLAMA_RS_COMPUTE_TOOL_CALL_HAYSTACK_VENDORED_THREW_CXX_EXCEPTION, + LLAMA_RS_COMPUTE_TOOL_CALL_HAYSTACK_LLAMA_CPP_OUT_OF_MEMORY, + LLAMA_RS_COMPUTE_TOOL_CALL_HAYSTACK_LLAMA_CPP_THREW_CXX_EXCEPTION, } llama_rs_compute_tool_call_haystack_status; llama_rs_compute_tool_call_haystack_status llama_rs_compute_tool_call_haystack( @@ -33,8 +33,8 @@ typedef enum llama_rs_diagnose_tool_call_synthetic_renders_status { LLAMA_RS_DIAGNOSE_TOOL_CALL_SYNTHETIC_RENDERS_MODEL_HAS_NO_CHAT_TEMPLATE, LLAMA_RS_DIAGNOSE_TOOL_CALL_SYNTHETIC_RENDERS_MODEL_HAS_NO_VOCAB, LLAMA_RS_DIAGNOSE_TOOL_CALL_SYNTHETIC_RENDERS_ERROR_STRING_ALLOCATION_FAILED, - LLAMA_RS_DIAGNOSE_TOOL_CALL_SYNTHETIC_RENDERS_VENDORED_OUT_OF_MEMORY, - LLAMA_RS_DIAGNOSE_TOOL_CALL_SYNTHETIC_RENDERS_VENDORED_THREW_CXX_EXCEPTION, + LLAMA_RS_DIAGNOSE_TOOL_CALL_SYNTHETIC_RENDERS_LLAMA_CPP_OUT_OF_MEMORY, + LLAMA_RS_DIAGNOSE_TOOL_CALL_SYNTHETIC_RENDERS_LLAMA_CPP_THREW_CXX_EXCEPTION, } llama_rs_diagnose_tool_call_synthetic_renders_status; llama_rs_diagnose_tool_call_synthetic_renders_status llama_rs_diagnose_tool_call_synthetic_renders( diff --git a/llama-cpp-bindings-tests/tests/context_state.rs b/llama-cpp-bindings-tests/tests/context_state.rs index 6a52a4dac..b9efaac74 100644 --- a/llama-cpp-bindings-tests/tests/context_state.rs +++ b/llama-cpp-bindings-tests/tests/context_state.rs @@ -6,7 +6,8 @@ use anyhow::Result; use llama_cpp_bindings::DecodeError; use llama_cpp_bindings::LogitsError; use llama_cpp_bindings::context::LlamaContext; -use llama_cpp_bindings::error::KvCacheConversionError; +use llama_cpp_bindings::error::ClearKvCacheSeqError; +use llama_cpp_bindings::error::CopyKvCacheSeqError; use llama_cpp_bindings::error::KvCacheSeqAddError; use llama_cpp_bindings::error::KvCacheSeqDivError; use llama_cpp_bindings::llama_batch::LlamaBatch; @@ -726,7 +727,7 @@ fn clear_kv_cache_resets_positions(fixture: &LlamaFixture<'_>) -> Result<()> { prime_kv_cache(fixture, &mut context)?; - context.clear_kv_cache()?; + context.clear_kv_cache(); assert_eq!(context.kv_cache_seq_pos_max(0)?, -1); Ok(()) @@ -1000,7 +1001,7 @@ fn kv_cache_seq_keep_retains_specified_sequence(fixture: &LlamaFixture<'_>) -> R prime_kv_cache(fixture, &mut context)?; - context.kv_cache_seq_keep(0)?; + context.kv_cache_seq_keep(0); assert!(context.kv_cache_seq_pos_max(0)? >= 0); @@ -1133,7 +1134,7 @@ fn copy_kv_cache_seq_rejects_p0_exceeding_i32_max(fixture: &LlamaFixture<'_>) -> assert_eq!( result.unwrap_err(), - KvCacheConversionError::P0TooLarge( + CopyKvCacheSeqError::P0TooLarge( i32::try_from(u32::MAX).expect_err("u32::MAX does not fit into i32") ) ); @@ -1180,7 +1181,7 @@ fn copy_kv_cache_seq_rejects_p1_exceeding_i32_max(fixture: &LlamaFixture<'_>) -> assert_eq!( result.unwrap_err(), - KvCacheConversionError::P1TooLarge( + CopyKvCacheSeqError::P1TooLarge( i32::try_from(u32::MAX).expect_err("u32::MAX does not fit into i32") ) ); @@ -1220,14 +1221,16 @@ fn copy_kv_cache_seq_rejects_p1_exceeding_i32_max(fixture: &LlamaFixture<'_>) -> n_batch = 512, n_ubatch = 128, )] -fn clear_kv_cache_seq_rejects_src_exceeding_i32_max(fixture: &LlamaFixture<'_>) -> Result<()> { +fn clear_kv_cache_seq_rejects_sequence_id_exceeding_i32_max( + fixture: &LlamaFixture<'_>, +) -> Result<()> { let mut context = fixture.build_context()?; let result = context.clear_kv_cache_seq(Some(u32::MAX), None, None); assert_eq!( result.unwrap_err(), - KvCacheConversionError::SeqIdTooLarge( + ClearKvCacheSeqError::SeqIdTooLarge( i32::try_from(u32::MAX).expect_err("u32::MAX does not fit into i32") ) ); @@ -1274,7 +1277,7 @@ fn clear_kv_cache_seq_rejects_p0_exceeding_i32_max(fixture: &LlamaFixture<'_>) - assert_eq!( result.unwrap_err(), - KvCacheConversionError::P0TooLarge( + ClearKvCacheSeqError::P0TooLarge( i32::try_from(u32::MAX).expect_err("u32::MAX does not fit into i32") ) ); @@ -1321,7 +1324,7 @@ fn clear_kv_cache_seq_rejects_p1_exceeding_i32_max(fixture: &LlamaFixture<'_>) - assert_eq!( result.unwrap_err(), - KvCacheConversionError::P1TooLarge( + ClearKvCacheSeqError::P1TooLarge( i32::try_from(u32::MAX).expect_err("u32::MAX does not fit into i32") ) ); diff --git a/llama-cpp-bindings-tests/tests/embedding_models.rs b/llama-cpp-bindings-tests/tests/embedding_models.rs index 98f9ec9ad..12a8d0742 100644 --- a/llama-cpp-bindings-tests/tests/embedding_models.rs +++ b/llama-cpp-bindings-tests/tests/embedding_models.rs @@ -4,6 +4,11 @@ use std::time::Duration; use anyhow::Context; use anyhow::Result; use anyhow::bail; +use llama_cpp_bindings::ClearKvCacheSeqError; +use llama_cpp_bindings::CopyKvCacheSeqError; +use llama_cpp_bindings::KvCacheSeqAddError; +use llama_cpp_bindings::KvCacheSeqDivError; +use llama_cpp_bindings::KvCacheSeqPosMaxError; use llama_cpp_bindings::context::LlamaContext; use llama_cpp_bindings::ggml_time_us; use llama_cpp_bindings::llama_batch::LlamaBatch; @@ -67,7 +72,7 @@ fn embedding_generation_produces_vectors(fixture: &LlamaFixture<'_>) -> Result<( assert_eq!(classifier.pending_prompt_tokens(), prompt_token_count); assert_eq!(classifier.usage().prompt_tokens, 0); - ctx.clear_kv_cache()?; + ctx.clear_kv_cache(); ctx.decode(&mut batch) .with_context(|| "llama_decode() failed")?; @@ -180,7 +185,7 @@ fn reranking_produces_scores(fixture: &LlamaFixture<'_>) -> Result<()> { assert_eq!(classifier.pending_prompt_tokens(), total_token_count); assert_eq!(classifier.usage().prompt_tokens, 0); - ctx.clear_kv_cache()?; + ctx.clear_kv_cache(); ctx.decode(&mut batch) .with_context(|| "llama_decode() failed")?; @@ -393,7 +398,7 @@ fn embeddings_returns_distinct_values_when_reused_batch_has_extra_capacity( batch.add_sequence(&tokens, sequence_id, true)?; } - context.clear_kv_cache()?; + context.clear_kv_cache(); context.decode(&mut batch)?; for sequence_index in 0..iteration_inputs.len() { @@ -581,7 +586,51 @@ fn embedding_model_exposes_tool_call_markers(fixture: &LlamaFixture<'_>) -> Resu fn embedding_model_exposes_streaming_markers(fixture: &LlamaFixture<'_>) -> Result<()> { let markers = fixture.model.streaming_markers()?; - assert!(markers.has_any()); + assert!(!markers.is_empty()); + + Ok(()) +} + +#[llama_test( + model_source = HuggingFace("nomic-ai/nomic-embed-text-v1.5-GGUF", "nomic-embed-text-v1.5.Q4_K_M.gguf"), + n_gpu_layers = 999, + load_mode = Mmap, + n_ctx = 512, + n_batch = 2048, + n_ubatch = 512, + n_threads_batch = 8, + embeddings = true, +)] +fn kv_cache_operations_respect_an_embedding_context_without_memory( + fixture: &LlamaFixture<'_>, +) -> Result<()> { + let mut context = fixture.build_context()?; + + context.clear_kv_cache(); + assert_eq!(context.clear_kv_cache_seq(Some(0), None, None), Ok(())); + context.kv_cache_seq_keep(0); + + assert_eq!( + context.copy_kv_cache_seq(0, 1, None, None), + Err(CopyKvCacheSeqError::MemoryHandleUnavailable) + ); + assert_eq!( + context.kv_cache_seq_add(0, None, None, 1), + Err(KvCacheSeqAddError::MemoryHandleUnavailable) + ); + let divisor = NonZeroU8::new(2).ok_or_else(|| anyhow::anyhow!("2 is non-zero"))?; + assert_eq!( + context.kv_cache_seq_div(0, None, None, divisor), + Err(KvCacheSeqDivError::MemoryHandleUnavailable) + ); + assert_eq!( + context.kv_cache_seq_pos_max(0), + Err(KvCacheSeqPosMaxError::MemoryHandleUnavailable) + ); + assert!(matches!( + context.clear_kv_cache_seq(Some(u32::MAX), None, None), + Err(ClearKvCacheSeqError::SeqIdTooLarge(_)) + )); Ok(()) } diff --git a/llama-cpp-bindings-tests/tests/generation_control.rs b/llama-cpp-bindings-tests/tests/generation_control.rs index ccd7342e0..2fe178733 100644 --- a/llama-cpp-bindings-tests/tests/generation_control.rs +++ b/llama-cpp-bindings-tests/tests/generation_control.rs @@ -15,10 +15,8 @@ use llama_cpp_bindings::llama_batch::LlamaBatch; use llama_cpp_bindings::llguidance_sampler::create_llg_sampler; use llama_cpp_bindings::model::AddBos; use llama_cpp_bindings::model::LlamaChatMessage; -use llama_cpp_bindings::sampled_token_classifier::SampledTokenClassifier; use llama_cpp_bindings::sampled_token_section::SampledTokenSection; use llama_cpp_bindings::sampling::LlamaSampler; -use llama_cpp_bindings::streaming_markers::StreamingMarkers; use llama_cpp_bindings::token::LlamaToken; use llama_cpp_bindings_tests::classify_sample_loop::ClassifySampleLoop; use llama_cpp_test_harness::LlamaFixture; @@ -1903,13 +1901,14 @@ fn classifier_construction_is_idempotent_across_calls(fixture: &LlamaFixture<'_> n_batch = 128, n_ubatch = 64, )] -fn ingest_with_no_markers_emits_undeterminable_with_visible_and_raw_piece( +fn ingest_flushes_an_unmatched_token_with_its_visible_and_raw_piece( fixture: &LlamaFixture<'_>, ) -> Result<()> { let model = fixture.model; - let mut classifier = SampledTokenClassifier::new(model, StreamingMarkers::default()); + let mut classifier = model.sampled_token_classifier()?; - let outcomes = classifier.ingest(model.token_bos())?; + let mut outcomes = classifier.ingest(model.token_bos())?; + outcomes.extend(classifier.flush()); assert_eq!(outcomes.len(), 1); let outcome = &outcomes[0]; @@ -1954,14 +1953,13 @@ fn ingest_with_no_markers_emits_undeterminable_with_visible_and_raw_piece( n_batch = 128, n_ubatch = 64, )] -fn ingest_with_no_markers_decodes_each_token_independently( - fixture: &LlamaFixture<'_>, -) -> Result<()> { +fn ingest_accounts_for_each_unmatched_token_after_flush(fixture: &LlamaFixture<'_>) -> Result<()> { let model = fixture.model; - let mut classifier = SampledTokenClassifier::new(model, StreamingMarkers::default()); + let mut classifier = model.sampled_token_classifier()?; classifier.ingest(model.token_bos())?; classifier.ingest(model.token_eos())?; + classifier.flush(); assert_eq!(classifier.usage().undeterminable_tokens, 2); Ok(()) @@ -1999,9 +1997,9 @@ fn ingest_with_no_markers_decodes_each_token_independently( n_batch = 128, n_ubatch = 64, )] -fn ingest_prompt_token_with_no_markers_is_a_noop(fixture: &LlamaFixture<'_>) -> Result<()> { +fn ingest_unmatched_prompt_tokens_does_not_record_usage(fixture: &LlamaFixture<'_>) -> Result<()> { let model = fixture.model; - let mut classifier = SampledTokenClassifier::new(model, StreamingMarkers::default()); + let mut classifier = model.sampled_token_classifier()?; let usage_before = *classifier.usage(); classifier.ingest_prompt_token(model.token_bos()); @@ -2046,7 +2044,7 @@ fn ingest_prompt_token_with_no_markers_is_a_noop(fixture: &LlamaFixture<'_>) -> )] fn feed_prompt_to_batch_increments_pending_prompt_tokens(fixture: &LlamaFixture<'_>) -> Result<()> { let model = fixture.model; - let mut classifier = SampledTokenClassifier::new(model, StreamingMarkers::default()); + let mut classifier = model.sampled_token_classifier()?; let mut batch = LlamaBatch::new(8, 1)?; classifier.feed_prompt_to_batch(&mut batch, model.token_bos(), 0, &[0], false)?; @@ -2092,7 +2090,7 @@ fn feed_prompt_to_batch_increments_pending_prompt_tokens(fixture: &LlamaFixture< )] fn feed_prompt_sequence_to_batch_stages_all_tokens(fixture: &LlamaFixture<'_>) -> Result<()> { let model = fixture.model; - let mut classifier = SampledTokenClassifier::new(model, StreamingMarkers::default()); + let mut classifier = model.sampled_token_classifier()?; let mut batch = LlamaBatch::new(8, 1)?; let tokens = vec![model.token_bos(), model.token_eos(), model.token_nl()]; @@ -2140,7 +2138,7 @@ fn commit_prompt_tokens_promotes_pending_count_to_usage_and_clears( fixture: &LlamaFixture<'_>, ) -> Result<()> { let model = fixture.model; - let mut classifier = SampledTokenClassifier::new(model, StreamingMarkers::default()); + let mut classifier = model.sampled_token_classifier()?; let mut batch = LlamaBatch::new(8, 1)?; classifier.feed_prompt_to_batch(&mut batch, model.token_bos(), 0, &[0], false)?; @@ -2191,7 +2189,7 @@ fn discard_pending_prompt_tokens_clears_count_without_recording_usage( fixture: &LlamaFixture<'_>, ) -> Result<()> { let model = fixture.model; - let mut classifier = SampledTokenClassifier::new(model, StreamingMarkers::default()); + let mut classifier = model.sampled_token_classifier()?; let mut batch = LlamaBatch::new(8, 1)?; classifier.feed_prompt_to_batch(&mut batch, model.token_bos(), 0, &[0], false)?; diff --git a/llama-cpp-bindings-tests/tests/model_introspection.rs b/llama-cpp-bindings-tests/tests/model_introspection.rs index ca691cbd3..c04c50ed3 100644 --- a/llama-cpp-bindings-tests/tests/model_introspection.rs +++ b/llama-cpp-bindings-tests/tests/model_introspection.rs @@ -803,7 +803,7 @@ fn fit_params_aligns_the_extra_model_context_with_the_fitted_one( assert_ne!( extra_context_params.n_ctx(), Some(unfittable_extra_n_ctx), - "the vendored fit must overwrite the extra model's context size" + "the llama.cpp fit must overwrite the extra model's context size" ); assert_eq!( extra_context_params.context_params.n_ctx, diff --git a/llama-cpp-bindings-tests/tests/structured_chat_output.rs b/llama-cpp-bindings-tests/tests/structured_chat_output.rs index f7754d8f6..5ffb33628 100644 --- a/llama-cpp-bindings-tests/tests/structured_chat_output.rs +++ b/llama-cpp-bindings-tests/tests/structured_chat_output.rs @@ -1,7 +1,9 @@ use anyhow::Result; use anyhow::bail; use llama_cpp_bindings::ChatMessageParseOutcome; +use llama_cpp_bindings::MarkerRole; use llama_cpp_bindings::ParsedChatMessage; +use llama_cpp_bindings::SampledTokenSection; use llama_cpp_bindings::TokenUsage; use llama_cpp_bindings::ToolCallArgsShape; use llama_cpp_bindings::ToolCallArguments; @@ -1465,7 +1467,7 @@ fn qwen35_chat_inference_emits_reasoning_when_template_auto_opens( n_batch = 512, n_ubatch = 128, )] -fn qwen35_streaming_markers_tokenize_every_reasoning_boundary( +fn qwen35_shared_reasoning_close_and_tool_call_open_is_one_transition( fixture: &LlamaFixture<'_>, ) -> Result<()> { let reasoning_markers = fixture @@ -1474,18 +1476,40 @@ fn qwen35_streaming_markers_tokenize_every_reasoning_boundary( .expect("Qwen3.5 must expose reasoning markers"); let streaming_markers = fixture.model.streaming_markers()?; - assert!(streaming_markers.reasoning_open.is_some()); + assert!(streaming_markers.iter().any(|marker| { + marker.roles().contains(&MarkerRole::ReasoningOpen) && !marker.tokens().is_empty() + })); assert_eq!( - streaming_markers.reasoning_closes.len(), - reasoning_markers.closes.len() - ); - assert!( streaming_markers - .reasoning_closes .iter() - .all(|tokens| !tokens.is_empty()) + .filter(|marker| marker.roles().contains(&MarkerRole::ReasoningClose)) + .count(), + reasoning_markers.closes.len() ); + let reasoning_open = streaming_markers + .iter() + .find(|marker| marker.roles().contains(&MarkerRole::ReasoningOpen)) + .expect("Qwen3.5 must expose a reasoning opener") + .tokens() + .to_vec(); + let shared_boundary = streaming_markers + .iter() + .find(|marker| { + marker.roles().contains(&MarkerRole::ReasoningClose) + && marker.roles().contains(&MarkerRole::ToolCallOpen) + }) + .expect("Qwen3.5 must share its tool-call opener with a reasoning close") + .tokens() + .to_vec(); + + let mut classifier = fixture.model.sampled_token_classifier()?; + classifier.ingest_prompt_tokens(&reasoning_open); + assert_eq!(classifier.current_section(), SampledTokenSection::Reasoning); + + classifier.ingest_prompt_tokens(&shared_boundary); + assert_eq!(classifier.current_section(), SampledTokenSection::ToolCall); + Ok(()) } diff --git a/llama-cpp-bindings/Cargo.toml b/llama-cpp-bindings/Cargo.toml index 666a10075..3eaf97f0a 100644 --- a/llama-cpp-bindings/Cargo.toml +++ b/llama-cpp-bindings/Cargo.toml @@ -23,6 +23,7 @@ thiserror = { workspace = true } toktrie = { workspace = true } [dev-dependencies] +llama-cpp-wrapper-error-fixture = { workspace = true } serial_test = { workspace = true } [features] diff --git a/llama-cpp-bindings/src/context.rs b/llama-cpp-bindings/src/context.rs index 0ae741aaf..c8c266dd7 100644 --- a/llama-cpp-bindings/src/context.rs +++ b/llama-cpp-bindings/src/context.rs @@ -45,16 +45,16 @@ fn new_context_with_model_status_to_result( } .into() }), - llama_cpp_bindings_sys::LLAMA_RS_NEW_CONTEXT_WITH_MODEL_VENDORED_RETURNED_NULL => { + llama_cpp_bindings_sys::LLAMA_RS_NEW_CONTEXT_WITH_MODEL_LLAMA_CPP_RETURNED_NULL => { Err(LlamaContextLoadError::Unconstructible) } llama_cpp_bindings_sys::LLAMA_RS_NEW_CONTEXT_WITH_MODEL_ERROR_STRING_ALLOCATION_FAILED => { Err(LlamaContextLoadError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_NEW_CONTEXT_WITH_MODEL_VENDORED_OUT_OF_MEMORY => { - Err(LlamaContextLoadError::VendoredOutOfMemory) + llama_cpp_bindings_sys::LLAMA_RS_NEW_CONTEXT_WITH_MODEL_LLAMA_CPP_OUT_OF_MEMORY => { + Err(LlamaContextLoadError::LlamaCppOutOfMemory) } - llama_cpp_bindings_sys::LLAMA_RS_NEW_CONTEXT_WITH_MODEL_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_NEW_CONTEXT_WITH_MODEL_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string( out_error, @@ -95,16 +95,16 @@ fn new_context_with_model_status_to_result( fn decode_status_to_result( status: llama_cpp_bindings_sys::llama_rs_decode_status, - out_vendored_return_code: i32, + out_llama_cpp_return_code: i32, out_error: *mut std::os::raw::c_char, ) -> Result<(), DecodeError> { match status { llama_cpp_bindings_sys::LLAMA_RS_DECODE_OK => Ok(()), - llama_cpp_bindings_sys::LLAMA_RS_DECODE_VENDORED_RETURNED_NONZERO_CODE => { + llama_cpp_bindings_sys::LLAMA_RS_DECODE_LLAMA_CPP_RETURNED_NONZERO_CODE => { let code = - NonZeroI32::new(out_vendored_return_code).ok_or(crate::FfiContractError { + NonZeroI32::new(out_llama_cpp_return_code).ok_or(crate::FfiContractError { operation: "llama_rs_decode", - detail: "nonzero vendored return status contained zero", + detail: "nonzero llama.cpp return status contained zero", })?; Err(DecodeError::from(code)) } @@ -115,10 +115,10 @@ fn decode_status_to_result( llama_cpp_bindings_sys::LLAMA_RS_DECODE_ERROR_STRING_ALLOCATION_FAILED => { Err(DecodeError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_DECODE_VENDORED_OUT_OF_MEMORY => { - Err(DecodeError::VendoredOutOfMemory) + llama_cpp_bindings_sys::LLAMA_RS_DECODE_LLAMA_CPP_OUT_OF_MEMORY => { + Err(DecodeError::LlamaCppOutOfMemory) } - llama_cpp_bindings_sys::LLAMA_RS_DECODE_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_DECODE_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string( out_error, @@ -150,7 +150,7 @@ fn decode_status_to_result( fn encode_status_to_result( status: llama_cpp_bindings_sys::llama_rs_encode_status, - out_vendored_return_code: i32, + out_llama_cpp_return_code: i32, out_error: *mut std::os::raw::c_char, ) -> Result<(), EncodeError> { match status { @@ -158,11 +158,11 @@ fn encode_status_to_result( llama_cpp_bindings_sys::LLAMA_RS_ENCODE_MODEL_HAS_NO_ENCODER => { Err(EncodeError::ModelHasNoEncoder) } - llama_cpp_bindings_sys::LLAMA_RS_ENCODE_VENDORED_RETURNED_NONZERO_CODE => { + llama_cpp_bindings_sys::LLAMA_RS_ENCODE_LLAMA_CPP_RETURNED_NONZERO_CODE => { let code = - NonZeroI32::new(out_vendored_return_code).ok_or(crate::FfiContractError { + NonZeroI32::new(out_llama_cpp_return_code).ok_or(crate::FfiContractError { operation: "llama_rs_encode", - detail: "nonzero vendored return status contained zero", + detail: "nonzero llama.cpp return status contained zero", })?; Err(EncodeError::from(code)) } @@ -173,10 +173,10 @@ fn encode_status_to_result( llama_cpp_bindings_sys::LLAMA_RS_ENCODE_ERROR_STRING_ALLOCATION_FAILED => { Err(EncodeError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_ENCODE_VENDORED_OUT_OF_MEMORY => { - Err(EncodeError::VendoredOutOfMemory) + llama_cpp_bindings_sys::LLAMA_RS_ENCODE_LLAMA_CPP_OUT_OF_MEMORY => { + Err(EncodeError::LlamaCppOutOfMemory) } - llama_cpp_bindings_sys::LLAMA_RS_ENCODE_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_ENCODE_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string( out_error, @@ -370,17 +370,17 @@ impl<'model> LlamaContext<'model> { /// /// - `DecodeError` if the decoding failed. pub fn decode(&mut self, batch: &mut LlamaBatch) -> Result<(), DecodeError> { - let mut out_vendored_return_code: i32 = 0; + let mut out_llama_cpp_return_code: i32 = 0; let mut out_error: *mut std::os::raw::c_char = std::ptr::null_mut(); let status = unsafe { llama_cpp_bindings_sys::llama_rs_decode( self.context.as_ptr(), batch.llama_batch, - &raw mut out_vendored_return_code, + &raw mut out_llama_cpp_return_code, &raw mut out_error, ) }; - decode_status_to_result(status, out_vendored_return_code, out_error)?; + decode_status_to_result(status, out_llama_cpp_return_code, out_error)?; self.initialized_logits .clone_from(&batch.initialized_logits); @@ -392,17 +392,17 @@ impl<'model> LlamaContext<'model> { /// /// - `EncodeError` if the encoding failed. pub fn encode(&mut self, batch: &mut LlamaBatch) -> Result<(), EncodeError> { - let mut out_vendored_return_code: i32 = 0; + let mut out_llama_cpp_return_code: i32 = 0; let mut out_error: *mut std::os::raw::c_char = std::ptr::null_mut(); let status = unsafe { llama_cpp_bindings_sys::llama_rs_encode( self.context.as_ptr(), batch.llama_batch, - &raw mut out_vendored_return_code, + &raw mut out_llama_cpp_return_code, &raw mut out_error, ) }; - encode_status_to_result(status, out_vendored_return_code, out_error)?; + encode_status_to_result(status, out_llama_cpp_return_code, out_error)?; self.initialized_logits .clone_from(&batch.initialized_logits); @@ -630,9 +630,9 @@ mod unit_tests { } #[test] - fn new_context_vendored_returned_null_maps_unconstructible() { + fn new_context_llama_cpp_returned_null_maps_unconstructible() { let result = new_context_with_model_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_NEW_CONTEXT_WITH_MODEL_VENDORED_RETURNED_NULL, + llama_cpp_bindings_sys::LLAMA_RS_NEW_CONTEXT_WITH_MODEL_LLAMA_CPP_RETURNED_NULL, std::ptr::null_mut(), std::ptr::null_mut(), ); @@ -654,7 +654,7 @@ mod unit_tests { #[test] fn new_context_cxx_exception_without_a_message_is_a_contract_error() { let result = new_context_with_model_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_NEW_CONTEXT_WITH_MODEL_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_NEW_CONTEXT_WITH_MODEL_LLAMA_CPP_THREW_CXX_EXCEPTION, std::ptr::null_mut(), std::ptr::null_mut(), ); @@ -689,7 +689,7 @@ mod unit_tests { #[test] fn decode_nonzero_code_maps_from_code() { let result = decode_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_DECODE_VENDORED_RETURNED_NONZERO_CODE, + llama_cpp_bindings_sys::LLAMA_RS_DECODE_LLAMA_CPP_RETURNED_NONZERO_CODE, 1, std::ptr::null_mut(), ); @@ -733,7 +733,7 @@ mod unit_tests { #[test] fn decode_cxx_exception_without_a_message_is_a_contract_error() { let result = decode_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_DECODE_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_DECODE_LLAMA_CPP_THREW_CXX_EXCEPTION, 0, std::ptr::null_mut(), ); @@ -751,7 +751,7 @@ mod unit_tests { #[test] fn decode_nonzero_status_with_zero_code_is_contract_error() { let result = decode_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_DECODE_VENDORED_RETURNED_NONZERO_CODE, + llama_cpp_bindings_sys::LLAMA_RS_DECODE_LLAMA_CPP_RETURNED_NONZERO_CODE, 0, std::ptr::null_mut(), ); @@ -760,7 +760,7 @@ mod unit_tests { result, Err(DecodeError::FfiContract(crate::FfiContractError { operation: "llama_rs_decode", - detail: "nonzero vendored return status contained zero", + detail: "nonzero llama.cpp return status contained zero", })) ); } @@ -792,7 +792,7 @@ mod unit_tests { #[test] fn encode_nonzero_code_maps_from_code() { let result = encode_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_ENCODE_VENDORED_RETURNED_NONZERO_CODE, + llama_cpp_bindings_sys::LLAMA_RS_ENCODE_LLAMA_CPP_RETURNED_NONZERO_CODE, 1, std::ptr::null_mut(), ); @@ -836,7 +836,7 @@ mod unit_tests { #[test] fn encode_cxx_exception_without_a_message_is_a_contract_error() { let result = encode_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_ENCODE_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_ENCODE_LLAMA_CPP_THREW_CXX_EXCEPTION, 0, std::ptr::null_mut(), ); @@ -854,7 +854,7 @@ mod unit_tests { #[test] fn encode_nonzero_status_with_zero_code_is_contract_error() { let result = encode_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_ENCODE_VENDORED_RETURNED_NONZERO_CODE, + llama_cpp_bindings_sys::LLAMA_RS_ENCODE_LLAMA_CPP_RETURNED_NONZERO_CODE, 0, std::ptr::null_mut(), ); @@ -863,7 +863,7 @@ mod unit_tests { result, Err(EncodeError::FfiContract(crate::FfiContractError { operation: "llama_rs_encode", - detail: "nonzero vendored return status contained zero", + detail: "nonzero llama.cpp return status contained zero", })) ); } @@ -983,13 +983,13 @@ mod ffi_contract_status_tests { ) ); let outcome_3 = new_context_with_model_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_NEW_CONTEXT_WITH_MODEL_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_NEW_CONTEXT_WITH_MODEL_LLAMA_CPP_OUT_OF_MEMORY, ptr::null_mut(), ptr::null_mut(), ); assert_eq!( outcome_3.err(), - Some(LlamaContextLoadError::VendoredOutOfMemory) + Some(LlamaContextLoadError::LlamaCppOutOfMemory) ); } @@ -1026,11 +1026,11 @@ mod ffi_contract_status_tests { ) ); let outcome_2 = decode_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_DECODE_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_DECODE_LLAMA_CPP_OUT_OF_MEMORY, 0, ptr::null_mut(), ); - assert_eq!(outcome_2.err(), Some(DecodeError::VendoredOutOfMemory)); + assert_eq!(outcome_2.err(), Some(DecodeError::LlamaCppOutOfMemory)); } #[test] @@ -1066,10 +1066,10 @@ mod ffi_contract_status_tests { ) ); let outcome_2 = encode_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_ENCODE_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_ENCODE_LLAMA_CPP_OUT_OF_MEMORY, 0, ptr::null_mut(), ); - assert_eq!(outcome_2.err(), Some(EncodeError::VendoredOutOfMemory)); + assert_eq!(outcome_2.err(), Some(EncodeError::LlamaCppOutOfMemory)); } } diff --git a/llama-cpp-bindings/src/context/kv_cache.rs b/llama-cpp-bindings/src/context/kv_cache.rs index 85dfce326..efc9e584a 100644 --- a/llama-cpp-bindings/src/context/kv_cache.rs +++ b/llama-cpp-bindings/src/context/kv_cache.rs @@ -4,8 +4,10 @@ use std::os::raw::c_char; use std::ptr; use crate::context::LlamaContext; -use crate::error::kv_cache_conversion_error::KvCacheConversionError; -use crate::error::{KvCacheSeqAddError, KvCacheSeqDivError, KvCacheSeqPosMaxError}; +use crate::error::{ + ClearKvCacheSeqError, CopyKvCacheSeqError, KvCacheSeqAddError, KvCacheSeqDivError, + KvCacheSeqPosMaxError, +}; use llama_cpp_ffi_status::read_and_free_cpp_string; fn kv_cache_seq_add_status_to_result( @@ -23,10 +25,10 @@ fn kv_cache_seq_add_status_to_result( llama_cpp_bindings_sys::LLAMA_RS_MEMORY_SEQ_ADD_ERROR_STRING_ALLOCATION_FAILED => { Err(KvCacheSeqAddError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_MEMORY_SEQ_ADD_VENDORED_OUT_OF_MEMORY => { - Err(KvCacheSeqAddError::VendoredOutOfMemory) + llama_cpp_bindings_sys::LLAMA_RS_MEMORY_SEQ_ADD_LLAMA_CPP_OUT_OF_MEMORY => { + Err(KvCacheSeqAddError::LlamaCppOutOfMemory) } - llama_cpp_bindings_sys::LLAMA_RS_MEMORY_SEQ_ADD_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_MEMORY_SEQ_ADD_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string( out_error, @@ -73,10 +75,10 @@ fn kv_cache_seq_div_status_to_result( llama_cpp_bindings_sys::LLAMA_RS_MEMORY_SEQ_DIV_ERROR_STRING_ALLOCATION_FAILED => { Err(KvCacheSeqDivError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_MEMORY_SEQ_DIV_VENDORED_OUT_OF_MEMORY => { - Err(KvCacheSeqDivError::VendoredOutOfMemory) + llama_cpp_bindings_sys::LLAMA_RS_MEMORY_SEQ_DIV_LLAMA_CPP_OUT_OF_MEMORY => { + Err(KvCacheSeqDivError::LlamaCppOutOfMemory) } - llama_cpp_bindings_sys::LLAMA_RS_MEMORY_SEQ_DIV_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_MEMORY_SEQ_DIV_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string( out_error, @@ -146,10 +148,10 @@ fn kv_cache_seq_pos_max_status_to_result( llama_cpp_bindings_sys::LLAMA_RS_MEMORY_SEQ_POS_MAX_ERROR_STRING_ALLOCATION_FAILED => { Err(KvCacheSeqPosMaxError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_MEMORY_SEQ_POS_MAX_VENDORED_OUT_OF_MEMORY => { - Err(KvCacheSeqPosMaxError::VendoredOutOfMemory) + llama_cpp_bindings_sys::LLAMA_RS_MEMORY_SEQ_POS_MAX_LLAMA_CPP_OUT_OF_MEMORY => { + Err(KvCacheSeqPosMaxError::LlamaCppOutOfMemory) } - llama_cpp_bindings_sys::LLAMA_RS_MEMORY_SEQ_POS_MAX_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_MEMORY_SEQ_POS_MAX_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string( out_error, @@ -168,19 +170,17 @@ fn kv_cache_seq_pos_max_status_to_result( } impl LlamaContext<'_> { - /// # Errors - /// Returns [`KvCacheConversionError::MemoryHandleUnavailable`] when the context was - /// built without a memory module, so a null handle is never handed to llama.cpp. - fn memory_handle( - &self, - ) -> Result { + fn memory_handle(&self) -> Option { let mem = unsafe { llama_cpp_bindings_sys::llama_get_memory(self.context.as_ptr()) }; - if mem.is_null() { - return Err(KvCacheConversionError::MemoryHandleUnavailable); - } + if mem.is_null() { None } else { Some(mem) } + } - Ok(mem) + fn required_memory_handle( + &self, + ) -> Result { + self.memory_handle() + .ok_or(CopyKvCacheSeqError::MemoryHandleUnavailable) } /// # Errors @@ -191,67 +191,62 @@ impl LlamaContext<'_> { dest: i32, p0: Option, p1: Option, - ) -> Result<(), KvCacheConversionError> { + ) -> Result<(), CopyKvCacheSeqError> { let p0 = p0 .map_or(Ok(-1), i32::try_from) - .map_err(KvCacheConversionError::P0TooLarge)?; + .map_err(CopyKvCacheSeqError::P0TooLarge)?; let p1 = p1 .map_or(Ok(-1), i32::try_from) - .map_err(KvCacheConversionError::P1TooLarge)?; - let mem = self.memory_handle()?; + .map_err(CopyKvCacheSeqError::P1TooLarge)?; + let mem = self.required_memory_handle()?; unsafe { llama_cpp_bindings_sys::llama_memory_seq_cp(mem, src, dest, p0, p1) }; Ok(()) } /// # Errors - /// If the sequence id or either position exceeds [`i32::MAX`], the context has no - /// memory module, or llama.cpp reports that the partial sequence could not be removed. + /// If the sequence id or either position exceeds [`i32::MAX`], or llama.cpp reports + /// that the partial sequence could not be removed. A context without a memory module + /// holds no KV cache, so the removal trivially succeeds. pub fn clear_kv_cache_seq( &mut self, - src: Option, + seq_id: Option, p0: Option, p1: Option, - ) -> Result<(), KvCacheConversionError> { - let src = src + ) -> Result<(), ClearKvCacheSeqError> { + let seq_id = seq_id .map_or(Ok(-1), i32::try_from) - .map_err(KvCacheConversionError::SeqIdTooLarge)?; + .map_err(ClearKvCacheSeqError::SeqIdTooLarge)?; let p0 = p0 .map_or(Ok(-1), i32::try_from) - .map_err(KvCacheConversionError::P0TooLarge)?; + .map_err(ClearKvCacheSeqError::P0TooLarge)?; let p1 = p1 .map_or(Ok(-1), i32::try_from) - .map_err(KvCacheConversionError::P1TooLarge)?; - let mem = self.memory_handle()?; + .map_err(ClearKvCacheSeqError::P1TooLarge)?; + let Some(mem) = self.memory_handle() else { + return Ok(()); + }; - if unsafe { llama_cpp_bindings_sys::llama_memory_seq_rm(mem, src, p0, p1) } { + if unsafe { llama_cpp_bindings_sys::llama_memory_seq_rm(mem, seq_id, p0, p1) } { return Ok(()); } - Err(KvCacheConversionError::PartialSequenceNotRemoved { - seq_id: src, - p0, - p1, - }) + Err(ClearKvCacheSeqError::PartialSequenceNotRemoved { seq_id, p0, p1 }) } - /// # Errors - /// If the context has no memory module. - pub fn clear_kv_cache(&mut self) -> Result<(), KvCacheConversionError> { - let mem = self.memory_handle()?; + pub fn clear_kv_cache(&mut self) { + let Some(mem) = self.memory_handle() else { + return; + }; let clear_data_buffers = true; unsafe { llama_cpp_bindings_sys::llama_memory_clear(mem, clear_data_buffers) }; - - Ok(()) } - /// # Errors - /// If the context has no memory module. - pub fn kv_cache_seq_keep(&mut self, seq_id: i32) -> Result<(), KvCacheConversionError> { - let mem = self.memory_handle()?; + pub fn kv_cache_seq_keep(&mut self, seq_id: i32) { + let Some(mem) = self.memory_handle() else { + return; + }; unsafe { llama_cpp_bindings_sys::llama_memory_seq_keep(mem, seq_id) }; - - Ok(()) } /// # Errors @@ -385,10 +380,10 @@ mod tests { } #[test] - fn add_vendored_exception_status_without_a_message_is_a_contract_error_with_unknown_message() { + fn add_llama_cpp_exception_status_without_a_message_is_a_contract_error_with_unknown_message() { assert_eq!( kv_cache_seq_add_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_MEMORY_SEQ_ADD_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_MEMORY_SEQ_ADD_LLAMA_CPP_THREW_CXX_EXCEPTION, ptr::null_mut(), ), Err(crate::FfiContractError { @@ -456,10 +451,10 @@ mod tests { } #[test] - fn div_vendored_exception_status_without_a_message_is_a_contract_error_with_unknown_message() { + fn div_llama_cpp_exception_status_without_a_message_is_a_contract_error_with_unknown_message() { assert_eq!( kv_cache_seq_div_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_MEMORY_SEQ_DIV_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_MEMORY_SEQ_DIV_LLAMA_CPP_THREW_CXX_EXCEPTION, ptr::null_mut(), ), Err(crate::FfiContractError { @@ -590,10 +585,10 @@ mod tests { } #[test] - fn seq_pos_max_vendored_exception_status_without_a_message_is_a_contract_error_error() { + fn seq_pos_max_llama_cpp_exception_status_without_a_message_is_a_contract_error_error() { assert_eq!( kv_cache_seq_pos_max_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_MEMORY_SEQ_POS_MAX_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_MEMORY_SEQ_POS_MAX_LLAMA_CPP_THREW_CXX_EXCEPTION, -1, 2, ptr::null_mut(), @@ -645,12 +640,12 @@ mod ffi_contract_status_tests { ) ); let outcome_1 = kv_cache_seq_add_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_MEMORY_SEQ_ADD_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_MEMORY_SEQ_ADD_LLAMA_CPP_OUT_OF_MEMORY, ptr::null_mut(), ); assert_eq!( outcome_1.err(), - Some(KvCacheSeqAddError::VendoredOutOfMemory) + Some(KvCacheSeqAddError::LlamaCppOutOfMemory) ); } @@ -671,26 +666,26 @@ mod ffi_contract_status_tests { ) ); let outcome_1 = kv_cache_seq_div_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_MEMORY_SEQ_DIV_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_MEMORY_SEQ_DIV_LLAMA_CPP_OUT_OF_MEMORY, ptr::null_mut(), ); assert_eq!( outcome_1.err(), - Some(KvCacheSeqDivError::VendoredOutOfMemory) + Some(KvCacheSeqDivError::LlamaCppOutOfMemory) ); } #[test] fn kv_cache_seq_pos_max_status_to_result_maps_every_contract_status() { let outcome_0 = kv_cache_seq_pos_max_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_MEMORY_SEQ_POS_MAX_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_MEMORY_SEQ_POS_MAX_LLAMA_CPP_OUT_OF_MEMORY, 0, 0, ptr::null_mut(), ); assert_eq!( outcome_0.err(), - Some(KvCacheSeqPosMaxError::VendoredOutOfMemory) + Some(KvCacheSeqPosMaxError::LlamaCppOutOfMemory) ); } } diff --git a/llama-cpp-bindings/src/context/session.rs b/llama-cpp-bindings/src/context/session.rs index fa47402a2..511d2ab23 100644 --- a/llama-cpp-bindings/src/context/session.rs +++ b/llama-cpp-bindings/src/context/session.rs @@ -48,10 +48,10 @@ fn state_data_status_to_result( llama_cpp_bindings_sys::LLAMA_RS_STATE_DATA_ERROR_STRING_ALLOCATION_FAILED => { Err(StateDataError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_STATE_DATA_VENDORED_OUT_OF_MEMORY => { - Err(StateDataError::VendoredOutOfMemory) + llama_cpp_bindings_sys::LLAMA_RS_STATE_DATA_LLAMA_CPP_OUT_OF_MEMORY => { + Err(StateDataError::LlamaCppOutOfMemory) } - llama_cpp_bindings_sys::LLAMA_RS_STATE_DATA_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_STATE_DATA_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { llama_cpp_ffi_status::read_and_free_cpp_string( out_error, @@ -253,7 +253,7 @@ impl LlamaContext<'_> { /// # Errors /// - /// Returns [`StateDataError`] when the vendored serializer fails; the exception is + /// Returns [`StateDataError`] when the llama.cpp serializer fails; the exception is /// caught in the C++ wrapper so it can never unwind across the FFI boundary. /// /// # Safety @@ -283,7 +283,7 @@ impl LlamaContext<'_> { /// /// # Errors /// - /// Returns [`StateDataError`] when the vendored deserializer rejects the buffer. + /// Returns [`StateDataError`] when the llama.cpp deserializer rejects the buffer. pub unsafe fn set_state_data(&mut self, src: &[u8]) -> Result { let mut byte_count = 0usize; let mut out_error: *mut std::ffi::c_char = std::ptr::null_mut(); @@ -326,7 +326,7 @@ impl LlamaContext<'_> { /// /// # Errors /// - /// Returns [`StateDataError`] when the vendored serializer fails. + /// Returns [`StateDataError`] when the llama.cpp serializer fails. pub unsafe fn state_seq_get_data_ext( &self, dest: &mut [u8], @@ -357,7 +357,7 @@ impl LlamaContext<'_> { /// /// # Errors /// - /// Returns [`StateDataError`] when the vendored deserializer rejects the buffer. + /// Returns [`StateDataError`] when the llama.cpp deserializer rejects the buffer. pub unsafe fn state_seq_set_data_ext( &mut self, src: &[u8], @@ -476,11 +476,11 @@ mod ffi_contract_status_tests { #[test] fn state_data_status_to_result_maps_every_contract_status() { let outcome_0 = state_data_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_STATE_DATA_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_STATE_DATA_LLAMA_CPP_OUT_OF_MEMORY, 0, ptr::null_mut(), "", ); - assert_eq!(outcome_0.err(), Some(StateDataError::VendoredOutOfMemory)); + assert_eq!(outcome_0.err(), Some(StateDataError::LlamaCppOutOfMemory)); } } diff --git a/llama-cpp-bindings/src/context/state_data_error.rs b/llama-cpp-bindings/src/context/state_data_error.rs index f99562d50..0487b2980 100644 --- a/llama-cpp-bindings/src/context/state_data_error.rs +++ b/llama-cpp-bindings/src/context/state_data_error.rs @@ -6,10 +6,10 @@ pub enum StateDataError { FfiContract(#[from] crate::FfiContractError), #[error("not enough memory")] NotEnoughMemory, - #[error("the vendored library ran out of memory")] - VendoredOutOfMemory, + #[error("the llama.cpp library ran out of memory")] + LlamaCppOutOfMemory, #[error( - "the vendored deserializer restored nothing from a {provided_bytes}-byte snapshot; \ + "the llama.cpp deserializer restored nothing from a {provided_bytes}-byte snapshot; \ llama.cpp logs the cause and reports zero bytes rather than throwing" )] NothingRestored { provided_bytes: usize }, diff --git a/llama-cpp-bindings/src/error.rs b/llama-cpp-bindings/src/error.rs index 9896295e3..04deed8f4 100644 --- a/llama-cpp-bindings/src/error.rs +++ b/llama-cpp-bindings/src/error.rs @@ -1,6 +1,8 @@ pub mod apply_chat_template_error; pub mod bracketed_args_failure; pub mod chat_template_error; +pub mod clear_kv_cache_seq_error; +pub mod copy_kv_cache_seq_error; pub mod decode_error; pub mod embeddings_error; pub mod encode_error; @@ -11,7 +13,6 @@ pub mod grammar_runtime_error; pub mod json_object_failure; pub mod json_schema_to_grammar_error; pub mod key_value_xml_tags_failure; -pub mod kv_cache_conversion_error; pub mod kv_cache_seq_add_error; pub mod kv_cache_seq_div_error; pub mod kv_cache_seq_pos_max_error; @@ -43,6 +44,8 @@ pub use llama_cpp_ffi_status::FfiStatusError; pub use apply_chat_template_error::ApplyChatTemplateError; pub use bracketed_args_failure::BracketedArgsFailure; pub use chat_template_error::ChatTemplateError; +pub use clear_kv_cache_seq_error::ClearKvCacheSeqError; +pub use copy_kv_cache_seq_error::CopyKvCacheSeqError; pub use decode_error::DecodeError; pub use embeddings_error::EmbeddingsError; pub use encode_error::EncodeError; @@ -53,7 +56,6 @@ pub use grammar_runtime_error::GrammarRuntimeError; pub use json_object_failure::JsonObjectFailure; pub use json_schema_to_grammar_error::JsonSchemaToGrammarError; pub use key_value_xml_tags_failure::KeyValueXmlTagsFailure; -pub use kv_cache_conversion_error::KvCacheConversionError; pub use kv_cache_seq_add_error::KvCacheSeqAddError; pub use kv_cache_seq_div_error::KvCacheSeqDivError; pub use kv_cache_seq_pos_max_error::KvCacheSeqPosMaxError; diff --git a/llama-cpp-bindings/src/error/apply_chat_template_error.rs b/llama-cpp-bindings/src/error/apply_chat_template_error.rs index f1a913da1..12a53c44e 100644 --- a/llama-cpp-bindings/src/error/apply_chat_template_error.rs +++ b/llama-cpp-bindings/src/error/apply_chat_template_error.rs @@ -10,8 +10,8 @@ pub enum ApplyChatTemplateError { TemplateApplicationFailed, #[error("not enough memory to render the chat template")] NotEnoughMemory, - #[error("the vendored chat-template renderer ran out of memory")] - VendoredOutOfMemory, + #[error("the llama.cpp chat-template renderer ran out of memory")] + LlamaCppOutOfMemory, #[error("{message}")] Reported { message: String }, } diff --git a/llama-cpp-bindings/src/error/kv_cache_conversion_error.rs b/llama-cpp-bindings/src/error/clear_kv_cache_seq_error.rs similarity index 56% rename from llama-cpp-bindings/src/error/kv_cache_conversion_error.rs rename to llama-cpp-bindings/src/error/clear_kv_cache_seq_error.rs index f545744f8..45c01bcdc 100644 --- a/llama-cpp-bindings/src/error/kv_cache_conversion_error.rs +++ b/llama-cpp-bindings/src/error/clear_kv_cache_seq_error.rs @@ -2,15 +2,13 @@ use std::ffi::c_int; use std::num::TryFromIntError; #[derive(Debug, Eq, PartialEq, thiserror::Error)] -pub enum KvCacheConversionError { - #[error("Provided sequence id is too large for a i32")] +pub enum ClearKvCacheSeqError { + #[error("provided sequence id is too large for an i32")] SeqIdTooLarge(#[source] TryFromIntError), - #[error("Provided start position is too large for a i32")] + #[error("provided start position is too large for an i32")] P0TooLarge(#[source] TryFromIntError), - #[error("Provided end position is too large for a i32")] + #[error("provided end position is too large for an i32")] P1TooLarge(#[source] TryFromIntError), - #[error("the context has no memory module attached")] - MemoryHandleUnavailable, #[error("sequence {seq_id} could not be partially removed over positions [{p0}, {p1})")] PartialSequenceNotRemoved { seq_id: c_int, p0: c_int, p1: c_int }, } diff --git a/llama-cpp-bindings/src/error/copy_kv_cache_seq_error.rs b/llama-cpp-bindings/src/error/copy_kv_cache_seq_error.rs new file mode 100644 index 000000000..011e70744 --- /dev/null +++ b/llama-cpp-bindings/src/error/copy_kv_cache_seq_error.rs @@ -0,0 +1,11 @@ +use std::num::TryFromIntError; + +#[derive(Debug, Eq, PartialEq, thiserror::Error)] +pub enum CopyKvCacheSeqError { + #[error("provided start position is too large for an i32")] + P0TooLarge(#[source] TryFromIntError), + #[error("provided end position is too large for an i32")] + P1TooLarge(#[source] TryFromIntError), + #[error("the context has no memory module attached")] + MemoryHandleUnavailable, +} diff --git a/llama-cpp-bindings/src/error/decode_error.rs b/llama-cpp-bindings/src/error/decode_error.rs index 5f22ca36a..c929af077 100644 --- a/llama-cpp-bindings/src/error/decode_error.rs +++ b/llama-cpp-bindings/src/error/decode_error.rs @@ -21,8 +21,8 @@ pub enum DecodeError { UnknownStatus { code: c_int }, #[error("not enough memory")] NotEnoughMemory, - #[error("the vendored library ran out of memory")] - VendoredOutOfMemory, + #[error("the llama.cpp library ran out of memory")] + LlamaCppOutOfMemory, #[error("{message}")] Reported { message: String }, } diff --git a/llama-cpp-bindings/src/error/encode_error.rs b/llama-cpp-bindings/src/error/encode_error.rs index 247f6ea86..74a93d907 100644 --- a/llama-cpp-bindings/src/error/encode_error.rs +++ b/llama-cpp-bindings/src/error/encode_error.rs @@ -21,8 +21,8 @@ pub enum EncodeError { UnknownStatus { code: c_int }, #[error("not enough memory")] NotEnoughMemory, - #[error("the vendored library ran out of memory")] - VendoredOutOfMemory, + #[error("the llama.cpp library ran out of memory")] + LlamaCppOutOfMemory, #[error("{message}")] Reported { message: String }, } diff --git a/llama-cpp-bindings/src/error/fit_error.rs b/llama-cpp-bindings/src/error/fit_error.rs index db8e5d20e..8aa5a00e9 100644 --- a/llama-cpp-bindings/src/error/fit_error.rs +++ b/llama-cpp-bindings/src/error/fit_error.rs @@ -12,8 +12,8 @@ pub enum FitError { UnknownStatus { code: i32 }, #[error("not enough memory")] NotEnoughMemory, - #[error("the vendored library ran out of memory")] - VendoredOutOfMemory, + #[error("the llama.cpp library ran out of memory")] + LlamaCppOutOfMemory, #[error("{message}")] Reported { message: String }, } diff --git a/llama-cpp-bindings/src/error/grammar_error.rs b/llama-cpp-bindings/src/error/grammar_error.rs index 1fd2b6b9f..e2e059755 100644 --- a/llama-cpp-bindings/src/error/grammar_error.rs +++ b/llama-cpp-bindings/src/error/grammar_error.rs @@ -37,8 +37,8 @@ pub enum GrammarError { InvalidTriggerPattern { message: String }, #[error("not enough memory")] NotEnoughMemory, - #[error("the vendored library ran out of memory")] - VendoredOutOfMemory, + #[error("the llama.cpp library ran out of memory")] + LlamaCppOutOfMemory, #[error("{message}")] Reported { message: String }, } diff --git a/llama-cpp-bindings/src/error/json_schema_to_grammar_error.rs b/llama-cpp-bindings/src/error/json_schema_to_grammar_error.rs index c9adcf4eb..73c5373dc 100644 --- a/llama-cpp-bindings/src/error/json_schema_to_grammar_error.rs +++ b/llama-cpp-bindings/src/error/json_schema_to_grammar_error.rs @@ -13,8 +13,8 @@ pub enum JsonSchemaToGrammarError { InvalidSchema { message: String }, #[error("not enough memory")] NotEnoughMemory, - #[error("the vendored library ran out of memory")] - VendoredOutOfMemory, + #[error("the llama.cpp library ran out of memory")] + LlamaCppOutOfMemory, #[error("{message}")] Reported { message: String }, #[error("grammar returned by json_schema_to_grammar is not valid UTF-8")] diff --git a/llama-cpp-bindings/src/error/kv_cache_seq_add_error.rs b/llama-cpp-bindings/src/error/kv_cache_seq_add_error.rs index 36323085a..a018a705f 100644 --- a/llama-cpp-bindings/src/error/kv_cache_seq_add_error.rs +++ b/llama-cpp-bindings/src/error/kv_cache_seq_add_error.rs @@ -16,8 +16,8 @@ pub enum KvCacheSeqAddError { MemoryHandleUnavailable, #[error("not enough memory")] NotEnoughMemory, - #[error("the vendored library ran out of memory")] - VendoredOutOfMemory, + #[error("the llama.cpp library ran out of memory")] + LlamaCppOutOfMemory, #[error("{message}")] Reported { message: String }, } diff --git a/llama-cpp-bindings/src/error/kv_cache_seq_div_error.rs b/llama-cpp-bindings/src/error/kv_cache_seq_div_error.rs index b500dc7f1..463d345f0 100644 --- a/llama-cpp-bindings/src/error/kv_cache_seq_div_error.rs +++ b/llama-cpp-bindings/src/error/kv_cache_seq_div_error.rs @@ -16,8 +16,8 @@ pub enum KvCacheSeqDivError { MemoryHandleUnavailable, #[error("not enough memory")] NotEnoughMemory, - #[error("the vendored library ran out of memory")] - VendoredOutOfMemory, + #[error("the llama.cpp library ran out of memory")] + LlamaCppOutOfMemory, #[error("{message}")] Reported { message: String }, } diff --git a/llama-cpp-bindings/src/error/kv_cache_seq_pos_max_error.rs b/llama-cpp-bindings/src/error/kv_cache_seq_pos_max_error.rs index fc1ac9bbc..0c8fbba6a 100644 --- a/llama-cpp-bindings/src/error/kv_cache_seq_pos_max_error.rs +++ b/llama-cpp-bindings/src/error/kv_cache_seq_pos_max_error.rs @@ -10,8 +10,8 @@ pub enum KvCacheSeqPosMaxError { SequenceIdOutOfRange { seq_id: i32 }, #[error("not enough memory")] NotEnoughMemory, - #[error("the vendored library ran out of memory")] - VendoredOutOfMemory, + #[error("the llama.cpp library ran out of memory")] + LlamaCppOutOfMemory, #[error("{message}")] Reported { message: String }, } diff --git a/llama-cpp-bindings/src/error/llama_context_load_error.rs b/llama-cpp-bindings/src/error/llama_context_load_error.rs index 94309ad8b..145ad6229 100644 --- a/llama-cpp-bindings/src/error/llama_context_load_error.rs +++ b/llama-cpp-bindings/src/error/llama_context_load_error.rs @@ -8,8 +8,8 @@ pub enum LlamaContextLoadError { Unconstructible, #[error("not enough memory")] NotEnoughMemory, - #[error("the vendored library ran out of memory")] - VendoredOutOfMemory, + #[error("the llama.cpp library ran out of memory")] + LlamaCppOutOfMemory, #[error("{message}")] Reported { message: String }, } diff --git a/llama-cpp-bindings/src/error/llama_model_load_error.rs b/llama-cpp-bindings/src/error/llama_model_load_error.rs index 1e27b6200..38b676d93 100644 --- a/llama-cpp-bindings/src/error/llama_model_load_error.rs +++ b/llama-cpp-bindings/src/error/llama_model_load_error.rs @@ -17,8 +17,8 @@ pub enum LlamaModelLoadError { Unloadable, #[error("not enough memory")] NotEnoughMemory, - #[error("the vendored library ran out of memory")] - VendoredOutOfMemory, + #[error("the llama.cpp library ran out of memory")] + LlamaCppOutOfMemory, #[error("{message}")] Reported { message: String }, } diff --git a/llama-cpp-bindings/src/error/marker_detection_error.rs b/llama-cpp-bindings/src/error/marker_detection_error.rs index 0d6abc58d..c3740617e 100644 --- a/llama-cpp-bindings/src/error/marker_detection_error.rs +++ b/llama-cpp-bindings/src/error/marker_detection_error.rs @@ -3,6 +3,7 @@ use std::string::FromUtf8Error; use crate::error::chat_template_error::ChatTemplateError; use crate::error::string_to_token_error::StringToTokenError; +use crate::token::LlamaToken; #[derive(Debug, PartialEq, Eq, thiserror::Error)] pub enum MarkerDetectionError { @@ -18,8 +19,8 @@ pub enum MarkerDetectionError { ModelHasNoChatTemplate { operation: &'static str }, #[error("{operation} could not run because the model has no vocab")] ModelHasNoVocab { operation: &'static str }, - #[error("the vendored library ran out of memory")] - VendoredOutOfMemory, + #[error("the llama.cpp library ran out of memory")] + LlamaCppOutOfMemory, #[error("reasoning-marker detection failed: {message}")] ReasoningMarkerDetectionFailed { message: String }, #[error("tool-call haystack computation failed: {message}")] @@ -30,6 +31,10 @@ pub enum MarkerDetectionError { ReasoningMarkersFreeFailed { message: String }, #[error("a detected marker string could not be tokenised: {0}")] MarkerTokenizationFailed(#[from] StringToTokenError), + #[error("marker detection produced an empty token sequence")] + EmptyMarker, + #[error("marker token sequence {tokens:?} opens more than one section")] + AmbiguousMarkerOpeners { tokens: Vec }, #[error("the chat template is not valid UTF-8: {0}")] ToolCallTemplateNotUtf8(#[from] Utf8Error), #[error("the chat template could not be retrieved for tool-call marker detection: {0}")] diff --git a/llama-cpp-bindings/src/error/parse_chat_message_error.rs b/llama-cpp-bindings/src/error/parse_chat_message_error.rs index 4498177c0..05ff0e87c 100644 --- a/llama-cpp-bindings/src/error/parse_chat_message_error.rs +++ b/llama-cpp-bindings/src/error/parse_chat_message_error.rs @@ -14,8 +14,8 @@ pub enum ParseChatMessageError { NoVocab, #[error("not enough memory")] NotEnoughMemory, - #[error("the vendored library ran out of memory")] - VendoredOutOfMemory, + #[error("the llama.cpp library ran out of memory")] + LlamaCppOutOfMemory, #[error("the chat parser could not be constructed: {message}")] ParserCreationFailed { message: String }, #[error("the chat parser did not recognize the message: {message}")] diff --git a/llama-cpp-bindings/src/error/sample_error.rs b/llama-cpp-bindings/src/error/sample_error.rs index 99202aa68..37d28de86 100644 --- a/llama-cpp-bindings/src/error/sample_error.rs +++ b/llama-cpp-bindings/src/error/sample_error.rs @@ -9,8 +9,8 @@ pub enum SampleError { FfiContract(#[from] crate::FfiContractError), #[error("not enough memory")] NotEnoughMemory, - #[error("the vendored library ran out of memory")] - VendoredOutOfMemory, + #[error("the llama.cpp library ran out of memory")] + LlamaCppOutOfMemory, #[error("applying the sampler to the token data array failed: {0}")] SamplerApply(#[from] SamplerApplyError), #[error("token detokenization failed during classification: {0}")] diff --git a/llama-cpp-bindings/src/error/sampler_accept_error.rs b/llama-cpp-bindings/src/error/sampler_accept_error.rs index 20ba25a91..b8865984a 100644 --- a/llama-cpp-bindings/src/error/sampler_accept_error.rs +++ b/llama-cpp-bindings/src/error/sampler_accept_error.rs @@ -6,8 +6,8 @@ pub enum SamplerAcceptError { FfiContract(#[from] crate::FfiContractError), #[error("not enough memory")] NotEnoughMemory, - #[error("the vendored library ran out of memory")] - VendoredOutOfMemory, + #[error("the llama.cpp library ran out of memory")] + LlamaCppOutOfMemory, #[error("grammar state corrupted during accept: {message}")] GrammarStateCorrupted { message: String }, #[error("the grammar sampler callback failed during accept: {message}")] diff --git a/llama-cpp-bindings/src/error/sampler_apply_error.rs b/llama-cpp-bindings/src/error/sampler_apply_error.rs index ca0dbc88e..dd8941462 100644 --- a/llama-cpp-bindings/src/error/sampler_apply_error.rs +++ b/llama-cpp-bindings/src/error/sampler_apply_error.rs @@ -8,10 +8,10 @@ pub enum SamplerApplyError { NullSampler, #[error("the sampler ran out of memory while applying to the token data array")] NotEnoughMemory, - #[error("the vendored sampler ran out of memory")] - VendoredOutOfMemory, + #[error("the llama.cpp sampler ran out of memory")] + LlamaCppOutOfMemory, #[error( - "the vendored sampler threw a C++ exception while applying to the token data array: {message}" + "the llama.cpp sampler threw a C++ exception while applying to the token data array: {message}" )] Reported { message: String }, } diff --git a/llama-cpp-bindings/src/error/string_to_token_error.rs b/llama-cpp-bindings/src/error/string_to_token_error.rs index a9c5acc55..8d9e5dd08 100644 --- a/llama-cpp-bindings/src/error/string_to_token_error.rs +++ b/llama-cpp-bindings/src/error/string_to_token_error.rs @@ -12,8 +12,8 @@ pub enum StringToTokenError { CIntConversionError(#[from] std::num::TryFromIntError), #[error("not enough memory")] NotEnoughMemory, - #[error("the vendored library ran out of memory")] - VendoredOutOfMemory, + #[error("the llama.cpp library ran out of memory")] + LlamaCppOutOfMemory, #[error("{message}")] Reported { message: String }, } diff --git a/llama-cpp-bindings/src/json_schema_to_grammar.rs b/llama-cpp-bindings/src/json_schema_to_grammar.rs index 4cc2fd682..561e2ba52 100644 --- a/llama-cpp-bindings/src/json_schema_to_grammar.rs +++ b/llama-cpp-bindings/src/json_schema_to_grammar.rs @@ -30,8 +30,8 @@ unsafe fn json_schema_to_grammar_status_to_result( llama_cpp_bindings_sys::LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_ERROR_STRING_ALLOCATION_FAILED => { Err(JsonSchemaToGrammarError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_VENDORED_OUT_OF_MEMORY => { - Err(JsonSchemaToGrammarError::VendoredOutOfMemory) + llama_cpp_bindings_sys::LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_LLAMA_CPP_OUT_OF_MEMORY => { + Err(JsonSchemaToGrammarError::LlamaCppOutOfMemory) } llama_cpp_bindings_sys::LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_INVALID_SCHEMA => { let message = unsafe { @@ -43,7 +43,7 @@ unsafe fn json_schema_to_grammar_status_to_result( }?; Err(JsonSchemaToGrammarError::InvalidSchema { message }) } - llama_cpp_bindings_sys::LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string( error_ptr, @@ -192,10 +192,10 @@ mod tests { } #[test] - fn vendored_exception_status_without_a_message_is_a_contract_error() { + fn llama_cpp_exception_status_without_a_message_is_a_contract_error() { let result = unsafe { json_schema_to_grammar_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_LLAMA_CPP_THREW_CXX_EXCEPTION, std::ptr::null_mut(), std::ptr::null_mut(), ) @@ -367,14 +367,14 @@ mod ffi_contract_status_tests { ); let outcome_3 = unsafe { json_schema_to_grammar_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_LLAMA_CPP_OUT_OF_MEMORY, ptr::null_mut(), ptr::null_mut(), ) }; assert_eq!( outcome_3.err(), - Some(JsonSchemaToGrammarError::VendoredOutOfMemory) + Some(JsonSchemaToGrammarError::LlamaCppOutOfMemory) ); } } diff --git a/llama-cpp-bindings/src/lib.rs b/llama-cpp-bindings/src/lib.rs index ba5f1bcc3..eea27d8fe 100644 --- a/llama-cpp-bindings/src/lib.rs +++ b/llama-cpp-bindings/src/lib.rs @@ -38,7 +38,8 @@ pub mod load_backends_error; pub mod load_backends_from_path; pub mod log_options; pub mod log_record; -pub mod marker_kind; +pub mod marker_role; +pub mod marker_role_candidate; pub mod mask_outcome; pub mod max_devices; pub mod mlock_supported; @@ -54,6 +55,7 @@ pub mod sampling; pub mod sanitized_grammar; pub mod send_logs_to_log; pub mod streaming_json_probe; +pub mod streaming_marker; pub mod streaming_markers; pub mod synthetic_tool_call_renders; pub mod timing; @@ -62,13 +64,13 @@ pub mod tool_call_format; pub mod tool_call_marker_pair; pub use error::{ - ApplyChatTemplateError, ChatTemplateError, DecodeError, EmbeddingsError, EncodeError, - EvalMultimodalChunksError, FfiContractError, FfiStatusError, GrammarError, - JsonSchemaToGrammarError, KvCacheSeqAddError, KvCacheSeqDivError, KvCacheSeqPosMaxError, - LlamaContextLoadError, LlamaCppError, LlamaLoraAdapterInitError, LlamaLoraAdaptersError, - LlamaModelLoadError, LogitsError, MarkerDetectionError, MetaValError, ModelParamsError, - NewLlamaChatMessageError, ParseChatMessageError, Result, SampleError, SamplerAcceptError, - SamplingError, StringToTokenError, TokenSamplingError, TokenToStringError, + ApplyChatTemplateError, ChatTemplateError, ClearKvCacheSeqError, CopyKvCacheSeqError, + DecodeError, EmbeddingsError, EncodeError, EvalMultimodalChunksError, FfiContractError, + FfiStatusError, GrammarError, JsonSchemaToGrammarError, KvCacheSeqAddError, KvCacheSeqDivError, + KvCacheSeqPosMaxError, LlamaContextLoadError, LlamaCppError, LlamaLoraAdapterInitError, + LlamaLoraAdaptersError, LlamaModelLoadError, LogitsError, MarkerDetectionError, MetaValError, + ModelParamsError, NewLlamaChatMessageError, ParseChatMessageError, Result, SampleError, + SamplerAcceptError, SamplingError, StringToTokenError, TokenSamplingError, TokenToStringError, }; pub use chat_message_parse_outcome::ChatMessageParseOutcome; @@ -81,10 +83,14 @@ pub use llama_cpp_bindings_types::{ ReasoningMarkers, TokenUsage, TokenUsageError, ToolCallArgsShape, ToolCallArguments, ToolCallMarkers, ToolCallValueQuote, XmlTagsShape, }; +pub use marker_role::MarkerRole; +pub use marker_role_candidate::MarkerRoleCandidate; pub use raw_chat_message::RawChatMessage; pub use sampled_token::SampledToken; pub use sampled_token_classifier::SampledTokenClassifier; pub use sampled_token_section::SampledTokenSection; +pub use streaming_marker::StreamingMarker; +pub use streaming_markers::StreamingMarkers; pub use synthetic_tool_call_renders::SyntheticToolCallRenders; pub use ggml_time_us::ggml_time_us; diff --git a/llama-cpp-bindings/src/marker_kind.rs b/llama-cpp-bindings/src/marker_kind.rs deleted file mode 100644 index fe027e7ac..000000000 --- a/llama-cpp-bindings/src/marker_kind.rs +++ /dev/null @@ -1,7 +0,0 @@ -#[derive(Copy, Clone, Debug, Eq, PartialEq)] -pub enum MarkerKind { - ReasoningOpen, - ReasoningClose, - ToolCallOpen, - ToolCallClose, -} diff --git a/llama-cpp-bindings/src/marker_role.rs b/llama-cpp-bindings/src/marker_role.rs new file mode 100644 index 000000000..665a4fba5 --- /dev/null +++ b/llama-cpp-bindings/src/marker_role.rs @@ -0,0 +1,79 @@ +use crate::sampled_token_section::SampledTokenSection; + +#[derive(Copy, Clone, Debug, Eq, PartialEq)] +/// A semantic transition performed when a streaming marker is consumed. +pub enum MarkerRole { + ReasoningOpen, + ReasoningClose, + ToolCallOpen, + ToolCallClose, +} + +impl MarkerRole { + #[must_use] + pub const fn opened_section(self) -> Option { + match self { + Self::ReasoningOpen => Some(SampledTokenSection::Reasoning), + Self::ToolCallOpen => Some(SampledTokenSection::ToolCall), + Self::ReasoningClose | Self::ToolCallClose => None, + } + } + + #[must_use] + pub const fn closed_section(self) -> Option { + match self { + Self::ReasoningClose => Some(SampledTokenSection::Reasoning), + Self::ToolCallClose => Some(SampledTokenSection::ToolCall), + Self::ReasoningOpen | Self::ToolCallOpen => None, + } + } +} + +#[cfg(test)] +mod tests { + use super::MarkerRole; + use crate::sampled_token_section::SampledTokenSection; + + struct RoleSection { + role: MarkerRole, + section: SampledTokenSection, + } + + #[test] + fn an_opening_role_opens_its_section_and_closes_nothing() { + let cases = [ + RoleSection { + role: MarkerRole::ReasoningOpen, + section: SampledTokenSection::Reasoning, + }, + RoleSection { + role: MarkerRole::ToolCallOpen, + section: SampledTokenSection::ToolCall, + }, + ]; + + for RoleSection { role, section } in cases { + assert_eq!(role.opened_section(), Some(section)); + assert_eq!(role.closed_section(), None); + } + } + + #[test] + fn a_closing_role_closes_its_section_and_opens_nothing() { + let cases = [ + RoleSection { + role: MarkerRole::ReasoningClose, + section: SampledTokenSection::Reasoning, + }, + RoleSection { + role: MarkerRole::ToolCallClose, + section: SampledTokenSection::ToolCall, + }, + ]; + + for RoleSection { role, section } in cases { + assert_eq!(role.closed_section(), Some(section)); + assert_eq!(role.opened_section(), None); + } + } +} diff --git a/llama-cpp-bindings/src/marker_role_candidate.rs b/llama-cpp-bindings/src/marker_role_candidate.rs new file mode 100644 index 000000000..6fd437378 --- /dev/null +++ b/llama-cpp-bindings/src/marker_role_candidate.rs @@ -0,0 +1,12 @@ +use crate::marker_role::MarkerRole; +use crate::token::LlamaToken; + +#[derive(Clone, Debug, Eq, PartialEq)] +/// A tokenized marker string together with the single role it was detected as. +/// +/// Candidates are merged by token sequence, so the same tokens detected under two +/// roles become one [`crate::streaming_marker::StreamingMarker`] carrying both. +pub struct MarkerRoleCandidate { + pub tokens: Vec, + pub role: MarkerRole, +} diff --git a/llama-cpp-bindings/src/model.rs b/llama-cpp-bindings/src/model.rs index 8ce33b3b4..329c76e6c 100644 --- a/llama-cpp-bindings/src/model.rs +++ b/llama-cpp-bindings/src/model.rs @@ -1,6 +1,8 @@ pub mod add_bos; pub mod llama_chat_message; pub mod llama_chat_template; +pub mod llama_lazy_mode; +pub mod llama_lazy_mode_parse_error; pub mod llama_load_mode; pub mod llama_load_mode_parse_error; pub mod llama_lora_adapter; @@ -37,6 +39,8 @@ use crate::chat_template_tool_calls; use crate::llama_backend::LlamaBackend; use crate::llama_token_attrs::LlamaTokenAttrs; use crate::llama_token_attrs_from_int_error::LlamaTokenAttrsFromIntError; +use crate::marker_role::MarkerRole; +use crate::marker_role_candidate::MarkerRoleCandidate; use crate::model::tokenizer_input::TokenizerInput; use crate::raw_chat_message::RawChatMessage; use crate::resolved_tool_call_markers::ResolvedToolCallMarkers; @@ -56,6 +60,8 @@ use crate::{ pub use add_bos::AddBos; pub use llama_chat_message::LlamaChatMessage; pub use llama_chat_template::LlamaChatTemplate; +pub use llama_lazy_mode::LlamaLazyMode; +pub use llama_lazy_mode_parse_error::LlamaLazyModeParseError; pub use llama_load_mode::LlamaLoadMode; pub use llama_load_mode_parse_error::LlamaLoadModeParseError; pub use llama_lora_adapter::LlamaLoraAdapter; @@ -114,8 +120,8 @@ unsafe fn parsed_chat_free_status_to_result( llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_FREE_ERROR_STRING_ALLOCATION_FAILED => { Err(ParseChatMessageError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_FREE_VENDORED_OUT_OF_MEMORY => { - Err(ParseChatMessageError::VendoredOutOfMemory) + llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_FREE_LLAMA_CPP_OUT_OF_MEMORY => { + Err(ParseChatMessageError::LlamaCppOutOfMemory) } llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_FREE_DESTRUCTOR_THREW_CXX_EXCEPTION => { let message = unsafe { @@ -150,8 +156,8 @@ unsafe fn chat_parser_free_status_to_result( llama_cpp_bindings_sys::LLAMA_RS_CHAT_PARSER_FREE_ERROR_STRING_ALLOCATION_FAILED => { Err(ParseChatMessageError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_CHAT_PARSER_FREE_VENDORED_OUT_OF_MEMORY => { - Err(ParseChatMessageError::VendoredOutOfMemory) + llama_cpp_bindings_sys::LLAMA_RS_CHAT_PARSER_FREE_LLAMA_CPP_OUT_OF_MEMORY => { + Err(ParseChatMessageError::LlamaCppOutOfMemory) } llama_cpp_bindings_sys::LLAMA_RS_CHAT_PARSER_FREE_DESTRUCTOR_THREW_CXX_EXCEPTION => { let message = unsafe { @@ -227,7 +233,7 @@ unsafe fn load_model_from_file_status_to_result( chat_parser: OnceLock::new(), }) } - llama_cpp_bindings_sys::LLAMA_RS_LOAD_MODEL_FROM_FILE_VENDORED_RETURNED_NULL => { + llama_cpp_bindings_sys::LLAMA_RS_LOAD_MODEL_FROM_FILE_LLAMA_CPP_RETURNED_NULL => { if path.exists() { Err(LlamaModelLoadError::Unloadable) } else { @@ -237,10 +243,10 @@ unsafe fn load_model_from_file_status_to_result( llama_cpp_bindings_sys::LLAMA_RS_LOAD_MODEL_FROM_FILE_ERROR_STRING_ALLOCATION_FAILED => { Err(LlamaModelLoadError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_LOAD_MODEL_FROM_FILE_VENDORED_OUT_OF_MEMORY => { - Err(LlamaModelLoadError::VendoredOutOfMemory) + llama_cpp_bindings_sys::LLAMA_RS_LOAD_MODEL_FROM_FILE_LLAMA_CPP_OUT_OF_MEMORY => { + Err(LlamaModelLoadError::LlamaCppOutOfMemory) } - llama_cpp_bindings_sys::LLAMA_RS_LOAD_MODEL_FROM_FILE_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_LOAD_MODEL_FROM_FILE_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string( out_error, @@ -305,10 +311,10 @@ unsafe fn parse_chat_message_status_to_result( llama_cpp_bindings_sys::LLAMA_RS_PARSE_CHAT_MESSAGE_ERROR_STRING_ALLOCATION_FAILED => { Err(ParseChatMessageError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_PARSE_CHAT_MESSAGE_VENDORED_OUT_OF_MEMORY => { - Err(ParseChatMessageError::VendoredOutOfMemory) + llama_cpp_bindings_sys::LLAMA_RS_PARSE_CHAT_MESSAGE_LLAMA_CPP_OUT_OF_MEMORY => { + Err(ParseChatMessageError::LlamaCppOutOfMemory) } - llama_cpp_bindings_sys::LLAMA_RS_PARSE_CHAT_MESSAGE_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_PARSE_CHAT_MESSAGE_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string( *out_error, @@ -384,10 +390,10 @@ unsafe fn chat_parser_create_status_to_result( llama_cpp_bindings_sys::LLAMA_RS_CHAT_PARSER_CREATE_ERROR_STRING_ALLOCATION_FAILED => { Err(ParseChatMessageError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_CHAT_PARSER_CREATE_VENDORED_OUT_OF_MEMORY => { - Err(ParseChatMessageError::VendoredOutOfMemory) + llama_cpp_bindings_sys::LLAMA_RS_CHAT_PARSER_CREATE_LLAMA_CPP_OUT_OF_MEMORY => { + Err(ParseChatMessageError::LlamaCppOutOfMemory) } - llama_cpp_bindings_sys::LLAMA_RS_CHAT_PARSER_CREATE_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_CHAT_PARSER_CREATE_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string( *out_error, @@ -477,10 +483,10 @@ unsafe fn apply_chat_template_status_to_result( llama_cpp_bindings_sys::LLAMA_RS_APPLY_CHAT_TEMPLATE_ERROR_STRING_ALLOCATION_FAILED => { Err(ApplyChatTemplateError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_APPLY_CHAT_TEMPLATE_VENDORED_OUT_OF_MEMORY => { - Err(ApplyChatTemplateError::VendoredOutOfMemory) + llama_cpp_bindings_sys::LLAMA_RS_APPLY_CHAT_TEMPLATE_LLAMA_CPP_OUT_OF_MEMORY => { + Err(ApplyChatTemplateError::LlamaCppOutOfMemory) } - llama_cpp_bindings_sys::LLAMA_RS_APPLY_CHAT_TEMPLATE_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_APPLY_CHAT_TEMPLATE_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string( out_error, @@ -1017,11 +1023,14 @@ impl LlamaModel { let resolved_tool_call_markers = self.resolve_tool_call_marker_strings(autoparser_open, autoparser_close)?; - let mut reasoning_closes = Vec::new(); + let mut candidates = Vec::new(); if let Some(markers) = &reasoning_markers { for marker in &markers.closes { if let Some(tokens) = self.tokenize_marker(Some(marker))? { - reasoning_closes.push(tokens); + candidates.push(MarkerRoleCandidate { + tokens, + role: MarkerRole::ReasoningClose, + }); } } } @@ -1029,14 +1038,26 @@ impl LlamaModel { let reasoning_open = reasoning_markers .as_ref() .map(|markers| markers.open.as_str()); - let reasoning_open = self.tokenize_marker(reasoning_open)?; + if let Some(tokens) = self.tokenize_marker(reasoning_open)? { + candidates.push(MarkerRoleCandidate { + tokens, + role: MarkerRole::ReasoningOpen, + }); + } + if let Some(tokens) = self.tokenize_marker(resolved_tool_call_markers.open.as_deref())? { + candidates.push(MarkerRoleCandidate { + tokens, + role: MarkerRole::ToolCallOpen, + }); + } + if let Some(tokens) = self.tokenize_marker(resolved_tool_call_markers.close.as_deref())? { + candidates.push(MarkerRoleCandidate { + tokens, + role: MarkerRole::ToolCallClose, + }); + } - Ok(StreamingMarkers { - reasoning_open, - reasoning_closes, - tool_call_open: self.tokenize_marker(resolved_tool_call_markers.open.as_deref())?, - tool_call_close: self.tokenize_marker(resolved_tool_call_markers.close.as_deref())?, - }) + StreamingMarkers::from_candidates(candidates) } fn resolve_tool_call_marker_strings( @@ -1373,11 +1394,11 @@ unsafe fn parsed_chat_content_status_to_result( unsafe { llama_cpp_bindings_sys::llama_rs_string_free(out_error) }; Err(ParseChatMessageError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_CONTENT_VENDORED_OUT_OF_MEMORY => { + llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_CONTENT_LLAMA_CPP_OUT_OF_MEMORY => { unsafe { llama_cpp_bindings_sys::llama_rs_string_free(out_error) }; - Err(ParseChatMessageError::VendoredOutOfMemory) + Err(ParseChatMessageError::LlamaCppOutOfMemory) } - llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_CONTENT_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_CONTENT_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string( out_error, @@ -1450,11 +1471,11 @@ unsafe fn parsed_chat_reasoning_content_status_to_result( unsafe { llama_cpp_bindings_sys::llama_rs_string_free(out_error) }; Err(ParseChatMessageError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_REASONING_CONTENT_VENDORED_OUT_OF_MEMORY => { + llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_REASONING_CONTENT_LLAMA_CPP_OUT_OF_MEMORY => { unsafe { llama_cpp_bindings_sys::llama_rs_string_free(out_error) }; - Err(ParseChatMessageError::VendoredOutOfMemory) + Err(ParseChatMessageError::LlamaCppOutOfMemory) } - llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_REASONING_CONTENT_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_REASONING_CONTENT_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string(out_error, "llama_rs_parsed_chat_reasoning_content", "reported a thrown C++ exception without an error message") }?; Err(ParseChatMessageError::Reported { message }) @@ -1520,11 +1541,11 @@ unsafe fn parsed_chat_tool_call_count_status_to_result( unsafe { llama_cpp_bindings_sys::llama_rs_string_free(out_error) }; Err(ParseChatMessageError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_COUNT_VENDORED_OUT_OF_MEMORY => { + llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_COUNT_LLAMA_CPP_OUT_OF_MEMORY => { unsafe { llama_cpp_bindings_sys::llama_rs_string_free(out_error) }; - Err(ParseChatMessageError::VendoredOutOfMemory) + Err(ParseChatMessageError::LlamaCppOutOfMemory) } - llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_COUNT_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_COUNT_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string(out_error, "llama_rs_parsed_chat_tool_call_count", "reported a thrown C++ exception without an error message") }?; Err(ParseChatMessageError::Reported { message }) @@ -1593,11 +1614,11 @@ unsafe fn parsed_chat_tool_call_id_status_to_result( unsafe { llama_cpp_bindings_sys::llama_rs_string_free(out_error) }; Err(ParseChatMessageError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_ID_VENDORED_OUT_OF_MEMORY => { + llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_ID_LLAMA_CPP_OUT_OF_MEMORY => { unsafe { llama_cpp_bindings_sys::llama_rs_string_free(out_error) }; - Err(ParseChatMessageError::VendoredOutOfMemory) + Err(ParseChatMessageError::LlamaCppOutOfMemory) } - llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_ID_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_ID_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string(out_error, "llama_rs_parsed_chat_tool_call_id", "reported a thrown C++ exception without an error message") }?; Err(ParseChatMessageError::Reported { message }) @@ -1671,11 +1692,11 @@ unsafe fn parsed_chat_tool_call_name_status_to_result( unsafe { llama_cpp_bindings_sys::llama_rs_string_free(out_error) }; Err(ParseChatMessageError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_NAME_VENDORED_OUT_OF_MEMORY => { + llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_NAME_LLAMA_CPP_OUT_OF_MEMORY => { unsafe { llama_cpp_bindings_sys::llama_rs_string_free(out_error) }; - Err(ParseChatMessageError::VendoredOutOfMemory) + Err(ParseChatMessageError::LlamaCppOutOfMemory) } - llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_NAME_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_NAME_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string(out_error, "llama_rs_parsed_chat_tool_call_name", "reported a thrown C++ exception without an error message") }?; Err(ParseChatMessageError::Reported { message }) @@ -1749,11 +1770,11 @@ unsafe fn parsed_chat_tool_call_arguments_status_to_result( unsafe { llama_cpp_bindings_sys::llama_rs_string_free(out_error) }; Err(ParseChatMessageError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_ARGUMENTS_VENDORED_OUT_OF_MEMORY => { + llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_ARGUMENTS_LLAMA_CPP_OUT_OF_MEMORY => { unsafe { llama_cpp_bindings_sys::llama_rs_string_free(out_error) }; - Err(ParseChatMessageError::VendoredOutOfMemory) + Err(ParseChatMessageError::LlamaCppOutOfMemory) } - llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_ARGUMENTS_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_ARGUMENTS_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string(out_error, "llama_rs_parsed_chat_tool_call_arguments", "reported a thrown C++ exception without an error message") }?; Err(ParseChatMessageError::Reported { message }) @@ -1953,10 +1974,10 @@ unsafe fn detect_reasoning_markers_status_to_result( llama_cpp_bindings_sys::LLAMA_RS_DETECT_REASONING_MARKERS_ERROR_STRING_ALLOCATION_FAILED => { Err(MarkerDetectionError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_DETECT_REASONING_MARKERS_VENDORED_OUT_OF_MEMORY => { - Err(MarkerDetectionError::VendoredOutOfMemory) + llama_cpp_bindings_sys::LLAMA_RS_DETECT_REASONING_MARKERS_LLAMA_CPP_OUT_OF_MEMORY => { + Err(MarkerDetectionError::LlamaCppOutOfMemory) } - llama_cpp_bindings_sys::LLAMA_RS_DETECT_REASONING_MARKERS_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_DETECT_REASONING_MARKERS_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string(out_error, "llama_rs_detect_reasoning_markers", "reported a thrown C++ exception without an error message") }?; Err(MarkerDetectionError::ReasoningMarkerDetectionFailed { message }) } @@ -2053,8 +2074,8 @@ unsafe fn reasoning_markers_free_status_to_result( llama_cpp_bindings_sys::LLAMA_RS_REASONING_MARKERS_FREE_ERROR_STRING_ALLOCATION_FAILED => { Err(MarkerDetectionError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_REASONING_MARKERS_FREE_VENDORED_OUT_OF_MEMORY => { - Err(MarkerDetectionError::VendoredOutOfMemory) + llama_cpp_bindings_sys::LLAMA_RS_REASONING_MARKERS_FREE_LLAMA_CPP_OUT_OF_MEMORY => { + Err(MarkerDetectionError::LlamaCppOutOfMemory) } llama_cpp_bindings_sys::LLAMA_RS_REASONING_MARKERS_FREE_DESTRUCTOR_THREW_CXX_EXCEPTION => { let message = unsafe { @@ -2138,10 +2159,10 @@ unsafe fn compute_tool_call_haystack_status_to_result( llama_cpp_bindings_sys::LLAMA_RS_COMPUTE_TOOL_CALL_HAYSTACK_ERROR_STRING_ALLOCATION_FAILED => { Err(MarkerDetectionError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_COMPUTE_TOOL_CALL_HAYSTACK_VENDORED_OUT_OF_MEMORY => { - Err(MarkerDetectionError::VendoredOutOfMemory) + llama_cpp_bindings_sys::LLAMA_RS_COMPUTE_TOOL_CALL_HAYSTACK_LLAMA_CPP_OUT_OF_MEMORY => { + Err(MarkerDetectionError::LlamaCppOutOfMemory) } - llama_cpp_bindings_sys::LLAMA_RS_COMPUTE_TOOL_CALL_HAYSTACK_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_COMPUTE_TOOL_CALL_HAYSTACK_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string(out_error, "llama_rs_compute_tool_call_haystack", "reported a thrown C++ exception without an error message") }?; Err(MarkerDetectionError::ToolCallHaystackComputationFailed { message }) } @@ -2222,10 +2243,10 @@ unsafe fn diagnose_tool_call_synthetic_renders_status_to_result( llama_cpp_bindings_sys::LLAMA_RS_DIAGNOSE_TOOL_CALL_SYNTHETIC_RENDERS_ERROR_STRING_ALLOCATION_FAILED => { Err(MarkerDetectionError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_DIAGNOSE_TOOL_CALL_SYNTHETIC_RENDERS_VENDORED_OUT_OF_MEMORY => { - Err(MarkerDetectionError::VendoredOutOfMemory) + llama_cpp_bindings_sys::LLAMA_RS_DIAGNOSE_TOOL_CALL_SYNTHETIC_RENDERS_LLAMA_CPP_OUT_OF_MEMORY => { + Err(MarkerDetectionError::LlamaCppOutOfMemory) } - llama_cpp_bindings_sys::LLAMA_RS_DIAGNOSE_TOOL_CALL_SYNTHETIC_RENDERS_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_DIAGNOSE_TOOL_CALL_SYNTHETIC_RENDERS_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string(out_error, "llama_rs_diagnose_tool_call_synthetic_renders", "reported a thrown C++ exception without an error message") }?; Err(MarkerDetectionError::ToolCallSyntheticRenderDiagnosisFailed { message }) } @@ -2315,10 +2336,10 @@ unsafe fn tokenize_status_to_result( llama_cpp_bindings_sys::LLAMA_RS_TOKENIZE_ERROR_STRING_ALLOCATION_FAILED => { Err(StringToTokenError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_TOKENIZE_VENDORED_OUT_OF_MEMORY => { - Err(StringToTokenError::VendoredOutOfMemory) + llama_cpp_bindings_sys::LLAMA_RS_TOKENIZE_LLAMA_CPP_OUT_OF_MEMORY => { + Err(StringToTokenError::LlamaCppOutOfMemory) } - llama_cpp_bindings_sys::LLAMA_RS_TOKENIZE_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_TOKENIZE_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string( out_error, @@ -2824,10 +2845,10 @@ mod ffi_status_mapping_tests { } #[test] - fn load_model_from_file_vendored_returned_null_for_missing_path_is_file_not_found() { + fn load_model_from_file_llama_cpp_returned_null_for_missing_path_is_file_not_found() { let result = unsafe { load_model_from_file_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_LOAD_MODEL_FROM_FILE_VENDORED_RETURNED_NULL, + llama_cpp_bindings_sys::LLAMA_RS_LOAD_MODEL_FROM_FILE_LLAMA_CPP_RETURNED_NULL, ptr::null_mut(), ptr::null_mut(), Path::new("/definitely/missing/model.gguf"), @@ -2860,7 +2881,7 @@ mod ffi_status_mapping_tests { fn load_model_from_file_cxx_exception_without_a_message_is_a_contract_error() { let result = unsafe { load_model_from_file_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_LOAD_MODEL_FROM_FILE_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_LOAD_MODEL_FROM_FILE_LLAMA_CPP_THREW_CXX_EXCEPTION, ptr::null_mut(), ptr::null_mut(), Path::new("/some/path"), @@ -2977,14 +2998,14 @@ mod ffi_status_mapping_tests { }; let result = unsafe { chat_parser_create_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_CHAT_PARSER_CREATE_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_CHAT_PARSER_CREATE_LLAMA_CPP_THREW_CXX_EXCEPTION, ptr::null_mut(), &raw mut out_error, ) }; let Err(ParseChatMessageError::ParserCreationFailed { message }) = result else { - panic!("the vendored exception status must surface the wrapper message"); + panic!("the llama.cpp exception status must surface the wrapper message"); }; assert_eq!(message, "the parser could not be built"); @@ -3056,14 +3077,14 @@ mod ffi_status_mapping_tests { }; let result = unsafe { parse_chat_message_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_PARSE_CHAT_MESSAGE_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_PARSE_CHAT_MESSAGE_LLAMA_CPP_THREW_CXX_EXCEPTION, ptr::null_mut(), &raw mut out_error, ) }; let Err(ParseChatMessageError::MessageUnrecognized { message }) = result else { - panic!("the vendored exception status must surface the wrapper message"); + panic!("the llama.cpp exception status must surface the wrapper message"); }; assert_eq!(message, "the message could not be parsed"); @@ -3132,14 +3153,14 @@ mod ffi_status_mapping_tests { unsafe { llama_cpp_bindings_sys::llama_rs_string_dup(c"content read failed".as_ptr()) }; let result = unsafe { parsed_chat_content_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_CONTENT_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_CONTENT_LLAMA_CPP_THREW_CXX_EXCEPTION, ptr::null_mut(), out_error, ) }; let Err(ParseChatMessageError::Reported { message }) = result else { - panic!("the vendored exception status must surface the wrapper message"); + panic!("the llama.cpp exception status must surface the wrapper message"); }; assert_eq!(message, "content read failed"); @@ -3203,14 +3224,14 @@ mod ffi_status_mapping_tests { }; let result = unsafe { parsed_chat_reasoning_content_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_REASONING_CONTENT_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_REASONING_CONTENT_LLAMA_CPP_THREW_CXX_EXCEPTION, ptr::null_mut(), out_error, ) }; let Err(ParseChatMessageError::Reported { message }) = result else { - panic!("the vendored exception status must surface the wrapper message"); + panic!("the llama.cpp exception status must surface the wrapper message"); }; assert_eq!(message, "reasoning read failed"); @@ -3267,14 +3288,14 @@ mod ffi_status_mapping_tests { }; let result = unsafe { parsed_chat_tool_call_count_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_COUNT_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_COUNT_LLAMA_CPP_THREW_CXX_EXCEPTION, 0, out_error, ) }; let Err(ParseChatMessageError::Reported { message }) = result else { - panic!("the vendored exception status must surface the wrapper message"); + panic!("the llama.cpp exception status must surface the wrapper message"); }; assert_eq!(message, "tool-call count failed"); @@ -3357,7 +3378,7 @@ mod ffi_status_mapping_tests { }; let result = unsafe { parsed_chat_tool_call_id_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_ID_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_ID_LLAMA_CPP_THREW_CXX_EXCEPTION, 0, ptr::null_mut(), out_error, @@ -3365,7 +3386,7 @@ mod ffi_status_mapping_tests { }; let Err(ParseChatMessageError::Reported { message }) = result else { - panic!("the vendored exception status must surface the wrapper message"); + panic!("the llama.cpp exception status must surface the wrapper message"); }; assert_eq!(message, "tool-call id read failed"); @@ -3449,7 +3470,7 @@ mod ffi_status_mapping_tests { }; let result = unsafe { parsed_chat_tool_call_name_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_NAME_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_NAME_LLAMA_CPP_THREW_CXX_EXCEPTION, 0, ptr::null_mut(), out_error, @@ -3457,7 +3478,7 @@ mod ffi_status_mapping_tests { }; let Err(ParseChatMessageError::Reported { message }) = result else { - panic!("the vendored exception status must surface the wrapper message"); + panic!("the llama.cpp exception status must surface the wrapper message"); }; assert_eq!(message, "tool-call name read failed"); @@ -3541,7 +3562,7 @@ mod ffi_status_mapping_tests { }; let result = unsafe { parsed_chat_tool_call_arguments_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_ARGUMENTS_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_ARGUMENTS_LLAMA_CPP_THREW_CXX_EXCEPTION, 0, ptr::null_mut(), out_error, @@ -3549,7 +3570,7 @@ mod ffi_status_mapping_tests { }; let Err(ParseChatMessageError::Reported { message }) = result else { - panic!("the vendored exception status must surface the wrapper message"); + panic!("the llama.cpp exception status must surface the wrapper message"); }; assert_eq!(message, "tool-call arguments read failed"); @@ -3605,7 +3626,7 @@ mod ffi_status_mapping_tests { fn detect_reasoning_markers_cxx_exception_without_a_message_is_a_contract_error() { let result = unsafe { detect_reasoning_markers_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_DETECT_REASONING_MARKERS_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_DETECT_REASONING_MARKERS_LLAMA_CPP_THREW_CXX_EXCEPTION, ptr::null(), ptr::null_mut(), ) @@ -3805,7 +3826,7 @@ mod ffi_status_mapping_tests { fn compute_tool_call_haystack_cxx_exception_without_a_message_is_a_contract_error() { let result = unsafe { compute_tool_call_haystack_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_COMPUTE_TOOL_CALL_HAYSTACK_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_COMPUTE_TOOL_CALL_HAYSTACK_LLAMA_CPP_THREW_CXX_EXCEPTION, ptr::null(), ptr::null_mut(), ) @@ -3875,7 +3896,7 @@ mod ffi_status_mapping_tests { fn diagnose_tool_call_synthetic_renders_cxx_exception_without_a_message_is_a_contract_error() { let result = unsafe { diagnose_tool_call_synthetic_renders_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_DIAGNOSE_TOOL_CALL_SYNTHETIC_RENDERS_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_DIAGNOSE_TOOL_CALL_SYNTHETIC_RENDERS_LLAMA_CPP_THREW_CXX_EXCEPTION, ptr::null(), ptr::null(), ptr::null_mut(), @@ -3942,7 +3963,7 @@ mod ffi_status_mapping_tests { fn tokenize_cxx_exception_without_a_message_is_a_contract_error() { let result = unsafe { tokenize_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_TOKENIZE_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_TOKENIZE_LLAMA_CPP_THREW_CXX_EXCEPTION, 0, ptr::null_mut(), ) @@ -4055,7 +4076,7 @@ mod ffi_status_mapping_tests { let out_error = unsafe { llama_cpp_bindings_sys::llama_rs_string_dup(message.as_ptr()) }; let result = unsafe { super::apply_chat_template_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_APPLY_CHAT_TEMPLATE_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_APPLY_CHAT_TEMPLATE_LLAMA_CPP_THREW_CXX_EXCEPTION, ptr::null_mut(), out_error, ) @@ -4415,16 +4436,16 @@ mod ffi_status_mapping_tests { } #[test] - fn chat_parser_free_vendored_out_of_memory_is_preserved() { + fn chat_parser_free_llama_cpp_out_of_memory_is_preserved() { let result = unsafe { chat_parser_free_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_CHAT_PARSER_FREE_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_CHAT_PARSER_FREE_LLAMA_CPP_OUT_OF_MEMORY, ptr::null_mut(), ) }; - let Err(ParseChatMessageError::VendoredOutOfMemory) = result else { - panic!("a vendored allocation failure must be reported as its own variant"); + let Err(ParseChatMessageError::LlamaCppOutOfMemory) = result else { + panic!("a llama.cpp allocation failure must be reported as its own variant"); }; } @@ -4494,16 +4515,16 @@ mod ffi_status_mapping_tests { } #[test] - fn parsed_chat_free_vendored_out_of_memory_is_preserved() { + fn parsed_chat_free_llama_cpp_out_of_memory_is_preserved() { let result = unsafe { parsed_chat_free_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_FREE_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_FREE_LLAMA_CPP_OUT_OF_MEMORY, ptr::null_mut(), ) }; - let Err(ParseChatMessageError::VendoredOutOfMemory) = result else { - panic!("a vendored allocation failure must be reported as its own variant"); + let Err(ParseChatMessageError::LlamaCppOutOfMemory) = result else { + panic!("a llama.cpp allocation failure must be reported as its own variant"); }; } @@ -4568,15 +4589,15 @@ mod ffi_status_mapping_tests { } #[test] - fn reasoning_markers_free_vendored_out_of_memory_is_preserved() { + fn reasoning_markers_free_llama_cpp_out_of_memory_is_preserved() { let result = unsafe { reasoning_markers_free_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_REASONING_MARKERS_FREE_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_REASONING_MARKERS_FREE_LLAMA_CPP_OUT_OF_MEMORY, ptr::null_mut(), ) }; - assert_eq!(result, Err(MarkerDetectionError::VendoredOutOfMemory)); + assert_eq!(result, Err(MarkerDetectionError::LlamaCppOutOfMemory)); } #[test] @@ -4697,7 +4718,7 @@ mod ffi_contract_status_tests { ); let outcome_3 = unsafe { load_model_from_file_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_LOAD_MODEL_FROM_FILE_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_LOAD_MODEL_FROM_FILE_LLAMA_CPP_OUT_OF_MEMORY, ptr::null_mut(), ptr::null_mut(), Path::new("/missing-for-contract-test.gguf"), @@ -4705,7 +4726,7 @@ mod ffi_contract_status_tests { }; assert_eq!( outcome_3.err(), - Some(LlamaModelLoadError::VendoredOutOfMemory) + Some(LlamaModelLoadError::LlamaCppOutOfMemory) ); } @@ -4782,14 +4803,14 @@ mod ffi_contract_status_tests { ); let outcome_4 = unsafe { parse_chat_message_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_PARSE_CHAT_MESSAGE_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_PARSE_CHAT_MESSAGE_LLAMA_CPP_OUT_OF_MEMORY, ptr::null_mut(), &raw mut out_error_slot, ) }; - let Err(ParseChatMessageError::VendoredOutOfMemory) = outcome_4 else { + let Err(ParseChatMessageError::LlamaCppOutOfMemory) = outcome_4 else { panic!( - "LLAMA_RS_PARSE_CHAT_MESSAGE_VENDORED_OUT_OF_MEMORY must map to VendoredOutOfMemory" + "LLAMA_RS_PARSE_CHAT_MESSAGE_LLAMA_CPP_OUT_OF_MEMORY must map to LlamaCppOutOfMemory" ); }; } @@ -4850,14 +4871,14 @@ mod ffi_contract_status_tests { ); let outcome_3 = unsafe { chat_parser_create_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_CHAT_PARSER_CREATE_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_CHAT_PARSER_CREATE_LLAMA_CPP_OUT_OF_MEMORY, ptr::null_mut(), &raw mut out_error_slot, ) }; - let Err(ParseChatMessageError::VendoredOutOfMemory) = outcome_3 else { + let Err(ParseChatMessageError::LlamaCppOutOfMemory) = outcome_3 else { panic!( - "LLAMA_RS_CHAT_PARSER_CREATE_VENDORED_OUT_OF_MEMORY must map to VendoredOutOfMemory" + "LLAMA_RS_CHAT_PARSER_CREATE_LLAMA_CPP_OUT_OF_MEMORY must map to LlamaCppOutOfMemory" ); }; } @@ -4951,14 +4972,14 @@ mod ffi_contract_status_tests { ); let outcome_5 = unsafe { apply_chat_template_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_APPLY_CHAT_TEMPLATE_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_APPLY_CHAT_TEMPLATE_LLAMA_CPP_OUT_OF_MEMORY, ptr::null_mut(), ptr::null_mut(), ) }; assert_eq!( outcome_5.err(), - Some(ApplyChatTemplateError::VendoredOutOfMemory) + Some(ApplyChatTemplateError::LlamaCppOutOfMemory) ); } @@ -5000,14 +5021,14 @@ mod ffi_contract_status_tests { ); let outcome_2 = unsafe { parsed_chat_content_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_CONTENT_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_CONTENT_LLAMA_CPP_OUT_OF_MEMORY, ptr::null_mut(), ptr::null_mut(), ) }; - let Err(ParseChatMessageError::VendoredOutOfMemory) = outcome_2 else { + let Err(ParseChatMessageError::LlamaCppOutOfMemory) = outcome_2 else { panic!( - "LLAMA_RS_PARSED_CHAT_CONTENT_VENDORED_OUT_OF_MEMORY must map to VendoredOutOfMemory" + "LLAMA_RS_PARSED_CHAT_CONTENT_LLAMA_CPP_OUT_OF_MEMORY must map to LlamaCppOutOfMemory" ); }; } @@ -5053,11 +5074,11 @@ mod ffi_contract_status_tests { } ); let outcome_2 = unsafe { - parsed_chat_reasoning_content_status_to_result(llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_REASONING_CONTENT_VENDORED_OUT_OF_MEMORY, ptr::null_mut(), ptr::null_mut()) + parsed_chat_reasoning_content_status_to_result(llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_REASONING_CONTENT_LLAMA_CPP_OUT_OF_MEMORY, ptr::null_mut(), ptr::null_mut()) }; - let Err(ParseChatMessageError::VendoredOutOfMemory) = outcome_2 else { + let Err(ParseChatMessageError::LlamaCppOutOfMemory) = outcome_2 else { panic!( - "LLAMA_RS_PARSED_CHAT_REASONING_CONTENT_VENDORED_OUT_OF_MEMORY must map to VendoredOutOfMemory" + "LLAMA_RS_PARSED_CHAT_REASONING_CONTENT_LLAMA_CPP_OUT_OF_MEMORY must map to LlamaCppOutOfMemory" ); }; } @@ -5104,14 +5125,14 @@ mod ffi_contract_status_tests { ); let outcome_2 = unsafe { parsed_chat_tool_call_count_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_COUNT_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_COUNT_LLAMA_CPP_OUT_OF_MEMORY, 0, ptr::null_mut(), ) }; - let Err(ParseChatMessageError::VendoredOutOfMemory) = outcome_2 else { + let Err(ParseChatMessageError::LlamaCppOutOfMemory) = outcome_2 else { panic!( - "LLAMA_RS_PARSED_CHAT_TOOL_CALL_COUNT_VENDORED_OUT_OF_MEMORY must map to VendoredOutOfMemory" + "LLAMA_RS_PARSED_CHAT_TOOL_CALL_COUNT_LLAMA_CPP_OUT_OF_MEMORY must map to LlamaCppOutOfMemory" ); }; } @@ -5160,15 +5181,15 @@ mod ffi_contract_status_tests { ); let outcome_2 = unsafe { parsed_chat_tool_call_id_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_ID_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_ID_LLAMA_CPP_OUT_OF_MEMORY, 0, ptr::null_mut(), ptr::null_mut(), ) }; - let Err(ParseChatMessageError::VendoredOutOfMemory) = outcome_2 else { + let Err(ParseChatMessageError::LlamaCppOutOfMemory) = outcome_2 else { panic!( - "LLAMA_RS_PARSED_CHAT_TOOL_CALL_ID_VENDORED_OUT_OF_MEMORY must map to VendoredOutOfMemory" + "LLAMA_RS_PARSED_CHAT_TOOL_CALL_ID_LLAMA_CPP_OUT_OF_MEMORY must map to LlamaCppOutOfMemory" ); }; } @@ -5217,15 +5238,15 @@ mod ffi_contract_status_tests { ); let outcome_2 = unsafe { parsed_chat_tool_call_name_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_NAME_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_NAME_LLAMA_CPP_OUT_OF_MEMORY, 0, ptr::null_mut(), ptr::null_mut(), ) }; - let Err(ParseChatMessageError::VendoredOutOfMemory) = outcome_2 else { + let Err(ParseChatMessageError::LlamaCppOutOfMemory) = outcome_2 else { panic!( - "LLAMA_RS_PARSED_CHAT_TOOL_CALL_NAME_VENDORED_OUT_OF_MEMORY must map to VendoredOutOfMemory" + "LLAMA_RS_PARSED_CHAT_TOOL_CALL_NAME_LLAMA_CPP_OUT_OF_MEMORY must map to LlamaCppOutOfMemory" ); }; } @@ -5268,11 +5289,11 @@ mod ffi_contract_status_tests { } ); let outcome_2 = unsafe { - parsed_chat_tool_call_arguments_status_to_result(llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_ARGUMENTS_VENDORED_OUT_OF_MEMORY, 0, ptr::null_mut(), ptr::null_mut()) + parsed_chat_tool_call_arguments_status_to_result(llama_cpp_bindings_sys::LLAMA_RS_PARSED_CHAT_TOOL_CALL_ARGUMENTS_LLAMA_CPP_OUT_OF_MEMORY, 0, ptr::null_mut(), ptr::null_mut()) }; - let Err(ParseChatMessageError::VendoredOutOfMemory) = outcome_2 else { + let Err(ParseChatMessageError::LlamaCppOutOfMemory) = outcome_2 else { panic!( - "LLAMA_RS_PARSED_CHAT_TOOL_CALL_ARGUMENTS_VENDORED_OUT_OF_MEMORY must map to VendoredOutOfMemory" + "LLAMA_RS_PARSED_CHAT_TOOL_CALL_ARGUMENTS_LLAMA_CPP_OUT_OF_MEMORY must map to LlamaCppOutOfMemory" ); }; } @@ -5281,14 +5302,14 @@ mod ffi_contract_status_tests { fn detect_reasoning_markers_status_to_result_maps_every_contract_status() { let outcome_0 = unsafe { detect_reasoning_markers_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_DETECT_REASONING_MARKERS_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_DETECT_REASONING_MARKERS_LLAMA_CPP_OUT_OF_MEMORY, ptr::null(), ptr::null_mut(), ) }; assert_eq!( outcome_0.err(), - Some(MarkerDetectionError::VendoredOutOfMemory) + Some(MarkerDetectionError::LlamaCppOutOfMemory) ); } @@ -5347,14 +5368,14 @@ mod ffi_contract_status_tests { ); let outcome_3 = unsafe { compute_tool_call_haystack_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_COMPUTE_TOOL_CALL_HAYSTACK_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_COMPUTE_TOOL_CALL_HAYSTACK_LLAMA_CPP_OUT_OF_MEMORY, ptr::null(), ptr::null_mut(), ) }; assert_eq!( outcome_3.err(), - Some(MarkerDetectionError::VendoredOutOfMemory) + Some(MarkerDetectionError::LlamaCppOutOfMemory) ); } @@ -5413,11 +5434,11 @@ mod ffi_contract_status_tests { ) ); let outcome_4 = unsafe { - diagnose_tool_call_synthetic_renders_status_to_result(llama_cpp_bindings_sys::LLAMA_RS_DIAGNOSE_TOOL_CALL_SYNTHETIC_RENDERS_VENDORED_OUT_OF_MEMORY, ptr::null(), ptr::null(), ptr::null_mut()) + diagnose_tool_call_synthetic_renders_status_to_result(llama_cpp_bindings_sys::LLAMA_RS_DIAGNOSE_TOOL_CALL_SYNTHETIC_RENDERS_LLAMA_CPP_OUT_OF_MEMORY, ptr::null(), ptr::null(), ptr::null_mut()) }; assert_eq!( outcome_4.err(), - Some(MarkerDetectionError::VendoredOutOfMemory) + Some(MarkerDetectionError::LlamaCppOutOfMemory) ); } @@ -5493,14 +5514,14 @@ mod ffi_contract_status_tests { ); let outcome_4 = unsafe { tokenize_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_TOKENIZE_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_TOKENIZE_LLAMA_CPP_OUT_OF_MEMORY, 0, ptr::null_mut(), ) }; assert_eq!( outcome_4.err(), - Some(StringToTokenError::VendoredOutOfMemory) + Some(StringToTokenError::LlamaCppOutOfMemory) ); } } diff --git a/llama-cpp-bindings/src/model/llama_lazy_mode.rs b/llama-cpp-bindings/src/model/llama_lazy_mode.rs new file mode 100644 index 000000000..c3745c784 --- /dev/null +++ b/llama-cpp-bindings/src/model/llama_lazy_mode.rs @@ -0,0 +1,89 @@ +use crate::model::llama_lazy_mode_parse_error::LlamaLazyModeParseError; + +#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)] +pub enum LlamaLazyMode { + Off, + #[default] + Auto, + On, +} + +impl TryFrom for LlamaLazyMode { + type Error = LlamaLazyModeParseError; + + fn try_from(value: llama_cpp_bindings_sys::llama_lazy_mode) -> Result { + match value { + llama_cpp_bindings_sys::LLAMA_LAZY_MODE_OFF => Ok(Self::Off), + llama_cpp_bindings_sys::LLAMA_LAZY_MODE_AUTO => Ok(Self::Auto), + llama_cpp_bindings_sys::LLAMA_LAZY_MODE_ON => Ok(Self::On), + value => Err(LlamaLazyModeParseError { + value: i64::from(value), + }), + } + } +} + +impl From for llama_cpp_bindings_sys::llama_lazy_mode { + fn from(value: LlamaLazyMode) -> Self { + match value { + LlamaLazyMode::Off => llama_cpp_bindings_sys::LLAMA_LAZY_MODE_OFF, + LlamaLazyMode::Auto => llama_cpp_bindings_sys::LLAMA_LAZY_MODE_AUTO, + LlamaLazyMode::On => llama_cpp_bindings_sys::LLAMA_LAZY_MODE_ON, + } + } +} + +#[cfg(test)] +mod tests { + use super::LlamaLazyMode; + use crate::model::llama_lazy_mode_parse_error::LlamaLazyModeParseError; + + const LAZY_MODES: &[(LlamaLazyMode, llama_cpp_bindings_sys::llama_lazy_mode)] = &[ + ( + LlamaLazyMode::Off, + llama_cpp_bindings_sys::LLAMA_LAZY_MODE_OFF, + ), + ( + LlamaLazyMode::Auto, + llama_cpp_bindings_sys::LLAMA_LAZY_MODE_AUTO, + ), + ( + LlamaLazyMode::On, + llama_cpp_bindings_sys::LLAMA_LAZY_MODE_ON, + ), + ]; + + #[test] + fn every_rust_lazy_mode_maps_to_its_ffi_value() { + for &(lazy_mode, ffi_value) in LAZY_MODES { + assert_eq!( + llama_cpp_bindings_sys::llama_lazy_mode::from(lazy_mode), + ffi_value + ); + } + } + + #[test] + fn every_ffi_lazy_mode_maps_to_its_rust_value() { + for &(lazy_mode, ffi_value) in LAZY_MODES { + assert_eq!(LlamaLazyMode::try_from(ffi_value), Ok(lazy_mode)); + } + } + + #[test] + fn unknown_ffi_lazy_mode_preserves_its_value() { + let unknown = llama_cpp_bindings_sys::llama_lazy_mode::MAX; + + assert_eq!( + LlamaLazyMode::try_from(unknown), + Err(LlamaLazyModeParseError { + value: i64::from(unknown) + }) + ); + } + + #[test] + fn default_lazy_mode_is_auto() { + assert_eq!(LlamaLazyMode::default(), LlamaLazyMode::Auto); + } +} diff --git a/llama-cpp-bindings/src/model/llama_lazy_mode_parse_error.rs b/llama-cpp-bindings/src/model/llama_lazy_mode_parse_error.rs new file mode 100644 index 000000000..e32390169 --- /dev/null +++ b/llama-cpp-bindings/src/model/llama_lazy_mode_parse_error.rs @@ -0,0 +1,5 @@ +#[derive(Clone, Copy, Debug, Eq, PartialEq, thiserror::Error)] +#[error("unknown llama lazy mode {value}")] +pub struct LlamaLazyModeParseError { + pub value: i64, +} diff --git a/llama-cpp-bindings/src/model/params.rs b/llama-cpp-bindings/src/model/params.rs index fe55927f5..949475dbc 100644 --- a/llama-cpp-bindings/src/model/params.rs +++ b/llama-cpp-bindings/src/model/params.rs @@ -6,6 +6,8 @@ use std::ptr::null; use crate::LlamaCppError; use crate::context::params::LlamaContextParams; use crate::error::{FitError, ModelParamsError}; +use crate::model::llama_lazy_mode::LlamaLazyMode; +use crate::model::llama_lazy_mode_parse_error::LlamaLazyModeParseError; use crate::model::llama_load_mode::LlamaLoadMode; use crate::model::llama_load_mode_parse_error::LlamaLoadModeParseError; use crate::model::llama_split_mode_parse_error::LlamaSplitModeParseError; @@ -32,13 +34,13 @@ fn fit_params_status_to_result( ) -> Result<(), FitError> { match status { llama_cpp_bindings_sys::LLAMA_RS_FIT_PARAMS_OK => Ok(()), - llama_cpp_bindings_sys::LLAMA_RS_FIT_PARAMS_VENDORED_REPORTED_FAILURE => { + llama_cpp_bindings_sys::LLAMA_RS_FIT_PARAMS_LLAMA_CPP_REPORTED_FAILURE => { Err(FitError::NoFittingMemoryLayout) } - llama_cpp_bindings_sys::LLAMA_RS_FIT_PARAMS_VENDORED_REPORTED_ERROR => { + llama_cpp_bindings_sys::LLAMA_RS_FIT_PARAMS_LLAMA_CPP_REPORTED_ERROR => { Err(FitError::Aborted) } - llama_cpp_bindings_sys::LLAMA_RS_FIT_PARAMS_VENDORED_RETURNED_UNRECOGNIZED_STATUS_CODE => { + llama_cpp_bindings_sys::LLAMA_RS_FIT_PARAMS_LLAMA_CPP_RETURNED_UNRECOGNIZED_STATUS_CODE => { Err(FitError::UnknownStatus { code: out_unrecognized_status_code, }) @@ -46,10 +48,10 @@ fn fit_params_status_to_result( llama_cpp_bindings_sys::LLAMA_RS_FIT_PARAMS_ERROR_STRING_ALLOCATION_FAILED => { Err(FitError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_FIT_PARAMS_VENDORED_OUT_OF_MEMORY => { - Err(FitError::VendoredOutOfMemory) + llama_cpp_bindings_sys::LLAMA_RS_FIT_PARAMS_LLAMA_CPP_OUT_OF_MEMORY => { + Err(FitError::LlamaCppOutOfMemory) } - llama_cpp_bindings_sys::LLAMA_RS_FIT_PARAMS_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_FIT_PARAMS_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string( out_error, @@ -137,6 +139,7 @@ impl Debug for LlamaModelParams { .field("n_gpu_layers", &self.params.n_gpu_layers) .field("main_gpu", &self.params.main_gpu) .field("vocab_only", &self.params.vocab_only) + .field("lazy_mode", &self.lazy_mode()) .field("load_mode", &self.load_mode()) .field("load_mtp", &self.params.load_mtp) .field("split_mode", &self.split_mode()) @@ -273,6 +276,12 @@ impl LlamaModelParams { self.params.vocab_only } + /// # Errors + /// Returns [`LlamaLazyModeParseError`] when llama.cpp returns an unknown lazy mode. + pub fn lazy_mode(&self) -> Result { + LlamaLazyMode::try_from(self.params.lazy_mode) + } + /// # Errors /// Returns [`LlamaLoadModeParseError`] when llama.cpp returns an unknown load mode. pub fn load_mode(&self) -> Result { @@ -344,6 +353,12 @@ impl LlamaModelParams { self } + #[must_use] + pub fn with_lazy_mode(mut self, lazy_mode: LlamaLazyMode) -> Self { + self.params.lazy_mode = lazy_mode.into(); + self + } + #[must_use] pub fn with_load_mode(mut self, load_mode: LlamaLoadMode) -> Self { self.params.load_mode = load_mode.into(); @@ -388,7 +403,7 @@ impl LlamaModelParams { impl LlamaModelParams { /// # Errors /// - /// Returns one of the [`FitError`] variants matching the vendored wrapper's status code. + /// Returns one of the [`FitError`] variants matching the llama.cpp wrapper's status code. pub fn fit_params( mut self: Pin<&mut Self>, model_path: &CStr, @@ -475,6 +490,8 @@ impl Default for LlamaModelParams { #[cfg(test)] mod tests { + use crate::model::llama_lazy_mode::LlamaLazyMode; + use crate::model::llama_lazy_mode_parse_error::LlamaLazyModeParseError; use crate::model::llama_load_mode::LlamaLoadMode; use crate::model::split_mode::LlamaSplitMode; @@ -503,6 +520,36 @@ mod tests { assert!(params.devices().is_empty()); } + #[test] + fn default_params_use_automatic_lazy_loading() { + let params = LlamaModelParams::default(); + + assert_eq!(params.lazy_mode(), Ok(LlamaLazyMode::Auto)); + } + + #[test] + fn with_lazy_mode_sets_each_supported_mode() { + for mode in [LlamaLazyMode::Off, LlamaLazyMode::Auto, LlamaLazyMode::On] { + let params = LlamaModelParams::default().with_lazy_mode(mode); + + assert_eq!(params.lazy_mode(), Ok(mode)); + } + } + + #[test] + fn unknown_lazy_mode_in_model_params_preserves_its_value() { + let mut params = LlamaModelParams::default(); + let unknown = llama_cpp_bindings_sys::llama_lazy_mode::MAX; + params.params.lazy_mode = unknown; + + assert_eq!( + params.lazy_mode(), + Err(LlamaLazyModeParseError { + value: i64::from(unknown) + }) + ); + } + #[test] fn with_n_gpu_layers_sets_the_offload_count() { let params = LlamaModelParams::default().with_n_gpu_layers(999); @@ -609,6 +656,14 @@ mod tests { assert!(debug_output.contains("split_mode")); } + #[test] + fn debug_format_includes_lazy_mode() { + let debug_output = format!("{:?}", LlamaModelParams::default()); + + assert!(debug_output.contains("lazy_mode")); + assert!(debug_output.contains("Auto")); + } + #[test] fn builder_chaining_preserves_all_values() { let params = LlamaModelParams::default() @@ -844,7 +899,7 @@ mod tests { #[test] fn fit_params_status_reported_failure_returns_no_fitting_memory_layout() { let result = super::fit_params_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_FIT_PARAMS_VENDORED_REPORTED_FAILURE, + llama_cpp_bindings_sys::LLAMA_RS_FIT_PARAMS_LLAMA_CPP_REPORTED_FAILURE, 0, std::ptr::null_mut(), ); @@ -855,7 +910,7 @@ mod tests { #[test] fn fit_params_status_reported_error_returns_aborted() { let result = super::fit_params_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_FIT_PARAMS_VENDORED_REPORTED_ERROR, + llama_cpp_bindings_sys::LLAMA_RS_FIT_PARAMS_LLAMA_CPP_REPORTED_ERROR, 0, std::ptr::null_mut(), ); @@ -866,7 +921,7 @@ mod tests { #[test] fn fit_params_status_unrecognized_code_returns_unknown_status() { let result = super::fit_params_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_FIT_PARAMS_VENDORED_RETURNED_UNRECOGNIZED_STATUS_CODE, + llama_cpp_bindings_sys::LLAMA_RS_FIT_PARAMS_LLAMA_CPP_RETURNED_UNRECOGNIZED_STATUS_CODE, 42, std::ptr::null_mut(), ); @@ -891,7 +946,7 @@ mod tests { #[test] fn fit_params_status_cxx_exception_without_a_message_is_a_contract_error_with_unknown_error() { let result = super::fit_params_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_FIT_PARAMS_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_FIT_PARAMS_LLAMA_CPP_THREW_CXX_EXCEPTION, 0, std::ptr::null_mut(), ); @@ -984,13 +1039,13 @@ mod ffi_contract_status_tests { } #[test] - fn fit_params_status_vendored_out_of_memory_returns_vendored_out_of_memory() { + fn fit_params_status_llama_cpp_out_of_memory_returns_llama_cpp_out_of_memory() { let outcome = fit_params_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_FIT_PARAMS_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_FIT_PARAMS_LLAMA_CPP_OUT_OF_MEMORY, 0, ptr::null_mut(), ); - assert_eq!(outcome.err(), Some(FitError::VendoredOutOfMemory)); + assert_eq!(outcome.err(), Some(FitError::LlamaCppOutOfMemory)); } } diff --git a/llama-cpp-bindings/src/mtmd/mtmd_bitmap.rs b/llama-cpp-bindings/src/mtmd/mtmd_bitmap.rs index 291296744..01c104be6 100644 --- a/llama-cpp-bindings/src/mtmd/mtmd_bitmap.rs +++ b/llama-cpp-bindings/src/mtmd/mtmd_bitmap.rs @@ -42,7 +42,7 @@ unsafe fn from_file_status_to_result( })?; Ok(MtmdBitmap { bitmap }) } - llama_cpp_bindings_sys::LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_VENDORED_RETURNED_NULL => { + llama_cpp_bindings_sys::LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_LLAMA_CPP_RETURNED_NULL => { Err(MtmdBitmapError::FileUnreadable { path: PathBuf::from(path), }) @@ -50,10 +50,10 @@ unsafe fn from_file_status_to_result( llama_cpp_bindings_sys::LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_ERROR_STRING_ALLOCATION_FAILED => { Err(MtmdBitmapError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_VENDORED_OUT_OF_MEMORY => { - Err(MtmdBitmapError::VendoredOutOfMemory) + llama_cpp_bindings_sys::LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_LLAMA_CPP_OUT_OF_MEMORY => { + Err(MtmdBitmapError::LlamaCppOutOfMemory) } - llama_cpp_bindings_sys::LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string(out_error, "llama_rs_mtmd_bitmap_init_from_file", "reported a thrown C++ exception without an error message") }?; Err(MtmdBitmapError::Reported { message }) } @@ -80,6 +80,63 @@ unsafe fn from_file_status_to_result( } } +/// # Safety +/// +/// `out_bitmap` must be either null or a valid pointer to an `mtmd_bitmap` +/// allocated by `llama_rs_mtmd_bitmap_init_from_buf`. `out_error` must be +/// either null or a valid pointer to a null-terminated C string allocated by +/// `llama_rs_dup_string`. +unsafe fn from_buf_status_to_result( + status: llama_cpp_bindings_sys::llama_rs_mtmd_bitmap_init_from_buf_status, + out_bitmap: *mut llama_cpp_bindings_sys::mtmd_bitmap, + out_error: *mut c_char, +) -> Result { + match status { + llama_cpp_bindings_sys::LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_OK => { + let bitmap = NonNull::new(out_bitmap).ok_or_else(|| { + MtmdBitmapError::from(crate::FfiContractError { + operation: "llama_rs_mtmd_bitmap_init_from_buf", + detail: "success status contained a null bitmap", + }) + })?; + Ok(MtmdBitmap { bitmap }) + } + llama_cpp_bindings_sys::LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_LLAMA_CPP_RETURNED_NULL => { + Err(MtmdBitmapError::BitmapDecodeFailed) + } + llama_cpp_bindings_sys::LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_ERROR_STRING_ALLOCATION_FAILED => { + Err(MtmdBitmapError::NotEnoughMemory) + } + llama_cpp_bindings_sys::LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_LLAMA_CPP_OUT_OF_MEMORY => { + Err(MtmdBitmapError::LlamaCppOutOfMemory) + } + llama_cpp_bindings_sys::LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_LLAMA_CPP_THREW_CXX_EXCEPTION => { + let message = unsafe { read_and_free_cpp_string(out_error, "llama_rs_mtmd_bitmap_init_from_buf", "reported a thrown C++ exception without an error message") }?; + Err(MtmdBitmapError::Reported { message }) + } + llama_cpp_bindings_sys::LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_NULL_CTX_ARG => Err(crate::FfiContractError { + operation: "llama_rs_mtmd_bitmap_init_from_buf", + detail: "was given a null ctx argument", + } + .into()), + llama_cpp_bindings_sys::LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_NULL_BUF_ARG => Err(crate::FfiContractError { + operation: "llama_rs_mtmd_bitmap_init_from_buf", + detail: "was given a null buf argument", + } + .into()), + llama_cpp_bindings_sys::LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_NULL_OUT_BITMAP_ARG => Err(crate::FfiContractError { + operation: "llama_rs_mtmd_bitmap_init_from_buf", + detail: "was given a null out_bitmap argument", + } + .into()), + other => Err(crate::FfiStatusError { + operation: "llama_rs_mtmd_bitmap_init_from_buf", + code: i64::from(other), + } + .into()), + } +} + #[derive(Debug, Clone)] pub struct MtmdBitmap { pub bitmap: NonNull, @@ -156,21 +213,22 @@ impl MtmdBitmap { /// # Errors /// - /// * `NullResult` - Buffer could not be processed + /// Returns an [`MtmdBitmapError`] variant matching the wrapper's status code. pub fn from_buffer(ctx: &MtmdContext, data: &[u8]) -> Result { - let bitmap_wrapper = unsafe { - llama_cpp_bindings_sys::mtmd_helper_bitmap_init_from_buf( + let mut out_bitmap: *mut llama_cpp_bindings_sys::mtmd_bitmap = std::ptr::null_mut(); + let mut out_error: *mut c_char = std::ptr::null_mut(); + + let status = unsafe { + llama_cpp_bindings_sys::llama_rs_mtmd_bitmap_init_from_buf( ctx.context.as_ptr(), data.as_ptr(), data.len(), - false, + &raw mut out_bitmap, + &raw mut out_error, ) }; - let bitmap = - NonNull::new(bitmap_wrapper.bitmap).ok_or(MtmdBitmapError::BitmapDecodeFailed)?; - - Ok(Self { bitmap }) + unsafe { from_buf_status_to_result(status, out_bitmap, out_error) } } #[must_use] @@ -373,10 +431,10 @@ mod tests { } #[test] - fn from_file_status_vendored_returned_null_returns_file_unreadable() { + fn from_file_status_llama_cpp_returned_null_returns_file_unreadable() { let result = unsafe { super::from_file_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_VENDORED_RETURNED_NULL, + llama_cpp_bindings_sys::LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_LLAMA_CPP_RETURNED_NULL, std::ptr::null_mut(), std::ptr::null_mut(), "/missing/image.png", @@ -406,10 +464,10 @@ mod tests { } #[test] - fn from_file_status_vendored_threw_cxx_exception_without_a_message_is_a_contract_error() { + fn from_file_status_llama_cpp_threw_cxx_exception_without_a_message_is_a_contract_error() { let result = unsafe { super::from_file_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_LLAMA_CPP_THREW_CXX_EXCEPTION, std::ptr::null_mut(), std::ptr::null_mut(), "/missing/image.png", @@ -494,12 +552,206 @@ mod ffi_contract_status_tests { ); let outcome_2 = unsafe { from_file_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_MTMD_BITMAP_INIT_FROM_FILE_LLAMA_CPP_OUT_OF_MEMORY, ptr::null_mut(), ptr::null_mut(), "", ) }; - assert_eq!(outcome_2.err(), Some(MtmdBitmapError::VendoredOutOfMemory)); + assert_eq!(outcome_2.err(), Some(MtmdBitmapError::LlamaCppOutOfMemory)); + } +} + +#[cfg(test)] +mod from_buf_status_to_result_tests { + use std::ptr; + + use llama_cpp_ffi_status::read_and_free_cpp_string; + use llama_cpp_wrapper_error_fixture::wrapper_allocated_error_message_pointer; + + use super::from_buf_status_to_result; + use crate::mtmd::mtmd_bitmap_error::MtmdBitmapError; + + struct NullArgumentCase { + status: llama_cpp_bindings_sys::llama_rs_mtmd_bitmap_init_from_buf_status, + detail: &'static str, + } + + #[test] + fn success_status_adopts_the_bitmap_the_wrapper_produced() { + let pixels = [0u8; 12]; + let produced = unsafe { llama_cpp_bindings_sys::mtmd_bitmap_init(2, 2, pixels.as_ptr()) }; + + let result = unsafe { + from_buf_status_to_result( + llama_cpp_bindings_sys::LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_OK, + produced, + ptr::null_mut(), + ) + }; + + assert_eq!(result.map(|adopted| adopted.nx()), Ok(2)); + } + + #[test] + fn success_status_with_a_null_bitmap_is_a_contract_error() { + let result = unsafe { + from_buf_status_to_result( + llama_cpp_bindings_sys::LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_OK, + ptr::null_mut(), + ptr::null_mut(), + ) + }; + + assert_eq!( + result.err(), + Some( + crate::FfiContractError { + operation: "llama_rs_mtmd_bitmap_init_from_buf", + detail: "success status contained a null bitmap", + } + .into() + ) + ); + } + + #[test] + fn returned_null_status_reports_a_decode_failure() { + let result = unsafe { + from_buf_status_to_result( + llama_cpp_bindings_sys::LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_LLAMA_CPP_RETURNED_NULL, + ptr::null_mut(), + ptr::null_mut(), + ) + }; + + assert_eq!(result.err(), Some(MtmdBitmapError::BitmapDecodeFailed)); + } + + #[test] + fn error_string_allocation_failure_reports_not_enough_memory() { + let result = unsafe { + from_buf_status_to_result( + llama_cpp_bindings_sys::LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_ERROR_STRING_ALLOCATION_FAILED, + ptr::null_mut(), + ptr::null_mut(), + ) + }; + + assert_eq!(result.err(), Some(MtmdBitmapError::NotEnoughMemory)); + } + + #[test] + fn out_of_memory_status_reports_that_llama_cpp_ran_out_of_memory() { + let result = unsafe { + from_buf_status_to_result( + llama_cpp_bindings_sys::LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_LLAMA_CPP_OUT_OF_MEMORY, + ptr::null_mut(), + ptr::null_mut(), + ) + }; + + assert_eq!(result.err(), Some(MtmdBitmapError::LlamaCppOutOfMemory)); + } + + #[test] + fn exception_status_without_a_message_is_a_contract_error() { + let result = unsafe { + from_buf_status_to_result( + llama_cpp_bindings_sys::LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_LLAMA_CPP_THREW_CXX_EXCEPTION, + ptr::null_mut(), + ptr::null_mut(), + ) + }; + + assert_eq!( + result.err(), + Some( + crate::FfiContractError { + operation: "llama_rs_mtmd_bitmap_init_from_buf", + detail: "reported a thrown C++ exception without an error message", + } + .into() + ) + ); + } + + #[test] + fn exception_status_surfaces_the_reported_message() { + let expected_message = unsafe { + read_and_free_cpp_string( + wrapper_allocated_error_message_pointer(), + "llama_rs_mtmd_bitmap_init_from_buf", + "reported a thrown C++ exception without an error message", + ) + }; + let result = unsafe { + from_buf_status_to_result( + llama_cpp_bindings_sys::LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_LLAMA_CPP_THREW_CXX_EXCEPTION, + ptr::null_mut(), + wrapper_allocated_error_message_pointer(), + ) + }; + + assert_eq!( + result.err(), + expected_message + .ok() + .map(|message| MtmdBitmapError::Reported { message }) + ); + } + + #[test] + fn every_null_argument_status_is_a_contract_error() { + let cases = [ + NullArgumentCase { + status: llama_cpp_bindings_sys::LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_NULL_CTX_ARG, + detail: "was given a null ctx argument", + }, + NullArgumentCase { + status: llama_cpp_bindings_sys::LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_NULL_BUF_ARG, + detail: "was given a null buf argument", + }, + NullArgumentCase { + status: + llama_cpp_bindings_sys::LLAMA_RS_MTMD_BITMAP_INIT_FROM_BUF_NULL_OUT_BITMAP_ARG, + detail: "was given a null out_bitmap argument", + }, + ]; + + for NullArgumentCase { status, detail } in cases { + let result = + unsafe { from_buf_status_to_result(status, ptr::null_mut(), ptr::null_mut()) }; + + assert_eq!( + result.err(), + Some( + crate::FfiContractError { + operation: "llama_rs_mtmd_bitmap_init_from_buf", + detail, + } + .into() + ) + ); + } + } + + #[test] + fn unrecognized_status_reports_its_raw_code() { + let unrecognized = llama_cpp_bindings_sys::llama_rs_mtmd_bitmap_init_from_buf_status::MAX; + + let result = + unsafe { from_buf_status_to_result(unrecognized, ptr::null_mut(), ptr::null_mut()) }; + + assert_eq!( + result.err(), + Some( + crate::FfiStatusError { + operation: "llama_rs_mtmd_bitmap_init_from_buf", + code: i64::from(unrecognized), + } + .into() + ) + ); } } diff --git a/llama-cpp-bindings/src/mtmd/mtmd_bitmap_error.rs b/llama-cpp-bindings/src/mtmd/mtmd_bitmap_error.rs index 1020363b8..606085f11 100644 --- a/llama-cpp-bindings/src/mtmd/mtmd_bitmap_error.rs +++ b/llama-cpp-bindings/src/mtmd/mtmd_bitmap_error.rs @@ -22,8 +22,8 @@ pub enum MtmdBitmapError { FileUnreadable { path: PathBuf }, #[error("not enough memory")] NotEnoughMemory, - #[error("the vendored library ran out of memory")] - VendoredOutOfMemory, + #[error("the llama.cpp library ran out of memory")] + LlamaCppOutOfMemory, #[error("{message}")] Reported { message: String }, } diff --git a/llama-cpp-bindings/src/mtmd/mtmd_context.rs b/llama-cpp-bindings/src/mtmd/mtmd_context.rs index 513be8d7b..41069315d 100644 --- a/llama-cpp-bindings/src/mtmd/mtmd_context.rs +++ b/llama-cpp-bindings/src/mtmd/mtmd_context.rs @@ -21,13 +21,13 @@ fn map_tokenize_status( ) -> Result<(), MtmdTokenizeError> { match status { llama_cpp_bindings_sys::LLAMA_RS_MTMD_TOKENIZE_OK => Ok(()), - llama_cpp_bindings_sys::LLAMA_RS_MTMD_TOKENIZE_VENDORED_REPORTED_BITMAP_COUNT_DOES_NOT_MATCH_MARKER_COUNT => { + llama_cpp_bindings_sys::LLAMA_RS_MTMD_TOKENIZE_LLAMA_CPP_REPORTED_BITMAP_COUNT_DOES_NOT_MATCH_MARKER_COUNT => { Err(MtmdTokenizeError::BitmapCountDoesNotMatchMarkerCount) } - llama_cpp_bindings_sys::LLAMA_RS_MTMD_TOKENIZE_VENDORED_REPORTED_IMAGE_PREPROCESSING_ERROR => { + llama_cpp_bindings_sys::LLAMA_RS_MTMD_TOKENIZE_LLAMA_CPP_REPORTED_IMAGE_PREPROCESSING_ERROR => { Err(MtmdTokenizeError::MediaPreprocessingFailed) } - llama_cpp_bindings_sys::LLAMA_RS_MTMD_TOKENIZE_VENDORED_RETURNED_UNDOCUMENTED_NONZERO_CODE => { + llama_cpp_bindings_sys::LLAMA_RS_MTMD_TOKENIZE_LLAMA_CPP_RETURNED_UNDOCUMENTED_NONZERO_CODE => { Err(MtmdTokenizeError::UnknownStatus { code: undocumented_return_code, }) @@ -35,10 +35,10 @@ fn map_tokenize_status( llama_cpp_bindings_sys::LLAMA_RS_MTMD_TOKENIZE_ERROR_STRING_ALLOCATION_FAILED => { Err(MtmdTokenizeError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_MTMD_TOKENIZE_VENDORED_OUT_OF_MEMORY => { - Err(MtmdTokenizeError::VendoredOutOfMemory) + llama_cpp_bindings_sys::LLAMA_RS_MTMD_TOKENIZE_LLAMA_CPP_OUT_OF_MEMORY => { + Err(MtmdTokenizeError::LlamaCppOutOfMemory) } - llama_cpp_bindings_sys::LLAMA_RS_MTMD_TOKENIZE_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_MTMD_TOKENIZE_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string(out_error, "llama_rs_mtmd_tokenize", "reported a thrown C++ exception without an error message") }?; Err(MtmdTokenizeError::Reported { message }) } @@ -74,23 +74,23 @@ fn map_tokenize_status( fn map_encode_chunk_status( status: llama_cpp_bindings_sys::llama_rs_mtmd_encode_chunk_status, - vendored_return_code: i32, + llama_cpp_return_code: i32, out_error: *mut c_char, ) -> Result<(), MtmdEncodeError> { match status { llama_cpp_bindings_sys::LLAMA_RS_MTMD_ENCODE_CHUNK_OK => Ok(()), - llama_cpp_bindings_sys::LLAMA_RS_MTMD_ENCODE_CHUNK_VENDORED_RETURNED_NONZERO_CODE => { + llama_cpp_bindings_sys::LLAMA_RS_MTMD_ENCODE_CHUNK_LLAMA_CPP_RETURNED_NONZERO_CODE => { Err(MtmdEncodeError::EncodingFailed { - code: vendored_return_code, + code: llama_cpp_return_code, }) } llama_cpp_bindings_sys::LLAMA_RS_MTMD_ENCODE_CHUNK_ERROR_STRING_ALLOCATION_FAILED => { Err(MtmdEncodeError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_MTMD_ENCODE_CHUNK_VENDORED_OUT_OF_MEMORY => { - Err(MtmdEncodeError::VendoredOutOfMemory) + llama_cpp_bindings_sys::LLAMA_RS_MTMD_ENCODE_CHUNK_LLAMA_CPP_OUT_OF_MEMORY => { + Err(MtmdEncodeError::LlamaCppOutOfMemory) } - llama_cpp_bindings_sys::LLAMA_RS_MTMD_ENCODE_CHUNK_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_MTMD_ENCODE_CHUNK_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string( out_error, @@ -138,7 +138,7 @@ fn map_init_from_file_status( })?; Ok(MtmdContext { context }) } - llama_cpp_bindings_sys::LLAMA_RS_MTMD_INIT_FROM_FILE_VENDORED_RETURNED_NULL => { + llama_cpp_bindings_sys::LLAMA_RS_MTMD_INIT_FROM_FILE_LLAMA_CPP_RETURNED_NULL => { Err(MtmdInitError::Unloadable { path: std::path::PathBuf::from(mmproj_path), }) @@ -146,10 +146,10 @@ fn map_init_from_file_status( llama_cpp_bindings_sys::LLAMA_RS_MTMD_INIT_FROM_FILE_ERROR_STRING_ALLOCATION_FAILED => { Err(MtmdInitError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_MTMD_INIT_FROM_FILE_VENDORED_OUT_OF_MEMORY => { - Err(MtmdInitError::VendoredOutOfMemory) + llama_cpp_bindings_sys::LLAMA_RS_MTMD_INIT_FROM_FILE_LLAMA_CPP_OUT_OF_MEMORY => { + Err(MtmdInitError::LlamaCppOutOfMemory) } - llama_cpp_bindings_sys::LLAMA_RS_MTMD_INIT_FROM_FILE_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_MTMD_INIT_FROM_FILE_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string( out_error, @@ -286,7 +286,7 @@ impl MtmdContext { self.context.as_ptr(), chunks.chunks.as_ptr(), &raw const input_text, - bitmap_ptrs.as_ptr().cast_mut(), + bitmap_ptrs.as_ptr(), bitmaps.len(), &raw mut out_undocumented_return_code, &raw mut out_error, @@ -301,19 +301,19 @@ impl MtmdContext { /// /// Returns an [`MtmdEncodeError`] variant matching the wrapper's status code. pub fn encode_chunk(&self, chunk: &MtmdInputChunk) -> Result<(), MtmdEncodeError> { - let mut out_vendored_return_code: i32 = 0; + let mut out_llama_cpp_return_code: i32 = 0; let mut out_error: *mut c_char = std::ptr::null_mut(); let status = unsafe { llama_cpp_bindings_sys::llama_rs_mtmd_encode_chunk( self.context.as_ptr(), chunk.chunk.as_ptr(), - &raw mut out_vendored_return_code, + &raw mut out_llama_cpp_return_code, &raw mut out_error, ) }; - map_encode_chunk_status(status, out_vendored_return_code, out_error) + map_encode_chunk_status(status, out_llama_cpp_return_code, out_error) } } @@ -335,7 +335,7 @@ mod unit_tests { #[test] fn tokenize_status_maps_bitmap_count_mismatch() { let result = map_tokenize_status( - llama_cpp_bindings_sys::LLAMA_RS_MTMD_TOKENIZE_VENDORED_REPORTED_BITMAP_COUNT_DOES_NOT_MATCH_MARKER_COUNT, + llama_cpp_bindings_sys::LLAMA_RS_MTMD_TOKENIZE_LLAMA_CPP_REPORTED_BITMAP_COUNT_DOES_NOT_MATCH_MARKER_COUNT, 0, std::ptr::null_mut(), ); @@ -349,7 +349,7 @@ mod unit_tests { #[test] fn tokenize_status_maps_media_preprocessing_failed() { let result = map_tokenize_status( - llama_cpp_bindings_sys::LLAMA_RS_MTMD_TOKENIZE_VENDORED_REPORTED_IMAGE_PREPROCESSING_ERROR, + llama_cpp_bindings_sys::LLAMA_RS_MTMD_TOKENIZE_LLAMA_CPP_REPORTED_IMAGE_PREPROCESSING_ERROR, 0, std::ptr::null_mut(), ); @@ -360,7 +360,7 @@ mod unit_tests { #[test] fn tokenize_status_maps_unknown_status_with_value() { let result = map_tokenize_status( - llama_cpp_bindings_sys::LLAMA_RS_MTMD_TOKENIZE_VENDORED_RETURNED_UNDOCUMENTED_NONZERO_CODE, + llama_cpp_bindings_sys::LLAMA_RS_MTMD_TOKENIZE_LLAMA_CPP_RETURNED_UNDOCUMENTED_NONZERO_CODE, 42, std::ptr::null_mut(), ); @@ -393,7 +393,7 @@ mod unit_tests { #[test] fn encode_chunk_status_maps_encoding_failed_with_code() { let result = map_encode_chunk_status( - llama_cpp_bindings_sys::LLAMA_RS_MTMD_ENCODE_CHUNK_VENDORED_RETURNED_NONZERO_CODE, + llama_cpp_bindings_sys::LLAMA_RS_MTMD_ENCODE_CHUNK_LLAMA_CPP_RETURNED_NONZERO_CODE, 5, std::ptr::null_mut(), ); @@ -415,7 +415,7 @@ mod unit_tests { #[test] fn tokenize_status_maps_cxx_exception_to_without_a_message_is_a_contract_error() { let result = map_tokenize_status( - llama_cpp_bindings_sys::LLAMA_RS_MTMD_TOKENIZE_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_MTMD_TOKENIZE_LLAMA_CPP_THREW_CXX_EXCEPTION, 0, std::ptr::null_mut(), ); @@ -474,7 +474,7 @@ mod unit_tests { #[test] fn encode_chunk_status_maps_cxx_exception_to_without_a_message_is_a_contract_error() { let result = map_encode_chunk_status( - llama_cpp_bindings_sys::LLAMA_RS_MTMD_ENCODE_CHUNK_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_MTMD_ENCODE_CHUNK_LLAMA_CPP_THREW_CXX_EXCEPTION, 0, std::ptr::null_mut(), ); @@ -535,7 +535,7 @@ mod unit_tests { #[test] fn init_from_file_status_maps_cxx_exception_to_without_a_message_is_a_contract_error() { let result = map_init_from_file_status( - llama_cpp_bindings_sys::LLAMA_RS_MTMD_INIT_FROM_FILE_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_MTMD_INIT_FROM_FILE_LLAMA_CPP_THREW_CXX_EXCEPTION, std::ptr::null_mut(), std::ptr::null_mut(), "mmproj.gguf", @@ -628,13 +628,13 @@ mod ffi_contract_status_tests { ) ); let outcome_3 = map_tokenize_status( - llama_cpp_bindings_sys::LLAMA_RS_MTMD_TOKENIZE_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_MTMD_TOKENIZE_LLAMA_CPP_OUT_OF_MEMORY, 0, ptr::null_mut(), ); assert_eq!( outcome_3.err(), - Some(MtmdTokenizeError::VendoredOutOfMemory) + Some(MtmdTokenizeError::LlamaCppOutOfMemory) ); } @@ -671,11 +671,11 @@ mod ffi_contract_status_tests { ) ); let outcome_2 = map_encode_chunk_status( - llama_cpp_bindings_sys::LLAMA_RS_MTMD_ENCODE_CHUNK_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_MTMD_ENCODE_CHUNK_LLAMA_CPP_OUT_OF_MEMORY, 0, ptr::null_mut(), ); - assert_eq!(outcome_2.err(), Some(MtmdEncodeError::VendoredOutOfMemory)); + assert_eq!(outcome_2.err(), Some(MtmdEncodeError::LlamaCppOutOfMemory)); } #[test] @@ -729,11 +729,11 @@ mod ffi_contract_status_tests { ) ); let outcome_3 = map_init_from_file_status( - llama_cpp_bindings_sys::LLAMA_RS_MTMD_INIT_FROM_FILE_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_MTMD_INIT_FROM_FILE_LLAMA_CPP_OUT_OF_MEMORY, ptr::null_mut(), ptr::null_mut(), "", ); - assert_eq!(outcome_3.err(), Some(MtmdInitError::VendoredOutOfMemory)); + assert_eq!(outcome_3.err(), Some(MtmdInitError::LlamaCppOutOfMemory)); } } diff --git a/llama-cpp-bindings/src/mtmd/mtmd_default_marker.rs b/llama-cpp-bindings/src/mtmd/mtmd_default_marker.rs index 780eb447a..215e24abd 100644 --- a/llama-cpp-bindings/src/mtmd/mtmd_default_marker.rs +++ b/llama-cpp-bindings/src/mtmd/mtmd_default_marker.rs @@ -13,7 +13,7 @@ unsafe fn marker_bytes_to_str( /// /// Returns [`MtmdDefaultMarkerError::NotUtf8`] if llama.cpp's `mtmd_default_marker` /// returns bytes that are not valid UTF-8. The marker is a fixed ASCII constant in -/// the vendored library; surfacing the error keeps the failure explicit rather than +/// the llama.cpp library; surfacing the error keeps the failure explicit rather than /// papering over it with a substituted literal. pub fn mtmd_default_marker() -> Result<&'static str, MtmdDefaultMarkerError> { unsafe { marker_bytes_to_str(llama_cpp_bindings_sys::mtmd_default_marker()) } @@ -29,7 +29,7 @@ mod tests { #[test] fn returns_non_empty_marker() { - let marker = mtmd_default_marker().expect("vendored marker must be valid UTF-8"); + let marker = mtmd_default_marker().expect("llama.cpp marker must be valid UTF-8"); assert!(!marker.is_empty()); } diff --git a/llama-cpp-bindings/src/mtmd/mtmd_encode_error.rs b/llama-cpp-bindings/src/mtmd/mtmd_encode_error.rs index d7afa3010..0906829ab 100644 --- a/llama-cpp-bindings/src/mtmd/mtmd_encode_error.rs +++ b/llama-cpp-bindings/src/mtmd/mtmd_encode_error.rs @@ -8,8 +8,8 @@ pub enum MtmdEncodeError { EncodingFailed { code: i32 }, #[error("not enough memory")] NotEnoughMemory, - #[error("the vendored library ran out of memory")] - VendoredOutOfMemory, + #[error("the llama.cpp library ran out of memory")] + LlamaCppOutOfMemory, #[error("{message}")] Reported { message: String }, } diff --git a/llama-cpp-bindings/src/mtmd/mtmd_eval_error.rs b/llama-cpp-bindings/src/mtmd/mtmd_eval_error.rs index 2f6814fce..bc40ade6f 100644 --- a/llama-cpp-bindings/src/mtmd/mtmd_eval_error.rs +++ b/llama-cpp-bindings/src/mtmd/mtmd_eval_error.rs @@ -21,8 +21,8 @@ pub enum MtmdEvalError { UnknownChunkType(#[from] MtmdInputChunkTypeError), #[error("not enough memory")] NotEnoughMemory, - #[error("the vendored library ran out of memory")] - VendoredOutOfMemory, + #[error("the llama.cpp library ran out of memory")] + LlamaCppOutOfMemory, #[error("{message}")] Reported { message: String }, } diff --git a/llama-cpp-bindings/src/mtmd/mtmd_init_error.rs b/llama-cpp-bindings/src/mtmd/mtmd_init_error.rs index 71d7f8321..6f9f45cf1 100644 --- a/llama-cpp-bindings/src/mtmd/mtmd_init_error.rs +++ b/llama-cpp-bindings/src/mtmd/mtmd_init_error.rs @@ -14,8 +14,8 @@ pub enum MtmdInitError { Unloadable { path: PathBuf }, #[error("not enough memory")] NotEnoughMemory, - #[error("the vendored library ran out of memory")] - VendoredOutOfMemory, + #[error("the llama.cpp library ran out of memory")] + LlamaCppOutOfMemory, #[error("{message}")] Reported { message: String }, } diff --git a/llama-cpp-bindings/src/mtmd/mtmd_input_chunk.rs b/llama-cpp-bindings/src/mtmd/mtmd_input_chunk.rs index ed228ab0e..21b5c26da 100644 --- a/llama-cpp-bindings/src/mtmd/mtmd_input_chunk.rs +++ b/llama-cpp-bindings/src/mtmd/mtmd_input_chunk.rs @@ -37,23 +37,23 @@ const unsafe fn tokens_from_raw_ptr<'chunk>( fn eval_chunk_single_status_to_result( status: llama_cpp_bindings_sys::llama_rs_mtmd_eval_chunk_single_status, final_position: llama_cpp_bindings_sys::llama_pos, - out_vendored_return_code: i32, + out_llama_cpp_return_code: i32, out_error: *mut c_char, ) -> Result { match status { llama_cpp_bindings_sys::LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_OK => Ok(final_position), - llama_cpp_bindings_sys::LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_VENDORED_RETURNED_NONZERO_CODE => { + llama_cpp_bindings_sys::LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_LLAMA_CPP_RETURNED_NONZERO_CODE => { Err(MtmdEvalError::EvalFailed { - code: out_vendored_return_code, + code: out_llama_cpp_return_code, }) } llama_cpp_bindings_sys::LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_ERROR_STRING_ALLOCATION_FAILED => { Err(MtmdEvalError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_VENDORED_OUT_OF_MEMORY => { - Err(MtmdEvalError::VendoredOutOfMemory) + llama_cpp_bindings_sys::LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_LLAMA_CPP_OUT_OF_MEMORY => { + Err(MtmdEvalError::LlamaCppOutOfMemory) } - llama_cpp_bindings_sys::LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string( out_error, @@ -213,7 +213,7 @@ impl MtmdInputChunk { } let mut final_position: llama_cpp_bindings_sys::llama_pos = start_position; - let mut out_vendored_return_code: i32 = 0; + let mut out_llama_cpp_return_code: i32 = 0; let mut out_error: *mut c_char = std::ptr::null_mut(); let status = unsafe { @@ -226,7 +226,7 @@ impl MtmdInputChunk { n_batch, logits_last, &raw mut final_position, - &raw mut out_vendored_return_code, + &raw mut out_llama_cpp_return_code, &raw mut out_error, ) }; @@ -234,7 +234,7 @@ impl MtmdInputChunk { eval_chunk_single_status_to_result( status, final_position, - out_vendored_return_code, + out_llama_cpp_return_code, out_error, ) } @@ -291,7 +291,7 @@ mod unit_tests { #[test] fn eval_chunk_single_status_nonzero_code_maps_to_eval_failed() { let result = eval_chunk_single_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_VENDORED_RETURNED_NONZERO_CODE, + llama_cpp_bindings_sys::LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_LLAMA_CPP_RETURNED_NONZERO_CODE, 0, -3, std::ptr::null_mut(), @@ -315,7 +315,7 @@ mod unit_tests { #[test] fn eval_chunk_single_status_cxx_exception_reports_unknown_error_for_null() { let result = eval_chunk_single_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_LLAMA_CPP_THREW_CXX_EXCEPTION, 0, 0, std::ptr::null_mut(), @@ -443,11 +443,11 @@ mod ffi_contract_status_tests { ) ); let outcome_4 = eval_chunk_single_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_MTMD_EVAL_CHUNK_SINGLE_LLAMA_CPP_OUT_OF_MEMORY, 0, 0, ptr::null_mut(), ); - assert_eq!(outcome_4.err(), Some(MtmdEvalError::VendoredOutOfMemory)); + assert_eq!(outcome_4.err(), Some(MtmdEvalError::LlamaCppOutOfMemory)); } } diff --git a/llama-cpp-bindings/src/mtmd/mtmd_tokenize_error.rs b/llama-cpp-bindings/src/mtmd/mtmd_tokenize_error.rs index e3019c7c8..505adf3a7 100644 --- a/llama-cpp-bindings/src/mtmd/mtmd_tokenize_error.rs +++ b/llama-cpp-bindings/src/mtmd/mtmd_tokenize_error.rs @@ -18,8 +18,8 @@ pub enum MtmdTokenizeError { UnknownStatus { code: i32 }, #[error("not enough memory")] NotEnoughMemory, - #[error("the vendored library ran out of memory")] - VendoredOutOfMemory, + #[error("the llama.cpp library ran out of memory")] + LlamaCppOutOfMemory, #[error("{message}")] Reported { message: String }, } diff --git a/llama-cpp-bindings/src/sampled_token_classifier.rs b/llama-cpp-bindings/src/sampled_token_classifier.rs index ceb273ca2..8f73a880a 100644 --- a/llama-cpp-bindings/src/sampled_token_classifier.rs +++ b/llama-cpp-bindings/src/sampled_token_classifier.rs @@ -13,7 +13,6 @@ use crate::error::SampleError; use crate::error::TokenToStringError; use crate::eval_multimodal_chunks_params::EvalMultimodalChunksParams; use crate::llama_batch::LlamaBatch; -use crate::marker_kind::MarkerKind; use crate::model::LlamaModel; use crate::mtmd::MtmdContext; use crate::mtmd::MtmdInputChunks; @@ -27,12 +26,20 @@ pub use crate::classified_sample::ClassifiedSample; use crate::ingest_outcome::IngestOutcome; pub use crate::sampled_token_section::SampledTokenSection; +#[derive(Copy, Clone, Debug)] +enum PendingMarkerStatus { + Unmatched, + ResolvedBoundary, + AmbiguousBoundary, +} + #[derive(Clone, Debug)] struct PendingToken { token: LlamaToken, decoded: String, section: SampledTokenSection, - is_boundary: bool, + section_before_token: SampledTokenSection, + marker_status: PendingMarkerStatus, is_from_prompt: bool, is_held_for_probe: bool, } @@ -79,7 +86,7 @@ impl<'model> SampledTokenClassifier<'model> { /// detokenised. The failure is surfaced rather than substituting an empty /// piece, so classification never silently drops generated text. pub fn ingest(&mut self, token: LlamaToken) -> Result, TokenToStringError> { - if !self.markers.has_any() { + if self.markers.is_empty() { self.usage.record_undeterminable_token(); let piece = self.decode(token)?; return Ok(vec![IngestOutcome { @@ -94,7 +101,8 @@ impl<'model> SampledTokenClassifier<'model> { token, decoded: decoded.clone(), section: self.section, - is_boundary: false, + section_before_token: self.section, + marker_status: PendingMarkerStatus::Unmatched, is_from_prompt: false, is_held_for_probe: false, }); @@ -124,7 +132,7 @@ impl<'model> SampledTokenClassifier<'model> { } pub fn ingest_prompt_token(&mut self, token: LlamaToken) { - if !self.markers.has_any() { + if self.markers.is_empty() { return; } @@ -132,7 +140,8 @@ impl<'model> SampledTokenClassifier<'model> { token, decoded: String::new(), section: self.section, - is_boundary: false, + section_before_token: self.section, + marker_status: PendingMarkerStatus::Unmatched, is_from_prompt: true, is_held_for_probe: false, }); @@ -142,7 +151,7 @@ impl<'model> SampledTokenClassifier<'model> { } pub fn ingest_prompt_tokens(&mut self, tokens: &[LlamaToken]) { - if !self.markers.has_any() { + if self.markers.is_empty() { return; } for &token in tokens { @@ -168,78 +177,32 @@ impl<'model> SampledTokenClassifier<'model> { } fn try_consume_marker_at_tail(&mut self) { - let marker_match = self - .markers - .reasoning_open - .as_deref() - .and_then(|marker| self.marker_span_start(marker)) - .map(|span_start| (span_start, MarkerKind::ReasoningOpen)) - .or_else(|| { - self.markers.reasoning_closes.iter().find_map(|marker| { - self.marker_span_start(marker) - .map(|span_start| (span_start, MarkerKind::ReasoningClose)) - }) - }) - .or_else(|| { - self.markers - .tool_call_open - .as_deref() - .and_then(|marker| self.marker_span_start(marker)) - .map(|span_start| (span_start, MarkerKind::ToolCallOpen)) - }) - .or_else(|| { - self.markers - .tool_call_close - .as_deref() - .and_then(|marker| self.marker_span_start(marker)) - .map(|span_start| (span_start, MarkerKind::ToolCallClose)) - }); - - if let Some((span_start, marker_kind)) = marker_match { - self.mark_marker_span(span_start, marker_kind); - } - } + let pending_tokens: Vec<_> = self.pending.iter().map(|entry| entry.token).collect(); + let Some(marker) = self.markers.longest_matching_suffix(&pending_tokens) else { + return; + }; + let span_start = self.pending.len() - marker.tokens().len(); + let span_section = marker.span_section(self.pending[span_start].section_before_token); + let next_section = marker.next_section(); + let is_ambiguous_prefix = self.markers.is_prefix_of_longer_marker(marker.tokens()); - fn marker_span_start(&self, marker: &[LlamaToken]) -> Option { - if marker.is_empty() || self.pending.len() < marker.len() { - return None; - } - let span_start = self.pending.len() - marker.len(); - self.pending - .iter() - .skip(span_start) - .zip(marker) - .all(|(entry, marker_token)| entry.token == *marker_token) - .then_some(span_start) + self.mark_marker_span(span_start, span_section, next_section, is_ambiguous_prefix); } - fn mark_marker_span(&mut self, span_start: usize, kind: MarkerKind) { - let next_section = match kind { - MarkerKind::ReasoningOpen => SampledTokenSection::Reasoning, - MarkerKind::ReasoningClose | MarkerKind::ToolCallClose => SampledTokenSection::Content, - MarkerKind::ToolCallOpen => SampledTokenSection::ToolCall, - }; - let span_section = match kind { - MarkerKind::ReasoningOpen => SampledTokenSection::Reasoning, - MarkerKind::ToolCallOpen => SampledTokenSection::ToolCall, - MarkerKind::ReasoningClose => { - if self.section == SampledTokenSection::Reasoning { - SampledTokenSection::Reasoning - } else { - SampledTokenSection::Content - } - } - MarkerKind::ToolCallClose => { - if self.section == SampledTokenSection::ToolCall { - SampledTokenSection::ToolCall - } else { - SampledTokenSection::Content - } - } + fn mark_marker_span( + &mut self, + span_start: usize, + span_section: SampledTokenSection, + next_section: SampledTokenSection, + is_ambiguous_prefix: bool, + ) { + let marker_status = if is_ambiguous_prefix { + PendingMarkerStatus::AmbiguousBoundary + } else { + PendingMarkerStatus::ResolvedBoundary }; - for entry in self.pending.iter_mut().skip(span_start) { - entry.is_boundary = true; + entry.marker_status = marker_status; entry.section = span_section; } @@ -261,7 +224,9 @@ impl<'model> SampledTokenClassifier<'model> { .count(); let drainable = self.pending.len().saturating_sub(probe_held); let beyond_lookback = drainable > lookback; - if !front.is_boundary && !beyond_lookback { + let resolved_boundary = + matches!(front.marker_status, PendingMarkerStatus::ResolvedBoundary); + if !resolved_boundary && !beyond_lookback { break; } let Some(entry) = self.pending.pop_front() else { @@ -382,10 +347,10 @@ impl<'model> SampledTokenClassifier<'model> { SampledTokenSection::Pending => SampledToken::Undeterminable(entry.token), }; - let visible_piece = if entry.is_boundary { - String::new() - } else { + let visible_piece = if matches!(entry.marker_status, PendingMarkerStatus::Unmatched) { entry.decoded.clone() + } else { + String::new() }; IngestOutcome { @@ -548,10 +513,13 @@ impl<'model> SampledTokenClassifier<'model> { #[cfg(test)] mod tests { use super::JsonProbeState; + use super::PendingMarkerStatus; use super::PendingToken; use super::ProbeMode; use super::SampledTokenClassifier; use crate::ingest_outcome::IngestOutcome; + use crate::marker_role::MarkerRole; + use crate::marker_role_candidate::MarkerRoleCandidate; use crate::sampled_token::SampledToken; use crate::sampled_token_section::SampledTokenSection; use crate::streaming_markers::StreamingMarkers; @@ -565,12 +533,22 @@ mod tests { reasoning_open: Option>, reasoning_close: Option>, ) -> StreamingMarkers { - StreamingMarkers { - reasoning_open, - reasoning_closes: reasoning_close.into_iter().collect(), - tool_call_open: None, - tool_call_close: None, - } + let candidates = reasoning_open + .into_iter() + .map(|tokens| MarkerRoleCandidate { + tokens, + role: MarkerRole::ReasoningOpen, + }) + .chain( + reasoning_close + .into_iter() + .map(|tokens| MarkerRoleCandidate { + tokens, + role: MarkerRole::ReasoningClose, + }), + ); + + StreamingMarkers::from_candidates(candidates).expect("synthetic markers must be valid") } fn synthetic_classifier(markers: StreamingMarkers) -> SampledTokenClassifier<'static> { @@ -591,7 +569,8 @@ mod tests { token: token(token_id), decoded: decoded.to_owned(), section: classifier.section, - is_boundary: false, + section_before_token: classifier.section, + marker_status: PendingMarkerStatus::Unmatched, is_from_prompt: false, is_held_for_probe: false, }); @@ -602,7 +581,8 @@ mod tests { token: token(token_id), decoded: String::new(), section: classifier.section, - is_boundary: false, + section_before_token: classifier.section, + marker_status: PendingMarkerStatus::Unmatched, is_from_prompt: true, is_held_for_probe: false, }); @@ -639,6 +619,136 @@ mod tests { .collect() } + fn markers_sharing_tool_call_open_with_a_reasoning_close( + shared: Vec, + ) -> StreamingMarkers { + StreamingMarkers::from_candidates([ + MarkerRoleCandidate { + tokens: vec![token(100)], + role: MarkerRole::ReasoningOpen, + }, + MarkerRoleCandidate { + tokens: vec![token(200)], + role: MarkerRole::ReasoningClose, + }, + MarkerRoleCandidate { + tokens: shared.clone(), + role: MarkerRole::ReasoningClose, + }, + MarkerRoleCandidate { + tokens: shared, + role: MarkerRole::ToolCallOpen, + }, + MarkerRoleCandidate { + tokens: vec![token(201)], + role: MarkerRole::ToolCallClose, + }, + ]) + .expect("synthetic markers must be valid") + } + + #[test] + fn a_token_that_is_both_a_reasoning_close_and_the_tool_call_open_opens_the_tool_call() { + let mut classifier = + synthetic_classifier(markers_sharing_tool_call_open_with_a_reasoning_close(vec![ + token(300), + ])); + classifier.section = SampledTokenSection::Content; + + push_pending(&mut classifier, 300, ""); + classifier.try_consume_marker_at_tail(); + + assert_eq!(classifier.section, SampledTokenSection::ToolCall); + } + + #[test] + fn a_shared_marker_ends_reasoning_by_opening_the_tool_call() { + let mut classifier = + synthetic_classifier(markers_sharing_tool_call_open_with_a_reasoning_close(vec![ + token(300), + ])); + classifier.section = SampledTokenSection::Reasoning; + + push_pending(&mut classifier, 300, ""); + classifier.try_consume_marker_at_tail(); + + assert_eq!(classifier.section, SampledTokenSection::ToolCall); + } + + #[test] + fn longer_marker_reclassifies_a_completed_marker_that_was_its_prefix() { + let markers = StreamingMarkers::from_candidates([ + MarkerRoleCandidate { + tokens: vec![token(300)], + role: MarkerRole::ReasoningClose, + }, + MarkerRoleCandidate { + tokens: vec![token(300), token(301)], + role: MarkerRole::ToolCallOpen, + }, + ]) + .expect("synthetic markers must be valid"); + let mut classifier = synthetic_classifier(markers); + classifier.section = SampledTokenSection::Reasoning; + + push_pending(&mut classifier, 300, ""); + classifier.try_consume_marker_at_tail(); + let outcomes = classifier.drain_overflow(); + + assert_eq!(classifier.section, SampledTokenSection::ToolCall); + assert_eq!( + outcome_sections(&outcomes), + vec![SampledTokenSection::ToolCall, SampledTokenSection::ToolCall] + ); + assert_eq!(outcome_pieces(&outcomes), vec!["", ""]); + } + + #[test] + fn longer_closing_marker_uses_section_from_before_its_shorter_prefix() { + let markers = StreamingMarkers::from_candidates([ + MarkerRoleCandidate { + tokens: vec![token(300)], + role: MarkerRole::ReasoningClose, + }, + MarkerRoleCandidate { + tokens: vec![token(300), token(301)], + role: MarkerRole::ReasoningClose, + }, + ]); + assert!(markers.is_ok()); + let markers = markers.unwrap_or_default(); + let mut classifier = synthetic_classifier(markers); + classifier.section = SampledTokenSection::Reasoning; + + push_pending(&mut classifier, 300, ""); + classifier.try_consume_marker_at_tail(); + let outcomes = classifier.drain_overflow(); + + assert_eq!(classifier.section, SampledTokenSection::Content); + assert_eq!( + outcome_sections(&outcomes), + vec![ + SampledTokenSection::Reasoning, + SampledTokenSection::Reasoning + ] + ); + assert_eq!(outcome_pieces(&outcomes), vec!["", ""]); + assert_eq!(classifier.usage().reasoning_tokens, 2); + assert_eq!(classifier.usage().content_tokens, 0); + assert_eq!(classifier.usage().tool_call_tokens, 0); + assert_eq!(classifier.usage().undeterminable_tokens, 0); + } + #[test] fn single_token_close_marker_when_already_in_reasoning_emits_empty_piece_for_marker() { let markers = markers_with(Some(vec![token(100)]), Some(vec![token(200)])); @@ -762,12 +872,25 @@ mod tests { #[test] fn spurious_tool_call_close_in_reasoning_section_classifies_as_tool_call() { - let markers = StreamingMarkers { - reasoning_open: Some(vec![token(100)]), - reasoning_closes: vec![vec![token(200)]], - tool_call_open: Some(vec![token(300)]), - tool_call_close: Some(vec![token(400)]), - }; + let markers = StreamingMarkers::from_candidates([ + MarkerRoleCandidate { + tokens: vec![token(100)], + role: MarkerRole::ReasoningOpen, + }, + MarkerRoleCandidate { + tokens: vec![token(200)], + role: MarkerRole::ReasoningClose, + }, + MarkerRoleCandidate { + tokens: vec![token(300)], + role: MarkerRole::ToolCallOpen, + }, + MarkerRoleCandidate { + tokens: vec![token(400)], + role: MarkerRole::ToolCallClose, + }, + ]) + .expect("synthetic markers must be valid"); let mut classifier = synthetic_classifier(markers); classifier.section = SampledTokenSection::ToolCall; @@ -910,7 +1033,8 @@ mod tests { token: token(202), decoded: "k>".to_owned(), section: classifier.section, - is_boundary: false, + section_before_token: classifier.section, + marker_status: PendingMarkerStatus::Unmatched, is_from_prompt: false, is_held_for_probe: false, }); @@ -975,7 +1099,8 @@ mod tests { token: token(50), decoded: "hi".to_owned(), section: classifier.section, - is_boundary: false, + section_before_token: classifier.section, + marker_status: PendingMarkerStatus::Unmatched, is_from_prompt: false, is_held_for_probe: false, }); @@ -1076,8 +1201,12 @@ mod tests { let returned = classifier.markers(); - assert_eq!(returned.reasoning_open.as_deref(), Some(&[token(1)][..])); - assert_eq!(returned.reasoning_closes, vec![vec![token(2)]]); + assert!(returned.iter().any(|marker| { + marker.tokens() == [token(1)] && marker.roles() == [MarkerRole::ReasoningOpen] + })); + assert!(returned.iter().any(|marker| { + marker.tokens() == [token(2)] && marker.roles() == [MarkerRole::ReasoningClose] + })); } #[test] @@ -1093,8 +1222,11 @@ mod tests { #[test] fn spurious_tool_call_close_in_content_section_classifies_as_content() { - let mut markers = markers_with(None, None); - markers.tool_call_close = Some(vec![token(300)]); + let markers = StreamingMarkers::from_candidates([MarkerRoleCandidate { + tokens: vec![token(300)], + role: MarkerRole::ToolCallClose, + }]) + .expect("synthetic markers must be valid"); let mut classifier = synthetic_classifier(markers); classifier.section = SampledTokenSection::Content; @@ -1110,12 +1242,11 @@ mod tests { } fn markers_with_tool_call_open(tool_call_open: Vec) -> StreamingMarkers { - StreamingMarkers { - reasoning_open: None, - reasoning_closes: Vec::new(), - tool_call_open: Some(tool_call_open), - tool_call_close: None, - } + StreamingMarkers::from_candidates([MarkerRoleCandidate { + tokens: tool_call_open, + role: MarkerRole::ToolCallOpen, + }]) + .expect("synthetic markers must be valid") } fn feed_json_string( @@ -1320,12 +1451,21 @@ mod tests { #[test] fn json_probe_does_not_engage_in_reasoning_section() { - let markers = StreamingMarkers { - reasoning_open: Some(vec![token(800)]), - reasoning_closes: vec![vec![token(801)]], - tool_call_open: Some(vec![token(900)]), - tool_call_close: None, - }; + let markers = StreamingMarkers::from_candidates([ + MarkerRoleCandidate { + tokens: vec![token(800)], + role: MarkerRole::ReasoningOpen, + }, + MarkerRoleCandidate { + tokens: vec![token(801)], + role: MarkerRole::ReasoningClose, + }, + MarkerRoleCandidate { + tokens: vec![token(900)], + role: MarkerRole::ToolCallOpen, + }, + ]) + .expect("synthetic markers must be valid"); let mut classifier = synthetic_classifier(markers); classifier.section = SampledTokenSection::Reasoning; @@ -1520,7 +1660,8 @@ mod tests { token: token(1), decoded: "before".to_owned(), section: SampledTokenSection::Content, - is_boundary: false, + section_before_token: SampledTokenSection::Content, + marker_status: PendingMarkerStatus::Unmatched, is_from_prompt: false, is_held_for_probe: false, }); @@ -1528,7 +1669,8 @@ mod tests { token: token(2), decoded: "{}".to_owned(), section: SampledTokenSection::Content, - is_boundary: false, + section_before_token: SampledTokenSection::Content, + marker_status: PendingMarkerStatus::Unmatched, is_from_prompt: false, is_held_for_probe: true, }); diff --git a/llama-cpp-bindings/src/sampling.rs b/llama-cpp-bindings/src/sampling.rs index 6092ec9b6..d87581733 100644 --- a/llama-cpp-bindings/src/sampling.rs +++ b/llama-cpp-bindings/src/sampling.rs @@ -25,10 +25,10 @@ fn check_sampler_accept_status( llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_ACCEPT_ERROR_STRING_ALLOCATION_FAILED => { Err(SamplerAcceptError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_ACCEPT_VENDORED_OUT_OF_MEMORY => { - Err(SamplerAcceptError::VendoredOutOfMemory) + llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_ACCEPT_LLAMA_CPP_OUT_OF_MEMORY => { + Err(SamplerAcceptError::LlamaCppOutOfMemory) } - llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_ACCEPT_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_ACCEPT_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string( error_ptr, @@ -70,10 +70,10 @@ fn sampler_sample_status_to_result( llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_SAMPLE_ERROR_STRING_ALLOCATION_FAILED => { Err(SampleError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_SAMPLE_VENDORED_OUT_OF_MEMORY => { - Err(SampleError::VendoredOutOfMemory) + llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_SAMPLE_LLAMA_CPP_OUT_OF_MEMORY => { + Err(SampleError::LlamaCppOutOfMemory) } - llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_SAMPLE_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_SAMPLE_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string( error_ptr, @@ -128,16 +128,16 @@ fn sampler_init_grammar_status_to_result( llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_INIT_GRAMMAR_OK => { LlamaSampler::from_raw(sampler, "grammar").map_err(Into::into) } - llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_INIT_GRAMMAR_VENDORED_RETURNED_NULL => { + llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_INIT_GRAMMAR_LLAMA_CPP_RETURNED_NULL => { Err(GrammarError::GrammarMalformed) } llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_INIT_GRAMMAR_ERROR_STRING_ALLOCATION_FAILED => { Err(GrammarError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_INIT_GRAMMAR_VENDORED_OUT_OF_MEMORY => { - Err(GrammarError::VendoredOutOfMemory) + llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_INIT_GRAMMAR_LLAMA_CPP_OUT_OF_MEMORY => { + Err(GrammarError::LlamaCppOutOfMemory) } - llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_INIT_GRAMMAR_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_INIT_GRAMMAR_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string( error_ptr, @@ -178,20 +178,20 @@ fn sampler_init_grammar_lazy_patterns_status_to_result( llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_OK => { LlamaSampler::from_raw(sampler, "lazy grammar").map_err(Into::into) } - llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_VENDORED_RETURNED_NULL => { + llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_LLAMA_CPP_RETURNED_NULL => { Err(GrammarError::LazyGrammarMalformed) } llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_ERROR_STRING_ALLOCATION_FAILED => { Err(GrammarError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_VENDORED_OUT_OF_MEMORY => { - Err(GrammarError::VendoredOutOfMemory) + llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_LLAMA_CPP_OUT_OF_MEMORY => { + Err(GrammarError::LlamaCppOutOfMemory) } llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_INVALID_TRIGGER_PATTERN => { let message = unsafe { read_and_free_cpp_string(error_ptr, "llama_rs_sampler_init_grammar_lazy_patterns", "reported a thrown C++ exception without an error message") }?; Err(GrammarError::InvalidTriggerPattern { message }) } - llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string(error_ptr, "llama_rs_sampler_init_grammar_lazy_patterns", "reported a thrown C++ exception without an error message") }?; Err(GrammarError::Reported { message }) } @@ -961,7 +961,7 @@ mod tests { assert_eq!( super::check_sampler_accept_status( - llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_ACCEPT_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_ACCEPT_LLAMA_CPP_THREW_CXX_EXCEPTION, out_error, ), Err(SamplerAcceptError::GrammarStateCorrupted { @@ -1008,7 +1008,7 @@ mod tests { #[test] fn sampler_sample_status_exception_without_a_message_is_a_contract_error() { let result = super::sampler_sample_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_SAMPLE_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_SAMPLE_LLAMA_CPP_THREW_CXX_EXCEPTION, -1, std::ptr::null_mut(), ); @@ -1040,7 +1040,7 @@ mod tests { #[test] fn sampler_init_grammar_status_null_maps_to_grammar_malformed() { let result = super::sampler_init_grammar_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_INIT_GRAMMAR_VENDORED_RETURNED_NULL, + llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_INIT_GRAMMAR_LLAMA_CPP_RETURNED_NULL, std::ptr::null_mut(), std::ptr::null_mut(), ); @@ -1062,7 +1062,7 @@ mod tests { #[test] fn sampler_init_grammar_status_exception_without_a_message_is_a_contract_error() { let result = super::sampler_init_grammar_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_INIT_GRAMMAR_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_INIT_GRAMMAR_LLAMA_CPP_THREW_CXX_EXCEPTION, std::ptr::null_mut(), std::ptr::null_mut(), ); @@ -1098,7 +1098,7 @@ mod tests { #[test] fn sampler_init_grammar_lazy_patterns_status_null_maps_to_lazy_patterns_grammar_malformed() { let result = super::sampler_init_grammar_lazy_patterns_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_VENDORED_RETURNED_NULL, + llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_LLAMA_CPP_RETURNED_NULL, std::ptr::null_mut(), std::ptr::null_mut(), ); @@ -1120,7 +1120,7 @@ mod tests { #[test] fn sampler_init_grammar_lazy_patterns_status_exception_without_a_message_is_a_contract_error() { let result = super::sampler_init_grammar_lazy_patterns_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_LLAMA_CPP_THREW_CXX_EXCEPTION, std::ptr::null_mut(), std::ptr::null_mut(), ); @@ -1191,12 +1191,12 @@ mod ffi_contract_status_tests { ) ); let outcome_1 = check_sampler_accept_status( - llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_ACCEPT_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_ACCEPT_LLAMA_CPP_OUT_OF_MEMORY, ptr::null_mut(), ); assert_eq!( outcome_1.err(), - Some(SamplerAcceptError::VendoredOutOfMemory) + Some(SamplerAcceptError::LlamaCppOutOfMemory) ); } @@ -1248,11 +1248,11 @@ mod ffi_contract_status_tests { ) ); let outcome_3 = sampler_sample_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_SAMPLE_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_SAMPLE_LLAMA_CPP_OUT_OF_MEMORY, 0, ptr::null_mut(), ); - assert_eq!(outcome_3.err(), Some(SampleError::VendoredOutOfMemory)); + assert_eq!(outcome_3.err(), Some(SampleError::LlamaCppOutOfMemory)); } #[test] @@ -1273,11 +1273,11 @@ mod ffi_contract_status_tests { ) ); let outcome_1 = sampler_init_grammar_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_INIT_GRAMMAR_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_INIT_GRAMMAR_LLAMA_CPP_OUT_OF_MEMORY, ptr::null_mut(), ptr::null_mut(), ); - assert_eq!(outcome_1.err(), Some(GrammarError::VendoredOutOfMemory)); + assert_eq!(outcome_1.err(), Some(GrammarError::LlamaCppOutOfMemory)); } #[test] @@ -1297,7 +1297,7 @@ mod ffi_contract_status_tests { .into() ) ); - let outcome_1 = sampler_init_grammar_lazy_patterns_status_to_result(llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_VENDORED_OUT_OF_MEMORY, ptr::null_mut(), ptr::null_mut()); - assert_eq!(outcome_1.err(), Some(GrammarError::VendoredOutOfMemory)); + let outcome_1 = sampler_init_grammar_lazy_patterns_status_to_result(llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_INIT_GRAMMAR_LAZY_PATTERNS_LLAMA_CPP_OUT_OF_MEMORY, ptr::null_mut(), ptr::null_mut()); + assert_eq!(outcome_1.err(), Some(GrammarError::LlamaCppOutOfMemory)); } } diff --git a/llama-cpp-bindings/src/streaming_marker.rs b/llama-cpp-bindings/src/streaming_marker.rs new file mode 100644 index 000000000..f2706d6be --- /dev/null +++ b/llama-cpp-bindings/src/streaming_marker.rs @@ -0,0 +1,91 @@ +use crate::marker_role::MarkerRole; +use crate::sampled_token_section::SampledTokenSection; +use crate::token::LlamaToken; + +#[derive(Clone, Debug, Eq, PartialEq)] +/// A normalized token sequence and every semantic role attached to it. +pub struct StreamingMarker { + tokens: Vec, + roles: Vec, +} + +impl StreamingMarker { + #[must_use] + pub fn new(tokens: Vec, role: MarkerRole) -> Self { + Self { + tokens, + roles: vec![role], + } + } + + pub fn add_role(&mut self, role: MarkerRole) { + if !self.roles.contains(&role) { + self.roles.push(role); + } + } + + #[must_use] + /// Returns the tokens that form this marker. + pub fn tokens(&self) -> &[LlamaToken] { + &self.tokens + } + + #[must_use] + /// Returns the transitions associated with this marker. + pub fn roles(&self) -> &[MarkerRole] { + &self.roles + } + + #[must_use] + pub fn opener_count(&self) -> usize { + self.roles + .iter() + .filter(|role| role.opened_section().is_some()) + .count() + } + + fn opened_section(&self) -> Option { + self.roles.iter().find_map(|role| role.opened_section()) + } + + #[must_use] + pub fn span_section(&self, current: SampledTokenSection) -> SampledTokenSection { + self.opened_section().unwrap_or_else(|| { + if self + .roles + .iter() + .any(|role| role.closed_section() == Some(current)) + { + current + } else { + SampledTokenSection::Content + } + }) + } + + #[must_use] + pub fn next_section(&self) -> SampledTokenSection { + self.opened_section() + .unwrap_or(SampledTokenSection::Content) + } +} + +#[cfg(test)] +mod tests { + use super::StreamingMarker; + use crate::marker_role::MarkerRole; + use crate::token::LlamaToken; + + #[test] + fn a_role_the_marker_already_carries_is_not_added_twice() { + let mut marker = StreamingMarker::new(vec![LlamaToken::new(1)], MarkerRole::ReasoningClose); + + marker.add_role(MarkerRole::ToolCallOpen); + marker.add_role(MarkerRole::ToolCallOpen); + + assert_eq!( + marker.roles(), + &[MarkerRole::ReasoningClose, MarkerRole::ToolCallOpen] + ); + } +} diff --git a/llama-cpp-bindings/src/streaming_markers.rs b/llama-cpp-bindings/src/streaming_markers.rs index 87400eb9a..af43ed63c 100644 --- a/llama-cpp-bindings/src/streaming_markers.rs +++ b/llama-cpp-bindings/src/streaming_markers.rs @@ -1,41 +1,94 @@ +use crate::error::MarkerDetectionError; +use crate::marker_role_candidate::MarkerRoleCandidate; +use crate::streaming_marker::StreamingMarker; use crate::token::LlamaToken; #[derive(Clone, Debug, Default, Eq, PartialEq)] +/// The normalized streaming markers detected for a model. pub struct StreamingMarkers { - pub reasoning_open: Option>, - pub reasoning_closes: Vec>, - pub tool_call_open: Option>, - pub tool_call_close: Option>, + markers: Vec, } impl StreamingMarkers { + /// # Errors + /// + /// Returns [`MarkerDetectionError::EmptyMarker`] when a candidate carries no tokens, + /// and [`MarkerDetectionError::AmbiguousMarkerOpeners`] when one token sequence would + /// open more than one section. + pub fn from_candidates( + candidates: impl IntoIterator, + ) -> Result { + let mut markers: Vec = Vec::new(); + + for MarkerRoleCandidate { tokens, role } in candidates { + if tokens.is_empty() { + return Err(MarkerDetectionError::EmptyMarker); + } + + if let Some(marker) = markers.iter_mut().find(|marker| marker.tokens() == tokens) { + marker.add_role(role); + } else { + markers.push(StreamingMarker::new(tokens, role)); + } + } + + if let Some(marker) = markers.iter().find(|marker| marker.opener_count() > 1) { + return Err(MarkerDetectionError::AmbiguousMarkerOpeners { + tokens: marker.tokens().to_vec(), + }); + } + + Ok(Self { markers }) + } + #[must_use] - pub const fn has_any(&self) -> bool { - self.reasoning_open.is_some() - || !self.reasoning_closes.is_empty() - || self.tool_call_open.is_some() - || self.tool_call_close.is_some() + /// Returns whether the model exposes no streaming markers. + pub const fn is_empty(&self) -> bool { + self.markers.is_empty() + } + + #[must_use] + /// Returns the number of distinct marker token sequences. + pub const fn len(&self) -> usize { + self.markers.len() + } + + /// Iterates over the distinct marker token sequences. + pub fn iter(&self) -> impl Iterator { + self.markers.iter() } #[must_use] pub fn max_token_len(&self) -> usize { - [ - self.reasoning_open.as_deref(), - self.tool_call_open.as_deref(), - self.tool_call_close.as_deref(), - ] - .into_iter() - .flatten() - .map(<[LlamaToken]>::len) - .chain(self.reasoning_closes.iter().map(Vec::len)) - .max() - .unwrap_or(0) + self.markers + .iter() + .map(|marker| marker.tokens().len()) + .max() + .unwrap_or(0) + } + + #[must_use] + pub fn longest_matching_suffix(&self, tokens: &[LlamaToken]) -> Option<&StreamingMarker> { + self.markers + .iter() + .filter(|marker| tokens.ends_with(marker.tokens())) + .max_by_key(|marker| marker.tokens().len()) + } + + #[must_use] + pub fn is_prefix_of_longer_marker(&self, tokens: &[LlamaToken]) -> bool { + self.markers.iter().any(|marker| { + marker.tokens().len() > tokens.len() && marker.tokens().starts_with(tokens) + }) } } #[cfg(test)] mod tests { use super::StreamingMarkers; + use crate::error::MarkerDetectionError; + use crate::marker_role::MarkerRole; + use crate::marker_role_candidate::MarkerRoleCandidate; use crate::token::LlamaToken; fn token(id: i32) -> LlamaToken { @@ -43,20 +96,126 @@ mod tests { } #[test] - fn streaming_markers_with_no_markers_reports_none() { + fn empty_collection_reports_no_markers() { let markers = StreamingMarkers::default(); - assert!(!markers.has_any()); + + assert!(markers.is_empty()); + assert_eq!(markers.len(), 0); assert_eq!(markers.max_token_len(), 0); } #[test] - fn streaming_markers_max_token_len_takes_longest() { - let markers = StreamingMarkers { - reasoning_open: Some(vec![token(1)]), - reasoning_closes: vec![vec![token(2), token(3), token(4)]], - tool_call_open: Some(vec![token(5), token(6)]), - tool_call_close: None, - }; + fn candidates_with_the_same_tokens_are_one_marker_with_multiple_roles() { + let markers = StreamingMarkers::from_candidates([ + MarkerRoleCandidate { + tokens: vec![token(1)], + role: MarkerRole::ReasoningClose, + }, + MarkerRoleCandidate { + tokens: vec![token(1)], + role: MarkerRole::ToolCallOpen, + }, + ]) + .expect("a close and an opener compose into one transition"); + + let marker = markers.iter().next().expect("one marker must remain"); + assert_eq!(marker.tokens(), &[token(1)]); + assert_eq!( + marker.roles(), + &[MarkerRole::ReasoningClose, MarkerRole::ToolCallOpen] + ); + } + + #[test] + fn empty_marker_is_rejected() { + assert_eq!( + StreamingMarkers::from_candidates([MarkerRoleCandidate { + tokens: Vec::new(), + role: MarkerRole::ReasoningOpen + }]), + Err(MarkerDetectionError::EmptyMarker) + ); + } + + #[test] + fn two_openers_for_the_same_tokens_are_rejected() { + let marker_tokens = vec![token(1), token(2)]; + + assert_eq!( + StreamingMarkers::from_candidates([ + MarkerRoleCandidate { + tokens: marker_tokens.clone(), + role: MarkerRole::ReasoningOpen + }, + MarkerRoleCandidate { + tokens: marker_tokens.clone(), + role: MarkerRole::ToolCallOpen + }, + ]), + Err(MarkerDetectionError::AmbiguousMarkerOpeners { + tokens: marker_tokens + }) + ); + } + + #[test] + fn longest_matching_suffix_wins() { + let markers = StreamingMarkers::from_candidates([ + MarkerRoleCandidate { + tokens: vec![token(2)], + role: MarkerRole::ReasoningClose, + }, + MarkerRoleCandidate { + tokens: vec![token(1), token(2)], + role: MarkerRole::ToolCallOpen, + }, + ]) + .expect("markers are valid"); + + let matched = markers + .longest_matching_suffix(&[token(1), token(2)]) + .expect("a suffix must match"); + + assert_eq!(matched.tokens(), &[token(1), token(2)]); + assert_eq!(matched.roles(), &[MarkerRole::ToolCallOpen]); + } + + #[test] + fn shorter_complete_marker_reports_when_it_is_still_an_ambiguous_prefix() { + let markers = StreamingMarkers::from_candidates([ + MarkerRoleCandidate { + tokens: vec![token(1)], + role: MarkerRole::ReasoningClose, + }, + MarkerRoleCandidate { + tokens: vec![token(1), token(2)], + role: MarkerRole::ToolCallOpen, + }, + ]) + .expect("markers are valid"); + + assert!(markers.is_prefix_of_longer_marker(&[token(1)])); + assert!(!markers.is_prefix_of_longer_marker(&[token(1), token(2)])); + } + + #[test] + fn max_token_len_uses_the_longest_normalized_marker() { + let markers = StreamingMarkers::from_candidates([ + MarkerRoleCandidate { + tokens: vec![token(1)], + role: MarkerRole::ReasoningOpen, + }, + MarkerRoleCandidate { + tokens: vec![token(2), token(3), token(4)], + role: MarkerRole::ReasoningClose, + }, + MarkerRoleCandidate { + tokens: vec![token(5), token(6)], + role: MarkerRole::ToolCallOpen, + }, + ]) + .expect("markers are valid"); + assert_eq!(markers.max_token_len(), 3); } } diff --git a/llama-cpp-bindings/src/token/data_array.rs b/llama-cpp-bindings/src/token/data_array.rs index 04eef6b4b..e2fd0d813 100644 --- a/llama-cpp-bindings/src/token/data_array.rs +++ b/llama-cpp-bindings/src/token/data_array.rs @@ -20,10 +20,10 @@ fn sampler_apply_status_to_result( llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_APPLY_ERROR_STRING_ALLOCATION_FAILED => { Err(SamplerApplyError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_APPLY_VENDORED_OUT_OF_MEMORY => { - Err(SamplerApplyError::VendoredOutOfMemory) + llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_APPLY_LLAMA_CPP_OUT_OF_MEMORY => { + Err(SamplerApplyError::LlamaCppOutOfMemory) } - llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_APPLY_VENDORED_THREW_CXX_EXCEPTION => { + llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_APPLY_LLAMA_CPP_THREW_CXX_EXCEPTION => { let message = unsafe { read_and_free_cpp_string( out_error, @@ -88,7 +88,7 @@ impl LlamaTokenDataArray { impl LlamaTokenDataArray { /// # Errors /// - /// Returns [`crate::FfiContractError`] when the vendored sampler grows the array beyond the + /// Returns [`crate::FfiContractError`] when the llama.cpp sampler grows the array beyond the /// capacity this buffer was allocated with, which would make the following `set_len` /// undefined behaviour. /// @@ -122,7 +122,7 @@ impl LlamaTokenDataArray { if c_llama_token_data_array.size > self.data.capacity() { return Err(crate::FfiContractError { operation: "modify_as_c_llama_token_data_array", - detail: "the vendored sampler grew the token data array beyond its capacity", + detail: "the llama.cpp sampler grew the token data array beyond its capacity", }); } @@ -149,7 +149,7 @@ impl LlamaTokenDataArray { /// # Errors /// - /// Returns [`SamplerApplyError`] if the sampler pointer is null, the vendored + /// Returns [`SamplerApplyError`] if the sampler pointer is null, the llama.cpp /// sampler runs out of memory, or it throws a C++ exception while applying. pub fn apply_sampler(&mut self, sampler: &LlamaSampler) -> Result<(), SamplerApplyError> { unsafe { @@ -217,7 +217,7 @@ mod tests { fn sampler_apply_status_cxx_exception_without_a_message_is_a_contract_error() { assert_eq!( sampler_apply_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_APPLY_VENDORED_THREW_CXX_EXCEPTION, + llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_APPLY_LLAMA_CPP_THREW_CXX_EXCEPTION, std::ptr::null_mut(), ), Err(crate::FfiContractError { @@ -444,7 +444,7 @@ mod tests { result, Err(crate::FfiContractError { operation: "modify_as_c_llama_token_data_array", - detail: "the vendored sampler grew the token data array beyond its capacity", + detail: "the llama.cpp sampler grew the token data array beyond its capacity", }) ); assert_eq!(array.data.len(), 1); @@ -515,12 +515,12 @@ mod ffi_contract_status_tests { ) ); let outcome_2 = sampler_apply_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_APPLY_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_SAMPLER_APPLY_LLAMA_CPP_OUT_OF_MEMORY, ptr::null_mut(), ); assert_eq!( outcome_2.err(), - Some(SamplerApplyError::VendoredOutOfMemory) + Some(SamplerApplyError::LlamaCppOutOfMemory) ); } } diff --git a/llama-cpp-ffi-status/Cargo.toml b/llama-cpp-ffi-status/Cargo.toml index 49d593b6f..703f1da12 100644 --- a/llama-cpp-ffi-status/Cargo.toml +++ b/llama-cpp-ffi-status/Cargo.toml @@ -10,5 +10,8 @@ repository.workspace = true llama-cpp-bindings-sys = { workspace = true } thiserror = { workspace = true } +[dev-dependencies] +llama-cpp-wrapper-error-fixture = { workspace = true } + [lints] workspace = true diff --git a/llama-cpp-ffi-status/src/read_and_free_cpp_string.rs b/llama-cpp-ffi-status/src/read_and_free_cpp_string.rs index 039f06e1a..ab25f7d20 100644 --- a/llama-cpp-ffi-status/src/read_and_free_cpp_string.rs +++ b/llama-cpp-ffi-status/src/read_and_free_cpp_string.rs @@ -39,43 +39,18 @@ pub unsafe fn read_and_free_cpp_string( #[cfg(test)] mod tests { - use std::ffi::c_char; use std::ptr; + use llama_cpp_wrapper_error_fixture::wrapper_allocated_error_message_pointer; + use super::read_and_free_cpp_string; use crate::ffi_contract_error::FfiContractError; - fn vendored_error_message_pointer() -> *mut c_char { - let schema = c"not a json schema at all"; - let mut out_grammar: *mut c_char = ptr::null_mut(); - let mut out_error: *mut c_char = ptr::null_mut(); - - let status = unsafe { - llama_cpp_bindings_sys::llama_rs_json_schema_to_grammar( - schema.as_ptr(), - false, - &raw mut out_grammar, - &raw mut out_error, - ) - }; - - assert_eq!( - status, - llama_cpp_bindings_sys::LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_VENDORED_THREW_CXX_EXCEPTION - ); - assert!( - !out_error.is_null(), - "the wrapper must store a message alongside the exception status" - ); - - out_error - } - #[test] fn reads_and_reclaims_a_string_allocated_by_the_wrapper() { let message = unsafe { read_and_free_cpp_string( - vendored_error_message_pointer(), + wrapper_allocated_error_message_pointer(), "llama_rs_json_schema_to_grammar", "reported a thrown C++ exception without an error message", ) @@ -84,7 +59,7 @@ mod tests { assert_eq!( message.map(|text| text.contains("parse error")), Ok(true), - "the vendored json parser reports its failure through the error slot" + "the llama.cpp json parser reports its failure through the error slot" ); } diff --git a/llama-cpp-gbnf/src/gbnf_validation_error.rs b/llama-cpp-gbnf/src/gbnf_validation_error.rs index 47aff84aa..fe8891cd9 100644 --- a/llama-cpp-gbnf/src/gbnf_validation_error.rs +++ b/llama-cpp-gbnf/src/gbnf_validation_error.rs @@ -20,8 +20,8 @@ pub enum GbnfValidationError { LeftRecursion, #[error("not enough memory")] NotEnoughMemory, - #[error("the vendored library ran out of memory")] - VendoredOutOfMemory, + #[error("the llama.cpp library ran out of memory")] + LlamaCppOutOfMemory, #[error("the llama.cpp grammar engine failed: {message}")] Reported { message: String }, } diff --git a/llama-cpp-gbnf/src/validate_gbnf.rs b/llama-cpp-gbnf/src/validate_gbnf.rs index ccf4860f8..665f1d7f4 100644 --- a/llama-cpp-gbnf/src/validate_gbnf.rs +++ b/llama-cpp-gbnf/src/validate_gbnf.rs @@ -52,8 +52,8 @@ fn validation_status_to_result( llama_cpp_bindings_sys::LLAMA_RS_GBNF_VALIDATION_ERROR_STRING_ALLOCATION_FAILED => { Err(GbnfValidationError::NotEnoughMemory) } - llama_cpp_bindings_sys::LLAMA_RS_GBNF_VALIDATION_VENDORED_OUT_OF_MEMORY => { - Err(GbnfValidationError::VendoredOutOfMemory) + llama_cpp_bindings_sys::LLAMA_RS_GBNF_VALIDATION_LLAMA_CPP_OUT_OF_MEMORY => { + Err(GbnfValidationError::LlamaCppOutOfMemory) } LLAMA_RS_GBNF_VALIDATION_THREW_CXX_EXCEPTION => { let message = unsafe { @@ -249,14 +249,14 @@ mod tests { } #[test] - fn vendored_out_of_memory_status_is_distinct_from_error_string_allocation() { + fn llama_cpp_out_of_memory_status_is_distinct_from_error_string_allocation() { assert_eq!( validation_status_to_result( - llama_cpp_bindings_sys::LLAMA_RS_GBNF_VALIDATION_VENDORED_OUT_OF_MEMORY, + llama_cpp_bindings_sys::LLAMA_RS_GBNF_VALIDATION_LLAMA_CPP_OUT_OF_MEMORY, "root", std::ptr::null_mut(), ), - Err(GbnfValidationError::VendoredOutOfMemory) + Err(GbnfValidationError::LlamaCppOutOfMemory) ); } diff --git a/llama-cpp-wrapper-error-fixture/Cargo.toml b/llama-cpp-wrapper-error-fixture/Cargo.toml new file mode 100644 index 000000000..6959d3233 --- /dev/null +++ b/llama-cpp-wrapper-error-fixture/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "llama-cpp-wrapper-error-fixture" +description = "Produces wrapper-allocated error messages so FFI status tests can exercise ownership transfer" +version.workspace = true +edition.workspace = true +license.workspace = true +repository.workspace = true +publish = false + +[dependencies] +llama-cpp-bindings-sys = { workspace = true } + +[lints] +workspace = true diff --git a/llama-cpp-wrapper-error-fixture/src/lib.rs b/llama-cpp-wrapper-error-fixture/src/lib.rs new file mode 100644 index 000000000..fb54cf139 --- /dev/null +++ b/llama-cpp-wrapper-error-fixture/src/lib.rs @@ -0,0 +1,3 @@ +pub mod wrapper_allocated_error_message_pointer; + +pub use wrapper_allocated_error_message_pointer::wrapper_allocated_error_message_pointer; diff --git a/llama-cpp-wrapper-error-fixture/src/wrapper_allocated_error_message_pointer.rs b/llama-cpp-wrapper-error-fixture/src/wrapper_allocated_error_message_pointer.rs new file mode 100644 index 000000000..60e414ae4 --- /dev/null +++ b/llama-cpp-wrapper-error-fixture/src/wrapper_allocated_error_message_pointer.rs @@ -0,0 +1,40 @@ +use std::ffi::c_char; +use std::ptr::null_mut; + +/// Hands back a message string the C++ wrappers allocated themselves. +/// +/// Tests use it to drive the paths that take ownership of such a pointer. The caller +/// owns the allocation and must release it through `read_and_free_cpp_string`. +/// +/// # Panics +/// +/// Panics when llama.cpp stops rejecting the malformed schema this fixture relies on, +/// or accepts it without storing a message, because the fixture can no longer produce +/// the pointer it promises. +#[must_use] +pub fn wrapper_allocated_error_message_pointer() -> *mut c_char { + let schema = c"not valid json at all"; + let mut out_grammar: *mut c_char = null_mut(); + let mut out_error: *mut c_char = null_mut(); + + let status = unsafe { + llama_cpp_bindings_sys::llama_rs_json_schema_to_grammar( + schema.as_ptr(), + false, + &raw mut out_grammar, + &raw mut out_error, + ) + }; + + assert_eq!( + status, + llama_cpp_bindings_sys::LLAMA_RS_JSON_SCHEMA_TO_GRAMMAR_LLAMA_CPP_THREW_CXX_EXCEPTION, + "the fixture depends on the llama.cpp json parser rejecting this schema" + ); + assert!( + !out_error.is_null(), + "the wrapper must store a message alongside the exception status" + ); + + out_error +}