From 09065ccf673e03c65ad02678999904b793864778 Mon Sep 17 00:00:00 2001 From: Reiase Date: Fri, 22 Jul 2022 22:28:40 +0800 Subject: [PATCH] use median latency instead of mean latency (#78) Signed-off-by: reiase --- nebullvm/measure.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/nebullvm/measure.py b/nebullvm/measure.py index b42312da..2b17b9c6 100644 --- a/nebullvm/measure.py +++ b/nebullvm/measure.py @@ -70,7 +70,7 @@ def compute_tf_latency( def compute_optimized_running_time( - optimized_model: BaseInferenceLearner, steps: int = 100 + optimized_model: BaseInferenceLearner, steps: int = 100, min_steps=5 ) -> float: """Compute the running time of the optimized model. @@ -86,11 +86,22 @@ def compute_optimized_running_time( model_inputs = optimized_model.get_inputs_example() latencies = [] + last_median = None for _ in range(steps): starting_time = time.time() _ = optimized_model.predict(*model_inputs) latencies.append(time.time() - starting_time) - return sum(latencies) / steps + if len(latencies) > min_steps: + median = np.median(latencies) + diff = ( + np.abs(median - last_median) / last_median + if last_median is not None + else 1.0 + ) + if diff < 0.05: + return median + last_median = median + return np.median(latencies) def compute_relative_difference(