From 74fbcb2b3ecb613b104bf55c4f04fe3cc985b434 Mon Sep 17 00:00:00 2001 From: Julian Brunner Date: Sun, 19 Apr 2026 16:40:07 +0200 Subject: [PATCH 01/10] use /// instead of // for llama_token_to_piece docstring --- include/llama.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/include/llama.h b/include/llama.h index 75095b22d08f..213459602fd8 100644 --- a/include/llama.h +++ b/include/llama.h @@ -1130,11 +1130,11 @@ extern "C" { bool add_special, bool parse_special); - // Token Id -> Piece. - // Uses the vocabulary in the provided context. - // Does not write null terminator to the buffer. - // User can skip up to 'lstrip' leading spaces before copying (useful when encoding/decoding multiple tokens with 'add_space_prefix') - // @param special If true, special tokens are rendered in the output. + /// Token Id -> Piece. + /// Uses the vocabulary in the provided context. + /// Does not write null terminator to the buffer. + /// User can skip up to 'lstrip' leading spaces before copying (useful when encoding/decoding multiple tokens with 'add_space_prefix') + /// @param special If true, special tokens are rendered in the output. LLAMA_API int32_t llama_token_to_piece( const struct llama_vocab * vocab, llama_token token, From 72d91803c19b6e551b3b5b99e28f8bc66e46ef9f Mon Sep 17 00:00:00 2001 From: Julian Brunner Date: Sun, 19 Apr 2026 16:45:18 +0200 Subject: [PATCH 02/10] clarify llama_token_to_piece docstring in analogy to llama_detokenize --- include/llama.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/llama.h b/include/llama.h index 213459602fd8..3f802f05da86 100644 --- a/include/llama.h +++ b/include/llama.h @@ -1133,7 +1133,9 @@ extern "C" { /// Token Id -> Piece. /// Uses the vocabulary in the provided context. /// Does not write null terminator to the buffer. - /// User can skip up to 'lstrip' leading spaces before copying (useful when encoding/decoding multiple tokens with 'add_space_prefix') + /// @return Returns the number of chars/bytes on success, no more than length. + /// @return Returns a negative number on failure - the number of chars/bytes that would have been returned. + /// @param lstrip User can skip up to 'lstrip' leading spaces before copying (useful when encoding/decoding multiple tokens with 'add_space_prefix') /// @param special If true, special tokens are rendered in the output. LLAMA_API int32_t llama_token_to_piece( const struct llama_vocab * vocab, From b6387d21e4e91a3d00ce42fcdace4371edf45447 Mon Sep 17 00:00:00 2001 From: Julian Brunner Date: Sun, 19 Apr 2026 16:46:56 +0200 Subject: [PATCH 03/10] perform bounds checking on token in token_to_piece --- include/llama.h | 1 + src/llama-vocab.cpp | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/include/llama.h b/include/llama.h index 3f802f05da86..a5e971d72ca9 100644 --- a/include/llama.h +++ b/include/llama.h @@ -1135,6 +1135,7 @@ extern "C" { /// Does not write null terminator to the buffer. /// @return Returns the number of chars/bytes on success, no more than length. /// @return Returns a negative number on failure - the number of chars/bytes that would have been returned. + /// @return Returns INT32_MIN if the token is not in the vocabulary /// @param lstrip User can skip up to 'lstrip' leading spaces before copying (useful when encoding/decoding multiple tokens with 'add_space_prefix') /// @param special If true, special tokens are rendered in the output. LLAMA_API int32_t llama_token_to_piece( diff --git a/src/llama-vocab.cpp b/src/llama-vocab.cpp index a5cf148b268f..55f4f229c00e 100644 --- a/src/llama-vocab.cpp +++ b/src/llama-vocab.cpp @@ -3382,6 +3382,11 @@ std::vector llama_vocab::impl::tokenize( } int32_t llama_vocab::impl::token_to_piece(llama_token token, char * buf, int32_t length, int32_t lstrip, bool special) const { + if (token < 0 || token >= (int32_t) id_to_token.size()) { + LLAMA_LOG_ERROR("%s: invalid token %d\n", __func__, token); + return std::numeric_limits::min(); + } + // ref: https://github.com/ggml-org/llama.cpp/pull/7587#discussion_r1620983843 static const int attr_special = LLAMA_TOKEN_ATTR_UNKNOWN | LLAMA_TOKEN_ATTR_CONTROL; const llama_token_attr attr = token_get_attr(token); From 492a14f5e321488caa681dc0df076b22dcc299b7 Mon Sep 17 00:00:00 2001 From: Julian Brunner Date: Sun, 19 Apr 2026 16:50:45 +0200 Subject: [PATCH 04/10] remove always-true conditional --- src/llama-vocab.cpp | 126 ++++++++++++++++++++++---------------------- 1 file changed, 62 insertions(+), 64 deletions(-) diff --git a/src/llama-vocab.cpp b/src/llama-vocab.cpp index 55f4f229c00e..837414967f25 100644 --- a/src/llama-vocab.cpp +++ b/src/llama-vocab.cpp @@ -3422,82 +3422,80 @@ int32_t llama_vocab::impl::token_to_piece(llama_token token, char * buf, int32_t } } - if (0 <= token && token < (int32_t) id_to_token.size()) { - const std::string & token_text = id_to_token[token].text; - switch (get_type()) { - case LLAMA_VOCAB_TYPE_WPM: - case LLAMA_VOCAB_TYPE_SPM: - case LLAMA_VOCAB_TYPE_UGM: { - // NOTE: we accept all unsupported token types, - // suppressing them like CONTROL tokens. - if (attr & (attr_special | LLAMA_TOKEN_ATTR_USER_DEFINED)) { - return _try_copy(token_text.data(), token_text.size()); - } - if (attr & LLAMA_TOKEN_ATTR_NORMAL) { + const std::string & token_text = id_to_token[token].text; + switch (get_type()) { + case LLAMA_VOCAB_TYPE_WPM: + case LLAMA_VOCAB_TYPE_SPM: + case LLAMA_VOCAB_TYPE_UGM: { + // NOTE: we accept all unsupported token types, + // suppressing them like CONTROL tokens. + if (attr & (attr_special | LLAMA_TOKEN_ATTR_USER_DEFINED)) { + return _try_copy(token_text.data(), token_text.size()); + } + if (attr & LLAMA_TOKEN_ATTR_NORMAL) { + std::string result = token_text; + llama_unescape_whitespace(result); + return _try_copy(result.data(), result.size()); + } + if (attr & LLAMA_TOKEN_ATTR_BYTE) { + char byte = (char) token_to_byte(token); + return _try_copy((char*) &byte, 1); + } + break; + } + case LLAMA_VOCAB_TYPE_BPE: { + // NOTE: we accept all unsupported token types, + // suppressing them like CONTROL tokens. + if (attr & (attr_special | LLAMA_TOKEN_ATTR_USER_DEFINED)) { + return _try_copy(token_text.data(), token_text.size()); + } + if (attr & LLAMA_TOKEN_ATTR_NORMAL) { + if (escape_whitespaces) { + // SPM-style BPE: tokens contain ▁ for spaces std::string result = token_text; llama_unescape_whitespace(result); return _try_copy(result.data(), result.size()); } - if (attr & LLAMA_TOKEN_ATTR_BYTE) { - char byte = (char) token_to_byte(token); - return _try_copy((char*) &byte, 1); - } - break; + std::string result = llama_decode_text(token_text); + return _try_copy(result.data(), result.size()); } - case LLAMA_VOCAB_TYPE_BPE: { - // NOTE: we accept all unsupported token types, - // suppressing them like CONTROL tokens. - if (attr & (attr_special | LLAMA_TOKEN_ATTR_USER_DEFINED)) { - return _try_copy(token_text.data(), token_text.size()); - } - if (attr & LLAMA_TOKEN_ATTR_NORMAL) { - if (escape_whitespaces) { - // SPM-style BPE: tokens contain ▁ for spaces - std::string result = token_text; - llama_unescape_whitespace(result); - return _try_copy(result.data(), result.size()); - } - std::string result = llama_decode_text(token_text); - return _try_copy(result.data(), result.size()); - } - if (attr & LLAMA_TOKEN_ATTR_BYTE) { - char byte = (char) token_to_byte(token); - return _try_copy((char*) &byte, 1); - } - break; + if (attr & LLAMA_TOKEN_ATTR_BYTE) { + char byte = (char) token_to_byte(token); + return _try_copy((char*) &byte, 1); } - case LLAMA_VOCAB_TYPE_RWKV: { - std::vector result = llama_unescape_rwkv_token(token_text); - - // If we don't have enough space, return an error - if (result.size() > (size_t)length) { - return -(int)result.size(); - } + break; + } + case LLAMA_VOCAB_TYPE_RWKV: { + std::vector result = llama_unescape_rwkv_token(token_text); - memcpy(buf, result.data(), result.size()); - return (int)result.size(); + // If we don't have enough space, return an error + if (result.size() > (size_t)length) { + return -(int)result.size(); } - case LLAMA_VOCAB_TYPE_PLAMO2: { - // PLaMo-2 uses similar token handling as BPE/SPM - if (vocab.is_byte(token)) { - // Handle byte tokens like <0xXX> - if (token_text.length() == 6 && token_text.substr(0, 3) == "<0x" && token_text.back() == '>') { - int hex_val = std::stoi(token_text.substr(3, 2), nullptr, 16); - if (length < 1) { - return -1; - } - buf[0] = static_cast(hex_val); - return 1; + + memcpy(buf, result.data(), result.size()); + return (int)result.size(); + } + case LLAMA_VOCAB_TYPE_PLAMO2: { + // PLaMo-2 uses similar token handling as BPE/SPM + if (vocab.is_byte(token)) { + // Handle byte tokens like <0xXX> + if (token_text.length() == 6 && token_text.substr(0, 3) == "<0x" && token_text.back() == '>') { + int hex_val = std::stoi(token_text.substr(3, 2), nullptr, 16); + if (length < 1) { + return -1; } + buf[0] = static_cast(hex_val); + return 1; } - - // Normal token - just copy the text - std::string result = token_text; - return _try_copy(result.data(), result.size()); } - default: - GGML_ABORT("fatal error"); + + // Normal token - just copy the text + std::string result = token_text; + return _try_copy(result.data(), result.size()); } + default: + GGML_ABORT("fatal error"); } return 0; From 3828da49057515f48760949c4c71a75a36941967 Mon Sep 17 00:00:00 2001 From: Julian Brunner Date: Mon, 20 Apr 2026 10:14:27 +0200 Subject: [PATCH 05/10] handle INT32_MIN result of llama_token_to_piece in downstream uses --- common/common.cpp | 3 +++ examples/batched.swift/Sources/main.swift | 3 +++ examples/llama.swiftui/llama.cpp.swift/LibLlama.swift | 4 +++- tests/test-backend-sampler.cpp | 3 +++ tools/mtmd/mtmd.cpp | 3 +++ 5 files changed, 15 insertions(+), 1 deletion(-) diff --git a/common/common.cpp b/common/common.cpp index d77ddeda10eb..efea88aae70f 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -1662,6 +1662,9 @@ std::string common_token_to_piece(const struct llama_vocab * vocab, llama_token std::string piece; piece.resize(piece.capacity()); // using string internal cache, 15 bytes + '\n' const int n_chars = llama_token_to_piece(vocab, token, &piece[0], piece.size(), 0, special); + if (n_chars == std::numeric_limits::min()) { + throw std::runtime_error("Token to piece failed: supplied token is invalid"); + } if (n_chars < 0) { piece.resize(-n_chars); int check = llama_token_to_piece(vocab, token, &piece[0], piece.size(), 0, special); diff --git a/examples/batched.swift/Sources/main.swift b/examples/batched.swift/Sources/main.swift index fd90bbec5f75..83b47e7604e0 100644 --- a/examples/batched.swift/Sources/main.swift +++ b/examples/batched.swift/Sources/main.swift @@ -224,6 +224,9 @@ private func tokenize(text: String, add_bos: Bool) -> [llama_token] { private func token_to_piece(token: llama_token, buffer: inout [CChar]) -> String? { var result = [CChar](repeating: 0, count: 8) let nTokens = llama_token_to_piece(vocab, token, &result, Int32(result.count), 0, false) + if nTokens == Int32.min { + return nil + } if nTokens < 0 { let actualTokensCount = -Int(nTokens) result = .init(repeating: 0, count: actualTokensCount) diff --git a/examples/llama.swiftui/llama.cpp.swift/LibLlama.swift b/examples/llama.swiftui/llama.cpp.swift/LibLlama.swift index dc2bafc88b17..476e3c524231 100644 --- a/examples/llama.swiftui/llama.cpp.swift/LibLlama.swift +++ b/examples/llama.swiftui/llama.cpp.swift/LibLlama.swift @@ -319,7 +319,9 @@ actor LlamaContext { result.deallocate() } let nTokens = llama_token_to_piece(vocab, token, result, 8, 0, false) - + if nTokens == Int32.min { + return [] + } if nTokens < 0 { let newResult = UnsafeMutablePointer.allocate(capacity: Int(-nTokens)) newResult.initialize(repeating: Int8(0), count: Int(-nTokens)) diff --git a/tests/test-backend-sampler.cpp b/tests/test-backend-sampler.cpp index 58361ae80aea..d5f7838d99fd 100644 --- a/tests/test-backend-sampler.cpp +++ b/tests/test-backend-sampler.cpp @@ -250,6 +250,9 @@ struct test_context { std::string piece; piece.resize(piece.capacity()); // using string internal cache, 15 bytes + '\n' const int n_chars = llama_token_to_piece(vocab, token, &piece[0], piece.size(), 0, special); + if (n_chars == std::numeric_limits::min()) { + throw std::runtime_error("Token to piece failed: supplied token is invalid"); + } if (n_chars < 0) { piece.resize(-n_chars); int check = llama_token_to_piece(vocab, token, &piece[0], piece.size(), 0, special); diff --git a/tools/mtmd/mtmd.cpp b/tools/mtmd/mtmd.cpp index 63b7e4d052a8..4955fff25d66 100644 --- a/tools/mtmd/mtmd.cpp +++ b/tools/mtmd/mtmd.cpp @@ -619,6 +619,9 @@ struct mtmd_context { std::string piece; piece.resize(piece.capacity()); // using string internal cache, 15 bytes + '\n' const int n_chars = llama_token_to_piece(vocab, token, &piece[0], piece.size(), 0, special); + if (n_chars == std::numeric_limits::min()) { + throw std::runtime_error("Token to piece failed: supplied token is invalid"); + } if (n_chars < 0) { piece.resize(-n_chars); int check = llama_token_to_piece(vocab, token, &piece[0], piece.size(), 0, special); From af7af8adbe7318a80a5167000cc83d06203e3189 Mon Sep 17 00:00:00 2001 From: Julian Brunner Date: Sun, 26 Apr 2026 12:55:19 +0200 Subject: [PATCH 06/10] add limits includes in files that did not use it before --- tests/test-backend-sampler.cpp | 1 + tools/mtmd/mtmd.cpp | 1 + 2 files changed, 2 insertions(+) diff --git a/tests/test-backend-sampler.cpp b/tests/test-backend-sampler.cpp index d5f7838d99fd..6b0669a4f9ba 100644 --- a/tests/test-backend-sampler.cpp +++ b/tests/test-backend-sampler.cpp @@ -8,6 +8,7 @@ #undef NDEBUG #endif +#include #include #include #include diff --git a/tools/mtmd/mtmd.cpp b/tools/mtmd/mtmd.cpp index 4955fff25d66..fbb1550beb09 100644 --- a/tools/mtmd/mtmd.cpp +++ b/tools/mtmd/mtmd.cpp @@ -16,6 +16,7 @@ #include #endif +#include #include #include #include From 6b190ab6aa4529e1b37f9a8e796ff420983b0c05 Mon Sep 17 00:00:00 2001 From: Julian Brunner Date: Mon, 27 Apr 2026 17:51:13 +0200 Subject: [PATCH 07/10] add invalid token guard to token_to_piece_for_cache --- src/llama-vocab.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/llama-vocab.cpp b/src/llama-vocab.cpp index 837414967f25..61dd304c69cb 100644 --- a/src/llama-vocab.cpp +++ b/src/llama-vocab.cpp @@ -3137,6 +3137,9 @@ std::string llama_vocab::impl::token_to_piece_for_cache(llama_token token, bool std::string piece; piece.resize(piece.capacity()); // using string internal cache const int n_chars = vocab.token_to_piece(token, &piece[0], piece.size(), 0, special); + if (n_chars == std::numeric_limits::min()) { + throw std::runtime_error("Token to piece failed: supplied token is invalid"); + } if (n_chars < 0) { piece.resize(-n_chars); int check = vocab.token_to_piece(token, &piece[0], piece.size(), 0, special); From 0e9b56dd30043229dbbd9817edb4def108795490 Mon Sep 17 00:00:00 2001 From: Julian Brunner Date: Tue, 28 Apr 2026 00:45:53 +0200 Subject: [PATCH 08/10] propagate token_to_piece failure --- common/common.cpp | 3 +++ common/speculative.cpp | 1 + include/llama.h | 1 + src/llama-sampler.cpp | 6 ++++++ src/llama-vocab.cpp | 6 ++++++ tests/test-tokenizer-random.py | 2 ++ 6 files changed, 19 insertions(+) diff --git a/common/common.cpp b/common/common.cpp index efea88aae70f..ce61d8aa5766 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -1687,6 +1687,9 @@ std::string common_detokenize(const struct llama_vocab * vocab, const std::vecto std::string text; text.resize(std::max(text.capacity(), tokens.size())); int32_t n_chars = llama_detokenize(vocab, tokens.data(), (int32_t)tokens.size(), &text[0], (int32_t)text.size(), false, special); + if (n_chars == std::numeric_limits::min()) { + throw std::runtime_error("Detokenization failed: some supplied token is invalid"); + } if (n_chars < 0) { text.resize(-n_chars); n_chars = llama_detokenize(vocab, tokens.data(), (int32_t)tokens.size(), &text[0], (int32_t)text.size(), false, special); diff --git a/common/speculative.cpp b/common/speculative.cpp index 253a5ececbb5..ba6a3b6dc605 100644 --- a/common/speculative.cpp +++ b/common/speculative.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include diff --git a/include/llama.h b/include/llama.h index a5e971d72ca9..09707f8cdad5 100644 --- a/include/llama.h +++ b/include/llama.h @@ -1150,6 +1150,7 @@ extern "C" { /// @param text The char pointer must be large enough to hold the resulting text. /// @return Returns the number of chars/bytes on success, no more than text_len_max. /// @return Returns a negative number on failure - the number of chars/bytes that would have been returned. + /// @return Returns INT32_MIN if any of the tokens is not in the vocabulary /// @param remove_special Allow to remove BOS and EOS tokens if model is configured to do so. /// @param unparse_special If true, special tokens are rendered in the output. LLAMA_API int32_t llama_detokenize( diff --git a/src/llama-sampler.cpp b/src/llama-sampler.cpp index 9bbc5dbde247..87ba68f38c80 100644 --- a/src/llama-sampler.cpp +++ b/src/llama-sampler.cpp @@ -3676,6 +3676,9 @@ static void llama_sampler_infill_apply(struct llama_sampler * smpl, llama_token_ } int len0 = ctx->vocab->token_to_piece(cur_p->data[i0].id, ctx->buf0.data(), ctx->buf0.size(), 0, false); + if (len0 == std::numeric_limits::min()) { + throw std::runtime_error("Token to piece failed: supplied token is invalid"); + } if (len0 < 0) { ctx->buf0.resize(len0); len0 = ctx->vocab->token_to_piece(cur_p->data[i0].id, ctx->buf0.data(), ctx->buf0.size(), 0, false); @@ -3683,6 +3686,9 @@ static void llama_sampler_infill_apply(struct llama_sampler * smpl, llama_token_ } int len1 = ctx->vocab->token_to_piece(cur_p->data[i1].id, ctx->buf1.data(), ctx->buf1.size(), 0, false); + if (len1 == std::numeric_limits::min()) { + throw std::runtime_error("Token to piece failed: supplied token is invalid"); + } if (len1 < 0) { ctx->buf1.resize(len1); len1 = ctx->vocab->token_to_piece(cur_p->data[i1].id, ctx->buf1.data(), ctx->buf1.size(), 0, false); diff --git a/src/llama-vocab.cpp b/src/llama-vocab.cpp index 61dd304c69cb..47fa13983e36 100644 --- a/src/llama-vocab.cpp +++ b/src/llama-vocab.cpp @@ -3545,6 +3545,9 @@ int32_t llama_vocab::impl::detokenize( GGML_ASSERT(avail >= 0); int32_t n_chars = token_to_piece(tokens[i], text, avail, remove_space, unparse_special); remove_space = false; + if (n_chars == std::numeric_limits::min()) { + return std::numeric_limits::min(); + } if (n_chars < 0) { avail = 0; total -= n_chars; @@ -3970,6 +3973,9 @@ std::string llama_vocab::detokenize(const std::vector & tokens, boo std::string text; text.resize(std::max(text.capacity(), tokens.size())); int32_t n_chars = detokenize(tokens.data(), (int32_t)tokens.size(), &text[0], (int32_t)text.size(), false, special); + if (n_chars == std::numeric_limits::min()) { + throw std::runtime_error("Detokenization failed: some supplied token is invalid"); + } if (n_chars < 0) { text.resize(-n_chars); n_chars = detokenize(tokens.data(), (int32_t)tokens.size(), &text[0], (int32_t)text.size(), false, special); diff --git a/tests/test-tokenizer-random.py b/tests/test-tokenizer-random.py index 8fc476b63c3f..d7700107ab27 100644 --- a/tests/test-tokenizer-random.py +++ b/tests/test-tokenizer-random.py @@ -110,6 +110,8 @@ def detokenize(self, ids: list[int], remove_special: bool = False, unparse_speci for i, id in enumerate(ids): self.token_ids[i] = id num = self.lib.llama_detokenize(self.model, self.token_ids, len(ids), self.text_buff, len(self.text_buff), remove_special, unparse_special) + if num == - (1 << 31): + raise RuntimeError("error: detokenization failed: some supplied token is invalid") while num < 0 and len(self.text_buff) < (16 << 20): self.text_buff = self.ffi.new("uint8_t[]", -2 * num) num = self.lib.llama_detokenize(self.model, self.token_ids, len(ids), self.text_buff, len(self.text_buff), remove_special, unparse_special) From 36fe0b038834d3570daa6fd9af7c34185a95e2ab Mon Sep 17 00:00:00 2001 From: Julian Brunner Date: Tue, 28 Apr 2026 09:04:43 +0200 Subject: [PATCH 09/10] consistent punctuation --- include/llama.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/llama.h b/include/llama.h index 09707f8cdad5..0a87b3a0a97c 100644 --- a/include/llama.h +++ b/include/llama.h @@ -1135,7 +1135,7 @@ extern "C" { /// Does not write null terminator to the buffer. /// @return Returns the number of chars/bytes on success, no more than length. /// @return Returns a negative number on failure - the number of chars/bytes that would have been returned. - /// @return Returns INT32_MIN if the token is not in the vocabulary + /// @return Returns INT32_MIN if the token is not in the vocabulary. /// @param lstrip User can skip up to 'lstrip' leading spaces before copying (useful when encoding/decoding multiple tokens with 'add_space_prefix') /// @param special If true, special tokens are rendered in the output. LLAMA_API int32_t llama_token_to_piece( @@ -1150,7 +1150,7 @@ extern "C" { /// @param text The char pointer must be large enough to hold the resulting text. /// @return Returns the number of chars/bytes on success, no more than text_len_max. /// @return Returns a negative number on failure - the number of chars/bytes that would have been returned. - /// @return Returns INT32_MIN if any of the tokens is not in the vocabulary + /// @return Returns INT32_MIN if any of the tokens is not in the vocabulary. /// @param remove_special Allow to remove BOS and EOS tokens if model is configured to do so. /// @param unparse_special If true, special tokens are rendered in the output. LLAMA_API int32_t llama_detokenize( From 2b00549b188e21d58487fdf27408816e8dacdf5e Mon Sep 17 00:00:00 2001 From: Julian Brunner Date: Tue, 28 Apr 2026 09:09:11 +0200 Subject: [PATCH 10/10] exit on llama_token_to_piece error --- examples/batched.swift/Sources/main.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/batched.swift/Sources/main.swift b/examples/batched.swift/Sources/main.swift index 83b47e7604e0..b2d79a69eb14 100644 --- a/examples/batched.swift/Sources/main.swift +++ b/examples/batched.swift/Sources/main.swift @@ -225,7 +225,8 @@ private func token_to_piece(token: llama_token, buffer: inout [CChar]) -> String var result = [CChar](repeating: 0, count: 8) let nTokens = llama_token_to_piece(vocab, token, &result, Int32(result.count), 0, false) if nTokens == Int32.min { - return nil + print("llama_token_to_piece() failed") + exit(1) } if nTokens < 0 { let actualTokensCount = -Int(nTokens)