Skip to content

armgit / PS5LM

Public Imported from GitHub
0

llama.cpp on a jailbroken PS5: run any GGUF model natively on the console, starting with Qwen 3.8

llama.cpp patch 0006: tojson writes floats as Python does

Mert Cobanove21a57dae08a
diff --git a/patches/0006-jinja-tojson-floats-as-python.patch b/patches/0006-jinja-tojson-floats-as-python.patch
new file mode 100644
index 0000000..d2c138d
--- /dev/null
+++ b/patches/0006-jinja-tojson-floats-as-python.patch
@@ -0,0 +1,69 @@
+diff --git a/common/jinja/value.cpp b/common/jinja/value.cpp
+index 6edd8b9..d467942 100644
+--- a/common/jinja/value.cpp
++++ b/common/jinja/value.cpp
+@@ -5,6 +5,8 @@
+ // for converting from JSON to jinja values
+ #include "json.h"
+
++#include <charconv>
++#include <cmath>
+ #include <sstream>
+ #include <string>
+ #include <cctype>
+@@ -1519,6 +1521,46 @@ void global_from_json(context & ctx, const common_json & json_obj, bool mark_inp
+
+ // recursively convert value to JSON string
+ // TODO: avoid circular references
++// A float as Python's json.dumps writes it (repr): the shortest digits that read back the same
++// value, fixed notation for exponents -4 to 15 with ".0" on whole numbers, else 1e+16 style.
++// ostream's default (6 significant digits) wrote 0.0 as 0 and 1234567.89 as 1.23457e+06.
++static std::string float_to_json(double v) {
++ if (std::isnan(v)) {
++ return "NaN";
++ }
++ if (std::isinf(v)) {
++ return v > 0 ? "Infinity" : "-Infinity";
++ }
++ char buf[64];
++ const auto res = std::to_chars(buf, buf + sizeof(buf), v, std::chars_format::scientific);
++ const std::string sci(buf, res.ptr); // [-]d[.ddd]e(+|-)dd
++ const size_t e_pos = sci.find('e');
++ const bool neg = sci[0] == '-';
++ std::string digits;
++ for (size_t i = neg ? 1 : 0; i < e_pos; i++) {
++ if (sci[i] != '.') {
++ digits += sci[i];
++ }
++ }
++ const int exp = std::stoi(sci.substr(e_pos + 1));
++ std::string out = neg ? "-" : "";
++ if (exp >= -4 && exp < 16) {
++ if (exp < 0) {
++ out += "0." + std::string(-exp - 1, '0') + digits;
++ } else {
++ if (digits.size() <= (size_t) exp + 1) {
++ digits.append(exp + 1 - digits.size(), '0');
++ }
++ const std::string frac = digits.substr(exp + 1);
++ out += digits.substr(0, exp + 1) + "." + (frac.empty() ? "0" : frac);
++ }
++ } else {
++ out += digits.substr(0, 1) + (digits.size() > 1 ? "." + digits.substr(1) : "");
++ out += (exp < 0 ? "e-" : "e+") + std::string(std::abs(exp) < 10 ? "0" : "") + std::to_string(std::abs(exp));
++ }
++ return out;
++}
++
+ static void value_to_json_internal(std::ostringstream & oss, const value & val, int curr_lvl, int indent, const std::string_view item_sep, const std::string_view key_sep) {
+ auto indent_str = [indent, curr_lvl]() -> std::string {
+ return (indent > 0) ? std::string(curr_lvl * indent, ' ') : "";
+@@ -1534,7 +1576,7 @@ static void value_to_json_internal(std::ostringstream & oss, const value & val,
+ } else if (is_val<value_int>(val)) {
+ oss << val->as_int();
+ } else if (is_val<value_float>(val)) {
+- oss << val->as_float();
++ oss << float_to_json(val->as_float());
+ } else if (is_val<value_string>(val)) {
+ oss << "\"";
+ for (char c : val->as_string().str()) {