code: 
  
  
  
  
  
  '''simple docstring'''
  
  from typing import Any, Dict, List, Mapping
  
  import numpy as np
  import torch
  
  from ...utils import is_cython_available, requires_backends
  
  
  if is_cython_available():
        import pyximport
  
        pyximport.install(setup_args={"""include_dirs""": np.get_include()})
        from . import algos_graphormer  # noqa E402
  
  
  
  
  
  def lowerCamelCase		(  __lowerCamelCase				:					Union[str, Any]      , __lowerCamelCase				:					int = 512					)		->int:
      _SCREAMING_SNAKE_CASE             =    x.shape[1] if len(x.shape					) > 1 else 1
      _SCREAMING_SNAKE_CASE             =    1 + np.arange(0      , feature_num * offset      , __lowerCamelCase      , dtype=np.intaa					)
      _SCREAMING_SNAKE_CASE             =    x + feature_offset
      return x
  
  
  
  
  
  def lowerCamelCase		(  __lowerCamelCase				:					Union[str, Any]      , __lowerCamelCase				:					Tuple=True					)		->Optional[int]:
      requires_backends(__lowerCamelCase      , ["""cython"""]					)
  
      if keep_features and "edge_attr" in item.keys():  # edge_attr
          _SCREAMING_SNAKE_CASE             =    np.asarray(item["""edge_attr"""]      , dtype=np.intaa					)
      else:
          _SCREAMING_SNAKE_CASE             =    np.ones((len(item["""edge_index"""][0]					), 1)      , dtype=np.intaa					)  # same embedding for all
  
      if keep_features and "node_feat" in item.keys():  # input_nodes
          _SCREAMING_SNAKE_CASE             =    np.asarray(item["""node_feat"""]      , dtype=np.intaa					)
      else:
          _SCREAMING_SNAKE_CASE             =    np.ones((item["""num_nodes"""], 1)      , dtype=np.intaa					)  # same embedding for all
  
      _SCREAMING_SNAKE_CASE             =    np.asarray(item["""edge_index"""]      , dtype=np.intaa					)
  
      _SCREAMING_SNAKE_CASE             =    convert_to_single_emb(__lowerCamelCase					) + 1
      _SCREAMING_SNAKE_CASE             =    item["""num_nodes"""]
  
      if len(edge_attr.shape					) == 1:
          _SCREAMING_SNAKE_CASE             =    edge_attr[:, None]
      _SCREAMING_SNAKE_CASE             =    np.zeros([num_nodes, num_nodes, edge_attr.shape[-1]]      , dtype=np.intaa					)
      _SCREAMING_SNAKE_CASE             =    convert_to_single_emb(__lowerCamelCase					) + 1
  
      # node adj matrix [num_nodes, num_nodes] bool
      _SCREAMING_SNAKE_CASE             =    np.zeros([num_nodes, num_nodes]      , dtype=__lowerCamelCase					)
      _SCREAMING_SNAKE_CASE             =    True
  
      _SCREAMING_SNAKE_CASE       , _SCREAMING_SNAKE_CASE             =    algos_graphormer.floyd_warshall(__lowerCamelCase					)
      _SCREAMING_SNAKE_CASE             =    np.amax(__lowerCamelCase					)
  
      _SCREAMING_SNAKE_CASE             =    algos_graphormer.gen_edge_input(__lowerCamelCase      , __lowerCamelCase      , __lowerCamelCase					)
      _SCREAMING_SNAKE_CASE             =    np.zeros([num_nodes + 1, num_nodes + 1]      , dtype=np.single					)  # with graph token
  
      # combine
      _SCREAMING_SNAKE_CASE             =    input_nodes + 1  # we shift all indices by one for padding
      _SCREAMING_SNAKE_CASE             =    attn_bias
      _SCREAMING_SNAKE_CASE             =    attn_edge_type
      _SCREAMING_SNAKE_CASE             =    shortest_path_result.astype(np.intaa					) + 1  # we shift all indices by one for padding
      _SCREAMING_SNAKE_CASE             =    np.sum(__lowerCamelCase      , axis=1					).reshape(-1					) + 1  # we shift all indices by one for padding
      _SCREAMING_SNAKE_CASE             =    item["""in_degree"""]  # for undirected graph
      _SCREAMING_SNAKE_CASE             =    input_edges + 1  # we shift all indices by one for padding
      if "labels" not in item:
          _SCREAMING_SNAKE_CASE             =    item["""y"""]
  
      return item
  
  
  
  
  
  
  class       a_  :
  
      '''simple docstring'''
  
  
      def __init__(	self				,	A=20				,	A=False )	->     Optional[int]:
          if not is_cython_available():
              raise ImportError("""Graphormer preprocessing needs Cython (pyximport)""" )
  
          _SCREAMING_SNAKE_CASE             =    spatial_pos_max
          _SCREAMING_SNAKE_CASE             =    on_the_fly_processing
  
  
  
  
  
      def __call__(	self				,	A )	->     Dict[str, Any]:
          if self.on_the_fly_processing:
              _SCREAMING_SNAKE_CASE             =    [preprocess_item(A ) for i in features]
  
          if not isinstance(features[0]				,	A ):
              _SCREAMING_SNAKE_CASE             =    [vars(A ) for f in features]
          _SCREAMING_SNAKE_CASE             =    {}
  
          _SCREAMING_SNAKE_CASE             =    max(len(i["""input_nodes"""] ) for i in features )
          _SCREAMING_SNAKE_CASE             =    len(features[0]["""input_nodes"""][0] )
          _SCREAMING_SNAKE_CASE             =    len(features[0]["""attn_edge_type"""][0][0] )
          _SCREAMING_SNAKE_CASE             =    max(len(i["""input_edges"""][0][0] ) for i in features )
          _SCREAMING_SNAKE_CASE             =    len(features[0]["""input_edges"""][0][0][0] )
          _SCREAMING_SNAKE_CASE             =    len(A )
  
          _SCREAMING_SNAKE_CASE             =    torch.zeros(A				,	max_node_num + 1				,	max_node_num + 1				,	dtype=torch.float )
          _SCREAMING_SNAKE_CASE             =    torch.zeros(A				,	A				,	A				,	A				,	dtype=torch.long )
          _SCREAMING_SNAKE_CASE             =    torch.zeros(A				,	A				,	A				,	dtype=torch.long )
          _SCREAMING_SNAKE_CASE             =    torch.zeros(A				,	A				,	dtype=torch.long )
          _SCREAMING_SNAKE_CASE             =    torch.zeros(A				,	A				,	A				,	dtype=torch.long )
          _SCREAMING_SNAKE_CASE             =    torch.zeros(
              A				,	A				,	A				,	A				,	A				,	dtype=torch.long )
  
          for ix, f in enumerate(A ):
              for k in ["attn_bias", "attn_edge_type", "spatial_pos", "in_degree", "input_nodes", "input_edges"]:
                  _SCREAMING_SNAKE_CASE             =    torch.tensor(f[k] )
  
              if len(f["""attn_bias"""][1:, 1:][f["""spatial_pos"""] >= self.spatial_pos_max] ) > 0:
                  _SCREAMING_SNAKE_CASE             =    float("""-inf""" )
  
              _SCREAMING_SNAKE_CASE             =    f["""attn_bias"""]
              _SCREAMING_SNAKE_CASE             =    f[
                  """attn_edge_type"""
              ]
              _SCREAMING_SNAKE_CASE             =    f["""spatial_pos"""]
              _SCREAMING_SNAKE_CASE             =    f["""in_degree"""]
              _SCREAMING_SNAKE_CASE             =    f["""input_nodes"""]
              _SCREAMING_SNAKE_CASE             =    f["""input_edges"""]
  
          _SCREAMING_SNAKE_CASE             =    batch["""in_degree"""]
  
          _SCREAMING_SNAKE_CASE             =    features[0]["""labels"""]
          if len(A ) == 1:  # one task
              if isinstance(sample[0]				,	A ):  # regression
                  _SCREAMING_SNAKE_CASE             =    torch.from_numpy(np.concatenate([i["""labels"""] for i in features] ) )
              else:  # binary classification
                  _SCREAMING_SNAKE_CASE             =    torch.from_numpy(np.concatenate([i["""labels"""] for i in features] ) )
          else:  # multi task classification, left to float to keep the NaNs
              _SCREAMING_SNAKE_CASE             =    torch.from_numpy(np.stack([i["""labels"""] for i in features]				,	axis=0 ) )
  
          return batch
  
  
  
  
style_context: 
  
  
  
  
  
  '''simple docstring'''
  
  import math
  from timeit import timeit
  
  
  
  
  
  def lowerCamelCase		(  __lowerCamelCase				:					int					)		->int:
      _SCREAMING_SNAKE_CASE             =    0
      _SCREAMING_SNAKE_CASE             =    abs(__lowerCamelCase					)
      while True:
          _SCREAMING_SNAKE_CASE             =    n // 10
          digits += 1
          if n == 0:
              break
      return digits
  
  
  
  
  
  def lowerCamelCase		(  __lowerCamelCase				:					int					)		->int:
      return 1 if n == 0 else math.floor(math.log(abs(__lowerCamelCase					)      , 10					) + 1					)
  
  
  
  
  
  def lowerCamelCase		(  __lowerCamelCase				:					int					)		->int:
      return len(str(abs(__lowerCamelCase					)					)					)
  
  
  
  
  
  def lowerCamelCase		(  )		->None:
      from collections.abc import Callable
  
      def benchmark_a_function(__lowerCamelCase				:					Callable      , __lowerCamelCase				:					int					) -> None:
          _SCREAMING_SNAKE_CASE             =    F'{func.__name__}({value})'
          _SCREAMING_SNAKE_CASE             =    timeit(F'__main__.{call}'      , setup="""import __main__"""					)
          print(F'{call}: {func(__lowerCamelCase					)} -- {timing} seconds'					)
  
      for value in (26_2144, 1125_8999_0684_2624, 126_7650_6002_2822_9401_4967_0320_5376):
          for func in (num_digits, num_digits_fast, num_digits_faster):
              benchmark_a_function(__lowerCamelCase      , __lowerCamelCase					)
          print()
  
  
  if __name__ == "__main__":
        import doctest
  
        doctest.testmod()
        benchmark()
  
  
  
  