Skip to content

Fix JAX convert_to_tensor dropping input dtype when floatx is bfloat16. - #23683

Open
maitry63 wants to merge 3 commits into
keras-team:masterfrom
maitry63:fix_convert-to-tensor_bfloat16_jax
Open

maitry63 wants to merge 3 commits into
keras-team:masterfrom
maitry63:fix_convert-to-tensor_bfloat16_jax

Conversation

@maitry63

Copy link
Copy Markdown
Collaborator

This PR fixes bfloat16 fast path in the JAX backend was guarded by standardize_dtype(dtype) == "bfloat16". Since standardize_dtype(None) returns floatx(), that branch fired for every call that omitted dtype
once floatx was "bfloat16", and the following astype(None) resolved to float32.

This change ensures the branch only runs on an explicit bfloat16 request by checking dtype is not None.

Fixes: #23679

Contributor Agreement

Please review our PR Contribution Policy and AI-Assisted Contribution Policy and check all boxes below before submitting your PR for review:

  • This PR is linked to an issue that has been assigned to me (see Fixes #xxx above).
  • I am a human, and not a bot.
  • I will be responsible for responding to review comments in a timely manner.
  • I will work with the maintainers to push this PR forward until submission.

Note: Failing to adhere to this agreement may result in your future PRs no longer being reviewed. PRs without a linked, assigned issue will be converted to draft.

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request fixes a bug in the JAX backend's convert_to_tensor where floatx would override an input's dtype when floatx is set to "bfloat16". It ensures that the bfloat16 fast path is only triggered when dtype is explicitly requested, and adds comprehensive regression tests to verify this behavior. The review feedback suggests simplifying the condition in convert_to_tensor by removing the redundant standardize_dtype call, as dtype is already standardized earlier in the function.

Comment thread keras/src/backend/jax/core.py
@maitry63
maitry63 force-pushed the fix_convert-to-tensor_bfloat16_jax branch from 41026a5 to b1cf8ac Compare September 18, 2026 11:59
@codecov-commenter

codecov-commenter commented Sep 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.40%. Comparing base (8ddf4b0) to head (5902a25).

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #23683      +/-   ##
==========================================
- Coverage   86.96%   84.40%   -2.57%     
==========================================
  Files         484      484              
  Lines       71754    71754              
  Branches    11840    11840              
==========================================
- Hits        62400    60562    -1838     
- Misses       6224     8185    +1961     
+ Partials     3130     3007     -123     
Flag Coverage Δ
keras 84.23% <100.00%> (-2.55%) ⬇️
keras-cpu 84.23% <100.00%> (-1.91%) ⬇️
keras-gpu ?
keras-jax 58.28% <100.00%> (-2.16%) ⬇️
keras-numpy 54.06% <0.00%> (-0.15%) ⬇️
keras-openvino 59.80% <0.00%> (-0.15%) ⬇️
keras-tensorflow 59.95% <0.00%> (-2.09%) ⬇️
keras-torch 59.52% <0.00%> (-2.20%) ⬇️
keras-tpu ?
keras.applications ?
keras.applications-jax ?
keras.applications-numpy ?
keras.applications-openvino ?
keras.applications-tensorflow ?
keras.applications-torch ?
keras.wrappers ?
keras.wrappers-jax ?
keras.wrappers-numpy ?
keras.wrappers-openvino ?
keras.wrappers-tensorflow ?
keras.wrappers-torch ?

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

☔ View full report in Codecov by Harness.
📢 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.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

keras.ops.convert_to_tensor does not keep the input dtype on the jax backend when floatx is bfloat16

2 participants