test_cuda.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
  2. from itertools import product
  3. from pathlib import Path
  4. import pytest
  5. import torch
  6. from tests import CUDA_DEVICE_COUNT, CUDA_IS_AVAILABLE, MODEL, SOURCE
  7. from ultralytics import YOLO
  8. from ultralytics.cfg import TASK2DATA, TASK2MODEL, TASKS
  9. from ultralytics.utils import ASSETS, WEIGHTS_DIR
  10. from ultralytics.utils.checks import check_amp
  11. def test_checks():
  12. """Validate CUDA settings against torch CUDA functions."""
  13. assert torch.cuda.is_available() == CUDA_IS_AVAILABLE
  14. assert torch.cuda.device_count() == CUDA_DEVICE_COUNT
  15. @pytest.mark.skipif(not CUDA_IS_AVAILABLE, reason="CUDA is not available")
  16. def test_amp():
  17. """Test AMP training checks."""
  18. model = YOLO("yolo11n.pt").model.cuda()
  19. assert check_amp(model)
  20. @pytest.mark.slow
  21. @pytest.mark.skipif(True, reason="CUDA export tests disabled pending additional Ultralytics GPU server availability")
  22. @pytest.mark.skipif(not CUDA_IS_AVAILABLE, reason="CUDA is not available")
  23. @pytest.mark.parametrize(
  24. "task, dynamic, int8, half, batch",
  25. [ # generate all combinations but exclude those where both int8 and half are True
  26. (task, dynamic, int8, half, batch)
  27. # Note: tests reduced below pending compute availability expansion as GPU CI runner utilization is high
  28. # for task, dynamic, int8, half, batch in product(TASKS, [True, False], [True, False], [True, False], [1, 2])
  29. for task, dynamic, int8, half, batch in product(TASKS, [True], [True], [False], [2])
  30. if not (int8 and half) # exclude cases where both int8 and half are True
  31. ],
  32. )
  33. def test_export_engine_matrix(task, dynamic, int8, half, batch):
  34. """Test YOLO model export to TensorRT format for various configurations and run inference."""
  35. file = YOLO(TASK2MODEL[task]).export(
  36. format="engine",
  37. imgsz=32,
  38. dynamic=dynamic,
  39. int8=int8,
  40. half=half,
  41. batch=batch,
  42. data=TASK2DATA[task],
  43. workspace=1, # reduce workspace GB for less resource utilization during testing
  44. simplify=True, # use 'onnxslim'
  45. )
  46. YOLO(file)([SOURCE] * batch, imgsz=64 if dynamic else 32) # exported model inference
  47. Path(file).unlink() # cleanup
  48. Path(file).with_suffix(".cache").unlink() if int8 else None # cleanup INT8 cache
  49. @pytest.mark.skipif(not CUDA_IS_AVAILABLE, reason="CUDA is not available")
  50. def test_train():
  51. """Test model training on a minimal dataset using available CUDA devices."""
  52. device = 0 if CUDA_DEVICE_COUNT == 1 else [0, 1]
  53. YOLO(MODEL).train(data="coco8.yaml", imgsz=64, epochs=1, device=device) # requires imgsz>=64
  54. @pytest.mark.slow
  55. @pytest.mark.skipif(not CUDA_IS_AVAILABLE, reason="CUDA is not available")
  56. def test_predict_multiple_devices():
  57. """Validate model prediction consistency across CPU and CUDA devices."""
  58. model = YOLO("yolo11n.pt")
  59. model = model.cpu()
  60. assert str(model.device) == "cpu"
  61. _ = model(SOURCE) # CPU inference
  62. assert str(model.device) == "cpu"
  63. model = model.to("cuda:0")
  64. assert str(model.device) == "cuda:0"
  65. _ = model(SOURCE) # CUDA inference
  66. assert str(model.device) == "cuda:0"
  67. model = model.cpu()
  68. assert str(model.device) == "cpu"
  69. _ = model(SOURCE) # CPU inference
  70. assert str(model.device) == "cpu"
  71. model = model.cuda()
  72. assert str(model.device) == "cuda:0"
  73. _ = model(SOURCE) # CUDA inference
  74. assert str(model.device) == "cuda:0"
  75. @pytest.mark.skipif(not CUDA_IS_AVAILABLE, reason="CUDA is not available")
  76. def test_autobatch():
  77. """Check optimal batch size for YOLO model training using autobatch utility."""
  78. from ultralytics.utils.autobatch import check_train_batch_size
  79. check_train_batch_size(YOLO(MODEL).model.cuda(), imgsz=128, amp=True)
  80. @pytest.mark.slow
  81. @pytest.mark.skipif(not CUDA_IS_AVAILABLE, reason="CUDA is not available")
  82. def test_utils_benchmarks():
  83. """Profile YOLO models for performance benchmarks."""
  84. from ultralytics.utils.benchmarks import ProfileModels
  85. # Pre-export a dynamic engine model to use dynamic inference
  86. YOLO(MODEL).export(format="engine", imgsz=32, dynamic=True, batch=1)
  87. ProfileModels([MODEL], imgsz=32, half=False, min_time=1, num_timed_runs=3, num_warmup_runs=1).profile()
  88. @pytest.mark.skipif(not CUDA_IS_AVAILABLE, reason="CUDA is not available")
  89. def test_predict_sam():
  90. """Test SAM model predictions using different prompts, including bounding boxes and point annotations."""
  91. from ultralytics import SAM
  92. from ultralytics.models.sam import Predictor as SAMPredictor
  93. # Load a model
  94. model = SAM(WEIGHTS_DIR / "sam2.1_b.pt")
  95. # Display model information (optional)
  96. model.info()
  97. # Run inference
  98. model(SOURCE, device=0)
  99. # Run inference with bboxes prompt
  100. model(SOURCE, bboxes=[439, 437, 524, 709], device=0)
  101. # Run inference with no labels
  102. model(ASSETS / "zidane.jpg", points=[900, 370], device=0)
  103. # Run inference with 1D points and 1D labels
  104. model(ASSETS / "zidane.jpg", points=[900, 370], labels=[1], device=0)
  105. # Run inference with 2D points and 1D labels
  106. model(ASSETS / "zidane.jpg", points=[[900, 370]], labels=[1], device=0)
  107. # Run inference with multiple 2D points and 1D labels
  108. model(ASSETS / "zidane.jpg", points=[[400, 370], [900, 370]], labels=[1, 1], device=0)
  109. # Run inference with 3D points and 2D labels (multiple points per object)
  110. model(ASSETS / "zidane.jpg", points=[[[900, 370], [1000, 100]]], labels=[[1, 1]], device=0)
  111. # Create SAMPredictor
  112. overrides = dict(conf=0.25, task="segment", mode="predict", imgsz=1024, model=WEIGHTS_DIR / "mobile_sam.pt")
  113. predictor = SAMPredictor(overrides=overrides)
  114. # Set image
  115. predictor.set_image(ASSETS / "zidane.jpg") # set with image file
  116. # predictor(bboxes=[439, 437, 524, 709])
  117. # predictor(points=[900, 370], labels=[1])
  118. # Reset image
  119. predictor.reset_image()