From 4cd5fda79b132ddac04a35cea76a6d757c5fb9dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Faruk=20G=C3=9CM=C3=9C=C5=9ETA=C5=9E?= Date: Fri, 15 May 2026 13:55:23 +0300 Subject: [PATCH] Fix `get_flops` dtype mismatch on half-precision models (#24504) Co-authored-by: Glenn Jocher --- ultralytics/utils/torch_utils.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ultralytics/utils/torch_utils.py b/ultralytics/utils/torch_utils.py index 2f7fd7b6..1eb17e45 100644 --- a/ultralytics/utils/torch_utils.py +++ b/ultralytics/utils/torch_utils.py @@ -457,12 +457,12 @@ def get_flops(model, imgsz=640): try: # Method 1: Use stride-based input tensor stride = max(int(model.stride.max()), 32) if hasattr(model, "stride") else 32 # max stride - im = torch.empty((1, p.shape[1], stride, stride), device=p.device) # input image in BCHW format + im = torch.empty((1, p.shape[1], stride, stride), device=p.device, dtype=p.dtype) # input image in BCHW flops = thop.profile(deepcopy(model), inputs=[im], verbose=False)[0] / 1e9 * 2 # stride GFLOPs return flops * imgsz[0] / stride * imgsz[1] / stride # imgsz GFLOPs except Exception: # Method 2: Use actual image size (required for RTDETR models) - im = torch.empty((1, p.shape[1], *imgsz), device=p.device) # input image in BCHW format + im = torch.empty((1, p.shape[1], *imgsz), device=p.device, dtype=p.dtype) # input image in BCHW format return thop.profile(deepcopy(model), inputs=[im], verbose=False)[0] / 1e9 * 2 # imgsz GFLOPs except Exception: return 0.0 @@ -487,14 +487,14 @@ def get_flops_with_torch_profiler(model, imgsz=640): try: # Use stride size for input tensor stride = (max(int(model.stride.max()), 32) if hasattr(model, "stride") else 32) * 2 # max stride - im = torch.empty((1, p.shape[1], stride, stride), device=p.device) # input image in BCHW format + im = torch.empty((1, p.shape[1], stride, stride), device=p.device, dtype=p.dtype) # input image in BCHW with torch.profiler.profile(with_flops=True) as prof: model(im) flops = sum(x.flops for x in prof.key_averages()) / 1e9 flops = flops * imgsz[0] / stride * imgsz[1] / stride # 640x640 GFLOPs except Exception: # Use actual image size for input tensor (i.e. required for RTDETR models) - im = torch.empty((1, p.shape[1], *imgsz), device=p.device) # input image in BCHW format + im = torch.empty((1, p.shape[1], *imgsz), device=p.device, dtype=p.dtype) # input image in BCHW format with torch.profiler.profile(with_flops=True) as prof: model(im) flops = sum(x.flops for x in prof.key_averages()) / 1e9