Skip to content

XLS: support all hls4ml rounding modes. - #1532

Open
vasdommes wants to merge 2 commits into
fastmachinelearning:mainfrom
vasdommes:xls_rounding_modes
Open

XLS: support all hls4ml rounding modes.#1532
vasdommes wants to merge 2 commits into
fastmachinelearning:mainfrom
vasdommes:xls_rounding_modes

Conversation

@vasdommes

Copy link
Copy Markdown
Contributor

Description

Currently, XLS backend supports only rounding modes that exist (under different names) in DSLX stdlib: TRN, TRN_ZERO, RND_INF, RND_CONV.

fn convert_rounding_mode<rm: RoundingMode>() -> round::RoundingMode {
match rm {
RoundingMode::TRN => round::RoundingMode::RTN,
RoundingMode::TRN_ZERO => round::RoundingMode::RTZ,
// RoundingMode::RND => TODO,
// RoundingMode::RND_ZERO => TODO,
RoundingMode::RND_INF => round::RoundingMode::RNA,
// RoundingMode::RND_MIN_INF => TODO,
RoundingMode::RND_CONV => round::RoundingMode::RNE,
_ => {
assert_fmt!(false, "unsupported_RoundingMode_{}", (rm as RoundingModeIntegerType));
round::RoundingMode::RTN
}
}
}

This PR adds missing rounding modes.

Implementation:

  • Copied round.x from stdlib, changed enum RoundingMode to the one from hls4ml
  • Added missing cases to the function round().
  • Added DSLX tests

Type of change

  • New feature (non-breaking change which adds functionality)

Tests

Unit tests in DSLX code can be run e.g. as (for xls build from sources in$HOME/xls):

$HOME/xls/bazel-bin/xls/dslx/interpreter_main --dslx_stdlib_path=$HOME/xls/xls/dslx/stdlib ap_round.x

All existing hls4ml tests pass.

I also ran softmax tests based on the commit e5bf4af0 from PR #1476. These tests originally failed with

[Assert] Assertion failure via assert_fmt! @ /builds/fastmachinelearning/hls4ml/test/pytest/test_softmax_test_softmax_16,6-input_shape0-auto-io_parallel-False-stable-XLS-activation/firmware/ap_types/fixed_point_util.x:592:24-592:95: unsupported_RoundingMode_3

(which means RoundingMode::RND)

Checklist

  • I have read the guidelines for contributing.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have made corresponding changes to the documentation.
  • My changes generate no new warnings.
  • I have installed and run pre-commit on the files I edited or added.
  • I have added tests that prove my fix is effective or that my feature works.

@jmitrevs jmitrevs added the please test Trigger testing by creating local PR branch label Aug 25, 2026
@jmitrevs
jmitrevs requested a lite review from Copilot August 25, 2026 20:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends the XLS backend’s fixed-point rounding support to cover all hls4ml rounding modes by introducing an hls4ml-compatible DSLX rounding module and wiring it into existing fixed-point utilities.

Changes:

  • Added a new DSLX rounding implementation (ap_round.x) adapted from XLS stdlib to support the full hls4ml RoundingMode set.
  • Updated fixed_point_util.x to use the new ap_round module directly (removing the previous partial adapter/conversion logic).
  • Added DSLX unit tests to validate all rounding modes and verify equivalence for modes that map to XLS stdlib rounding.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
hls4ml/templates/xls/firmware/ap_types/fixed_point_util.x Switches rounding to use ap_types.ap_round and removes the old partial rounding-mode conversion layer.
hls4ml/templates/xls/firmware/ap_types/ap_round.x New rounding implementation and tests supporting all hls4ml rounding modes (with conversions for stdlib-equivalent modes).

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread hls4ml/templates/xls/firmware/ap_types/fixed_point_util.x Outdated
Comment thread hls4ml/templates/xls/firmware/ap_types/ap_round.x Outdated
Comment thread hls4ml/templates/xls/firmware/ap_types/ap_round.x Outdated
@jmitrevs jmitrevs added please test Trigger testing by creating local PR branch and removed please test Trigger testing by creating local PR branch labels Aug 28, 2026
@jmitrevs

Copy link
Copy Markdown
Contributor

Is it worth adding explicit pytests for the rounding modes?

@JanFSchulte

Copy link
Copy Markdown
Contributor

Probably a good idea to add those.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

please test Trigger testing by creating local PR branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants