diff --git a/docs/en/macros/predict-args.md b/docs/en/macros/predict-args.md
index d6ba7205..6bde71e4 100644
--- a/docs/en/macros/predict-args.md
+++ b/docs/en/macros/predict-args.md
@@ -21,3 +21,4 @@
| `name` | `str` | `None` | Name of the prediction run. Used for creating a subdirectory within the project folder, where prediction outputs are stored if `save` is enabled. |
| `stream` | `bool` | `False` | Enables memory-efficient processing for long videos or numerous images by returning a generator of Results objects instead of loading all frames into memory at once. |
| `verbose` | `bool` | `True` | Controls whether to display detailed inference logs in the terminal, providing real-time feedback on the prediction process. |
+| `compile` | `bool` | `False` | Enables PyTorch 2.x `torch.compile` graph compilation to optimize model execution. Applies to supported devices (CUDA/CPU/MPS). Uses `backend='inductor'`, `mode='max-autotune'`. Falls back to eager mode with a warning if unsupported. |
diff --git a/docs/en/macros/train-args.md b/docs/en/macros/train-args.md
index 6555f547..7e3734de 100644
--- a/docs/en/macros/train-args.md
+++ b/docs/en/macros/train-args.md
@@ -48,3 +48,4 @@
| `dropout` | `float` | `0.0` | Dropout rate for regularization in classification tasks, preventing overfitting by randomly omitting units during training. |
| `val` | `bool` | `True` | Enables validation during training, allowing for periodic evaluation of model performance on a separate dataset. |
| `plots` | `bool` | `False` | Generates and saves plots of training and validation metrics, as well as prediction examples, providing visual insights into model performance and learning progression. |
+| `compile` | `bool` | `False` | Enables PyTorch 2.x `torch.compile` graph compilation to optimize model execution. Applies to supported devices (CUDA/CPU/MPS). Uses `backend='inductor'`, `mode='max-autotune'`. Falls back to eager mode with a warning if unsupported. |
diff --git a/docs/en/macros/validation-args.md b/docs/en/macros/validation-args.md
index c33bbb38..9f41c5cb 100644
--- a/docs/en/macros/validation-args.md
+++ b/docs/en/macros/validation-args.md
@@ -24,3 +24,4 @@
| `agnostic_nms` | `bool` | `False` | Enables class-agnostic [Non-Maximum Suppression](https://www.ultralytics.com/glossary/non-maximum-suppression-nms), which merges overlapping boxes regardless of their predicted class. Useful for instance-focused applications. |
| `single_cls` | `bool` | `False` | Treats all classes as a single class during validation. Useful for evaluating model performance on binary detection tasks or when class distinctions aren't important. |
| `visualize` | `bool` | `False` | Visualizes the ground truths, true positives, false positives and false negatives for each image. Useful for debugging and model interpretation. |
+| `compile` | `bool` | `False` | Enables PyTorch 2.x `torch.compile` graph compilation to optimize model execution. Applies to supported devices (CUDA/CPU/MPS). Uses `backend='inductor'`, `mode='max-autotune'`. Falls back to eager mode with a warning if unsupported. |
diff --git a/docs/en/reference/utils/torch_utils.md b/docs/en/reference/utils/torch_utils.md
index e7a7b991..447ed949 100644
--- a/docs/en/reference/utils/torch_utils.md
+++ b/docs/en/reference/utils/torch_utils.md
@@ -107,7 +107,7 @@ keywords: Ultralytics, torch utils, model optimization, device selection, infere
-## ::: ultralytics.utils.torch_utils.de_parallel
+## ::: ultralytics.utils.torch_utils.unwrap_model
@@ -137,4 +137,12 @@ keywords: Ultralytics, torch utils, model optimization, device selection, infere
## ::: ultralytics.utils.torch_utils.profile_ops
+
+
+## ::: ultralytics.utils.torch_utils.disable_dynamo
+
+
+
+## ::: ultralytics.utils.torch_utils.attempt_compile
+
diff --git a/ultralytics/__init__.py b/ultralytics/__init__.py
index 21e017a4..a2c28781 100644
--- a/ultralytics/__init__.py
+++ b/ultralytics/__init__.py
@@ -1,6 +1,6 @@
# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
-__version__ = "8.3.195"
+__version__ = "8.3.196"
import os
diff --git a/ultralytics/cfg/__init__.py b/ultralytics/cfg/__init__.py
index 0cc1cb14..fb0b14cd 100644
--- a/ultralytics/cfg/__init__.py
+++ b/ultralytics/cfg/__init__.py
@@ -237,6 +237,7 @@ CFG_BOOL_KEYS = frozenset(
"nms",
"profile",
"multi_scale",
+ "compile",
}
)
diff --git a/ultralytics/cfg/default.yaml b/ultralytics/cfg/default.yaml
index b481e75c..b6532b87 100644
--- a/ultralytics/cfg/default.yaml
+++ b/ultralytics/cfg/default.yaml
@@ -37,6 +37,7 @@ fraction: 1.0 # (float) dataset fraction to train on (default is 1.0, all images
profile: False # (bool) profile ONNX and TensorRT speeds during training for loggers
freeze: # (int | list, optional) freeze first n layers, or freeze list of layer indices during training
multi_scale: False # (bool) Whether to use multiscale during training
+compile: False # (bool) Run torch.compile() on the model before train/val/predict
# Segmentation
overlap_mask: True # (bool) merge object masks into a single image mask during training (segment train only)
mask_ratio: 4 # (int) mask downsample ratio (segment train only)
diff --git a/ultralytics/data/augment.py b/ultralytics/data/augment.py
index 48d9e2ba..d25fba39 100644
--- a/ultralytics/data/augment.py
+++ b/ultralytics/data/augment.py
@@ -2202,7 +2202,7 @@ class Format:
)
labels["masks"] = masks
labels["img"] = self._format_img(img)
- labels["cls"] = torch.from_numpy(cls) if nl else torch.zeros(nl)
+ labels["cls"] = torch.from_numpy(cls) if nl else torch.zeros(nl, 1)
labels["bboxes"] = torch.from_numpy(instances.bboxes) if nl else torch.zeros((nl, 4))
if self.return_keypoint:
labels["keypoints"] = (
diff --git a/ultralytics/data/build.py b/ultralytics/data/build.py
index 44b68316..5dfbff12 100644
--- a/ultralytics/data/build.py
+++ b/ultralytics/data/build.py
@@ -217,7 +217,7 @@ def build_dataloader(dataset, batch: int, workers: int, shuffle: bool = True, ra
collate_fn=getattr(dataset, "collate_fn", None),
worker_init_fn=seed_worker,
generator=generator,
- drop_last=drop_last,
+ drop_last=drop_last and len(dataset) % batch != 0,
)
diff --git a/ultralytics/engine/predictor.py b/ultralytics/engine/predictor.py
index b358ce0f..771ea589 100644
--- a/ultralytics/engine/predictor.py
+++ b/ultralytics/engine/predictor.py
@@ -51,7 +51,7 @@ from ultralytics.nn.autobackend import AutoBackend
from ultralytics.utils import DEFAULT_CFG, LOGGER, MACOS, WINDOWS, callbacks, colorstr, ops
from ultralytics.utils.checks import check_imgsz, check_imshow
from ultralytics.utils.files import increment_path
-from ultralytics.utils.torch_utils import select_device, smart_inference_mode
+from ultralytics.utils.torch_utils import attempt_compile, select_device, smart_inference_mode
STREAM_WARNING = """
inference results will accumulate in RAM unless `stream=True` is passed, causing potential out-of-memory
@@ -409,6 +409,8 @@ class BasePredictor:
if hasattr(self.model, "imgsz") and not getattr(self.model, "dynamic", False):
self.args.imgsz = self.model.imgsz # reuse imgsz from export metadata
self.model.eval()
+ if self.args.compile:
+ self.model = attempt_compile(self.model, device=self.device)
def write_results(self, i: int, p: Path, im: torch.Tensor, s: list[str]) -> str:
"""
diff --git a/ultralytics/engine/trainer.py b/ultralytics/engine/trainer.py
index 0869c311..a7460f8e 100644
--- a/ultralytics/engine/trainer.py
+++ b/ultralytics/engine/trainer.py
@@ -46,6 +46,7 @@ from ultralytics.utils.torch_utils import (
TORCH_2_4,
EarlyStopping,
ModelEMA,
+ attempt_compile,
autocast,
convert_optimizer_state_dict_to_fp16,
init_seeds,
@@ -54,6 +55,7 @@ from ultralytics.utils.torch_utils import (
strip_optimizer,
torch_distributed_zero_first,
unset_deterministic,
+ unwrap_model,
)
@@ -256,6 +258,14 @@ class BaseTrainer:
self.model = self.model.to(self.device)
self.set_model_attributes()
+ # Initialize loss criterion before compilation for torch.compile compatibility
+ if hasattr(self.model, "init_criterion"):
+ self.model.criterion = self.model.init_criterion()
+
+ # Compile model
+ if self.args.compile:
+ self.model = attempt_compile(self.model, device=self.device)
+
# Freeze layers
freeze_list = (
self.args.freeze
@@ -404,6 +414,7 @@ class BaseTrainer:
# Forward
with autocast(self.amp):
batch = self.preprocess_batch(batch)
+ metadata = {k: batch.pop(k, None) for k in ["im_file", "ori_shape", "resized_shape"]}
loss, self.loss_items = self.model(batch)
self.loss = loss.sum()
if RANK != -1:
@@ -445,6 +456,7 @@ class BaseTrainer:
)
self.run_callbacks("on_batch_end")
if self.args.plots and ni in self.plot_idx:
+ batch = {**batch, **metadata}
self.plot_training_samples(batch, ni)
self.run_callbacks("on_train_batch_end")
@@ -565,7 +577,7 @@ class BaseTrainer:
"epoch": self.epoch,
"best_fitness": self.best_fitness,
"model": None, # resume and final checkpoints derive from EMA
- "ema": deepcopy(self.ema.ema).half(),
+ "ema": deepcopy(unwrap_model(self.ema.ema)).half(),
"updates": self.ema.updates,
"optimizer": convert_optimizer_state_dict_to_fp16(deepcopy(self.optimizer.state_dict())),
"train_args": vars(self.args), # save as dict
@@ -592,8 +604,6 @@ class BaseTrainer:
self.best.write_bytes(serialized_ckpt) # save best.pt
if (self.save_period > 0) and (self.epoch % self.save_period == 0):
(self.wdir / f"epoch{self.epoch}.pt").write_bytes(serialized_ckpt) # save epoch, i.e. 'epoch3.pt'
- # if self.args.close_mosaic and self.epoch == (self.epochs - self.args.close_mosaic - 1):
- # (self.wdir / "last_mosaic.pt").write_bytes(serialized_ckpt) # save mosaic checkpoint
def get_dataset(self):
"""
@@ -667,7 +677,7 @@ class BaseTrainer:
def validate(self):
"""
- Run validation on test set using self.validator.
+ Run validation on val set using self.validator.
Returns:
metrics (dict): Dictionary of validation metrics.
@@ -755,6 +765,7 @@ class BaseTrainer:
strip_optimizer(f, updates={k: ckpt[k]} if k in ckpt else None)
LOGGER.info(f"\nValidating {f}...")
self.validator.args.plots = self.args.plots
+ self.validator.args.compile = False # disable final val compile as too slow
self.metrics = self.validator(model=f)
self.metrics.pop("fitness", None)
self.run_callbacks("on_fit_epoch_end")
diff --git a/ultralytics/engine/validator.py b/ultralytics/engine/validator.py
index a7c15219..bcba8459 100644
--- a/ultralytics/engine/validator.py
+++ b/ultralytics/engine/validator.py
@@ -36,7 +36,7 @@ from ultralytics.nn.autobackend import AutoBackend
from ultralytics.utils import LOGGER, TQDM, callbacks, colorstr, emojis
from ultralytics.utils.checks import check_imgsz
from ultralytics.utils.ops import Profile
-from ultralytics.utils.torch_utils import de_parallel, select_device, smart_inference_mode
+from ultralytics.utils.torch_utils import attempt_compile, select_device, smart_inference_mode, unwrap_model
class BaseValidator:
@@ -148,6 +148,8 @@ class BaseValidator:
# Force FP16 val during training
self.args.half = self.device.type != "cpu" and trainer.amp
model = trainer.ema.ema or trainer.model
+ if trainer.args.compile and hasattr(model, "_orig_mod"):
+ model = model._orig_mod # validate non-compiled original model to avoid issues
model = model.half() if self.args.half else model.float()
self.loss = torch.zeros_like(trainer.loss_items, device=trainer.device)
self.args.plots &= trainer.stopper.possible_stop or (trainer.epoch == trainer.epochs - 1)
@@ -186,6 +188,8 @@ class BaseValidator:
self.dataloader = self.dataloader or self.get_dataloader(self.data.get(self.args.split), self.args.batch)
model.eval()
+ if self.args.compile:
+ model = attempt_compile(model, device=self.device)
model.warmup(imgsz=(1 if pt else self.args.batch, self.data["channels"], imgsz, imgsz)) # warmup
self.run_callbacks("on_val_start")
@@ -196,7 +200,7 @@ class BaseValidator:
Profile(device=self.device),
)
bar = TQDM(self.dataloader, desc=self.get_desc(), total=len(self.dataloader))
- self.init_metrics(de_parallel(model))
+ self.init_metrics(unwrap_model(model))
self.jdict = [] # empty before each val
for batch_i, batch in enumerate(bar):
self.run_callbacks("on_val_batch_start")
diff --git a/ultralytics/models/yolo/classify/train.py b/ultralytics/models/yolo/classify/train.py
index 702faee4..06b791dd 100644
--- a/ultralytics/models/yolo/classify/train.py
+++ b/ultralytics/models/yolo/classify/train.py
@@ -55,20 +55,10 @@ class ClassificationTrainer(BaseTrainer):
"""
Initialize a ClassificationTrainer object.
- This constructor sets up a trainer for image classification tasks, configuring the task type and default
- image size if not specified.
-
Args:
cfg (dict[str, Any], optional): Default configuration dictionary containing training parameters.
overrides (dict[str, Any], optional): Dictionary of parameter overrides for the default configuration.
_callbacks (list[Any], optional): List of callback functions to be executed during training.
-
- Examples:
- Create a trainer with custom configuration
- >>> from ultralytics.models.yolo.classify import ClassificationTrainer
- >>> args = dict(model="yolo11n-cls.pt", data="imagenet10", epochs=3)
- >>> trainer = ClassificationTrainer(overrides=args)
- >>> trainer.train()
"""
if overrides is None:
overrides = {}
@@ -155,7 +145,7 @@ class ClassificationTrainer(BaseTrainer):
with torch_distributed_zero_first(rank): # init dataset *.cache only once if DDP
dataset = self.build_dataset(dataset_path, mode)
- loader = build_dataloader(dataset, batch_size, self.args.workers, rank=rank)
+ loader = build_dataloader(dataset, batch_size, self.args.workers, rank=rank, drop_last=self.args.compile)
# Attach inference transforms
if mode != "train":
if is_parallel(self.model):
diff --git a/ultralytics/models/yolo/detect/train.py b/ultralytics/models/yolo/detect/train.py
index b5f1685d..97d562ec 100644
--- a/ultralytics/models/yolo/detect/train.py
+++ b/ultralytics/models/yolo/detect/train.py
@@ -15,10 +15,10 @@ from ultralytics.data import build_dataloader, build_yolo_dataset
from ultralytics.engine.trainer import BaseTrainer
from ultralytics.models import yolo
from ultralytics.nn.tasks import DetectionModel
-from ultralytics.utils import LOGGER, RANK
+from ultralytics.utils import DEFAULT_CFG, LOGGER, RANK
from ultralytics.utils.patches import override_configs
from ultralytics.utils.plotting import plot_images, plot_labels, plot_results
-from ultralytics.utils.torch_utils import de_parallel, torch_distributed_zero_first
+from ultralytics.utils.torch_utils import torch_distributed_zero_first, unwrap_model
class DetectionTrainer(BaseTrainer):
@@ -54,6 +54,18 @@ class DetectionTrainer(BaseTrainer):
>>> trainer.train()
"""
+ def __init__(self, cfg=DEFAULT_CFG, overrides: dict[str, Any] | None = None, _callbacks=None):
+ """
+ Initialize a DetectionTrainer object for training YOLO object detection model training.
+
+ Args:
+ cfg (dict, optional): Default configuration dictionary containing training parameters.
+ overrides (dict, optional): Dictionary of parameter overrides for the default configuration.
+ _callbacks (list, optional): List of callback functions to be executed during training.
+ """
+ super().__init__(cfg, overrides, _callbacks)
+ self.dynamic_tensors = ["batch_idx", "cls", "bboxes"]
+
def build_dataset(self, img_path: str, mode: str = "train", batch: int | None = None):
"""
Build YOLO Dataset for training or validation.
@@ -66,7 +78,7 @@ class DetectionTrainer(BaseTrainer):
Returns:
(Dataset): YOLO dataset object configured for the specified mode.
"""
- gs = max(int(de_parallel(self.model).stride.max() if self.model else 0), 32)
+ gs = max(int(unwrap_model(self.model).stride.max() if self.model else 0), 32)
return build_yolo_dataset(self.args, img_path, batch, self.data, mode=mode, rect=mode == "val", stride=gs)
def get_dataloader(self, dataset_path: str, batch_size: int = 16, rank: int = 0, mode: str = "train"):
@@ -89,8 +101,14 @@ class DetectionTrainer(BaseTrainer):
if getattr(dataset, "rect", False) and shuffle:
LOGGER.warning("'rect=True' is incompatible with DataLoader shuffle, setting shuffle=False")
shuffle = False
- workers = self.args.workers if mode == "train" else self.args.workers * 2
- return build_dataloader(dataset, batch_size, workers, shuffle, rank) # return dataloader
+ return build_dataloader(
+ dataset,
+ batch=batch_size,
+ workers=self.args.workers if mode == "train" else self.args.workers * 2,
+ shuffle=shuffle,
+ rank=rank,
+ drop_last=self.args.compile and mode == "train",
+ )
def preprocess_batch(self, batch: dict) -> dict:
"""
@@ -120,6 +138,10 @@ class DetectionTrainer(BaseTrainer):
] # new shape (stretched to gs-multiple)
imgs = nn.functional.interpolate(imgs, size=ns, mode="bilinear", align_corners=False)
batch["img"] = imgs
+
+ if self.args.compile:
+ for k in self.dynamic_tensors:
+ torch._dynamo.maybe_mark_dynamic(batch[k], 0)
return batch
def set_model_attributes(self):
diff --git a/ultralytics/models/yolo/detect/val.py b/ultralytics/models/yolo/detect/val.py
index 3a8112a9..de1d33be 100644
--- a/ultralytics/models/yolo/detect/val.py
+++ b/ultralytics/models/yolo/detect/val.py
@@ -299,7 +299,9 @@ class DetectionValidator(BaseValidator):
(torch.utils.data.DataLoader): Dataloader for validation.
"""
dataset = self.build_dataset(dataset_path, batch=batch_size, mode="val")
- return build_dataloader(dataset, batch_size, self.args.workers, shuffle=False, rank=-1) # return dataloader
+ return build_dataloader(
+ dataset, batch_size, self.args.workers, shuffle=False, rank=-1, drop_last=self.args.compile
+ )
def plot_val_samples(self, batch: dict[str, Any], ni: int) -> None:
"""
diff --git a/ultralytics/models/yolo/obb/train.py b/ultralytics/models/yolo/obb/train.py
index fe9f7920..c1d06ae6 100644
--- a/ultralytics/models/yolo/obb/train.py
+++ b/ultralytics/models/yolo/obb/train.py
@@ -37,21 +37,12 @@ class OBBTrainer(yolo.detect.DetectionTrainer):
"""
Initialize an OBBTrainer object for training Oriented Bounding Box (OBB) models.
- This trainer extends the DetectionTrainer class to specialize in training models that detect oriented
- bounding boxes. It automatically sets the task to 'obb' in the configuration.
-
Args:
cfg (dict, optional): Configuration dictionary for the trainer. Contains training parameters and
model configuration.
overrides (dict, optional): Dictionary of parameter overrides for the configuration. Any values here
will take precedence over those in cfg.
_callbacks (list[Any], optional): List of callback functions to be invoked during training.
-
- Examples:
- >>> from ultralytics.models.yolo.obb import OBBTrainer
- >>> args = dict(model="yolo11n-obb.pt", data="dota8.yaml", epochs=3)
- >>> trainer = OBBTrainer(overrides=args)
- >>> trainer.train()
"""
if overrides is None:
overrides = {}
diff --git a/ultralytics/models/yolo/pose/train.py b/ultralytics/models/yolo/pose/train.py
index ceb280c9..564a23d2 100644
--- a/ultralytics/models/yolo/pose/train.py
+++ b/ultralytics/models/yolo/pose/train.py
@@ -44,9 +44,6 @@ class PoseTrainer(yolo.detect.DetectionTrainer):
"""
Initialize a PoseTrainer object for training YOLO pose estimation models.
- This initializes a trainer specialized for pose estimation tasks, setting the task to 'pose' and
- handling specific configurations needed for keypoint detection models.
-
Args:
cfg (dict, optional): Default configuration dictionary containing training parameters.
overrides (dict, optional): Dictionary of parameter overrides for the default configuration.
@@ -55,17 +52,12 @@ class PoseTrainer(yolo.detect.DetectionTrainer):
Notes:
This trainer will automatically set the task to 'pose' regardless of what is provided in overrides.
A warning is issued when using Apple MPS device due to known bugs with pose models.
-
- Examples:
- >>> from ultralytics.models.yolo.pose import PoseTrainer
- >>> args = dict(model="yolo11n-pose.pt", data="coco8-pose.yaml", epochs=3)
- >>> trainer = PoseTrainer(overrides=args)
- >>> trainer.train()
"""
if overrides is None:
overrides = {}
overrides["task"] = "pose"
super().__init__(cfg, overrides, _callbacks)
+ self.dynamic_tensors = ["batch_idx", "cls", "bboxes", "keypoints"]
if isinstance(self.args.device, str) and self.args.device.lower() == "mps":
LOGGER.warning(
diff --git a/ultralytics/models/yolo/segment/train.py b/ultralytics/models/yolo/segment/train.py
index 9d59809e..9badc995 100644
--- a/ultralytics/models/yolo/segment/train.py
+++ b/ultralytics/models/yolo/segment/train.py
@@ -32,24 +32,16 @@ class SegmentationTrainer(yolo.detect.DetectionTrainer):
"""
Initialize a SegmentationTrainer object.
- This initializes a trainer for segmentation tasks, extending the detection trainer with segmentation-specific
- functionality. It sets the task to 'segment' and prepares the trainer for training segmentation models.
-
Args:
cfg (dict): Configuration dictionary with default training settings.
overrides (dict, optional): Dictionary of parameter overrides for the default configuration.
_callbacks (list, optional): List of callback functions to be executed during training.
-
- Examples:
- >>> from ultralytics.models.yolo.segment import SegmentationTrainer
- >>> args = dict(model="yolo11n-seg.pt", data="coco8-seg.yaml", epochs=3)
- >>> trainer = SegmentationTrainer(overrides=args)
- >>> trainer.train()
"""
if overrides is None:
overrides = {}
overrides["task"] = "segment"
super().__init__(cfg, overrides, _callbacks)
+ self.dynamic_tensors = ["batch_idx", "cls", "bboxes", "masks"]
def get_model(self, cfg: dict | str | None = None, weights: str | Path | None = None, verbose: bool = True):
"""
diff --git a/ultralytics/models/yolo/world/train.py b/ultralytics/models/yolo/world/train.py
index 1690be7e..825b8859 100644
--- a/ultralytics/models/yolo/world/train.py
+++ b/ultralytics/models/yolo/world/train.py
@@ -12,7 +12,7 @@ from ultralytics.data import build_yolo_dataset
from ultralytics.models.yolo.detect import DetectionTrainer
from ultralytics.nn.tasks import WorldModel
from ultralytics.utils import DEFAULT_CFG, LOGGER, RANK
-from ultralytics.utils.torch_utils import de_parallel
+from ultralytics.utils.torch_utils import unwrap_model
def on_pretrain_routine_end(trainer) -> None:
@@ -20,7 +20,7 @@ def on_pretrain_routine_end(trainer) -> None:
if RANK in {-1, 0}:
# Set class names for evaluation
names = [name.split("/", 1)[0] for name in list(trainer.test_loader.dataset.data["names"].values())]
- de_parallel(trainer.ema.ema).set_classes(names, cache_clip_model=False)
+ unwrap_model(trainer.ema.ema).set_classes(names, cache_clip_model=False)
class WorldTrainer(DetectionTrainer):
@@ -105,7 +105,7 @@ class WorldTrainer(DetectionTrainer):
Returns:
(Any): YOLO dataset configured for training or validation.
"""
- gs = max(int(de_parallel(self.model).stride.max() if self.model else 0), 32)
+ gs = max(int(unwrap_model(self.model).stride.max() if self.model else 0), 32)
dataset = build_yolo_dataset(
self.args, img_path, batch, self.data, mode=mode, rect=mode == "val", stride=gs, multi_modal=mode == "train"
)
@@ -160,7 +160,7 @@ class WorldTrainer(DetectionTrainer):
return txt_map
LOGGER.info(f"Caching text embeddings to '{cache_path}'")
assert self.model is not None
- txt_feats = de_parallel(self.model).get_text_pe(texts, batch, cache_clip_model=False)
+ txt_feats = unwrap_model(self.model).get_text_pe(texts, batch, cache_clip_model=False)
txt_map = dict(zip(texts, txt_feats.squeeze(0)))
torch.save(txt_map, cache_path)
return txt_map
diff --git a/ultralytics/models/yolo/world/train_world.py b/ultralytics/models/yolo/world/train_world.py
index 9b77603c..558323c5 100644
--- a/ultralytics/models/yolo/world/train_world.py
+++ b/ultralytics/models/yolo/world/train_world.py
@@ -6,7 +6,7 @@ from ultralytics.data import YOLOConcatDataset, build_grounding, build_yolo_data
from ultralytics.data.utils import check_det_dataset
from ultralytics.models.yolo.world import WorldTrainer
from ultralytics.utils import DATASETS_DIR, DEFAULT_CFG, LOGGER
-from ultralytics.utils.torch_utils import de_parallel
+from ultralytics.utils.torch_utils import unwrap_model
class WorldTrainerFromScratch(WorldTrainer):
@@ -101,7 +101,7 @@ class WorldTrainerFromScratch(WorldTrainer):
Returns:
(YOLOConcatDataset | Dataset): The constructed dataset for training or validation.
"""
- gs = max(int(de_parallel(self.model).stride.max() if self.model else 0), 32)
+ gs = max(int(unwrap_model(self.model).stride.max() if self.model else 0), 32)
if mode != "train":
return build_yolo_dataset(self.args, img_path, batch, self.data, mode=mode, rect=False, stride=gs)
datasets = [
diff --git a/ultralytics/models/yolo/yoloe/train.py b/ultralytics/models/yolo/yoloe/train.py
index 395f24db..aaeffd1c 100644
--- a/ultralytics/models/yolo/yoloe/train.py
+++ b/ultralytics/models/yolo/yoloe/train.py
@@ -13,7 +13,7 @@ from ultralytics.data.augment import LoadVisualPrompt
from ultralytics.models.yolo.detect import DetectionTrainer, DetectionValidator
from ultralytics.nn.tasks import YOLOEModel
from ultralytics.utils import DEFAULT_CFG, LOGGER, RANK
-from ultralytics.utils.torch_utils import de_parallel
+from ultralytics.utils.torch_utils import unwrap_model
from ..world.train_world import WorldTrainerFromScratch
from .val import YOLOEDetectValidator
@@ -39,9 +39,6 @@ class YOLOETrainer(DetectionTrainer):
"""
Initialize the YOLOE Trainer with specified configurations.
- This method sets up the YOLOE trainer with the provided configuration and overrides, initializing
- the training environment, model, and callbacks for YOLOE object detection training.
-
Args:
cfg (dict): Configuration dictionary with default training settings from DEFAULT_CFG.
overrides (dict, optional): Dictionary of parameter overrides for the default configuration.
@@ -102,7 +99,7 @@ class YOLOETrainer(DetectionTrainer):
Returns:
(Dataset): YOLO dataset configured for training or validation.
"""
- gs = max(int(de_parallel(self.model).stride.max() if self.model else 0), 32)
+ gs = max(int(unwrap_model(self.model).stride.max() if self.model else 0), 32)
return build_yolo_dataset(
self.args, img_path, batch, self.data, mode=mode, rect=mode == "val", stride=gs, multi_modal=mode == "train"
)
@@ -223,7 +220,7 @@ class YOLOETrainerFromScratch(YOLOETrainer, WorldTrainerFromScratch):
return txt_map
LOGGER.info(f"Caching text embeddings to '{cache_path}'")
assert self.model is not None
- txt_feats = de_parallel(self.model).get_text_pe(texts, batch, without_reprta=True, cache_clip_model=False)
+ txt_feats = unwrap_model(self.model).get_text_pe(texts, batch, without_reprta=True, cache_clip_model=False)
txt_map = dict(zip(texts, txt_feats.squeeze(0)))
torch.save(txt_map, cache_path)
return txt_map
diff --git a/ultralytics/nn/modules/head.py b/ultralytics/nn/modules/head.py
index a277aa66..e7708efa 100644
--- a/ultralytics/nn/modules/head.py
+++ b/ultralytics/nn/modules/head.py
@@ -13,7 +13,7 @@ from torch.nn.init import constant_, xavier_uniform_
from ultralytics.utils import NOT_MACOS14
from ultralytics.utils.tal import TORCH_1_10, dist2bbox, dist2rbox, make_anchors
-from ultralytics.utils.torch_utils import fuse_conv_and_bn, smart_inference_mode
+from ultralytics.utils.torch_utils import disable_dynamo, fuse_conv_and_bn, smart_inference_mode
from .block import DFL, SAVPE, BNContrastiveHead, ContrastiveHead, Proto, Residual, SwiGLUFFN
from .conv import Conv, DWConv
@@ -149,6 +149,7 @@ class Detect(nn.Module):
y = self.postprocess(y.permute(0, 2, 1), self.max_det, self.nc)
return y if self.export else (y, {"one2many": x, "one2one": one2one})
+ @disable_dynamo
def _inference(self, x: list[torch.Tensor]) -> torch.Tensor:
"""
Decode predicted bounding boxes and class probabilities based on multiple-level feature maps.
diff --git a/ultralytics/nn/tasks.py b/ultralytics/nn/tasks.py
index 9a044294..e1887794 100644
--- a/ultralytics/nn/tasks.py
+++ b/ultralytics/nn/tasks.py
@@ -334,7 +334,8 @@ class BaseModel(torch.nn.Module):
if getattr(self, "criterion", None) is None:
self.criterion = self.init_criterion()
- preds = self.forward(batch["img"]) if preds is None else preds
+ if preds is None:
+ preds = self.forward(batch["img"])
return self.criterion(preds, batch)
def init_criterion(self):
@@ -775,7 +776,8 @@ class RTDETRDetectionModel(DetectionModel):
"gt_groups": gt_groups,
}
- preds = self.predict(img, batch=targets) if preds is None else preds
+ if preds is None:
+ preds = self.predict(img, batch=targets)
dec_bboxes, dec_scores, enc_bboxes, enc_scores, dn_meta = preds if self.training else preds[1]
if dn_meta is None:
dn_bboxes, dn_scores = None, None
diff --git a/ultralytics/utils/callbacks/tensorboard.py b/ultralytics/utils/callbacks/tensorboard.py
index f5adc31c..5dbe3e1d 100644
--- a/ultralytics/utils/callbacks/tensorboard.py
+++ b/ultralytics/utils/callbacks/tensorboard.py
@@ -70,14 +70,14 @@ def _log_tensorboard_graph(trainer) -> None:
# Try simple method first (YOLO)
try:
trainer.model.eval() # place in .eval() mode to avoid BatchNorm statistics changes
- WRITER.add_graph(torch.jit.trace(torch_utils.de_parallel(trainer.model), im, strict=False), [])
+ WRITER.add_graph(torch.jit.trace(torch_utils.unwrap_model(trainer.model), im, strict=False), [])
LOGGER.info(f"{PREFIX}model graph visualization added ✅")
return
except Exception:
# Fallback to TorchScript export steps (RTDETR)
try:
- model = deepcopy(torch_utils.de_parallel(trainer.model))
+ model = deepcopy(torch_utils.unwrap_model(trainer.model))
model.eval()
model = model.fuse(verbose=False)
for m in model.modules():
diff --git a/ultralytics/utils/loss.py b/ultralytics/utils/loss.py
index a61037d9..ad0fbb0d 100644
--- a/ultralytics/utils/loss.py
+++ b/ultralytics/utils/loss.py
@@ -11,7 +11,7 @@ import torch.nn.functional as F
from ultralytics.utils.metrics import OKS_SIGMA
from ultralytics.utils.ops import crop_mask, xywh2xyxy, xyxy2xywh
from ultralytics.utils.tal import RotatedTaskAlignedAssigner, TaskAlignedAssigner, dist2bbox, dist2rbox, make_anchors
-from ultralytics.utils.torch_utils import autocast
+from ultralytics.utils.torch_utils import autocast, disable_dynamo
from .metrics import bbox_iou, probiou
from .tal import bbox2dist
@@ -215,6 +215,7 @@ class v8DetectionLoss:
self.assigner = TaskAlignedAssigner(topk=tal_topk, num_classes=self.nc, alpha=0.5, beta=6.0)
self.bbox_loss = BboxLoss(m.reg_max).to(device)
self.proj = torch.arange(m.reg_max, dtype=torch.float, device=device)
+ disable_dynamo(self.__class__) # exclude from compile
def preprocess(self, targets: torch.Tensor, batch_size: int, scale_tensor: torch.Tensor) -> torch.Tensor:
"""Preprocess targets by converting to tensor format and scaling coordinates."""
@@ -287,9 +288,14 @@ class v8DetectionLoss:
# Bbox loss
if fg_mask.sum():
- target_bboxes /= stride_tensor
loss[0], loss[2] = self.bbox_loss(
- pred_distri, pred_bboxes, anchor_points, target_bboxes, target_scores, target_scores_sum, fg_mask
+ pred_distri,
+ pred_bboxes,
+ anchor_points,
+ target_bboxes / stride_tensor,
+ target_scores,
+ target_scores_sum,
+ fg_mask,
)
loss[0] *= self.hyp.box # box gain
@@ -388,7 +394,7 @@ class v8SegmentationLoss(v8DetectionLoss):
loss[2] *= self.hyp.cls # cls gain
loss[3] *= self.hyp.dfl # dfl gain
- return loss * batch_size, loss.detach() # loss(box, cls, dfl)
+ return loss * batch_size, loss.detach() # loss(box, seg, cls, dfl)
@staticmethod
def single_mask_loss(
diff --git a/ultralytics/utils/torch_utils.py b/ultralytics/utils/torch_utils.py
index e25c5ebb..83f167cd 100644
--- a/ultralytics/utils/torch_utils.py
+++ b/ultralytics/utils/torch_utils.py
@@ -429,7 +429,7 @@ def get_flops(model, imgsz=640):
return 0.0 # if not installed return 0.0 GFLOPs
try:
- model = de_parallel(model)
+ model = unwrap_model(model)
p = next(model.parameters())
if not isinstance(imgsz, list):
imgsz = [imgsz, imgsz] # expand if int/float
@@ -460,7 +460,7 @@ def get_flops_with_torch_profiler(model, imgsz=640):
"""
if not TORCH_2_0: # torch profiler implemented in torch>=2.0
return 0.0
- model = de_parallel(model)
+ model = unwrap_model(model)
p = next(model.parameters())
if not isinstance(imgsz, list):
imgsz = [imgsz, imgsz] # expand if int/float
@@ -577,17 +577,24 @@ def is_parallel(model):
return isinstance(model, (nn.parallel.DataParallel, nn.parallel.DistributedDataParallel))
-def de_parallel(model):
+def unwrap_model(m: nn.Module) -> nn.Module:
"""
- De-parallelize a model: return single-GPU model if model is of type DP or DDP.
+ Unwrap compiled and parallel models to get the base model.
Args:
- model (nn.Module): Model to de-parallelize.
+ m (nn.Module): A model that may be wrapped by torch.compile (._orig_mod) or parallel wrappers such as
+ DataParallel/DistributedDataParallel (.module).
Returns:
- (nn.Module): De-parallelized model.
+ m (nn.Module): The unwrapped base model without compile or parallel wrappers.
"""
- return model.module if is_parallel(model) else model
+ while True:
+ if hasattr(m, "_orig_mod") and isinstance(m._orig_mod, nn.Module):
+ m = m._orig_mod
+ elif hasattr(m, "module") and isinstance(m.module, nn.Module):
+ m = m.module
+ else:
+ return m
def one_cycle(y1=0.0, y2=1.0, steps=100):
@@ -669,7 +676,7 @@ class ModelEMA:
tau (int, optional): EMA decay time constant.
updates (int, optional): Initial number of updates.
"""
- self.ema = deepcopy(de_parallel(model)).eval() # FP32 EMA
+ self.ema = deepcopy(unwrap_model(model)).eval() # FP32 EMA
self.updates = updates # number of EMA updates
self.decay = lambda x: decay * (1 - math.exp(-x / tau)) # decay exponential ramp (to help early epochs)
for p in self.ema.parameters():
@@ -687,7 +694,7 @@ class ModelEMA:
self.updates += 1
d = self.decay(self.updates)
- msd = de_parallel(model).state_dict() # model state_dict
+ msd = unwrap_model(model).state_dict() # model state_dict
for k, v in self.ema.state_dict().items():
if v.dtype.is_floating_point: # true for FP16 and FP32
v *= d
@@ -997,3 +1004,98 @@ class FXModel(nn.Module):
x = m(x) # run
y.append(x) # save output
return x
+
+
+def disable_dynamo(func: Any) -> Any:
+ """
+ Disable torch.compile/dynamo for a callable when available.
+
+ Args:
+ func (Any): Callable object to wrap. Could be a function, method, or class.
+
+ Returns:
+ func (Any): Same callable, wrapped by torch._dynamo.disable when available, otherwise unchanged.
+
+ Examples:
+ >>> @disable_dynamo
+ ... def fn(x):
+ ... return x + 1
+ >>> # Works even if torch._dynamo is not available
+ >>> _ = fn(1)
+ """
+ if hasattr(torch, "_dynamo"):
+ return torch._dynamo.disable(func)
+ return func
+
+
+def attempt_compile(
+ model: torch.nn.Module,
+ device: torch.device,
+ imgsz: int = 640,
+ use_autocast: bool = False,
+ warmup: bool = False,
+ prefix: str = colorstr("compile:"),
+) -> torch.nn.Module:
+ """
+ Compile a model with torch.compile and optionally warm up the graph to reduce first-iteration latency.
+
+ This utility attempts to compile the provided model using the inductor backend with dynamic shapes enabled and an
+ autotuning mode. If compilation is unavailable or fails, the original model is returned unchanged. An optional
+ warmup performs a single forward pass on a dummy input to prime the compiled graph and measure compile/warmup time.
+
+ Args:
+ model (torch.nn.Module): Model to compile.
+ device (torch.device): Inference device used for warmup and autocast decisions.
+ imgsz (int, optional): Square input size to create a dummy tensor with shape (1, 3, imgsz, imgsz) for warmup.
+ use_autocast (bool, optional): Whether to run warmup under autocast on CUDA or MPS devices.
+ warmup (bool, optional): Whether to execute a single dummy forward pass to warm up the compiled model.
+ prefix (str, optional): Message prefix for logger output.
+
+ Returns:
+ model (torch.nn.Module): Compiled model if compilation succeeds, otherwise the original unmodified model.
+
+ Notes:
+ - If the current PyTorch build does not provide torch.compile, the function returns the input model immediately.
+ - Warmup runs under torch.inference_mode and may use torch.autocast for CUDA/MPS to align compute precision.
+ - CUDA devices are synchronized after warmup to account for asynchronous kernel execution.
+
+ Examples:
+ >>> device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
+ >>> # Try to compile and warm up a model with a 640x640 input
+ >>> model = attempt_compile(model, device=device, imgsz=640, use_autocast=True, warmup=True)
+ """
+ if not hasattr(torch, "compile"):
+ return model
+
+ LOGGER.info(f"{prefix} starting torch.compile...")
+ t0 = time.perf_counter()
+ try:
+ model = torch.compile(model, mode="max-autotune", backend="inductor")
+ except Exception as e:
+ LOGGER.warning(f"{prefix} torch.compile failed, continuing uncompiled: {e}")
+ return model
+ t_compile = time.perf_counter() - t0
+
+ t_warm = 0.0
+ if warmup:
+ # Use a single dummy tensor to build the graph shape state and reduce first-iteration latency
+ dummy = torch.zeros(1, 3, imgsz, imgsz, device=device)
+ if use_autocast and device.type == "cuda":
+ dummy = dummy.half()
+ t1 = time.perf_counter()
+ with torch.inference_mode():
+ if use_autocast and device.type in {"cuda", "mps"}:
+ with torch.autocast(device.type):
+ _ = model(dummy)
+ else:
+ _ = model(dummy)
+ if device.type == "cuda":
+ torch.cuda.synchronize(device)
+ t_warm = time.perf_counter() - t1
+
+ total = t_compile + t_warm
+ if warmup:
+ LOGGER.info(f"{prefix} complete in {total:.1f}s (compile {t_compile:.1f}s + warmup {t_warm:.1f}s)")
+ else:
+ LOGGER.info(f"{prefix} compile complete in {t_compile:.1f}s (no warmup)")
+ return model