Metrix treats counters from independent profiler executions as correlated samples
Summary
Metrix can split a derived metric’s required hardware counters across multiple profiler passes. Each pass reruns the target application, but results with matching ordinal replay and dispatch IDs are merged as if they came from one execution.
This can produce derived rates and extrema from counter values and durations that were never observed together.
Current behavior
CounterBackend.profile() in metrix/src/metrix/backends/base.py:
- Splits incompatible counters into separate passes (
_split_counters_into_passes/_get_counter_groups). - Reruns the target for every pass and replay (
_run_rocprofin the per-replay loop). - Assigns the same ordinal
run_idto separate executions (r.run_id = replay_idis reset independently in each pass). - Merges results by
(kernel_name, dispatch_id, run_id)viacounters.update(), keeping the first pass’sduration_ns. - Computes derived metric statistics from that merged data (
compute_metric_stats/_compute_with_stat_typetake min/max/avg of each input independently).
Matching identifiers or dispatch counts only establish similar-looking populations. They do not make two application executions correlated.
Metric batching has the same problem: when len(metrics) > MAX_METRICS_PER_BATCH (6), batches rerun the target independently and merge _aggregated outputs by kernel/dispatch key without preserving execution provenance.
Missing required counters can still become valid zeros: @metric wrappers use self._raw_data.get(counter, 0), and _compute_with_stat_type omits absent counters from _raw_data before calling compute().
Generic reproduction
Use a fake backend where:
- one derived metric requires five counters;
- the hardware block allows four counters per pass;
- pass 1 returns counters A–D with a 100 µs duration;
- pass 2 returns counter E with a 200 µs duration;
- both passes return the same kernel name, dispatch ID, and replay ID.
Metrix accepts and merges these results into one synthetic sample, even though no execution produced counters A–E with either duration.
A variable workload can also execute different dispatches while retaining equal kernel counts, allowing population checks based only on counts to pass.
Expected behavior
- Preserve physical pass and execution provenance.
- Call a metric sample “correlated” only when every required input came from the same profiler execution.
- Treat matching population signatures as comparability validation, not correlation.
- When a metric inherently requires separate executions, expose a documented aggregate estimate or report unsupported extrema instead of manufacturing per-replay min/max values.
- Reject population drift across counter passes and metric batches (equal counts with different dispatch IDs, or equal IDs with different grid/workgroup shapes).
- Never convert missing required inputs into valid zero values.
- Preserve existing public per-dispatch duration semantics.
Regression coverage
Please cover:
- equal counts with different dispatch IDs;
- equal IDs with different grid or workgroup shapes;
- different durations across counter passes;
- metrics split by hardware counter limits;
- more-than-six-metric batch execution;
- missing required counters;
- deterministic aggregate-estimate behavior;
- API, CLI, JSON, and MCP result consistency.
Relationship to PR #190
PR #190 fixes a related but separate problem: same-kernel aggregation left counters and duration on different bases (counters summed, duration averaged). It merges repeat dispatches into a single average dispatch — dividing counters by the dispatch count as well — so rate metrics stay consistent and duration_ns keeps its public per-dispatch meaning.
This issue concerns counters collected from entirely separate application executions and should remain distinct from that fix.
@mehdi-saeedi can we close this now?
Thanks @mawad-amd. #198 resolved part of this: a missing input is no longer a valid zero, duration_ns keeps its per-dispatch meaning, weighted metrics keep their inputs in one batch, and metrics that opt in with requires_consistent_passes reject passes whose dispatch populations differ.
#200 covers the population checks that were still open: every multi-pass profile is validated (not only opted-in metrics), the population includes grid and workgroup shapes, and batches of more than six metrics are validated before they merge.
That leaves one point from this issue: how a metric that inherently needs separate executions should report extrema (a documented aggregate estimate, or no per-replay min/max). Once #200 lands, I'm happy to close this and open a narrower issue for that, unless you'd rather keep it here.
Closing this now that #200 has landed. Together with #198 it covers what this issue asked for: missing inputs are no longer valid zeros, duration_ns keeps its per-dispatch meaning, and every multi-pass profile and every metric batch is checked to have run the same dispatches (including grid and workgroup shapes) before counters are merged. The one remaining point, how a metric that inherently needs separate executions should report per-replay extrema, is narrow enough to raise on its own if it comes up. Thanks @mawad-amd for the review.