Skip to content

Commit

Permalink
Upgrade to cosmocc 3.2.4
Browse files Browse the repository at this point in the history
We're now able to use the C++ exception code in the upstream llama.cpp,
json.h, and httplib.h projects. This is important since it means things
like invalid JSON requests won't cause the entire server to crash. This
change is also going to make merging from upstream easier going forward

Fixes Mozilla-Ocho#116
  • Loading branch information
jart committed Jan 9, 2024
1 parent 7a5ec37 commit 3384234
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 170 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- conf -*-

/o
/cosmocc
/.cosmocc
/TAGS
/HTAGS
/cosmocc
Expand Down
8 changes: 4 additions & 4 deletions build/config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#── vi: set noet ft=make ts=8 sw=8 fenc=utf-8 :vi ────────────────────┘

PREFIX = /usr/local
COSMOCC = cosmocc/3.2.3
COSMOCC = .cosmocc/3.2.4
TOOLCHAIN = $(COSMOCC)/bin/cosmo

AR = $(TOOLCHAIN)ar
Expand All @@ -13,7 +13,7 @@ MKDEPS = $(COSMOCC)/bin/mkdeps
INSTALL = install

ARFLAGS = rcsD
CCFLAGS = -g -O3
CCFLAGS = -g -O3 -fexceptions
CPPFLAGS_ = -iquote. -mcosmo
TARGET_ARCH = -Xx86_64-mssse3

Expand Down Expand Up @@ -50,5 +50,5 @@ clean:; rm -rf o
.PHONY: distclean
distclean:; rm -rf o cosmocc

cosmocc/3.2.3:
build/download-cosmocc.sh $@ 3.2.3 6b2bffb9bc4ebff41a2e4657aab6b8e725729fb2fbd128b06196b2b28aedb322
.cosmocc/3.2.4:
build/download-cosmocc.sh $@ 3.2.4 d2fa6dbf6f987310494581deff5b915dbdc5ca701f20f7613bb0dcf1de2ee511
6 changes: 2 additions & 4 deletions llama.cpp/base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ For more information, please refer to <http://unlicense.org>
#include <stdexcept>
#include <string>

#include "runtime.h"

class base64_error : public std::runtime_error
{
public:
Expand Down Expand Up @@ -237,7 +235,7 @@ class base64
++in_begin;

if (c != '=') {
ThrowRuntimeError("invalid base64 character.");
throw std::runtime_error("invalid base64 character.");
}
}
}
Expand Down Expand Up @@ -387,7 +385,7 @@ class base64
}
}

ThrowRuntimeError("invalid base64 character.");
throw std::runtime_error("invalid base64 character.");
}
};

Expand Down
27 changes: 4 additions & 23 deletions llama.cpp/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "llama.h"
#include "ggml-cuda.h"
#include "ggml-metal.h"
#include "runtime.h"

#include <algorithm>
#include <cassert>
Expand Down Expand Up @@ -124,21 +123,17 @@ void process_escapes(std::string& input) {

bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
bool result = true;
#ifndef _LIBCPP_NO_EXCEPTIONS
try {
#endif
if (!gpt_params_parse_ex(argc, argv, params)) {
gpt_print_usage(argc, argv, gpt_params());
exit(0);
}
#ifndef _LIBCPP_NO_EXCEPTIONS
}
catch (const std::invalid_argument & ex) {
fprintf(stderr, "%s\n", ex.what());
gpt_print_usage(argc, argv, gpt_params());
exit(1);
}
#endif
if (FLAG_gpu == LLAMAFILE_GPU_DISABLE) {
params.n_gpu_layers = 0;
}
Expand Down Expand Up @@ -669,7 +664,6 @@ bool gpt_params_parse_ex(int argc, char ** argv, gpt_params & params) {
llama_token key;
char sign = 0;
std::string value_str;
#ifndef _LIBCPP_NO_EXCEPTIONS
try {
if (ss >> key && ss >> sign && std::getline(ss, value_str) && (sign == '+' || sign == '-')) {
sparams.logit_bias[key] = std::stof(value_str) * ((sign == '-') ? -1.0f : 1.0f);
Expand All @@ -680,19 +674,6 @@ bool gpt_params_parse_ex(int argc, char ** argv, gpt_params & params) {
invalid_param = true;
break;
}
#else
if (ss >> key && ss >> sign && std::getline(ss, value_str) && (sign == '+' || sign == '-')) {
errno = 0;
sparams.logit_bias[key] = std::stof(value_str) * ((sign == '-') ? -1.0f : 1.0f);
if (errno) {
invalid_param = true;
break;
}
} else {
invalid_param = true;
break;
}
#endif
} else if (arg == "-h" || arg == "--help") {
return false;

Expand Down Expand Up @@ -795,17 +776,17 @@ bool gpt_params_parse_ex(int argc, char ** argv, gpt_params & params) {
// End of Parse args for logging parameters
#endif // LOG_DISABLE_LOGS
} else {
ThrowInvalidArgument("error: unknown argument: " + arg);
throw std::invalid_argument("error: unknown argument: " + arg);
}
}
if (invalid_param) {
ThrowInvalidArgument("error: invalid parameter for argument: " + arg);
throw std::invalid_argument("error: invalid parameter for argument: " + arg);
}
if (params.prompt_cache_all &&
(params.interactive || params.interactive_first ||
params.instruct)) {

ThrowInvalidArgument("error: --prompt-cache-all not supported in interactive mode yet\n");
throw std::invalid_argument("error: --prompt-cache-all not supported in interactive mode yet\n");
}

if (params.escape) {
Expand Down Expand Up @@ -1081,7 +1062,7 @@ static ggml_type kv_cache_type_from_str(const std::string & s) {
return GGML_TYPE_Q5_1;
}

ThrowRuntimeError("Invalid cache type: " + s);
throw std::runtime_error("Invalid cache type: " + s);
}

struct llama_context_params llama_context_params_from_gpt_params(const gpt_params & params) {
Expand Down
33 changes: 12 additions & 21 deletions llama.cpp/grammar-parser.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "grammar-parser.h"
#include "runtime.h"
#include <cstdint>
#include <cwchar>
#include <string>
Expand Down Expand Up @@ -69,7 +68,7 @@ namespace grammar_parser {
}
}
if (pos != end) {
ThrowRuntimeError("expecting " + std::to_string(size) + " hex chars at " + src);
throw std::runtime_error("expecting " + std::to_string(size) + " hex chars at " + src);
}
return std::make_pair(value, pos);
}
Expand All @@ -95,7 +94,7 @@ namespace grammar_parser {
pos++;
}
if (pos == src) {
ThrowRuntimeError(std::string("expecting name at ") + src);
throw std::runtime_error(std::string("expecting name at ") + src);
}
return pos;
}
Expand All @@ -115,12 +114,12 @@ namespace grammar_parser {
case ']':
return std::make_pair(src[1], src + 2);
default:
ThrowRuntimeError(std::string("unknown escape at ") + src);
throw std::runtime_error(std::string("unknown escape at ") + src);
}
} else if (*src) {
return decode_utf8(src);
}
ThrowRuntimeError("unexpected end of input");
throw std::runtime_error("unexpected end of input");
}

const char * parse_alternates(
Expand Down Expand Up @@ -186,12 +185,12 @@ namespace grammar_parser {
// output reference to synthesized rule
out_elements.push_back({LLAMA_GRETYPE_RULE_REF, sub_rule_id});
if (*pos != ')') {
ThrowRuntimeError(std::string("expecting ')' at ") + pos);
throw std::runtime_error(std::string("expecting ')' at ") + pos);
}
pos = parse_space(pos + 1, is_nested);
} else if (*pos == '*' || *pos == '+' || *pos == '?') { // repetition operator
if (last_sym_start == out_elements.size()) {
ThrowRuntimeError(std::string("expecting preceding item to */+/? at ") + pos);
throw std::runtime_error(std::string("expecting preceding item to */+/? at ") + pos);
}

// apply transformation to previous symbol (last_sym_start to end) according to
Expand Down Expand Up @@ -256,7 +255,7 @@ namespace grammar_parser {
const std::string name(src, name_len);

if (!(pos[0] == ':' && pos[1] == ':' && pos[2] == '=')) {
ThrowRuntimeError(std::string("expecting ::= at ") + pos);
throw std::runtime_error(std::string("expecting ::= at ") + pos);
}
pos = parse_space(pos + 3, true);

Expand All @@ -267,27 +266,23 @@ namespace grammar_parser {
} else if (*pos == '\n') {
pos++;
} else if (*pos) {
ThrowRuntimeError(std::string("expecting newline or end at ") + pos);
throw std::runtime_error(std::string("expecting newline or end at ") + pos);
}
return parse_space(pos, true);
}

parse_state parse(const char * src) {
#ifndef _LIBCPP_NO_EXCEPTIONS
try {
#endif
parse_state state;
const char * pos = parse_space(src, true);
while (*pos) {
pos = parse_rule(state, pos);
}
return state;
#ifndef _LIBCPP_NO_EXCEPTIONS
} catch (const std::exception & err) {
fprintf(stderr, "%s: error parsing grammar: %s\n", __func__, err.what());
return parse_state();
}
#endif
}

static void print_grammar_char(FILE * file, uint32_t c) {
Expand Down Expand Up @@ -345,15 +340,15 @@ namespace grammar_parser {
const std::vector<llama_grammar_element> & rule,
const std::map<uint32_t, std::string> & symbol_id_names) {
if (rule.empty() || rule.back().type != LLAMA_GRETYPE_END) {
ThrowRuntimeError(
throw std::runtime_error(
"malformed rule, does not end with LLAMA_GRETYPE_END: " + std::to_string(rule_id));
}
fprintf(file, "%s ::= ", symbol_id_names.at(rule_id).c_str());
for (size_t i = 0, end = rule.size() - 1; i < end; i++) {
llama_grammar_element elem = rule[i];
switch (elem.type) {
case LLAMA_GRETYPE_END:
ThrowRuntimeError(
throw std::runtime_error(
"unexpected end of rule: " + std::to_string(rule_id) + "," +
std::to_string(i));
case LLAMA_GRETYPE_ALT:
Expand All @@ -372,7 +367,7 @@ namespace grammar_parser {
break;
case LLAMA_GRETYPE_CHAR_RNG_UPPER:
if (i == 0 || !is_char_element(rule[i - 1])) {
ThrowRuntimeError(
throw std::runtime_error(
"LLAMA_GRETYPE_CHAR_RNG_UPPER without preceding char: " +
std::to_string(rule_id) + "," + std::to_string(i));
}
Expand All @@ -381,7 +376,7 @@ namespace grammar_parser {
break;
case LLAMA_GRETYPE_CHAR_ALT:
if (i == 0 || !is_char_element(rule[i - 1])) {
ThrowRuntimeError(
throw std::runtime_error(
"LLAMA_GRETYPE_CHAR_ALT without preceding char: " +
std::to_string(rule_id) + "," + std::to_string(i));
}
Expand All @@ -402,9 +397,7 @@ namespace grammar_parser {
}

void print_grammar(FILE * file, const parse_state & state) {
#ifndef _LIBCPP_NO_EXCEPTIONS
try {
#endif
std::map<uint32_t, std::string> symbol_id_names;
for (const auto & kv : state.symbol_ids) {
symbol_id_names[kv.second] = kv.first;
Expand All @@ -415,11 +408,9 @@ namespace grammar_parser {
print_rule(file, uint32_t(i), state.rules[i], symbol_id_names);
// fprintf(file, "\n");
}
#ifndef _LIBCPP_NO_EXCEPTIONS
} catch (const std::exception & err) {
fprintf(stderr, "\n%s: error printing grammar: %s\n", __func__, err.what());
}
#endif
}

std::vector<const llama_grammar_element *> parse_state::c_rules() {
Expand Down
Loading

0 comments on commit 3384234

Please sign in to comment.