## High Performance Im2win and Direct Convolutions using Three Tensor Layouts on SIMD Architectures

∗ ∗ ∗ † ∗ ‡§

Nanchang Hangkong University, China Microsoft, China University of Washington, USA Amazon Web Services, USA

Xiang Fu Xinpeng Zhang Jixiang Ma Peng Zhao Shuai Lu Xu T. Liu ∗ † ‡ §

Abstract -Convolution is the core component within deep neural networks and it is computationally intensive and time consuming. Tensor data layouts significantly impact convolution operations in terms of memory access and computational efficiency. Yet, there is still a lack of comprehensive performance characterization on data layouts on SIMD architectures concerning convolution methods. This paper proposes three novel data layouts for im2win convolution: NHWC, CHWN, and CHWN8, and introduces a set of general optimization techniques for both direct and im2win convolutions. We compare the optimized im2win convolution with the direct convolution and PyTorch's im2col-based convolution across the aforementioned layouts on SIMD machines. The experiments demonstrated that the im2win convolution with the new NHWC layout achieved up to 355% performance speedup over NCHW layout. Our optimizations also significantly improve the performance of both im2win and direct convolutions. Our optimized im2win and direct convolutions achieved up to 95% and 94% of machine's theoretical peak performance, respectively.

Index Terms -direct convolution, im2win convolution, NHWC layout, CHWN layout, CHWN8 layout

## I. INTRODUCTION

Convolution is the essential component of deep neural networks for computer vision tasks such as feature exaction from large-scale image data [1]. It not only comprises 50%-90% of computational operations including convolutional, pooling, ReLU, and fully-connected layers [2], but also consumes more than 90% of the total execution time of many popular neural networks [3]-[5]. Hence, optimizing convolution operations is crucial for enhancing the performance of neural networks.

Convolution methods can be classified into three main categories based on how they transform the input tensor: direct, im2col-based, and im2win. Direct convolution performs convolution operations directly on the tensor without changing its format [6]. This approach avoids extra memory consumption compared to im2col-based and im2win convolutions, but it suffers from nonconsecutive memory access. Im2colbased convolution transforms the convolution into general matrix-matrix multiplications (GEMM) [7], [8], leveraging optimized Basic Linear Algebra Subprograms (BLAS) [9] for excellent performance. It has regular memory access but a significant extra memory footprint, which greatly limits its applicability on memory-constrained devices. Previously, we propose a memory-efficient convolution called image-towindow (im2win), which reorganizes the input tensor into a row of dot product windows and flattens the unique elements

This work is not related to the author's position at Amazon.

of these windows into a row that correspond to the convolution operation's receptive fields [10], [11]. It provides sequential memory access and data reuse, and thus greatly reduces memory overhead.

A tensor memory/data layout (referred as layout henceforth) refers to how the data of a tensor is physically arranged in memory. There are commonly three layouts for tensors: NCHW, NHWC and CHWN, where N is the batch size, C is the number of channels, H is the image height, and W is the image width. It significantly impacts convolution operations in terms of memory access, computational efficiency and compatibility with deep learning frameworks [12]-[14].

Fig. 1. The original input tensor ( N = 1 , H original = W original = C original = 3 ) and its corresponding im2win tensor ( N = 1 , C im win 2 = 3 , H im win 2 = 2 , W im win 2 = 6 ) in the NCHW layout

![Image](https://ac.marcodsn.me/arxiv-markdown-images/2408.00278v1-figure-5c1381cd-af32-4809-8731-a720458a3cd7.jpg)

Tensors are stored as one-dimensional arrays in memory, although logically represented as four-dimensional arrays. An input tensor and an im2win tensor in a NCHW layout are illustrated in Figure 1. Different colors represent different channels. In the NCHW layout, the elements in the width dimension are contiguous in memory by prioritizing the width dimension first, followed by height, channel and batch. Assuming a stride of 1 and a filter tensor in the NCHW layout with dimensions of 1x3x2x2, the elements in the solid-lined boxes are used to compute the first output element in the output tensor, while the elements in the dashed-lined boxes are for the next. Further details will be provided in Section III.

In general, the memory efficiency and performance implications of various tensor layouts with different convolution algorithms on single-input, multiple-data (SIMD) architectures have received limited attention. The previous im2win works optimize the NCHW layout on CPU and GPU but have not tried the NHWC and CHWN layouts [10], [11]. Li et al. reveal the performance impact of the NCHW layout with the im2col-based convolution and the CHWN layout with the direct convolution in different CNN layers, and propose a fast multi-dimension layout transformation algorithm on GPU [15].

Li et al. optimize the cache utilization of the NCHW and NHWC layouts in DNN training along with DNN pruning on CPU [16]. To date, no performance characterization have been conducted on NCHW, NHWC and CHWN layouts on SIMD architectures with the three convolution methods.

To address aforementioned gaps, we propose three new layouts for the im2win convolution: NHWC, CHWN, and CHWN8, and propose a set of optimization techniques based on the roofline model for both direct and im2win convolutions. We compare the optimized im2win convolution with the optimized direct and PyTorch's im2col-based convolutions with the above layouts on SIMD machines. Our experiments demonstrate that the new NHWC layout on the im2win convolution achieves 11% to 355% performance speedup compared with the NCHW layout. Our proposed optimizations have been empirically validated to enhance the performance of both im2win and direct convolutions. Our optimized im2win convolution and our optimized direct convolution achieve up to 95% and 94% of the theoretical peak performance of the machine, respectively. As the main contributions, this work

- 1) proposes three novel layouts for im2win convolution.
- 2) comes up with a set of general optimization techniques that not only can be applied to im2win convolution and but also to direct convolution on different layouts.
- 3) compares the optimized im2win convolution with the im2col-based and the optimized direct convolutions using four tensor layouts on SIMD machines.

## II. PRELIMINARY AND RELATED WORKS

## A. Notation

The input of a convolution operation includes an input tensor ( I ), a filter tensor ( F ), and an output tensor ( O ). In these tensors, N i is the batch size, s is the stride size, C i and C o are the number of input and output channels (also known as feature maps), H i/f/o and W i/f/o denote the height and width of a feature map.

## B. Tensor Layouts: NCHW, NHWC, CHWN

In the NCHW layout, the input ( I ), the filter ( F ), and the output tensors ( O ) are expressed as I [ N i ][ C i ][ H i ][ W i ] , F [ C o ][ C i ][ H f ][ W f ] , and O [ N i ][ C o ][ H o ][ W o ] , respectively. The convolution is defined as:

$$\mathcal { O } _ { ( i, j, m, n ) } = \sum _ { j = 1 } ^ { C _ { i } } \sum _ { m = 1 } ^ { H _ { f } } \sum _ { n = 1 } ^ { W _ { f } } \left ( \mathcal { I } _ { ( i, j, m \times s + u, m \times s + v ) } \quad ( 1 ) \quad \text{In th}$$

$$\times \mathcal { F } _ { ( j, r, u, v ) } \right ),$$

In the NHWC layout, the input ( I ), the filter ( F ), and the output tensors ( O ) are expressed as I [ N i ][ H i ][ W C i ][ i ] , F [ C o ][ H f ][ W f ][ C i ] , and O [ N i ][ H o ][ W o ][ C o ] , respectively. The convolution is defined as:

$$\mathcal { O } _ { ( i, m, n, j ) } = \sum _ { j = 1 } ^ { C _ { i } } \sum _ { m = 1 } ^ { H _ { f } } \sum _ { n = 1 } ^ { W _ { f } } \left ( \mathcal { I } _ { ( i, m \times s + u, m \times s + v, j ) } \quad ( 2 ) \quad \text{$\text{$\text{$NHWC}$}$} \\.$$

×F

(

j,u,v,r

)

)

,

In the CHWN layout, the input ( I ), the filter ( F ) and the output tensors ( O ) are expressed as I [ C i ][ H i ][ W N i ][ i ] , F [ C i ][ H f ][ W f ][ C o ] , and O [ C o ][ H o ][ W o ][ N i ] respectively. The convolution is defined as:

$$\mathcal { O } _ { ( j, m, n, i ) } = \sum _ { j = 1 } ^ { C _ { i } } \sum _ { m = 1 } ^ { H _ { f } } \sum _ { n = 1 } ^ { W _ { f } } \left ( \mathcal { I } _ { ( j, m \times s + u, m \times s + v, i ) } \quad \ \ ( 3 ) \\ \times \mathcal { F } _ { ( r, u, v, j ) } \right ), \\ \text{--} \colon \circ \dots \quad \text{--} \colon \dots \quad \text{--} \colon \dots \dots$$

The definitions (1), (2) and (3) above are all subject to

$$j & = 1, 2,.., C _ { o }, m = 1, 2,.., H _ { o }, n = 1, 2,.., W _ { o }, \\ i & = 1, 2,.., N _ { i }, u = 1, 2,.., H _ { f }, v = 1, 2,.., W _ { f }, \\ r & = 1, 2,.., C _ { i }.$$

## C. Convolution Algorithms and Related Works

Direct convolution performs on the original I and F without any tensor transformation. It has seven nested for loops and an AXPY operation in the innermost loop. Based on the tensor layouts that direct convolution works with, the AXPY operation needs to read at different indices of F and I , and writes at different indices of O .

Direct convolution can compute with the original input tensors, so they usually adopt the NCHW layout of raw images. A study shows that, for convolution instances with large C, NHWC layout outperforms NCHW layout [15]. Grouping a certain dimension of the input tensors by a fixed size can also enhance the performance of direct convolution, such as NC32HW32 layout [17]. Several works [6], [18] have shown that the performance of direct convolution can be greatly improved by designing specific layouts based on the loop ordering of the algorithm on SIMD architecture.

The im2col-based convolution transforms a convolution operation into a GEMM operation. I [ N i ][ C i ][ H i ][ W i ] is processed in N i batches, each batch contains data I ′ [ C i ][ H i ][ W i ] (that is, a single image). The im2col algorithm flattens the elements of each dot product window of I ′ and copies them into a single row of a 2D matrix [7]. In addition to the conventional im2col data transformation algorithm, the MEC algorithm compresses the matrix layout, while still enabling the utilization of high-performance BLAS algorithms to perform convolution operations [19].

## III. HIGH-PERFORMANCE IM2WIN AND DIRECT CONVOLUTION USING THREE TENSOR LAYOUTS

In this section, we first review three layouts in the context of the direct convolution. Then we present three new layouts for the im2win convolution, following by how to determine the loop ordering based on the layouts and a set of optimizations for the im2win and direct convolutions on SIMD architectures.

## A. Motivations for Different Tensor Layouts

An example of direct convolution on an original input tensor in the NHWC layout is illustrated in Figure 2. The NHWC layout prioritizes the storage of elements in the last logical dimensionC i -followed by that of W H i , i , and N i . In the NHWC layout, the elements with the same N i , H i ,

Fig. 2. The original input tensor ( N i = 1 , H i = W i = C i = 3 ) and its corresponding im2win tensor ( N i = 1 , C i = 3 , H i = 2 , W i = 6 ) in the NHWC layout, the filter tensor ( N f = 1 , C f = 3 , H f = W f = 2 ), s = 1 , the output tensor ( N o = 1 , C o = 1 , H o = W o = 2 )

![Image](https://ac.marcodsn.me/arxiv-markdown-images/2408.00278v1-figure-3a4d20b7-75e8-45d0-a3d5-9a81dbcb6939.jpg)

Fig. 3. The original tensor ( N i = 8 , H i = W i = C i = 3 ) and its corresponding im2win tensor ( N i = 8 , C i = 3 , H i = 2 , W i = 6 ) in the CHWN/CHWN8 layout

![Image](https://ac.marcodsn.me/arxiv-markdown-images/2408.00278v1-figure-ce365929-69ff-43f6-a662-c4b7f46327e8.jpg)

and W i but different C i are contiguous in memory, which have unit stride access. Assuming a stride of 1, the red and green elements (outlined by solid lines, representing the convolutional windows to compute a single output element) in the input tensor are multiplied with the corresponding elements in the filter tensor, and the results are summed up (i.e., an AXPY) to obtain O 00 . Next, the green and yellow elements (outlined by dashed lines) are used to calculate O 01 . This process continues, with the convolutional window moving by the stride length in H i or W i , until all elements of the output tensor are computed.

Recall in Figure 1, the NCHW layout prioritizes the storage of elements in the last logical dimensionW i -followed by that of H i , C i , and N i . Note that all C i but not all W i are used during the AXPY operation to obtain one output

| Algorithm 1: Im2win Tensor Transformation   | Algorithm                                                           |
|---------------------------------------------|---------------------------------------------------------------------|
| Input: input tensor I in the NHWC layout,   | filter tensor F , Stride s                                          |
| H                                           | Output: Im2win tensor ˆ I in the NHWC layout                        |
| 1 o                                         | = ( H i - H f ) /s +1                                               |
| 2 for i 3                                   | = 1 to N i do for m = 1 to H o do                                   |
| 4                                           | for k = 1 to W i do                                                 |
| 5                                           | for u = 1 to H f do                                                 |
|                                             | r = 1 to C i do                                                     |
| 6                                           | for                                                                 |
| 7                                           | ˆ I [ i ][ m ][ u + k × H f ][ r ] = I [ i ][ m × s + u ][ k ][ r ] |

element. NCHW has non-unit stride access during the tensor convolution. However, the non-unit stride access may not be harmful, depending on the effects of caching and the access patterns used (determined by the loop ordering).

As shown in Figure 3, the CHWN layout stores elements by prioritizing N i , followed by W i , H i , and C i in memory. Previous research on GPU recommends to use N as the lowest dimension for coalesced memory access and data reuse in registers [15]. It has been observed that the performance is sensitive to the value of N. The elements within the solidlined boxes are used to compute the first eight output elements, while the elements within the dashed-lined box are used for the next eight output elements. This facilitates the use of vector registers for vectorization. Assuming s = 1 , the convolutional window moves by one element in both the H i and W i dimensions until all output elements are computed.

## B. Im2win Tensor Transformation on Three Tensor Layouts

In this subsection, we present the im2win tensor transformation on three tensor layouts. The im2win tensor transformation process for the NWHC layout is shown as Algorithm 1. For other layouts, slight modifications need to be made on it. The im2win transformation flattens the elements of a convolutional window, storing them contiguously in memory and prioritizing them in the C i dimension, as outlined in Algorithm 1 from Line 4 to Line 7. An example of transforming the original input tensor into an im2win tensor in the NHWC layout is

## Algorithm 2: Naive Im2win Convolution

```
  Algorithm 2: Naive Im2win Convolution
                                                                                                                                                                                                        
  Input: input tensor                                                                                                                                                                                                       

                                                                                                                                                                                                        }
                                                                                                                                                                                                      

1                                                                                                                                                                                                        0                                                                                                                                                                                                        1                                                                                                                                                                                                        2                                                                                                                                                                                                        3                                                                                                                                                                                                        4                                                                                                                                                                                                        6                                                                                                                                                                                                        8                                                                                                                                                                                                        9                                                                                                                                                                                                        7                                                                                                                                                                                                        10                                                                                                                                                                                                        5                                                                                                                                                                                                        16                                                                                                                                                                                                        19                                                                                                                                                                                                        20
```

shown in Figure 2. It is important to note that the green elements can be reused between two adjacent windows, which eliminates the need for repetitive storage in memory like the im2col transformation [7]. In the output im2win tensor of Algorithm 1, the elements involved in each convolution operation are contiguous and more compact in memory, improving spatial locality, cache and SIMD efficiency.

An example of the im2win convolution using the NHWC layout is shown in Figure 2. A naive im2win convolution method is shown as Algorithm 2. It is similar to the direct convolution, which involves seven nested loops and an AXPY in the innermost loop, except prior to that, the im2win convolution performs a tensor transformation upon I to get ˆ I , and F in the NHWC layout is transformed into a NWHC layout to match ˆ I . All output elements can be computed through the outer four loops of Algorithm 2 from Line 3 to Line 6.

An im2win tensor in CHWN/CHWN8 layout is shown in Figure 3. The characteristics of C i , H i , and W i in this layout are similar to NCHW, but the CHWN layout prioritizes storage in N i . The CHWN layout's efficiency is constrained by 256bit vector registers, which process only 8 outputs at once. When N i &gt; 8 , this leads to low cache utilization, as the cache holds unnecessary data for these 8 output calculations. Motivated by the approach of dividing C i into blocks in the direct convolution [18], we propose a new CHWN8 layout. CHWN8 lays 8 N i in the innermost layer and remaining N i in the outermost layer, which takes full advantage of the vector registers without sacrificing the cache utilization. N i can be set to a multiple of 8 (with padding if necessary), CHWN8 layout may provide better data continuity than the NHWC layout when C i is relatively small.

## C. Loop Reordering

In this subsection, we reorder the loops for the im2win and direct convolutions based on different tensor layouts. Ideally, we arrange the inner loops to access data closer in memory to enjoy the unit stride access as long as possible. In both direct and im2win convolutions, for NCHW, CHWN, and CHWN8 layouts, as they have the CHW memory access pattern, we use the width of the convolution window as the innermost loop,

## Algorithm 3: High Performance Im2win Convolution

```
<_SQL_>
```

followed by the height, and the channel ( W f , H f , and C i ). Conversely, for the NHWC layout, the innermost three loops iterate over the channel, the width, and the height ( C i , W f , and H f ) of the convolution window. For the im2win convolution, since the im2win transformation flattens the elements of a convolutional window, we usually coalesce the height and the width layers ( W ,H f f ) into a W f × H f .

Next we determine the order of the outer four loops. It not only determines the order in which the elements of the output tensor are produced but also influences the order in which the element in the input tensor are accessed. The layout mainly affects the inner three levels of the loop order, for the outer four levels the loop order applies to all data layouts. Recall in Figure 2, the green elements of the input tensor are shared between the convolution windows of adjacent output tensor elements. Therefore, we consider to position the width ( W o ) of the traversed output tensor in the fourth layer of loops at Line 6 in Algorithm 2. Because the memory access of the input tensor is expensive in a convolution operation, we set the channel ( C o ) as the third layer to reduce its access. Since the batch of the output tensor corresponds to the batch of the input tensor, we place the batch ( N i ) in the first layer of the loop. Finally we have the order of the outer four loops as NHCW in Algorithm 2.

## D. Optimizations for the Im2win and Direct Convolutions

In this subsection, we use the Roofline Model [20] to determine how to optimize the im2win and direct convolutions on SIMD systems. We propose a set of optimizations for both the im2win and direct convolutions and apply the optimizations to them (with a slight modification on the direct convolution). The optimizations are classified into two categories: reducing the memory bottleneck and increasing the arithmetic intensity of the convolution kernel. The former includes hoist, memory alignment, register and cache blocking. The latter includes loop unrolling, vectorization and FMA instructions, loop coalescing, and parallelization strategies. We have three level parallelization: Non-Uniform Memory Access (NUMA) level, thread level using OpenMP and instruction level using SIMD.

We present the optimized im2win convolution as Algorithm 3. Since each element of the output tensor in each batch ( N i ) can be computed independently, there is abundant parallelism available [21] and this is NUMA-friendly. Within each batch, the shared elements of adjacent windows can be maximized across the threads. When the dimension we choose to parallelize is small and the number of CPU cores is large, this leads to a workload imbalance in many core architecture. The solution is to coalesce multiple dimensions/loops into one parallel loop to achieve better load balance. In practice, we find that coalescing two dimensions of the output tensor yields the best load balance. Hence, we apply parallel strategies and coalesce N i and H o in a parallel loop at Line 4 in Algorithm 3.

In Algorithm 3, we hoist three things: the indices of the elements in the 1D array of the tensor, the elements of the input tensor, and the entire filter tensor at Line 12. We use register and cache blocking [22], [23] at Line 7 to reduce cache misses because the neighboring convolutional windows share duplicate elements of the input tensor, which W o,b is the blocking size. Next, we unroll the loop at Line 12 to take advantage of the spatial locality, because the innermost loop accesses consecutive elements. Since we are using FP32 and AVX2, we set the loop unrolling size N vec =8. Finally, we vectorize the input tensor and filter tensor in units of eight to use the FMA instruction in AVX2 [10] at Line 14.

A cache-line is 512 bits on contemporary x86 64 architectures, which is the minimum data quantity that can be fetched from memory to cache. Without memory alignment of the tensor data structure, the CPU needs to issue two memory requests to access an element, because the element may be in the middle of the cache-line. This hurts the performance greatly because not only the CPU has to wait for memory access but also more caches are used. This leads to higher cache miss and lower cache utilization. We store the elements of the tensor in a memory-aligned way using posix memalign in C during the memory allocation.

## IV. EXPERIMENTS

## A. Experimental Setup

Architectures. We use a server with two Intel ® Xeon ® Gold 6330 CPUs and 251 GB RAM. Each CPU has 28 physical cores, running at 2.0 GHz, with 48 KB L1d cache, 32 KB L1i cache, 1.28MB L2 cache and 43 MB L3 cache.

Benchmarks We aim to cover the majority of the convolutional layers in commonly used DNNs. Hence for our experimental evaluation, we select an state-of-the-art DNN benchmark [19] shown in Table I, which includes twelve unique convolution layers, conv1-conv12.

Software We compare the direct, im2win, and im2colbased convolutions with four tensor layouts: NHWC, NCHW,

TABLE I

TWELVE CONVOLUTION LAYERS OF THE DNN BENCHMARKS.

| NAME                                                                       | INPUT C i × H i × W i                                                                                                                                                 | FILTER, STRIDE C o × H f × W f , s h /s w                                                                                                                                                     | OUTPUT C o × H o × W o                                                                                                                                                  |
|----------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| conv1 conv2 conv3 conv4 conv5 conv6 conv7 conv8 conv9 conv10 conv11 conv12 | 3 × 227 × 227 3 × 231 × 231 3 × 227 × 227 64 × 224 × 224 96 × 24 × 24 256 × 12 × 12 3 × 224 × 224 64 × 112 × 112 64 × 56 × 56 128 × 28 × 28 256 × 14 × 14 512 × 7 × 7 | 96 × 11 × 11 , 4 96 × 11 × 11 , 4 64 × 7 × 7 , 2 64 × 7 × 7 , 2 256 × 5 × 5 , 1 512 × 3 × 3 , 1 64 × 3 × 3 , 1 128 × 3 × 3 , 1 64 × 3 × 3 , 1 128 × 3 × 3 , 1 256 × 3 × 3 , 1 512 × 3 × 3 , 1 | 96 × 55 × 55 96 × 56 × 56 64 × 111 × 111 64 × 109 × 109 256 × 20 × 20 512 × 10 × 10 64 × 222 × 222 128 × 110 × 110 64 × 54 × 54 128 × 26 × 26 256 × 12 × 12 512 × 5 × 5 |

CHWN, CHWN8. We intend to compare with the state-ofthe-art implementation of direct convolution and the layout proposed in [18], but their implementation is not opensourced. We use the im2col-based convolution in PyTorch 2.1 [24] with MKL [25]. Note that PyTorch only supports the NHWC and NCHW layouts. Our code is compiled with GCC 9.5.0 compiler and -O3 -mavx2 -mfma -fopenmp -march=native compilation flags. We use OpenMP 4.0 for parallelization with guided scheduling.

## B. Performance of Different Convolution Algorithms

We run each algorithm 50 times on each benchmark with N i =128 and report the best runtime. Figure 4 shows the performance results in TFLOPS and Figure 5 shows the memory usage of our high-performance direct convolution and highperformance im2win-based convolution, and the im2col-based convolution using MKL in PyTorch. In Figure 4, the left y-axis shows the performance in TFLOPS, and the right y-axis shows the performance of the machine peak. Overall, our optimized im2win convolution achieves eight best TFLOPS out of twelve benchmarks; our optimized direct convolution achieves three best TFLOPS out of twelve benchmarks and the im2win convolution achieves close performance on these three; the im2col-based convolution in PyTorch achieves one out of twelve best TFLOPS on conv12. All twelve best TFLOPS are all yielded from the NHWC layout across these three methods. Our proposed optimization techniques are proven effective on both im2win and direct convolutions. Our im2win convolution achieves 95% and 91% of the theoretical peak performance of the architecture on conv5 and conv6 respectively. Our direct convolution achieves 91% and 94% of the theoretical peak performance on conv5 and conv6 respectively.

With the NHWC layout and performance normalization, excluding conv6 and conv12, our im2win convolution achieves between 1.1 × and 4.6 × performance speedup, and our direct convolution achieves between 1.1 × and 3.8 × performance speedup against the im2col-based convolution. All the best performance of the im2win convolution on twelve benchmarks is achieved using the NHWC layout on CPU. Our im2win

Fig. 4. Performance results in TFLOPS of the direct convolution, the im2win convolution and the im2col-based convolution using different layouts. Note that the theoretical peak performance of the server is 3584 GFLOPS.

![Image](https://ac.marcodsn.me/arxiv-markdown-images/2408.00278v1-figure-75ab4e38-5cae-4d0c-bd51-697ef5d01f33.jpg)

8

Fig. 5. Memory usage of the direct, the im2win and the im2col-based convolutions using different tensor layouts. Note that in conv4, the im2col-based convolutions with the NWHC and NCHW layouts use 21GB of memory.

![Image](https://ac.marcodsn.me/arxiv-markdown-images/2408.00278v1-figure-c694ea9e-32ad-42c3-9ce8-2c7b17c33e09.jpg)

convolution using the NHWC layout outperforms the NCHW layout by at least 11% and up to 355% across all benchmarks, showcasing significant efficiency gains in various scenarios. For the direct convolution, nine out of twelve best TFLOPS are achieved using the NHWC layout on CPU, the rest three are achieved using the CHWN8 layout.

With the NCHW layout, our im2win-based convolution achieves between 1.4 × and 2.4 × performance speedup on all benchmarks, and PyTorch's im2col-based convolution achieves between 1.1 × and 7.5 × performance speedup (excluding conv7) against the direct convolution. The direct convolution performs poorly on the NCHW layout.

With the CHWN/CHWN8 layout, im2win CHWN8 outperforms im2win CHWN on all benchmarks from 3.7 × to 16 × ; direct CHWN8 outperforms 2.3 × and 8 × over direct CHWN except in conv7. This shows our proposed CHWN8 layout overwhelmingly beats the CHWN layout on these two methods. The direct convolution with the CHWN8 layout performs better than the CHNW, NCHW and NHWC layouts when C i is small ( C i = 3 for conv1, conv2, conv3). Similar results have also been observed in the previous GPU work [15].

In Figure 5, using the same tensor layout, the memory usage of each convolution is the same hence we only annotate one number per method. In all benchmarks, the direct convolution uses the least memory, while the im2col-based convolution consumes the most memory. On average, the im2col-based convolution has 3.9 × more memory usage than direct convolution, and im2win-based convolution has 1.5 × more memory usage than direct convolution. The im2win-based convolution uses on average 39% of the memory of the im2col-based method, and in conv5, it uses only 24% of the memory of the im2col-based convolution.

## V. CONCLUSION

We proposed three new layouts for the im2win convolution: NHWC, CHWN and CHWN8, and a set of general optimization techniques for both direct and im2win convolutions on SIMD architectures. We applied these optimizations on the im2win and the direct convolutions, and compared with PyTorch's im2col-based convolution on the above layouts along with the NCHW layout. Our experiments demonstrated that im2win convolution using the new NHWC layout achieved 11% to 355% performance speedup compared to NCHW layout. The proposed optimizations were proven to boost the performance of the im2win convolution and direct convolution. Our optimized im2win and direct convolutions achieved up to 95% and 94% of the theoretical peak performance of the machine, respectively.

## ACKNOWLEDGMENT

This work was partially supported by the NSF OAC2402542, and the National Natural Science Foundation of China (Grant No. 42261070).

## REFERENCES

- [1] Y. Cui, Y. Sun, J. Hu, and G. Sheng, 'A Convolutional Auto-Encoder Method for Anomaly Detection on System Logs,' in 2018 IEEE International Conference on Systems, Man, and Cybernetics (SMC) , Oct. 2018, pp. 3057-3062, iSSN: 2577-1655.
- [2] N. Ma, X. Zhang, H.-T. Zheng, and J. Sun, 'Shufflenet v2: Practical guidelines for efficient cnn architecture design,' in Proceedings of the European conference on computer vision (ECCV) , 2018, pp. 116-131.
- [3] V. Sze, Y.-H. Chen, T.-J. Yang, and J. S. Emer, 'Efficient processing of deep neural networks: A tutorial and survey,' Proceedings of the IEEE , vol. 105, no. 12, pp. 2295-2329, 2017.
- [4] Y. Yan, F. Guo, H. Mo, and X. Huang, 'Hierarchical tracking control for a composite mobile robot considering system uncertainties,' in 2024 16th International Conference on Computer and Automation Engineering (ICCAE) . IEEE, 2024, pp. 512-517.
- [5] J. Chen, W. Xu, and J. Wang, 'Prediction of car purchase amount based on genetic algorithm optimised bp neural network regression algorithm,' Preprints , June 2024. [Online]. Available: https://doi.org/10.20944/preprints202406.1304.v1
- [6] E. Georganas, S. Avancha, K. Banerjee, D. Kalamkar, G. Henry, H. Pabst, and A. Heinecke, 'Anatomy of high-performance deep learning convolutions on simd architectures,' in SC18: International Conference for High Performance Computing, Networking, Storage and Analysis . IEEE, 2018, pp. 830-841.
- [7] K. Chellapilla, S. Puri, and P. Simard, 'High performance convolutional neural networks for document processing,' in 10th Int'l Workshop on Frontiers in Handwriting Recog. , 2006.
- [8] A. V. Trusov, E. E. Limonova, D. P. Nikolaev, and V. V. Arlazarov, 'p-im2col: Simple yet efficient convolution algorithm with flexibly controlled memory overhead,' IEEE Access , vol. 9, pp. 168 162-168 184, 2021.
- [9] J. J. Dongarra, J. Du Croz, S. Hammarling, and I. S. Duff, 'A set of level 3 basic linear algebra subprograms,' ACM Transactions on Mathematical Software (TOMS) , vol. 16, no. 1, pp. 1-17, 1990.
- [10] S. Lu, J. Chu, and X. T. Liu, 'Im2win: Memory efficient convolution on SIMD architectures,' in 2022 IEEE High Performance Extreme Computing Conference (HPEC) , 2022, pp. 1-7.
- [11] S. Lu, J. Chu, L. Guo, and X. T. Liu, 'Im2win: An Efficient Convolution Paradigm on GPU,' in Euro-Par 2023: Parallel Processing , ser. Lecture Notes in Computer Science, J. Cano, M. D. Dikaiakos, G. A. Papadopoulos, M. Peric` as, and R. Sakellariou, Eds. Cham: Springer Nature Switzerland, 2023, pp. 592-607.
- [12] A. Paszke, S. Gross, F. Massa et al. , 'Pytorch: An imperative style, high-performance deep learning library,' in Proceedings of the 33rd International Conference on Neural Information Processing Systems (NeurIPS) , 2019, pp. 8024-8035.
- [13] M. Abadi, P. Barham, J. Chen et al. , 'Tensorflow: A system for largescale machine learning,' in Proceedings of the 12th USENIX Conference on Operating Systems Design and Implementation , ser. OSDI'16. USA: USENIX Association, 2016, p. 265-283.
- [14] S. Chetlur, C. Woolley, P. Vandermersch, J. Cohen, J. Tran, B. Catanzaro, and E. Shelhamer, 'cuDNN: Efficient primitives for deep learning,' CoRR , vol. abs/1410.0759, 2014.
- [15] C. Li, Y. Yang, M. Feng, S. Chakradhar, and H. Zhou, 'Optimizing Memory Efficiency for Deep Convolutional Neural Networks on GPUs,' in SC16: International Conference for High Performance Computing, Networking, Storage and Analysis . Salt Lake City, UT, USA: IEEE, Nov. 2016, pp. 633-644.
- [16] B. Li, Q. Xue, G. Yuan, S. Li, X. Ma, Y. Wang, and X. Tang, 'Optimizing Data Layout for Training Deep Neural Networks,' in Companion Proceedings of the Web Conference 2022 , ser. WWW '22. New York, NY, USA: ACM, Aug. 2022, pp. 548-554.
- [17] V. Ferrari, R. Sousa, M. Pereira, J. P. L. Carvalho, J. N. Amaral, J. Moreira, and G. Araujo, 'Advancing direct convolution using convolution slicing optimization and isa extensions,' in Advancing Direct Convolution using Convolution Slicing Optimization and ISA Extensions , March 2023.
- [18] J. Zhang, F. Franchetti, and T. M. Low, 'High performance zero-memory overhead direct convolutions,' in International Conference on Machine Learning . PMLR, 2018, pp. 5776-5785.
- [19] M. Cho and D. Brand, 'MEC: memory-efficient convolution for deep neural network,' in International Conference on Machine Learning (ICML) . PMLR, 2017, pp. 815-824.
- [20] S. Williams, A. Waterman, and D. Patterson, 'Roofline: an insightful visual performance model for multicore architectures,' Commun. ACM , vol. 52, no. 4, p. 65-76, apr 2009. [Online]. Available: https://doi.org/10.1145/1498765.1498785
- [21] E. Georganas, S. Avancha, K. Banerjee, D. Kalamkar, G. Henry, H. Pabst, and A. Heinecke, 'Anatomy of high-performance deep learning convolutions on simd architectures,' Proceedings -International Conference for High Performance Computing, Networking, Storage, and Analysis, SC 2018 , pp. 830-841, 2019.
- [22] M. Wolfe, 'Iteration space tiling for memory hierarchies,' in Proceedings of the Third SIAM Conference on Parallel Processing for Scientific Computing . USA: SIAM, 1987, p. 357-361.
- [23] --, 'More iteration space tiling,' in Supercomputing '89:Proceedings of the 1989 ACM/IEEE Conference on Supercomputing , 1989, pp. 655664.
- [24] 'PyTorch,' 2024. [Online]. Available: https://github.com/pytorch/ pytorch
- [25] 'MKL,' 2024. [Online]. Available: https://www.intel.com/content/www/ us/en/developer/tools/oneapi/onemkl.html

## APPENDIX

We leave some information out from the main body of this paper and have them as part of an appendix. Because we consider they are not essential to the main argument but may be useful for readers who want to dive deeper into the topic.

## A. Peak Performance

We use the following formula to calculate the peak-gflops of the server:

$$\frac { \ p e a k _ { f l o p } } { s } = ( \# p r o c e s s o r s ) \times ( \# c o r e s _ { p e r _ { p r o c e s s o r } } ) \\ \times ( c l o c k _ { s p e e d } [ 1 / s ] ) \times ( 2 \times \# F M A _ { u n i t s } ) \times \frac { \ v e c t o r _ { s i z e } [ b i t s ] } { 6 4 } \\ \vdots \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cdot \quad \cd.$$

Based on Equation (4), the peak GFLOPS of the server that we use in our experimental evaluation is 3584 GFLOPS.

## B. Batch Size Scaling on Different layouts

We perform a strong scaling on the batch size from 32, 64, 128, 256 to 512 with different layouts using the direct convolution and the im2win convolution. The performance results of the direct convolution with the CHWN, CHWN8, NCHW, and NHWC layouts are shown in Figure 6, Figure 7, Figure 8, and Figure 9, respectively. The performance results of the im2win convolution with the CHWN, CHWN8, NCHW, and NHWC layouts are shown in Figure 10, Figure 11, Figure 12, and Figure 13, respectively.

From the figures, we can tell that the CHWN layout is most sensitive to the batch size among four layouts. The performance of the direct and im2win convolutions is the best on twelve benchmarks when the batch size is 32 (except for the direct convolution on conv12). Recall in Section III-B, the efficiency of the CHWN layout is constrained by the number of vector registers on the SIMD machines. When N &gt; i 8 , this leads to low cache utilization.

With our proposed CHWN8 layouts, the performance of the direct convolution and the im2win convolution exhibits similar patterns on twelve benchmarks, that is, when the channel sizes of the benchmarks are small ( C i =3 for conv1, conv2, conv3), the smaller the batch size is, the better performance the convolutions have on these benchmarks; when the channel sizes of the benchmarks (conv4-conv12) are large, the larger the batch size is, the better performance the convolution have on these benchmarks.

With the NWHC and NCHW layouts, both convolution methods show no obvious evidence that they are sensitive to the batch size across all benchmarks. The performance variance is contributed by the overall dimension of the input/filter tensors, and the loop coalescing which we coalesce the N i and H o dimensions into one parallel loop to achieve better load balance.

![Image](https://ac.marcodsn.me/arxiv-markdown-images/2408.00278v1-figure-bf49b0ff-472c-4087-94e6-5de9dbe05f2d.jpg)

Fig. 6. The performance of the direct convolution in different batch sizes with the CHWN layout

![Image](https://ac.marcodsn.me/arxiv-markdown-images/2408.00278v1-figure-f2595bd3-7c49-4b30-bda7-39f3b8e67805.jpg)

Fig. 7. The performance of the direct convolution in different batch sizes with the CHWN8 layout

Fig. 8. The performance of the direct convolution in different batch sizes with the NCHW layout

![Image](https://ac.marcodsn.me/arxiv-markdown-images/2408.00278v1-figure-3fcc0a67-a87c-4529-99d6-0fbdf875f924.jpg)

Fig. 9. The performance of the direct convolution in different batch sizes with the NHWC layout

![Image](https://ac.marcodsn.me/arxiv-markdown-images/2408.00278v1-figure-be4dfa49-e09f-4de3-9384-6fec40ed7225.jpg)

![Image](https://ac.marcodsn.me/arxiv-markdown-images/2408.00278v1-figure-c8c9584a-8889-4ba7-96af-fcce6186c234.jpg)

Fig. 10. The performance of the im2win convolution in different batch sizes with the CHWN layout

![Image](https://ac.marcodsn.me/arxiv-markdown-images/2408.00278v1-figure-77e8c603-0bd9-4ec2-8e6a-5e38466d0299.jpg)

Fig. 11. The performance of the im2win convolution in different batch sizes with the CHWN8 layout

Fig. 12. The performance of the im2win convolution in different batch sizes with the NCHW layout

![Image](https://ac.marcodsn.me/arxiv-markdown-images/2408.00278v1-figure-a8543603-34a0-473c-bb03-ea9eb258005e.jpg)

Fig. 13. The performance of the im2win convolution in different batch sizes with the NHWC layout

![Image](https://ac.marcodsn.me/arxiv-markdown-images/2408.00278v1-figure-d73a18cf-648f-4f95-a4e8-12a25ba3bee2.jpg)
Xiang Fu Xinpeng Zhang Jixiang Ma Peng Zhao Shuai Lu Xu T. Liu
High Performance Im2win and Direct Convolutions using Three Tensor Layouts on SIMD Architectures
Convolution is the core component within deep neural networks and it is
computationally intensive and time consuming. Tensor data layouts significantly
impact convolution operations in terms of memory access and computational
efficiency. Yet, there is still a lack of comprehensive performance
characterization on data layouts on SIMD architectures concerning convolution
methods. This paper proposes three novel data layouts for im2win convolution:
NHWC, CHWN, and CHWN8, and introduces a set of general optimization techniques
for both direct and im2win convolutions. We compare the optimized im2win
convolution with the direct convolution and PyTorch's im2col-based convolution
across the aforementioned layouts on SIMD machines. The experiments
demonstrated that the im2win convolution with the new NHWC layout achieved up
to 355% performance speedup over NCHW layout. Our optimizations also
significantly improve the performance of both im2win and direct convolutions.
Our optimized im2win and direct convolutions achieved up to 95% and 94% of
machine's theoretical peak performance, respectively.