{'fraction_non_alphanumeric': 0.03712077751155806, 'fraction_numerical': 0.017952957851044445, 'mean_word_length': 5.1506849315068495, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 1, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
FocusFormer: Focusing on What We Need via Architecture Sampler

Vision Transformers (ViTs) have underpinned the recent breakthroughs in computer vision. However, designing the architectures of ViTs is laborious and heavily relies on expert knowledge. To automate the design process and incorporate deployment flexibility, one-shot neural architecture search decouples the supernet training and architecture specialization for diverse deployment scenarios. To cope with an enormous number of sub-networks in the supernet, existing methods treat all architectures equally important and randomly sample some of them in each update step during training. During architecture search, these methods focus on finding architectures on the Pareto frontier of performance and resource consumption, which forms a gap between training and deployment. In this paper, we devise a simple yet effective method, called FocusFormer, to bridge such a gap. To this end, we propose to learn an architecture sampler to assign higher sampling probabilities to those architectures on the Pareto frontier under different resource constraints during supernet training, making them sufficiently optimized and hence improving their performance. During specialization, we can directly use the well-trained architecture sampler to obtain accurate architectures satisfying the given resource constraint, which significantly improves the search efficiency. Extensive experiments on CIFAR-100 and ImageNet show that our FocusFormer is able to improve the performance of the searched architectures while significantly reducing the search cost. For example, on ImageNet, our FocusFormer-Ti with 1.4G FLOPs outperforms AutoFormer-Ti by 0.5% in terms of the Top-1 accuracy.


Introduction
With powerful computing resources and large amounts of labeled data, we have witnessed the tremendous successful applications of ViTs in computer vision [54,14,37]. To push the state-of-theart performance boundary, several studies [58,37,10,55] have been proposed to craft the architectures of ViTs and achieved competitive performance in many visual tasks, such as image classification [14,54,69] and dense prediction [6,73,9]. However, manually designing ViT architectures is laborious and human expertise is often sub-optimal in exploring the huge design space.
To automatically design ViT architectures, much effort has been devoted to neural architecture search (NAS) [43,36,5,50] by finding optimal architectures in a predefined search space. To obtain compact architectures, some works [36,60,72] propose to search architectures for a specific resource constraint or hardware requirement. Nevertheless, when it comes to diverse platforms under various resource constraints (e.g., FLOPs, latency, on-chip memory), these methods need to design architecture for each scenario from scratch and the search cost grows linearly with the number of possible cases, which is extremely computationally expensive. To tackle the above issue, one-shot NAS methods [23,16,65,18,7] have been proposed to decouple the model training and the Figure 1: An overview of the uniform distribution of each architectural dimension and the gap between training and deployment in one-shot NAS methods [65,7]. During supernet training, existing methods randomly sample sub-networks (rectangle) from a uniform architectural distribution in one update step. During deployment, these methods focus on the sub-networks (circle) on the Pareto frontier of performance and resource consumption. The other architectures that are not on the Pareto frontier are neglected. architecture search stage, where the first stage trains an over-parameterized supernet that covers a considerable number of sub-networks (e.g., 10 20 ) with diverse architectural configurations and the second stage selects an optimal sub-network given the target resource constraint during deployment.
Nevertheless, there exists a gap between training and deployment. Specifically, to deal with the tremendous number of sub-networks in supernet, existing methods [23,4,57,65] randomly sample some of them from a uniform architectural distribution (See Figure 1a) in each update step during training, where all architectures in the search space (rectangles in Figure 1b) have an equal probability of being chosen. During deployment, these methods focus on those architectures (circles in Figure 1b) that are on the Pareto frontier of performance and resource consumption. The other architectures (rectangles not on the Pareto frontier) that have poor performance or do not meet the resource constraint are neglected. In this case, the training stage does not concentrate on the architectures on the Pareto frontier. As a result, the architectures with good performance may not be sufficiently trained and hence the performance of the searched architectures is sub-optimal. Moreover, after supernet training, existing methods have to either use evolutionary search [23,4,65,7] or train a controller [64,48] to search for optimal architectures given a deployment scenario, which requires extensive architecture evaluations and hence incurs high computational cost.
In this paper, we devise a simple yet effective method, called FocusFormer, to bridge the gap between training and deployment. Unlike existing NAS methods for ViTs [65,7], we assume and highlight that not all sub-networks in the supernet are equally important. Based on this intuition, we devise a parameterized architecture sampler to learn the architectural distribution under diverse resource constraints, where we consider four important dimensions of ViTs including depth, embedding dimension, multi-layer perceptron (MLP) ratio, and head number. Instead of sampling from a uniform architectural distribution, we optimize the architecture sampler to generate sampling distributions conditioned on different resource constraints to assign higher probabilities to accurate sub-networks in an update step. In this way, we are able to train sub-networks that are likely to be on the Pareto frontier with more training budgets, thereby improving their performance. Once the supernet and architecture sampler have been well-trained, we can directly use the architecture sampler to instantly generate candidate ViT architectures with good performance given the target resource constraint in the architecture search stage. Therefore, our FocusFormer does not need to involve extensive architecture evaluations such as in evolutionary methods [65,46] and hence greatly improve the search efficiency. Extensive experiments on CIFAR-100 and ImageNet show that our proposed method improves the performance of the searched architectures while significantly reducing the search cost.
Our main contributions are summarized as follows: • We propose FocusFormer to bridge the gap between training and deployment in one-shot NAS for ViTs. To our knowledge, this problem has not been well studied.
• We design a parameterized architecture sampler to serve as the bridge. During supernet training, the architecture sampler is jointly optimized with network parameters to identify and assign higher sampling probabilities for architectures on the Pareto frontier under different resource constraints, that align with the searched networks at the specialization stage. In this way, we allocate more training resources to these salient architectures and thus improve their performance. During deployment, we can instantly find the target candidate architectures for a given resource constraint using the well-trained architecture sampler, with very high search efficiency.
• We evaluate our proposed method on CIFAR-100 and ImageNet. Extensive experiments demonstrate that our proposed method is able to improve the performance of the specialized network while greatly reducing the search cost. For example, on ImageNet, our FocusFormer-Ti surpasses AutoFormer-Ti by 0.5% on the Top-1 accuracy.

Related Work
Vision Transformers. Recently, ViTs [14,6,37,73] have shown great representational power in computer vision. Specifically, a ViT [14] contains a patch embedding layer, multiple Transformer blocks, and a task-dependent head, where each Transformer block consists of a multi-head selfattention (MSA) block and an MLP block. LayerNorm (LN) [1] and residual connection [27] are applied before and after each block, respectively. To improve the performance of ViTs, several methods are proposed, including but not limited to incorporating hierarchical representations [17,42,37,58], introducing inductive bias by inserting convolutional layers [61,35,66,20,15,8,19,26] and improving positional encoding [11,62], etc. Though achieving promising performance, the ViT architectures of these methods are manually designed, which may not fully explore the architecture space. Compared with these methods, our proposed FocusFormer focuses on automatically designing the ViT architecture.
Neural architecture search. Neural architecture search (NAS) [75,43,36] seeks to automatically find architectures with promising performance while satisfying the resource constraint B, which can be formulated as min where W α is the weights of the network with architecture α, C(α) is the computational cost of α, and L train , L val are the training loss and validation loss, respectively. Here, D train and D val are the training and validation dataset, respectively. To solve Problem (1), we have to train weights W α for each architecture α until convergence to obtain W * α , which takes unbearable computational cost. Most existing approaches either use random search [34,2], reinforcement learning [75,22,43,52], evolutionary search [41,40,46,47] or gradient-based optimization [36,5,60,25] to find optimal architectures.
In addition, one-shot NAS [4,23,16,65,7,18] has been proposed to decouple the model training stage and architecture search stage. In the model training stage, the goal is to train an over-parameterized supernet that supports many sub-networks of different configurations with the shared parameters to accommodate different deployment scenarios. Let W be the weights of the supernet. The objective function for the supernet training can be formulated as where S(W, α) is a selection function that chooses a part of parameters from W to constitute the sub-network with architecture configuration α and A is the architecture search space. To solve Problem (2), existing methods [65,7] assume that all the sub-networks are equally important and use a uniform sampling strategy to approximate the expectation term. Considering different resource constraints, Problem (2) can be rewritten as where B is a resource constraint sampled from a prior distribution B and U (·|B) is a uniform distribution over architectures conditioned on B. For the architecture search stage, the goal is to obtain the specialized sub-network that satisfies the resource constraint B given a deployment  Figure 2: An illustration of our proposed architecture sampler. We formulate the architecture search problem as a sequential prediction problem where each element of the sequence denotes an architectural dimension of a ViT. Starting from a prior resource distribution B, we first sample a resource constraint B ∼ B. We then feed B into an LSTM network with four fully connected layers and output the architectural distribution π(B; θ). Last, we obtain the candidate architecture by performing sampling from the architectural distribution α ∼ π(B; θ).
scenario while maximizing the validation accuracy Acc val (S(W * , α)), which can be formulated as Here, W * is the well-learned weight of the supernet in the model training stage. Except for the validation accuracy, one can also use the negative validation loss to measure the performance of the searched architectures [36,5]. To solve Problem (4), existing methods either use evolutionary search [23,4,68,57,65,7] or train a controller to search for optimal architectures [64,48,30]. Since the supernet has been well-trained, we are able to obtain any sub-network without any fine-tuning or retraining during the architecture search, which yields a much lower search cost compared with Problem (1). Compared with AutoFormer [65] and S3 [7], our proposed FocusFormer bridges the gap between training and deployment, which greatly improves the performance of the specialized sub-networks and search efficiency. Compared with GreedyNAS [67,31] and AttentiveNAS [56], our FocusFormer differs in several aspects. First, our method focuses on searching ViT architectures while GreedyNAS and AttentiveNAS concentrate on searching the architectures of convolutional neural networks (CNNs). Second, they need to sample and rank multiple architectures for each iteration during supernet training, which introduces extensive computational overhead. In contrast, our FocusFormer trains an architecture sampler to obtain sub-networks in an efficient way. Third, they need to use evolutionary search to find architectures, which is computationally expensive. Our FocusFormer uses the well-trained architecture sampler to instantly generate ViT candidate architectures, which is extremely efficient.

FocusFormer
In one-shot NAS, the objectives for the model training stage in Eq. (3) and the architecture search stage in Eq. (4) are different, resulting in a gap between training and deployment. Specifically, existing methods [4,68] assume that all sub-networks are equally important and perform a uniform sampling strategy during supernet training. However, during deployment, we only care about architectures on the Pareto frontier of performance under various resource constraints. In this case, the model training stage does not tailor for those specialized architectures on the best Pareto front. As a result, the architectures with good performance may not be sufficiently trained and hence lead to the sub-optimal performance. Moreover, once the supernet has been well-trained, we have to use either evolutionary search [23,4] or train a controller [64,48] to search for optimal architectures due to the different objectives between the model training and architecture search stages, which introduces additional training cost.
In this paper, we jointly learn an architecture sampler with network parameters to assign higher probabilities to architectures that are likely on the Pareto frontier under different resource constraints Algorithm 1 Training algorithm for FocusFormer.

5:
end if 6: for each batch of data in Dtrain do 7: Sample a resource constraint B ∼ B.

8:
Sample an architecture α ∼ π(B; θ). 9: Train supernet N with α. 10: end for 11: end for during supernet training. In this way, we put more training resources on these important ViT architectures and hence mitigate the gap between training and deployment. Once the supernet and architecture sampler have been well-trained, we can directly use our architecture sampler to select good candidate architectures given a specific resource constraint, which is extremely efficient.

Training Supernet with Architecture Sampler
Given a ViT supernet N with parameter W , we design an architecture sampler π(·; θ) parameterized by θ to obtain architectures with potentially good performance given arbitrary B. With the architecture sampler, we formulate the objective for the supernet training as Here, if we replace π(B; θ) with the uniform distribution over architectures U (·|B) conditioned on B, Problem (5) is reduced to the original one-shot NAS problem in Eq. (3). Equipped with π(·; θ), those ViT architectures that are potentially on the Pareto frontier are trained with more computing resources. In this way, the gap between training and deployment can be mitigated and hence improve the performance of the searched architectures under different resource constraints. We summarize our training pipeline in Algorithm 1. Note that training the architecture sampler for each epoch is computationally expensive and not necessary. To reduce the computational cost, we only update our architecture sampler per τ epoch.

Learning Architecture Sampler
To determine the ViT architecture, we formulate the NAS problem as a sequence prediction problem where each element of a sequence denotes an architectural dimension (e.g., the depth of ViT) following [43,75]. To predict the architectural distribution, our architecture sampler π(·; θ) is an LSTM network with four fully connected layers, which takes a resource constraint B as input and then outputs four architectural dimensions of a ViT, including depth, embedding dimension, head number and MLP ratio, as shown in Figure 2. We then perform sampling from the architectural distribution to obtain architectures, i.e., α ∼ π(B; θ). To represent different B, we build learnable embedding vector for each B following [43,21].
To train π(·; θ), we formulate the objective function as where R(W, α) is a performance metric of the sampled architecture α. To guide the training of π(·; θ), we hope the resource consumption of α not only close to the given resource constraint B, but also achieves high accuracy following [3]. Considering both the resource constraint and accuracy, we have Algorithm 2 Training algorithm for architecture sampler.

3:
Sample a resource constraint B ∼ B.

5:
Compute the reward R(W, α) using Eq. (7). 6: Update θ using Eq. (8). 7: end for where Acc val (S(W, α))) is the validation accuracy of α with corresponding weights S(W, α) and β is a hyper-parameter that makes a balance between accuracy and resource consumption. In order to reduce the training cost of π(B; θ), we only compute the validation accuracy on a mini-batch of data rather than the whole validation data set. In general, Eq. (7) is not differentiable w.r.t. θ. Following [75,43], we use reinforcement learning with policy gradient [59] to solve Problem (6). Specifically, we update θ by ascending the policy gradient, which can be formulated as: where η is the learning rate of π(·; θ). We summarized the training details of our proposed architecture sampler in Algorithm 2.
Note that our architecture sampler introduces some additional costs during supernet training. To reduce the training cost, we train the supernet using a progressive learning strategy following [53,33]. Specifically, we gradually increase the image size from a small S 0 × S 0 to the final S e × S e with a fixed patch size p. As a result, the number of patches is increased from S 2 0 /p 2 to S 2 e /p 2 . However, in ViT, the size of positional encodings is related to the number of patches. To overcome this limitation, we proposed to use conditional positional encodings following [11].
Resource constraint distribution. In Eqs. (5) and (6), we introduce a prior distribution B. For simplicity, we assume that B is a uniform distribution, which indicates that all resource constraints have an equal probability of being sampled. Note that there are a large number of B in B and we can not enumerate them all during training. To overcome this limitation, we perform quantization to obtain discrete resource constraint as B = round(B/s) * s, where round(x) returns the nearest integer of a given value x and s is a step size of the round function. To represent a resource constraint with an arbitrary value, we use the linear interpolation method following [21,44].

Efficient Architecture Search
Once we have well trained the supernet and the proposed architecture sampler, we now focus on obtaining architectures that satisfy the given resource constraint while achieving promising performance. Using the well-learned π(·; θ), we are able to directly obtain good candidate architectures. Specifically, given a resource constraint B, we first sample multiple architectures from the predicted architectural distribution π(B; θ). We will repeat the sampling process if the sampled ViT architecture does not satisfy the resource constraint, i.e., C(α) > B. Last, we select the ViT architecture with the highest validation accuracy as the searched architecture. Since only a limited number of architecture evaluations are involved, the search cost of our method is much smaller than the existing one-shot NAS methods [33,7].

Experiments
Datasets. We evaluate the proposed method on two image classification datasets, namely, CIFAR-100 [32] and ImageNet [13]. CIFAR-100 contains 100 classes, where each class has 500 training images and 100 testing images. ImageNet is a large-scale dataset that contains 1.28M training images and 50k validation images with 1k classes. To construct the validation set, we randomly sample 10k and 100k training samples from CIFAR-10 and ImageNet following [36,65].
Search space. Following [65], we apply our proposed method to ViT search space with three different settings, namely, supernet-tiny, supernet-small and supernet-base. Each search space contains four Table 1: Performance comparisons of different methods on ImageNet. * denotes that we obtain the results from [54]. * * indicates that we change the model to use conditional positional encodings [11] for fair comparisons. main dimensions to build transformer blocks: embedding dimension, the number of heads, MLP ratio and network depth. The detailed settings of each search space can be found in AutoFormer [65]. For convenience, we use "FocusFormer-Ti", "FocusFormer-S" and "FocusFormer-B" to represent the architectures obtained by FocusFormer in the tiny, small and base search space, respectively.
Evaluation metrics. We measure the performance of different methods using the Top-1 and Top-5 accuracy. Following [49,29], we use the floating-point operations (FLOPs) and the number of parameters (#Params) to measure the resource consumption and model size, respectively. Following [36], we also measure the training and search costs using the training and search durations on a single NVIDIA GeForce RTX 3090 GPU, respectively.
Implementation details. We train our model using a server with 8 V100 GPUs. Following [65], we train the supernet with the weight entanglement strategy. The supernet is trained for 500 epochs with a mini-batch size of 128 and 1024 on CIFAR-100 and ImageNet, respectively. We scale the learning rate according to the batch size with formula: lr scaled = 5×10 −4

512
× batchsize and use cosine rule [38] to decrease the learning rate. We use AdamW [39] for optimization. Following DeiT [54], we use RandAugment [12], Cutmix [70], Mixup [71] and random erasing [74] except the repeated augmentation [28] for data augmentation. For the progressive learning strategy, we set S e to 224. S 0 is set to 128 for experiments on CIFAR-100 and 160 for experiments on ImageNet. The patch size p is set to 16 following [14]. For the architecture sampler, we use the same batch size and optimization method as the supernet. We initialize the learning rate to 2.5 × 10 −4 and 1 × 10 −3 for the experiments on CIFAR-100 and ImageNet, respectively. We set the dimension of the hidden state for the LSTM network to 64. The number of training iterations K for π(·; θ) and the hyper-parameter β in Eq. (7) are set to 100 and 0.07, respectively. We set τ to 30 and 40 for the experiments on CIFAR-100 and ImageNet, respectively. For the architecture search stage, we randomly sample 30 architectures and select the architecture with the highest validation accuracy. All the searched architectures directly inherit weights from the learned supernet without any fine-tuning.

Main Results
We apply our FocusFormer to search ViT architectures under different FLOPs on ImageNet. We show the results in Table 1. For fair comparisons, we modify AutoFormer to use conditional positional encodings [11]. From the results, our searched ViTs perform better than the CNN counterparts, such as ResNet [27], RegNetY [45] and ResNeXt [63], which shows the strong representational power of ViTs. We can also observe that our proposed FocusFormer achieves much higher performance  Table 2: Effect of the architecture sampler. "AS in Train" denotes that we use architecture sampler rather than uniform sampling [65] to obtain sub-networks during supernet training. "AS in Search" indicates that we use architecture sampler instead of evolutionary search [65] to obtain candidate architectures during search. We report the results of FocusFormer-Ti obtained by different methods on CIFAR-100. " † " denotes that we include the training cost of the architecture sampler.   75.1% in terms of the Top-1 accuracy, which is 0.5% higher than AutoFormer-Ti.

Further Studies
Effect of the architecture sampler. To investigate the effect of our architecture sampler ("AS"), we apply "AS" to different stages. Specifically, applying the "AS" in the supernet training stage denotes that we sample sub-networks from the learned distribution using "AS" instead of a uniform distribution [65] while applying the "AS" in the search stage represents that we obtain candidate architectures using "AS" rather than the evolutionary methods [65]. The results are provided in Table 2. To show the supernet performance under various resource constraints, we also randomly sample 1k architectures from the supernet and show the results in Figures 3 and 4.
From the results, using the "AS" in the supernet training stage introduces a little training cost but consistently improves the performance under various resource constraints (See Figures 3 and 4). The performance improvement is more significant in high FLOPs scenarios. For example, with about 1.7G FLOPs, our method with "AS" in the supernet training stage outperforms those without "AS" by 0.5% in terms of the Top-1 accuracy on CIFAR-100. Moreover, using our "AS" in the search stage achieves comparable or even better performance than the evolutionary search while significantly reducing the search cost (0.3 vs. 1.7 GPU Hours). Using "AS" in both the training stage and search stage, our FocusFormer yields the best performance with 81.0% Top-1 accuracy and 1.7G FLOPs while the search cost is less than 0.1 GPU Hour. These results show that our "AS" is able to improve the performance of the searched architectures while significantly reducing the search cost.   Table 4. From the results, with the decrease of τ , the performance of the searched architecture becomes better first and then goes worse. For example, the searched architecture obtained by τ = 30 with 0.1G lower FLOPs surpasses that of τ = 40 by 0.3% in terms of the Top-1 accuracy.
One possible reason is that we need to update the architecture sampler regularly to a certain extent to accommodate the performance change during supernet training. However, too small τ indicates the frequent changes of the learned architectural distribution. As a result, the potentially good architectures may not be sufficiently trained. Moreover, as our FocusFormer achieves the best performance when τ is set to 20, we use it by default in our experiments.
Effect of different step sizes s. To investigate the effect of different step sizes s in resource constraint discretization, we train FocusFormer-Ti with different s ∈ {50, 100, 200, 300, 400, 500}.
Here, smaller s denotes more resource constraint candidates. From Table 5, the performance of searched architecture becomes better and then worsens with the decrease of s. On one hand, larger s results in a small number of resource constraint candidates, which can not accurately represent an arbitrary resource constraint due to coarse-grained linear interpolation. For example, our method yields the worst performance with s = 500.
On the other hand, smaller s leads to a large number of resource constraint candidates, which introduces too many learnable embedding vectors and hence incurs the optimization difficulty. With s = 200, the searched architectures achieve the best performance (81.0% in terms of the Top-1 accuracy).

Conclusion and Future Work
In this paper, we have proposed FocusFormer to mitigate the gap between training and deployment in one-shot neural architecture search. To this end, we have devised an architecture sampler to obtain ViT architectures that are likely on the Pareto frontier under diverse resource constraints. By jointly training the architecture sampler with network parameters, we put more training resources on these salient architectures and hence improve the performance of the searched ViT architectures. Once the supernet and architecture sampler have been well-trained, we can directly use our proposed architecture sampler to obtain architectures with promising performance while satisfying the given resource constraint. Experiments on CIFAR-100 and ImageNet have demonstrated that our proposed method is able to improve the performance of the searched architectures while significantly reducing the search cost. In the future, we may extend our method in several aspects. First, we may consider improving both the training and search efficiency by using some sparsity training strategies. Second, we may jointly perform neural architecture search and quantization simultaneously to obtain more compact ViTs with comparable performance.

Supplementary Material for FocusFormer: Focusing on What We Need via Architecture Sampler A Visualization of the searched architectures
In this section, we show the ViT architectures obtained by our FocusFormer in Figure A. The searched architectures tend to select large Q-K-V dimensions, MLP ratios, head numbers, and depth numbers with the increase of FLOPs and #Params. For FocusFormer-S and FocusFormer-B, we observe that the intermediate Transformer blocks prefer large head numbers, while the deep blocks tend to choose small head numbers.