This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/deploy/chat-templates/unlimited-ocr-parser.jinja
T
Juup 859efc7208 review: apply the Unlimited-OCR code-review findings
The 2a1ca01..f308216 range's 45 review findings (12 performance, 33 quality)
are applied: ABI 23 for the RadBatch append, the fused q|k|v projection, the
batched patch-embed GEMM and pitched layout copies, the sam_attn w-dot hoist
with an alignment-gated vector row path and NaN poisoning for malformed
segments, the fused quick_gelu act, and the R-SWA/scheduler/drafting gates.

Quality items: stale comments corrected, env reads hoisted, the ngram_window
negative refusal, the parts-form chat template instruction, the whitelist
fallback warning, and the segmented R-SWA refusal of the fixed/runtime window
mix.

The 3.2 MB generated resample header becomes a 9.6 MB little-endian blob
(tests/data/golden/uocr_resample_golden.bin) read by a small loader;
resample_golden.py emits and round-trip-verifies it.

One perf-med item (AVX2 bicubic passes/LUT patch staging in the processor) is
left as the follow-up the review itself recommends; the code says why.

Verified: ninja -C build-review -k 0; host ctest 89/89; GPU r4d_selftest
0 failures, kbench/kbench_moe and the card tests pass.
2026-10-20 07:06:18 +00:00

65 lines
3.2 KiB
Django/Jinja
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{#- Unlimited-OCR is a single-shot document parser: it expects the picture marker(s) followed by
the reference instruction, e.g. "<__media__>document parsing." (the server replaces each
marker with a picture's token run). Chat clients send the picture plus arbitrary text, often
nothing, and resend earlier turns including their pictures. A parser without its start
instruction free-runs like a language model and first emits prose (the Chinese/Russian
preamble seen from LibreChat), and text from earlier turns or a system message would be
concatenated into the prompt. This template keeps every picture marker (the server requires
the rendered prompt to account for every picture in the request) and the final user turn's
text, drops other text, and supplies the reference instruction when the final turn carries
none, for any number of pictures: one is "document parsing.", several are "Multi page
parsing." -#}
{%- set ns = namespace(last=none, at=-1, media=0, text=false) -%}
{%- for message in messages -%}
{%- if message['role'] == 'user' -%}
{%- set ns.last = message -%}
{%- set ns.at = loop.index0 -%}
{%- endif -%}
{%- endfor -%}
{%- if ns.last is not none -%}
{%- if ns.last['content'] is string -%}
{%- for piece in ns.last['content'].split('<__media__>') -%}
{%- if not loop.last -%}{%- set ns.media = ns.media + 1 -%}{%- endif -%}
{%- if ((piece | replace('', '')) | trim) != '' -%}{%- set ns.text = true -%}{%- endif -%}
{%- endfor -%}
{%- else -%}
{#- The OpenAI parts form counts the same way: media from the marker parts, text from any
non-empty text part (a client's U+FFFC object placeholder is whitespace). -#}
{%- for part in ns.last['content'] -%}
{%- if part['type'] == 'media_marker' -%}
{%- set ns.media = ns.media + 1 -%}
{%- elif part['type'] == 'text' and ((part['text'] | default('', true) | replace('', '')) | trim) != '' -%}
{%- set ns.text = true -%}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- endif -%}
{%- for message in messages -%}
{%- set mi = loop.index0 -%}
{%- if message['content'] is string -%}
{%- set c = message['content'] -%}
{%- if mi == ns.at -%}
{{- c | replace('', '') -}}
{%- else -%}
{%- for piece in c.split('<__media__>') -%}
{%- if not loop.last -%}<__media__>{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- else -%}
{%- for part in message['content'] -%}
{%- if part['type'] == 'media_marker' -%}<__media__>
{%- elif part['type'] == 'text' and mi == ns.at and ns.last is not none -%}{{- part['text'] -}}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- endfor -%}
{#- The instruction block serves both content forms: ns.media/ns.text above scanned the final
turn whichever it was. Output is whitespace-tight: every tag trims, the newline after
"Multi page parsing." is eaten by the next tag's `{%-`, and the rendered prompt ends with
the instruction verbatim -- keep the trims when reformatting this block. -#}
{%- if ns.last is not none and not ns.text -%}
{%- if ns.media > 1 -%}Multi page parsing.
{%- elif ns.media == 1 -%}document parsing.
{%- endif -%}
{%- endif -%}