Skip to content

Conversation

@Abubakar-rashid
Copy link

Description

Summary

Fixes numerical profiling failure when using Keras v3 models with hls4ml.model.profiling.numerical().

Problem

When calling numerical(model=kerasmodel, hls_model=hlsmodel, X=X_test) with a Keras v3 model, the function fails because convert_from_config() in hls4ml/converters/__init__.py was always using the Keras v2 converter (keras_v2_to_hls()), even for Keras v3 models.

The bug occurs at line 125 where the code routes to keras_v2_to_hls() for all Keras models without checking the version. This causes incompatibility issues when the numerical() profiling function internally calls get_unoptimized_hlsmodel(), which uses convert_from_config() to recreate the model.

Solution

Added Keras version detection in convert_from_config() to properly route:

  • Keras v3 models → keras_v3_to_hls()
  • Keras v2 models → keras_v2_to_hls()

This mirrors the same logic already present in convert_from_keras_model() (lines 231-237), ensuring consistency across the codebase.

Changes Made

  • hls4ml/converters/init.py: Added elif 'KerasModel' in yamlConfig: block with version check
  • test/pytest/test_keras_v3_profiling.py: New comprehensive test file with 5 unit tests

Type of change

  • Bug fix (non-breaking change that fixes an issue)
  • Documentation update
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • A new research paper code implementation
  • Other (Specify)

Tests

Test File: test/pytest/test_keras_v3_profiling.py

Created 5 unit tests that verify numerical profiling works correctly with Keras v3 models:

  1. test_keras_v3_numerical_profiling_simple_model - Tests basic profiling with Dense layers
  2. test_keras_v3_numerical_profiling_with_activations - Tests profiling with test data (X parameter)
  3. test_keras_v3_numerical_profiling_conv_model - Tests profiling with Conv2D layers
  4. test_keras_v3_numerical_profiling_with_hls_model - Tests integration with HLS models
  5. test_keras_v3_numerical_profiling_batch_norm - Tests with BatchNormalization layers

Test Behavior

  • Master branch: Tests FAIL (demonstrates the bug exists)
  • This branch: Tests PASS (demonstrates the fix works)

How to Reproduce/Run Tests

# Tests will be skipped if Keras < 3.0 is installed
pytest test/pytest/test_keras_v3_profiling.py -v

# To run in an environment with Keras 3.0+:
pip install keras>=3.0
pytest test/pytest/test_keras_v3_profiling.py -v

@jmitrevs jmitrevs added the please test Trigger testing by creating local PR branch label Jan 21, 2026
…to LONGLIST

- Removed else clause in convert_from_config() as it's no longer needed
- Added test_keras_v3_profiling to LONGLIST in generate_ci_yaml.py for proper CI environment
Break KERAS3_LIST into multiple lines to comply with max line length of 125 characters
@JanFSchulte JanFSchulte added please test Trigger testing by creating local PR branch and removed please test Trigger testing by creating local PR branch labels Jan 22, 2026
@JanFSchulte
Copy link
Contributor

The newly added test fails, but I think that is unrelated to what is done in the test itself, but an issue with the new keras3 environment. It seems to have QKeras installed, even though that doesn't work with keras3. So this check here https://github.com/fastmachinelearning/hls4ml/blob/main/hls4ml/model/profiling.py#L29-L34 enables QKeras for the profiling but then this line results in an error https://github.com/fastmachinelearning/hls4ml/blob/main/hls4ml/model/profiling.py#L40 where it claims that qkeras doesn't have QActivation as an attribute.

@marco66colombo Looking at this https://github.com/fastmachinelearning/hls4ml/blob/main/pyproject.toml#L56-L76 I don't understand why a qkeras import would work in the keras3 environment. Could you have a look at that?

@marco66colombo
Copy link
Contributor

@JanFSchulte, thank you for pointing this out. I am looking into it now.

@marco66colombo
Copy link
Contributor

Yes, this is related to the new Keras 3 environment.

It turns out that hgq2 has its own qkeras module (it installs a top-level qkeras/ package as part of the hgq2 distribution). This does not break the environment by itself, but it means that:

import qkeras

does not fail.

Because of that, this check in hls4ml.model.profiling:

try:
import qkeras
__qkeras_profiling_enabled__ = True
except ImportError:
__qkeras_profiling_enabled__ = False

enables QKeras profiling, and the code then fails when it tries to access:

qkeras.QActivation

Since hgq2 tests are currently skipped anyway, we can remove it from the pyproject.toml here

However, I also ran the new test_keras_v3_profiling tests without hgq2 installed, and they still fail, not due to qkeras, but due to other Keras3 specific issues. I did not investigate these in much detail yet.

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