Compare commits

...
3 Commits
Author SHA1 Message Date
Wenyu Chen 212c547fe9 ui: add cancel button to cancel model loading (#32225) 2026-11-23 09:27:44 +01:00
Leon f5e6915c28 webgpu : add ROLL support (#31887)
* webgpu : add ROLL support

* tests : keep ROLL cases portable across backends
2026-11-23 09:56:54 +02:00
Aldehir Rojas dd53f70d08 chat : improve inkling parser/grammar (#32833) 2026-11-23 09:54:42 +02:00
10 changed files with 145 additions and 68 deletions
+36 -38
View File
@@ -100,18 +100,18 @@ common_chat_params common_chat_params_init_inkling(const common_chat_template &
auto use_tools = has_tools && inputs.tool_choice != COMMON_CHAT_TOOL_CHOICE_NONE;
auto extract_reasoning = inputs.reasoning_format != COMMON_REASONING_FORMAT_NONE;
data.parser = build_chat_peg_parser([&](common_chat_peg_builder & p) {
auto header = p.literal(HEADER);
auto end = p.literal(END);
data.parser = build_chat_peg_parser(inputs.specials, [&](common_chat_peg_builder & p) {
auto header = p.token(HEADER);
auto end = p.token(END);
auto think = p.literal(THINK);
auto thinking = p.rule("thinking", extract_reasoning ? think + p.reasoning(p.until(END)) + end :
p.atomic(p.content(think)) + p.content(p.until(END) + end));
auto text = p.rule("text", p.literal(TEXT) + p.content(p.until(END)) + end);
auto think = p.token(THINK);
auto thinking = p.rule("thinking", extract_reasoning ? think + p.reasoning(p.until(end)) + end :
p.atomic(p.content(think)) + p.content(p.until(end) + end));
auto text = p.rule("text", p.token(TEXT) + p.content(p.until(end)) + end);
if (has_response_format) {
auto response_format = p.rule("response-format", p.content(p.schema(p.json(), "response-format-schema", inputs.json_schema)));
return p.zero_or_more(header + thinking) + header + p.literal(TEXT) + response_format + end;
return p.zero_or_more(header + thinking) + header + p.token(TEXT) + response_format + end;
}
auto blocks = p.zero_or_more(header + (thinking | text));
@@ -120,45 +120,43 @@ common_chat_params common_chat_params_init_inkling(const common_chat_template &
return blocks;
}
auto tool_choice = p.choice();
foreach_function(inputs.tools, [&](size_t tool_index, const json & tool) {
const auto & function = tool.at("function");
std::string name = function.at("name");
auto open = p.tool_name(p.literal(name)) + p.literal(TOOL_JSON) + p.literal("{\"name\":" + json(name).dump() + ",\"args\":");
auto args = p.tool_args(p.json());
tool_choice |= p.rule("tool-" + std::to_string(tool_index), p.tool(p.tool_open(open) + args + p.tool_close(p.literal("}")) + end));
});
// tool calls are the last blocks of the turn
auto calls = inputs.parallel_tool_calls ? tool_choice + p.zero_or_more(header + tool_choice) : tool_choice;
auto tool_calls = header + p.trigger_rule("tool-calls", calls);
// the call takes the name in the JSON
auto tool_json = p.token(TOOL_JSON);
auto tool_body = [&](const common_peg_parser & json_name) {
return p.tool_open(p.literal("{")) << p.json_member("name", p.literal("\"") + p.tool_name(json_name) + p.literal("\"")) << p.literal(",")
<< p.json_member("args", p.tool_args(p.json())) << p.tool_close(p.literal("}")) + end;
};
if (inputs.tool_choice == COMMON_CHAT_TOOL_CHOICE_REQUIRED) {
return blocks + tool_calls;
auto tool_choice = p.choice();
foreach_function(inputs.tools, [&](size_t tool_index, const json & tool) {
const auto & function = tool.at("function");
std::string name = function.at("name");
std::string quoted = json(name).dump();
// The research model does not generate a tool name before the tool call token
auto json_name = p.literal(quoted.substr(1, quoted.size() - 2));
tool_choice |= p.rule("tool-" + std::to_string(tool_index), p.tool(p.optional(p.literal(name)) + tool_json + tool_body(json_name)));
});
auto calls = inputs.parallel_tool_calls ? tool_choice + p.zero_or_more(header + tool_choice) : tool_choice;
return blocks + header + calls;
}
return blocks + p.optional(tool_calls);
auto name = p.until(tool_json);
auto body = tool_body(p.string_content('"'));
auto next = p.rule("tool-call", header + p.tool(name + tool_json + body));
auto more = inputs.parallel_tool_calls ? p.zero_or_more(next) : p.eps();
auto calls = header + name + p.trigger_rule("tool-calls", tool_json, body + more);
return blocks + p.optional(calls);
});
if (has_response_format || use_tools) {
data.grammar_lazy = !has_response_format && inputs.tool_choice != COMMON_CHAT_TOOL_CHOICE_REQUIRED;
data.grammar_lazy = false;
data.grammar = build_grammar([&](const common_grammar_builder & builder) {
data.parser.build_grammar(builder, data.grammar_lazy);
data.parser.build_grammar(builder, !has_response_format && inputs.tool_choice == COMMON_CHAT_TOOL_CHOICE_AUTO);
});
if (data.grammar_lazy) {
// a header followed by text and not by a content token is a tool call, the grammar starts at the name
data.grammar_triggers = {
{ COMMON_GRAMMAR_TRIGGER_TYPE_PATTERN, "<\\|message_model\\|>([^<])" },
};
// Inkling 1: the first header is in the generation prompt, and a lazy grammar does not get the prompt.
// an output that starts with no content token is a tool call. not for a continuation, it starts with text
if (!inputs.has_continuation()) {
data.grammar_triggers.push_back({ COMMON_GRAMMAR_TRIGGER_TYPE_PATTERN_FULL, "[^<]+" });
}
}
}
return data;
+1 -1
View File
@@ -97,7 +97,7 @@ Legend:
| REPEAT_BACK | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
| RMS_NORM | ❌ | ✅ | ✅ | ✅ | 🟡 | ✅ | ✅ | 🟡 | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
| RMS_NORM_BACK | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
| ROLL | ❌ | ❌ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
| ROLL | ❌ | ❌ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
| ROPE | ❌ | ✅ | ✅ | ✅ | 🟡 | 🟡 | ✅ | 🟡 | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
| ROPE_BACK | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
| ROUND | ❌ | ❌ | ✅ | 🟡 | 🟡 | ❌ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
+7 -1
View File
@@ -12345,7 +12345,13 @@
"WebGPU: WebGPU","PAD_REFLECT_1D","type=f32,ne_a=[257,7,1,1],pad_0=0,pad_1=256,view=0","support","1","yes","WebGPU"
"WebGPU: WebGPU","PAD_REFLECT_1D","type=f32,ne_a=[2,16777217,1,1],pad_0=1,pad_1=1,view=0","support","1","yes","WebGPU"
"WebGPU: WebGPU","PAD_REFLECT_1D","type=f32,ne_a=[3000,384,4,1],pad_0=10,pad_1=9,view=0","support","1","yes","WebGPU"
"WebGPU: WebGPU","ROLL","shift0=3,shift1=-2,shift3=1,shift4=-1","support","0","no","WebGPU"
"WebGPU: WebGPU","ROLL","shift0=3,shift1=-2,shift3=1,shift4=-1,permute=0,ne_a=[10,5,4,3]","support","1","yes","WebGPU"
"WebGPU: WebGPU","ROLL","shift0=3,shift1=-2,shift3=1,shift4=-1,permute=1,ne_a=[10,5,4,3]","support","1","yes","WebGPU"
"WebGPU: WebGPU","ROLL","shift0=0,shift1=0,shift3=0,shift4=0,permute=0,ne_a=[1,1,1,1]","support","1","yes","WebGPU"
"WebGPU: WebGPU","ROLL","shift0=256,shift1=0,shift3=0,shift4=0,permute=0,ne_a=[257,1,1,1]","support","1","yes","WebGPU"
"WebGPU: WebGPU","ROLL","shift0=-16,shift1=6,shift3=-2,shift4=1,permute=0,ne_a=[17,7,3,2]","support","1","yes","WebGPU"
"WebGPU: WebGPU","ROLL","shift0=4,shift1=0,shift3=0,shift4=0,permute=0,ne_a=[42,512,1,1]","support","1","yes","WebGPU"
"WebGPU: WebGPU","ROLL","shift0=4,shift1=0,shift3=0,shift4=0,permute=0,ne_a=[380,512,1,1]","support","1","yes","WebGPU"
"WebGPU: WebGPU","ARANGE","type=f32,start=0.000000,stop=10.000000,step=1.000000","support","1","yes","WebGPU"
"WebGPU: WebGPU","ARANGE","type=f32,start=3.000000,stop=3.500000,step=1.000000","support","1","yes","WebGPU"
"WebGPU: WebGPU","ARANGE","type=f32,start=-10.000000,stop=10.000000,step=0.300000","support","1","yes","WebGPU"
Can't render this file because it is too large.
@@ -2117,7 +2117,7 @@ class ggml_webgpu_shader_lib {
webgpu_pipeline get_pad_pipeline(const ggml_webgpu_shader_lib_context & context) {
ggml_webgpu_pad_pipeline_key key = {};
key.reflect = context.dst->op == GGML_OP_PAD_REFLECT_1D;
key.circular = !key.reflect && ggml_get_op_params_i32(context.dst, 8) != 0;
key.circular = context.dst->op == GGML_OP_ROLL || (!key.reflect && ggml_get_op_params_i32(context.dst, 8) != 0);
auto it = pad_pipelines.find(key);
if (it != pad_pipelines.end()) {
+13 -9
View File
@@ -964,16 +964,18 @@ static webgpu_encoded_op ggml_webgpu_pad(webgpu_context & ctx, ggml_tensor * src
(uint32_t) dst->ne[1],
(uint32_t) dst->ne[2],
(uint32_t) dst->ne[3],
// Pad sizes
(uint32_t) ggml_get_op_params_i32(dst, 0),
(uint32_t) ggml_get_op_params_i32(dst, 1),
(uint32_t) ggml_get_op_params_i32(dst, 2),
(uint32_t) ggml_get_op_params_i32(dst, 3),
(uint32_t) ggml_get_op_params_i32(dst, 4),
(uint32_t) ggml_get_op_params_i32(dst, 5),
(uint32_t) ggml_get_op_params_i32(dst, 6),
(uint32_t) ggml_get_op_params_i32(dst, 7),
};
for (int i = 0; i < 4; i++) {
if (dst->op == GGML_OP_ROLL) {
// Circular padding with unchanged dimensions implements a roll.
const int64_t shift = ggml_get_op_params_i32(dst, i);
params.push_back((uint32_t) (shift < 0 ? shift + src->ne[i] : shift));
params.push_back(0);
} else {
params.push_back((uint32_t) ggml_get_op_params_i32(dst, 2 * i));
params.push_back((uint32_t) ggml_get_op_params_i32(dst, 2 * i + 1));
}
}
std::vector<wgpu::BindGroupEntry> entries = {
ggml_webgpu_make_tensor_bind_group_entry(ctx, 0, src),
@@ -3740,6 +3742,7 @@ static std::optional<webgpu_encoded_op> ggml_webgpu_encode(webgpu_context ctx,
return ggml_webgpu_pool2d(ctx, src0, node);
case GGML_OP_COL2IM_1D:
return ggml_webgpu_col2im_1d(ctx, src0, node);
case GGML_OP_ROLL:
case GGML_OP_PAD_REFLECT_1D:
case GGML_OP_PAD:
return ggml_webgpu_pad(ctx, src0, node);
@@ -5299,6 +5302,7 @@ static bool ggml_backend_webgpu_device_supports_op(ggml_backend_dev_t dev, const
case GGML_OP_PAD_REFLECT_1D:
supports_op = op->type == GGML_TYPE_F32 && src0->type == GGML_TYPE_F32;
break;
case GGML_OP_ROLL:
case GGML_OP_PAD:
supports_op = op->type == GGML_TYPE_F32 && src0->type == GGML_TYPE_F32;
break;
+14 -5
View File
@@ -9087,17 +9087,18 @@ struct test_roll : public test_case {
const int shift3;
const int shift4;
const bool permute;
const std::array<int64_t, 4> ne_a;
std::string vars() override {
return VARS_TO_STR5(shift0, shift1, shift3, shift4, permute);
return VARS_TO_STR6(shift0, shift1, shift3, shift4, permute, ne_a);
}
test_roll(int shift0 = 3, int shift1 = -2, int shift3 = 1, int shift4 = -1, bool permute = false)
: shift0(shift0), shift1(shift1), shift3(shift3), shift4(shift4), permute(permute) {}
test_roll(int shift0 = 3, int shift1 = -2, int shift3 = 1, int shift4 = -1, bool permute = false,
std::array<int64_t, 4> ne_a = {10, 5, 4, 3})
: shift0(shift0), shift1(shift1), shift3(shift3), shift4(shift4), permute(permute), ne_a(ne_a) {}
ggml_tensor * build_graph(ggml_context * ctx) override {
int64_t ne[4] = {10, 5, 4, 3};
ggml_tensor * a = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne);
ggml_tensor * a = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne_a.data());
ggml_set_name(a, "a");
if (permute) {
@@ -13183,6 +13184,11 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
test_cases.emplace_back(new test_pad_reflect_1d(GGML_TYPE_F32, {3000, 384, 4, 1}));
test_cases.emplace_back(new test_roll());
test_cases.emplace_back(new test_roll(3, -2, 1, -1, true));
test_cases.emplace_back(new test_roll(0, 0, 0, 0, false, {1, 1, 1, 1}));
test_cases.emplace_back(new test_roll(256, 0, 0, 0, false, {257, 1, 1, 1}));
test_cases.emplace_back(new test_roll(-16, 6, -2, 1, false, {17, 7, 3, 2}));
test_cases.emplace_back(new test_roll(4, 0, 0, 0, false, {42, 512, 1, 1}));
test_cases.emplace_back(new test_roll(4, 0, 0, 0, false, {380, 512, 1, 1}));
test_cases.emplace_back(new test_arange());
test_cases.emplace_back(new test_arange(GGML_TYPE_F32, 3.0f, 3.5f, 1.0f));
test_cases.emplace_back(new test_arange(GGML_TYPE_F32, -10.0f, 10.0f, 0.3f));
@@ -13916,6 +13922,9 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_perf() {
test_cases.emplace_back(new test_arange(GGML_TYPE_F32, 0.0f, n, 1.0f));
}
test_cases.emplace_back(new test_roll(4, 0, 0, 0, false, {42, 512, 1, 1}));
test_cases.emplace_back(new test_roll(4, 0, 0, 0, false, {380, 512, 1, 1}));
// embedding lookups: one token (decode) and one batch of 2048 tokens (prefill)
for (int r : {16, 32768}) {
test_cases.emplace_back(new test_get_rows(GGML_TYPE_F8_E4M3, 160, 2000000, r, 1, 1, false)); // Qwen4-Exp n-gram table, 16 rows per token
+32 -9
View File
@@ -5653,6 +5653,7 @@ static void test_template_output_peg_parsers(bool detailed_debug) {
for (const int version : { 1, 2 }) {
auto tst = peg_tester(version == 1 ? "models/templates/thinkingmachines-Inkling.jinja" :
"models/templates/thinkingmachines-Inkling-2-Small.jinja", detailed_debug);
tst.specials({ "<|message_model|>", "<|content_thinking|>", "<|content_text|>", "<|content_invoke_tool_json|>", "<|end_message|>" });
// Inkling 1 has the first header in the generation prompt, Inkling 2 writes it
const std::string H = "<|message_model|>";
@@ -5693,7 +5694,8 @@ static void test_template_output_peg_parsers(bool detailed_debug) {
.expect_tool_calls({
{ "special_function", R"({"arg1":1})", "" },
})
.expect_rules({ "tool-0" })
.expect_rules({ "tool-calls" })
.expect_grammar()
.run();
// Thinking, content, then a tool call
@@ -5721,20 +5723,41 @@ static void test_template_output_peg_parsers(bool detailed_debug) {
})
.run();
// The block closer inside a string argument does not end the block
tst.test(P + "python<|content_invoke_tool_json|>{\"name\":\"python\",\"args\":{\"code\":\"print('<|end_message|>')\"}}" + E)
.tools({ python_tool })
.expect_tool_calls({
{ "python", R"JSON({"code":"print('<|end_message|>')"})JSON", "" },
})
.run();
// Forced tool call
tst.test(P + "<|content_thinking|>I should call it" + E + H + CALL)
.reasoning_format(COMMON_REASONING_FORMAT_AUTO)
.tool_choice(COMMON_CHAT_TOOL_CHOICE_REQUIRED)
.tools({ special_function_tool })
.expect(simple_assist_msg("", "I should call it", "special_function", R"({"arg1":1})", ""))
.expect_rules({ "tool-0" })
.expect_grammar()
.run();
// The research model does not write the name before the tool token
for (const auto tool_choice : { COMMON_CHAT_TOOL_CHOICE_AUTO, COMMON_CHAT_TOOL_CHOICE_REQUIRED }) {
tst.test(P + "<|content_invoke_tool_json|>{\"name\":\"special_function\",\"args\":{\"arg1\":1}}" + E)
.tool_choice(tool_choice)
.tools({ special_function_tool })
.expect_tool_calls({
{ "special_function", R"({"arg1":1})", "" },
})
.run();
}
// A tool that is not in the list, with spaces in the JSON
tst.test(P + "unknown_tool<|content_invoke_tool_json|>{\"name\": \"unknown_tool\", \"args\": {\"arg1\": 1}}" + E)
.tools({ special_function_tool })
.expect_tool_calls({
{ "unknown_tool", R"({"arg1": 1})", "" },
})
.run();
// The call takes the name in the JSON, not the one before the tool token
tst.test(P + "special<|content_invoke_tool_json|>{\"name\":\"special_function\",\"args\":{\"arg1\":1}}" + E)
.tools({ special_function_tool })
.expect_tool_calls({
{ "special_function", R"({"arg1":1})", "" },
})
.run();
// Response format
@@ -31,7 +31,29 @@
<div class={['flex w-5 shrink-0 items-center justify-center', className]}>
{#if isLoading}
<MODEL_LOAD_ICONS.loading class="{ICON_CLASS_DEFAULT} animate-spin text-muted-foreground" />
<MODEL_LOAD_ICONS.loading
class="{ICON_CLASS_DEFAULT} animate-spin text-muted-foreground {showAction && revealOnHover
? 'group-hover:hidden [@media(pointer:coarse)]:hidden'
: ''}"
/>
<div
class={!showAction
? 'hidden'
: revealOnHover
? 'hidden group-hover:flex [@media(pointer:coarse)]:flex'
: 'flex'}
>
<ActionIcon
class="h-5 w-5 hover:text-foreground"
icon={MODEL_LOAD_ICONS.cancel}
iconSize="h-4 w-4"
onclick={() => modelsStore.status.cancelLoad(option.model)}
stopPropagationOnClick
tooltip="Cancel loading model"
tooltipAsTitle
/>
</div>
{:else}
<!-- the state dot is what the row shows at rest; the action takes its place on hover -->
{#if showAction && revealOnHover}
@@ -22,6 +22,7 @@ import LoadIcon from '@lucide/svelte/icons/power';
import RetryIcon from '@lucide/svelte/icons/rotate-cw';
import PromptCommandIcon from '@lucide/svelte/icons/sparkles';
import VideoIcon from '@lucide/svelte/icons/video';
import CancelLoadIcon from '@lucide/svelte/icons/x';
import { ChatFormCommandAction, FileTypeCategory, ModelCapability } from '#lib/enums/index.ts';
import type { ModelCapabilities } from '#lib/types/models.js';
import type { Component } from 'svelte';
@@ -80,6 +81,7 @@ export const MODEL_DOWNLOAD_ICONS = {
/** Icons the model load control shows for its states and actions. */
export const MODEL_LOAD_ICONS = {
cancel: CancelLoadIcon,
failed: LoadFailedIcon,
load: LoadIcon,
loading: LoadingIcon,
@@ -152,6 +152,8 @@ export class ModelStatusManager {
private listedSidecarsByRepo = $derived.by(() =>
ModelsService.draftSidecarsByRepo(this.host.routerModels)
);
/** Models whose load the user cancelled; the failure the force-kill triggers is intentional, not a load failure. */
private loadCancelRequests = new SvelteSet<string>();
private loadingStates = new SvelteMap<string, boolean>();
private loadProgress = new SvelteMap<string, ModelLoadProgress>();
/** Paused downloads with their last reported progress, or null when none arrived before the pause. */
@@ -198,8 +200,8 @@ export class ModelStatusManager {
});
// /models/sse feed state, the single source of truth for status and load progress
private statusAbort: AbortController | null = null;
private statusReaderActive = false;
private statusReaderActive = false;
private statusWaiters = new SvelteMap<
string,
{ target: ServerModelStatus; resolve: () => void; reject: (e: Error) => void }[]
@@ -269,10 +271,15 @@ export class ModelStatusManager {
this.subscribe();
// mark before the kill: the feed can report the child's death before
// this request returns, and load() must not toast it as a failure
this.loadCancelRequests.add(modelId);
try {
await ModelsService.unload(modelId);
toast.info(`Load cancelled: ${this.host.toDisplayName(modelId)}`);
} catch (error) {
this.loadCancelRequests.delete(modelId);
toast.error(`Failed to cancel load: ${this.host.toDisplayName(modelId)}`);
throw error;
@@ -516,11 +523,17 @@ export class ModelStatusManager {
toast.success(`Model loaded: ${this.host.toDisplayName(modelId)}`);
} catch (error) {
this.rejectStatus(modelId, error instanceof Error ? error : new Error('load failed'));
this.host.error = error instanceof Error ? error.message : 'Failed to load model';
toast.error(`Failed to load model: ${this.host.toDisplayName(modelId)}`);
// a user cancel force-kills the child, which settles this waiter as a
// failure; cancelLoad already reported the cancel, so stay silent
if (!this.loadCancelRequests.delete(modelId)) {
this.host.error = error instanceof Error ? error.message : 'Failed to load model';
toast.error(`Failed to load model: ${this.host.toDisplayName(modelId)}`);
}
throw error;
} finally {
this.loadCancelRequests.delete(modelId);
this.loadingStates.set(modelId, false);
}
}