Repository navigation
Conversation
4cc3140 to
05f3ffd
Compare
|
Warning Gemini encountered an error creating the review. You can try again by commenting |
dmiltr3
left a comment
There was a problem hiding this comment.
Feedback / Questions
- Input Types in Test Case: In the added test
testAllCloseNonBroadcastableShapesXla, the tensortis created withdtype=np.complex64. However,logical_xorconverts its inputs to boolean anyway (_logical_binary_opexplicitly casts tonp.bool_). It might be slightly clearer to definetwith a boolean or integer type directly to avoid confusion, though it works fine as is. - Scope of Fix: The fix is added specifically to
allclose. Have you considered whether a similar guard should be added toiscloseas well, or if the current scope is sufficient?allclosecallsisclose, so coveringallclosefixes the most common failure path, but direct calls toisclosewith non-broadcastable shapes might still diverge. - Tolerance in Test: The test uses
atol=-2.92515058314041. Note that for non-inexact types,isclose(and thusallclose) ignoresatolandrtoland just checks equality (return a == b). The early return also skips this computation. Just something to keep in mind, as the negative value/precision has no effect on the test outcome for non-inexact dtypes. - XLA Compiler Dependency: Running this test on CPU might fail with 'Could not find compiler for platform Host' if the XLA CPU JIT compiler is not linked in the test target/environment. Ensure the test environment has the necessary compiler linked or the test is targeted to run on GPU/TPU where applicable.
Overall, the logic is sound.
Proposed Unit Test
Here is the unit test we used to verify the fix:
def testAllCloseNonBroadcastableShapesXla(self):
import tensorflow as tf
import numpy as np
t = tf.convert_to_tensor(np.arange(8, dtype=np.complex64))
def model(x):
a = tf.experimental.numpy.hstack((5, 5, 5, 5))
b = tf.experimental.numpy.logical_xor(x, x)
return tf.experimental.numpy.allclose(a, b, atol=-2.92515058314041)
eager_out = model(t)
xla_out = tf.function(model, jit_compile=True)(t)
self.assertAllEqual(eager_out, xla_out)
self.assertAllEqual(False, xla_out)Verification Results
We verified the fix using the provided test case under XLA compilation (jit_compile=True).
- Eager Output:
False - XLA Output:
False - No XLA compilation errors observed for incompatible static shapes.
|
Thanks for the review. I moved the static broadcastability check from This also covers direct calls to I also simplified the regression test to use direct integer tensors with shapes Testing completed: bazel test --config=linux -k \
//tensorflow/python/ops/numpy_ops:np_math_ops_test \
--test_filter='*CloseNonBroadcastableShapesXla*'
bazel test --config=linux -k \
//tensorflow/python/ops/numpy_ops:np_math_ops_testBoth tests passed locally. |
dmiltr3
left a comment
There was a problem hiding this comment.
We have reviewed your PR internally and verified the changes. Here is our recommendation for improving this fix to ensure consistency and compatibility.
Technical Review
1. Behavior of np.isclose with Non-Broadcastable Shapes
In your fix for np_math_ops.py, you added a check for broadcasting compatibility for integer/boolean dtypes:
try:
array_ops.broadcast_static_shape(a.shape, b.shape)
except ValueError:
return constant_op.constant(False)While standard Python/NumPy equality comparisons (a == b) might return False for non-broadcastable shapes in some contexts, specialized functions like np.isclose are expected to follow strict broadcasting rules.
Consistency Issues:
- Divergence from NumPy: Standard NumPy's
np.iscloseraises aValueErrorwhen operands cannot be broadcast, regardless of whether they are integers or floats. It does not returnFalse. - Internal Inconsistency: In your PR, this
return Falsebehavior is only applied to theelseblock (integers/bools). Theifblock (inexact/floats) still performsa - b. If shapes are non-broadcastable,a - bwill raise a broadcasting error. This makes the function behave inconsistently depending on the dtype.
Best Approach
To maintain compatibility with NumPy and ensure internal consistency, isclose should raise or propagate a ValueError (or TF equivalent) when operand shapes are non-broadcastable, for all dtypes.
We recommend:
- Removing the
try-exceptblock that returnsFalse. - Allowing the broadcasting exception to propagate naturally (or raising a graceful Python
ValueErrorafter static shape validation if necessary to prevent raw compiler crashes). - Updating the corresponding test case
testCloseNonBroadcastableShapesXlato expect this error/exception rather than assertingFalse.
Please let us know if you have any questions about this approach. We aim to keep tensorflow.experimental.numpy as faithful to standard NumPy semantics as possible.
|
Thanks for the clarification. I updated the implementation to follow NumPy’s strict broadcasting behavior. The non-inexact branch of Because I also updated the regression test to cover:
Testing completed successfully: bazel test --config=linux -k \
//tensorflow/python/ops/numpy_ops:np_math_ops_test \
--test_filter='*CloseNonBroadcastableShapesXla*'
bazel test --config=linux -k \
//tensorflow/python/ops/numpy_ops:np_math_ops_test |
dmiltr3
left a comment
There was a problem hiding this comment.
Here is a summary of the progress and any issues we found:
-
Status: Internal tests have completed successfully with all 2514 targets passing. Open-source validation has also succeeded.
-
Action Required: You added a new import
from tensorflow.python.eager import def_functioninnp_math_ops_test.py. Please update the dependencies for the test target in your build file (e.g.,BUILDorBUILD.bazel) to explicitly include this dependency.
28d4ca1 to
6e13bdb
Compare
|
Thanks. I added I also fixed the PyLint indentation issue and rebased the branch onto the current |
dmiltr3
left a comment
There was a problem hiding this comment.
Upon review, we noticed that the implementation in np_math_ops.py (specifically in isclose for integer types) used a == b. This can cause inconsistent behavior between eager execution and XLA compilation when dealing with non-broadcastable static shapes.
In TensorFlow, using == on Tensors with non-broadcastable shapes might fallback to scalar False or behave differently than expected NumPy semantics, which require raising a ValueError (Incompatible shapes). This discrepancy leads to errors during XLA compilation.
Suggested Fix
Please replace a == b with math_ops.equal(a, b) in the else branch of isclose function in third_party/tensorflow/python/ops/numpy_ops/np_math_ops.py.
else:
return math_ops.equal(a, b)This ensures strict broadcasting validation matching NumPy semantics and prevents XLA compilation failures.
Tests
We also suggest adding a regression test to verify this behavior, ensuring that isclose and allclose raise appropriate errors for non-broadcastable shapes, both with and without jit_compile=True.
|
Thanks for the review. This change is already implemented in the latest commit ( The non-inexact branch of return math_ops.equal(a, b)The regression test also verifies that The required Could you please take another look at the latest revision? |
|
Hi @furk4neg3, Closing this PR as the proposed changes have already been merged into the codebase. Thank you for your contribution! |
Fixes #122052.
This change makes the non-inexact path of
tf.experimental.numpy.iscloseuse strict TensorFlow equality for non-broadcastable inputs.Tensor equality normally returns scalar
Falsefor incompatible shapes through the incompatible-shape fallback. Underjit_compile=True, the graph could still contain anEqualoperation over incompatible shapes, causing XLA compilation to fail.The non-inexact path now uses
math_ops.equal, which applies strict broadcasting rules. As a result, non broadcastable inputs raise aValueErroror TensorFlow equivalent instead of returningFalsein eager execution and failing later during XLA compilation. This also keeps the behavior consistent with NumPy and with the existing inexact-type path.The change is placed in
iscloseso that both directisclosecalls andallclose, which reduces the result ofisclose, receive the fix.The regression test covers both
iscloseandallclosewith integer and floating-point inputs in eager and XLA-compiled execution.Testing