Skip to content

Update saving_lib.py to properly convert from h5py to numpy array. #21371

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

Closed

Conversation

pctablet505
Copy link
Collaborator

@pctablet505 pctablet505 commented Jun 10, 2025

What does this PR do?

Fixes an incompatibility when loading model weights in environments where TensorFlow's NumPy behavior is enabled (tf.experimental.numpy.experimental_enable_numpy_behavior). Previously, some variables loaded as h5py datasets were not converted to numpy arrays, causing downstream errors during model construction and weight assignment, especially for layers like ReversibleEmbedding and EinsumDense.

This update ensures that all h5py datasets are converted to numpy arrays before further processing in saving_lib.py.

Why is this needed?

When using TensorFlow's NumPy behavior, type promotion and array handling can differ, leading to incompatibilities if weights remain as h5py objects. This change guarantees consistent array types regardless of backend or NumPy mode.

Related Issues/PRs

@codecov-commenter
Copy link

codecov-commenter commented Jun 10, 2025

Codecov Report

Attention: Patch coverage is 50.00000% with 1 line in your changes missing coverage. Please review.

Project coverage is 82.72%. Comparing base (1d0358f) to head (1b0395b).
Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
keras/src/saving/saving_lib.py 50.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #21371      +/-   ##
==========================================
+ Coverage   82.70%   82.72%   +0.02%     
==========================================
  Files         565      565              
  Lines       54894    54906      +12     
  Branches     8517     8521       +4     
==========================================
+ Hits        45398    45419      +21     
+ Misses       7415     7399      -16     
- Partials     2081     2088       +7     
Flag Coverage Δ
keras 82.53% <50.00%> (+0.02%) ⬆️
keras-jax 63.55% <50.00%> (-0.03%) ⬇️
keras-numpy 58.70% <50.00%> (-0.03%) ⬇️
keras-openvino 33.57% <0.00%> (+0.03%) ⬆️
keras-tensorflow 63.93% <50.00%> (-0.03%) ⬇️
keras-torch 63.58% <50.00%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@james77777778
Copy link
Contributor

Hey @pctablet505
I just encountered a similar issue and fixed it in this PR:
#21365

Previously, I simply added an else block for this but I have updated it to your solution.

@pctablet505
Copy link
Collaborator Author

Hey @pctablet505 I just encountered a similar issue and fixed it in this PR: #21365

Previously, I simply added an else block for this but I have updated it to your solution.

Thanks for including it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants