|
| 1 | +# Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +from nemo.collections.common.prompts import PromptFormatter |
| 15 | +from nemo.collections.common.prompts.nemotron3p5 import Nemotron3p5PromptFormatter |
| 16 | + |
| 17 | + |
| 18 | +def test_nemotron3p5_is_registered(): |
| 19 | + assert PromptFormatter.resolve("nemotron3p5") is Nemotron3p5PromptFormatter |
| 20 | + |
| 21 | + |
| 22 | +def test_nemotron3p5_training_basic(bpe_tokenizer_with_think): |
| 23 | + formatter = Nemotron3p5PromptFormatter(bpe_tokenizer_with_think) |
| 24 | + ans = formatter.encode_dialog( |
| 25 | + [ |
| 26 | + {"role": "user", "slots": {"message": "TEST"}}, |
| 27 | + {"role": "assistant", "slots": {"message": "TEST"}}, |
| 28 | + ] |
| 29 | + ) |
| 30 | + |
| 31 | + assert set(ans) == {"input_ids", "context_ids", "answer_ids", "mask"} |
| 32 | + assert ( |
| 33 | + bpe_tokenizer_with_think.ids_to_text(ans["input_ids"].tolist()) |
| 34 | + == "<|im_start|>system\n<|im_end|>\n <|im_start|>user\nTEST<|im_end|>\n " |
| 35 | + "<|im_start|>assistant\n<think></think>TEST<|im_end|>\n" |
| 36 | + ) |
| 37 | + assert ans["mask"].tolist() == [False] * len(ans["context_ids"]) + [True] * len(ans["answer_ids"]) |
| 38 | + |
| 39 | + |
| 40 | +def test_nemotron3p5_inference_generation_prompt(bpe_tokenizer_with_think): |
| 41 | + formatter = Nemotron3p5PromptFormatter(bpe_tokenizer_with_think) |
| 42 | + thinking = formatter.encode_dialog( |
| 43 | + [{"role": "user", "slots": {"message": "TEST"}}], enable_thinking=True |
| 44 | + ) |
| 45 | + no_thinking = formatter.encode_dialog( |
| 46 | + [{"role": "user", "slots": {"message": "TEST"}}], enable_thinking=False |
| 47 | + ) |
| 48 | + |
| 49 | + assert ( |
| 50 | + bpe_tokenizer_with_think.ids_to_text(thinking["input_ids"].tolist()) |
| 51 | + == "<|im_start|>system\n<|im_end|>\n <|im_start|>user\nTEST<|im_end|>\n " |
| 52 | + "<|im_start|>assistant\n<think>\n" |
| 53 | + ) |
| 54 | + assert ( |
| 55 | + bpe_tokenizer_with_think.ids_to_text(no_thinking["input_ids"].tolist()) |
| 56 | + == "<|im_start|>system\n<|im_end|>\n <|im_start|>user\nTEST<|im_end|>\n " |
| 57 | + "<|im_start|>assistant\n<think></think>" |
| 58 | + ) |
0 commit comments