Skip to content

Commit 28425c8

Browse files
Yanghan Wangfacebook-github-bot
Yanghan Wang
authored andcommitted
fix inference accuracy test
Summary: Pull Request resolved: #5348 Some accuracy tests started to fail in between Jun 11 and Jun 17: - ❌ mask_rcnn_R_50_FPN_inference_acc_test - ✅ keypoint_rcnn_R_50_FPN_inference_acc_test - ✅ fast_rcnn_R_50_FPN_inference_acc_test - ❌ panoptic_fpn_R_50_inference_acc_test - ✅ retinanet_R_50_FPN_inference_acc_test - ❌ rpn_R_50_FPN_inference_acc_test - ✅ semantic_R_50_FPN_inference_acc_test - ❌ cascade_mask_rcnn_R_50_FPN_inference_acc_test V1: update the yaml to reflect the new scores. V5: it turns out that we can match the old scores by disabling tf32. Differential Revision: D61301698
1 parent bcfd464 commit 28425c8

10 files changed

+44
-0
lines changed

configs/quick_schedules/cascade_mask_rcnn_R_50_FPN_inference_acc_test.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ DATASETS:
55
TEST: ("coco_2017_val_100",)
66
TEST:
77
EXPECTED_RESULTS: [["bbox", "AP", 50.18, 0.02], ["segm", "AP", 43.87, 0.02]]
8+
FLOAT32_PRECISION: "highest"

configs/quick_schedules/fast_rcnn_R_50_FPN_inference_acc_test.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ DATASETS:
55
TEST: ("coco_2017_val_100",)
66
TEST:
77
EXPECTED_RESULTS: [["bbox", "AP", 45.70, 0.02]]
8+
FLOAT32_PRECISION: "highest"

configs/quick_schedules/keypoint_rcnn_R_50_FPN_inference_acc_test.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ DATASETS:
55
TEST: ("keypoints_coco_2017_val_100",)
66
TEST:
77
EXPECTED_RESULTS: [["bbox", "AP", 52.47, 0.02], ["keypoints", "AP", 67.36, 0.02]]
8+
FLOAT32_PRECISION: "highest"

configs/quick_schedules/mask_rcnn_R_50_C4_inference_acc_test.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ DATASETS:
55
TEST: ("coco_2017_val_100",)
66
TEST:
77
EXPECTED_RESULTS: [["bbox", "AP", 47.37, 0.02], ["segm", "AP", 40.99, 0.02]]
8+
FLOAT32_PRECISION: "highest"

configs/quick_schedules/mask_rcnn_R_50_DC5_inference_acc_test.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ DATASETS:
55
TEST: ("coco_2017_val_100",)
66
TEST:
77
EXPECTED_RESULTS: [["bbox", "AP", 47.44, 0.02], ["segm", "AP", 42.94, 0.02]]
8+
FLOAT32_PRECISION: "highest"

configs/quick_schedules/mask_rcnn_R_50_FPN_inference_acc_test.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ TEST:
88
AUG:
99
ENABLED: True
1010
MIN_SIZES: (700, 800) # to save some time
11+
FLOAT32_PRECISION: "highest"

configs/quick_schedules/panoptic_fpn_R_50_inference_acc_test.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ DATASETS:
55
TEST: ("coco_2017_val_100_panoptic_separated",)
66
TEST:
77
EXPECTED_RESULTS: [["bbox", "AP", 46.47, 0.02], ["segm", "AP", 43.39, 0.02], ["sem_seg", "mIoU", 42.55, 0.02], ["panoptic_seg", "PQ", 38.99, 0.02]]
8+
FLOAT32_PRECISION: "highest"

configs/quick_schedules/rpn_R_50_FPN_inference_acc_test.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ DATASETS:
55
TEST: ("coco_2017_val_100",)
66
TEST:
77
EXPECTED_RESULTS: [["box_proposals", "AR@1000", 58.16, 0.02]]
8+
FLOAT32_PRECISION: "highest"

detectron2/config/defaults.py

+4
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,10 @@
636636
# for about 10k iterations. It usually hurts total time, but can benefit for certain models.
637637
# If input images have the same or similar sizes, benchmark is often helpful.
638638
_C.CUDNN_BENCHMARK = False
639+
# Option to set PyTorch matmul and CuDNN's float32 precision. When set to non-empty string,
640+
# the corresponding precision ("highest", "high" or "medium") will be used. The highest
641+
# precision will effectively disable tf32.
642+
_C.FLOAT32_PRECISION = ""
639643
# The period (in terms of steps) for minibatch visualization at train time.
640644
# Set to 0 to disable.
641645
_C.VIS_PERIOD = 0

detectron2/engine/defaults.py

+32
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,30 @@ def _highlight(code, filename):
171171
return code
172172

173173

174+
# adapted from:
175+
# https://github.com/pytorch/tnt/blob/ebda066f8f55af6a906807d35bc829686618074d/torchtnt/utils/device.py#L328-L346
176+
def _set_float32_precision(precision: str = "high") -> None:
177+
"""Sets the precision of float32 matrix multiplications and convolution operations.
178+
179+
For more information, see the PyTorch docs:
180+
- https://pytorch.org/docs/stable/generated/torch.set_float32_matmul_precision.html
181+
- https://pytorch.org/docs/stable/backends.html#torch.backends.cudnn.allow_tf32
182+
183+
Args:
184+
precision: The setting to determine which datatypes to use for matrix
185+
multiplication and convolution operations.
186+
"""
187+
if not (torch.cuda.is_available()): # Not relevant for non-CUDA devices
188+
return
189+
# set precision for matrix multiplications
190+
torch.set_float32_matmul_precision(precision)
191+
# set precision for convolution operations
192+
if precision == "highest":
193+
torch.backends.cudnn.allow_tf32 = False
194+
else:
195+
torch.backends.cudnn.allow_tf32 = True
196+
197+
174198
def default_setup(cfg, args):
175199
"""
176200
Perform some basic common setups at the beginning of a job, including:
@@ -226,6 +250,14 @@ def default_setup(cfg, args):
226250
cfg, "CUDNN_BENCHMARK", "train.cudnn_benchmark", default=False
227251
)
228252

253+
fp32_precision = _try_get_key(cfg, "FLOAT32_PRECISION", "train.float32_precision", default="")
254+
if fp32_precision != "":
255+
logger.info(f"Set fp32 precision to {fp32_precision}")
256+
_set_float32_precision(fp32_precision)
257+
logger.info(f"{torch.get_float32_matmul_precision()=}")
258+
logger.info(f"{torch.backends.cuda.matmul.allow_tf32=}")
259+
logger.info(f"{torch.backends.cudnn.allow_tf32=}")
260+
229261

230262
def default_writers(output_dir: str, max_iter: Optional[int] = None):
231263
"""

0 commit comments

Comments
 (0)