diff --git a/README.md b/README.md index e71d03f..ec186ab 100644 --- a/README.md +++ b/README.md @@ -140,6 +140,7 @@ the shared Engram file, Unsloth's DSpark drafter and a pack directory made from build/bin/unbiased-strata --engine build/bin/strata-dsv41 \ -m /models/dsv41/IQ2_XXS/DeepSeek-V4.1-Flash-1017-GSQ-RCO-IQ2_XXS-00001-of-00004.gguf \ --host 127.0.0.1 --port 11434 --alias deepseek-v4.1-flash-1017-ista-iq2_xxs --sse-ping-interval 10 \ + --chat-template-file data/dsv41/chat_template-ista-1017.jinja --temp 1.0 --top-p 1.0 --min-p 0.0 --top-k 64 \ -- --pack /models/dsv41/IQ2_XXS/maya-ds4 --max-context 131072 --dspark auto --gpus 0 ``` diff --git a/data/dsv41/chat_template-ista-1017.jinja b/data/dsv41/chat_template-ista-1017.jinja new file mode 100644 index 0000000..4a2b356 --- /dev/null +++ b/data/dsv41/chat_template-ista-1017.jinja @@ -0,0 +1,148 @@ +{#- ISTA-DASLab DeepSeek-V4.1-Flash-1017 GGUF chat template with the 1017 DSML tag spelling written out (" calls>", " invoke", " parameter"): the GGUF's own text spells them "tool_calls>"/"invoke"/"parameter" through a dsml_token variable, the model writes the spaced form, and llama.cpp selects its V4.1 parser on the literal "|DSML| calls>" (UnbiasedStrata, 2026-10-24) #} +{%- if not add_generation_prompt is defined -%} + {%- set add_generation_prompt = false -%} +{%- endif -%} +{%- if not thinking is defined -%} + {%- if enable_thinking is defined -%} + {%- set thinking = enable_thinking -%} + {%- else -%} + {%- set thinking = false -%} + {%- endif -%} +{%- endif -%} +{%- if not drop_thinking is defined -%} + {%- set drop_thinking = true -%} +{%- endif -%} +{%- set dsml_token = '|DSML|' -%} +{%- set thinking_start_token = '' -%} +{%- set thinking_end_token = '' -%} +{#- DeepSeek-V4.1: reasoning effort is an integer budget in [1, 100] or 'low'/'high'/'max' (50/75/100), default 'high'. + It is rendered as a system prefix in thinking mode only. llama-server passes it as a string ("50"). -#} +{%- set effort_levels = {'low': 50, 'high': 75, 'max': 100} -%} +{%- set effort = namespace(value=75) -%} +{%- if reasoning_effort is defined and reasoning_effort is not none and reasoning_effort != '' -%} + {%- if reasoning_effort is string and reasoning_effort in effort_levels -%} + {%- set effort.value = effort_levels[reasoning_effort] -%} + {%- else -%} + {%- set effort.value = reasoning_effort | int -%} + {%- endif -%} + {%- if effort.value < 1 or effort.value > 100 -%} + {{- raise_exception('reasoning_effort must be an integer in [1, 100] or low/high/max, got ' + (reasoning_effort | string)) -}} + {%- endif -%} +{%- endif -%} +{%- set response_format_template = '## Response Format:\n\nYou MUST strictly adhere to the following schema to reply:\n' -%} +{%- set has_tools = false -%} +{%- set tools_header = '## Tools\n\nYou have access to a set of tools to help answer the user\'s question. You can invoke tools by writing a "<|DSML| calls>" block like the following:\n\n<|DSML| calls>\n<|DSML| invoke name="$TOOL_NAME">\n<|DSML| parameter name="$PARAMETER_NAME" string="true|false">$PARAMETER_VALUE\n...\n\n<|DSML| invoke name="$TOOL_NAME2">\n...\n\n\n\nString parameters should be specified as is and set `string="true"`. For all other types (numbers, booleans, arrays, objects), pass the value in JSON format and set `string="false"`.\n\nIf thinking_mode is enabled (triggered by ' + thinking_start_token + '), you MUST output your complete reasoning inside ' + thinking_start_token + '...' + thinking_end_token + ' BEFORE any tool calls or final response.\n\nOtherwise, output directly after ' + thinking_end_token + ' with tool calls or final response.\n\n### Available Tool Schemas\n\n' -%} +{%- set tools_footer = '\nYou MUST strictly follow the above defined tool name and parameter schemas to invoke tool calls.\n' -%} +{%- set ns = namespace(system_prompt='', is_first_sp=true, has_tool_calls=false) -%} +{%- set lead_system = messages and messages[0]['role'] == 'system' -%} +{%- if lead_system -%} + {%- set ns.system_prompt = messages[0]['content'] or '' -%} +{%- endif -%} +{%- if tools is defined and tools -%} + {%- set has_tools = true -%} + {%- set ts = namespace(schemas='') -%} + {%- for tool in tools -%} + {%- if tool['type'] == 'function' -%} + {%- set ts.schemas = ts.schemas + (tool['function'] | tojson) + '\n' -%} + {%- endif -%} + {%- endfor -%} + {%- if ns.system_prompt -%} + {%- set ns.system_prompt = ns.system_prompt + '\n\n' + tools_header + ts.schemas + tools_footer -%} + {%- else -%} + {%- set ns.system_prompt = tools_header + ts.schemas + tools_footer -%} + {%- endif -%} +{%- endif -%} +{%- if response_format is defined -%} + {%- if ns.system_prompt -%} + {%- set ns.system_prompt = ns.system_prompt + '\n\n' -%} + {%- endif -%} + {%- set ns.system_prompt = ns.system_prompt + response_format_template + (response_format | tojson) -%} +{%- endif -%} +{{- bos_token -}} +{%- set effort_prefix = 'Reasoning Effort: ' + (effort.value | string) + ' (range 1-100, the higher the value, the more thorough the reasoning)\n\n' if messages and thinking else '' -%} +{%- if effort_prefix or lead_system or ns.system_prompt -%} + {{- '<|System|>' + effort_prefix + ns.system_prompt -}} +{%- endif -%} +{%- set last_user_idx = namespace(value=-1) -%} +{%- for message in messages -%} + {%- if message['role'] == 'user' or message['role'] == 'developer' or (message['role'] == 'system' and loop.index0 > 0) -%} + {%- set last_user_idx.value = loop.index0 -%} + {%- endif -%} +{%- endfor -%} +{%- set state = namespace(in_user=false) -%} +{%- for message in messages -%} + {%- if message['role'] == 'tool' -%} + {%- set ns.has_tool_calls = true -%} + {%- endif -%} +{%- endfor -%} +{%- for message in messages -%} + {%- if message['role'] == 'system' and loop.index0 > 0 -%} + {%- set state.in_user = false -%} + {{- '<|System|>' + (message['content'] or '') -}} + {%- elif message['role'] == 'user' or message['role'] == 'developer' -%} + {%- if state.in_user -%} + {{- '\n\n' -}} + {%- else -%} + {{- '<|User|>' -}} + {%- set state.in_user = true -%} + {%- endif -%} + {{- message['content'] or '' -}} + {%- elif message['role'] == 'tool' -%} + {%- if state.in_user -%} + {{- '\n\n' -}} + {%- else -%} + {{- '<|User|>' -}} + {%- set state.in_user = true -%} + {%- endif -%} + {{- '' + (message['content'] or '') + '' -}} + {%- elif message['role'] == 'assistant' -%} + {%- set state.in_user = false -%} + {{- '<|Assistant|>' -}} + {%- set is_after_last_user = loop.index0 > last_user_idx.value -%} + {%- set keep_reasoning = thinking and ((not drop_thinking) or has_tools or is_after_last_user or ns.has_tool_calls) -%} + {%- if keep_reasoning -%} + {{- thinking_start_token -}} + {%- if message['reasoning_content'] is defined and message['reasoning_content'] -%} + {{- message['reasoning_content'] -}} + {%- endif -%} + {{- thinking_end_token -}} + {%- else -%} + {{- thinking_end_token -}} + {%- endif -%} + {%- if message['content'] is defined and message['content'] -%} + {{- message['content'] -}} + {%- endif -%} + {%- if message['tool_calls'] -%} + {{- '\n\n<|DSML| calls>\n' -}} + {%- for tool in message['tool_calls'] -%} + {%- set func = tool['function'] -%} + {{- '<|DSML| invoke name="' + func['name'] + '">\n' -}} + {%- set args = func['arguments'] -%} + {%- if args is string -%} + {%- set args = args | from_json -%} + {%- endif -%} + {%- for key, val in args.items() -%} + {%- if val is string -%} + {{- '<|DSML| parameter name="' + key + '" string="true">' + val + '\n' -}} + {%- else -%} + {{- '<|DSML| parameter name="' + key + '" string="false">' + (val | tojson) + '\n' -}} + {%- endif -%} + {%- endfor -%} + {%- if not args -%} + {{- '\n' -}} + {%- endif -%} + {{- '\n' -}} + {%- endfor -%} + {{- '' -}} + {%- endif -%} + {{- '<|end▁of▁sentence|>' -}} + {%- endif -%} +{%- endfor -%} +{%- if add_generation_prompt -%} + {{- '<|Assistant|>' -}} + {%- if thinking -%} + {{- thinking_start_token -}} + {%- else -%} + {{- thinking_end_token -}} + {%- endif -%} +{%- endif -%} diff --git a/docs/DEEPSEEK41.md b/docs/DEEPSEEK41.md index 01abfd8..cb659a9 100644 --- a/docs/DEEPSEEK41.md +++ b/docs/DEEPSEEK41.md @@ -60,9 +60,12 @@ into VRAM first; the engine updates it as it runs. build/bin/unbiased-strata --engine build/bin/strata-dsv41 \ -m /mnt/ds4/dsv41/IQ2_XXS/DeepSeek-V4.1-Flash-1017-GSQ-RCO-IQ2_XXS-00001-of-00004.gguf \ --host 0.0.0.0 --port 11434 --alias deepseek-v4.1-flash-1017-ista-iq2_xxs --sse-ping-interval 10 \ + --chat-template-file data/dsv41/chat_template-ista-1017.jinja --temp 1.0 --top-p 1.0 --min-p 0.0 --top-k 64 \ -- --pack /mnt/ds4/dsv41/IQ2_XXS/maya-ds4 --max-context 131072 --dspark auto --gpus 0 ``` +(`DS4_STORE_ZC=1 DS4_STORE_ZC_FRAC=100 DS4_READERS=16` in the environment on a slow CPU; see the measurements.) + - `-m` is shard 1: the front end reads the vocabulary and the chat template from it and no tensors. The pinned llama.cpp does not know `deepseek41`, so the vocabulary load takes the front end's `general.architecture` override path (USTRAT-6), as for any architecture it has not met. @@ -103,7 +106,51 @@ RAM 7.2 tokens/s; four R9700 (32 GB each) 31 tokens/s; "under about 40 GB of RAM | Model | IQ2_XXS set above, 2.00 bpw | | Context | 128K (`--max-context 131072`) | -Results: to be filled in at first light (decode and prefill tokens/s, VRAM split, load time). +Load: 35 s to healthy (the pack's files in the page cache or not; the RAM store reads its 28.8 GB in 9 s). VRAM: dense +5.86 GB, 4,700 of 15,360 experts (40.86 GB), caches 0.12 GB, 581 MiB free. 10,660 experts on the CPU lane, in place in +the files; the RAM store 41 GB (3,996 slots). DSpark stays unloaded under `--dspark auto` (the engine's rule: the experts +are far more than the card holds, so its verify rounds would gain nothing). + +Engine alone (`--ids ... --n 48`, caches dropped before each run), decode: + +| setting | tok/s | CPU lane, experts a token | of the per-token time | +| --- | ---: | ---: | --- | +| default | 2.00 | 66 | CPU lane 482 ms; store hits 63%, waits for the disk 79 ms; 317 MB read a token | +| `--ssd-cache 0` (page cache instead) | 1.99 | 66 | the same; 226 MB read a token | +| `--ssd-cache 16` | 1.93 | 66 | store hits 50%, waits 109 ms | +| `--mmap-experts off` (40 GB copied into RAM) | 1.95 | 66 | CPU lane 495 ms, no store | +| `DS4_STORE_ZC=1` (GPU computes the store's experts over PCIe, 50% visible) | 2.59 | 39 | CPU lane 353 ms; GPU 23 experts a token from the store | +| `DS4_STORE_ZC=1 DS4_STORE_ZC_FRAC=100` | 3.23 | 26 | CPU lane 258 ms; GPU 43 a token from the store | +| the same with `DS4_READERS=16` | 3.34 | 25 | CPU lane 246 ms; waits 79 ms | +| `DS4_STORE_ZC=1` with `--cpu-threads 12` | 2.14 | 44 | worse: the 12 vCPUs are 6 cores' SMT pairs | + +So on this machine the critical path is the CPU lane, not the disk: a Ryzen 7 1700's AVX2 cores dequantize IQ1_M / IQ2 +rows at roughly 10 ms an expert, and the disk's share of the token is 60 to 80 ms (the NVMe reads random 8 MiB at 3.5 GB/s; +the engine draws 230 to 340 MB a token). The zero-copy store is upstream's opt-in (slower than their CPU lane on a 5070 / +PCIe 5.0 box, and the lane that takes an expert depends on timing, so answers are not bit-reproducible); here it moves +two thirds of the CPU lane's work onto the A6000 over PCIe 3.0 x16 and is what the homelab unit runs with +(`Environment=DS4_STORE_ZC=1 DS4_STORE_ZC_FRAC=100 DS4_READERS=16`). + +Through the front end (first light, default store): a 42-token prompt read at 4.4 tok/s, 30 tokens written at 2.4 tok/s; a +329-token prompt at 17.8 tok/s and the answer at 2.5 tok/s; a 389-token answer streamed at 3.1 tok/s. Prefill is slow for +the same reason: a prompt touches most experts, and every CPU-lane expert it touches is dequantized on the CPU (and read +from the disk, 92 GB of them against 12 GB of page cache). With the zero-copy store (the unit's setting), through the front end: a 42-token prompt at 4.2 tok/s and 32 tokens +at 4.5 tok/s; a 400-token streamed answer at 6.9 tok/s (the store warm); a 319-token prompt at 18.4 tok/s and 85 tokens at +5.3 tok/s; the CPU lane took 2 to 3% of the expert uses. Healthy 50 s after start. + +## The chat template + +The ISTA GGUF carries an older DeepSeek template: it spells the tool tags `<|DSML|tool_calls>`, `<|DSML|invoke`, +`<|DSML|parameter` through a `dsml_token` variable, while the 1017 model writes `<|DSML| calls>`, `<|DSML| invoke`, +`<|DSML| parameter` (a space, and `calls`). With the GGUF's template llama.cpp does not select its V4.1 parser (it looks +for the literal `|DSML| calls>` in the template source) and a tool call came back as text in the content. DeepSeek's +official template and Unsloth's port (upstream Strata's fixture) both use Jinja's `dict()`, which the pinned llama.cpp's +minja does not have ("Callee is not a function: got Undefined (hint: 'dict')", the server exits). So +`data/dsv41/chat_template-ista-1017.jinja` is the GGUF's own template with the five tag spellings written out as literals, +nothing else changed; pass it with `--chat-template-file` before the `--`. With it: `finish_reason: tool_calls`, arguments +`{"city":"Paris","unit":"c"}`, reasoning split into `reasoning_content`, thinking on by default. + + ## Tests