Basic Vectorization — one MAC, bit-exact

basic_vec is the front-door for vectorization: the smallest demonstration that a Python-vectorized golden model and a vectorized Vitis kernel produce the same bits. It is a data/schema example — how to represent and compute on data, before any module-to-module interface is introduced.

The whole example is one elementwise multiply-accumulate, computed over arrays (no per-element Python loop) for each of the three numeric kinds, with the result asserted equal to Vitis C-sim bit-for-bit:

y = a * b + c
kind Python Vitis kernel bit-exact because
integer growth-aware operators (Int17 result) ap_int<17> y = a*b + c; integer arithmetic is exact; the operators track the growth
float numpy float32 passthrough a*b + c, built -ffp-contract=off same two roundings (no fused FMA)
fixed quantize(a*b + c, Q) ap_fixed<8,4> y = a*b + c; full precision, then quantize-on-assign

This is the teaching counterpart to the rigorous all-modes/all-widths sweep in examples/schemas/fixedpoint — the two share the same conformance machinery (BuildDag + run_dag_cli + gen→csim→compare-bits). The concepts (operators, the two paths, growth rules) live in the vectorization guide; this walkthrough shows them end-to-end on one example.

Learning Objectives

The first example, so everything here is new. In the order the pages teach it:

  1. Declare numbers as typed, vectorized DataSchema arrays (NumPy-backed, no per-element Python loop) in the three numeric kinds: integer, float and fixed point.
  2. Compute on them with the type-preserving operators (a*b + c), and state the result type the growth rules give.
  3. Write the matching minimal Vitis kernel for each kind, and name what makes each one bit-exact: integer growth, float built with -ffp-contract=off (no fused multiply-add), fixed-point quantize-on-assign.
  4. Run a gen → csim → compare-bits conformance DAG (BuildDag + run_dag_cli) and assert the Vitis output equals the Python output bit for bit.

The walkthrough

  1. The Python model — the vectorized golden: declare the arrays, apply a*b + c, derive the result type, emit the golden bits.
  2. The Vitis equivalent — the hand-written C++ kernels that mirror the op.
  3. Confirming the match — the build DAG, the Vitis C-sim, the bit comparison.

File map

In examples/basic_vec/:

  • basic_vec_build.py — the three MAC cases + the gen→csim→compare conformance DAG.
  • kernels.py — the three minimal, hand-written Vitis kernels (int / float / fixed).
  • run.tcl — the Vitis C-sim driver (-ffp-contract=off for the float kernel).

Running it

python examples/basic_vec/basic_vec_build.py --through gen   # kernels + vectors + golden (no Vitis)
python examples/basic_vec/basic_vec_build.py --through run   # the bit-exact csim conformance (Vitis)

The run stage asserts, per kind, that the Vitis output bits equal the Python operator bits exactly; any mismatch stops the build.


Table of contents

  • The Python model - The golden model: one vectorized multiply-accumulate per numeric kind, a*b + c over DataArrays with no per-element Python loop. The same expression in integer, float and fixed point, with the operators deriving the result width — 8-bit times 8-bit gives 16, plus a carry bit for the add.
  • The Vitis equivalent - The C++ side of the comparison, deliberately hand-written rather than generated: three minimal templates that reconstruct each operand bit-for-bit, compute the same a*b + c in typed C++, and emit the stored bits. Keeping codegen out of it keeps the focus on the bit-exactness; the generated flow is shown in the polynomial example.
  • Confirming the match - Comparing the Python golden against the Vitis kernels bit for bit, through a small build DAG — the same gen, csim and compare machinery the fuller examples/schemas/fixedpoint conformance sweep uses. gen writes the kernels, the input vectors and the expected bits; run invokes Vitis csim and compares.