Repository: pyg-team/pytorch_geometric

Issue:
in utils.subgraph.py  RuntimeError: indices should be either on cpu or on the same device as the indexed tensor (cpu)
### 🐛 Describe the bug

in utils.subgraph.py

edge_mask = node_mask[edge_index[0]] & node_mask[edge_index[1]]

RuntimeError: indices should be either on cpu or on the same device as the indexed tensor (cpu)

because edge_index on 'cuda:0' and node_mask on 'cpu'

being solved with: node_mask=node_mask.to(device=device)




### Versions

last version


Discussion:
Is this a bug? `subset` and `edge_index` needs to be on the same device. If that's not the case, the code is expected to crash.
Thank you for your prompt reply.
You must be right, I'm new to Python and the error can be on my side

Subset and edge_index were not on the same device, this causes the error
subset was generated by transform RadiusGraph<https://pytorch-geometric.readthedocs.io/en/latest/generated/torch_geometric.transforms.RadiusGraph.html#torch_geometric.transforms.RadiusGraph> or transform.largest_connected_components from a data on cuda:0
and ended up on the cpu whereas edge_index was cuda:0

I propose you to write the small piece of code that reproduce this error ?

Best,

Cedric
________________________________
De : Matthias Fey ***@***.***>
Envoyé : jeudi 25 janvier 2024 03:23
À : pyg-team/pytorch_geometric ***@***.***>
Cc : Allier, Cedric ***@***.***>; Author ***@***.***>
Objet : Re: [pyg-team/pytorch_geometric] in utils.subgraph.py RuntimeError: indices should be either on cpu or on the same device as the indexed tensor (cpu) (Issue #8817)

External Email: Use Caution



Is this a bug? subset and edge_index needs to be on the same device. If that's not the case, the code is expected to crash.

—
Reply to this email directly, view it on GitHub<https://urldefense.com/v3/__https://github.com/pyg-team/pytorch_geometric/issues/8817*issuecomment-1909639728__;Iw!!Eh6p8Q!AwlkJ5C8KHDsP_TlBDgeHSSSTijW5i_wsu400q9JUe3lh4jdubuJmsnSois5mvT3NWaFV5x9Gy4h0T2s_CfwsqTJj28$>, or unsubscribe<https://urldefense.com/v3/__https://github.com/notifications/unsubscribe-auth/A5KWXEF7NTA3QQNPRYHXVGDYQII7PAVCNFSM6AAAAABCJAS44WVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMBZGYZTSNZSHA__;!!Eh6p8Q!AwlkJ5C8KHDsP_TlBDgeHSSSTijW5i_wsu400q9JUe3lh4jdubuJmsnSois5mvT3NWaFV5x9Gy4h0T2s_CfwWIF0u5c$>.
You are receiving this because you authored the thread.Message ID: ***@***.***>


Resolving patch:
diff --git a/torch_geometric/transforms/largest_connected_components.py b/torch_geometric/transforms/largest_connected_components.py
--- a/torch_geometric/transforms/largest_connected_components.py
+++ b/torch_geometric/transforms/largest_connected_components.py
@@ -47,9 +47,11 @@ def forward(self, data: Data) -> Data:
             return data
 
         _, count = np.unique(component, return_counts=True)
-        subset = np.in1d(component, count.argsort()[-self.num_components:])
+        subset_np = np.in1d(component, count.argsort()[-self.num_components:])
+        subset = torch.from_numpy(subset_np)
+        subset = subset.to(data.edge_index.device, torch.bool)
 
-        return data.subgraph(torch.from_numpy(subset).to(torch.bool))
+        return data.subgraph(subset)
 
     def __repr__(self) -> str:
         return f'{self.__class__.__name__}({self.num_components})'


Test patch: