Repository navigation
fix(phi4): stage engine plans and export AIPerf tokenizer #1764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
yizhuoz004
merged 4 commits into
NVIDIA:main
from
yizhuoz004:aiperf-stage5-phi-tokenizer
Oct 17, 2026
+164
−4
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8a14ef0
test(phi4): export the checkpoint tokenizer for AIPerf
yizhuoz004 f7b1c22
fix(phi4): preserve the exported chat template
yizhuoz004 37d982b
fix(phi4): release each staged engine before building next
yizhuoz004 eed140a
test(phi4): reduce reference allocator fragmentation
yizhuoz004 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
families/phi4_multimodal/tests/aiperf/prepare_tokenizer.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| #!/usr/bin/env python3 | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """Export Phi4's standard tokenizer without its model-only remote configuration.""" | ||
|
|
||
| import argparse | ||
| import importlib.metadata | ||
| import json | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| def export_tokenizer(model_dir: Path, output_dir: Path) -> None: | ||
| from transformers import AutoTokenizer, GPT2TokenizerFast, PreTrainedTokenizerFast | ||
|
|
||
| config = json.loads((model_dir / "config.json").read_text()) | ||
| if config.get("model_type") not in {"phi4mm", "phi4_multimodal", "phi4multimodal"}: | ||
| raise ValueError("model directory must contain the Phi4 multimodal checkpoint") | ||
| tokenizer = GPT2TokenizerFast.from_pretrained(model_dir, local_files_only=True) | ||
| probes = ("hello", "What is the main object in this image? Answer in one word.", | ||
| "<|user|>\n<|image_1|>\nQuestion?<|end|>\n<|assistant|>\n", "找到图片中的狗。") | ||
| identities = [ | ||
| {"text": text, "add_special_tokens": special, | ||
| "token_ids": tokenizer.encode(text, add_special_tokens=special)} | ||
| for text in probes for special in (False, True) | ||
| ] | ||
| vocabulary = tokenizer.get_vocab() | ||
| # Keep the checkpoint's GPT-4o regex/backend unchanged. A model-specific | ||
| # GPT2 wrapper in a newer client can rebuild it with a different regex. | ||
| exported = PreTrainedTokenizerFast( | ||
| tokenizer_object=tokenizer.backend_tokenizer, | ||
| model_max_length=tokenizer.model_max_length, | ||
| padding_side=tokenizer.padding_side, | ||
| truncation_side=tokenizer.truncation_side, | ||
| clean_up_tokenization_spaces=tokenizer.clean_up_tokenization_spaces, | ||
| chat_template=tokenizer.chat_template, | ||
| **tokenizer.special_tokens_map, | ||
| ) | ||
| output_dir.mkdir(parents=True, exist_ok=False) | ||
| exported.save_pretrained(output_dir) | ||
| restored = AutoTokenizer.from_pretrained(output_dir, use_fast=True, local_files_only=True) | ||
| if restored.get_vocab() != vocabulary: | ||
| raise RuntimeError("exported tokenizer changed the vocabulary") | ||
| if restored.chat_template != tokenizer.chat_template: | ||
| raise RuntimeError("exported tokenizer changed the chat template") | ||
| for probe in identities: | ||
| if restored.encode(probe["text"], add_special_tokens=probe["add_special_tokens"]) != probe["token_ids"]: | ||
| raise RuntimeError("exported tokenizer changed token IDs or framing") | ||
| audit = {"source": str(model_dir.resolve()), | ||
| "transformers": importlib.metadata.version("transformers"), | ||
| "tokenizer_class": type(tokenizer).__name__, "probes": identities} | ||
| (output_dir / "trtmc-tokenizer-export.json").write_text(json.dumps(audit, indent=2) + "\n") | ||
|
|
||
|
|
||
| def main() -> None: | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument("--model-dir", type=Path, required=True, help="local Phi4 multimodal checkpoint") | ||
| parser.add_argument("--output-dir", type=Path, required=True, help="new directory for client tokenizer files") | ||
| args = parser.parse_args() | ||
| export_tokenizer(args.model_dir, args.output_dir) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """Resource configuration for the original FP32 eager family reference.""" | ||
|
|
||
| import os | ||
|
|
||
|
|
||
| def pytest_configure(config): | ||
| # The eager reference's varying allocations fragment a nearly full GPU. | ||
| # Keep its precision/operators unchanged and preserve caller configuration. | ||
| if "PYTORCH_ALLOC_CONF" not in os.environ: | ||
| os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """Keep serialized engines out of RAM while building the next network.""" | ||
|
|
||
| import gc | ||
| import json | ||
| from types import SimpleNamespace | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
| def test_each_plan_is_staged_and_released_before_the_next_build(tmp_path, monkeypatch): | ||
| pytest.importorskip("tensorrt") | ||
| from families.phi4_multimodal import model | ||
| from families.phi4_multimodal.edge_llm import dispatch | ||
| from tensorrt_model_connect.bundle_writer import BundleWriter | ||
|
|
||
| live = set() | ||
| built = [] | ||
|
|
||
| class Plan(bytes): | ||
| def __new__(cls, role): | ||
| value = super().__new__(cls, role.encode()) | ||
| value.role = role | ||
| live.add(role) | ||
| return value | ||
|
|
||
| def __del__(self): | ||
| live.remove(self.role) | ||
|
|
||
| def next_plan(role): | ||
| gc.collect() | ||
| assert not live, f"previous serialized engine retained during {role}: {live}" | ||
| built.append(role) | ||
| return Plan(role) | ||
|
|
||
| class Model: | ||
| @staticmethod | ||
| def load_weights(*_args): | ||
| return {} | ||
|
|
||
| @staticmethod | ||
| def build_engine(config, *_args, **_kwargs): | ||
| return next_plan(config.raw["_decoder_engine_role"]) | ||
|
|
||
| @staticmethod | ||
| def build_vision_engine(*_args, **_kwargs): | ||
| return next_plan("vision") | ||
|
|
||
| @staticmethod | ||
| def get_vl_config(_config): | ||
| return {"image_token_id": 3, "vision_output_dim": 4} | ||
|
|
||
| monkeypatch.setattr(model, "_Phi4MultimodalModel", Model) | ||
| monkeypatch.setattr(model, "_tokenizer_runtime_contract", lambda _directory: {}) | ||
| monkeypatch.setattr(dispatch, "build", lambda request, writer, native: native(request, writer)) | ||
| (tmp_path / "config.json").write_text(json.dumps({ | ||
| "model_type": "phi4mm", "hidden_size": 4, "num_hidden_layers": 1, | ||
| "vocab_size": 8, "num_attention_heads": 1, "num_key_value_heads": 1, | ||
| })) | ||
| request = SimpleNamespace( | ||
| mtp_seq_len=None, dynamic_kv_cache=False, image_height=None, image_width=None, | ||
| video_num_frames=None, max_batch_size=1, context_parallel_size=1, | ||
| task="vision_language_generation", tensor_parallel_size=1, quantization=None, | ||
| fp32_layers=(), model_dir=tmp_path, precision="fp16", max_sequence_length=768, | ||
| verbose=False, backend="tensorrt", | ||
| ) | ||
| writer = BundleWriter(tmp_path / "model.bundle") | ||
| try: | ||
| model.build(request, writer) | ||
| writer.finish() | ||
| finally: | ||
| writer.abort() | ||
| gc.collect() | ||
| assert built == ["prefill", "decode", "vision"] | ||
| assert not live | ||
| assert (tmp_path / "model.bundle").is_file() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.