LayerShuffle: Enhancing Robustness in Vision Transformers by Randomizing Layer Execution Order

## Abstract

Abstract Due to their architecture and how they are trained, artificial neural networks are typically not robust toward pruning, replacing, or shuffling layers at test time. However, such properties would be desirable for different applications, such as distributed neural network architectures where the order of execution cannot be guaranteed or parts of the network can fail during inference. In this work, we address these issues through a number of proposed training approaches for vision transformers whose most important component is randomizing the execution order of attention modules at training time. We show that with our proposed approaches, vision transformers are indeed capable to adapt to arbitrary layer execution orders at test time assuming one tolerates a reduction (about 20%) in accuracy at the same model size. We also find that our trained models can be randomly merged with each other resulting in functional (”Frankenstein”) models without loss of performance compared to the source models. Finally, we layer-prune our models at test time and find that their performance declines gracefully.

## 1 Introduction

While demonstrating impressive performance in many domains (e.g. see Krizhevsky et al. (); Vaswani et al. (); Radford et al. (); Rombach et al. ()), deep learning systems demand both extensive computational resources and tight integration of their parts. For applications at scale, they therefore increasingly require the construction of large datacenters with thousands of dedicated hardware accelerators. A paradigm shift from central to decentral model inference, where loosely coupled neural networks are distributed over a number of edge devices that share the computational load (Gacoin et al., ) of the model therefore seems ultimately desirable.
Unfortunately, current deep learning models lack the robustness necessary for such a paradigm shift.

In general, artificial neural networks (ANNs) (Schmidhuber, ) are not robust toward pruning or replacing network layers during deployment. Similarly, changing the order of execution in-between layers without further training usually results in catastrophic losses in accuracy. Nevertheless, these properties would be desirable e.g. in distributed setups as described above, where a model is executed on a number of shared nodes in a network.
In such a setting, overloaded or malfunctioning nodes could simply be skipped in favor of other available nodes. Furthermore, malfunctioning nodes or absent nodes could simply be replaced by a similar (not the same) node, allowing for simple logistics when deploying models in practice.

Figure: (a) LayerShuffle
Refer to caption: x1.png

Augmenting models with these properties has historically been challenging. Due to the structure of the most common types of ANNs and how they are trained through backpropagation (Linnainmaa, ; Werbos, ; Rumelhart et al., ), each neuron can only function by adapting to both its connected input and output neurons as well as the overall desired output of the network at training time.
Furthermore, the hierarchical organization of explanatory factors is usually considered a necessary prior in deep learning, i.e. one assumes that subsequent layers extract increasingly high-level features (Bengio et al., ). Therefore, switching the execution orders of layers implies that layers would need to adapt and extract either low-level or high-level features depending on their position in the network. Unfortunately, network layers adapting in such a way to a changed order of execution appears to be infeasible for most known network architectures. The above prior is therefore violated and the overall performance of the network suffers beyond the point where the network successfully executes the task it has been trained for.

The more recently discovered transformer (Vaswani et al., ) architecture has been shown to be more flexible. Transformers, when trained accordingly, can be layer-pruned at test-time (Fan et al., ), and recent work merges similar transformer-based language models (Akiba et al., ), all with only moderate reduction or even an improvement in performance. We hypothesize that the reason for the high adaptability of transformers can be found in self-attention modules being able to adapt their output based on the received input and that it should be possible to train a transformer network to not only adapt to the variation of its input features based on the overall network input but also the variations caused by receiving input from different layers during test time.

We propose and evaluate three training approaches for vision transformers to address the robustness issues laid out above. The most important component common to all approaches is randomizing the execution order of the vision transformer’s stacked self-attention-and-feed-forward modules at training time (Figure 1(a)). More precisely, we have the following main contribution in this paper:

- •
We find that using our training approaches, the layers of a vision transformer are indeed capable of adapting to an arbitrary execution order *at test time*, assuming one tolerates a moderate reduction in performance. Providing each layer additionally with its current position in the network improves performance only slightly compared to a model without it, suggesting that each attention layer is already capable of determining its role based on the incoming data alone.
- •
We perform a UMAP analysis that reveals that layers of models trained with our approach adjust their output depending on which position they hold in the network.
- •
We find that trained models can be layer-pruned at test time similar to the models trained with the techniques proposed in Fan et al. (), where their performance declines gracefully, i.e. models with reduced amounts of layers still remain functional.
- •
In addition, vision transformers, which have been made robust to execution order, can be merged with each other resulting in merged (”Frankenstein”) models without loss of performance compared to the source models.

## 2 Related work

Zhu et al. () find that for particular subsets of inputs, transformers perform better when changing the execution order of layers to an input-specific sequence. They optimize the execution order per sample in order to maximize the performance of the model for natural language processing tasks. While the goal in their work is to find a layer sequence of a pre-trained model that is optimal for a given input, our approach aims to make the model robust to any sequence of execution, where layers might even be missing.

In parallel to our work on vision transformers and within the wider framework of mechanistic interpretability for large language models (LLMs), Lad et al. () found that LLMs are very robust to changing the positions of adjacent layers or ablating single layers from the model. The main difference to our work is that the authors do not perform any refinement on the models and switch and ablate layers locally with the aim of better understanding the inner workings of LLMs. On the other hand, we focus on methods and training approaches to increase this innate robustness of the transformer architecture to a point where models at test time function regardless of their layer execution order, and respond gracefully to the ablation of several layers in any stage of the network.

Another related work is LayerDrop (Fan et al., ), where the authors focus on robust scalability for models on edge devices. They propose dropping whole transformer layers during training and show that this training approach allows models to still deliver acceptable (if somewhat reduced) performance upon pruning layers at test time (e.g. for balancing computational load). The main difference to our approach is that we randomly change the execution order during training, and, contrary to LayerDrop, do not remove any layers. Also, LayerDrop focuses on entirely on load balancing in compute-limited production systems while our main focus is on arbitrary execution order and the possibility to replace defective nodes by others on top of these issues in case of overloaded or malfunctioning nodes in distributed systems.

Recent work improves the performance of LLMs on predefined tasks, by merging them using evolutionary strategies (Akiba et al., ). Similar to Zhu et al. (), the authors’ overall aim is to increase performance rather than robustness in distributed environments, so contrary to our approach, layer execution order and scaling for reduced numbers of layers are in general not considered.

Work on introducing permutation invariance into neural networks has been conducted by Lee et al. (), Tang & Ha () as well as Pedersen & Risi (). The corresponding former two approaches exploit the permutation equivariance of attention, i.e. the fact that the order in which a sequence of vectors gets presented to the attention module does not change its result, but merely shuffles the sequence of output vectors. This equivariance is achieved by using a fixed-seed query vector in order to obtain an permutation invariant latent code. This latent code stays the same no matter in which order input tokens/patches are presented to the module. The main contrast to our work here is that we exploit permutation invariance in the order of layer executions rather than the order of tokens and patch embeddings and can therefore not make use of permutation equivariance of the attention operation, as it does not apply to switching inputs and outputs. Pedersen & Risi () also consider permutation invariance with respect to the input, although the emphasis of their approach lies on a number of smaller recurrent neural networks Werbos (); Hochreiter et al. () with shared weights, which extract a permutation-invariant representation of the input vector in a self-organized way without utilizing the permutation-equivariance of self-attention modules.

Finally, the work of Gacoin et al. (), not unlike our own, is motivated by the observation that a paradigm of distributed model inference over a number of loosely coupled compute nodes, edge devices or swarm agents promises a positive impact on the ecological and economical footprint of deep learning solutions. The authors propose a graph-theory-based framework to optimize the distribution of model parts to individual devices and optimize the overall energy consumption of the network. While our work sets out from the same motivation, it complements the approach of Gacoin et al. () as the the authors do not address robustness to adverse conditions in such distributed setups while it is the entire focus of this paper. The exact distribution of our models on the other hand, is beyond the scope of our work but combining our models with the approaches in (Gacoin et al., ) seems a promising direction of future research.

## 3 Methods

After giving a brief overview on vision transformers (Section 3.1), we investigate three approaches to them for arbitrary layer execution order: First, we simply permute the order of layers randomly during training, such that every training batch is presented to the network’s layers in a different random order (Section 3.2). Second, while randomly permuting the layer order as in the previous approach, we use an layer-depth encoding inspired by learned word embedding approaches (Section 3.3) to see if this additional information would further improve performance. Third, while randomly permuting layer order as in the previous approaches, we try to predict from the output of every layer at which position the layer is currently located in the network using a small layer position prediction network for every layer (Section 3.4). We now describe these approaches in more detail.

### 3.1 Vision Transformers

Dosovitskiy et al. () have successfully adapted the transformer architecture to computer vision by introducing a preprocessing step that converts images to suitable sequences.
They do so by splitting an image $\mathbf{x}\in\mathbb{R}^{H\times W\times C}$ into a sequence of N flattened patches
$\mathbf{x_{p}}\in\mathbb{R}^{N\times(P^{2}C)}$, and then pass each patch through a linear embedding layer
$\mathbf{E}\in\mathbb{R}^{(P^{2}C)\times D}.$
$H$,$W$ and $C$ are here the height, width and number of channels of the image respectively and $P$ is the patch size. $D$ is the internal latent dimension of the transformer which remains constant throughout the network and can be set as a hyperparameter.

After converting the image into a sequence that can be processed by a transformer encoder, inspired by BERT (Devlin et al., ), the authors prepend a class token to $\mathbf{x_{p}}$ in which the class information of the input image can be aggregated by the transformer. To encode position information into the embedding, a positional embedding tensor
$\mathbf{E}_{\text{pos}}\in\mathbb{R}^{(N+1)\times D}$ is added. Both the class token as well as the positional embeddings are learnable embeddings, which are trained jointly with the rest of the network. The resulting input sequence presented to the transformer network can be expressed as

$$ $\mathbf{z}_{0}=[\mathbf{x}_{\text{class}};\thinspace\mathbf{x}_{p}^{1}\mathbf{ E};\thinspace\mathbf{x}_{p}^{2}\mathbf{E};\thinspace\dots;\thinspace\mathbf{x} _{p}^{N}\mathbf{E}]+\mathbf{E}_{\text{pos}}.$ $$

This sequence is presented to a standard transformer architecture of stacked attention modules. Each attention module consists of a multi-head self-attention (MSA) layer and a feedforward layer or multilayer perceptron (MLP) layer. MSA layers utilize self-attention (SA) (Vaswani et al., ), a powerful concept that allows transformers to relate and combine its feature embeddings with each other. Self-attention extracts features from the input sequence $\mathbf{z}$, which in turn preforms a transformation of the input vector sequence.

Specifically, self-attention extracts query, key and value sequences $\mathbf{q}$, $\mathbf{k}$ and $\mathbf{v}$ from the input sequence using a linear projection $\mathbf{U}_{qkv}\in\mathbb{R}^{D\times 3D_{h}}$:
$[\mathbf{q},\mathbf{k},\mathbf{v}]=\mathbf{z}\mathbf{U}_{qkv}.$
The $\mathbf{q}$ and $\mathbf{k}$ sequences are then used to compute a Softmax-normalized transformation matrix $\mathbf{A}$ indicating how to incorporate information of the whole sequence (i.e. in our case all image patches) for every single vector of the sequence:
$\mathbf{A}=\text{Softmax}(\frac{\mathbf{q}\mathbf{k}^{\intercal}}{\sqrt{D_{h}}
}).$
Scaling the dot-product product by $\sqrt{D_{h}}$ here ensures a balanced distribution of the Softmax output. After obtaining $\mathbf{A}$, the output of SA is computed as
$\text{SA}(\mathbf{z})=\mathbf{A}\mathbf{v}.$

A multi-head self-attention (MSA) layer (Vaswani et al., ) performs several attention operations in parallel, concatenates the result and projects it back to the internally used latent dimension of the transformer:

$$ $\text{MSA}(\mathbf{z})=[\text{SA}_{1}(\mathbf{z});\thinspace\text{SA}_{2}( \mathbf{z});\thinspace\dots;\thinspace\text{SA}_{k}(\mathbf{z})]\mathbf{U}_{ \text{msa}}$ $$

In an attention module the multi-head self-attention layer is followed by a multi-layer-perceptron (MLP) layer transforming the recently combined embeddings to extract new feature representations. Before presenting $\mathbf{z}$ to each layer in the module, the embeddings are normalized using LayerNorm (Ba et al., ). To ensure consistent gradient flow during training, residual connections (He et al., ) are behind both the MSA and the MLP layers (Wang et al., ). Furthermore, as a regularization measure, Dropout (Srivastava et al., ) is applied after every MSA and MLP layer.
In summary, given the sequence $\mathbf{z}_{t-1}$ from a previous attention module as input, we first compute the intermediate representation

$$ $\mathbf{z}_{t}^{\prime}=\text{MSA}(\text{LN}(\mathbf{z}_{t-1}))+\mathbf{z}_{t- 1},$ (1) $$

which is the presented to the MLP layer to compute the final output of the module

$$ $\mathbf{z}_{t}=\text{MLP}(\text{LN}(\mathbf{z}_{t}^{\prime}))+\mathbf{z}_{t}^{ \prime}.$ (2) $$

Finally, after N attention modules, the first vector of the sequence (corresponding to the class-token in the preprocessed input) is handed to a linear layer
$\mathbf{W}_{\text{out}}\in\mathbb{R}^{D\times C}$ to predict the final class of the image:
$\mathbf{y}=\text{argmax}(\mathbf{z}_{L}^{0}\mathbf{W}_{\text{out}}).$ $C$ denotes the number of classes.

### 3.2 Randomly permuting layer order during forward pass

During each forward pass, i.e. for each batch presented to the vision transformer, we randomly permute the execution order of layers during training. The intention here is to teach the layers to not only extract meaningful intermediate representations when receiving input from a particular layer, but to be able to process and encode information from and for all possible layers in the network. In terms of training, exchanging the order of layers does not require any changes in the basic error backpropagation algorithm. For the forward path, the order how weight matrices are multiplied and activation and attention functions applied changes for every batch and forward pass. This needs to be accounted in the backward pass by propagating the gradients in the precise reverse order that has been set in the forward pass, i.e.  multiplying the computed per-layer gradient matrices in the correct order. As we use Pytorch (Paszke et al., ) in all our experiments, this aspect is taken care of the framework’s autogradient feature. We refer to this model as LayerShuffle. To further illustrate the approach, a pseudocode listing is given in Algorithm 1.

Figure: Algorithm 1 Executing the forward path of a vision transformer with random layer order.

### 3.3 Layer position encoding

Figure: Figure 2: Attention module with layer position encoding.
Refer to caption: x4.png

In the second approach, LayerShuffle-position, we provide each layer with its current position in the network. Through this variation we aim to test if each layer can already adapt sufficiently by itself to information coming from different layers during test time or if giving it the current position can help further. In more detail, jointly with permuting the layer execution order, each layer learns a vector embedding $\mathbf{e_{\text{layer}}}^{p}\in\mathbb{R}^{F}$ for each possible index position $p\in[1,L]$ of the layer during training, where L is the number of layers and $F=32$ is our chosen embedding dimension. The layer’s current index $p$ in the network is presented together with the input to the layer $\mathbf{z}_{t-1}$ (Figure 2). The layer fetches the embedding vector $\mathbf{e_{\text{layer}}}^{p}$ associated with the passed index $p$ and concatenates it to the input vector $z_{t-1}$:
$\mathbf{h}_{t}=\text{concat}(\mathbf{z}_{t-1},\text{repeat}(\mathbf{e_{\text{
layer}}}^{p},N+1)).$
N is the number of patches extracted form the input image, the functions concat and repeat respectively concatenate and repeat tensors along their last (most varying) dimension.
A projection network, which consists of a LayerNorm (LN) (Ba et al., ) module, a single linear layer $\mathbf{W}_{\text{proj}}$, a GELU (Hendrycks & Gimpel, ) activation function as well as a Dropout (Srivastava et al., ) module, is then used to combine input and embedding and reduce it again to the used latent dimension $D$ of the transformer. To ensure gradient flow during training, a residual connection is added as well:

$$ $\mathbf{z}_{t}^{\prime\prime}=\text{Dropout}(\text{GELU}(\text{LN}(\mathbf{h}_ {t})W_{\text{proj}}))+\mathbf{z}_{t-1}$ $$

The resulting output $\mathbf{z}_{t}^{\prime\prime}$ is passed on to a regular multi-head-attention-and-feed-forward structure as described in Equations 1 and 2.

### 3.4 Predicting current layer position

Figure: Figure 3: Attention module with layer position prediction.
Refer to caption: x5.png

To determine if the incoming information to each attention layer is indeed sufficient for it to figure out its role, we specifically test for this ability with the LayerShuffle-predict variant. We equip each layer of the network with a simple position prediction module that takes the current layer output as an input and seeks to predict the current position of the layer in the network (Figure 3). The module consists of a single linear layer $\mathbf{W}_{pred}\in\mathbb{R}^{D\times L}$ receiving layer-normalized (LN)input.
$\mathbf{u}=\text{LN}(\mathbf{z}_{t})\mathbf{W}_{pred}$.

Each of these layer order prediction modules optimizes a cross-entropy loss where then the overall network optimizes the loss $\mathcal{L}_{\mathrm{out}}+\sum^{\forall i}\mathcal{L}_{i}.$
Here, $\mathcal{L}_{\mathrm{out}}$ is the regular cross-entropy loss of the output layer, and $\mathcal{L}_{i}$ is the layer position prediction loss of layer i, which is also a cross-entropy loss:

$$ $\mathcal{L}_{i}=-\log\left(\frac{\exp(u_{p})}{\sum^{\forall l\in L}\exp(u_{l}) }\right),$ $$

where $L$ is the number of layers in the network, $\mathbf{u}$ is the $L$-dimensional output of the position prediction network of layer $i$, and $u_{l}$ denotes the $l$-th dimension of the vector. $u_{p}$ is the output logit denoting the network’s predicted confidence that the layer currently is deployed at its actual position with index $p$.

## 4 Experiments

We conduct our experiments on the ILSVRC2012 dataset (Russakovsky et al., ), more commonly termed ImageNet2012. We use the original *ViT-B/16* (Dosovitskiy et al., ) vision transformer,
with the publicly available pre-trained weights provided on huggingface (). This model has been pre-trained on ImageNet21k (Ridnik et al., ) at an 224$\times$224 input image resolution and refined on ImageNet2012 at the same resolution. We refine the model again on ImageNet2012 at the same resolution, but using the training processes as described in Section 3. That is, we randomly permute layer execution order while refining the model. To establish a baseline, we refine the original *ViT-B/16* for one more epoch without changing the layer order. We have limited ourselves to a single epoch here as we found any longer training to be unlikely to bring additional improvement in preliminary experiments. For each approach, including the baseline, we train $5$ networks and compare their average validation accuracy.

All models are refined using Adam (Kingma & Ba, ) ($\beta_{1}=0.9$, $\beta_{2}=0.999$, $\epsilon=10^{-6}$), where an initial learning rate of $10^{-4}$ was empirically found to work best. In terms of batch size, we evaluate training batch sizes of 640 images, which is the maximum multiple of 8 that can fit in the video memory of our used GPU, as well as 128 images for models that benefit from a smaller batch size. We find that even smaller batch sizes do not yield any improvement in performance for our models. Inspecting training curves shows that the performance of our models plateaus at 20 epochs the latest, which we therefore set as the maximum number of training epochs. We use a form of early stopping by evaluating the model achieving lowest crossentropy loss on the validation set after 20 epochs. All models have been trained on a single NVIDIA H100 Tensor Core GPU with 80GB of memory, where the training of a single model for 20 epochs takes about 7 hours.

### 4.1 Sequential vs. arbitrary execution order

We report the average accuracy for the baseline and LayerShuffle approaches in Table 1.
Our baseline refined from a pre-trained *ViT-B/16* model performs very much as expected: For a classic sequential execution order of the model layers, the average validation accuracy performs very close to the original model (Dosovitskiy et al., ) at $82.61\%$ at a low standard deviation of 0.08. Not surprisingly, for an arbitrary layer execution order, the average model accuracy declines catastrophically to $0.13\%$. Our original assertion that in general, neural networks are not robust to changing the execution order of their layers, is in line with these results.

Our LayerShuffle approach shows slightly lower performance than the baseline when executing layers in their original order (average accuracies of $75.22$, $75.28$, and $74.41$ for our *LayerShuffle*, *LayerShuffle-position* and *LayerShuffle-predict* respectively), but improves dramatically over the baseline model in an arbitrary execution order setting. Surprisingly, the simplest *LayerShuffle* approach receives an accuracy of $62.77\%$, only slightly lower than the accuracy *63.61%* of the *LayerShuffle-position*, which receives information about the layer position.
While all three approaches seem to be overall on par, we observe the position prediction approach, *LayerShuffle-predict*, to be the least well-performing of the proposed approaches with an average accuracy of $61.18\%$. A possible explanation might be that due to optimization of multiple objectives (fitting both the output labels as well as predicting the current position of the layer) this approach requires more careful hyperparameter tuning. This hypothesis is also supported by the somewhat higher standard deviation of $1.06$ percentage points in accuracy, compared to the $0.41$ and $0.23$ percentage points of *LayerShuffle* and *LayerShuffle-position* respectively.
A further interesting observation is to be made when comparing the performance for sequential and arbitrary execution order for each approach respectively. For all approaches, using the original layer order for sequential execution still performs better than an arbitrary order. This is most likely a consequence of fine-tuning from a sequentially trained model.

For the layer position prediction approach, we measure the average accuracy of layer position predictions over all five trained *LayerShuffle-predict* models, and find that the layer position is predicted correctly in $99.99\%$ of all cases. These results demonstrate that each layer has enough information coming from its inputs alone to predict where it is in the network, providing the basis to adapt to its current position. We investigate this further when analyzing intermediate network representations in Section 4.3.

In conclusion, refining a pre-trained model while randomly permuting the execution order of the network layers can make a model more robust towards such arbitrary execution orders at test time. On the other hand, Dropout and LayerNorm by themselves do not have the same effect and fail to produce networks robust against layer shuffling.

**Table 1: *Approach accuracy for sequential and arbitrary execution order of layers on the ImageNet2012 validation set.* The baseline model (ViT-B/16) performs best when executed sequentially but fails catastrophically when the layers are executed in an arbitrary order. Even the simplest LayerShuffle variant (LayerShuffle), in which the model does not have any information about its current position, reaches an accuracy of 62.77. All of our proposed training approaches permit the models to be executed with arbitrary layer execution order at test time, while still delivering good performance for the original model execution order.**
| layer order | ViT-B/16 | LayerShuffle | LayerShuffle-position | LayerShuffle-predict |
| --- | --- | --- | --- | --- |
| sequential | $\mathbf{82.61\pm 0.08\%}$ | $75.22\pm 0.28\%$ | $\mathit{75.28\pm 0.18\%}$ | $74.41\pm 0.20\%$ |
| arbitrary | $0.13\pm 0.03\%$ | $\mathit{62.77\pm 0.41\%}$ | $\mathbf{63.61\pm 0.23\%}$ | $61.18\pm 1.06\%$ |

### 4.2 Removing layers during test time

To determine how neural networks trained with LayerShuffle would perform when several devices in a (distributed) model become unavailable, we further investigate the effect of pruning an increasing amount of layers during test time. We evaluate its average validation accuracy over 5 models when only using 3, 6, or 9 layers. In addition, we refine the original *ViT-B/16* transformer using LayerDrop (Fan et al., ) with a drop probability of 0.2 (as recommended by the authors) and compare it as a baseline to our approach under identical conditions. Note that whenever we evaluate the accuracy of our proposed approaches as well as the baseline, we do so two times: Once, for the original ”sequential” layer order as originally intended and trained for the *ViT-B/16* transformer, and once with arbitrary layer execution order where we change the order randomly for every forward path.

For sequential execution (Figures 1(b)), LayerDrop with a drop rate of 0.2 behaves similarly to LayerShuffle, with the exception that our approach performs better for a small number (3) of layers with an average accuracy of approximately $18\%$ vs. close to $0\%$ for LayerDrop. While for 6 layers, both approaches are roughly on par, for 9 layers LayerShuffle is slightly outperformed by LayerDrop as both approaches show an average accuracy in the $70-80\%$ range. At the full amount of 12 layers, this gap in average accuracy stays roughly the same as the LayerDrop-refined model closes in on the full accuracy of the original model, while our LayerShuffle approach achieves slightly lower accuracies (see also Table 1). For comparison, we also visualize models where we refined a reduced number of 3, 6, and 9 layers: while delivering similar performance as LayerDrop for 9 and 12 layers, these models perform significantly better than the previously discussed approaches at lower numbers, i.e. 3 and 6 layers. They do however, bear the drawback that for each specific amount of layers a new model must be refined from the original model, whereas for both LayerDrop and our LayerShuffle approach, only a single full-size model needs to be refined and the number of layers can be configured at will at test time.

For arbitrary execution (Figure 1(c)), LayerShuffle is the only approach that succeeds, with the average accuracy improving as the number of layers is increased. LayerDrop does not perform well regardless of the number of layers in the model.
A noteworthy detail is the comparable high average accuracy of the fully retrained baseline with 3 layers. Given the low performance of the refined models with 6 and 9 layers, as well as that there are only 6 possible permutations for 3 layers, the most likely explanation is that one of the 5 random permutations evaluated for the model was the original layer execution order the model has been trained for, i.e.  [1,2,3] therefore skewing the achieved accuracy in this case.

In conclusion, we find that our proposed approach has similar test-time scaling capabilities as LayerDrop, while still ensuring robustness towards arbitrary layer execution orders.

### 4.3 Analysis of intermediate network representations

To gain a deeper insight into how information is encoded in our trained models, we compute Uniform Manfold Approximation and Projection (UMAP) (McInnes et al., ) embeddings of the outputs of a particular attention module, where we color-code all output vectors based on the position the module held in the network when producing this output.

Figure: Figure 4: *UMAP embedding of the output of a layer trained with shuffling execution order.* The layer produces outputs in different subspaces of the latent space depending on their current position in the network. Lighter colors indicate layer positions closer to the input; layer positions close to the output are shown in dark colors. Note that information about the layer position has not been presented to the UMAP algorithm and the visual proximity of points with similar layer position is an emergent feature of our training approaches.
Refer to caption: x6.png

For the UMAP analysis, we present 1,000 randomly sampled images from the ImageNet2012 validation set to a LayerShuffle-trained model. While we use an evaluation batch size of 1 image and record all outputs of a single, previously selected layer, we randomly permute the execution order of layers such that the selected layer changes position in the network during every forward path. After the layer output vectors for all 1,000 images have been recorded, a UMAP reduction of the output space to 2D is performed.

Figure 4 shows the two-dimensional UMAP-embeddings of the output space of one particular layer in a trained *LayerShuffle* transformer. The current position of the layer in the network when producing a given output is color-coded from light position close to the input to dark (position close to the output). Note that this information about the layer position has not been presented to the UMAP algorithm. The plot shows that, while there is no sharp separation between outputs generated at different positions in the network, the layer clearly adapts to its current position and extracts different features for different positions in the network. A further interesting observation is the very distinct collection of points for layer positions close to the input, which are detached from the remaining manifold of points, which suggests that this initial stage, where early low-level features are extracted, requires special treatment of the layer.

### 4.4 Merging models with arbitrary execution order

Being robust against permuting the layer execution order, opens interesting other possibilities such as model merging, i.e. creating a new model from the layers of several identically trained models. The underlying rationale is that such merged models could also occur in a distributed setting, where compute nodes, whose layers have been trained as part of distinct models, but with the same training process, could form ad-hoc models together.

To construct merged models, where each layer stems from a different model, we require $12$ models for the $12$ layers of the *ViT-B/16*. We therefore train 7 more networks for our *LayerShuffle* approach and the baseline. Subsequently, we create 100 merged models (out of $12!$ possible combinations) by randomly sampling from these models for our proposed approach as well as the baseline respectively (models are not mixed between approaches). As mentioned previously, layers are sampled in such a way that no two layers in a merged model stem from the same model. We then evaluate the validation accuracy of all 100 models for both approaches.

Table 5 summarises the results. The merged baseline model *ViT-B/16* deteriorates from $82.61\%$ average accuracy to $1.87\%$ (despite sequential layer execution as required by the model) making the resulting merged model effectively unusable. The merged *LayerShuffle* models, on the other hand, perform slightly below the original model with an average accuracy of $59.68\%$ as opposed to the $62.77\%$ of the latter. Less surprisingly, merged models show a higher standard deviation at $1.15$ percentage points for the merged models vs. $0.41$ percentage points for the original ones as merged models do not contain any two layers that have been trained together, which makes their performance vary more. We can further improve performance by ensembling the 12 models trained with LayerShuffle, using the average of their output logit vectors. Such neural network ensembles often reach a better performance (Hansen & Salamon, ), which is also the case here with a significant improvement and an accuracy of $69.20\%$ for LayerShuffle Ensemble.

Visualizing the accuracies of all achieved models in a swarm plot paints a similar picture (Figure 5).
While most merged *LayerShuffle* achieve an accuracy close to the average accuracy of the original models, for the baseline, all merged models exhibit very low accuracies. In conclusion, we find that permuting layer order during training enables the construction of merged (or ”Frankenstein”) vision transformers, where each layer of the transformer can be taken from a different model, as long as all models have been refined from the same base model on the same data.

**Table 2: *Accuracy of merged ViTs.* Merged *LayerShuffle* achieve an accuracy close to the average accuracy of the original models, while for the baseline, the merged model exhibits very low accuracy. Ensembles of *LayerShuffle* models show clear improvement over single models.**
| Model | Top-1 Acc. | Top-5 Acc. | layer order |
| --- | --- | --- | --- |
| ViT-B/16 merged | $1.87\pm 6.51\%$ | $4.53\pm 11.82\%$ | sequential |
| LayerShuffle merged | $59.68\pm 1.15\%$ | $82.16\pm 1.03\%$ | arbitrary |
| LayerShuffle Ensemble | $69.20\%$ | $88.93\%$ | arbitrary |

Figure: Figure 5: *ImageNet2012 validation accuracies of merged models for LayerShuffle and ViT-B/16 models.* Enforcing random layer execution order during training permits the construction of ViT Frankenstein models. The performance of merged models composed of Baseline models on the other hand breaks down.
Refer to caption: x7.png

## 5 Discussion and Future Work

This paper presented a new approach called LayerShuffle, which enabled vision transformers to be robust to arbitrary order execution, pruning at test time, as well as adhoc-construction of merged models. For sequential execution, LayerShuffle performs on average
only slightly worse than the LayerDrop approach but is the only method that works when the layer execution is arbitrary.
A latent space analysis confirmed that layers of models trained with LayerShuffle adjust their output depending on which position they hold in the network. Finally, we investigated whether it is possible to build merged models from the models trained with LayerShuffle and found the performance of the built merged models to be only slightly less than the performance of our trained models, contrary to the baseline, where virtually all merged models delivered very poor performance.

That LayerShuffle works well is surprising and here we have only scratched the surface of its effect on the trained neural networks. To gain a deeper understanding, further analysis will include an inspection of the norms of the outputs of multi-head-attention and multi-layer-perceptron layers. This type of investigation could reveal whether layers learn to switch off their output for inputs they cannot process such that further relevant processing can occur in a suitable downstream layer to which the data is relayed through the attention module’s residual connections. Inspecting the model’s attention maps and embedding the intermediate latent vectors of all layers at all positions in a single two-dimensional embedding might yield further insights as well.

In the future, these properties could make LayerShuffle-trained models ideal candidates to be distributed over a number of very loosely coupled compute nodes to share the computational load of model inference. Given the enormous engineering, financial and logistical effort as well as the environmental impact (Strubell et al., ) of building and maintaining datacenters for state-of-the-art deep learning approaches on the one hand, as well as the large amount of available, but scattered compute through existing smartphones, laptop computers, smart appliances and other edge devices on the other hand, approaches that allow ad-hoc neural networks performing inference together could be of great impact. We therefore consider the deployment and orchestration of our trained models onto an actual set of edge devices and the practical implementation of the inference process on a network of such devices, likely by combining our approach with previously proposed frameworks to address this issue (Gacoin et al., ), a very promising direction of future research.

## Acknowledgements

We thank the members of the REAL and Creative AI group for useful discussions. This work was supported by a research grant (40575) from VILLUM FONDEN.

## Peer Reviews

confidence: 4
contribution: 1
initial_rating: 5
presentation: 2
questions: See the weaknesses.
soundness: 2
strengths: It makes the vision transformers capable of adapting to arbitrary layer execution orders at test time.
summary: This paper investigates alternative training approaches for vision transformers where the layers are permuted randomly to make them robust to pruning and shuffling at test time. Variants also include using layer-depth encoding and layer position prediction.
weaknesses: I carefully reviewed the paper, and rather than addressing an extensive list of less critical issues, I will concentrate on the most significant concerns that influence my rating. My focus will be on the following two points:

1) The reduction in performance, which is around or above 20%, is substantial. This limits the useability and effectiveness of the method, making motivation unclear. In what practical real-world application is it preferable to run vision transformers, in particular the ones used in the paper (ViT and DeiT) in distributed setups where one could tolerate 20% loss? Energy consumption optimization does not seem to be a motivator since there is an added cost of data transfer across nodes in a distributed setup. Instead of layer shuffling, layer dropping or just using smaller models might be more advantageous alternatives. Why would one use "any sequence of layer execution"?

2) Robustification to changing the order of execution and shuffling has been investigated in the past, for instance, as discussed in the paper by instance-wise layer reordering (ICLR 2020), permutation invariance (many works here), LayerDrop (ICLR 2020), RandConv (ICLR 2021), and ShuffleTransformer (last two can be added to related work). As mentioned, auto-regressive models leveraging self-attention across tokens, including LLMs, are very robust to changing the positions of adjacent layers or the underlying layers from the model. After patch embedding, ViT also follows a similar architecture. Besides, 
a recent work, ShuffleMamba discusses a similar stochastic layerwise shuffle regularization in training. Considering this, the novelty of the method becomes borderline.
I am open to reconsidering my rating based on a satisfactory rebuttal from the authors.

---

confidence: 5
contribution: 3
initial_rating: 5
presentation: 3
questions: Please refer to the questions in the weaknesses section.
soundness: 3
strengths: There are some strengths in the paper:
* The authors propose a method termed LayerShuffle to shuffle the layer order of a model during training. With LayerShuffle the model is less sensitive to the layer order and therefore more robust.
* A UMAP analysis reveals that layers of models trained with LayerShuffle adjust their output depending on which position they hold in the network.
* With LayerShuffle the model is still functional with performance degradation.
summary: The paper proposes a method termed LayerShuffle to shuffle the layer order of a model during training so that the model is robust to different layer orders. A UMAP analysis reveals that layers of models trained with LayerShuffle adjust their output depending on which position they hold in the network.
weaknesses: Although there are some spotlights in the paper, there are still some unclearnesses that need to be clarified:
* Novelty is limited. The key idea of the paper is to shuffle the layer order of a model during training so that the model is robust to different layer orders. 
* Model performance drops. Line 285 says the model performance with LayerShuffle is “slightly” lower than the one without LayerShuffle. However, the degradation is over 28% on the simple classification task according to Table 1 (CIFAR, sequential, LS-pred).
* LS and LS-Pos are almost the same according to Table 1. LS-pred is always the worst. Is it necessary to have three variants without performance improvement?
* More experiments on different model architectures. It would be great if the authors could conduct experiments with 2 more sota models.
* More experiments on different tasks. It would be great if the authors could conduct experiments on 2 more tasks.
* More experiments on different datasets. It would be great if the authors could conduct experiments on 2 more datasets.

---

confidence: 5
contribution: 2
initial_rating: 1
presentation: 2
questions: See weakness.
soundness: 2
strengths: 1. The paper method is well written.
2. The concept is interesting.
summary: This paper proposes a new layer shuffling method for Transformers to spread the model's components onto different computing devices so that the order of layers does not matter and immense speedup can be achieved. Also, the proposed method claims to be able to combine randomly trained networks without loss of accuracy.
weaknesses: 1. The concept is interesting; however, the argument that the computing machine can fail at a time does not convince me to develop this method. The paper has weak motivation.
2. The paper evaluation is not sufficient.
3. There are several Transformer architectures which have not been evaluated. Currently, the evaluation is only done on ViT/DeiT, which is not convincing to me. 
4. Hyperparameter study is not done.
5. Accuracy loss is significant (20%). This accuracy gap is very substantial.
6. I have particular concerns about Figure 1c. It is unfair to compare layer-drop or reduced depth with layer shuffle configuration at deployment because the two baselines are not trained with layer shuffling order. This figure is entirely misleading and should be dropped.
7. The only change made in the transformer block is the layer index, which is not a significant contribution.
8. Please include several recent transformer-based baselines such as Swin, EfficientVit, etc.

---

confidence: 4
contribution: 3
initial_rating: 6
presentation: 4
questions: Since I have included all questions and concerns in the weaknesses section, here are some minor corrections and typos I noticed:

1. Line 41-42: Change "deployment.Similarly, ..." to "deployment. Similarly, ..."
2. I think it would be beneficial to include a quantitative comparison with the baseline LayerNorm in Table 1, rather than only presenting it as a graph in Figure 1.
soundness: 3
strengths: S1. The methodology is highly intuitive and straightforward, making it easy to comprehend. The clarity in the writing style further enhances readability.

S2. The proposed method demonstrates strong potential for real-world applications and provides a promising foundation for future research directions.

S3. The paper presents a solid level of technical novelty, with innovative thinking underpinning the approach.
summary: This paper tackles the challenge of layer-order robustness in vision transformers by proposing a training method that randomizes attention module execution order.
This approach allows the model to handle arbitrary layer orders at test time with only a slight reduction in accuracy.
Through analysis, the authors find that layers adapt based on their inference position, contributing variably to the model’s predictions
Additionally, the trained models can be combined to create effective "Frankenstein" networks without compromising performance.
Finally, they demonstrate that the models maintain graceful performance degradation under layer pruning, suggesting strong potential for distributed architectures where execution order may be unreliable.
weaknesses: W1. The study primarily evaluates a low-level task, specifically classification, which raises the question of whether similar performance trends would hold for more complex tasks. It would be helpful if the authors could provide results for additional tasks, such as segmentation or regression, to gauge the method’s broader applicability.

W2. While the evaluation results on ImageNet show an acceptable performance drop relative to the baseline, Table 1 indicates a more substantial performance drop on the CIFAR-100 dataset. This suggests that performance may correlate with task complexity, though the current study does not provide sufficient insight into this relationship.

W3. I find it challenging to conceive of grounded examples for S2. This may be due to my limited expertise in distributed systems. Could the authors provide examples illustrating practical applications of the approach? Specifically, how does the robustness in arbitrary layers contribute to these applications?

W4. Some captions lack sufficient explanatory information. For instance, in Figure 1(b), it’s unclear what “ReducedRetrain” represents—is it the original model? Additionally, in Table 1, does the italicized font indicate the second-best performance?