Enterprise-ready security: ISO 27001 + SOC 2 Type I compliant.

Link to this sectionReference for ultralytics/nn/backends/ascend.py#

Improvements

This page is sourced from https://github.com/ultralytics/ultralytics/blob/main/ultralytics/nn/backends/ascend.py. Have an improvement or example to add? Open a Pull Request — thank you! 🙏


Summary

Link to this sectionClass ultralytics.nn.backends.ascend.AscendBackend#

AscendBackend()

Bases: BaseBackend

Huawei Ascend NPU inference backend for CANN offline models.

Loads a compiled .om offline model and runs inference on the Ascend AI Processor through the ais_bench runtime, which wraps CANN's pyACL bindings.

Methods

NameDescription
__del__Release the Ascend device-side resources held by the inference session.
forwardRun inference on the Ascend NPU.
load_modelLoad an Ascend model from a directory containing a .om file.
Source code in ultralytics/nn/backends/ascend.py

View on GitHub

class AscendBackend(BaseBackend):

Link to this sectionMethod ultralytics.nn.backends.ascend.AscendBackend.__del__#

def __del__(self)

Release the Ascend device-side resources held by the inference session.

Source code in ultralytics/nn/backends/ascend.py

View on GitHub

def __del__(self):
    """Release the Ascend device-side resources held by the inference session."""
    if model := getattr(self, "model", None):
        model.free_resource()

Link to this sectionMethod ultralytics.nn.backends.ascend.AscendBackend.forward#

def forward(self, im: torch.Tensor) -> np.ndarray | list[np.ndarray]

Run inference on the Ascend NPU.

Args

NameTypeDescriptionDefault
imtorch.TensorInput image tensor in BCHW format, normalized to [0, 1].required

Returns

TypeDescription
`np.ndarraylist[np.ndarray]`
Source code in ultralytics/nn/backends/ascend.py

View on GitHub

def forward(self, im: torch.Tensor) -> np.ndarray | list[np.ndarray]:
    """Run inference on the Ascend NPU.

    Args:
        im (torch.Tensor): Input image tensor in BCHW format, normalized to [0, 1].

    Returns:
        (np.ndarray | list[np.ndarray]): Model predictions as a single array or list of arrays.
    """
    y = self.model.infer([im.cpu().numpy()])
    return y[0] if len(y) == 1 else y

Link to this sectionMethod ultralytics.nn.backends.ascend.AscendBackend.load_model#

def load_model(self, weight: str | Path) -> None

Load an Ascend model from a directory containing a .om file.

Args

NameTypeDescriptionDefault
weight`strPath`Path to the Ascend model directory containing the .om offline model.

Raises

TypeDescription
ImportErrorIf the ais_bench Python package is not installed.
FileNotFoundErrorIf no .om file is found in the given directory.
Source code in ultralytics/nn/backends/ascend.py

View on GitHub

def load_model(self, weight: str | Path) -> None:
    """Load an Ascend model from a directory containing a .om file.

    Args:
        weight (str | Path): Path to the Ascend model directory containing the .om offline model.

    Raises:
        ImportError: If the ``ais_bench`` Python package is not installed.
        FileNotFoundError: If no .om file is found in the given directory.
    """
    try:
        from ais_bench.infer.interface import InferSession
    except ImportError as e:
        raise ImportError(
            "Ascend inference requires the CANN runtime and `ais_bench` Python package. "
            "See https://docs.ultralytics.com/integrations/ascend/#runtime-installation for instructions."
        ) from e

    LOGGER.info(f"Loading {weight} for Huawei Ascend inference...")

    w = Path(weight)
    found = next(w.rglob("*.om"), None)
    if found is None:
        raise FileNotFoundError(f"No .om file found in: {w}")

    self.model = InferSession(getattr(self.device, "index", None) or 0, str(found))

    # Load metadata
    metadata_file = found.parent / "metadata.yaml"
    if metadata_file.exists():
        self.apply_metadata(YAML.load(metadata_file))



Contributors