Repository navigation
Conversation
The CPU topk function allocated k columns even if there were fewer items, so recommend/similar_items/similar_users with N > n_items returned the remaining slots filled with itemid 0 and a score of 0. ranking_metrics_at_k counted those padded entries as hits (precision > 1 when a user liked item 0), and read past the end of the returned ids when the model returned fewer than K items. Limit k to the number of items in topk, and only look at the returned items in ranking_metrics_at_k.
Author
|
@benfred gentle ping: could you take a look when you have time? The ranking metrics read past the end of the array in some cases, which can give precision > 1; the PR includes a test. |
benfred
reviewed
Oct 28, 2026
benfred
left a comment
Owner
There was a problem hiding this comment.
thanks!
Mostly looks good, some nitpicks below:
| cdef int query_rows = query.shape[0] | ||
|
|
||
| # we can't return more results than there are items | ||
| k = min(k, items.shape[0]) |
Owner
There was a problem hiding this comment.
we should probably also fix this in the GPU code
Line 132 in 8a95dbe
|
|
||
| # the model can return fewer than K items (for instance if K is larger than | ||
| # the number of items), so only look at the items that were actually returned | ||
| num_recs = min(K, ids.shape[1]) |
Owner
There was a problem hiding this comment.
maybe just num_recs = min(K, items) ? (The model shouldn't be returning fewer than this).
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
When
Nis larger than the number of items, the CPU matrix factorization models returnNresults anyway, and the extra slots are filled with itemid0and score0:This breaks
ranking_metrics_at_kwhenKis larger than the number of items. Every padded0counts as a hit for users that have item 0 in the test set, so precision can go above 1. For example, on a 300x200 random matrix,ranking_metrics_at_k(model, train, test, K=300)givesprecision=1.49. Also,ranking_metrics_at_kalways loops overKcolumns ofidswith bounds checking disabled, so if a model returns fewer thanKitems, it reads past the end of the array.Changes:
implicit/cpu/topk.pyx: limitkto the number of items. This fixesrecommend,similar_itemsandsimilar_usersfor the CPU models. The existingtest_recommendalready says that asking for too many items "should return only the available items" (recommend N top recommendations #22).implicit/evaluation.pyx: only loop over the items that the model actually returned.I added
test_ranking_metrics_k_larger_than_number_of_itemsfor ALS, BPR and LMF. It checks thatrecommendwithN=100on 50 items returns 50 columns, and that the metrics withK=100are the same as withK=50. It fails on main and passes with this change. The full test suite passes locally on macOS arm64 (167 passed, 54 skipped since I don't have CUDA). I didn't change the GPU code path since I can't test it.