-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[mlir][complex] Prevent underflow in complex.abs #76316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a581036
[mlir][complex] Prevent underflow in complex.abs
Lewuathe c4e8c8b
[mlir][complex] Add test case to check zero element handling
Lewuathe f865a29
Merge branch 'main' into gh-62011
Lewuathe 3e838e1
Merge branch 'main' into gh-62011
Lewuathe d3e818d
Merge branch 'main' into gh-62011
Lewuathe e5a2f57
Merge branch 'main' into gh-62011
Lewuathe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,6 +106,27 @@ func.func @angle(%arg: complex<f32>) -> f32 { | |
func.return %angle : f32 | ||
} | ||
|
||
func.func @test_element_f64(%input: tensor<?xcomplex<f64>>, | ||
%func: (complex<f64>) -> f64) { | ||
%c0 = arith.constant 0 : index | ||
%c1 = arith.constant 1 : index | ||
%size = tensor.dim %input, %c0: tensor<?xcomplex<f64>> | ||
|
||
scf.for %i = %c0 to %size step %c1 { | ||
%elem = tensor.extract %input[%i]: tensor<?xcomplex<f64>> | ||
|
||
%val = func.call_indirect %func(%elem) : (complex<f64>) -> f64 | ||
vector.print %val : f64 | ||
scf.yield | ||
} | ||
func.return | ||
} | ||
|
||
func.func @abs(%arg: complex<f64>) -> f64 { | ||
%abs = complex.abs %arg : complex<f64> | ||
func.return %abs : f64 | ||
} | ||
|
||
func.func @entry() { | ||
// complex.sqrt test | ||
%sqrt_test = arith.constant dense<[ | ||
|
@@ -300,5 +321,28 @@ func.func @entry() { | |
call @test_element(%angle_test_cast, %angle_func) | ||
: (tensor<?xcomplex<f32>>, (complex<f32>) -> f32) -> () | ||
|
||
// complex.abs test | ||
%abs_test = arith.constant dense<[ | ||
Lewuathe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(1.0, 1.0), | ||
// CHECK: 1.414 | ||
(1.0e300, 1.0e300), | ||
// CHECK-NEXT: 1.41421e+300 | ||
(1.0e-300, 1.0e-300), | ||
// CHECK-NEXT: 1.41421e-300 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The returned values are |
||
(5.0, 0.0), | ||
// CHECK-NEXT: 5 | ||
(0.0, 6.0), | ||
// CHECK-NEXT: 6 | ||
(7.0, 8.0) | ||
// CHECK-NEXT: 10.6301 | ||
]> : tensor<6xcomplex<f64>> | ||
%abs_test_cast = tensor.cast %abs_test | ||
: tensor<6xcomplex<f64>> to tensor<?xcomplex<f64>> | ||
|
||
%abs_func = func.constant @abs : (complex<f64>) -> f64 | ||
|
||
call @test_element_f64(%abs_test_cast, %abs_func) | ||
: (tensor<?xcomplex<f64>>, (complex<f64>) -> f64) -> () | ||
|
||
func.return | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering what's going to happen here if
real
is zero. Should the computation be wrapped inscf.if
instead of using anarith.select
below?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should be no problem as long as the
realIsZero
is being true. The decision operator does not seem to be evaluated. The case withreal
is zero is returning expected value.https://github.com/llvm/llvm-project/pull/76316/files#diff-01fda3fd3b20157ca6cbd39d5c8203a33814516e46c22a544d65dddb3008644aR333
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the value is not used afterwards but I'm wondering if executing a division by zero could cause a crash (maybe only on some platforms). Maybe ask on Discourse before merging. If you don't get an answer, I'd say just merge as is and if it causes issues in the future it will be a simple fix (wrapping the computation in
scf.if
instead of usingarith.select
).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. That makes sense. I posted the question here.
https://discourse.llvm.org/t/devision-by-zero-semantics-in-arith-divf/76514