Fix get_flops dtype mismatch on half-precision models (#24504)

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
This commit is contained in:
Ahmet Faruk GÜMÜŞTAŞ 2026-05-15 13:55:23 +03:00 committed by GitHub
parent 14ea57b119
commit 4cd5fda79b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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