diff --git a/docs/en/usage/cfg.md b/docs/en/usage/cfg.md index 6d58670f..4dadaa84 100644 --- a/docs/en/usage/cfg.md +++ b/docs/en/usage/cfg.md @@ -201,7 +201,7 @@ Default settings include: - **Confidence Threshold (`conf=0.25`)**: Minimum confidence for detections. - **IoU Threshold (`iou=0.7`)**: For [Non-Maximum Suppression (NMS)](https://www.ultralytics.com/glossary/non-maximum-suppression-nms). - **Image Size (`imgsz=640`)**: Resizes input images. -- **Device (`device=None`)**: Selects CPU or GPU. +- **Device (`device=None`)**: Selects CPU, GPU, Apple MPS or Huawei Ascend NPU (`npu`). For a full overview, see [Predict Settings](#predict-settings) and the [Predict Guide](../modes/predict.md). diff --git a/docs/macros/export-args.md b/docs/macros/export-args.md index ca58f65f..d181a636 100644 --- a/docs/macros/export-args.md +++ b/docs/macros/export-args.md @@ -12,7 +12,7 @@ | `workspace` | `float` or `None` | `None` | Sets the maximum workspace size in GiB for [TensorRT](https://docs.ultralytics.com/integrations/tensorrt/) optimizations, balancing memory usage and performance. Use `None` for auto-allocation by TensorRT up to device maximum. | | `nms` | `bool` | `False` | Adds Non-Maximum Suppression (NMS) to the exported model when supported (see [Export Formats](https://docs.ultralytics.com/modes/export/)), improving detection post-processing efficiency. Not available for end2end models. | | `batch` | `int` | `1` | Specifies export model batch inference size or the maximum number of images the exported model will process concurrently in `predict` mode. For Edge TPU exports, this is automatically set to 1. | -| `device` | `str` | `None` | Specifies the device for exporting: GPU (`device=0`), CPU (`device=cpu`), MPS for Apple silicon (`device=mps`) or DLA for NVIDIA Jetson (`device=dla:0` or `device=dla:1`). TensorRT exports automatically use GPU. | +| `device` | `str` | `None` | Specifies the device for exporting: GPU (`device=0`), CPU (`device=cpu`), MPS for Apple silicon (`device=mps`), Huawei Ascend NPU (`device=npu` or `device=npu:0`), or DLA for NVIDIA Jetson (`device=dla:0` or `device=dla:1`). TensorRT exports automatically use GPU. | | `data` | `str` | `'coco8.yaml'` | Path to the [dataset](https://docs.ultralytics.com/datasets/) configuration file, essential for INT8 quantization calibration. If not specified with INT8 enabled, `coco8.yaml` will be used as a fallback for calibration. | | `fraction` | `float` | `1.0` | Specifies the fraction of the dataset to use for INT8 quantization calibration. Allows for calibrating on a subset of the full dataset, useful for experiments or when resources are limited. If not specified with INT8 enabled, the full dataset will be used. | | `end2end` | `bool` | `None` | Overrides the end-to-end mode in YOLO models that support NMS-free inference (YOLO26, YOLOv10). Setting it to `False` lets you export these models to be compatible with the traditional NMS-based postprocessing pipeline. | diff --git a/docs/macros/predict-args.md b/docs/macros/predict-args.md index b92f5e8b..f4eae3a2 100644 --- a/docs/macros/predict-args.md +++ b/docs/macros/predict-args.md @@ -6,7 +6,7 @@ | `imgsz` | `int` or `tuple` | `640` | Defines the image size for inference. Can be a single integer `640` for square resizing or a (height, width) tuple. Proper sizing can improve detection [accuracy](https://www.ultralytics.com/glossary/accuracy) and processing speed. | | `rect` | `bool` | `True` | If enabled, minimally pads the shorter side of the image until it's divisible by stride to improve inference speed. If disabled, pads the image to a square during inference. | | `half` | `bool` | `False` | Enables half-[precision](https://www.ultralytics.com/glossary/precision) (FP16) inference, which can speed up model inference on supported GPUs with minimal impact on accuracy. | -| `device` | `str` | `None` | Specifies the device for inference (e.g., `cpu`, `cuda:0` or `0`). Allows users to select between CPU, a specific GPU, or other compute devices for model execution. | +| `device` | `str` | `None` | Specifies the device for inference (e.g., `cpu`, `cuda:0`, `0`, `npu` or `npu:0`). Allows users to select between CPU, a specific GPU, Huawei Ascend NPU, or other compute devices for model execution. | | `batch` | `int` | `1` | Specifies the batch size for inference (only works when the source is [a directory, video file, or `.txt` file](https://docs.ultralytics.com/modes/predict/#inference-sources)). A larger batch size can provide higher throughput, shortening the total amount of time required for inference. | | `max_det` | `int` | `300` | Maximum number of detections allowed per image. Limits the total number of objects the model can detect in a single inference, preventing excessive outputs in dense scenes. | | `vid_stride` | `int` | `1` | Frame stride for video inputs. Allows skipping frames in videos to speed up processing at the cost of temporal resolution. A value of 1 processes every frame, higher values skip frames. | diff --git a/docs/macros/train-args.md b/docs/macros/train-args.md index f398ceda..7c7c9248 100644 --- a/docs/macros/train-args.md +++ b/docs/macros/train-args.md @@ -10,7 +10,7 @@ | `save` | `bool` | `True` | Enables saving of training checkpoints and final model weights. Useful for resuming training or [model deployment](https://www.ultralytics.com/glossary/model-deployment). | | `save_period` | `int` | `-1` | Frequency of saving model checkpoints, specified in epochs. A value of -1 disables this feature. Useful for saving interim models during long training sessions. | | `cache` | `bool` | `False` | Enables caching of dataset images in memory (`True`/`ram`), on disk (`disk`), or disables it (`False`). Improves training speed by reducing disk I/O at the cost of increased memory usage. | -| `device` | `int` or `str` or `list` | `None` | Specifies the computational device(s) for training: a single GPU (`device=0`), multiple GPUs (`device=[0,1]`), CPU (`device=cpu`), MPS for Apple silicon (`device=mps`), or auto-selection of most idle GPU (`device=-1`) or multiple idle GPUs (`device=[-1,-1]`) | +| `device` | `int` or `str` or `list` | `None` | Specifies the computational device(s) for training: a single GPU (`device=0`), multiple GPUs (`device=[0,1]`), CPU (`device=cpu`), MPS for Apple silicon (`device=mps`), Huawei Ascend NPU (`device=npu` or `device=npu:0`), or auto-selection of most idle GPU (`device=-1`) or multiple idle GPUs (`device=[-1,-1]`) | | `workers` | `int` | `8` | Number of worker threads for data loading (per `RANK` if Multi-GPU training). Influences the speed of data preprocessing and feeding into the model, especially useful in multi-GPU setups. | | `project` | `str` | `None` | Name of the project directory where training outputs are saved. Allows for organized storage of different experiments. | | `name` | `str` | `None` | Name of the training run. Used for creating a subdirectory within the project folder, where training logs and outputs are stored. | diff --git a/docs/macros/validation-args.md b/docs/macros/validation-args.md index 2a7972e5..fca228bf 100644 --- a/docs/macros/validation-args.md +++ b/docs/macros/validation-args.md @@ -8,7 +8,7 @@ | `iou` | `float` | `0.7` | Sets the [Intersection Over Union](https://www.ultralytics.com/glossary/intersection-over-union-iou) threshold for [Non-Maximum Suppression](https://www.ultralytics.com/glossary/non-maximum-suppression-nms). Controls duplicate detection elimination. | | `max_det` | `int` | `300` | Limits the maximum number of detections per image. Useful in dense scenes to prevent excessive detections and manage computational resources. | | `half` | `bool` | `False` | Enables half-[precision](https://www.ultralytics.com/glossary/precision) (FP16) computation, reducing memory usage and potentially increasing speed with minimal impact on [accuracy](https://www.ultralytics.com/glossary/accuracy). | -| `device` | `str` | `None` | Specifies the device for validation (`cpu`, `cuda:0`, etc.). When `None`, automatically selects the best available device. Multiple CUDA devices can be specified with comma separation. | +| `device` | `str` | `None` | Specifies the device for validation (`cpu`, `cuda:0`, `npu`, `npu:0`, etc.). When `None`, automatically selects the best available device. Multiple CUDA devices can be specified with comma separation. | | `dnn` | `bool` | `False` | If `True`, uses the [OpenCV](https://www.ultralytics.com/glossary/opencv) DNN module for ONNX model inference, offering an alternative to [PyTorch](https://www.ultralytics.com/glossary/pytorch) inference methods. | | `plots` | `bool` | `True` | When set to `True`, generates and saves plots of predictions versus ground truth, confusion matrices, and PR curves for visual evaluation of model performance. | | `classes` | `list[int]` | `None` | Specifies a list of class IDs to evaluate. Useful for filtering out and focusing only on certain classes during evaluation. | diff --git a/ultralytics/__init__.py b/ultralytics/__init__.py index 2d68d4c9..fccd888f 100644 --- a/ultralytics/__init__.py +++ b/ultralytics/__init__.py @@ -1,6 +1,6 @@ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license -__version__ = "8.4.21" +__version__ = "8.4.22" import importlib import os diff --git a/ultralytics/utils/torch_utils.py b/ultralytics/utils/torch_utils.py index 5d4b7ba7..eedbb99c 100644 --- a/ultralytics/utils/torch_utils.py +++ b/ultralytics/utils/torch_utils.py @@ -142,8 +142,8 @@ def select_device(device="", newline=False, verbose=True): Args: device (str | torch.device, optional): Device string or torch.device object. Options include 'cpu', 'cuda', '0', - '0,1,2,3', 'mps', or '-1' for auto-select. Defaults to auto-selecting the first available GPU, or CPU if no - GPU is available. + '0,1,2,3', 'mps', 'npu', 'npu:0', or '-1' for auto-select. Defaults to auto-selecting the first available + GPU, or CPU if no GPU is available. newline (bool, optional): If True, adds a newline at the end of the log string. verbose (bool, optional): If True, logs the device information. @@ -168,6 +168,34 @@ def select_device(device="", newline=False, verbose=True): for remove in "cuda:", "none", "(", ")", "[", "]", "'", " ": device = device.replace(remove, "") # to string, 'cuda:0' -> '0' and '(0, 1)' -> '0,1' + # Huawei Ascend NPU + if device.startswith("npu"): + try: + import torch_npu # noqa + except ImportError: + raise ValueError(f"Invalid NPU 'device={device}'. Install 'torch_npu' at https://github.com/Ascend/pytorch") + + if not hasattr(torch, "npu") or not torch.npu.is_available(): + raise ValueError(f"Invalid NPU 'device={device}' requested. Ascend NPU is not available.") + + # Parse 'npu' or 'npu:N' (multi-NPU not yet supported) + suffix = device[3:] + if suffix == "": + idx = 0 + elif suffix.startswith(":") and suffix[1:].isdigit(): + idx = int(suffix[1:]) + else: + raise ValueError(f"Invalid NPU 'device={device}' format. Use 'npu' or 'npu:0'.") + + n = torch.npu.device_count() + if idx >= n: + raise ValueError(f"Invalid NPU 'device={device}' requested. Only {n} NPU(s) available.") + + torch.npu.set_device(idx) + if verbose: + LOGGER.info(f"{s}NPU:{idx} ({torch.npu.get_device_name(idx)})\n") + return torch.device(f"npu:{idx}") + # Auto-select GPUs if "-1" in device: from ultralytics.utils.autodevice import GPUInfo