Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
3,054 changes: 1,592 additions & 1,462 deletions poetry.lock

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,20 @@ pytorch-lightning = [
ipywidgets = {version = ">=7.7,<8.2", optional = true}
plotly = {version="^5.22.0", optional = true}
nbformat = {version = ">=4.2.0", optional = true}

# cupy-cuda12x is incompatible with MacOS.
# It's possible to install pure `cupy` and it will be fully compatible
# but it will work only with CPU and will be slower than `numpy`.
# That's why we don't install cupy at all on MacOS and handle it during the import.
cupy-cuda12x = [
{version = "^13.3.0", python = "<3.13", optional = true},
{version = "^13.4.0", python = ">=3.13", optional = true},
{version = "^13.3.0", python = "<3.13", markers = "sys_platform != 'darwin'", optional = true},
{version = "^13.4.0", python = ">=3.13", markers = "sys_platform != 'darwin'", optional = true},
]
# This is a dependency of cupy-cuda12x
# poetry can't resolve appropriate version of fastrlock for Python 3.13
# and cupy-cuda12x, so we add the version restriction here manually to avoid
# installing older version of fastrlock which is incompatible with Python 3.13
fastrlock = {version = "^0.8.3", optional = true}
fastrlock = {version = "^0.8.3", markers = "sys_platform != 'darwin'", optional = true}

[tool.poetry.extras]
lightfm = ["rectools-lightfm"]
Expand Down
11 changes: 5 additions & 6 deletions rectools/models/rank/rank_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,11 @@ def rank(
)

if filter_pairs_csr is not None:
mask = (
torch.from_numpy(filter_pairs_csr[cur_user_emb_inds].toarray()[:, sorted_object_whitelist]).to(
scores.device
)
!= 0
)
# Convert cur_user_emb_inds to numpy to avoid
# AttributeError: 'torch.dtype' object has no attribute 'kind'
cur_user_filter_pairs_csr = filter_pairs_csr[cur_user_emb_inds.cpu().numpy()]
whitelisted_filter_matrix = cur_user_filter_pairs_csr.toarray()[:, sorted_object_whitelist]
mask = torch.from_numpy(whitelisted_filter_matrix).to(scores.device) != 0
scores = torch.masked_fill(scores, mask, mask_values)

top_scores, top_inds = torch.topk(
Expand Down
19 changes: 10 additions & 9 deletions tests/models/nn/transformers/test_bert4rec.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,9 @@ def get_val_mask_func(interactions: pd.DataFrame, val_users: tp.List[int]) -> np
False,
pd.DataFrame(
{
Columns.User: [10, 10, 10, 30, 30, 30, 40, 40, 40],
Columns.Item: [12, 13, 11, 12, 13, 11, 12, 13, 11],
Columns.Rank: [1, 2, 3, 1, 2, 3, 1, 2, 3],
Columns.User: [30, 30, 30, 40, 40, 40],
Columns.Item: [12, 13, 11, 12, 13, 11],
Columns.Rank: [1, 2, 3, 1, 2, 3],
}
),
pd.DataFrame(
Expand Down Expand Up @@ -289,8 +289,6 @@ def get_trainer() -> Trainer:
similarity_module_kwargs={"distance": u2i_dist},
)
model.fit(dataset=dataset_devices)
users = np.array([10, 30, 40])
actual = model.recommend(users=users, dataset=dataset_devices, k=3, filter_viewed=filter_viewed)
if accelerator == "cpu" and n_devices == 1:
expected = expected_cpu_1
elif accelerator == "cpu" and n_devices == 2:
Expand All @@ -299,6 +297,9 @@ def get_trainer() -> Trainer:
expected = expected_gpu_1
else:
expected = expected_gpu_2
users = np.unique(expected[Columns.User])
actual = model.recommend(users=users, dataset=dataset_devices, k=3, filter_viewed=filter_viewed)

pd.testing.assert_frame_equal(actual.drop(columns=Columns.Score), expected)
pd.testing.assert_frame_equal(
actual.sort_values([Columns.User, Columns.Score], ascending=[True, False]).reset_index(drop=True),
Expand Down Expand Up @@ -392,9 +393,9 @@ def test_u2i_losses(
False,
pd.DataFrame(
{
Columns.User: [10, 10, 30, 30, 40, 40],
Columns.Item: [13, 11, 13, 11, 13, 11],
Columns.Rank: [1, 2, 1, 2, 1, 2],
Columns.User: [30, 30, 40, 40],
Columns.Item: [13, 11, 13, 11],
Columns.Rank: [1, 2, 1, 2],
}
),
),
Expand All @@ -421,7 +422,7 @@ def test_with_whitelist(
similarity_module_type=DistanceSimilarityModule,
)
model.fit(dataset=dataset_devices)
users = np.array([10, 30, 40])
users = np.unique(expected[Columns.User])
items_to_recommend = np.array([11, 13, 17])
actual = model.recommend(
users=users,
Expand Down
14 changes: 7 additions & 7 deletions tests/models/nn/transformers/test_sasrec.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,9 +556,9 @@ def test_with_whitelist(
None,
pd.DataFrame(
{
Columns.TargetItem: [12, 12, 12, 14, 14, 14, 17, 17, 17],
Columns.Item: [12, 13, 14, 14, 12, 15, 17, 13, 14],
Columns.Rank: [1, 2, 3, 1, 2, 3, 1, 2, 3],
Columns.TargetItem: [12, 12, 12, 17, 17, 17],
Columns.Item: [12, 13, 14, 17, 13, 14],
Columns.Rank: [1, 2, 3, 1, 2, 3],
}
),
),
Expand All @@ -567,9 +567,9 @@ def test_with_whitelist(
None,
pd.DataFrame(
{
Columns.TargetItem: [12, 12, 12, 14, 14, 14, 17, 17, 17],
Columns.Item: [13, 14, 11, 12, 15, 17, 13, 14, 11],
Columns.Rank: [1, 2, 3, 1, 2, 3, 1, 2, 3],
Columns.TargetItem: [12, 12, 12, 17, 17, 17],
Columns.Item: [13, 14, 11, 13, 14, 11],
Columns.Rank: [1, 2, 3, 1, 2, 3],
}
),
),
Expand Down Expand Up @@ -607,7 +607,7 @@ def test_i2i(
similarity_module_type=DistanceSimilarityModule,
)
model.fit(dataset=dataset)
target_items = np.array([12, 14, 17])
target_items = np.unique(expected[Columns.TargetItem])
actual = model.recommend_to_items(
target_items=target_items,
dataset=dataset,
Expand Down
8 changes: 4 additions & 4 deletions tests/models/test_implicit_bpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ def dataset(self) -> Dataset:
False,
pd.DataFrame(
{
Columns.User: [10, 10, 20, 20],
Columns.Item: [11, 17, 11, 17],
Columns.Rank: [1, 2, 1, 2],
Columns.User: [20, 20],
Columns.Item: [11, 17],
Columns.Rank: [1, 2],
}
),
pd.DataFrame(
Expand All @@ -120,7 +120,7 @@ def test_basic(
self._init_model_factors_inplace(base_model, dataset)
model = ImplicitBPRWrapperModel(model=base_model).fit(dataset)
actual = model.recommend(
users=np.array([10, 20]),
users=np.unique(expected_cpu[Columns.User]),
dataset=dataset,
k=2,
filter_viewed=filter_viewed,
Expand Down