Repository navigation
Fix assert_close to support comparisons involving Python and NumPy scalars - #188945
Partha-Shankar wants to merge 3 commits into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/188945
Note: Links to docs will display an error until the docs builds have been completed. ❌ 2 New Failures, 45 Unclassified FailuresAs of commit f57775f with merge base 447c525 ( NEW FAILURES - The following jobs have failed:
UNCLASSIFIED FAILURES - DrCI could not classify the following jobs because the workflow did not run on the merge base. The failures may be pre-existing on trunk or introduced by this PR:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
|
|
@pytorchbot label "topic: not user facing" |
…ed test failure) torch.compile/Dynamo traces NumPy scalars as 0-dimensional ndarrays rather than np.generic instances, so the scalar-detection check needed to account for that.
|
Hi @mruberry @rgommers — following up with a fix for the Root CauseUnder Fix Applied
Pushed as commit f57775f. Happy to address any review feedback once CI reruns on this commit. |
|
Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as |
Description
This PR resolves issue #161863 where
torch.testing.assert_closewould raise aTypeError(no comparison pair was able to handle inputs) when comparing atorch.Tensorto a Python scalar (int,float,complex,bool) or a NumPy scalar (np.generic).Implementation Details
TensorLikePair._process_inputsto identify if either input is a Python or NumPy scalar. If so, we bypass the strictdirectly_relatedsubclass and type matching checks.torch.Tensoroperand and initializing the scalar directly on that device viatorch.as_tensor(tensor_like, device=device)during conversion.TensorLikePair.check_dtype=True, which will still correctly raise anAssertionErrorfor mismatched dtypes (e.g.,float32vsfloat64), as documented.Link to Issue
Fixes #161863
Test Plan
test/test_testing.pycovering CPU tensor vs scalar, CUDA tensor vs scalar, NumPy scalar vs tensor, and type checks.cc @mruberry @rgommers