-
Notifications
You must be signed in to change notification settings - Fork 513
Fix Keras v3 model conversion in numerical profiling #1421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Fix Keras v3 model conversion in numerical profiling #1421
Conversation
…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
|
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 @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? |
|
@JanFSchulte, thank you for pointing this out. I am looking into it now. |
|
Yes, this is related to the new Keras 3 environment. It turns out that import qkerasdoes not fail. Because of that, this check in hls4ml/hls4ml/model/profiling.py Lines 29 to 34 in 46bb486
enables QKeras profiling, and the code then fails when it tries to access: qkeras.QActivationSince However, I also ran the new test_keras_v3_profiling tests without |
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 becauseconvert_from_config()inhls4ml/converters/__init__.pywas 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 thenumerical()profiling function internally callsget_unoptimized_hlsmodel(), which usesconvert_from_config()to recreate the model.Solution
Added Keras version detection in
convert_from_config()to properly route:keras_v3_to_hls()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
elif 'KerasModel' in yamlConfig:block with version checkType of change
Tests
Test File:
test/pytest/test_keras_v3_profiling.pyCreated 5 unit tests that verify numerical profiling works correctly with Keras v3 models:
Test Behavior
How to Reproduce/Run Tests