Skip to content

Commit dfdaae1

Browse files
vkhalidovfacebook-github-bot
authored andcommitted
added pure computation inference time
Summary: It's more useful to report pure computation time as inference time of a model, rather than the total inference time including data loader and evaluator operations. The results tend to be more stable and reflect better model operation. This adds a sync point to the inference code, which implies however little to no overhead, since synchronization would most likely happen during `evaluator.evaluate(inputs, outputs)` on the next line anyway. Reviewed By: ppwwyyxx Differential Revision: D17905849 fbshipit-source-id: ded18415f14b11c8d6aab3cd333cba7eb1126ed8
1 parent c869c9f commit dfdaae1

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

detectron2/evaluation/evaluator.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,17 @@ def inference_on_dataset(model, data_loader, evaluator):
107107
logging_interval = 50
108108
num_warmup = min(5, logging_interval - 1, total - 1)
109109
start_time = time.time()
110+
total_compute_time = 0
110111
with inference_context(model), torch.no_grad():
111112
for idx, inputs in enumerate(data_loader):
112113
if idx == num_warmup:
113114
start_time = time.time()
115+
total_compute_time = 0
114116

117+
start_compute_time = time.time()
115118
outputs = model(inputs)
119+
torch.cuda.synchronize()
120+
total_compute_time += time.time() - start_compute_time
116121
evaluator.process(inputs, outputs)
117122

118123
if (idx + 1) % logging_interval == 0:
@@ -136,6 +141,12 @@ def inference_on_dataset(model, data_loader, evaluator):
136141
total_time_str, total_time / (total - num_warmup), num_devices
137142
)
138143
)
144+
total_compute_time_str = str(datetime.timedelta(seconds=int(total_compute_time)))
145+
logger.info(
146+
"Total inference pure compute time: {} ({:.6f} s / img per device, on {} devices)".format(
147+
total_compute_time_str, total_compute_time / (total - num_warmup), num_devices
148+
)
149+
)
139150

140151
results = evaluator.evaluate()
141152
# An evaluator may return None when not in main process.

0 commit comments

Comments
 (0)