NAR-Former V2: Rethinking Transformer for Universal Neural Network
  Representation Learning
As more deep learning models are being applied in real-world applications,
there is a growing need for modeling and learning the representations of neural
networks themselves. An efficient representation can be used to predict target
attributes of networks without the need for actual training and deployment
procedures, facilitating efficient network deployment and design. Recently,
inspired by the success of Transformer, some Transformer-based representation
learning frameworks have been proposed and achieved promising performance in
handling cell-structured models. However, graph neural network (GNN) based
approaches still dominate the field of learning representation for the entire
network. In this paper, we revisit Transformer and compare it with GNN to
analyse their different architecture characteristics. We then propose a
modified Transformer-based universal neural network representation learning
model NAR-Former V2. It can learn efficient representations from both
cell-structured networks and entire networks. Specifically, we first take the
network as a graph and design a straightforward tokenizer to encode the network
into a sequence. Then, we incorporate the inductive representation learning
capability of GNN into Transformer, enabling Transformer to generalize better
when encountering unseen architecture. Additionally, we introduce a series of
simple yet effective modifications to enhance the ability of the Transformer in
learning representation from graph structures. Our proposed method surpasses
the GNN-based method NNLP by a significant margin in latency estimation on the
NNLQP dataset. Furthermore, regarding accuracy prediction on the NASBench101
and NASBench201 datasets, our method achieves highly comparable performance to
other state-of-the-art methods.
Yun Yi, Haokui Zhang, Rong Xiao, Nannan Wang, Xiaoyu Wang
# NAR-Former V2: Rethinking Transformer for Universal Neural Network Representation Learning

###### Abstract

As more deep learning models are being applied in real-world applications, there is a growing need for modeling and learning the representations of neural networks themselves. An efficient representation can be used to predict target attributes of networks without the need for actual training and deployment procedures, facilitating efficient network deployment and design. Recently, inspired by the success of Transformer, some Transformer-based representation learning frameworks have been proposed and achieved promising performance in handling cell-structured models. However, graph neural network (GNN) based approaches still dominate the field of learning representation for the entire network. In this paper, we revisit Transformer and compare it with GNN to analyse their different architecture characteristics. We then propose a modified Transformer-based universal neural network representation learning model NAR-Former V2. It can learn efficient representations from both cell-structured networks and entire networks. Specifically, we first take the network as a graph and design a straightforward tokenizer to encode the network into a sequence. Then, we incorporate the inductive representation learning capability of GNN into Transformer, enabling Transformer to generalize better when encountering unseen architecture. Additionally, we introduce a series of simple yet effective modifications to enhance the ability of the Transformer in learning representation from graph structures. Our proposed method surpasses the GNN-based method NNLP by a significant margin in latency estimation on the NNLQP dataset. Furthermore, regarding accuracy prediction on the NASBench101 and NASBench201 datasets, our method achieves highly comparable performance to other state-of-the-art methods.

## 1 Introduction

With the maturity of deep learning technology, an increasing number of deep network models of various sizes and structures are being proposed and implemented in academic research and industrial applications. In this process, the rapid deployment of networks and the design of new networks that meet task requirements are significant. To address this issue, researchers propose using machine learning models to solve the deployment and design problems of the models themselves. One popular strategy is encoding the input neural network and utilizing the resulting neural network representation to predict a specific target attribute directly without actually executing the evaluation program. In recent years, we have witnessed success in accelerating model deployment and design processes with the help of neural network representations [2; 3; 11; 10; 9; 8; 20]. Taking the advantages of latency predictors [2; 3; 7; 6; 20; 31], significant time cost and expertise efforts can be saved by not having to carry out the time-consuming process of compilation, deployment, inference, and latency
evaluation when engineers choose networks for application. Through the use of accuracy predictors [2; 9; 10; 12; 20; 8; 26], researchers can avoid the resource-intensive process of network training and instead perform a forward inference process to evaluate the accuracy of a multitude of networks. This measure dramatically reduces the time cost associated with network design.

Although the vanilla Transformer is designed for natural language processing, Transformer architecture has found widespread adoption across diverse fields owing to its strengths in global modeling and parallelizable computation [4; 5; 14; 19; 34; 30]. Very recently, several researchers have attempted to learn appropriate representations for neural networks via Transformer [2; 9]. These methods have indeed achieved leading performance on relevant tasks. Nevertheless, they are mainly designed for encoding the architecture of cells (basic micro units of repeatable neural networks) in cell-structured networks. As shown in the latency prediction experiment in NAR-Former [2], poor generalization performance occurs when the depth of the input architecture reaches hundreds of layers. In the development process of neural network representation learning, Graph neural network (GNN) [32; 35] is also a promising technique for learning neural network representations [3; 24; 23; 22; 20]. They model the input neural architecture as a directed acyclic graph (DAG) and operate on the graph-structured data, which comprises the node information matrix and adjacency matrix. Recently, the NNLP [3] introduced a dedicated latency prediction model based on GNNs, which is capable of encoding the complete neural network having hundreds of layers and achieving a cutting-edge advance.

In fact, both cell-structured architectures and complete neural networks are widely used in various applications. Cell-structured models offer good scalability, allowing for easy scaling by adding or removing cells. This adaptability makes them suitable for addressing problems of different complexities and data sizes, while also facilitating incremental model development and deployment.omplete neural networks provide better flexibility in connectivity and can achieve higher accuracy in certain cases. Furthermore, in some cases, such as in latency estimation, encoding the complete network is necessary. To handle various network architectures in different tasks, both GNN-based and Transformer-based models are necessary. However, this issue of utilizing multiple architectures can introduce constraints that may not be conducive to practical applications. For instance, when a designed network requires specific attributes, having similar model structures and high-accuracy predictions for different attributes can reduce code redundancy and improve work efficiency.

In this paper, we build upon the research conducted in NAR-Former [2] and present a novel framework called NAR-Former V2 for universal neural network representation learning. Our framework can handle cell-structured networks and learning representations for the entire network. To accomplish this, we incorporate graph-specific properties into the vanilla Transformer and introduce a graph-aided attention-based Transformer block. This approach combines the strengths of both Transformer and graph neural networks (GNNs). Extensive experiments are conducted to evaluate our proposed framework. Results show that:(1) our method can be applied to predict different attributes, can outperform the state-of-the-art method in latency prediction on the NNLQP dataset [3], and can achieve promising results in accuracy sorting prediction on the NAS-Bench-101 and NAS-Bench-201 datasets [1; 21]; (2) our method has good scalability, which is capable of encoding network having only a few operations or complete neural networks that have hundreds of operations.

## 2 Related work

### Representation and attribute prediction of neural networks

Neural network representation learning is the base for evaluating the attributes of different networks via machine learning models. Early methods [33; 26] construct representation models for learning neural network representation based on LSTM and MLP. Peephole [33] inputs the embedding of each layer to LSTM to predict accuracy, which neglects the topological structure and is limited to handling only sequential architectures. Later, in order to better capture the structural information of the network, an accuracy predictor [8] uses a binary path encoding with a length equal to the number of possible paths from input to output given in terms of operations, where the element at the corresponding position of the path presenting in the input network is set to 1. When the neural network is regarded as a directed acyclic graph, the adjacency matrix describes the connection between nodes, so it is naturally used to encode the topological structure of the neural network. NAS-Bench-101 [1] proposed to encode the given neural network as a concatenated vector of a flat adjacency matrix
and a list of node labels. Many other methods [3; 7; 11; 20; 22; 23] realize accuracy and latency prediction by directly inputting the original two-dimensional adjacency matrix together with the node information matrix to GNN, which can realize the explicit encoding of the input network topology. Recently, other methods have focused on enhancing the original GNN or introducing transformers to obtain more meaningful neural network representations [16; 12; 9; 2].

### Transformer

Transformer [34] is a self-attention-based neural network architecture that has revolutionized natural language processing [15; 30; 17] and has been adopted in many other fields [4; 5; 14; 2; 9; 18; 19]. Transformer has recently been successfully introduced into neural network representation learning [9; 2]. TNASP [9] inputs the sum of operation type embedding matrix and Laplacian matrix into standard Transformer. NAR-Former [2], on the other hand, encodes each operation and connection information of this operation into token and inputs all tokens into a proposed multi-stage fusion transformer. Excellent attribute prediction results have been achieved on cell-based dataset by using these methods. However, the strong long-range modeling ability of the self-attention mechanism may also result in subtle local variation affecting the representation of all tokens. Due to the potential impact of this feature on the generalization ability, although NAR-Former [2] has made attempts to encode complete neural networks, the results are still unsatisfactory.

### Graph neural network

GNNs are designed to handle graph-structured data, which is a fundamental representation for many real-world problems such as social network analysis and recommendation systems [27; 29]. Given that neural networks can be viewed as graphs, GNN-based models have emerged as a prominent and widely adopted approach for neural network representation learning [3; 7; 11; 20; 22; 23]. GNNs show generalization ability through a simple mechanism of aggregating information from neighbors. For instance, the recently proposed GNN-based model [3] can obtain representations of neural networks with hundreds of layers and achieves new state-of-the-art results in latency prediction, even if the input network structure has not been seen during training. Nevertheless, the simple structural characteristics of GNNs, which contribute to its strong generalization ability, also lead to the need for further improvement in the performance of methods based on original GNN in cellular structure and complete neural network representation learning. Therefore, it is a promising approach for neural network representation learning to combine the Transformer and GNN to leverage the strengths of both models.

Figure 1: Diagrams of three modules. (a) The vanilla Transformer block [25]. (b) The GNN layer1 with mean aggregator [32]. (c) The proposed graph-aided attention Transformer block.
Method

### Motivation

As mentioned in the Sec. 1, Transformer-based models have demonstrated remarkable performance in encoding and learning representations of neural networks when the input is in the form of cells. However, when dealing with complete deep neural networks (DNNs) consisting of hundreds of layers, and the depth of the input data is unknown during training, they may sometimes exhibit poorer performance compared to GNN-based methods. Additionally, as highlighted in [3], real-world applications often show significant differences in the topologies and depths between training and test samples. Consequently, representation learning models must possess strong generalization abilities for handling unseen data. In this regard, GNN-based models appear to achieve better performance.

This observation has prompted us to reconsider the two types of inputs, namely cells and complete DNNs, as well as the two representation learning models, the Transformer and GNN. Through a detailed comparative analysis of the structures of the Transformer and GNN, we speculate that the insufficient generalization capability of Transformer-based methods may be attributed to its structure and computation characteristics. As we know, the self-attention structure in transformers is a crucial design that allows for the effective extraction of global features in a data-driven manner. However, this structure becomes a double-edged sword when learning network representations. For input neural networks with depths of hundreds of layers, the Transformer's impressive capability to capture global information can sometimes lead to excessive sensitivity. This stems from the fact that the Transformer models interactions between all tokens using its self-attention mechanism, treating the entire sequence as a fully connected graph. This dense attention mechanism can give rise to a particular issue: even a subtle variation, such as reducing the kernel size in layer "i" from \(5\times 5\) to \(3\times 3\), can affect the representation of all other layers, ultimately leading to significant differences in the final representation. As a result of this issue, the trained model may be biased toward fitting the training data. Consequently, when the model is employed for inferring architectures outside the training data distribution, it yields inferior results and demonstrates poorer generalization performance. The corresponding experiments are presented in Sec. 4.4.

### Transformer grafted with GNN

Fig.1 shows the vanilla transformer block, GNN block, and our proposed graph-aided attention Transformer block. As shown in Fig.1 (a), the vanilla Transformer block has two major parts:

\[\hat{H}^{l}=\mathrm{SelfAttn}(\mathrm{LN}(H^{l-1}))+H^{l-1}, \tag{1}\] \[H^{l}=\mathrm{FFN}(\mathrm{LN}(\hat{H}^{l}))+\hat{H}^{l}, \tag{2}\]

where \(H^{l}\) is the feature for the layer \(l\). \(\hat{H}^{l}\) is an intermediate result. SelfAttn, FFN, and LN refer to self-attention, feed-forward network, and layer normalization, respectively. GNN block just has one major part, where the representation is updated following:

\[\hat{H}^{l}=\mathrm{GraphAggre}(H^{l-1},A)+W_{r}^{l}H^{l-1}, \tag{3}\] \[H^{l}=\mathrm{L}_{2}(\hat{H}^{l}), \tag{4}\]

where \(\mathrm{GraphAggre}(H^{l-1},A)=W_{a}^{l}(\mathrm{Norm}(A)H^{l-1})\). \(A\in\mathbb{R}^{N\times N}\) is the adjacency matrix, and \(\mathrm{L}_{2}\) denotes the \(\mathrm{l}2\)-normalization function. \(W\) with different superscripts and subscripts represents different learnable transformation matrices.

Comparing formulas (3) and (4) with formulas (1) and (2), we can observe two major differences between the Transformer block and GNN block:

* The Transformer utilizes self-attention to fuse information from a global perspective, while the GNN uses graph aggregation to fuse neighbor information based on the adjacency matrix.
* The Transformer block includes an additional FFN (Feed-Forward Network) component, which enhances information interaction between channels.
The advantage of self-attention lies in its data-driven structure, allowing for flexible adjustment of information fusion weights based on the input data. On the other hand, the advantage of GNN is that graph aggregation focuses on the topological structure. These two advantages are not contradictory to each other. Consequently, we have naturally come up with an idea to combine the strengths of self-attention and graph aggregation. This approach inherits the flexibility of self-attention while benefiting from the good generalization capability of graph aggregation.

To implement this idea, we consider the neural network encoded as a graph, with the operations or layers in the network treated as nodes. Assuming the graph has \(N\) nodes, the transformer layer we have designed for universal neural network representation learning (Fig. 1 (c)) is calculated as follows:

\[\widetilde{H}^{l}=\mathrm{TAEnhance}(H^{l-1},D), \tag{5}\] \[\hat{H}^{l}=\mathrm{L}_{2}(\mathrm{GraphAttn}(\widetilde{H}^{l}, A)+W_{r}^{l}\widetilde{H}^{l}),\] (6) \[H^{l}=\mathrm{GFFN}(\mathrm{LN}(\hat{H}^{l}))+\hat{H}^{l}, \tag{7}\]

where \(D\in\mathbb{R}^{N\times 1}\) is a vector that records the number of nodes directly connected to each node. The Graph-aided Attention (GraphAttn) module is responsible for performing attention calculations using the properties of the graph structure to adjust global self-attention. The Type-aware Enhancement module (TAEnhance) is utilized to further enhance the representation. We introduce Grouped Feed Forward Network (GFFN) by introducing group linear transformation into the original FFN. In the following sections, we will provide a detailed introduction to each component.

Graph-aided attentionIn the proposed graph-aided attention, we employ the adjacency matrix to govern the attention calculation range. Moreover, the adjacency matrix characterizes the inter-layer connection relationships within the neural network, enabling the model to acquire topology knowledge. Hence, we define this module as the Graph-aided Attention module:

\[X^{l}=\mathrm{Sigmoid}(W_{q}^{l}\widetilde{H}^{l}+b_{q}^{l}), \tag{8}\] \[S^{l}=(X^{l}X^{lT}/\sqrt{d})\odot A,\] (9) \[Z^{l}=W_{a}^{l}(\mathrm{Norm}(S^{l})\widetilde{H}^{l})+b_{a}^{l}. \tag{10}\]

The \(\mathrm{Norm}(\cdot)\) means that for each node, the attention weights between it and other nodes are transformed to (0, 1) by dividing it by the sum. The character \(b\) with different superscripts and subscripts represents different learnable biases. The \(d\) refers to feature dimension of \(X^{l}\). Note that simply using the adjacency matrix to control the attention map in self-attention is insufficient. To make this approach work, we have discarded the original softmax operation in self-attention and replaced it with a linear attention mechanism. This is because the softmax operation tends to focus excessively on the current node while neglecting neighboring nodes. Consequently, we have inserted a sigmoid activation function before the linear attention to ensure that all values in the attention map are positive. For further comparisons with the original self-attention, please refer to the supplementary.

Type-Aware enhancement moduleThe connection between a layer and other layers is related to the type of that layer. Therefore, the number of connected layers in each layer can be used to assist the model in learning the type of layer. By fully utilizing the internal characteristics of this graph-structured data, it is beneficial to improve the learned representations. The enhanced representation is obtained by:

\[\mathrm{TAEnhance}(H^{l-1},D)=\mathrm{Sigmoid}(W_{d}^{l}D+b_{d}^{l})\odot H ^{l-1}. \tag{11}\]

### Universal representation learning for neural network

In this subsection, we will present the construction of a comprehensive framework for neural network encoding and representation based on the proposed enhanced Transformer block. We will also explain how this framework can be utilized to predict network attributes. The overall system, as depicted in
Fig. 2, is composed of three consecutive stages: neural network encoding, representation learning, and attribute prediction.

Neural network encodingWe have taken inspiration from the tokenizer used in NAR-Former [2] and made certain modifications to encode the input neural network. For a given network consisting of \(N\) layers or operations, whether it is a cell architecture or a complete DNN, we represent it as a sequence feature comprising vectors corresponding to each layer: \(T=(t_{1},t_{2},\cdots,t_{N})\in\mathbb{R}^{N\times C}\). Each vector encapsulates both the operation and position information: \(t_{i}=(t_{i}^{\mathrm{op}},t_{i}^{\mathrm{os}})\in\mathbb{R}^{C}\).

Following the encoding scheme proposed in NAR-Former [2], we use the position encoding formula [34; 13] to transform the single real-valued numbers (e.g. operation labels and node position indices) of relevant information into a higher-dimensional space. We denote this mapping scheme as \(f_{\mathrm{PE}}(\cdot)\).

For the node position encoding \(t_{i}^{\mathrm{pos}}\), since our improved transformer can obtain the topology information of the network with the help of the adjacency matrix, we only encode the self-position of the node with \(f_{\mathrm{PE}}(\cdot)\). For the operation encoding \(t_{i}^{\mathrm{op}}\), there are slight differences in the specific encoding content for input networks of different scales. If the input is in the form of a cell architecture [1; 21], there are usually no more than ten different options for each architecture operation. In this case, we directly assign category labels to all possible operations, and then use the function \(f_{\mathrm{PE}}(\cdot)\) to encode the label of the operation to obtain \(t_{i}^{\mathrm{op}}\). However, when a complete DNN is used as input [3], more abundant operational information can be extracted and encoded. In this case, we first use one-hot vectors, which ensure the same distance between different categories, to encode the type of operation (e.g. convolution, batch normalization, ReLU, concatenation). Then use the function \(f_{\mathrm{PE}}(\cdot)\) to encode the properties (e.g. kernel size, number of groups) of the operation, which is then concatenated with the one-hot type vector as \(t_{i}^{\mathrm{op}}\).

Representation learningThe model for learning neural network representations \(H^{K}\) is constructed by stacking multiple instances of our proposed enhanced Transformer blocks. These improvements, specifically tailored for neural network data, allow the model to learn representations that are more meaningful and exhibit enhanced generalization capabilities.

Attributes predictingTaking the representation \(H^{K}\) as input, the target attribute can be predicted by using the predicting head:

\[\hat{y}=-\mathrm{logsigmoid}(\mathrm{FC}(\mathrm{ReLU}(\mathrm{FC}(\mathrm{ ReLU}(\mathrm{FC}(H^{K}))))). \tag{12}\]

Currently, among the various attributes of the network, accuracy and latency are the two main types of predicted objects. Because they have extremely high acquisition costs, and are the primary manifestation of network performance and efficiency.

For latency prediction, due to the strong correlation between batch size, memory access, parameter quantity, and FLOPs with network latency, the encoding corresponding to these characteristics and the representation \(H^{K}\) are input into the predicting head together. To train the whole latency prediction model, the mean square error (MSE) function is adopted to measure the difference between predicted results and the ground truths.

For accuracy prediction, in addition to MSE loss function, architecture consistency loss (AC_loss) and sequence ranking related loss (SR_loss) proposed by NAR-Former [2] are also used. Following NAR-Former, we employed a hierarchical fusion strategy in accuracy prediction experiments. We

Figure 2: Overview of attribute prediction model.
use a simplified approach, which computes the weighted sum of the outputs of each transformer layer with adaptive weights.

## 4 Experiments

In this section, we conduct experiments on NNLQP [3], NAS-Bench-101 [1], and NAS-Bench-201 [21] to evaluate the performance of our NAR-Former V2. A series of ablation experiments were performed to corroborate the effectiveness of our design details. More details about implementation and analysis will be provided in the supplementary materials.

### Implementation details

Model detailsFor latency experiments, the number of GraphAttn-based Transformer blocks is set to 2, which is the same as the baseline [3]. As for accuracy predicting, we fix the number of Transformer blocks to 6 to align with the standard Transformer used in the baseline [2].

Training detailsAll experiments were trained using the Adam optimizer. We used a linear learning rate decay strategy with a warm-up, in which the learning rate uniformly increased to 0.001 during the first 10% of the training steps and then gradually decayed to 0. The batch size was fixed at 16. Our models are trained on a machine with a GeForce RTX 3090 GPU. To avoid randomness, each model was trained for 12 times and the two experiments with the best and worst indicators were discarded.

### Latency prediction

We conduct latency prediction on the recently released NNLQP dataset [3], which comprises 20000 complete deep learning networks and their corresponding latencies on the target hardware. This dataset has 10 different types of networks (referring to the first column of Tab. 1), with 2000 networks per type. Following NNLP [3], we use Mean Absolute Percentage Error (MAPE) and Error Bound Accuracy (Acc(\(\delta\))) to measure the deviations between latency predictions and ground truths. The lower the MAPE, the higher the prediction accuracy, while the opposite is true for Acc(\(\delta\)).

Here, we considered two different scenarios. In the first scenario, the training and testing sets are from the same distribution. We constructed the training set with the first 1800 samples from each of the ten network types, and the remaining 2000 networks were used as the testing set. The detailed results are shown in Tab. 1. When testing with all test samples, the average MAPE of our method is 0.4% lower than that of NNLP [3], and the average Acc(10%) is 1.16% higher than that of NNLP. When tested on various types of network data separately, except for the NASBench201 family, our method consistently outperforms NNLP. This indicates that our improved transformer, which utilizes the structural characteristics of the graph, has learned more reasonable representations than the original GNN.

The second scenario has more practical application significance, that is, the network type needed to be inferred is not seen during the training progress. There are ten sets of experiments in this part, with each set taking one type of network as the test set, while all samples from the other nine types of networks are used as the training set. As shown in Tab. 2, it can be seen that using only FLOPs and memory access information to predict latency is not enough. Suffering from the gap between the accumulation of kernel delays and the actual latency, kernel-based methods (TPU[7] and nn-Meter[6]) perform worse than the GNN-based model NNLP that directly encodes and predicts

\begin{table}
\begin{tabular}{l|c c|c c c} \hline \hline  & \multicolumn{3}{c}{MAPE\(\downarrow\)} & \multicolumn{2}{c}{Acc(10\%)\(\uparrow\)} \\ Test Model & NNLP [3] & Ours & NNLP [3] & Ours \\  & avg / best & avg / best & avg / best & avg / best \\ \hline All & 3.47\% / 3.44\% & 3.07\% / 3.00\% & 95.25\% / 95.50\% & 96.41\% / 96.30\% \\ \hline AlexNet & 6.37\% / 6.21\% & 6.18\% / 5.97\% & 81.75\% / 84.50\% & 81.90\% / 84.00\% \\ EfficientNet & 3.04\% / 2.82\% & 2.34\% / 2.22\% & 98.00\% / 97.00\% & 98.50\% / 100.0\% \\ GoogleNet & 4.18\% / 4.12\% & 3.63\% / 3.46\% & 93.70\% / 93.50\% & 95.95\% / 95.50\% \\ MnasNet & 2.60\% / 2.46\% & 1.80\% / 1.70\% & 97.70\% / 98.50\% & 99.70\% / 100.0\% \\ MobileNetV2 & 2.47\% / 2.37\% & 1.83\% / 1.72\% & 99.30\% / 99.50\% & 99.90\% / 100.0\% \\ MobileNetV3 & 3.50\% / 3.43\% & 3.12\% / 2.98\% & 95.35\% / 96.00\% & 96.75\% / 98.00\% \\ NasBench201 & 1.46\% / 1.31\% & 1.82\% / 1.18\% & 100.00\% / 100.0\% & 100.00\% / 100.0\% \\ SqueezeNet & 4.03\% / 3.97\% & 3.54\% / 3.34\% & 93.25\% / 93.00\% & 95.95\% / 96.50\% \\ VGG & 3.73\% / 3.63\% & 3.51\% / 3.29\% & 92.55\% / 96.50\% & 98.55\% / 96.00\% \\ ResNet & 3.34\% / 3.25\% & 3.11\% / 2.89\% & 98.40\% / 98.50\% & 98.55\% / 99.00\% \\ \hline \hline \end{tabular}
\end{table}
Table 1: Latency prediction on NNLQP [3]. Training and test sets have the same distribution.
the entire network. Benefiting from considering the entire input network and grafting GNN into the transformer, our method achieves the best MAPE and Acc(10%) on the average indicators of 10 experimental groups. Compared with the second-best method NNLP, the average Acc(10%) of our method has an marked increase of 8.08%.

### Accuracy prediction

#### 4.3.1 Experiments on NAS-Bench-101

NAS-Bench-101 [1] provides 423624 different cell architectures and the accuracies of the complete neural network constructed based on each cell on different datasets. Following [9], 0.1% and 1% of the whole data is used as training set and another 200 samples are used for validation. We use Kendall's Tau [36] to evaluate the correlation between the predicted sequence and the real sequence, and a higher value indicates a better results. The Kendall's Tau is calculated on the whole dataset or 100 testing samples. We report the average results of our predictor in 10 repeated experiments. **Results** are shown in Tab. 3. When only 424 samples were available for training, our method achieves the highest Kendall's Tau. We achieves 0.773 when tested using the whole testing set, which is 0.8% and 8.9% higher than the transformer-based model [22] and GNN-based model [22], respectively. This proves that the modifications we made to the transformer based on inspiration from GNN are effective.

#### 4.3.2 Experiments on NAS-Bench-201

NAS-Bench-201 [21] is another cell-based dataset, which contains 15625 cell-accuracy pairs. Following [9], 5% and 10% of the whole data is used as training set and another 200 samples are used for validation. We use Kendall's Tau [36] computed on the whole dataset as the evaluation metric in this part. Average results of our predictor of 10 runs are reported. **Results** are shown in Tab. 4. The conclusion of this experiment is similar to Sec. 4.3.1. When compared with the second-best method, a substantial improvement (2.5%) of Kendall's Tau can be seen in the setting of training with 781 samples.

\begin{table}
\begin{tabular}{l l c c} \hline \hline \multirow{2}{*}{Backbone} & \multirow{2}{*}{Model} & \multicolumn{2}{c}{Training Samples} \\  & & 0.1\% & 0.1\% & 1\% \\  & & (424) & (424) & (4236) \\ \hline \multicolumn{3}{c}{Test Samples} \\  & & 100 & all & all \\ \hline CNN & ReNAS [10] & 0.634 & 0.657 & 0.816 \\ \hline LSTM & NAO [28] & 0.704 & 0.666 & 0.775 \\  & NAO+SE & 0.732 & 0.680 & 0.787 \\ \hline \multirow{3}{*}{GNN} & NP [22] & 0.710 & 0.679 & 0.769 \\  & NP + SE & 0.713 & 0.684 & 0.773 \\ \cline{1-1}  & CTNAS [11] & 0.751 & - & \(\cdot\) \\ \hline \multirow{3}{*}{Transformer} & TNASP [9] & 0.752 & 0.705 & 0.820 \\ \cline{1-1}  & TNASP + SE & 0.754 & 0.722 & 0.820 \\ \cline{1-1}  & NAR-Former [2] & 0.801 & 0.765 & **0.871** \\ \cline{1-1}  & NAR-Former V2 & **0.802** & **0.773** & 0.861 \\ \hline \hline \end{tabular}
\end{table}
Table 3: Accuracy prediction on NAS-Bench-101 [1]. “SE” denotes the self-evolution strategy proposed by TNASP [9].

\begin{table}
\begin{tabular}{l l c c c c c c c} \hline \hline \multirow{2}{*}{Metric} & \multirow{2}{*}{Test Model} & \multirow{2}{*}{FLOPs} & FLOPs & m-Meter & TPU & BRP & NNLP [3] & Ours \\  & & & \multicolumn{1}{c}{AMCA [6]} & (7) & \multicolumn{1}{c}{JANs [20]} & (avg/ best) & \\ \hline \multirow{8}{*}{MAPE} & AlexNet & 44.65\% & 15.45\% & 7.20\% & 10.55\% & 31.68\% & 10.64\% / 9.71\% & 24.28\% / 18.29\% \\  & EfficientNet & 58.36\% & 53.96\% & 18.93\% & 16.74\% & 51.97\% & 21.46\% / 18.72\% & 13.20\% / 11.37\% \\  & GoogleNet & 30.76\% & 32.54\% & 11.71\% & 8.10\% & 25.48\% & 13.28\% / 10.90\% & 6.51\% / 6.15\% \\  & MnasNet & 40.31\% & 35.96\% & 10.69\% & 11.61\% & 17.26\% & 12.07\% / 10.86\% & 7.16\% / 6.93\% \\  & MobileNetVVV & 37.42\% & 35.27\% & 6.43\% & 12.68\% & 20.42\% & 8.87\% / 7.34\% & 6.73\% / 5.65\% \\  & MobileNetVVVV & 64.64\% & 57.13\% & 35.27\% & 9.97\% & 58.13\% & 14.57\% / 13.17\% & 9.06\% / 8.72\% \\  & NasBench201 & 80.41\% & 33.52\% & 9.57\% & 58.94\% & 13.28\% & 9.60\% / 8.19\% & 9.21\% / 7.89\% \\  & ResNet & 21.18\% & 18.91\% & 15.58\% & 20.05\% & 15.84\% & 7.54\% / 7.12\% & 6.80\% / 6.44\% \\  & SqueezeNet & 29.89\% & 39.19\% & 8.69\% & 24.60\% & 42.55\% & 9.84\% / 9.52\% & 7.08\% / 6.56\% \\  & VGG & 69.34\% & 66.63\% & 19.47\% & 38.73\% & 30.95\% & _F.06\% / 7.17\% & 15.40\% / 14.26\% \\ \hline \multirow{8}{*}{Acc(10\%)} & Average & 47.70\% & 37.26\% & 15.35\% & 21.70\% & 30.76\% & 11.35\% / 10.72\% & 10.35\% / 19.15\% \\ \cline{1-1}  & AlexNet & 6.59\% & 40.50\% & 75.44\% & 57.10\% & 15.20\% & 59.0\% & 6.40\% & 24.05\% / 28.06\% \\ \cline{1-1}  & EfficientNet & 0.05\% & 0.05\% & 23.40\% & 17.00\% & 10.16\% & 25.37\% / 28.80\% & 44.01\% / 50.20\% \\ \cline{1-1}  & GoogleNet & 12.75\% & 9.00\% & 47.40\% & 69.00\% & 12.55\% & 36.30\% / 48.75\% & 80.10\% / 83.35\% \\ \cline{1-1}  & MnasNet & 6.20\% & 9.80\% & 60.95\% & 44.65\% & 34.30\% & 55.89\% / 61.25\% & 73.46\% / 81.60\% \\ \cline{1-1}  & MobileNetVVV & 6.90\% & 8.05\% & 80.75\% & 33.99\% & 29.05\% & 63.03\% / 72.50\% & 78.46\% / 83.08\% \\ \cline{1-1}  & MobileNetVVV & 0.05\% & 0.05\% & 23.45\% & 46.25\% & 13.85\% & 42.36\% / 49.65\% & 68.43\% / 70.50\% \\ \cline{1-1}  & NasBench201 & 0.00\% & 10.55\% & 60.65\% & 2.50\% & 43.45\% & 60.70\% / 70.60\% & 63.13\% / 71.70\% \\ \cline{1-1}  & ResNet & 26.50\% & 29.80\% & 39.45\% & 27.30\% & 39.80\% & 72.88\% / 76.40\% & 77.24\% / 79.70\% \\ \cline{1-1}  & SqueezeNet & 16.10\% & 21.35\% & 36.20\% & 25.65\% & 11.85\% & 56.69\% & 60.40\% & 50.1\% / 9.25\% \\ \cline{1-1}  & VGG & 4.80\% & 2.10\% & 25.50\% & 2.60\% & 13.20\% & 71.04\% / 73.75\% & 45.21\% / 45.30\% \\ \cline{1
_Compared to NAR-Former, NAR-Former V2 achieves comparable accuracy prediction performance with fewer parameters. In latency prediction experiments, NNLP outperforms NAR-Former by a significant margin, and NAR-Former V2 exhibits a clear advantage over NNLP (a direct comparison experiment is provided in the supplementary material). In summary, by incorporating the strengths of GNN, the universal representation learning framework NAR-Former V2 is significantly enhanced. NAR-Former V2 addresses the shortcomings of NAR-Former, which was overly sensitive when handling complete network structures, while still retaining the outstanding performance of NAR-Former when handling cell-structured networks._

### Ablation studies

In this section, we conducted a series of ablation experiments on the NNLQP dataset to investigate the impact of various modifications. The results from Rows (2) and (3) in Table 5 indicate that for type encoding without numerical relationships, using one-hot vectors with equidistant properties across different categories is more suitable. Comparing Row (3) in Table 5 with Row (4), we observe that introducing GNN characteristics into the Transformer improves the model's ability to learn effective representations and achieve more accurate predictions compared to using the original GNN. When replacing the FFN with the GFFN module with eight groups (Row (5)), the number of model parameters reduces to approximately one eighth of that in Row (4), without a significant decrease in prediction accuracy. Compared to Row (5), Row (6) demonstrates an increase of 0.35% in ACC(10%) and 0.95% in ACC(5%). This confirms the role of the type-aware enhancement module in further refining and enhancing the rationality of the representations.

To verify our hypothesis regarding the generalization ability of the network and the effectiveness of the proposed graph-aided attention, we conducted comparative experiments in scenarios where the training and testing data have different distributions. The results of these experiments are presented in Table 6. In order to perform the experiment on global attention, we excluded the step of multiplying the adjacency matrix \(A\) in Equation 9, and instead replaced \(S^{l}\) with \(X^{l}X^{lt}/\sqrt{d}\). Results in Table 6 demonstrate that incorporating the adjacency matrix to restrict the scope of attention calculation is indeed beneficial for latency prediction on unseen data. The model utilizing graph-aided attention exhibited a significant improvement of 7.68% in ACC(10%) compared to the model using global attention.

## 5 Conclusion

In this paper, we combine the strengths of Transformer and GNN to develop a universal neural network representation learning model. This model is capable of effectively processing models of varying scales, ranging from several layers to hundreds of layers. Our proposed model addresses the limitations of previous Transformer-based methods, which exhibited excessive sensitivity when dealing with complete network structures. However, it still maintains exceptional performance when handling cell-structured networks. In future work, we will focus on optimizing the design of the representation learning framework and applying it to a broader range of practical applications. Such as using the proposed model to search for the best mixed precision model inference strategies.

\begin{table}
\begin{tabular}{l c c} \hline \hline Attention & MAPE\(\downarrow\) & ACC(10\%)\(\uparrow\) \\ \hline Global & 16.88\% & 36.32\% \\ Local & 13.20\% & 44.01\% \\ \hline \hline \end{tabular}
\end{table}
Table 6: The influence of using different attentions. Test on EfficientNet.

\begin{table}
\begin{tabular}{c c c c c c c c c} \hline \hline Row & Structure & Op & Op & Graph- & GFFN & TA- & MAPE\(\downarrow\) & Acc(10\%)\(\uparrow\) & Acc(5\%)\(\uparrow\) \\  & Type & Attributes & Attn & & Enhance & & & \\ \hline
1(Baseline) & GNN & One-hot & Real Num & - & - & - & 3.48 & 95.26 & 77.80 \\
2 & GNN & PE & PE & - & - & - & 3.43(-0.05) & 95.11(-0.15) & 79.58(-4.78) \\
3 & GNN & One-hot & PE & - & - & - & 3.33(-0.15) & 95.57(+0.31) & 80.19(+2.39) \\ \hline
4 & Transformer & One-hot & PE & ✓ & - & - & 3.20(-0.28) & 96.00(+0.74) & 81.86(+0.40) \\
5 & Transformer & One-hot & PE & ✓ & ✓ & - & 3.20(-0.28) & 96.06(+0.80) & 81.76(+3.96) \\
6 & Transformer & One-hot & PE & ✓ & ✓ & ✓ & 3.07(0.41) & 96.41(+1.15) & 82.71(+4.91) \\ \hline \hline \end{tabular}
\end{table}
Table 5: Ablation studies on NNLQP [3]. “PE” denotes position encoding.