Repository: pyg-team/pytorch_geometric

Issue:
Confusing UserWarning for `recommender_system.py` example
### 🐛 Describe the bug

```
/usr/local/lib/python3.10/dist-packages/torch_geometric/nn/pool/knn.py:115: UserWarning: Capping 'k' to faiss' upper limit of 2048 (got 1000). This may cause some relevant items to not be retrieved.
  warnings.warn(f"Capping 'k' to faiss' upper limit of 2048 "
self.numel= 9742
```

this happens because even though the user inputted k is <2048, query_k=min(query_l, self.numel)=9472. this then triggers the warning but the warning tells the user that we are capping at 2048 because they passed 1000. This is very unintuitive for a user unless they read through the code themselves.

Problem Warning Code:
```
        query_k = k

        if exclude_links is not None:
            deg = degree(exclude_links[0], num_nodes=emb.size(0)).max()
            query_k = k + int(deg.max() if deg.numel() > 0 else 0)
        print("self.numel=",self.numel)
        query_k = min(query_k, self.numel)

        if query_k > 2048:  # `faiss` supports up-to `k=2048`:
            warnings.warn(f"Capping 'k' to faiss' upper limit of 2048 "
                          f"(got {k}). This may cause some relevant items to "
                          f"not be retrieved.")
            query_k = 2048

        score, index = self.index.search(emb.detach(), query_k)

```


### Versions

latest


Discussion:

Resolving patch:
diff --git a/torch_geometric/nn/pool/knn.py b/torch_geometric/nn/pool/knn.py
--- a/torch_geometric/nn/pool/knn.py
+++ b/torch_geometric/nn/pool/knn.py
@@ -111,10 +111,15 @@ def search(
 
         query_k = min(query_k, self.numel)
 
-        if query_k > 2048:  # `faiss` supports up-to `k=2048`:
+        if k > 2048:  # `faiss` supports up-to `k=2048`:
             warnings.warn(f"Capping 'k' to faiss' upper limit of 2048 "
                           f"(got {k}). This may cause some relevant items to "
                           f"not be retrieved.")
+        elif query_k > 2048:
+            warnings.warn(f"Capping 'k' to faiss' upper limit of 2048 "
+                          f"(got {k} which got extended to {query_k} due to "
+                          f"the exclusion of existing links). This may cause "
+                          f"some relevant items to not be retrieved.")
             query_k = 2048
 
         score, index = self.index.search(emb.detach(), query_k)
diff --git a/torch_geometric/utils/_negative_sampling.py b/torch_geometric/utils/_negative_sampling.py
--- a/torch_geometric/utils/_negative_sampling.py
+++ b/torch_geometric/utils/_negative_sampling.py
@@ -86,11 +86,12 @@ def negative_sampling(
         # invalid samples.
         mask = idx.new_ones(population, dtype=torch.bool)
         mask[idx] = False
-        for _ in range(3):  # Number of tries to sample negative indices.
+        for _ in range(100):  # Number of tries to sample negative indices.
             rnd = sample(population, sample_size, idx.device)
             rnd = rnd[mask[rnd]]  # Filter true negatives.
             neg_idx = rnd if neg_idx is None else torch.cat([neg_idx, rnd])
             if neg_idx.numel() >= num_neg_samples:
+                print("BREAK")
                 neg_idx = neg_idx[:num_neg_samples]
                 break
             mask[neg_idx] = False
@@ -197,10 +198,14 @@ def batched_negative_sampling(
     neg_edge_indices = []
     for i, edge_index in enumerate(edge_indices):
         edge_index = edge_index - ptr[i]
+        print(i, edge_index.shape, num_nodes[i])
+        print(num_neg_samples)
         neg_edge_index = negative_sampling(edge_index, num_nodes[i],
                                            num_neg_samples, method,
                                            force_undirected)
         neg_edge_index += ptr[i]
+        print(neg_edge_index)
+        print('out', neg_edge_index.shape)
         neg_edge_indices.append(neg_edge_index)
 
     return torch.cat(neg_edge_indices, dim=1)


Test patch: