Datasets:
Upload tools/nemotron_fmt.py with huggingface_hub
Browse files- tools/nemotron_fmt.py +84 -0
tools/nemotron_fmt.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Chat rendering for NVIDIA-Nemotron-3.5-Lightning-30B-A3B.
|
| 2 |
+
|
| 3 |
+
Same contract as ``glimmer_fmt``: ``render()`` turns a pool dialogue record into
|
| 4 |
+
the exact byte string the model sees at inference.
|
| 5 |
+
|
| 6 |
+
Unlike ``glimmer_fmt`` this is not a hand port. Nemotron's ``chat_template.jinja``
|
| 7 |
+
is driven directly through ``transformers.apply_chat_template``, which is the
|
| 8 |
+
reference implementation, so byte equality is true by construction rather than
|
| 9 |
+
by assertion.
|
| 10 |
+
|
| 11 |
+
Differences from Muse-Glimmer that matter for the corpus:
|
| 12 |
+
* special tokens are ``<|im_start|>`` / ``<|im_end|>`` / ``<think>`` /
|
| 13 |
+
``<tool_call>``, not ``<|start|>`` / ``<|message|>`` / ``<|eot|>``
|
| 14 |
+
* ``add_bos_token`` is false in tokenizer_config, so documents carry no BOS
|
| 15 |
+
* the template takes no date arguments, so builds are reproducible with no
|
| 16 |
+
pinning needed (Glimmer needed current_date to avoid strftime_now)
|
| 17 |
+
* there is no reasoning_strength dial; the template has a boolean
|
| 18 |
+
``enable_thinking``, so the pool's four strengths collapse onto it
|
| 19 |
+
"""
|
| 20 |
+
from __future__ import annotations
|
| 21 |
+
|
| 22 |
+
import os
|
| 23 |
+
from typing import Any, Sequence
|
| 24 |
+
|
| 25 |
+
# Kept for interface parity with glimmer_fmt; Nemotron's template uses neither.
|
| 26 |
+
DEFAULT_REASONING = "high"
|
| 27 |
+
DEFAULT_KNOWLEDGE_CUTOFF = None
|
| 28 |
+
|
| 29 |
+
MODEL_DIR = os.environ.get("NEMOTRON_MODEL_DIR", "/root/work/nemo-bf16")
|
| 30 |
+
|
| 31 |
+
# Reasoning strengths in the pool that mean "do not emit a thinking channel".
|
| 32 |
+
_NO_THINK = {"none", "off", "no", "disabled", "low0"}
|
| 33 |
+
|
| 34 |
+
_tok = None
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def _tokenizer():
|
| 38 |
+
global _tok
|
| 39 |
+
if _tok is None:
|
| 40 |
+
from transformers import AutoTokenizer # heavy; imported lazily
|
| 41 |
+
_tok = AutoTokenizer.from_pretrained(MODEL_DIR, trust_remote_code=False)
|
| 42 |
+
if not _tok.chat_template:
|
| 43 |
+
raise SystemExit(f"no chat_template found in {MODEL_DIR}")
|
| 44 |
+
return _tok
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def _clean(messages: Sequence[dict]) -> list[dict]:
|
| 48 |
+
"""Drop Glimmer-only per-message keys the Nemotron template does not read.
|
| 49 |
+
|
| 50 |
+
``recipient``/``end_turn``/``channel`` are Muse-Glimmer routing fields.
|
| 51 |
+
``reasoning_content`` and ``tool_calls`` are kept - Nemotron's template
|
| 52 |
+
consumes both.
|
| 53 |
+
"""
|
| 54 |
+
keep = {"role", "content", "reasoning_content", "tool_calls",
|
| 55 |
+
"tool_call_id", "name"}
|
| 56 |
+
out = []
|
| 57 |
+
for m in messages:
|
| 58 |
+
d = {k: v for k, v in m.items() if k in keep and v is not None}
|
| 59 |
+
d.setdefault("content", "")
|
| 60 |
+
out.append(d)
|
| 61 |
+
return out
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def render(messages: Sequence[dict],
|
| 65 |
+
tools: Sequence[dict] | None = None,
|
| 66 |
+
reasoning_strength: str | None = None,
|
| 67 |
+
knowledge_cutoff: Any = None, # unused, interface parity
|
| 68 |
+
current_date: Any = None, # unused, interface parity
|
| 69 |
+
namespace_descriptions: Any = None, # unused, Glimmer-only
|
| 70 |
+
add_generation_prompt: bool = False,
|
| 71 |
+
add_bos: bool = False) -> str:
|
| 72 |
+
tok = _tokenizer()
|
| 73 |
+
enable_thinking = str(reasoning_strength or DEFAULT_REASONING).lower() not in _NO_THINK
|
| 74 |
+
|
| 75 |
+
text = tok.apply_chat_template(
|
| 76 |
+
_clean(messages),
|
| 77 |
+
tools=list(tools) if tools else None,
|
| 78 |
+
enable_thinking=enable_thinking,
|
| 79 |
+
add_generation_prompt=add_generation_prompt,
|
| 80 |
+
tokenize=False,
|
| 81 |
+
)
|
| 82 |
+
if add_bos and tok.bos_token and not text.startswith(tok.bos_token):
|
| 83 |
+
text = tok.bos_token + text
|
| 84 |
+
return text
|