Conversation
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>
There was a problem hiding this comment.
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.
| try: | ||
| from torch.utils import _pytree as torch_tree | ||
| except ImportError: | ||
| torch_tree = None |
There was a problem hiding this comment.
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 Report❌ Patch coverage is
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…e_node_class Signed-off-by: Soumyajit Ghosh <jobsoumyajit6124@gmail.com>
|
Thank you for the review. Addressed in commit 9d80046: updated |
|
Closing as the bug was closed as "won't fix". The current API is intentional. |
Description
Fixes #23575
In Keras 3.14.x and 3.15.x,
keras.ops.randomsubmodule was not exported in the public API because random operations inkeras/src/random/were only decorated with@keras_export("keras.random.<op>"). Whilekeras/src/ops/__init__.pyimportedfrom keras.src.backend import random, code generation inapi_gen.pyrelies onnamextraversing decorated symbols, causingkeras.ops.randomto be omitted fromkeras/api/ops. This also blocked dependent packages such askeras-hub(e.g. Swin Transformer DropPath layer callingkeras.ops.random.uniform).Changes
@keras_exportdecorators inkeras/src/random/random.pyandkeras/src/random/seed_generator.pyto dual-export under bothkeras.random.<symbol>andkeras.ops.random.<symbol>(coveringnormal,categorical,uniform,randint,truncated_normal,dropout,shuffle,gamma,binomial,beta, andSeedGenerator).keras/api/ops/randomandkeras/api/_tf_keras/keras/ops/randomgenerated exports, and exposedfrom keras.ops import random as randominkeras/api/ops/__init__.py.src/backend/numpyfromapi_gen.pysearch directories alongside other backend folders (jax,tensorflow,torch,openvino).ImportErrorguard inkeras/src/tree/torchtree_impl.pywhen PyTorch is not present.test_ops_random_exportinkeras/src/ops/ops_test.pyto assert export and symbol equality betweenkeras.ops.randomandkeras.random.Contributor Agreement
Please review our AI-Assisted Contribution Policy and check all boxes below before submitting your PR for review:
Note: Failing to adhere to this agreement may result in your future PRs no longer being reviewed.