Skip to content

Commit

Permalink
fix editorconfig lints
Browse files Browse the repository at this point in the history
  • Loading branch information
ochafik committed Sep 26, 2024
1 parent ab25e3f commit 1b62801
Show file tree
Hide file tree
Showing 17 changed files with 114 additions and 106 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,11 @@ indent_style = tab
[examples/cvector-generator/*.txt]
trim_trailing_whitespace = unset
insert_final_newline = unset

[{tests/chat/templates/*.jinja,tests/chat/goldens/*.txt}]
indent_style = unset
indent_size = unset
end_of_line = unset
charset = unset
trim_trailing_whitespace = unset
insert_final_newline = unset
2 changes: 1 addition & 1 deletion common/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1516,7 +1516,7 @@ bool llama_chat_verify_template(const std::string & tmpl, bool use_jinja) {
nullptr,
tmpl.c_str(),
chat,
1,
1,
/* add_ass= */ true,
/* buffer= */ nullptr,
/* length= */ 0,
Expand Down
10 changes: 5 additions & 5 deletions common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ class llama_antiprompts {
f = f->fail;
}

child.fail = (f == &root && f->children.find(c) == f->children.end())
child.fail = (f == &root && f->children.find(c) == f->children.end())
? &root : &f->children[c];

if (child.fail->output != -1) {
Expand Down Expand Up @@ -654,7 +654,7 @@ class llama_antiprompts {
},
stop_words,
grammar_trigger_words
);
);
}

void build(const std::function<std::vector<llama_token>(const std::string)> & tokenizer, const std::vector<std::string> & stop_words, const std::vector<std::string> & grammar_trigger_words) {
Expand Down Expand Up @@ -708,7 +708,7 @@ class llama_antiprompts {
MatchResult findFirstMatch(const std::string& text, size_t offset = 0) {
TrieNode* current = &root;
MatchResult partialMatch{std::string::npos, "", true, 0, false};

for (size_t i = offset; i < text.length(); ++i) {
char c = text[i];
while (current != &root && current->children.find(c) == current->children.end()) {
Expand Down Expand Up @@ -736,12 +736,12 @@ class llama_antiprompts {
partialMatch.is_grammar_trigger = false;
}
}

// If we've found a partial match and haven't returned a full match, return the partial match
if (partialMatch.pos != std::string::npos) {
return partialMatch;
}

return {std::string::npos, "", false, 0, false};
}
};
Expand Down
76 changes: 38 additions & 38 deletions common/minja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Value : public std::enable_shared_from_this<Value> {
}
return Value();
}

bool empty() {
return args.empty() && kwargs.empty();
}
Expand All @@ -61,7 +61,7 @@ class Value : public std::enable_shared_from_this<Value> {
}
}
};

using CallableType = std::function<Value(const std::shared_ptr<Context> &, Arguments &)>;
using FilterType = std::function<Value(const std::shared_ptr<Context> &, Arguments &)>;

Expand Down Expand Up @@ -143,7 +143,7 @@ class Value : public std::enable_shared_from_this<Value> {
} else if (is_boolean()) {
out << (this->to_bool() ? "True" : "False");
} else if (is_string()) {
dump_string(primitive_, out, string_quote);
dump_string(primitive_, out, string_quote);
} else {
out << primitive_.dump();
}
Expand Down Expand Up @@ -175,7 +175,7 @@ class Value : public std::enable_shared_from_this<Value> {
primitive_ = v;
}
}

std::vector<Value> keys() {
if (!object_) throw std::runtime_error("Value is not an object: " + dump());
std::vector<Value> res;
Expand Down Expand Up @@ -267,7 +267,7 @@ class Value : public std::enable_shared_from_this<Value> {
if (is_string()) return !get<std::string>().empty();
if (is_array()) return !empty();
return true;
}
}

bool operator<(const Value & other) const {
if (is_null())
Expand Down Expand Up @@ -369,7 +369,7 @@ class Value : public std::enable_shared_from_this<Value> {
if (!contains(key)) return default_value;
return at(key).get<T>();
}

template <typename T>
T get() const {
if (is_primitive()) return primitive_.get<T>();
Expand Down Expand Up @@ -730,7 +730,7 @@ class TemplateNode {
Location location_;
protected:
virtual void do_render(std::ostringstream & out, const std::shared_ptr<Context> & context) const = 0;

public:
TemplateNode(const Location & location) : location_(location) {}
void render(std::ostringstream & out, const std::shared_ptr<Context> & context) const {
Expand Down Expand Up @@ -817,7 +817,7 @@ class ForNode : public TemplateNode {
ForNode(const Location & location, std::vector<std::string> && var_names, std::unique_ptr<Expression> && iterable,
std::unique_ptr<Expression> && condition, std::unique_ptr<TemplateNode> && body, bool recursive, std::unique_ptr<TemplateNode> && else_body)
: TemplateNode(location), var_names(var_names), iterable(std::move(iterable)), condition(std::move(condition)), body(std::move(body)), recursive(recursive), else_body(std::move(else_body)) {}

void do_render(std::ostringstream & out, const std::shared_ptr<Context> & context) const override {
// https://jinja.palletsprojects.com/en/3.0.x/templates/#for

Expand Down Expand Up @@ -920,7 +920,7 @@ class MacroNode : public TemplateNode {
auto & arg_name = arg.first;
auto it = named_param_positions.find(arg_name);
if (it == named_param_positions.end()) throw std::runtime_error("Unknown parameter name for macro " + name->get_name() + ": " + arg_name);

call_context->set(arg_name, arg.second);
param_set[it->second] = true;
}
Expand Down Expand Up @@ -1098,7 +1098,7 @@ class BinaryOpExpr : public Expression {
: Expression(location), left(std::move(l)), right(std::move(r)), op(o) {}
Value do_evaluate(const std::shared_ptr<Context> & context) const override {
auto l = left->evaluate(context);

auto do_eval = [&](const Value & l) -> Value {
if (op == Op::Is || op == Op::IsNot) {
auto t = dynamic_cast<VariableExpr*>(right.get());
Expand Down Expand Up @@ -1297,7 +1297,7 @@ class Parser {
std::shared_ptr<std::string> template_str;
CharIterator start, end, it;
Options options;

Parser(const std::shared_ptr<std::string>& template_str, const Options & options) : template_str(template_str), options(options) {
if (!template_str) throw std::runtime_error("Template string is null");
start = it = this->template_str->begin();
Expand Down Expand Up @@ -1326,7 +1326,7 @@ class Parser {
case 'b': result += '\b'; break;
case 'f': result += '\f'; break;
case '\\': result += '\\'; break;
default:
default:
if (*it == quote) {
result += quote;
} else {
Expand Down Expand Up @@ -1562,7 +1562,7 @@ class Parser {
if (!identifier) throw std::runtime_error("Expected identifier after 'is' keyword");

return nonstd_make_unique<BinaryOpExpr>(
left->location,
left->location,
std::move(left), std::move(identifier),
negated ? BinaryOpExpr::Op::IsNot : BinaryOpExpr::Op::Is);
}
Expand All @@ -1588,7 +1588,7 @@ class Parser {
if (consumeToken("(").empty()) throw std::runtime_error("Expected opening parenthesis in param list");

Expression::Parameters result;

while (it != end) {
if (!consumeToken(")").empty()) {
return result;
Expand Down Expand Up @@ -1622,7 +1622,7 @@ class Parser {
if (consumeToken("(").empty()) throw std::runtime_error("Expected opening parenthesis in call args");

Expression::Arguments result;

while (it != end) {
if (!consumeToken(")").empty()) {
return result;
Expand Down Expand Up @@ -1655,7 +1655,7 @@ class Parser {
static std::regex ident_regex(R"((?!not|is|and|or|del)[a-zA-Z_]\w*)");
auto location = get_location();
auto ident = consumeToken(ident_regex);
if (ident.empty())
if (ident.empty())
return nullptr;
return nonstd_make_unique<VariableExpr>(location, ident);
}
Expand Down Expand Up @@ -1699,7 +1699,7 @@ class Parser {
}
return left;
}

std::unique_ptr<Expression> parseMathMulDiv() {
auto left = parseMathUnaryPlusMinus();
if (!left) throw std::runtime_error("Expected left side of 'math mul/div' expression");
Expand All @@ -1709,9 +1709,9 @@ class Parser {
while (!(op_str = consumeToken(mul_div_tok)).empty()) {
auto right = parseMathUnaryPlusMinus();
if (!right) throw std::runtime_error("Expected right side of 'math mul/div' expression");
auto op = op_str == "*" ? BinaryOpExpr::Op::Mul
: op_str == "**" ? BinaryOpExpr::Op::MulMul
: op_str == "/" ? BinaryOpExpr::Op::Div
auto op = op_str == "*" ? BinaryOpExpr::Op::Mul
: op_str == "**" ? BinaryOpExpr::Op::MulMul
: op_str == "/" ? BinaryOpExpr::Op::Div
: op_str == "//" ? BinaryOpExpr::Op::DivDiv
: BinaryOpExpr::Op::Mod;
left = nonstd_make_unique<BinaryOpExpr>(get_location(), std::move(left), std::move(right), op);
Expand Down Expand Up @@ -1741,14 +1741,14 @@ class Parser {
auto op_str = consumeToken(unary_plus_minus_tok);
auto expr = parseValueExpression();
if (!expr) throw std::runtime_error("Expected expr of 'unary plus/minus' expression");

if (!op_str.empty()) {
auto op = op_str == "+" ? UnaryOpExpr::Op::Plus : UnaryOpExpr::Op::Minus;
return nonstd_make_unique<UnaryOpExpr>(get_location(), std::move(expr), op);
}
return expr;
}

std::unique_ptr<Expression> parseValueExpression() {
auto parseValue = [&]() -> std::unique_ptr<Expression> {
auto location = get_location();
Expand All @@ -1774,7 +1774,7 @@ class Parser {
};

auto value = parseValue();

while (it != end && consumeSpaces() && peekSymbols({ "[", "." })) {
if (!consumeToken("[").empty()) {
std::unique_ptr<Expression> index;
Expand All @@ -1797,7 +1797,7 @@ class Parser {
}
if (!index) throw std::runtime_error("Empty index in subscript");
if (consumeToken("]").empty()) throw std::runtime_error("Expected closing bracket in subscript");

value = nonstd_make_unique<SubscriptExpr>(value->location, std::move(value), std::move(index));
} else if (!consumeToken(".").empty()) {
auto identifier = parseIdentifier();
Expand Down Expand Up @@ -1825,10 +1825,10 @@ class Parser {

std::unique_ptr<Expression> parseBracedExpressionOrArray() {
if (consumeToken("(").empty()) return nullptr;

auto expr = parseExpression();
if (!expr) throw std::runtime_error("Expected expression in braced expression");

if (!consumeToken(")").empty()) {
return expr; // Drop the parentheses
}
Expand All @@ -1851,7 +1851,7 @@ class Parser {

std::unique_ptr<Expression> parseArray() {
if (consumeToken("[").empty()) return nullptr;

std::vector<std::unique_ptr<Expression>> elements;
if (!consumeToken("]").empty()) {
return nonstd_make_unique<ArrayExpr>(get_location(), std::move(elements));
Expand All @@ -1876,7 +1876,7 @@ class Parser {

std::unique_ptr<Expression> parseDictionary() {
if (consumeToken("{").empty()) return nullptr;

std::vector<std::pair<std::unique_ptr<Expression>, std::unique_ptr<Expression>>> elements;
if (!consumeToken("}").empty()) {
return nonstd_make_unique<DictExpr>(get_location(), std::move(elements));
Expand All @@ -1892,7 +1892,7 @@ class Parser {
};

parseKeyValuePair();

while (it != end) {
if (!consumeToken(",").empty()) {
parseKeyValuePair();
Expand Down Expand Up @@ -1950,15 +1950,15 @@ class Parser {
static std::regex text_regex(R"([\s\S\n]*?($|(?=\{\{|\{%|\{#)))");
static std::regex expr_close_regex(R"([\s\n]*([-~])?\}\})");
static std::regex block_close_regex(R"([\s\n]*([-~])?%\})");

TemplateTokenVector tokens;
std::vector<std::string> group;
std::string text;

try {
while (it != end) {
auto location = get_location();

if (!(group = consumeTokenGroups(comment_tok, SpaceHandling::Keep)).empty()) {
auto pre_space = parsePreSpace(group[1]);
auto content = group[2];
Expand All @@ -1985,7 +1985,7 @@ class Parser {
};

if ((keyword = consumeToken(block_keyword_tok)).empty()) throw std::runtime_error("Expected block keyword");

if (keyword == "if") {
auto condition = parseExpression();
if (!condition) throw std::runtime_error("Expected condition in if block");
Expand Down Expand Up @@ -2019,7 +2019,7 @@ class Parser {
condition = parseExpression();
}
auto recursive = !consumeToken(recursive_tok).empty();

auto post_space = parseBlockClose();
tokens.push_back(nonstd_make_unique<ForTemplateToken>(location, pre_space, post_space, std::move(varnames), std::move(iterable), std::move(condition), recursive));
} else if (keyword == "endfor") {
Expand All @@ -2034,7 +2034,7 @@ class Parser {
if (!(group = consumeTokenGroups(namespaced_var_regex)).empty()) {
ns = group[1];
var_names.push_back(group[2]);

if (consumeToken("=").empty()) throw std::runtime_error("Expected equals sign in set block");

value = parseExpression();
Expand Down Expand Up @@ -2115,7 +2115,7 @@ class Parser {
} else if (auto text_token = dynamic_cast<TextTemplateToken*>(token.get())) {
SpaceHandling pre_space = (it - 1) != begin ? (*(it - 2))->post_space : SpaceHandling::Keep;
SpaceHandling post_space = it != end ? (*it)->pre_space : SpaceHandling::Keep;

auto text = text_token->text;
if (pre_space == SpaceHandling::Strip) {
static std::regex leading_space_regex(R"(^(\s|\r|\n)+)");
Expand All @@ -2131,7 +2131,7 @@ class Parser {
static std::regex trailing_last_line_space_regex(R"((^|\n)[ \t]*$)");
text = std::regex_replace(text, trailing_last_line_space_regex, "$1");
}

if (it == end && !options.keep_trailing_newline) {
static std::regex r(R"([\n\r]$)");
text = std::regex_replace(text, r, ""); // Strip one trailing newline
Expand Down Expand Up @@ -2473,7 +2473,7 @@ inline std::shared_ptr<Context> Context::builtins() {
int64_t start = param_set[0] ? startEndStep[0] : 0;
int64_t end = startEndStep[1];
int64_t step = param_set[2] ? startEndStep[2] : 1;

auto res = Value::array();
if (step > 0) {
for (int64_t i = start; i < end; i += step) {
Expand Down
2 changes: 1 addition & 1 deletion common/sampling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ bool gpt_sampler_trigger_grammar(const struct llama_model * model, gpt_sampler *
llama_sampler_accept_str(gsmpl->grmr, trigger.c_str());
return true;
}

struct gpt_sampler * gpt_sampler_init(const struct llama_model * model, const struct gpt_sampler_params & params) {
llama_sampler_chain_params lparams = llama_sampler_chain_default_params();

Expand Down
Loading

0 comments on commit 1b62801

Please sign in to comment.