Graphormer

## [](#overview)Overview

The Graphormer model was proposed in [Do Transformers Really Perform Bad for Graph Representation?](https://arxiv.org/abs/2106.05234) by Chengxuan Ying, Tianle Cai, Shengjie Luo, Shuxin Zheng, Guolin Ke, Di He, Yanming Shen and Tie-Yan Liu. It is a Graph Transformer model, modified to allow computations on graphs instead of text sequences by generating embeddings and features of interest during preprocessing and collation, then using a modified attention.

The abstract from the paper is the following:

_The Transformer architecture has become a dominant choice in many domains, such as natural language processing and computer vision. Yet, it has not achieved competitive performance on popular leaderboards of graph-level prediction compared to mainstream GNN variants. Therefore, it remains a mystery how Transformers could perform well for graph representation learning. In this paper, we solve this mystery by presenting Graphormer, which is built upon the standard Transformer architecture, and could attain excellent results on a broad range of graph representation learning tasks, especially on the recent OGB Large-Scale Challenge. Our key insight to utilizing Transformer in the graph is the necessity of effectively encoding the structural information of a graph into the model. To this end, we propose several simple yet effective structural encoding methods to help Graphormer better model graph-structured data. Besides, we mathematically characterize the expressive power of Graphormer and exhibit that with our ways of encoding the structural information of graphs, many popular GNN variants could be covered as the special cases of Graphormer._

Tips:

This model will not work well on large graphs (more than 100 nodes/edges), as it will make the memory explode. You can reduce the batch size, increase your RAM, or decrease the `UNREACHABLE_NODE_DISTANCE` parameter in algos\_graphormer.pyx, but it will be hard to go above 700 nodes/edges.

This model does not use a tokenizer, but instead a special collator during training.

This model was contributed by [clefourrier](https://huggingface.co/clefourrier). The original code can be found [here](https://github.com/microsoft/Graphormer).

## [](#transformers.GraphormerConfig)GraphormerConfig

### class transformers.GraphormerConfig

[](#transformers.GraphormerConfig)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/models/graphormer/configuration_graphormer.py#L30)

( num\_classes: int = 1num\_atoms: int = 4608num\_edges: int = 1536num\_in\_degree: int = 512num\_out\_degree: int = 512num\_spatial: int = 512num\_edge\_dis: int = 128multi\_hop\_max\_dist: int = 5spatial\_pos\_max: int = 1024edge\_type: str = 'multi\_hop'max\_nodes: int = 512share\_input\_output\_embed: bool = Falsenum\_hidden\_layers: int = 12embedding\_dim: int = 768ffn\_embedding\_dim: int = 768num\_attention\_heads: int = 32dropout: float = 0.1attention\_dropout: float = 0.1layerdrop: float = 0.0encoder\_normalize\_before: bool = Falsepre\_layernorm: bool = Falseapply\_graphormer\_init: bool = Falseactivation\_fn: str = 'gelu'embed\_scale: float = Nonefreeze\_embeddings: bool = Falsenum\_trans\_layers\_to\_freeze: int = 0traceable: bool = Falseq\_noise: float = 0.0qn\_block\_size: int = 8kdim: int = Nonevdim: int = Nonebias: bool = Trueself\_attention: bool = Truepad\_token\_id = 0bos\_token\_id = 1eos\_token\_id = 2\*\*kwargs )

This is the configuration class to store the configuration of a [~GraphormerModel](/docs/transformers/v4.30.0/en/model_doc/graphormer#transformers.GraphormerModel). It is used to instantiate an Graphormer model according to the specified arguments, defining the model architecture. Instantiating a configuration with the defaults will yield a similar configuration to that of the Graphormer [graphormer-base-pcqm4mv1](https://huggingface.co/graphormer-base-pcqm4mv1) architecture.

Configuration objects inherit from [PretrainedConfig](/docs/transformers/v4.30.0/en/main_classes/configuration#transformers.PretrainedConfig) and can be used to control the model outputs. Read the documentation from [PretrainedConfig](/docs/transformers/v4.30.0/en/main_classes/configuration#transformers.PretrainedConfig) for more information.

## [](#transformers.GraphormerModel)GraphormerModel

### class transformers.GraphormerModel

[](#transformers.GraphormerModel)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/models/graphormer/modeling_graphormer.py#L782)

( config: GraphormerConfig )

The Graphormer model is a graph-encoder model.

It goes from a graph to its representation. If you want to use the model for a downstream classification task, use GraphormerForGraphClassification instead. For any other downstream task, feel free to add a new class, or combine this model with a downstream model of your choice, following the example in GraphormerForGraphClassification.

#### forward

[](#transformers.GraphormerModel.forward)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/models/graphormer/modeling_graphormer.py#L811)

( input\_nodes: LongTensorinput\_edges: LongTensorattn\_bias: Tensorin\_degree: LongTensorout\_degree: LongTensorspatial\_pos: LongTensorattn\_edge\_type: LongTensorperturb = Nonemasked\_tokens = Nonereturn\_dict: typing.Optional\[bool\] = None\*\*unused )

## [](#transformers.GraphormerForGraphClassification)GraphormerForGraphClassification

### class transformers.GraphormerForGraphClassification

[](#transformers.GraphormerForGraphClassification)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/models/graphormer/modeling_graphormer.py#L853)

( config: GraphormerConfig )

This model can be used for graph-level classification or regression tasks.

It can be trained on

-   regression (by setting config.num\_classes to 1); there should be one float-type label per graph
-   one task classification (by setting config.num\_classes to the number of classes); there should be one integer label per graph
-   binary multi-task classification (by setting config.num\_classes to the number of labels); there should be a list of integer labels for each graph.

#### forward

[](#transformers.GraphormerForGraphClassification.forward)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/models/graphormer/modeling_graphormer.py#L876)

( input\_nodes: LongTensorinput\_edges: LongTensorattn\_bias: Tensorin\_degree: LongTensorout\_degree: LongTensorspatial\_pos: LongTensorattn\_edge\_type: LongTensorlabels: typing.Optional\[torch.LongTensor\] = Nonereturn\_dict: typing.Optional\[bool\] = None\*\*unused )