Defining the kernel in Python and HLS

A kernel is written twice:

  1. In Python – a module whose run_iter says what one firing does. It is the model pysim runs, and the specification of the hardware.
  2. In HLS C++ – the body Vitis synthesizes. It is the hardware, written to read the same way as run_iter, line for line.

Nothing makes the two agree by construction except the parts that are generated from one source: the message structs and their readers and writers (from the schemas), the lane routines (from the element types), the top and its ports (from the module’s ports). The rest – the loop, the arithmetic, the error handling – is in both, and the RTL comparison is what checks them against each other: pysim runs the Python, the RTL runs the HLS, and compare requires the same words (Functional verification). So write the Python first, verify it against the golden, then write the HLS to match it. (A simple body can be generated from run_iter instead of written – how a body is realized – but the kernels in this flow write theirs.)

Everything else is derived from the one Python class: the testbench, the RTL top, the calibration.

A kernel is a FreeRunMod: a leaf with its own run_iter, or a composite of child kernels. This page is the leaf, the one with a body to write; a composite is its children and their wiring (add_comp, add_if), and nothing else (Kernels).

A leaf kernel: the structure

Every leaf kernel has the same parts. The polynomial’s, with each part’s job:

@dataclass
class PolyKernel(FreeRunMod):
    cpp_kernel_name: ClassVar[str | None] = "poly_kernel"     # its name in C++: the top, the task
    calib_holdout: ClassVar[dict] = {"nwords": CALIB_HOLDOUT}  # values held out of the timing fit
    mm_views: ClassVar[tuple] = (...)                          # how a bus reaches it, behind one

    clk: Clock = field(default_factory=lambda: Clock(freq=100e6))   # parameters are dataclass fields

    def __post_init__(self) -> None:      # the structure: its streams and its timed phases
        ...
    def run_iter(self):                   # the behavior: one firing, one command
        ...
    def kernel_task(self):                # the HLS body that realizes run_iter
        ...
    def workload(self, kind="test"):      # the scenarios its harness plays
        ...
part what it says who uses it
cpp_kernel_name the kernel’s name in C++ code generation: the top, the task, the csynth script
dataclass fields build parameters (HwParam: a width, a depth) and the clock every flow: a different value is a different module
__post_init__ its streams, each end registered with add_endpoint; the timed phases pysim, the harness, the RTL top, calibration
run_iter one firing: read a message, do the work, write the answer pysim – and the specification of the HLS body
kernel_task() which hand-written HLS body realizes run_iter, and its argument order code generation
workload(kind) the scenarios, {input stream: bursts}: "test", "calib" its harness
mm_views, calib_holdout optional: its memory-mapped side; the calibration’s held-out values a bus system; calibration

Its streams

def __post_init__(self) -> None:
    super().__post_init__()
    self.s_in = FramedStreamIFSlave(name=f"{self.name}_s_in", sim=self.sim, bitwidth=W)
    self.m_out = FramedStreamIFMaster(name=f"{self.name}_m_out", sim=self.sim, bitwidth=W)
    self.m_status = FramedStreamIFMaster(name=f"{self.name}_m_status", sim=self.sim, bitwidth=W)
    for ep in (self.s_in, self.m_out, self.m_status):
        self.add_endpoint(ep)
    self.samp_phase = TimedPhase("samples", spans={...}, features=("nwords",), calib_dir=HERE / "calib")
    self.status_phase = TimedPhase("status", spans={...}, calib_dir=HERE / "calib")
    for ph in (self.samp_phase, self.status_phase):
        self.add_timing_phase(ph)

An input stream is declared by its slave end (s_...), an output stream by its master end (m_...); a framed stream carries TLAST, because the protocol is made of messages (Kernels). The timed phases are the module’s timing, declared here and calibrated later (Timing and resources).

The HLS body it names

def kernel_task(self):
    return KernelTask("poly_kernel_task", "poly_kernel_task.h", ("s_in", "m_out", "m_status"),
                      template_args=(W,))

The body’s streams are named in the order the C++ function takes them; the generator writes the top around it (Simulating at RTL). A leaf whose body can be extracted from run_iter names none.

The command-response kernel in Python

For a command-response kernel, run_iter is one job – in general form:

# __post_init__: the job loop is a pipelined timed phase -- a latency, then the results at its rate
self.job_phase = TimedPhase(
    "job", spans={"latency": (self.s_in.first, self.m_out.first),
                  "fill_time": (self.m_out.first, self.m_out.last)}, features=("n",))
self.add_timing_phase(self.job_phase)

def run_iter(self):
    hdr = yield from self.s_in.get_schema(CmdHdr)                  # 1. the command
    n = hdr.n
    yield from self.m_out.write(Resp(tx_id=hdr.tx_id, n=n))        #    the results' header
    r = yield from self.job_phase.pipeline(                        # 2. n elements in, the work,
        lambda x: array(S64, golden(x.val)),                       #    vectorized, n results out --
        load=self.s_in.get_array(S16, n),                         #    overlapped, as in hardware
        store=self.m_out.write_array(S64, n), n=n)
    code = StatusCode.OK if len(r.y.val) == n else StatusCode.TLAST_EARLY
    yield from self.m_status.write(Status(tx_id=hdr.tx_id, status=code))   # 3. the status
    if code != StatusCode.OK:
        yield self.event()                                         # fatal: never triggered
  • The work is the golden, called on the whole job at once – a NumPy expression, not a Python loop over elements (Vectorization).
  • The timing is a timed phase with the HLS loop’s two numbers as its spans: the latency from the first element in to the first result out, and the fill at the loop’s rate. pipeline starts the results a latency after the first element arrived, so reading, computing and writing overlap as they do in hardware (Pipelined bodies); the numbers come from calibrating the phase, not the code.
  • A fatal error waits on an event nothing triggers: the module stops taking input, as the RTL does.
  • run_iter is the specification of the HLS body, which should read the same way, line for line.

The HLS body

One firing of the hls::task is one job, written straight down – the same three steps, and a flag for the fatal error:

static bool halted = false;                               // set by an error, cleared by a reset
#pragma HLS reset variable=halted
if (halted) return;                                       // stopped: read nothing, write nothing

CmdHdr h;
h.read_axi4_stream<W>(s_in);                              // 1. the command
Resp r; r.tx_id = h.tx_id; r.n = h.n;
r.write_axi4_stream<W>(m_out, true);                      //    the results' header

bool early = false;
for (int i = 0; i < h.n; i += PF) {                       // 2. the work, one word per iteration
#pragma HLS PIPELINE II=1
    ... read a word with the lane routines; TLAST before n sets early and breaks ...
    ... compute its lanes, write the result word; TLAST on the last one ...
}

Status s; s.tx_id = h.tx_id; s.status = early ? StatusCode::TLAST_EARLY : StatusCode::OK;
s.write_axi4_stream<W>(m_status, true);                   // 3. the status
if (early) halted = true;

The command, header and status structs come from the schemas; the loop over n is the lane loop, because several elements are packed into each bus word. Both of the loop’s shapes – a word per iteration with the work unrolled, or an element per iteration – and why the body is a straight loop rather than a state machine are in Design patterns for loop optimization.

The host

The host sends commands and collects results and statuses, usually as two processes – a writer and a reader – so it never has to finish one job before starting the next:

  • Keep a bounded number of jobs in flight. The writer sends a command only while fewer than k are outstanding; the reader frees a slot per status. That bounds everything a job leaves behind without the host polling anything.
  • Wait, do not poll. Over a bus the host waits on each queue’s interrupt (Interrupts).
  • Read as the kernel writes: the header, the n results it announces, the status.
  • Check the echo. Every header’s and status’s tx_id (and cfg_id, if there is one) against what was sent.

Next: Simulating in Python – the kernel run beside everything else.