code: def adjacency_tensor_to_stacked_matrix(
      num_relations: int,
      num_entities: int,
      source: torch.LongTensor,
      target: torch.LongTensor,
      edge_type: torch.LongTensor,
      edge_weights: Optional[torch.FloatTensor] = None,
      horizontal: bool = True,
  ) -> torch.Tensor:
      """
      Stack adjacency matrices as described in [thanapalasingam2021]_.
  
      This method re-arranges the (sparse) adjacency tensor of shape
      `(num_entities, num_relations, num_entities)` to a sparse adjacency matrix of shape
      `(num_entities, num_relations * num_entities)` (horizontal stacking) or
      `(num_entities * num_relations, num_entities)` (vertical stacking). Thereby, we can perform the relation-specific
      message passing of R-GCN by a single sparse matrix multiplication (and some additional pre- and/or
      post-processing) of the inputs.
  
      :param num_relations:
          the number of relations
      :param num_entities:
          the number of entities
      :param source: shape: (num_triples,)
          the source entity indices
      :param target: shape: (num_triples,)
          the target entity indices
      :param edge_type: shape: (num_triples,)
          the edge type, i.e., relation ID
      :param edge_weights: shape: (num_triples,)
          scalar edge weights
      :param horizontal:
          whether to use horizontal or vertical stacking
  
      :return: shape: `(num_entities * num_relations, num_entities)` or `(num_entities, num_entities * num_relations)`
          the stacked adjacency matrix
      """
      offset = edge_type * num_entities
      if horizontal:
          size = (num_entities, num_relations * num_entities)
          target = offset + target
      else:
          size = (num_relations * num_entities, num_entities)
          source = offset + source
      indices = torch.stack([source, target], dim=0)
      if edge_weights is None:
          edge_weights = torch.ones_like(
              source, dtype=torch.get_default_dtype()
          )
      return torch.sparse_coo_tensor(
          indices=indices,
          values=edge_weights,
          size=size,
      )
  
imports: from __future__ import annotations
  import functools
  import logging
  import pathlib
  import re
  import subprocess
  from abc import ABC, abstractmethod
  from itertools import chain
  from textwrap import dedent
  from typing import Any, Callable, Collection, Dict, Iterable, List, Literal, Mapping, Optional, Sequence, Union, cast
  import more_itertools
  import requests
  import torch
  from tqdm.auto import tqdm
  from ..constants import PYKEEN_MODULE
  from ..typing import OneOrSequence
  from ..utils import nested_get, rate_limited, upgrade_to_sequence
  from ..version import get_version
label_desc: Use image node_id 4 for calling a global function with example usage: adjacency_tensor_to_stacked_matrix(num_relations, num_entities, source, target, edge_type, edge_weights, horizontal) and returns: torch