code: def grid_index(
      height: int,
      width: int,
      device: Optional[torch.device] = None,
  ) -> Tensor:
      w = width
      kernel = torch.tensor(
          [-w - 1, -1, w - 1, -w, 0, w, -w + 1, 1, w + 1],
          device=device,
      )
  
      row = torch.arange(
          height * width, dtype=torch.long, device=device
      )
      row = row.view(-1, 1).repeat(1, kernel.size(0))
      col = row + kernel.view(1, -1)
      row, col = row.view(height, -1), col.view(height, -1)
      index = torch.arange(
          3, row.size(1) - 3, dtype=torch.long, device=device
      )
      row, col = row[:, index].view(-1), col[:, index].view(-1)
  
      mask = (col >= 0) & (col < height * width)
      row, col = row[mask], col[mask]
  
      edge_index = torch.stack([row, col], dim=0)
      edge_index = coalesce(edge_index, num_nodes=height * width)
      return edge_index
  
imports: from typing import Optional, Tuple
  import torch
  from torch import Tensor
  from torch_geometric.utils import coalesce
label_desc: Use image node_id 2 for calling a global function with example usage: grid_index(height, width, device) and returns: edge_index