Skip to content

Export random submodule under public keras.ops API - #23580

Closed
somuai wants to merge 2 commits into
keras-team:masterfrom
somuai:fix-ops-random-public-export
Closed

somuai wants to merge 2 commits into
keras-team:masterfrom
somuai:fix-ops-random-public-export

Conversation

@somuai

@somuai somuai commented Sep 7, 2026

Copy link
Copy Markdown

Description

Fixes #23575

In Keras 3.14.x and 3.15.x, keras.ops.random submodule was not exported in the public API because random operations in keras/src/random/ were only decorated with @keras_export("keras.random.<op>"). While keras/src/ops/__init__.py imported from keras.src.backend import random, code generation in api_gen.py relies on namex traversing decorated symbols, causing keras.ops.random to be omitted from keras/api/ops. This also blocked dependent packages such as keras-hub (e.g. Swin Transformer DropPath layer calling keras.ops.random.uniform).

Changes

  1. Updated @keras_export decorators in keras/src/random/random.py and keras/src/random/seed_generator.py to dual-export under both keras.random.<symbol> and keras.ops.random.<symbol> (covering normal, categorical, uniform, randint, truncated_normal, dropout, shuffle, gamma, binomial, beta, and SeedGenerator).
  2. Added keras/api/ops/random and keras/api/_tf_keras/keras/ops/random generated exports, and exposed from keras.ops import random as random in keras/api/ops/__init__.py.
  3. Excluded src/backend/numpy from api_gen.py search directories alongside other backend folders (jax, tensorflow, torch, openvino).
  4. Added defensive ImportError guard in keras/src/tree/torchtree_impl.py when PyTorch is not present.
  5. Added unit test test_ops_random_export in keras/src/ops/ops_test.py to assert export and symbol equality between keras.ops.random and keras.random.

Contributor Agreement

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

  • 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.

Dual-export all random operations and SeedGenerator under both
keras.random and keras.ops.random so that ops.random.* symbols
can be accessed through public API entry points.

Signed-off-by: Soumyajit Ghosh <jobsoumyajit6124@gmail.com>

@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 exposes the random operations and SeedGenerator under the keras.ops.random namespace, updates the API generation script, and adds corresponding unit tests. It also adds an ImportError guard for PyTorch's _pytree in torchtree_impl.py. The feedback notes that the PyTorch import guard is incomplete because calling register_tree_node_class when PyTorch is missing will still result in an AttributeError, and suggests returning the class unchanged if torch_tree is None.

Comment on lines +3 to +6
try:
from torch.utils import _pytree as torch_tree
except ImportError:
torch_tree = None

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.

medium

While adding the ImportError guard for torch_tree prevents immediate import-time failures when PyTorch is not installed, any subsequent calls to functions in this module (such as register_tree_node_class, which is typically used as a class decorator at import time) will raise an AttributeError when they attempt to access attributes on torch_tree.

To make this defensive guard fully robust, please ensure that register_tree_node_class and any other functions in this file gracefully handle the case where torch_tree is None (e.g., by returning the class unchanged or acting as a no-op).

For example:

def register_tree_node_class(cls):
    if torch_tree is None:
        return cls
    # ... existing registration logic ...

@codecov-commenter

codecov-commenter commented Sep 7, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 76.47059% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.27%. Comparing base (0e3f1a5) to head (9d80046).

Files with missing lines Patch % Lines
keras/src/tree/torchtree_impl.py 33.33% 3 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #23580      +/-   ##
==========================================
- Coverage   85.03%   84.27%   -0.77%     
==========================================
  Files         468      468              
  Lines       71296    71301       +5     
  Branches    11833    11834       +1     
==========================================
- Hits        60629    60090     -539     
- Misses       7644     8194     +550     
+ Partials     3023     3017       -6     
Flag Coverage Δ
keras 84.09% <76.47%> (-0.74%) ⬇️
keras-cpu 84.09% <76.47%> (-0.01%) ⬇️
keras-gpu ?
keras-jax 58.31% <64.70%> (-0.34%) ⬇️
keras-numpy 54.00% <64.70%> (-0.01%) ⬇️
keras-openvino 59.65% <64.70%> (-0.01%) ⬇️
keras-tensorflow 59.96% <64.70%> (-0.29%) ⬇️
keras-torch 59.44% <76.47%> (-0.41%) ⬇️
keras-tpu ?

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.

…e_node_class

Signed-off-by: Soumyajit Ghosh <jobsoumyajit6124@gmail.com>
@somuai

somuai commented Sep 7, 2026

Copy link
Copy Markdown
Author

Thank you for the review. Addressed in commit 9d80046: updated register_tree_node_class in keras/src/tree/torchtree_impl.py to immediately return cls unchanged if torch_tree is None.

@hertschuh

Copy link
Copy Markdown
Collaborator

Closing as the bug was closed as "won't fix".

The current API is intentional.

@hertschuh hertschuh closed this Sep 8, 2026
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.

[Bug] keras.ops.random submodule not exported in public API in Keras 3.14.x and 3.15.x

4 participants