[Numpy] Fix some TF test failures introduced by NumPy 2.0 update. #73730
+125
−72
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
[Numpy] Fix some TF test failures introduced by NumPy 2.0 update.
This change fixes some common test level errors.
Error type: Out-of-bound python value to numpy data type conversion
Solutions:
- If the test is not specifically designed to evaluate overflow
behavior, modify the values or data types used to ensure they fall
within the range for the target NumPy data type.
- Or exclude or modify values that are incompatible with certain data
types.
- To retain the previous behavior where out-of-bounds conversions
resulted in overflow, use np.array(value).astype(dtype) instead of
np.array(value, dtype=dtype).
Error type: Deprecated NumPy namespaces/APIs
Solution: Replace deprecated code with the recommended alternatives from
the NumPy documentation. If the suggested replacement isn't available
in NumPy 1.x, implement version-specific logic or macros to ensure
compatibility across different NumPy versions.
Error type: NumPy 2.0 has updated requirements for the array protocol. np.array(obj, copy=False) is no longer supported.
Solution: Follow the NumPy 2.0 migration guide to address the error: https://numpy.org/devdocs/numpy_2_0_migration_guide.html#adapting-to-changes-in-the-copy-keyword.
Error type: NumPy scalar representations now include a np.type prefix (e.g., np.int64(1) instead of just 1).
Solutions:
- Convert the NumPy scalar back to a Python value before printing or
comparing it. Use methods like .item() or .tolist().
- Or if it is an AssertionError, update the expected error messages to
include the np.type prefix for NumPy values.
An additional error: The maximum number of dimensions (and arguments) was increased to 64 https://numpy.org/doc/stable/numpy_2_0_migration_guide.html#increased-maximum-number-of-dimensions. Update the maximum value in the tensor test TFETensorTest.testNumpyTooManyDimensions.