Skip to content

Implement nanquantile function in keras.ops#22405

Open
shashaka wants to merge 12 commits intokeras-team:masterfrom
shashaka:nanquantile
Open

Implement nanquantile function in keras.ops#22405
shashaka wants to merge 12 commits intokeras-team:masterfrom
shashaka:nanquantile

Conversation

@shashaka
Copy link
Collaborator

Adds keras.ops.nanquantile, which computes the q-th quantile of the input tensor along a specified axis while ignoring NaN values.
Supported across NumPy, TensorFlow, PyTorch, and JAX backends. Not supported on OpenVINO.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces the nanquantile operation to keras.ops, enabling users to calculate quantiles of tensors while automatically disregarding any NaN values present. This enhancement provides a robust statistical utility that is crucial for data analysis and preprocessing in machine learning workflows, ensuring compatibility and consistent behavior across major backend frameworks like TensorFlow, PyTorch, JAX, and NumPy.

Highlights

  • New Functionality: Implemented keras.ops.nanquantile, a new function to compute quantiles while ignoring NaN values, aligning with NumPy's nanquantile behavior.
  • Multi-Backend Support: The nanquantile function is supported across NumPy, TensorFlow, PyTorch, and JAX backends, ensuring consistent behavior across different deep learning frameworks.
  • OpenVINO Exclusion: Explicitly marked nanquantile as not supported for the OpenVINO backend, with corresponding test exclusions.
  • Comprehensive Testing: Added extensive symbolic and concrete tests for nanquantile to verify its correctness across various inputs, axes, and quantile methods.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • keras/api/_tf_keras/keras/ops/init.py
    • Imported nanquantile from keras.src.ops.numpy to expose it in the Keras API.
  • keras/api/_tf_keras/keras/ops/numpy/init.py
    • Imported nanquantile from keras.src.ops.numpy to expose it under the keras.ops.numpy API.
  • keras/api/ops/init.py
    • Imported nanquantile from keras.src.ops.numpy to expose it in the Keras API.
  • keras/api/ops/numpy/init.py
    • Imported nanquantile from keras.src.ops.numpy to expose it under the keras.ops.numpy API.
  • keras/src/backend/jax/numpy.py
    • Implemented nanquantile by wrapping JAX's jnp.nanquantile function, including tensor conversion.
  • keras/src/backend/numpy/numpy.py
    • Implemented nanquantile by wrapping NumPy's np.nanquantile function, with added type handling for boolean inputs.
  • keras/src/backend/openvino/excluded_concrete_tests.txt
    • Added NumpyDtypeTest::test_nanquantile and NumpyOneInputOpsCorrectnessTest::test_nanquantile to the list of excluded tests for OpenVINO.
  • keras/src/backend/openvino/numpy.py
    • Added a placeholder nanquantile function that raises a NotImplementedError for the OpenVINO backend.
  • keras/src/backend/tensorflow/numpy.py
    • Implemented nanquantile with custom logic for TensorFlow, handling NaN values, axis reduction, and keepdims using tf.boolean_mask and tf.map_fn.
  • keras/src/backend/torch/numpy.py
    • Implemented nanquantile with custom logic for PyTorch, handling NaN values, axis reduction, keepdims, and dtype conversion using torch.nanquantile.
  • keras/src/ops/numpy.py
    • Defined the Nanquantile operation class and the nanquantile functional API, including comprehensive docstrings, argument descriptions, and examples.
    • Added logic for symbolic tensor handling and output shape computation for Nanquantile.
  • keras/src/ops/numpy_test.py
    • Added new test cases for nanquantile to verify its symbolic shape inference.
    • Added extensive concrete test cases for nanquantile to ensure numerical correctness across various inputs, quantile probabilities, axes, and interpolation methods, including scenarios with all NaNs or mixed valid/NaN values.
    • Added dtype-specific tests for nanquantile to confirm correct output data types.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

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 introduces the keras.ops.nanquantile function, providing implementations for NumPy, JAX, TensorFlow, and PyTorch backends. The implementations for NumPy and JAX are straightforward wrappers, while the TensorFlow and PyTorch versions contain custom logic. The associated tests are comprehensive and cover various use cases and backends.

I've identified a critical issue in the TensorFlow backend implementation that could lead to incorrect results when using integer dtypes. Additionally, there's a minor code duplication in the PyTorch backend implementation. My review includes suggestions to address these points.

@codecov-commenter
Copy link

codecov-commenter commented Mar 12, 2026

Codecov Report

❌ Patch coverage is 92.38095% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.04%. Comparing base (80bad7a) to head (afb036f).

Files with missing lines Patch % Lines
keras/src/ops/numpy.py 86.36% 2 Missing and 1 partial ⚠️
keras/src/backend/torch/numpy.py 92.85% 1 Missing and 1 partial ⚠️
keras/api/_tf_keras/keras/ops/__init__.py 0.00% 1 Missing ⚠️
keras/api/_tf_keras/keras/ops/numpy/__init__.py 0.00% 1 Missing ⚠️
keras/src/backend/tensorflow/numpy.py 97.43% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #22405      +/-   ##
==========================================
+ Coverage   83.03%   83.04%   +0.01%     
==========================================
  Files         596      596              
  Lines       66684    66789     +105     
  Branches    10382    10400      +18     
==========================================
+ Hits        55371    55468      +97     
- Misses       8676     8681       +5     
- Partials     2637     2640       +3     
Flag Coverage Δ
keras 82.87% <92.38%> (+0.01%) ⬆️
keras-jax 60.50% <26.66%> (-0.06%) ⬇️
keras-numpy 54.75% <29.52%> (-0.04%) ⬇️
keras-openvino 49.87% <20.95%> (-0.05%) ⬇️
keras-tensorflow 61.79% <59.04%> (-0.01%) ⬇️
keras-torch 60.60% <47.61%> (-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.

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.

3 participants