Skip to content

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

Merged
merged 1 commit into from
Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Microsoft.ML.Data/Data/Conversion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

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?

/// <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;
}

Copy link
Member Author

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.

Copy link
Member

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.

  1. We now have TryParse and TryConvert. Which one should new callers choose?
  2. We have TryConvert for Int32, but no other types?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Whichever signature they need. If they want an out, they choose TryParse, if they want ref, they choose TryConvert.
  2. 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.

{
TryParseSigned(I4.MaxValue, in span, out long? res);
if (res.HasValue)
{
value = (I4)res.GetValueOrDefault();
Copy link
Member

@codemzs codemzs Feb 18, 2019

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;

Copy link
Member Author

@stephentoub stephentoub Feb 19, 2019

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;.

Copy link
Member

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!

Copy link
Contributor

@TomFinley TomFinley Feb 19, 2019

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.

return true;
}

return false;
}
public void Convert(in TX span, ref U4 value)
{
value = ParseU4(in span);
Expand Down
14 changes: 2 additions & 12 deletions src/Microsoft.ML.Data/DataLoadSave/Text/TextLoaderParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1016,18 +1016,8 @@ public int GatherFields(ReadOnlyMemory<char> lineSpan, ReadOnlySpan<char> span,
}
var spanT = Fields.Spans[Fields.Count - 1];

// Note that Convert throws exception the text is unparsable.
int csrc = default;
try
{
Conversions.Instance.Convert(in spanT, ref csrc);
}
catch
{
Contracts.Assert(csrc == default);
}

if (csrc <= 0)
int csrc = 0;
if (!Conversions.Instance.TryConvert(in spanT, ref csrc) || csrc <= 0)
{
_stats.LogBadFmt(ref scan, "Bad dimensionality or ambiguous sparse item. Use sparse=- for non-sparse file, and/or quote the value.");
break;
Expand Down