-
Notifications
You must be signed in to change notification settings - Fork 8
Fix mbridge inference and use dynamic inference from mcore #627
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
Open
oyilmaz-nvidia
wants to merge
5
commits into
main
Choose a base branch
from
remove-direct-nemo-imports-in-inference
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c4dcc44
inference: remove direct nemo imports, add InferenceWrapperConfig for…
oyilmaz-nvidia ce646ce
inference: fix InferenceWrapperConfig and add buffer_size_gb support
oyilmaz-nvidia 3b99d12
Update mbridge commit
oyilmaz-nvidia 21862a6
Fix megatron-bridge install
chtruong814 c5fdd40
Set cryptography to < 47
chtruong814 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
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,188 @@ | ||
| # Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """NeMo utility code copied from the NeMo project. | ||
|
|
||
| Standalone utilities (MCoreTokenizerWrappper, checkpoint path helpers) are | ||
| copied directly and have no dependency on the nemo package. | ||
|
|
||
| Complex types that are tightly coupled to NeMo's class hierarchy and | ||
| serialization system (GPTConfig, T5Config, io, set_modelopt_spec_if_exists_in_ckpt) | ||
| are re-exported here from the nemo package so that inference_base.py and | ||
| tron_utils.py do not need to import from nemo directly. | ||
|
|
||
| Sources: | ||
| - MCoreTokenizerWrappper : nemo/collections/llm/inference/base.py | ||
| - ckpt_to_dir, | ||
| idempotent_path_append, | ||
| ckpt_to_context_subdir : nemo/lightning/ckpt_utils.py | ||
| - ckpt_to_weights_subdir : nemo/lightning/io/pl.py | ||
| - constants : nemo/lightning/ckpt_utils.py | ||
| """ | ||
|
|
||
| import inspect | ||
| from pathlib import Path | ||
| from typing import Any, Union | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Constants (from nemo.lightning.ckpt_utils) | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| # NeMo-2 checkpoint structure: | ||
| # <ckpt_dir>/weights/ – model weights | ||
| # <ckpt_dir>/context/ – hyper-parameters / IO context | ||
| WEIGHTS_PATH: str = "weights" | ||
| CONTEXT_PATH: str = "context" | ||
| ADAPTER_META_FILENAME: str = "adapter_metadata.json" | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Checkpoint path utilities (simplified from nemo.lightning.ckpt_utils and | ||
| # nemo.lightning.io.pl – AdapterPath and MultiStorageClient branches removed | ||
| # because they are not required for basic NeMo-2 inference). | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def ckpt_to_dir(filepath: Union[str, Path]) -> Path: | ||
| """Return the checkpoint directory path for a given filepath. | ||
|
|
||
| PTL treats checkpoints as ``.ckpt`` files. This helper strips the | ||
| extension (appending it first when absent) and returns a :class:`Path` | ||
| suitable for use as a distributed-checkpoint directory. | ||
|
|
||
| Copied from ``nemo.lightning.ckpt_utils.ckpt_to_dir`` with the | ||
| ``AdapterPath`` and ``MultiStorageClient`` branches removed. | ||
| """ | ||
| filepath = Path(filepath) | ||
|
|
||
| if filepath.suffix != ".ckpt": | ||
| filepath = filepath.with_suffix(filepath.suffix + ".ckpt") | ||
|
|
||
| assert filepath.suffix == ".ckpt", f"filepath: {filepath} must have .ckpt extension" | ||
|
|
||
| # Return path whose name is the original filepath without the .ckpt extension. | ||
| return filepath.with_name(filepath.stem) | ||
|
|
||
|
|
||
| def idempotent_path_append(base_dir: Union[str, Path], suffix: str) -> Path: | ||
| """Append *suffix* to *base_dir* only when it is not already the last component. | ||
|
|
||
| Copied from ``nemo.lightning.ckpt_utils.idempotent_path_append`` with the | ||
| ``AdapterPath`` and ``MultiStorageClient`` branches removed. | ||
| """ | ||
| base_dir = Path(base_dir) | ||
| if base_dir.parts[-1] != suffix: | ||
| base_dir = base_dir / suffix | ||
| return base_dir | ||
|
|
||
|
|
||
| def ckpt_to_context_subdir(filepath: Union[str, Path]) -> Path: | ||
| """Return the ``context`` sub-directory of a NeMo-2 checkpoint. | ||
|
|
||
| Copied from ``nemo.lightning.ckpt_utils.ckpt_to_context_subdir``. | ||
| """ | ||
| base_dir = ckpt_to_dir(filepath=filepath) | ||
| return idempotent_path_append(base_dir, CONTEXT_PATH) | ||
|
|
||
|
|
||
| def ckpt_to_weights_subdir(filepath: Union[str, Path], is_saving: bool) -> Path: | ||
| """Return the ``weights`` sub-directory of a NeMo-2 checkpoint. | ||
|
|
||
| Copied from ``nemo.lightning.io.pl.ckpt_to_weights_subdir`` with the | ||
| ``AdapterPath`` branch removed. | ||
| """ | ||
| filepath = ckpt_to_dir(filepath=filepath) | ||
| base_dir = filepath | ||
|
|
||
| if base_dir.parts[-1] != WEIGHTS_PATH: | ||
| maybe_base_dir = base_dir / WEIGHTS_PATH | ||
| if maybe_base_dir.is_dir() or is_saving: | ||
| base_dir = maybe_base_dir | ||
|
|
||
| if is_saving: | ||
| assert base_dir.parts[-1] == WEIGHTS_PATH | ||
| assert base_dir.parent == filepath | ||
|
|
||
| return base_dir | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # MCoreTokenizerWrappper (from nemo.collections.llm.inference.base) | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| class MCoreTokenizerWrappper: | ||
| """Thin wrapper that adapts a NeMo tokenizer to the MCore generate API. | ||
|
|
||
| MCore's generate pipeline expects ``tokenizer.detokenize``, | ||
| ``tokenizer.tokenize``, ``tokenizer.bos``, and ``tokenizer.pad`` – | ||
| this wrapper maps those calls to the corresponding NeMo tokenizer | ||
| methods/properties. | ||
|
|
||
| Copied verbatim from ``nemo.collections.llm.inference.base.MCoreTokenizerWrappper``. | ||
| """ | ||
|
|
||
| def __init__(self, tokenizer, vocab_size=None): | ||
| self.tokenizer = tokenizer | ||
| self.eod = tokenizer.eod | ||
| self.vocab_size = vocab_size or tokenizer.vocab_size | ||
|
|
||
| def detokenize(self, tokens, remove_special_tokens=False): | ||
| """Detokenize *tokens* into a string.""" | ||
| if "remove_special_tokens" in inspect.signature(self.tokenizer.ids_to_text).parameters: | ||
| return self.tokenizer.ids_to_text(tokens, remove_special_tokens) | ||
| return self.tokenizer.ids_to_text(tokens) | ||
|
|
||
| def tokenize(self, prompt): | ||
| """Tokenize *prompt* into a list of token IDs.""" | ||
| return self.tokenizer.text_to_ids(prompt) | ||
|
|
||
| @property | ||
| def additional_special_tokens_ids(self): | ||
| """IDs of additional special tokens.""" | ||
| return self.tokenizer.additional_special_tokens_ids | ||
|
|
||
| @property | ||
| def bos(self): | ||
| """Beginning-of-sequence token ID.""" | ||
| return self.tokenizer.bos_id | ||
|
|
||
| @property | ||
| def pad(self): | ||
| """Padding token ID.""" | ||
| return self.tokenizer.pad_id | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # NeMo complex types | ||
| # | ||
| # GPTConfig, T5Config, io, and set_modelopt_spec_if_exists_in_ckpt are | ||
| # deeply coupled to NeMo's class hierarchy and serialization system. | ||
| # Checkpoints saved by NeMo contain instances of these exact classes, so | ||
| # they must originate from the nemo package to preserve isinstance() | ||
| # compatibility. They are re-exported here so that inference_base.py and | ||
| # tron_utils.py do not need to import from nemo directly. | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| try: | ||
| from nemo.collections.llm.gpt.model.base import GPTConfig | ||
| from nemo.collections.llm.modelopt import set_modelopt_spec_if_exists_in_ckpt | ||
| from nemo.collections.llm.t5.model.t5 import T5Config | ||
| from nemo.lightning import io | ||
|
|
||
| HAVE_NEMO = True | ||
| except (ImportError, ModuleNotFoundError): | ||
| GPTConfig = Any | ||
| T5Config = Any | ||
| io = None | ||
| set_modelopt_spec_if_exists_in_ckpt = None | ||
| HAVE_NEMO = False |
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@oyilmaz-nvidia do we want to move nemo 2.0 functionality here ? Can't we just remove it since nemo 2.0 deployment code is already removed anyway
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So that's for importing the nemo and I'll have another PR to remove it. It's actually a lot more challenging than just adding here.