code: def test_gat_conv():
      x1 = torch.randn(4, 8)
      x2 = torch.randn(2, 16)
      edge_index = torch.tensor([[0, 1, 2, 3], [0, 0, 1, 1]])
      adj1 = to_torch_csc_tensor(edge_index, size=(4, 4))
  
      conv = GATConv(8, 32, heads=2)
      assert str(conv) == "GATConv(8, 32, heads=2)"
      out = conv(x1, edge_index)
      assert out.size() == (4, 64)
      assert torch.allclose(conv(x1, edge_index, size=(4, 4)), out)
      assert torch.allclose(conv(x1, adj1.t()), out, atol=1e-6)
  
      if torch_geometric.typing.WITH_TORCH_SPARSE:
          adj2 = SparseTensor.from_edge_index(
              edge_index, sparse_sizes=(4, 4)
          )
          assert torch.allclose(conv(x1, adj2.t()), out, atol=1e-6)
  
      if is_full_test():
  
          class MyModule(torch.nn.Module):
              def __init__(self):
                  super().__init__()
                  self.conv = conv
  
              def forward(
                  self,
                  x: Tensor,
                  edge_index: Adj,
                  size: Size = None,
              ) -> Tensor:
                  return self.conv(x, edge_index, size=size)
  
          jit = torch.jit.script(MyModule())
          assert torch.allclose(jit(x1, edge_index), out)
          assert torch.allclose(jit(x1, edge_index, size=(4, 4)), out)
  
          if torch_geometric.typing.WITH_TORCH_SPARSE:
              assert torch.allclose(jit(x1, adj2.t()), out, atol=1e-6)
  
      # Test `return_attention_weights`.
      result = conv(x1, edge_index, return_attention_weights=True)
      assert torch.allclose(result[0], out)
      assert result[1][0].size() == (2, 7)
      assert result[1][1].size() == (7, 2)
      assert result[1][1].min() >= 0 and result[1][1].max() <= 1
  
      if torch_geometric.typing.WITH_PT113:
          # PyTorch < 1.13 does not support multi-dimensional CSR values :(
          result = conv(x1, adj1.t(), return_attention_weights=True)
          assert torch.allclose(result[0], out, atol=1e-6)
          assert result[1][0].size() == torch.Size([4, 4, 2])
          assert result[1][0]._nnz() == 7
  
      if torch_geometric.typing.WITH_TORCH_SPARSE:
          result = conv(x1, adj2.t(), return_attention_weights=True)
          assert torch.allclose(result[0], out, atol=1e-6)
          assert result[1].sizes() == [4, 4, 2] and result[1].nnz() == 7
  
      if is_full_test():
  
          class MyModule(torch.nn.Module):
              def __init__(self):
                  super().__init__()
                  self.conv = conv
  
              def forward(
                  self,
                  x: Tensor,
                  edge_index: Tensor,
              ) -> Tuple[Tensor, Tuple[Tensor, Tensor]]:
                  return self.conv(
                      x, edge_index, return_attention_weights=True
                  )
  
          jit = torch.jit.script(MyModule())
          result = jit(x1, edge_index)
          assert torch.allclose(result[0], out)
          assert result[1][0].size() == (2, 7)
          assert result[1][1].size() == (7, 2)
          assert result[1][1].min() >= 0 and result[1][1].max() <= 1
  
          if torch_geometric.typing.WITH_TORCH_SPARSE:
  
              class MyModule(torch.nn.Module):
                  def __init__(self):
                      super().__init__()
                      self.conv = conv
  
                  def forward(
                      self,
                      x: Tensor,
                      edge_index: SparseTensor,
                  ) -> Tuple[Tensor, SparseTensor]:
                      return self.conv(
                          x, edge_index, return_attention_weights=True
                      )
  
              jit = torch.jit.script(MyModule())
              result = jit(x1, adj2.t())
              assert torch.allclose(result[0], out, atol=1e-6)
              assert (
                  result[1].sizes() == [4, 4, 2]
                  and result[1].nnz() == 7
              )
  
      # Test bipartite message passing:
      adj1 = to_torch_csc_tensor(edge_index, size=(4, 2))
  
      conv = GATConv((8, 16), 32, heads=2)
      assert str(conv) == "GATConv((8, 16), 32, heads=2)"
  
      out1 = conv((x1, x2), edge_index)
      assert out1.size() == (2, 64)
      assert torch.allclose(
          conv((x1, x2), edge_index, size=(4, 2)), out1
      )
      assert torch.allclose(conv((x1, x2), adj1.t()), out1, atol=1e-6)
  
      out2 = conv((x1, None), edge_index, size=(4, 2))
      assert out2.size() == (2, 64)
      assert torch.allclose(conv((x1, None), adj1.t()), out2, atol=1e-6)
  
      if torch_geometric.typing.WITH_TORCH_SPARSE:
          adj2 = SparseTensor.from_edge_index(
              edge_index, sparse_sizes=(4, 2)
          )
          assert torch.allclose(
              conv((x1, x2), adj2.t()), out1, atol=1e-6
          )
          assert torch.allclose(
              conv((x1, None), adj2.t()), out2, atol=1e-6
          )
  
      if is_full_test():
  
          class MyModule(torch.nn.Module):
              def __init__(self):
                  super().__init__()
                  self.conv = conv
  
              def forward(
                  self,
                  x: Tuple[Tensor, Optional[Tensor]],
                  edge_index: Adj,
                  size: Size = None,
              ) -> Tensor:
                  return self.conv(x, edge_index, size=size)
  
          jit = torch.jit.script(MyModule())
          assert torch.allclose(jit((x1, x2), edge_index), out1)
          assert torch.allclose(
              jit((x1, x2), edge_index, size=(4, 2)), out1
          )
          assert torch.allclose(
              jit((x1, None), edge_index, size=(4, 2)), out2
          )
  
          if torch_geometric.typing.WITH_TORCH_SPARSE:
              assert torch.allclose(
                  jit((x1, x2), adj2.t()), out1, atol=1e-6
              )
              assert torch.allclose(
                  jit((x1, None), adj2.t()), out2, atol=1e-6
              )
  
file_path: pytorch_geometric/test/nn/conv/test_gat_conv.py
imports: from typing import Optional, Tuple
  import pytest
  import torch
  from torch import Tensor
  import torch_geometric.typing
  from torch_geometric.nn import GATConv
  from torch_geometric.testing import is_full_test, withCUDA
  from torch_geometric.typing import Adj, Size, SparseTensor
  from torch_geometric.utils import to_torch_csc_tensor
label_desc: Use image node_id 1 for calling a global function with example usage: test_gat_conv() and returns: self, self, self, self