diff --git a/common/chat.cpp b/common/chat.cpp
index e104cc482..cb2c0fc35 100644
--- a/common/chat.cpp
+++ b/common/chat.cpp
@@ -2071,7 +2071,7 @@ static common_chat_params common_chat_params_init_gpt_oss(const common_chat_temp
// Trigger on tool calls that appear in the commentary channel
data.grammar_triggers.push_back({
COMMON_GRAMMAR_TRIGGER_TYPE_PATTERN,
- "<\\|channel\\|>(commentary|analysis) to"
+ "<\\|channel\\|>(?:commentary|analysis) to"
});
// Trigger tool calls that appear in the role section, either at the
@@ -2404,17 +2404,17 @@ static common_chat_params common_chat_params_init_hermes_2_pro(const common_chat
(inputs.parallel_tool_calls ? "(" + tool_call + ")+" : tool_call));
// Trigger on some common known "good bad" outputs (only from the start and with a json that's about a specific argument name to avoid false positives)
data.grammar_triggers.push_back({
- COMMON_GRAMMAR_TRIGGER_TYPE_PATTERN_FULL,
+ COMMON_GRAMMAR_TRIGGER_TYPE_PATTERN,
// If thinking_forced_open, then we capture the tag in the grammar,
// (important for required tool choice) and in the trigger's first capture (decides what is sent to the grammar)
- std::string(data.thinking_forced_open ? "[\\s\\S]*?(\\s*)" : "(?:[\\s\\S]*?\\s*)?") + (
+ std::string(data.thinking_forced_open ? "(\\s*)" : "") + (
"\\s*("
"(?:"
"||||)?"
"\\s*\\{\\s*\"name\"\\s*:\\s*\"(?:" + string_join(escaped_names, "|") + ")\""
")"
- ")[\\s\\S]*"
+ ")"
),
});
data.preserved_tokens = {
diff --git a/common/regex-partial.cpp b/common/regex-partial.cpp
index 4bff6b663..e667a209e 100644
--- a/common/regex-partial.cpp
+++ b/common/regex-partial.cpp
@@ -27,7 +27,7 @@ common_regex_match common_regex::search(const std::string & input, size_t pos, b
return res;
}
std::match_results srmatch;
- if (std::regex_match(input.rbegin(), input.rend() - pos, srmatch, rx_reversed_partial)) {
+ if (std::regex_search(input.rbegin(), input.rend() - pos, srmatch, rx_reversed_partial, std::regex_constants::match_continuous)) {
auto group = srmatch[1].str();
if (group.length() != 0) {
auto it = srmatch[1].second.base();
@@ -55,18 +55,18 @@ common_regex_match common_regex::search(const std::string & input, size_t pos, b
to see if a string ends with a partial regex match, but but it's not in std::regex yet.
Instead, we'll the regex into a partial match regex operating as a full match on the reverse iterators of the input.
- - /abcd/ -> (dcba|cba|ba|a).* -> ((?:(?:(?:(?:d)?c)?b)?a).*
- - /a|b/ -> (a|b).*
+ - /abcd/ -> ^(dcba|cba|ba|a) -> ^((?:(?:(?:(?:d)?c)?b)?a)
+ - /a|b/ -> ^(a|b)
- /a*?/ -> error, could match ""
- - /a*b/ -> ((?:b)?a*+).* (final repetitions become eager)
- - /.*?ab/ -> ((?:b)?a).* (merge .*)
- - /a.*?b/ -> ((?:b)?.*?a).* (keep reluctant matches)
- - /a(bc)d/ -> ((?:(?:d)?(?:(?:c)?b))?a).*
- - /a(bc|de)/ -> ((?:(?:(?:e)?d)?|(?:(?:c)?b)?)?a).*
- - /ab{2,4}c/ -> abbb?b?c -> ((?:(?:(?:(?:(?:c)?b)?b)?b?)?b?)?a).*
+ - /a*b/ -> ^((?:b)?a*+) (final repetitions become eager)
+ - /.*?ab/ -> ^((?:b)?a) (omit .*)
+ - /a.*?b/ -> ^((?:b)?.*?a) (keep reluctant matches)
+ - /a(bc)d/ -> ^((?:(?:d)?(?:(?:c)?b))?a)
+ - /a(bc|de)/ -> ^((?:(?:(?:e)?d)?|(?:(?:c)?b)?)?a)
+ - /ab{2,4}c/ -> ^cbbb?b?a -> ^((?:(?:(?:(?:(?:c)?b)?b)?b?)?b?)?a)
- The regex will match a reversed string fully, and the end of the first (And only) capturing group will indicate the reversed start of the original partial pattern
- (i.e. just where the final .* starts in the inverted pattern; all other groups are turned into non-capturing groups, and reluctant quantifiers are ignored)
+ The regex will match a reversed string fully, and the end of the first (And only) capturing group will indicate the reversed start of the original partial pattern.
+ All other groups are turned into non-capturing groups, and reluctant quantifiers are ignored.
*/
std::string regex_to_reversed_partial_regex(const std::string & pattern) {
auto it = pattern.begin();
@@ -177,7 +177,7 @@ std::string regex_to_reversed_partial_regex(const std::string & pattern) {
}
}
- // /abcd/ -> (dcba|cba|ba|a).* -> ((?:(?:(?:d)?c)?b)?a).*
+ // /abcd/ -> ^(dcba|cba|ba|a) -> ^((?:(?:(?:d)?c)?b)?a)
// if n(=4) parts, opening n-1(=3) non-capturing groups after the 1 capturing group
// We'll do the outermost capturing group and final .* in the enclosing function.
std::vector res_alts;
@@ -200,5 +200,5 @@ std::string regex_to_reversed_partial_regex(const std::string & pattern) {
throw std::runtime_error("Unmatched '(' in pattern");
}
- return "(" + res + ")[\\s\\S]*";
+ return "^(" + res + ")";
}
diff --git a/common/sampling.cpp b/common/sampling.cpp
index c66f935c6..68e36e874 100644
--- a/common/sampling.cpp
+++ b/common/sampling.cpp
@@ -179,24 +179,30 @@ struct common_sampler * common_sampler_init(const struct llama_model * model, co
#endif // LLAMA_USE_LLGUIDANCE
} else {
std::vector trigger_patterns;
- std::vector patterns_anywhere;
std::vector trigger_tokens;
for (const auto & trigger : params.grammar_triggers) {
switch (trigger.type) {
case COMMON_GRAMMAR_TRIGGER_TYPE_WORD:
{
const auto & word = trigger.value;
- patterns_anywhere.push_back(regex_escape(word));
+ trigger_patterns.push_back(regex_escape(word));
break;
}
case COMMON_GRAMMAR_TRIGGER_TYPE_PATTERN:
{
- patterns_anywhere.push_back(trigger.value);
+ trigger_patterns.push_back(trigger.value);
break;
}
case COMMON_GRAMMAR_TRIGGER_TYPE_PATTERN_FULL:
{
- trigger_patterns.push_back(trigger.value);
+ const auto & pattern = trigger.value;
+ std::string anchored = "^$";
+ if (!pattern.empty()) {
+ anchored = (pattern.front() != '^' ? "^" : "")
+ + pattern
+ + (pattern.back() != '$' ? "$" : "");
+ }
+ trigger_patterns.push_back(anchored);
break;
}
case COMMON_GRAMMAR_TRIGGER_TYPE_TOKEN:
@@ -210,10 +216,6 @@ struct common_sampler * common_sampler_init(const struct llama_model * model, co
}
}
- if (!patterns_anywhere.empty()) {
- trigger_patterns.push_back("^[\\s\\S]*?(" + string_join(patterns_anywhere, "|") + ")[\\s\\S]*");
- }
-
std::vector trigger_patterns_c;
trigger_patterns_c.reserve(trigger_patterns.size());
for (const auto & regex : trigger_patterns) {
diff --git a/ggml/src/ggml-cuda/argsort.cu b/ggml/src/ggml-cuda/argsort.cu
index da9652c3b..99669200f 100644
--- a/ggml/src/ggml-cuda/argsort.cu
+++ b/ggml/src/ggml-cuda/argsort.cu
@@ -29,8 +29,8 @@ static void argsort_f32_i32_cuda_cub(ggml_cuda_pool & pool,
const int nrows,
ggml_sort_order order,
cudaStream_t stream) {
- ggml_cuda_pool_alloc temp_indices_alloc(pool, ncols * nrows);
- ggml_cuda_pool_alloc temp_keys_alloc(pool, ncols * nrows);
+ ggml_cuda_pool_alloc temp_indices_alloc(pool, ((size_t) ncols) * nrows);
+ ggml_cuda_pool_alloc temp_keys_alloc(pool, ((size_t) ncols) * nrows);
ggml_cuda_pool_alloc offsets_alloc(pool, nrows + 1);
int * temp_indices = temp_indices_alloc.get();
diff --git a/ggml/src/ggml-cuda/common.cuh b/ggml/src/ggml-cuda/common.cuh
index a6e21092a..ba1c14569 100644
--- a/ggml/src/ggml-cuda/common.cuh
+++ b/ggml/src/ggml-cuda/common.cuh
@@ -1070,6 +1070,7 @@ struct ggml_cuda_graph {
bool disable_due_to_too_many_updates = false;
bool disable_due_to_failed_graph_capture = false;
int number_consecutive_updates = 0;
+ bool cuda_graphs_enabled = false;
std::vector ggml_graph_properties;
#endif
};
diff --git a/ggml/src/ggml-cuda/fattn-common.cuh b/ggml/src/ggml-cuda/fattn-common.cuh
index 8dc82a9d3..09c19429a 100644
--- a/ggml/src/ggml-cuda/fattn-common.cuh
+++ b/ggml/src/ggml-cuda/fattn-common.cuh
@@ -918,7 +918,9 @@ void launch_fattn(
blocks_num.y = 1;
blocks_num.z = 1;
- dst_tmp_meta.alloc(blocks_num.x*ncols * (2*2 + DV) * sizeof(float));
+ if (ntiles_total % blocks_num.x != 0) { // Fixup is only needed if the SMs work on fractional tiles.
+ dst_tmp_meta.alloc((size_t(blocks_num.x) * ncols * (2 + DV/2)));
+ }
} else {
const int ntiles_KQ = (K->ne[1] + nbatch_fa - 1) / nbatch_fa; // Max. number of parallel blocks limited by tensor size.
diff --git a/ggml/src/ggml-cuda/ggml-cuda.cu b/ggml/src/ggml-cuda/ggml-cuda.cu
index edb8a12f8..e3854758c 100644
--- a/ggml/src/ggml-cuda/ggml-cuda.cu
+++ b/ggml/src/ggml-cuda/ggml-cuda.cu
@@ -3265,6 +3265,7 @@ static void evaluate_and_capture_cuda_graph(ggml_backend_cuda_context * cuda_ctx
should_launch_concurrent_events = should_launch_concurrent_events && event.is_valid();
}
}
+
if (should_launch_concurrent_events) {
// Restore original node order within each concurrent region to enable fusion within streams
@@ -3316,6 +3317,8 @@ static void evaluate_and_capture_cuda_graph(ggml_backend_cuda_context * cuda_ctx
cgraph->nodes[start_pos + i] = const_cast(event.original_order[i]);
}
}
+ } else {
+ stream_ctx.concurrent_events.clear();
}
for (int i = 0; i < cgraph->n_nodes; i++) {
@@ -3704,11 +3707,7 @@ static void evaluate_and_capture_cuda_graph(ggml_backend_cuda_context * cuda_ctx
}
}
-static enum ggml_status ggml_backend_cuda_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
- ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
-
- ggml_cuda_set_device(cuda_ctx->device);
-
+static bool ggml_cuda_set_cuda_graph_enabled(ggml_backend_cuda_context * cuda_ctx) {
#ifdef USE_CUDA_GRAPH
static const bool disable_cuda_graphs_due_to_env = (getenv("GGML_CUDA_DISABLE_GRAPHS") != nullptr);
@@ -3718,7 +3717,6 @@ static enum ggml_status ggml_backend_cuda_graph_compute(ggml_backend_t backend,
}
bool use_cuda_graph = true;
- bool cuda_graph_update_required = false;
if (cuda_ctx->cuda_graph->graph == nullptr) {
if (ggml_cuda_info().devices[cuda_ctx->device].cc < GGML_CUDA_CC_AMPERE) {
@@ -3739,6 +3737,29 @@ static enum ggml_status ggml_backend_cuda_graph_compute(ggml_backend_t backend,
use_cuda_graph = false;
}
+ cuda_ctx->cuda_graph->cuda_graphs_enabled = use_cuda_graph;
+#else
+ bool use_cuda_graph = false;
+#endif // USE_CUDA_GRAPH
+
+ return use_cuda_graph;
+}
+
+static enum ggml_status ggml_backend_cuda_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
+ ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *) backend->context;
+
+ bool use_cuda_graph = false;
+ bool cuda_graph_update_required = false;
+
+ // graph_optimize calls set_cuda_graph_enabled, in-case it not called (i.e. graph_compute is directly called)
+ // we call it here instead.
+#ifdef USE_CUDA_GRAPH
+ if (!cuda_ctx->cuda_graph) {
+ use_cuda_graph = ggml_cuda_set_cuda_graph_enabled(cuda_ctx);
+ } else {
+ use_cuda_graph = cuda_ctx->cuda_graph && cuda_ctx->cuda_graph->cuda_graphs_enabled;
+ }
+
if (use_cuda_graph) {
cuda_graph_update_required = is_cuda_graph_update_required(cuda_ctx, cgraph);
@@ -3758,6 +3779,7 @@ static enum ggml_status ggml_backend_cuda_graph_compute(ggml_backend_t backend,
#endif
}
}
+#endif // USE_CUDA_GRAPH
if (use_cuda_graph && cuda_graph_update_required) {
// Start CUDA graph capture
@@ -3769,11 +3791,6 @@ static enum ggml_status ggml_backend_cuda_graph_compute(ggml_backend_t backend,
CUDA_CHECK(cudaStreamBeginCapture(cuda_ctx->stream(), cudaStreamCaptureModeRelaxed));
}
-#else
- bool use_cuda_graph = false;
- bool cuda_graph_update_required = false;
-#endif // USE_CUDA_GRAPH
-
bool graph_evaluated_or_captured = false;
evaluate_and_capture_cuda_graph(cuda_ctx, cgraph, graph_evaluated_or_captured, use_cuda_graph, cuda_graph_update_required);
@@ -3809,8 +3826,10 @@ static void ggml_backend_cuda_event_wait(ggml_backend_t backend, ggml_backend_ev
static void ggml_backend_cuda_graph_optimize(ggml_backend_t backend, ggml_cgraph * cgraph) {
ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *) backend->context;
+ const bool use_cuda_graph = ggml_cuda_set_cuda_graph_enabled(cuda_ctx);
+
static bool enable_graph_optimization = [] {
- const char * env = getenv("GGML_CUDA_GRAPH_OPT");
+ const char * env = getenv("GGML_CUDA_GRAPH_OPT");
return env != nullptr && atoi(env) == 1;
}();
@@ -3818,12 +3837,13 @@ static void ggml_backend_cuda_graph_optimize(ggml_backend_t backend, ggml_cgraph
return;
}
- GGML_ASSERT(ggml_backend_cuda_get_device_count() == 1 && "compute graph optimization is only supported on single GPU in the CUDA backend");
- GGML_LOG_DEBUG("Optimizing CUDA graph %p with %d nodes\n", cgraph->nodes, cgraph->n_nodes);
-
ggml_cuda_stream_context & stream_context = cuda_ctx->stream_context();
stream_context.reset();
+ if (!use_cuda_graph || ggml_backend_cuda_get_device_count() != 1) {
+ return;
+ }
+
// number of out-degrees for a particular node
std::unordered_map fan_out;
// reverse mapping of node to index in the cgraph
@@ -3884,6 +3904,12 @@ static void ggml_backend_cuda_graph_optimize(ggml_backend_t backend, ggml_cgraph
if (count >= min_fan_out && count <= max_fan_out) {
const int root_node_idx = node_indices[root_node];
+ // only optimize for attn_norm
+ // TODO: make this more generic
+ if (!strstr(root_node->name, "attn_norm")) {
+ continue;
+ }
+
bool is_part_of_event = false;
for (const auto & [start, end] : concurrent_node_ranges) {
if (root_node_idx >= start && root_node_idx <= end) {
diff --git a/src/llama-context.cpp b/src/llama-context.cpp
index 57da38648..a47299716 100644
--- a/src/llama-context.cpp
+++ b/src/llama-context.cpp
@@ -1468,7 +1468,7 @@ ggml_cgraph * llama_context::graph_reserve(
if (n_tokens % n_seqs != 0) {
n_tokens = ((n_tokens + (n_seqs - 1)) / n_seqs) * n_seqs; // round to next multiple of n_seqs
- n_outputs = std::min(n_outputs, n_tokens);
+ n_outputs = std::max(n_outputs, n_tokens);
//LLAMA_LOG_DEBUG("%s: making n_tokens a multiple of n_seqs - n_tokens = %u, n_seqs = %u, n_outputs = %u\n", __func__, n_tokens, n_seqs, n_outputs);
}
diff --git a/src/llama-grammar.cpp b/src/llama-grammar.cpp
index 48e9c019a..c66d80050 100644
--- a/src/llama-grammar.cpp
+++ b/src/llama-grammar.cpp
@@ -394,6 +394,44 @@ static void print_rule(
fprintf(file, "\n");
}
+//
+// Regex utilities
+//
+
+size_t llama_grammar_trigger_pattern::find(const std::string & input) const {
+ auto find_start_pos = [](const std::smatch & match) {
+ // get from the first matched capturing group to the end of the string
+ size_t start = std::string::npos;
+ for (auto i = 1u; i < match.size(); i++) {
+ if (match.length(i) > 0) {
+ start = match.position(i);
+ break;
+ }
+ }
+ if (start == std::string::npos) {
+ start = match.position(0);
+ }
+ return start;
+ };
+
+ if (!pattern.empty() && pattern.front() == '^' && pattern.back() == '$') {
+ // match against the entire input
+ std::smatch match;
+ if (std::regex_match(input, match, regex)) {
+ return find_start_pos(match);
+ }
+ }
+
+ // search anywhere
+ std::smatch match;
+ if (std::regex_search(input, match, regex)) {
+ return find_start_pos(match);
+ }
+
+ return std::string::npos;
+}
+
+
//
// implementation
//
@@ -1372,21 +1410,10 @@ void llama_grammar_accept_impl(struct llama_grammar & grammar, llama_token token
grammar.trigger_buffer_positions.push_back(std::make_pair(token, position));
grammar.trigger_buffer += piece;
- std::smatch match;
for (const auto & trigger_pattern : grammar.trigger_patterns) {
- if (std::regex_match(grammar.trigger_buffer, match, trigger_pattern.regex)) {
+ auto start = trigger_pattern.find(grammar.trigger_buffer);
+ if (start != std::string::npos) {
grammar.awaiting_trigger = false;
- // get from the first matched capturing group to the end of the string
- size_t start = std::string::npos;
- for (auto i = 1u; i < match.size(); i++) {
- if (match.length(i) > 0) {
- start = match.position(i);
- break;
- }
- }
- if (start == std::string::npos) {
- start = match.position(0);
- }
// replay tokens that overlap with [start, end)
for (const auto & [tok, tok_pos] : grammar.trigger_buffer_positions) {
diff --git a/src/llama-grammar.h b/src/llama-grammar.h
index a4c978ac1..b5a0e588e 100644
--- a/src/llama-grammar.h
+++ b/src/llama-grammar.h
@@ -119,6 +119,8 @@ struct llama_grammar_parser {
struct llama_grammar_trigger_pattern {
std::string pattern;
std::regex regex;
+
+ size_t find(const std::string & input) const;
};
struct llama_grammar {
diff --git a/src/llama-graph.cpp b/src/llama-graph.cpp
index 1d0d7197e..8edf7d749 100644
--- a/src/llama-graph.cpp
+++ b/src/llama-graph.cpp
@@ -32,7 +32,7 @@ bool llm_graph_input_embd::can_reuse(const llm_graph_params & params) {
bool res = true;
res &= (!tokens && !params.ubatch.token) || (tokens && tokens->ne[0] == params.ubatch.n_tokens);
- res &= (!embd && !params.ubatch.embd) || (embd && embd->ne[0] == params.ubatch.n_tokens);
+ res &= (!embd && !params.ubatch.embd) || (embd && embd->ne[1] == params.ubatch.n_tokens);
return res;
}
@@ -62,7 +62,7 @@ void llm_graph_input_pos::set_input(const llama_ubatch * ubatch) {
bool llm_graph_input_pos::can_reuse(const llm_graph_params & params) {
bool res = true;
- res &= pos->ne[0] == params.ubatch.n_tokens;
+ res &= pos->ne[0] == params.ubatch.n_tokens*n_pos_per_embd;
return res;
}