Timing Models

Every HwModule carries two models. The functional model says what it computes; the timing model says how long that takes – when, in simulated time, the work finishes.

Most of the time you do not write one. The transfers time themselves: a stream get / write, a memory read, a bus transaction advance simulated time by how long the transfer takes, and a stall lengthens them on its own. What the framework cannot know is how long your compute takes – the body of a custom hook – and that is what a module declares.

Declare it

A module states its timing as timed phases: spans between events at its own ports – the first or last beat of a transfer – each a function of features known before it starts, predicted by a model. The figure on that page shows the polynomial kernel’s three spans on a real RTL trace.

  • What a timed phase is – events, spans, features, the two rules, on a trace.
  • LT vs CT models – why a transfer is one event, not one per cycle: the bet that makes a whole-system Python simulation fast.
  • Declaring a timed phase – TimedPhase, add_timing_phase, the rules a declaration is held to.
  • Sequential bodies – the whole input, then the compute: one span, yield phase.timeout(...).
  • Pipelined bodies – compute while the input arrives: a latency and a fill, phase.pipeline(...).
  • The form of a span’s model – a pipelined loop costs latency + ii·(m − 1), and how that maps onto the spans.

Calibrate it

  • Calibrating a timed phase – generated from the declaration: the RTL transfers on the named ports, paired with pysim’s records, fitted per span; poly re-fitted from its committed trace.

What modules do not declare is timed by the platform and the framework, calibrated once and reused:

  • The bus-transfer model – how long the interconnect takes to move n words in k bursts: a property of the platform, fit once per platform and reused by every accelerator.
  • Component residuals and the mem-stream residual – how the framework’s memory-mapped stream blocks were calibrated before timed phases: the gap between RTL and pysim, fit per (component, platform). They keep that method until they are declared as phases.
  • Polling Overhead – the loosely-timed model of MMIFMaster.poll_until: the bandwidth a poll loop steals from its bus and the discovery latency.

Compose it

  • Timing a composite – what a pipeline’s, a loop’s or a pool’s pysim needs beyond calibrated blocks: every task in it timed, each block calibrated on the patterns it sees there (gapped frames included), cut-through at burst granularity, and a cycle gate against the RTL.

Software is timed the same way, on a processor rather than a component: a processor model prices a Python function’s work counters with models calibrated on gem5.

The two-level split: bus vs component

For a component that moves data over m_axi, the run’s cost splits:

    RTL cycles  =  bus transfer  +  component control
                    └─ PLATFORM ─┘   └── COMPONENT ──┘

The bus transfer – how long the interconnect takes to move n words in k bursts – is a property of the platform (memory system + AXI adapter), so it is fit once per platform and reused by every accelerator (BusCalib). With that charged in pysim, what is left is the component’s own time – a timed phase, or for the framework’s mem streams, a fitted residual (StreamTimingModel). A new accelerator on a calibrated platform inherits the bus term for nothing and only has to calibrate what is genuinely its own.

Everything is stored in cycles

Fitted timing numbers are cycles, not seconds, so the artifact is clock-independent: re-deploying at a different simulation frequency needs no refit. The clock that does change them is the synthesis clock, which is why a platform is keyed by part and period.

The machinery underneath – the model base, the corpus format, the confidence levels – is axis-agnostic and lives in Model calibration. Resource models use the same base; only the source of a number differs.

See also

  • Simulation timing model – the Clock, self.timeout, and where transfer vs. compute latency is charged. This section assumes that page.
  • Model calibration – the shared CalibModel, corpus and confidence the calibration half is built on.
  • Timing Analysis Tools – the measurement side: cycle counts and port transfers from a VCD / cosim run, which is where the datapoints come from.
  • Custom hooks – the hand-written compute whose timing you declare here.

Table of contents

  • What a timed phase is - A module's timing is stated as timed phases: spans between events at its own ports -- the first or last beat of a transfer -- whose durations are a function of features known before the span starts, learned by a model. Shown on a real RTL trace of the polynomial kernel: a 4-cycle response, a 34-cycle latency and a 7-cycle fill. Everything a calibration needs -- what to probe, what to log, how to pair pysim with RTL -- follows from the declaration.
  • LT vs CT models - Loosely-timed (LT) vs cycle-timed (CT) simulation. Waveflow models timing loosely — one timed event per transaction, not per clock edge — which is what makes a whole-system Python simulation fast, and is the same choice established computer-architecture simulators make.
  • Declaring a timed phase - How a module declares its timing: build a TimedPhase from spans between its own endpoints' events (ep.first / ep.last), the features the durations depend on, and seed numbers; register it with add_timing_phase, like an endpoint. The phase's model predicts each span in cycles with no simulator. The rules: a span uses the module's own endpoints, an event names an operation not a role or a message, features are checked by name. The phase charges only the module's own time; the transfers time themselves, and a stall lengthens a firing on its own.
  • Sequential bodies - A sequential body -- the whole input, then the compute, then the output -- is one span from the input's last beat to the output's first. The body reads and writes as usual and waits the phase with `yield phase.timeout(**features)` between them; the next write closes the span's record. Run on a small module: one 4-word burst, an 11-cycle span, the result delivered at 18. Declaring the span also says the body reads sequentially, so a burst that sat in the input buffer is read at a word a cycle, as the RTL reads it.
  • Pipelined bodies - A pipelined body -- the loop that reads a word, computes, writes a word -- overlaps its input and output: the first output comes a latency after the first input, and later outputs are paced by the slower of input arrival and the loop's rate. Declared as two spans, latency (input first -> output first) and fill_time (output first -> output last), and run with `phase.pipeline(compute, load=ep.get_array(T, n), store=ep.write_array(T, n), **features)`, which waits exactly that race from real events and returns what it did. Run on a small module: 8 words, latency 5, fill 7.
  • The form of a span's model - What a span's model is for the common case, a pipelined loop: m iterations cost latency + ii·(m − 1) cycles. A sequential span is that whole formula; a pipelined phase splits it into its two spans, latency (a constant) and fill_time (ii·(m − 1)). Both are linear in the features, which is what LinearTiming fits: an intercept plus a coefficient per feature, one LinCalibModel per span. Poly's fitted fill_time, -1 + 1·nwords, is ii·(m − 1) with ii = 1. Unrolling makes the trip count ceil(n / U): compute it in the body and pass it as the feature.
  • Calibrating a timed phase - Calibrating a module's timed phases is generated from its declarations (waveflow/calib/phases.py): the ports to observe, their transfers read off an RTL trace, the pairing with pysim's phase records (pysim gives which transfers and the features, the RTL the times), and a per-span fit with interior points held out. The one hand-written piece is a scenario that produces the features. Shown re-fitting poly from its committed trace: response 4, latency 34, fill nwords - 1, held-out lengths exact. And the lesson poly taught: exact spans do not make an exact total when some of the module's time is in no span.
  • Component residuals - A component's control residual is the delay pysim is MISSING once the bus term is charged: residual = rtl_span - pysim_span + current_dly, fit per (component, platform). TimingModel collects RTL firings (from a trace) and pysim firings (from a run) into independent trees, joins them on the feature point (nwords, num_trans), and fits a CalibModel; predict() returns the delay a FreeRunMod injects via timed_delay. Stored shared (platform_dir -> the committed library) or custom (calib_dir -> a project dir). CollectTimingStep / FitTimingStep automate it.
  • The bus-transfer model - BusCalib fits the platform's m_axi transfer law — span(num_trans, nwords) per direction — and stores it in mm_bus.json beside the platform manifest. The datapoints are measured component-independently off the memory port (measure_bus_span groups beats into transfers by idle gaps); a sweep accumulates a per-run corpus (add_run -> points/) then fits. bus_timing() hands back a configured BusTiming the memory slave charges during pysim, or an unconfigured one (word_bw fallback) on an uncalibrated platform. CalibBusStep automates it as a DAG step.
  • Polling Overhead - The loosely-timed polling-overhead model — MMIFMaster.poll_until with a restricted PollCond, the per-bus bandwidth-steal (ov) derating and the deterministic discovery-latency delay, modeled in O(transactions) rather than by stepping every poll cycle.
  • The mem-stream residual - The reusable m_axi mem-streams (MemRStream / MemWStream) are timed by the bus law plus a per-firing CONTROL residual — a (component, platform) property fit once and reused by every accelerator. The residual is fit by a per-component FIXTURE under waveflow/calib/fixtures/ (mem_w_stream.py, mem_r_stream.py): a ComponentFixture declares the component id, the regression basis, a size sweep, a run_pysim that drives the component STANDALONE (StreamDriver -> component -> sink, bus law on the memory so the residual is control-only), and rtl_firings (measured spans or None). fixture.calibrate(platform) sweeps, collects RTL + pysim, and fits into components//params.json. An accelerator loads it by pointing its mem stages at the platform. memcpy is writer-bound so only the writer fixture existed; the reader-bound interleaver is what forced mem_r_stream.py.
  • Timing a composite - What a composite's pysim needs beyond calibrated blocks, learned on os_filterbank (a loop of eight tasks around one shared FFT, 27% short of its RTL while every gate passed, now within 2.7%): every task in the loop timed, not only the library's; each block calibrated on the input patterns it sees in the composite, gapped frames included; cut-through at burst granularity, not store-and-forward and not word by word; and a cycle gate against the RTL, both directions, a stated tolerance. A checklist, the reason for each item, and what it cost in speed (1-4% more wall time).