Archived
radiance 1.0.0
This commit is contained in:
commit
ae74a8aebf
616 files changed
+338692
No files matched your search
Vendored
+200
@@ -0,0 +1,200 @@
|
||||
/* vendor/shim/common.h -- NOT a lift. This file is ours.
|
||||
*
|
||||
* llama.cpp's common/common.h is 1000 lines of CLI arguments, sampling parameters, model
|
||||
* downloading and llama.h types. The lifted chat and PEG-parser sources need eleven string
|
||||
* helpers out of it and nothing else. Providing those here, with upstream's implementations
|
||||
* (MIT, llama.cpp 06938ac12 common/common.{h,cpp}) transcribed unchanged, is what lets the lifted
|
||||
* files compile BYTE-FOR-BYTE as upstream wrote them -- which is the whole point of the
|
||||
* patch-series rule in spec §12.
|
||||
*
|
||||
* If a future refresh of vendor/ brings in a lift that needs more of common.h, the choice is to
|
||||
* add it here or to rewrite that lift into core/. It is not to start editing the lift.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
#include <regex>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include "ggml.h" /* GGML_ASSERT, which string_format uses */
|
||||
#include "llama.h" /* llama_token and LLAMA_TOKEN_NULL, which common_grammar_trigger uses */
|
||||
|
||||
#ifdef __GNUC__
|
||||
# define LLAMA_COMMON_ATTRIBUTE_FORMAT(...) __attribute__((format(printf, __VA_ARGS__)))
|
||||
#else
|
||||
# define LLAMA_COMMON_ATTRIBUTE_FORMAT(...)
|
||||
#endif
|
||||
|
||||
LLAMA_COMMON_ATTRIBUTE_FORMAT(1, 2)
|
||||
inline std::string string_format(const char* fmt, ...) {
|
||||
va_list ap, ap2;
|
||||
va_start(ap, fmt);
|
||||
va_copy(ap2, ap);
|
||||
const int size = vsnprintf(nullptr, 0, fmt, ap);
|
||||
GGML_ASSERT(size >= 0);
|
||||
std::vector<char> buf((size_t)size + 1);
|
||||
const int size2 = vsnprintf(buf.data(), (size_t)size + 1, fmt, ap2);
|
||||
GGML_ASSERT(size2 == size);
|
||||
va_end(ap2);
|
||||
va_end(ap);
|
||||
return std::string(buf.data(), (size_t)size);
|
||||
}
|
||||
|
||||
inline 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, 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);
|
||||
}
|
||||
|
||||
inline std::string regex_escape(const std::string& s) {
|
||||
static const std::regex special_chars("[.^$|()*+?\\[\\]{}\\\\]");
|
||||
return std::regex_replace(s, special_chars, "\\$&");
|
||||
}
|
||||
|
||||
inline std::string string_join(const std::vector<std::string>& values,
|
||||
const std::string& separator) {
|
||||
std::ostringstream result;
|
||||
for (size_t i = 0; i < values.size(); ++i) {
|
||||
if (i > 0) result << separator;
|
||||
result << values[i];
|
||||
}
|
||||
return result.str();
|
||||
}
|
||||
|
||||
inline std::vector<std::string> string_split(const std::string& str,
|
||||
const std::string& delimiter) {
|
||||
std::vector<std::string> parts;
|
||||
size_t start = 0, end = str.find(delimiter);
|
||||
while (end != std::string::npos) {
|
||||
parts.push_back(str.substr(start, end - start));
|
||||
start = end + delimiter.length();
|
||||
end = str.find(delimiter, start);
|
||||
}
|
||||
parts.push_back(str.substr(start));
|
||||
return parts;
|
||||
}
|
||||
|
||||
inline std::string string_repeat(const std::string& str, size_t n) {
|
||||
if (n == 0) return "";
|
||||
std::string result;
|
||||
result.reserve(str.length() * n);
|
||||
for (size_t i = 0; i < n; ++i) result += str;
|
||||
return result;
|
||||
}
|
||||
|
||||
inline std::string string_strip(const std::string& str) {
|
||||
size_t start = 0, end = str.size();
|
||||
while (start < end && std::isspace((unsigned char)str[start])) ++start;
|
||||
while (end > start && std::isspace((unsigned char)str[end - 1])) --end;
|
||||
return str.substr(start, end - start);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
static std::vector<T> string_split(const std::string& str, char delim) {
|
||||
static_assert(!std::is_same<T, std::string>::value,
|
||||
"Please use the specialized version for std::string");
|
||||
std::vector<T> values;
|
||||
std::istringstream str_stream(str);
|
||||
std::string token;
|
||||
while (std::getline(str_stream, token, delim)) {
|
||||
T value;
|
||||
std::istringstream token_stream(token);
|
||||
token_stream >> value;
|
||||
values.push_back(value);
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
template <>
|
||||
inline std::vector<std::string> string_split<std::string>(const std::string& str, char delim) {
|
||||
std::vector<std::string> parts;
|
||||
size_t begin_pos = 0, delim_pos = str.find(delim);
|
||||
while (delim_pos != std::string::npos) {
|
||||
parts.emplace_back(str.substr(begin_pos, delim_pos - begin_pos));
|
||||
begin_pos = delim_pos + 1;
|
||||
delim_pos = str.find(delim, begin_pos);
|
||||
}
|
||||
parts.emplace_back(str.substr(begin_pos));
|
||||
return parts;
|
||||
}
|
||||
|
||||
inline bool string_starts_with(std::string_view str, std::string_view prefix) {
|
||||
return str.size() >= prefix.size() && str.compare(0, prefix.size(), prefix) == 0;
|
||||
}
|
||||
|
||||
inline bool string_starts_with(std::string_view str, char prefix) {
|
||||
return !str.empty() && str.front() == prefix;
|
||||
}
|
||||
|
||||
inline bool string_ends_with(std::string_view str, std::string_view suffix) {
|
||||
return str.size() >= suffix.size() &&
|
||||
str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
|
||||
}
|
||||
|
||||
inline bool string_remove_suffix(std::string& str, std::string_view suffix) {
|
||||
if (string_ends_with(str, suffix)) {
|
||||
str.resize(str.size() - suffix.size());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* The longest suffix of `str` that is a prefix of `stop`, i.e. where a stop string might be
|
||||
* starting. The streaming detokeniser in core/text/tokenizer.cpp solves the same problem for
|
||||
* itself; this one is here because the lifted partial-parse code calls it. */
|
||||
inline size_t string_find_partial_stop(std::string_view str, std::string_view stop) {
|
||||
if (!str.empty() && !stop.empty()) {
|
||||
const size_t max_len = std::min(str.size(), stop.size());
|
||||
const char last_char = str.back();
|
||||
for (size_t len = max_len; len > 0; --len) {
|
||||
if (stop[len - 1] == last_char && string_ends_with(str, stop.substr(0, len)))
|
||||
return str.size() - len;
|
||||
}
|
||||
}
|
||||
return std::string::npos;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ types
|
||||
*
|
||||
* Three enums and a struct that live in common.h upstream and are named in the lifted headers'
|
||||
* public signatures. They are transcribed unchanged; a value renumbered here would silently mean
|
||||
* something else in a lifted .cpp. */
|
||||
|
||||
enum common_reasoning_format {
|
||||
COMMON_REASONING_FORMAT_NONE,
|
||||
COMMON_REASONING_FORMAT_AUTO, /* same as deepseek, using message.reasoning_content */
|
||||
COMMON_REASONING_FORMAT_DEEPSEEK_LEGACY, /* leave inline in <think> tags when streaming */
|
||||
COMMON_REASONING_FORMAT_DEEPSEEK, /* extract, including in streaming deltas */
|
||||
};
|
||||
|
||||
enum common_grammar_trigger_type {
|
||||
COMMON_GRAMMAR_TRIGGER_TYPE_TOKEN,
|
||||
COMMON_GRAMMAR_TRIGGER_TYPE_WORD,
|
||||
COMMON_GRAMMAR_TRIGGER_TYPE_PATTERN,
|
||||
COMMON_GRAMMAR_TRIGGER_TYPE_PATTERN_FULL,
|
||||
};
|
||||
|
||||
struct common_grammar_trigger {
|
||||
common_grammar_trigger_type type;
|
||||
std::string value;
|
||||
llama_token token = LLAMA_TOKEN_NULL;
|
||||
};
|
||||
|
||||
/* common.h's token renderer, on the same never-reached branch as the llama_model accessors
|
||||
* above. Radiance's own is rad::Tokenizer::piece(); this one exists so chat.cpp compiles. */
|
||||
std::string common_token_to_piece(const llama_vocab* vocab, llama_token token, bool special);
|
||||
Vendored
+28
@@ -0,0 +1,28 @@
|
||||
/* vendor/shim/ggml.h -- NOT a lift. This file is ours.
|
||||
*
|
||||
* The lifted chat and PEG-parser sources include "ggml.h" for exactly two macros. Taking ggml to
|
||||
* get them would take the thing spec §12 says we do not take, and editing the lifts to drop the
|
||||
* include would make them unpatchable. A shim is the third option: the lifted files compile
|
||||
* BYTE-FOR-BYTE unchanged, and everything they needed from outside is here where it is visible.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
|
||||
/* GGML_ABORT is reached only on an invariant the lifted code believes cannot break. It behaves
|
||||
* the way rad::fatal() does -- print and exit non-zero rather than continue from a broken state
|
||||
* (spec §17) -- and is spelt without including rad_internal.h so that vendor/ stays free of any
|
||||
* dependency on core/. */
|
||||
#define GGML_ABORT(...) \
|
||||
do { \
|
||||
fprintf(stderr, "fatal: %s:%d: ", __FILE__, __LINE__); \
|
||||
fprintf(stderr, __VA_ARGS__); \
|
||||
fputc('\n', stderr); \
|
||||
abort(); \
|
||||
} while (0)
|
||||
|
||||
#define GGML_ASSERT(x) \
|
||||
do { \
|
||||
if (!(x)) GGML_ABORT("assertion failed: %s", #x); \
|
||||
} while (0)
|
||||
Vendored
+44
@@ -0,0 +1,44 @@
|
||||
/* vendor/shim/llama.h -- NOT a lift. This file is ours.
|
||||
*
|
||||
* llama.cpp's common.h includes llama.h, and the lifted chat sources reach through it for
|
||||
* exactly one type and one sentinel. Taking llama.h would take the engine we are replacing, so
|
||||
* the two names are declared here. They agree with radiance's own token type by construction:
|
||||
* a token id is an int32_t everywhere in this project, and -1 is not a token.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
typedef int32_t llama_token;
|
||||
|
||||
#define LLAMA_TOKEN_NULL (-1)
|
||||
|
||||
/* ------------------------------------------------------------------ the llama_model surface
|
||||
*
|
||||
* chat.cpp's `common_chat_templates_init()` can take a llama_model and read the template, the
|
||||
* BOS/EOS spellings and the add_bos/add_eos flags off it. Radiance has no llama_model: the .rad
|
||||
* carries all four (RadVocabHeader.chat_template, bos, eos, add_bos, add_eos) and core/text/
|
||||
* chat.cpp passes them in as the overrides upstream already supports.
|
||||
*
|
||||
* So these are declared and defined (vendor/shim/llama_shim.cpp) purely so the lifted file
|
||||
* compiles unedited. Every one of them is on a branch guarded by `if (model)`, and radiance
|
||||
* always passes model == nullptr, so none of them is ever reached. The one exception is
|
||||
* llama_chat_apply_template(), which is the LEGACY non-jinja template route; it returns a
|
||||
* failure, because that route is a hardcoded table of known templates and taking it would
|
||||
* reintroduce exactly the "recognise the model" failure mode spec §12 removes. */
|
||||
struct llama_model;
|
||||
struct llama_vocab;
|
||||
|
||||
struct llama_chat_message {
|
||||
const char* role;
|
||||
const char* content;
|
||||
};
|
||||
|
||||
int32_t llama_chat_apply_template(const char* tmpl, const llama_chat_message* chat,
|
||||
size_t n_msg, bool add_ass, char* buf, int32_t length);
|
||||
const char* llama_model_chat_template(const llama_model* model, const char* name);
|
||||
const llama_vocab* llama_model_get_vocab(const llama_model* model);
|
||||
llama_token llama_vocab_bos(const llama_vocab* vocab);
|
||||
llama_token llama_vocab_eos(const llama_vocab* vocab);
|
||||
bool llama_vocab_get_add_bos(const llama_vocab* vocab);
|
||||
bool llama_vocab_get_add_eos(const llama_vocab* vocab);
|
||||
Vendored
+24
@@ -0,0 +1,24 @@
|
||||
/* vendor/shim/llama_shim.cpp -- NOT a lift. This file is ours.
|
||||
*
|
||||
* Definitions for the llama.h surface declared in vendor/shim/llama.h. See the comment there for
|
||||
* why they exist and why none of them can be reached with model == nullptr, which is the only
|
||||
* way radiance calls into the lifted chat code.
|
||||
*/
|
||||
#include "common.h"
|
||||
#include "llama.h"
|
||||
|
||||
int32_t llama_chat_apply_template(const char*, const llama_chat_message*, size_t, bool,
|
||||
char*, int32_t) {
|
||||
/* The legacy route, refused. A hardcoded table of recognised templates is the failure mode
|
||||
* this project replaced with a declared chain; falling into it silently would undo that. */
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char* llama_model_chat_template(const llama_model*, const char*) { return nullptr; }
|
||||
const llama_vocab* llama_model_get_vocab(const llama_model*) { return nullptr; }
|
||||
llama_token llama_vocab_bos(const llama_vocab*) { return LLAMA_TOKEN_NULL; }
|
||||
llama_token llama_vocab_eos(const llama_vocab*) { return LLAMA_TOKEN_NULL; }
|
||||
bool llama_vocab_get_add_bos(const llama_vocab*) { return false; }
|
||||
bool llama_vocab_get_add_eos(const llama_vocab*) { return false; }
|
||||
|
||||
std::string common_token_to_piece(const llama_vocab*, llama_token, bool) { return {}; }
|
||||
Vendored
+29
@@ -0,0 +1,29 @@
|
||||
/* vendor/shim/log.h -- NOT a lift. This file is ours.
|
||||
*
|
||||
* llama.cpp's log.h is a whole logging subsystem with a background thread and its own verbosity
|
||||
* plumbing. The lifted files use four macros from it. Routing those four at radiance's logger
|
||||
* would make vendor/ depend on core/, so they go to stderr behind one runtime switch, and
|
||||
* core/text/chat.cpp turns that switch on when rad's own level reaches Debug.
|
||||
*
|
||||
* The cost, named: a lifted file's log line does not carry rad's level, file or timestamp. These
|
||||
* lines exist for debugging a template the auto-parser could not crack, which is a developer
|
||||
* activity and not a production one.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "ggml.h" /* upstream's log.h includes ggml.h for ggml_log_level, and lifted files rely
|
||||
* on that transitively -- json-partial.cpp uses GGML_ASSERT without
|
||||
* including ggml.h itself. Carrying the same transitive include keeps
|
||||
* those files unedited. */
|
||||
#include <cstdio>
|
||||
|
||||
namespace rad_vendor_log {
|
||||
/* Off by default. Anything below Warn from a vendored file is noise during normal operation. */
|
||||
inline bool& verbose() { static bool v = false; return v; }
|
||||
}
|
||||
|
||||
#define LOG_ERR(...) fprintf(stderr, __VA_ARGS__)
|
||||
#define LOG_WRN(...) fprintf(stderr, __VA_ARGS__)
|
||||
#define LOG_INF(...) do { if (::rad_vendor_log::verbose()) fprintf(stderr, __VA_ARGS__); } while (0)
|
||||
#define LOG_DBG(...) do { if (::rad_vendor_log::verbose()) fprintf(stderr, __VA_ARGS__); } while (0)
|
||||
#define LOG_CNT(...) do { if (::rad_vendor_log::verbose()) fprintf(stderr, __VA_ARGS__); } while (0)
|
||||
Reference in new issue
Block a user