Skip to content
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

Wrong evaluation in sparse/higherorderfns.jl fix#23857 #24806

Merged
merged 5 commits into from
Mar 22, 2018
Merged

Wrong evaluation in sparse/higherorderfns.jl fix#23857 #24806

merged 5 commits into from
Mar 22, 2018

Conversation

KlausC
Copy link
Contributor

@KlausC KlausC commented Nov 27, 2017

Division of a sparse matrix by row vector causes 0's to become NaN
#23857

@Sacha0
Copy link
Member

Sacha0 commented Nov 27, 2017

Thanks @KlausC! Apologies in advance for not reviewing this pull request in the short term. (Time is short with feature freeze fast approaching, and I have been remiss in reviewing a number of higher priority items as it stands.) Best!

@kshyatt kshyatt added the domain:arrays:sparse Sparse arrays label Nov 30, 2017
@KlausC
Copy link
Contributor Author

KlausC commented Dec 27, 2017

@Sacha0 just a reminder - it's 30 days now

@Sacha0
Copy link
Member

Sacha0 commented Dec 27, 2017

Regrettably, work falling under the heading bugfix will need to wait till after proper feature freeze (and likely then some) :).

@StefanKarpinski StefanKarpinski added the kind:bugfix This change fixes an existing bug label Dec 27, 2017
@KlausC
Copy link
Contributor Author

KlausC commented Mar 1, 2018

Maybe it is time now to have a lock on this bug fix. The change was rather simple, as far as I remember.

@KlausC
Copy link
Contributor Author

KlausC commented Mar 20, 2018

@mbauman , @andreasnoack , wouldn't it make sense to merge this minor bug fix as well. I fear it is getting lost after similar changes have been done to the same file(s). #26474

@mbauman
Copy link
Sponsor Member

mbauman commented Mar 20, 2018

I'm so sorry that this has fallen through the cracks. The change here is indeed simpler than it looks — here's a better view: https://github.com/JuliaLang/julia/pull/24806/files?w=1

I'm hesitant here because a very similar structure is used in the other branches and functions here — do they need the same fix? I'd like to understand a bit more about why this is failing, but I'm not seeing it at the moment.

@KlausC
Copy link
Contributor Author

KlausC commented Mar 20, 2018

I'd like to understand a bit more about why this is failing

It is nearly 4 month by now. I think, I understood it then, but have to recover now. I will try tomorrow, to explain the change. At least it did not break any tests and fixed the reported errors.

@@ -719,23 +719,15 @@ function _broadcast_notzeropres!(f::Tf, fillvalue, C::SparseVecOrMat, A::SparseV
Bk, stopBk = numcols(B) == 1 ? (colstartind(B, 1), colboundind(B, 1)) : (colstartind(B, j), colboundind(B, j))
Bx = Bk < stopBk ? storedvals(B)[Bk] : zero(eltype(B))
fzAvB = f(zero(eltype(A)), Bx)
if _iszero(fzAvB)
Copy link
Member

Choose a reason for hiding this comment

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

Having briefly refreshed my memory of these methods, I believe that the _iszero(fzAvB) check (and similar _iszero(fvAzB) check in the preceding branch where A rather than B expands vertically) should instead be something to the effect of fzAvB == fillvalue in the methods for the not-zero-preserving-f case. These branches should stay around, however, as otherwise performance may degrade appreciably. Regrettably a nanosoldier suite for these methods remains a "one day" project. Best!

@KlausC
Copy link
Contributor Author

KlausC commented Mar 21, 2018

_broadcast_nonzeropres! is only called, if A or B have to be expanded vertically, that means each column of A or B has to be replaced by copies of the first element.

I think the _broadcast_nozeropres! function works as follows:

  1. It expands destination array C to full size (C.m*C.n) and fills all value cells with value f(zero(), zero()). (lines 644ff)
  2. According to the shapes of A and B it uses different "branches", which have some structure in common
  3. The outer loop proceeds column by column - index j.
  4. assuming A is to be expanded, Ax is the value of A[1,j], which has to be joined with all B[i,j] for all row indices i.
  5. An optimization is tried as follows: if f(Ax, zero(()) == fillvalue only the elements B[i,j] different from zero can contribute new values to the result. So only nonzero part of B[:,j] has to be looped, thus exploiting the sparsity of B . The other slots of C already are populated with fillvalue.
  6. In the other case, f(Ax, B[i,j]) has to be stored for all i. In this case, the pre-calculated f(Ax, zero()) can be re-used for all i with B[i,j] == zero().

The bug is, that instead of f(Ax, zero(()) == fillvalue the program checks for f(Ax, zero()) == zero(). That is presumably a copy-error form _broadcst_zeropres!.

Copy link
Sponsor Member

@mbauman mbauman left a 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 and change. Makes sense, and this now matches the vararg _broadcast_notzeropres! method below.

Copy link
Member

@Sacha0 Sacha0 left a comment

Choose a reason for hiding this comment

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

Thanks for persisting with this @KlausC! :)

@Sacha0
Copy link
Member

Sacha0 commented Mar 22, 2018

(Absent objections, requests for time, or someone beating me to it, I plan to merge these changes tomorrow morning PST or later. Best!)

@mbauman mbauman merged commit c38298a into JuliaLang:master Mar 22, 2018
@KlausC KlausC deleted the fix#23857 branch March 23, 2018 09:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:arrays:sparse Sparse arrays kind:bugfix This change fixes an existing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants