Skip to content

Commit 1e3a42a

Browse files
roclarkmonica-sekoyan
authored andcommitted
Safely import optional python packages (#13936)
* Safely import nvidia-resiliency-ext package The nvidia-resiliency-ext package is not compatible with Windows/MacOS currently and cannot be installed directly on those platforms. Given the number of nested imports in the nemo.collections.llm API, it will try and automatically import the resliency package even if not used. Safely importing the package allows all modules to be imported without blocking on the resiliency package for incompatible platforms. This opens up greater support on more platforms. If the nvidia-resiliency-ext package is needed when these lines are called, it will throw an import error only when directly called. Signed-Off-By: Robert Clark <[email protected]> * Safely import modelopt algorithms The default modelopt model transforms rely on the modelopt package being installed which is not available on all platforms. This module gets auto-imported when importing the nemo.collections.llm module and will fail on platforms that don't support modelopt, such as Windows and MacOS, even if the package isn't used. Safely importing the package should allow the toolkit to be runnable on both Windows and MacOS in a minimal state, which increases its functionality, especially while paired with NeMo-Run. Signed-Off-By: Robert Clark <[email protected]> * Apply isort and black reformatting Signed-off-by: roclark <[email protected]> --------- Signed-off-by: Robert Clark <[email protected]> Signed-off-by: roclark <[email protected]>
1 parent 1095d20 commit 1e3a42a

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

nemo/collections/llm/modelopt/speculative/model_transform.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,19 @@
1616

1717
from nemo.collections.llm import GPTModel
1818
from nemo.utils import logging
19-
from nemo.utils.import_utils import safe_import
19+
from nemo.utils.import_utils import UnavailableError, safe_import
2020
from nemo.utils.model_utils import unwrap_model
2121

2222
mto, HAVE_MODELOPT = safe_import("modelopt.torch.opt")
2323
mtsp, _ = safe_import("modelopt.torch.speculative")
2424

25-
26-
ALGORITHMS = {
27-
"eagle3": mtsp.EAGLE3_DEFAULT_CFG,
28-
# more TBD
29-
}
25+
try:
26+
ALGORITHMS = {
27+
"eagle3": mtsp.EAGLE3_DEFAULT_CFG,
28+
# more TBD
29+
}
30+
except UnavailableError:
31+
ALGORITHMS = {}
3032

3133

3234
def apply_speculative_decoding(model: nn.Module, algorithm: str = "eagle3") -> nn.Module:

nemo/lightning/nemo_logger.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
import lightning.pytorch as pl
2323
from lightning.pytorch.callbacks.model_checkpoint import ModelCheckpoint as PTLModelCheckpoint
2424
from lightning.pytorch.loggers import Logger, TensorBoardLogger, WandbLogger
25-
from nvidia_resiliency_ext.ptl_resiliency.local_checkpoint_callback import LocalCheckpointCallback
2625

2726
from nemo.lightning.io.mixin import IOMixin
2827
from nemo.lightning.pytorch.callbacks import ModelCheckpoint
2928
from nemo.utils import logging
3029
from nemo.utils.app_state import AppState
30+
from nemo.utils.import_utils import safe_import
31+
32+
LocalCheckpointCallback, HAVE_RES = safe_import('nvidia_resiliency_ext.ptl_resiliency.local_checkpoint_callback')
3133

3234

3335
@dataclass

0 commit comments

Comments
 (0)