Files
nvbench/python/scripts/nvbench_tooling_deps.py
Oleksandr Pavlyk 5fd21dd7fa Load script tooling dependencies lazily
Add a shared nvbench_tooling_deps helper for importing packages required
by NVBench console tools. Missing tooling packages now raise a dedicated
error with an install recipe instead of failing with a raw ImportError.

Update script imports to work both as installed package modules and as
direct source-tree scripts by using the __package__ import pattern for
nvbench_json and the new tooling helper.

Defer nvbench-compare dependencies to the points where they are needed:
NumPy/colorama during normal comparison setup, tabulate during table
rendering, jsondiff only for device mismatch reporting, and plotting
packages only for plot modes.

Update tests to initialize compare tooling when calling internals
directly and add coverage for the tooling dependency loader.

Closes #384
2026-06-30 06:40:45 -05:00

38 lines
1.0 KiB
Python

#!/usr/bin/env python
#
# SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
from __future__ import annotations
import importlib
from dataclasses import dataclass
from types import ModuleType
@dataclass(frozen=True)
class ToolingDependency:
import_name: str
package_name: str
purpose: str
extra: str = "tools"
class MissingToolingDependencyError(RuntimeError):
pass
def require_tooling_dependency(
dependency: ToolingDependency, *, tool_name: str
) -> ModuleType:
try:
return importlib.import_module(dependency.import_name)
except ImportError as exc:
raise MissingToolingDependencyError(
f"{tool_name} requires {dependency.package_name!r} for "
f"{dependency.purpose}.\n\n"
"Install the required tooling dependencies with:\n"
f" python -m pip install 'cuda-bench[{dependency.extra}]'\n\n"
f"Original import error: {exc}"
) from exc