model fit crushes with more than 2^31 positive interactions
This is probably due to some int32 index. Could you please give me any feedback on this?
Yeah - I think the problem is that we were assuming a int32 indices/indptr being used as part of the sparse matrix passed in. When you have more than 2^31 non zero values passed in this fails.
This commit should fix https://github.com/benfred/implicit/commit/36732e31ff0537a2d7caf95008649f877c3453bf for at least the als model. Can you pull from master and see if that fixes your problems?
Thanks for the response! I am actually using the bpr model. Would it be possible to have a fix for bpr?
BPR should be fixed with this commit: https://github.com/benfred/implicit/commit/73565513544995f2fd624602cab0565ea03e083d .
I haven't pushed to pypi yet, but building from master here should get that in.
I tried building from master but I still get a segmentation fault. Maybe I am not building it properly. Should I do something more than python setup.py build python setup.py install ?
I think your build instructions should work, though I realized that I didn't check in the compiled cython file, so if you don't have cython installed it might have still picked up the old version. That should be fixed with this commit: https://github.com/benfred/implicit/commit/b8c6b2bc99948b618d51417eab712ed3440373c8
Any chance you can get me the stack trace of where its crashing? Like on linux, run 'gdb --args python yourprogram.py' or something.
I tested it out using some random data here (most real datasets I have are at least an order of magnitude smaller), and with the latest changes it worked for me. Can you test if this crashes on your system?
import numpy
import scipy.sparse
import implicit.bpr
import logging
logging.basicConfig(level=logging.DEBUG)
# create a large sparse matrix
count = 2200000000
colids = numpy.random.randint(100000, size=count, dtype=numpy.int32)
rowids = numpy.random.randint(100000, size=count, dtype=numpy.int32)
vals = numpy.ones(count, dtype=numpy.float32)
m = scipy.sparse.coo_matrix((vals, (colids, rowids)), shape=(100000, 100000))
# create model using
model = implicit.bpr.BayesianPersonalizedRanking()
model.fit(m)
I also bumped the version and pushed to pypi - I think its possible that you're still running the old version somehow (setup.py might not update properly if the version numbers are the same).
With version 0.3.2 I get this error: File "implicit/bpr.pyx", line 139, in implicit.bpr.BayesianPersonalizedRanking.fit IndexError: boolean index did not match indexed array along dimension 0; dimension is 43060797 but corresponding boolean dimension is 43060796
That last error is unrelated to the problem > 2^31 ratings being passed in.
It seems like the last error is because the ratings matrix has a user with no ratings in the last column of the input matrix, which exposed a new bug. This commit https://github.com/benfred/implicit/commit/1cb420a23b83dfd368a6b7341dcc2402ed280a28 adds a unittest for this bug, and fixes it.
That was indeed unrelated to the >2^31 problem but the same data worked with version 0.3.1. With 0.3.3 seems to be working again. However I still have the segfault:
#0 0x00007fffa607f5d0 in ?? () from /usr/local/lib/python3.6/site-packages/numpy/core/umath.cpython-36m-x86_64-linux-gnu.so
#1 0x00007fffa6080b3b in ?? () from /usr/local/lib/python3.6/site-packages/numpy/core/umath.cpython-36m-x86_64-linux-gnu.so
#2 0x00000000004aef1c in _PyCFunction_FastCallDict (kwargs=0x7fff928bcab0, nargs=
Interesting! thanks for sending that.
If I'm reading that right, it looks like its crashing in numpy code, which is being called from line 3453 in bpr.cpp
#0 0x00007fffa607f5d0 in ?? () from /usr/local/lib/python3.6/site-packages/numpy/core/umath.cpython-36m-x86_64-linux-gnu.so
#1 0x00007fffa6080b3b in ?? () from /usr/local/lib/python3.6/site-packages/numpy/core/umath.cpython-36m-x86_64-linux-gnu.so
...
#25 0x00007fff990ffd3b in __pyx_pf_8implicit_3bpr_27BayesianPersonalizedRanking_2fit (__pyx_v_self=0x7ffff6ccf2b0, __pyx_v_item_users=, __pyx_self=) at implicit/bpr.cpp:3453
Looking at bpr.cpp just before line 3453, it seems like its getting caused by
/* "implicit/bpr.pyx":121
* # for now, all we handle is float 32 values
* if Ciu.dtype != np.float32:
* Ciu = Ciu.astype(np.float32) # <<<<<<<<<<<<<<
*
* # initialize factors
Can you try calling
Ciu = Ciu.astype(np.float32) on the matrix you pass in? It seems like this might be an out of memory type condition
I tried it and it seems to be working fine, without consuming too much of my memory.
OK, so I tried casting the matrix to np.float32 and then pass it to fit, and it works! The only slight problem now is that the DEBUG message prints a wrong/negative percentage of correctly classified training samples, while training. I guess this is again due to an index overflow.
Awesome! Glad it works.
Pushed a fix in v0.3.4 for the logging message - https://github.com/benfred/implicit/commit/dfb62a473f4f8265f828d469fdc673e995c6decf
Thanks! Great job!
Now I am trying to train the AlternatingLeastSquares model with the same data (more than 2^31 samples) and it seems it is not working properly. It has been running for days without completing an iteration. When the samples are (a bit) less than 2^31 an iteration takes just a few minutes.
@MariosGr There is a fix for the ALS model here https://github.com/benfred/implicit/pull/400/files ( sorry for the late reply )
import numpy import scipy.sparse
import implicit.bpr
import logging logging.basicConfig(level=logging.DEBUG)
create a large sparse matrix
count = 2200000000 colids = numpy.random.randint(100000, size=count, dtype=numpy.int32) rowids = numpy.random.randint(100000, size=count, dtype=numpy.int32) vals = numpy.ones(count, dtype=numpy.float32) m = scipy.sparse.coo_matrix((vals, (colids, rowids)), shape=(100000, 100000))
create model using
model = implicit.bpr.BayesianPersonalizedRanking() model.fit(m)
Hey @benfred - An FYI I ran your example code and I'm getting the "Buffer dtype mismatch, expected 'int' but got 'long':
ValueError Traceback (most recent call last)
implicit/bpr.pyx in implicit.bpr.BayesianPersonalizedRanking.fit()
implicit/bpr.pyx in implicit.bpr.BayesianPersonalizedRanking._fit_gpu()
implicit/cuda/_cuda.pyx in implicit.cuda._cuda.CuIntVector.cinit()
ValueError: Buffer dtype mismatch, expected 'int' but got 'long'
This is an issue over here for nearest neighbors: https://github.com/benfred/implicit/issues/360, where you recommend reducing K, but just wanted to raise the issue that it's also popping up with BPR.