Skip to content

sync : vendor #13901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ charset = unset
trim_trailing_whitespace = unset
insert_final_newline = unset

[tools/mtmd/vendor/miniaudio.h]
[vendor/miniaudio/miniaudio.h]
trim_trailing_whitespace = unset
insert_final_newline = unset
13 changes: 5 additions & 8 deletions common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,20 @@ add_library(${TARGET} STATIC
arg.cpp
arg.h
base64.hpp
chat.cpp
chat.h
chat-parser.cpp
chat-parser.h
chat.cpp
chat.h
common.cpp
common.h
console.cpp
console.h
json-schema-to-grammar.cpp
json.hpp
json-partial.h
json-partial.cpp
json-partial.h
json-schema-to-grammar.cpp
llguidance.cpp
log.cpp
log.h
minja/chat-template.hpp
minja/minja.hpp
ngram-cache.cpp
ngram-cache.h
regex-partial.cpp
Expand Down Expand Up @@ -147,7 +144,7 @@ if (LLAMA_LLGUIDANCE)
set(LLAMA_COMMON_EXTRA_LIBS ${LLAMA_COMMON_EXTRA_LIBS} llguidance ${LLGUIDANCE_PLATFORM_LIBS})
endif ()

target_include_directories(${TARGET} PUBLIC .)
target_include_directories(${TARGET} PUBLIC . ../vendor)
target_compile_features (${TARGET} PUBLIC cxx_std_17)
target_link_libraries (${TARGET} PRIVATE ${LLAMA_COMMON_EXTRA_LIBS} PUBLIC llama Threads::Threads)

Expand Down
10 changes: 6 additions & 4 deletions common/arg.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include "gguf.h" // for reading GGUF splits
#include "arg.h"

#include "chat.h"
#include "common.h"
#include "gguf.h" // for reading GGUF splits
#include "json-schema-to-grammar.h"
#include "log.h"
#include "sampling.h"
#include "chat.h"

// fix problem with std::min and std::max
#if defined(_WIN32)
Expand All @@ -15,6 +16,9 @@
#include <windows.h>
#endif

#define JSON_ASSERT GGML_ASSERT
#include <nlohmann/json.hpp>

#include <algorithm>
#include <climits>
#include <cstdarg>
Expand All @@ -34,8 +38,6 @@
#include <future>
#endif

#include "json-schema-to-grammar.h"

using json = nlohmann::ordered_json;

std::initializer_list<enum llama_example> mmproj_examples = {
Expand Down
3 changes: 2 additions & 1 deletion common/chat-parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

#include "chat.h"
#include "json-partial.h"
#include "json.hpp"
#include "regex-partial.h"

#include <nlohmann/json.hpp>

#include <optional>
#include <string>
#include <vector>
Expand Down
8 changes: 4 additions & 4 deletions common/chat.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#include "chat.h"
#include "chat-parser.h"
#include "common.h"
#include "json-partial.h"
#include "json-schema-to-grammar.h"
#include "log.h"
#include "json-partial.h"
#include "minja/chat-template.hpp"
#include "minja/minja.hpp"
#include "regex-partial.h"

#include <minja/chat-template.hpp>
#include <minja/minja.hpp>

#include <cstdio>
#include <exception>
#include <iostream>
Expand All @@ -16,7 +17,6 @@
#include <string>
#include <vector>


static std::string format_time(const std::chrono::system_clock::time_point & now, const std::string & format) {
auto time = std::chrono::system_clock::to_time_t(now);
auto local_time = *std::localtime(&time);
Expand Down
9 changes: 5 additions & 4 deletions common/json-partial.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include <json-partial.h>
#include "ggml.h"
#include "json-partial.h"

#include "log.h"
#include <string>

#include <json.hpp>
#include <nlohmann/json.hpp>

#include <string>

using json = nlohmann::ordered_json;

Expand Down
3 changes: 2 additions & 1 deletion common/json-partial.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include <json.hpp>

#include <nlohmann/json.hpp>

// Healing marker (empty if the JSON was fully parsed / wasn't healed).
struct common_healing_marker {
Expand Down
3 changes: 2 additions & 1 deletion common/json-schema-to-grammar.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include "json-schema-to-grammar.h"
#include "common.h"

#include <nlohmann/json.hpp>

#include <algorithm>
#include <fstream>
#include <map>
#include <regex>
#include <sstream>
Expand Down
8 changes: 4 additions & 4 deletions common/json-schema-to-grammar.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once

#include "ggml.h"
// Change JSON_ASSERT from assert() to GGML_ASSERT:
#define JSON_ASSERT GGML_ASSERT
#include "json.hpp"
#include <nlohmann/json_fwd.hpp>

#include <functional>
#include <string>

std::string json_schema_to_grammar(const nlohmann::ordered_json & schema,
bool force_gbnf = false);
Expand Down
22 changes: 22 additions & 0 deletions scripts/sync_vendor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python3

import urllib.request

vendor = {
"https://github.com/nlohmann/json/releases/latest/download/json.hpp": "vendor/nlohmann/json.hpp",
"https://github.com/nlohmann/json/releases/latest/download/json_fwd.hpp": "vendor/nlohmann/json_fwd.hpp",

# sync manually
# "https://raw.githubusercontent.com/ochafik/minja/refs/heads/main/include/minja/minja.hpp": "vendor/minja/minja.hpp",
# "https://raw.githubusercontent.com/ochafik/minja/refs/heads/main/include/minja/chat-template.hpp": "vendor/minja/chat-template.hpp",

"https://raw.githubusercontent.com/nothings/stb/refs/heads/master/stb_image.h": "vendor/stb/stb_image.h",

"https://github.com/mackron/miniaudio/raw/refs/tags/0.11.22/miniaudio.h": "vendor/miniaudio/miniaudio.h",

"https://raw.githubusercontent.com/yhirose/cpp-httplib/refs/tags/v0.20.1/httplib.h": "vendor/cpp-httplib/httplib.h",
}

for url, filename in vendor.items():
print(f"downloading {url} to {filename}") # noqa: NP100
urllib.request.urlretrieve(url, filename)
11 changes: 6 additions & 5 deletions tests/test-chat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
//
// cmake -B build && cmake --build build --parallel && ./build/bin/test-chat ../minja/build/tests/*.jinja 2>/dev/null
//
#include <fstream>
#include <iostream>
#include <json.hpp>
#include <string>

#include "chat.h"

#include "../src/unicode.h"
#include "../src/llama-grammar.h"

#include <nlohmann/json.hpp>

#include <fstream>
#include <iostream>
#include <string>

using json = nlohmann::ordered_json;

static std::ostream & operator<<(std::ostream & os, const common_chat_msg_diff & diff) {
Expand Down
2 changes: 2 additions & 0 deletions tests/test-grammar-integration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "../src/unicode.h"
#include "../src/llama-grammar.h"

#include <nlohmann/json.hpp>

#include <cassert>
#include <string>
#include <vector>
Expand Down
2 changes: 2 additions & 0 deletions tests/test-json-schema-to-grammar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include "../src/llama-grammar.h"

#include <nlohmann/json.hpp>

#include <cassert>
#include <fstream>
#include <sstream>
Expand Down
3 changes: 1 addition & 2 deletions tools/mtmd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ add_library(mtmd_helper OBJECT

target_link_libraries(mtmd_helper PRIVATE ggml llama mtmd ${CMAKE_THREAD_LIBS_INIT})
target_include_directories(mtmd_helper PUBLIC .)
target_include_directories(mtmd_helper PRIVATE ./vendor)
target_include_directories(mtmd_helper PRIVATE ../..)
target_include_directories(mtmd_helper PRIVATE ../../vendor)
target_compile_features(mtmd_helper PRIVATE cxx_std_17)

if (BUILD_SHARED_LIBS)
Expand Down
4 changes: 2 additions & 2 deletions tools/mtmd/mtmd-helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
#define MA_NO_ENGINE
#define MA_NO_GENERATION
#define MA_API static
#include "vendor/miniaudio.h"
#include "miniaudio/miniaudio.h"

#define STB_IMAGE_IMPLEMENTATION
#include "vendor/stb_image.h"
#include "stb/stb_image.h"

#define LOG_INF(...) fprintf(stdout, __VA_ARGS__)
#define LOG_ERR(...) fprintf(stderr, __VA_ARGS__)
Expand Down
17 changes: 10 additions & 7 deletions tools/run/run.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
#include "chat.h"
#include "common.h"
#include "llama-cpp.h"
#include "log.h"

#include "linenoise.cpp/linenoise.h"

#define JSON_ASSERT GGML_ASSERT
#include <nlohmann/json.hpp>

#if defined(_WIN32)
# include <windows.h>
# include <io.h>
Expand All @@ -24,13 +34,6 @@
#include <string>
#include <vector>

#include "chat.h"
#include "common.h"
#include "json.hpp"
#include "linenoise.cpp/linenoise.h"
#include "llama-cpp.h"
#include "log.h"

#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(_WIN32)
[[noreturn]] static void sigint_handler(int) {
printf("\n" LOG_COL_DEFAULT);
Expand Down
1 change: 0 additions & 1 deletion tools/server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ endif()
set(TARGET_SRCS
server.cpp
utils.hpp
httplib.h
)
set(PUBLIC_ASSETS
index.html.gz
Expand Down
3 changes: 0 additions & 3 deletions tools/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
#include "mtmd.h"
#include "mtmd-helper.h"

// Change JSON_ASSERT from assert() to GGML_ASSERT:
#define JSON_ASSERT GGML_ASSERT
#include "json.hpp"
// mime type for sending response
#define MIMETYPE_JSON "application/json; charset=utf-8"

Expand Down
7 changes: 3 additions & 4 deletions tools/server/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@
#include "base64.hpp"
#include "mtmd.h"
#include "mtmd-helper.h"
#include "chat.h"

// increase max payload length to allow use of larger context size
#define CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH 1048576
// disable Nagle's algorithm
#define CPPHTTPLIB_TCP_NODELAY true
#include "httplib.h"
#include <cpp-httplib/httplib.h>

// Change JSON_ASSERT from assert() to GGML_ASSERT:
#define JSON_ASSERT GGML_ASSERT
#include "json.hpp"
#include "chat.h"
#include <nlohmann/json.hpp>

#include <random>
#include <sstream>
Expand Down
4 changes: 3 additions & 1 deletion tools/tts/tts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#include "sampling.h"
#include "log.h"
#include "llama.h"
#include "json.hpp"

#define JSON_ASSERT GGML_ASSERT
#include <nlohmann/json.hpp>

#include <algorithm>
#include <cmath>
Expand Down
18 changes: 15 additions & 3 deletions tools/server/httplib.h → vendor/cpp-httplib/httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#ifndef CPPHTTPLIB_HTTPLIB_H
#define CPPHTTPLIB_HTTPLIB_H

#define CPPHTTPLIB_VERSION "0.20.0"
#define CPPHTTPLIB_VERSION "0.20.1"

/*
* Configuration
Expand Down Expand Up @@ -145,6 +145,10 @@
#define CPPHTTPLIB_LISTEN_BACKLOG 5
#endif

#ifndef CPPHTTPLIB_MAX_LINE_LENGTH
#define CPPHTTPLIB_MAX_LINE_LENGTH 32768
#endif

/*
* Headers
*/
Expand Down Expand Up @@ -3067,6 +3071,11 @@ inline bool stream_line_reader::getline() {
#endif

for (size_t i = 0;; i++) {
if (size() >= CPPHTTPLIB_MAX_LINE_LENGTH) {
// Treat exceptionally long lines as an error to
// prevent infinite loops/memory exhaustion
return false;
}
char byte;
auto n = strm_.read(&byte, 1);

Expand Down Expand Up @@ -6055,6 +6064,8 @@ inline void calc_actual_timeout(time_t max_timeout_msec, time_t duration_msec,
auto actual_timeout_msec =
(std::min)(max_timeout_msec - duration_msec, timeout_msec);

if (actual_timeout_msec < 0) { actual_timeout_msec = 0; }

actual_timeout_sec = actual_timeout_msec / 1000;
actual_timeout_usec = (actual_timeout_msec % 1000) * 1000;
}
Expand Down Expand Up @@ -7327,8 +7338,9 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
}

// Setup `is_connection_closed` method
req.is_connection_closed = [&]() {
return !detail::is_socket_alive(strm.socket());
auto sock = strm.socket();
req.is_connection_closed = [sock]() {
return !detail::is_socket_alive(sock);
};

// Routing
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <string>
#include <vector>

#include <json.hpp>
#include <nlohmann/json.hpp>

using json = nlohmann::ordered_json;

Expand Down
2 changes: 1 addition & 1 deletion common/minja/minja.hpp → vendor/minja/minja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <utility>
#include <vector>

#include <json.hpp>
#include <nlohmann/json.hpp>

using json = nlohmann::ordered_json;

Expand Down
Loading
Loading