-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Remove exception due to supportsSparse:true in ReadFromTextLoader #2579
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
Conversation
In the GitHubLabeler sample, for example, there are ~450 InvalidOperationExceptions getting thrown and caught internally, as the implementation tries to parse some text as an int and fails. There's no need for an exception here; the implementation is already using a TryParse method, which it then turns into an exception and then immediately catches and ignores... we can just cut out the middle man and not throw.
Codecov Report
@@ Coverage Diff @@
## master #2579 +/- ##
==========================================
+ Coverage 71.5% 71.51% +<.01%
==========================================
Files 801 801
Lines 142023 142024 +1
Branches 16147 16148 +1
==========================================
+ Hits 101557 101563 +6
+ Misses 35998 35993 -5
Partials 4468 4468
|
TryParseSigned(I4.MaxValue, in span, out long? res); | ||
if (res.HasValue) | ||
{ | ||
value = (I4)res.GetValueOrDefault(); |
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.
GetValueOrDefault [](start = 32, length = 17)
Why not just GetValue() since you have already checked it has value? Alternatively we can remove this if condition and do the below:
value = (I4)res.GetValueOrDefault();
return res.HasValue;
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.
Why not just GetValue() since you have already checked it has value?
Exactly because we already checked it has a value. GetValueOrDefault()
just does return value;
whereas .Value
does something like if (!_hasValue) throw new Exception(); return _value;
.
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 for the explanation. My understanding of these routines was the exact opposite of what they really do. Looks good!
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.
Hmmm. We may have some code to rewrite a number of places... opened #2612 to track this.
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.
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.
Thank you @stephentoub .
@@ -1585,6 +1585,17 @@ public void Convert(in TX span, ref I4 value) | |||
{ | |||
value = ParseI4(in span); | |||
} | |||
public bool TryConvert(in TX span, ref I4 value) |
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.
Why not use the existing TryParse
method?
machinelearning/src/Microsoft.ML.Data/Data/Conversion.cs
Lines 1184 to 1201 in 369a9b6
/// <summary> | |
/// This produces zero for empty. It returns false if the text is not parsable or overflows. | |
/// On failure, it sets dst to the defualt value. | |
/// </summary> | |
public bool TryParse(in TX src, out I4 dst) | |
{ | |
dst = default; | |
TryParseSigned(I4.MaxValue, in src, out long? res); | |
if (res == null) | |
{ | |
dst = default; | |
return false; | |
} | |
Contracts.Assert(res.HasValue); | |
Contracts.Check((I4)res == res, "Overflow or underflow occured while converting value in text to int."); | |
dst = (I4)res; | |
return true; | |
} |
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 was trying to remain consistent with the existing code, which assigned a value, then passed it by ref, and then checked whether it remained what it was assigned. In practice it wouldn't matter because it would end up with the default value in either case, but it would incur additional writes in the failure case, and I didn't know whether it would matter. When I don't know a code base particularly well and what ramifications my changes might have, I try to maintain the existing code as much as possible.
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 guess my concern is the consistency in the internal Conversion
class.
- We now have
TryParse
andTryConvert
. Which one should new callers choose? - We have
TryConvert
for Int32, but no other types?
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.
- Whichever signature they need. If they want an out, they choose TryParse, if they want ref, they choose TryConvert.
- Yes. You'd prefer we add a bunch of dead code?
If you feel strongly about this and want to change the call site to use TryParse, go for it. As I noted, my goal was to muck minimally with the call site logic.
We don't need a TryConvert method, we already have a TryParse, which can be used instead.
In the GitHubLabeler sample, for example, there are ~450 InvalidOperationExceptions getting thrown and caught internally, as the implementation tries to parse some text as an int and fails. There's no need for an exception here; the implementation is already using a TryParse method, which it then turns into an exception and then immediately catches and ignores... we can just cut out the middle man and not throw.
Related to dotnet/machinelearning-samples#257
Related to #2576