nemotron 3.5 rerank 8b implementation

This commit is contained in:
Gaspa79 committed 2026-10-30 09:47:42 -03:00
1 parent d773992abc
commit 74e7fddd79
35 files changed
+1788 -90

No files matched your search

+7
View File
@@ -23,3 +23,10 @@ python3 tests/mknemotron35inputgolden.py --tokenizer /path/to/checkpoint/tokeniz
The generator needs Jinja2, Hugging Face tokenizers, and the checked-in vision fixture. The
golden records the source template, full tokenizer, and subset SHA-256 hashes. It runs no model
weights and is never called by CMake or CTest.
`rerank-template.jinja` is the unchanged template from
`nvidia/Nemotron-3.5-Rerank-8B-BF16` revision
`f88f8d9104726747791283ab156d2fcdb29e90a7`, under the same retained OpenMDW-1.1
license. It supplies the independent query/document rendering oracle to the
manually run `tests/mknemotron35rerankgolden.py`; the engine does not execute
remote model code. The tiny scoring head is seeded synthetic test data.
@@ -0,0 +1,105 @@
{%- if messages | length == 2 and messages[0]['role'] == 'query' and messages[1]['role'] == 'document' -%}
{# Render the checkpoint's query/document training format without BOS/EOS. #}
{%- macro text_content(content) -%}
{%- if content is string -%}
{{- content | replace('[', '[‌') | replace('<', '<‌') -}}
{%- else -%}
{%- set ns = namespace(first=true) -%}
{%- for block in content -%}
{%- if block['type'] == 'text' -%}
{%- if not ns.first -%}{{- '\n\n' -}}{%- endif -%}
{{- block['text'] | replace('[', '[‌') | replace('<', '<‌') -}}
{%- set ns.first = false -%}
{%- elif block['type'] not in ['image', 'image_url'] -%}
{{- raise_exception('Only text and image documents are supported') -}}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- endmacro -%}
{%- if messages | length != 2 or messages[0]['role'] != 'query' or messages[1]['role'] != 'document' -%}
{{- raise_exception('Reranking requires a query followed by a document') -}}
{%- endif -%}
{%- set ns = namespace(images=0) -%}
{%- if messages[0]['content'] is not string -%}
{%- for block in messages[0]['content'] -%}
{%- if block['type'] != 'text' -%}{{- raise_exception('Queries must be text') -}}{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- if messages[1]['content'] is not string -%}
{%- for block in messages[1]['content'] -%}
{%- if block['type'] in ['image', 'image_url'] -%}{%- set ns.images = ns.images + 1 -%}{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- if ns.images > 1 -%}{{- raise_exception('At most one image per document is supported') -}}{%- endif -%}
{%- set rendered -%}
{%- if ns.images -%}{{- '[IMG] ' -}}{%- endif -%}
{{- 'query: ' -}}{{- text_content(messages[0]['content']) -}}
{{- '\n\npassage: ' -}}{{- text_content(messages[1]['content']) -}}
{%- endset -%}
{{- rendered | trim if ns.images else rendered -}}
{%- else -%}
{# nemo-mistral-retrieval-v1
Derived from the Mistral Ministral-3 instruct chat template render_content
macro and its image-before-text canonicalization. Retrieval adaptation:
system content is the retrieval prefix; generation wrappers and BOS/EOS are
deliberately omitted so message inference matches training token for token.
#}
{%- macro mark_user_text(text) -%}
{{- text | replace("[", "[‌") | replace("<", "<‌") -}}
{%- endmacro -%}
{%- macro render_text_content(content, context_name) -%}
{%- if content is string -%}
{{- content -}}
{%- elif content is sequence -%}
{%- set ns = namespace(has_output=false) -%}
{%- for block in content -%}
{%- if block["type"] == "text" -%}
{%- if ns.has_output %}{{ "\n\n" }}{% endif -%}
{{- block["text"] -}}
{%- set ns.has_output = true -%}
{%- else -%}
{{- raise_exception(context_name + " may contain only text blocks") -}}
{%- endif -%}
{%- endfor -%}
{%- else -%}
{{- raise_exception(context_name + " must be a string or a list of text blocks") -}}
{%- endif -%}
{%- endmacro -%}
{%- macro render_user_content(content) -%}
{%- if content is string -%}
{{- mark_user_text(content) -}}
{%- elif content is sequence -%}
{%- set ns = namespace(has_output=false) -%}
{%- for block in content -%}
{%- if block["type"] in ["image", "image_url"] -%}
{%- if ns.has_output %}{{ " " }}{% endif -%}
{{- "[IMG]" -}}
{%- set ns.has_output = true -%}
{%- endif -%}
{%- endfor -%}
{%- for block in content -%}
{%- if block["type"] == "text" -%}
{%- if ns.has_output %}{{ " " }}{% endif -%}
{{- mark_user_text(block["text"]) -}}
{%- set ns.has_output = true -%}
{%- elif block["type"] not in ["image", "image_url"] -%}
{{- raise_exception("User content supports only text and image blocks") -}}
{%- endif -%}
{%- endfor -%}
{%- else -%}
{{- raise_exception("User content must be a string or a list of text and image blocks") -}}
{%- endif -%}
{%- endmacro -%}
{%- if messages | length == 1 and messages[0]["role"] == "user" -%}
{{- render_user_content(messages[0]["content"]) -}}
{%- elif messages | length == 2
and messages[0]["role"] == "system"
and messages[1]["role"] == "user" -%}
{{- render_text_content(messages[0]["content"], "System content") -}}
{{- " " -}}
{{- render_user_content(messages[1]["content"]) -}}
{%- else -%}
{{- raise_exception("Retrieval inputs require one user message with an optional leading system prefix") -}}
{%- endif -%}
{%- endif -%}