Skip to content

Commit af857b1

Browse files
Merge pull request #30 from InfiniTensor/fix/flops_calculation
fix - Change flops calculation logic
2 parents d789e90 + 1570445 commit af857b1

1 file changed

Lines changed: 22 additions & 12 deletions

File tree

infinimetrics/operators/infinicore_adapter.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,11 @@ def _parse_runtime_args(self, config: dict) -> dict:
198198

199199
def _handle_latency(self, metric: dict, context: dict):
200200
"""Handle latency metric."""
201-
if context.get("latency_ms") is not None:
201+
avg_latency_sec = context.get("avg_latency_sec")
202+
if avg_latency_sec is not None and avg_latency_sec > 0:
202203
metric.update(
203204
{
204-
"value": context["latency_ms"],
205+
"value": round(avg_latency_sec * 1000, 6), # Convert to ms
205206
"type": "scalar",
206207
"raw_data_url": "",
207208
"unit": "ms",
@@ -215,18 +216,17 @@ def _handle_accuracy(self, metric: dict, context: dict):
215216
def _handle_flops(self, metric: dict, context: dict, config: dict = None):
216217
"""Handle FLOPS metric."""
217218
value = 0.0
219+
avg_latency_sec = context.get("avg_latency_sec")
218220

219-
if context.get("latency_ms") and context.get("latency_ms", 0) > 0:
220-
# Calculate FLOPS from input/output configuration
221+
if avg_latency_sec and avg_latency_sec > 0:
221222
inputs = config.get(OperatorConfig.INPUTS, [])
222223
outputs = config.get(OperatorConfig.OUTPUTS, [])
223224
operator = config.get(OperatorConfig.OPERATOR, "").lower()
224225

225226
flops = FLOPSCalculator.get_flops(operator, inputs, outputs)
226-
latency_sec = context["latency_ms"] / 1000.0
227227

228-
if flops > 0 and latency_sec > 0:
229-
tflops = (flops / latency_sec) / 1e12
228+
if flops > 0:
229+
tflops = (flops / avg_latency_sec) / 1e12
230230
value = tflops if tflops < 0.0001 else round(tflops, 4)
231231

232232
metric.update(
@@ -236,16 +236,16 @@ def _handle_flops(self, metric: dict, context: dict, config: dict = None):
236236
def _handle_bandwidth(self, metric: dict, context: dict, config: dict = None):
237237
"""Handle bandwidth metric."""
238238
value = 0.0
239+
avg_latency_sec = context.get("avg_latency_sec")
239240

240-
if context.get("latency_ms") and context.get("latency_ms", 0) > 0:
241+
if avg_latency_sec and avg_latency_sec > 0:
241242
inputs = config.get(OperatorConfig.INPUTS, [])
242243
outputs = config.get(OperatorConfig.OUTPUTS, [])
243244

244245
bandwidth_info = calculate_bandwidth(inputs, outputs)
245-
latency_sec = context["latency_ms"] / 1000.0
246246

247-
if bandwidth_info["total_bytes"] > 0 and latency_sec > 0:
248-
bandwidth_gbs = (bandwidth_info["total_bytes"] / latency_sec) / 1e9
247+
if bandwidth_info["total_bytes"] > 0:
248+
bandwidth_gbs = (bandwidth_info["total_bytes"] / avg_latency_sec) / 1e9
249249
value = (
250250
bandwidth_gbs if bandwidth_gbs < 0.0001 else round(bandwidth_gbs, 4)
251251
)
@@ -290,8 +290,18 @@ def _convert_from_response(self, saved_files: list, original_req: dict) -> dict:
290290
device_type = saved_data[0].get("device", DEVICE_CPU).upper()
291291
latency_field = PERF_HOST if device_type == DEVICE_CPU else PERF_DEVICE
292292

293+
# Calculate average latency per iteration
294+
# InfiniCore returns total time for all iterations
295+
total_latency_ms = perf_data.get(latency_field)
296+
args_data = saved_data[0].get("args", {})
297+
num_iterations = max(args_data.get("num_iterations", 1), 1)
298+
299+
avg_latency_sec = None
300+
if total_latency_ms and total_latency_ms > 0:
301+
avg_latency_sec = (total_latency_ms / num_iterations) / 1000.0
302+
293303
context = {
294-
"latency_ms": perf_data.get(latency_field),
304+
"avg_latency_sec": avg_latency_sec,
295305
"tflops": tc_result.get(InfiniCoreResult.METRICS, {}).get("tflops"),
296306
"bandwidth_gbs": tc_result.get(InfiniCoreResult.METRICS, {}).get(
297307
"bandwidth_gbs"

0 commit comments

Comments
 (0)