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>
244 lines
6.6 KiB
C++
244 lines
6.6 KiB
C++
#pragma once
|
|
|
|
#include "common.h"
|
|
|
|
#include <chrono>
|
|
#include <exception>
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <regex>
|
|
#include <vector>
|
|
|
|
struct testing {
|
|
std::ostream &out;
|
|
std::vector<std::string> stack;
|
|
std::regex filter;
|
|
bool filter_tests = false;
|
|
bool throw_exception = false;
|
|
bool verbose = false;
|
|
int tests = 0;
|
|
int assertions = 0;
|
|
int failures = 0;
|
|
int unnamed = 0;
|
|
int exceptions = 0;
|
|
|
|
static constexpr std::size_t status_column = 80;
|
|
|
|
explicit testing(std::ostream &os = std::cout) : out(os) {}
|
|
|
|
std::string indent() const {
|
|
if (stack.empty()) {
|
|
return "";
|
|
}
|
|
return std::string((stack.size() - 1) * 2, ' ');
|
|
}
|
|
|
|
std::string full_name() const {
|
|
return string_join(stack, ".");
|
|
}
|
|
|
|
void log(const std::string & msg) {
|
|
if (verbose) {
|
|
out << indent() << " " << msg << "\n";
|
|
}
|
|
}
|
|
|
|
void set_filter(const std::string & re) {
|
|
filter = std::regex(re);
|
|
filter_tests = true;
|
|
}
|
|
|
|
bool should_run() const {
|
|
if (filter_tests) {
|
|
if (!std::regex_match(full_name(), filter)) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
template <typename F>
|
|
void run_with_exceptions(F &&f, const char *ctx) {
|
|
try {
|
|
f();
|
|
} catch (const std::exception &e) {
|
|
++failures;
|
|
++exceptions;
|
|
out << indent() << "UNHANDLED EXCEPTION (" << ctx << "): " << e.what() << "\n";
|
|
if (throw_exception) {
|
|
throw;
|
|
}
|
|
} catch (...) {
|
|
++failures;
|
|
++exceptions;
|
|
out << indent() << "UNHANDLED EXCEPTION (" << ctx << "): unknown\n";
|
|
if (throw_exception) {
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
|
|
void print_result(const std::string &label, int new_failures, int new_assertions, const std::string &extra = "") const {
|
|
std::string line = indent() + label;
|
|
|
|
std::string details;
|
|
if (new_assertions > 0) {
|
|
if (new_failures == 0) {
|
|
details = std::to_string(new_assertions) + " assertion(s)";
|
|
} else {
|
|
details = std::to_string(new_failures) + " of " +
|
|
std::to_string(new_assertions) + " assertion(s) failed";
|
|
}
|
|
}
|
|
if (!extra.empty()) {
|
|
if (!details.empty()) {
|
|
details += ", ";
|
|
}
|
|
details += extra;
|
|
}
|
|
|
|
if (!details.empty()) {
|
|
line += " (" + details + ")";
|
|
}
|
|
|
|
std::string status = (new_failures == 0) ? "[PASS]" : "[FAIL]";
|
|
|
|
if (line.size() + 1 < status_column) {
|
|
line.append(status_column - line.size(), ' ');
|
|
} else {
|
|
line.push_back(' ');
|
|
}
|
|
|
|
out << line << status << "\n";
|
|
}
|
|
|
|
template <typename F>
|
|
void test(const std::string &name, F f) {
|
|
stack.push_back(name);
|
|
if (!should_run()) {
|
|
stack.pop_back();
|
|
return;
|
|
}
|
|
|
|
++tests;
|
|
out << indent() << name << "\n";
|
|
|
|
int before_failures = failures;
|
|
int before_assertions = assertions;
|
|
|
|
run_with_exceptions([&] { f(*this); }, "test");
|
|
|
|
int new_failures = failures - before_failures;
|
|
int new_assertions = assertions - before_assertions;
|
|
|
|
print_result(name, new_failures, new_assertions);
|
|
|
|
stack.pop_back();
|
|
}
|
|
|
|
template <typename F>
|
|
void test(F f) {
|
|
test("test #" + std::to_string(++unnamed), f);
|
|
}
|
|
|
|
template <typename F>
|
|
void bench(const std::string &name, F f, int iterations = 100) {
|
|
stack.push_back(name);
|
|
if (!should_run()) {
|
|
stack.pop_back();
|
|
return;
|
|
}
|
|
|
|
++tests;
|
|
out << indent() << "[bench] " << name << "\n";
|
|
|
|
int before_failures = failures;
|
|
int before_assertions = assertions;
|
|
|
|
using clock = std::chrono::high_resolution_clock;
|
|
|
|
std::chrono::microseconds duration(0);
|
|
|
|
run_with_exceptions([&] {
|
|
for (auto i = 0; i < iterations; i++) {
|
|
auto start = clock::now();
|
|
f();
|
|
duration += std::chrono::duration_cast<std::chrono::microseconds>(clock::now() - start);
|
|
}
|
|
}, "bench");
|
|
|
|
auto avg_elapsed = duration.count() / iterations;
|
|
auto avg_elapsed_s = std::chrono::duration_cast<std::chrono::duration<double>>(duration).count() / iterations;
|
|
auto rate = (avg_elapsed_s > 0.0) ? (1.0 / avg_elapsed_s) : 0.0;
|
|
|
|
int new_failures = failures - before_failures;
|
|
int new_assertions = assertions - before_assertions;
|
|
|
|
std::string extra =
|
|
"n=" + std::to_string(iterations) +
|
|
" avg=" + std::to_string(avg_elapsed) + "us" +
|
|
" rate=" + std::to_string(int(rate)) + "/s";
|
|
|
|
print_result("[bench] " + name, new_failures, new_assertions, extra);
|
|
|
|
stack.pop_back();
|
|
}
|
|
|
|
template <typename F>
|
|
void bench(F f, int iterations = 100) {
|
|
bench("bench #" + std::to_string(++unnamed), f, iterations);
|
|
}
|
|
|
|
// Assertions
|
|
bool assert_true(bool cond) {
|
|
return assert_true("", cond);
|
|
}
|
|
|
|
bool assert_true(const std::string &msg, bool cond) {
|
|
++assertions;
|
|
if (!cond) {
|
|
++failures;
|
|
out << indent() << "ASSERTION FAILED";
|
|
if (!msg.empty()) {
|
|
out << " : " << msg;
|
|
}
|
|
out << "\n";
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
template <typename A, typename B>
|
|
bool assert_equal(const A &expected, const B &actual) {
|
|
return assert_equal("", expected, actual);
|
|
}
|
|
|
|
template <typename A, typename B>
|
|
bool assert_equal(const std::string &msg, const A &expected, const B &actual) {
|
|
++assertions;
|
|
if (!(actual == expected)) {
|
|
++failures;
|
|
out << indent() << "ASSERT EQUAL FAILED";
|
|
if (!msg.empty()) {
|
|
out << " : " << msg;
|
|
}
|
|
out << "\n";
|
|
|
|
out << indent() << " expected: " << expected << "\n";
|
|
out << indent() << " actual : " << actual << "\n";
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// Print summary and return an exit code
|
|
int summary() const {
|
|
out << "\n";
|
|
out << "tests : " << tests << "\n";
|
|
out << "assertions : " << assertions << "\n";
|
|
out << "failures : " << failures << "\n";
|
|
out << "exceptions : " << exceptions << "\n";
|
|
return failures == 0 ? 0 : 1;
|
|
}
|
|
};
|