mirror of
https://github.com/ikawrakow/ik_llama.cpp.git
synced 2026-03-10 14:00:08 +00:00
--------- Co-authored-by: Piotr Wilkin <piotr.wilkin@syndatis.com> common : add nemotron 3 parsing (#18077) common : add parser for ministral/mistral large 3/devstral 2 (#17713) common : default content to an empty string (#18485) chat: make tool description and parameters optional per OpenAI spec (#18478) Per the OpenAI API specification, both 'description' and 'parameters' fields in tool function definitions are optional. Previously, the parser would throw an exception if these fields were missing. Attempts to fix #17667 common : implement new jinja template engine (#18462) --------- Co-authored-by: Alde Rojas <hello@alde.dev> Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com> Co-authored-by: Georgi Gerganov <ggerganov@gmail.com> jinja: correct member access rule (#18905) jinja : fix lexing of float literals with sign (#18901) jinja : add missing tojson filter for bool (#18900) jinja : attribute support for join, map and sort (#18883) jinja : fix object item order (and properly implement dictsort) (#18904) tests : add test-jinja -py option for cross-checking (#18906) Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com> --------- Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com> ci : run test-jinja -py on high perf [no ci] (#18916) jinja : fix undefined keys and attributes and int/float as bool (#18924) jinja: support none|string (#18995) Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com> Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com> --------- Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com> jinja : implement mixed type object keys (#18955) --------- Co-authored-by: Xuan Son Nguyen <son@huggingface.co> jinja : undefined should be treated as sequence/iterable (return string/array) by filters/tests (#19147) `tojson` is not a supported `undefined` filter keep it DRY and fix some types jinja : do not pass empty tools and add some none filters (#19176) jinja : add unordered_map include to value.h [no ci] (#19205) jinja : add missing 'in' test to template engine (#19004) (#19239) The jinja template parser was missing the 'in' test from global_builtins(), causing templates using reject("in", ...), select("in", ...), or 'x is in(y)' to fail with "selectattr: unknown test 'in'". This broke tool-calling for Qwen3-Coder and any other model whose chat template uses the 'in' test. Added test_is_in supporting array, string, and object containment checks, mirroring the existing 'in' operator logic in runtime.cpp. Includes test cases for all three containment types plus reject/select filter usage. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Sid Mohan <sidmohan0@users.noreply.github.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com> Co-authored-by: Xuan Son Nguyen <son@huggingface.co> Add Jinja support for "indent" string filter (#19529) Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com> Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com> --------- Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com> add vendor refactor chat server : support preserving reasoning_content in assistant message (#18994) chat : fix translategemma crash on common_chat_format_example (#19019) chat: fix language input for translategemma (#19052) Co-authored-by: Aldehir Rojas <hello@alde.dev> --------- Co-authored-by: Aldehir Rojas <hello@alde.dev> chat: fix case where template accepts type content only (#19419) mtmd : chat : Fix extra \n between text and media marker (#19595) Thanks to @tugot17 for detecting and reporting the issue. For vision models (e.g. LFM2.5-VL-1.6B and Qwen/Qwen3-VL-4B-Instruct) `llama-mtmd-cli` produces identical output to HF implementation. However `llama-server` doesn't. I traced it down to extra newline inserted after `<__media__>`. This happens in `to_json_oaicompat`, that treats media markers as text and joins all parts with `\n` separator. PR introduces new type `media_marker` and uses it for media markers. Extra logic is added to prevent insertion of newlines before and after media markers. With this change number of input tokens is identical to HF implementation and as a result the output is also identical. I explored other ways to address the issue * remove completely `\n` between text parts in `to_json_oaicompat` * merge text messages in server-common.cpp before sending them to `to_json_oaicompat` Please propose alternative ways of fixing this issue. Co-authored-by: Piotr Wilkin (ilintar) <piotr.wilkin@syndatis.com> --------- Co-authored-by: Piotr Wilkin (ilintar) <piotr.wilkin@syndatis.com> common : merge qwen3-coder and nemotron nano 3 parsers (#19765) common : fix improper trimming in XML parser on complete message (#19805) Co-authored-by: Jules LEIDELINGER <11395311+julio75012@users.noreply.github.com> jinja: correct stats for tojson and string filters (#19785) jinja : correct default size for string slices (#19913) common : handle unicode during partial json parsing (#16526) common : fix json schema with '\' in literals (#17307) add back qwen_coder_xml and mirothinker Co-authored-by: Aldehir Rojas <hello@alde.dev>
150 lines
5.0 KiB
C++
150 lines
5.0 KiB
C++
#pragma once
|
|
|
|
#include <string>
|
|
#include <sstream>
|
|
#include <algorithm>
|
|
#include <cstdint>
|
|
#include <cstring>
|
|
|
|
namespace jinja {
|
|
|
|
static void string_replace_all(std::string & s, const std::string & search, const std::string & replace) {
|
|
if (search.empty()) {
|
|
return;
|
|
}
|
|
std::string builder;
|
|
builder.reserve(s.length());
|
|
size_t pos = 0;
|
|
size_t last_pos = 0;
|
|
while ((pos = s.find(search, last_pos)) != std::string::npos) {
|
|
builder.append(s, last_pos, pos - last_pos);
|
|
builder.append(replace);
|
|
last_pos = pos + search.length();
|
|
}
|
|
builder.append(s, last_pos, std::string::npos);
|
|
s = std::move(builder);
|
|
}
|
|
|
|
// for displaying source code around error position
|
|
static std::string peak_source(const std::string & source, size_t pos, size_t max_peak_chars = 40) {
|
|
if (source.empty()) {
|
|
return "(no source available)";
|
|
}
|
|
std::string output;
|
|
size_t start = (pos >= max_peak_chars) ? (pos - max_peak_chars) : 0;
|
|
size_t end = std::min(pos + max_peak_chars, source.length());
|
|
std::string substr = source.substr(start, end - start);
|
|
string_replace_all(substr, "\n", "↵");
|
|
output += "..." + substr + "...\n";
|
|
std::string spaces(pos - start + 3, ' ');
|
|
output += spaces + "^";
|
|
return output;
|
|
}
|
|
|
|
static std::string fmt_error_with_source(const std::string & tag, const std::string & msg, const std::string & source, size_t pos) {
|
|
std::ostringstream oss;
|
|
oss << tag << ": " << msg << "\n";
|
|
oss << peak_source(source, pos);
|
|
return oss.str();
|
|
}
|
|
|
|
// Note: this is a simple hasher, not cryptographically secure, just for hash table usage
|
|
struct hasher {
|
|
static constexpr auto size_t_digits = sizeof(size_t) * 8;
|
|
static constexpr size_t prime = size_t_digits == 64 ? 0x100000001b3 : 0x01000193;
|
|
static constexpr size_t seed = size_t_digits == 64 ? 0xcbf29ce484222325 : 0x811c9dc5;
|
|
static constexpr auto block_size = sizeof(size_t); // in bytes; allowing the compiler to vectorize the computation
|
|
|
|
static_assert(size_t_digits == 64 || size_t_digits == 32);
|
|
static_assert(block_size == 8 || block_size == 4);
|
|
|
|
uint8_t buffer[block_size];
|
|
size_t idx = 0; // current index in buffer
|
|
size_t state = seed;
|
|
|
|
hasher() = default;
|
|
hasher(const std::type_info & type_inf) noexcept {
|
|
const auto type_hash = type_inf.hash_code();
|
|
update(&type_hash, sizeof(type_hash));
|
|
}
|
|
|
|
// Properties:
|
|
// - update is not associative: update(a).update(b) != update(b).update(a)
|
|
// - update(a ~ b) == update(a).update(b) with ~ as concatenation operator --> useful for streaming
|
|
// - update("", 0) --> state unchanged with empty input
|
|
hasher& update(void const * bytes, size_t len) noexcept {
|
|
const uint8_t * c = static_cast<uint8_t const *>(bytes);
|
|
if (len == 0) {
|
|
return *this;
|
|
}
|
|
size_t processed = 0;
|
|
|
|
// first, fill the existing buffer if it's partial
|
|
if (idx > 0) {
|
|
size_t to_fill = block_size - idx;
|
|
if (to_fill > len) {
|
|
to_fill = len;
|
|
}
|
|
std::memcpy(buffer + idx, c, to_fill);
|
|
idx += to_fill;
|
|
processed += to_fill;
|
|
if (idx == block_size) {
|
|
update_block(buffer);
|
|
idx = 0;
|
|
}
|
|
}
|
|
|
|
// process full blocks from the remaining input
|
|
for (; processed + block_size <= len; processed += block_size) {
|
|
update_block(c + processed);
|
|
}
|
|
|
|
// buffer any remaining bytes
|
|
size_t remaining = len - processed;
|
|
if (remaining > 0) {
|
|
std::memcpy(buffer, c + processed, remaining);
|
|
idx = remaining;
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
// convenience function for testing only
|
|
hasher& update(const std::string & s) noexcept {
|
|
return update(s.data(), s.size());
|
|
}
|
|
|
|
// finalize and get the hash value
|
|
// note: after calling digest, the hasher state is modified, do not call update() again
|
|
size_t digest() noexcept {
|
|
// if there are remaining bytes in buffer, fill the rest with zeros and process
|
|
if (idx > 0) {
|
|
for (size_t i = idx; i < block_size; ++i) {
|
|
buffer[i] = 0;
|
|
}
|
|
update_block(buffer);
|
|
idx = 0;
|
|
}
|
|
|
|
return state;
|
|
}
|
|
|
|
private:
|
|
// IMPORTANT: block must have at least block_size bytes
|
|
void update_block(const uint8_t * block) noexcept {
|
|
size_t blk = static_cast<uint32_t>(block[0])
|
|
| (static_cast<uint32_t>(block[1]) << 8)
|
|
| (static_cast<uint32_t>(block[2]) << 16)
|
|
| (static_cast<uint32_t>(block[3]) << 24);
|
|
if constexpr (block_size == 8) {
|
|
blk = blk | (static_cast<uint64_t>(block[4]) << 32)
|
|
| (static_cast<uint64_t>(block[5]) << 40)
|
|
| (static_cast<uint64_t>(block[6]) << 48)
|
|
| (static_cast<uint64_t>(block[7]) << 56);
|
|
}
|
|
state ^= blk;
|
|
state *= prime;
|
|
}
|
|
};
|
|
|
|
} // namespace jinja
|