optimizing convolutional neural networks on sunway ... · supercomputer abstract: sunway taihulight...

11
Optimizing Convolutional Neural Networks on Sunway TaihuLight Supercomputer Abstract: Sunway TaihuLight supercomputer is announced in June 2016 and currently ranks the first place on the Top 500 List. The supercomputer is powered by the SW26010, which is a new many-core processor designed with on-chip heterogeneous techniques. In this paper, we present our work on exploring the potential of SW26010 architecture for convolutional neural networks, which is one of the most effective deep learning models and involves a large amount of computations in the training tasks. Based on the characteristics of SW26010 processor, we derive a performance model to identify the most suitable approach of mapping convolutional computations onto the many-core architecture. Optimization methods targeting local directive memory usage, vector register usage and instruction pipelines are proposed for further performance improvement, guided by the performance model. With the proposed design and optimization methods, we manage to achieve a double-precision performance of 1.6 TFlops for the convolution computation, up to 54% of the theoretical peak performance. Compared with cuDNN on NVIDIA Tesla K40m GPU, our work results in 1.9 to 9.7 times speedup on performance and 14% improvement on hardware efficiency according to the evaluation with 132 test cases. Key words: Convolutional Neural Network, Deep Learning, Heterogeneous Many-core Architecture, Sunway Taihu- Light Supercomputer 1 Introduction Convolutional neural network (CNN[1]) is one of the most suc- cessful deep learning models. The training process of CNNs in- volves a large amount of computations, and has become one a popular research topics in HPC field. GPUs have currently been considered as the most efficient hardware choice for deep learn- ing tasks, and have widely adopted in both academia and indus- try. However, with the increasing complexity of CNNs, higher demands are put forward on not only processors and accelerators, but also on systems, such as an customized HPC server or even a cluster, where remains challenges for higher efficient solutions than the state-of-art GPUs and GPU-based HPC platforms. Sunway TaihuLight[2], a supercomputer that ranks the first in the world with over 100 PFlops computing capacity, is pow- ered by the SW26010 many-core processor, which is designed with on-chip heterogeneous techniques and can provides a peak double-precision performance of 3.06 TFlops. SW26010 intro- duces a number of unique features that could potentially help the training process of CNNs, such as the user-controlled local direc- tive memory (LDM), the hardware-supported register-level data sharing, and a unified memory space shared by all processing el- ements. To explore the potential, in this paper, we present our work on designing and optimizing the convolution neural network based on SW26010 many-core architecture. The major contributions of this work include: Based on the characteristics of SW26010, we derive a per- formance model to identify the most suitable approach of mapping convolutional computations onto the many-core architecture. We design LDM usage strategies, including double buffer- ing and LDM blocking, to improve the efficiency and to re- duce the required memory bandwidth between main mem- ory and LDM. We design register communication and register blocking strategies to take fully use of the vector registers in the com- puting processing elements and to implement the core com- putation efficiently. Based on the double-pipeline architecture of the computing processing elements, we adopt loop enrolling and instruc- tion re-ordering for the core computation, which improves the execution efficiency of the instruction flow. An evaluation with 132 test cases is presented and the results show that our implementation can provide an average double- precision performance of about 1.6 TFlops, achieving 54% of

Upload: others

Post on 31-Jul-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Optimizing Convolutional Neural Networks on Sunway ... · Supercomputer Abstract: Sunway TaihuLight supercomputer is announced in June 2016 and currently ranks the first place on

Optimizing Convolutional Neural Networks on Sunway TaihuLightSupercomputer

Abstract: Sunway TaihuLight supercomputer is announced in June 2016 and currently ranks the first place on the

Top 500 List. The supercomputer is powered by the SW26010, which is a new many-core processor designed

with on-chip heterogeneous techniques. In this paper, we present our work on exploring the potential of SW26010

architecture for convolutional neural networks, which is one of the most effective deep learning models and involves

a large amount of computations in the training tasks. Based on the characteristics of SW26010 processor, we

derive a performance model to identify the most suitable approach of mapping convolutional computations onto

the many-core architecture. Optimization methods targeting local directive memory usage, vector register usage

and instruction pipelines are proposed for further performance improvement, guided by the performance model.

With the proposed design and optimization methods, we manage to achieve a double-precision performance of 1.6

TFlops for the convolution computation, up to 54% of the theoretical peak performance. Compared with cuDNN on

NVIDIA Tesla K40m GPU, our work results in 1.9 to 9.7 times speedup on performance and 14% improvement on

hardware efficiency according to the evaluation with 132 test cases.

Key words: Convolutional Neural Network, Deep Learning, Heterogeneous Many-core Architecture, Sunway Taihu-Light Supercomputer

1 Introduction

Convolutional neural network (CNN[1]) is one of the most suc-cessful deep learning models. The training process of CNNs in-volves a large amount of computations, and has become one apopular research topics in HPC field. GPUs have currently beenconsidered as the most efficient hardware choice for deep learn-ing tasks, and have widely adopted in both academia and indus-try. However, with the increasing complexity of CNNs, higherdemands are put forward on not only processors and accelerators,but also on systems, such as an customized HPC server or evena cluster, where remains challenges for higher efficient solutionsthan the state-of-art GPUs and GPU-based HPC platforms.

Sunway TaihuLight[2], a supercomputer that ranks the firstin the world with over 100 PFlops computing capacity, is pow-ered by the SW26010 many-core processor, which is designedwith on-chip heterogeneous techniques and can provides a peakdouble-precision performance of 3.06 TFlops. SW26010 intro-duces a number of unique features that could potentially help thetraining process of CNNs, such as the user-controlled local direc-tive memory (LDM), the hardware-supported register-level datasharing, and a unified memory space shared by all processing el-ements.

To explore the potential, in this paper, we present our work on

designing and optimizing the convolution neural network basedon SW26010 many-core architecture. The major contributions ofthis work include:

• Based on the characteristics of SW26010, we derive a per-formance model to identify the most suitable approach ofmapping convolutional computations onto the many-corearchitecture.

• We design LDM usage strategies, including double buffer-ing and LDM blocking, to improve the efficiency and to re-duce the required memory bandwidth between main mem-ory and LDM.

• We design register communication and register blockingstrategies to take fully use of the vector registers in the com-puting processing elements and to implement the core com-putation efficiently.

• Based on the double-pipeline architecture of the computingprocessing elements, we adopt loop enrolling and instruc-tion re-ordering for the core computation, which improvesthe execution efficiency of the instruction flow.

An evaluation with 132 test cases is presented and the resultsshow that our implementation can provide an average double-precision performance of about 1.6 TFlops, achieving 54% of

Page 2: Optimizing Convolutional Neural Networks on Sunway ... · Supercomputer Abstract: Sunway TaihuLight supercomputer is announced in June 2016 and currently ranks the first place on

2 Tsinghua Science and Technology, XXX 2017, 000(0): 000-000

Algorithm 1 Original algorithm of a convolutional layer1: //Assume that IN [Bs][Ni][Ri][Ci], OUT [Bs][No][Ro][Co], CONVW [No][Ni][Kr][Kc] and b[No] are input/output feature maps,

convolutional kernels and bias2: //Kr = Kc = K represent the number of rows and columns of a 2-dimensional convolutional kernel3: //The output images OUT are initialed with the bias b4: for cB := 0 : 1 : Bs do5: for cNo := 0 : 1 : No do6: for cRo := 0 : 1 : Ro do7: for cCo := 0 : 1 : Co do8: for cNi := 0 : 1 : Ni do9: for cKr := 0 : 1 : Kr do

10: for cKc := 0 : 1 : Kc do11: OUT [cB][cNo][cRo][cCo]+ = CONVW [cNo][cNi][Kr − 1− cKr][Kc − 1− cKc]

∗IN [cB][cNi][cRo + cKr][cCo + cKc];12: end for13: end for14: end for15: end for16: end for17: end for18: end for

the theoretical peak performance of SW26010. Compared withcuDNN on NVIDIA Tesla K40m GPU, our work results in 1.9to 9.7 times speedup on performance and 14% improvement onhardware efficiency, which proves the capability of SW26010processor and Sunway TaihuLight supercomputer to traininglarge scale CNNs.

2 Background

2.1 Convolutional Neural NetworksCNNs usually contain multiple computing layers, among whichconvolutional layers usually take the majority of computing time(over 90%) in most of CNNs. Therefore, we focus on the opti-mization of convolutional computation in this paper.

Table 1 Configurations of a convolutional layer

Ni Number of input feature mapsRi Height of an input feature mapCi Width of an input feature mapNo Number of output feature mapsRo Height of an output feature mapCo Width of an output feature mapK Size of convolution kernel

We first give the description of convolutional layer configura-tions listed in Tab. 1. The input data of a convolutional layer con-sists of Ni channels, each of which can be considered as a featuremap with size of Ri×Ci. Similarly, the output of a convolutionallayer consists of No feature maps with size of Ro×Co. To calcu-late the values in an output feature map, Ni convolutional kernelswith size of K × K and 1 bias value are required. Each kernelconvolutes with an input feature map. The output value equals

to the sum of Ni convolution results and the bias value. There-fore, there are Ni × No convolutional kernels and No bias in aconvolutional layer.

The training process of a CNN model is based on the stochas-tic gradient descent (SGD) algorithm. In each training step, thenetwork is trained with a batch of samples. We define the batchsize as Bs, then the original algorithm of a convolutional layerin a training iteration can be described as Algorithm 1. The in-put data, output data and convolution weights are organized in4-dimension tensors and there are 7 nested loops in the algo-rithm, which provides possibilities for parallel optimization onmany-core processors like SW26010.

2.2 SW26010 Many-core Architecture

Figure 1 shows the architecture of SW26010 many-core pro-cessor. Each processor consists of four core groups (CGs) andeach CG includes 65 cores: one management processing element(MPE), and 64 computing processing element (CPEs), organizedas an 8 by 8 mesh. The MPE and CPE are both complete 64-bitRISC cores but serve as different roles in a computing task.

The MPE has 32KB L1 instruction cache, 32KB L1 data cacheand 256KB L2 cache, and supports the complete interrupt func-tions, memory management, superscalar, and out-of-order in-struction issue/execution. The MPE is good at handling the man-agement, task schedule, and data communications.

The CPE is designed for maximizing the aggregated comput-ing throughput while minimizing the complexity of the micro-architecture. Each CPE has 16-KB L1 instruction cache and64KB local directive memory (LDM). The LDM can be consid-ered as a user-controlled fast buffer, which allows orchestratedmemory usage strategies for different implementations, so thatLDM-level optimization is one of the important ways to improvethe computation throughput.

A CPE has 32 vector registers (256 bits) and two execution

Page 3: Optimizing Convolutional Neural Networks on Sunway ... · Supercomputer Abstract: Sunway TaihuLight supercomputer is announced in June 2016 and currently ranks the first place on

First author et al.: Optimizing Convolutional Neural Networks on Sunway TaihuLight Supercomputer 3

Fig. 1 SW26010 architecture

pipelines (P0 and P1). P0 supports scalar and vectorized com-puting operations of both floating-point and integer, while P1supports data load/store, compare, jump operations and scalar in-teger operations. The double-pipeline design provides probabil-ity for the overlapping of data accessing and computation opera-tions. Therefore, register level and instruction level optimizationcan improve the performance of core computation.

Inside the 8 × 8 CPE mesh, there is a control network, adata transfer network (connecting the CPEs to the memory in-terface), 8 column communication buses, and 8 row communi-cation buses. The column and row communication buses enablefast register-level data communication between CPEs in the samecolumn and same row, providing important data sharing and co-operation capability within the CPE mesh.

Each CG connects to a Memory Controller (MC), throughwhich 8GB memory space can be accessed and shared by theMPE and the CPE mesh. The maximum memory bandwidth ofan MC is 36GB/s. An on-chip network (NoC) connects fourCGs, so that the memory of a CG can also be shared to otherCGs. Users can explicitly set the size of each CGs private mem-ory space, and the size of the shared memory space. ThroughNoC, data sharing between four CGs can be implemented with-out memory data copy, which enables highly efficient CG-levelparallelism for communication intensive problems. Under thesharing mode, the maximum memory bandwidth of four CGs isup to 144GB/s.

2.3 Related WorksA straightforward implementation of the original convolution al-gorithm involves strong data dependency in the innermost ac-cumulation computation. To improve the parallelism, differentoptimized implementations are proposed, which can be summa-rized into the following three categories.

• Time-domain transformation methods are first in-troduced in the early phase of CNN optimizationsresearches[3–5]. By lowering the convolution operationinto matrix multiplication, the performance can be im-proved with the help of high efficient BLAS on different

hardware platforms. However, additional data transforma-tion is required, which either consumes more memory spaceand extra data copy operations, or involves complicatedmemory address remapping. Therefore, memory consump-tion and bandwidth are major problems for time-domaintransformation methods, and the overall performance islimited by the performance of BLAS.

• Frequency-domain transformation methods can reducethe arithmetic complexity of convolution operations. FFT-based[6, 7] and Winograd’s filtering[8] based convolutionalgorithm are proposed and performs well in cases withboth large and small convolution kernel sizes. Similar totime-domain based methods, additional FFT transformationis required as well as extra memory consumption, and theoverall performance is limited by the performance of FFT.

• Direct convolution optimization methods can reduce thedata dependency by re-designing the convolution algorithmwith loop reordering and data blocking, so that to improvethe parallelism of the core computation. Instead of re-lying on existing BLAS or FFT libraries, direct convolu-tion implementations require hardware-oriented optimiza-tion methods to take full advantage of the hardware ar-chitecture, and therefore, the overall performance can ap-proach to the peak performance of the processor. More-over, by carefully designing the data blocking strategies,additional data transformation and extra memory consump-tion can be avoided, which is more suitable for memory andbandwidth bounded architectures.

Besides the algorithm optimization, various hardware accel-erators are employed to accelerate the convolution computation,such as GPU, FPGA and ASIC, focusing on both classificationand training process of CNNs. FPGAs[9–11] and ASICs[12–15]are usually used for classification tasks due to the customizabil-ity of data precision, low latency and high energy efficiency.GPUs have currently dominated the competition of the HPC plat-forms for training tasks. Especially, NVIDIA has launched sev-eral deep learning specific GPUs as well as cuDNN[16] library,

Page 4: Optimizing Convolutional Neural Networks on Sunway ... · Supercomputer Abstract: Sunway TaihuLight supercomputer is announced in June 2016 and currently ranks the first place on

4 Tsinghua Science and Technology, XXX 2017, 000(0): 000-000

CPE

Registers

LDM

Theoretical Peak𝑃𝑒𝑟𝑓 = 742.4𝐺𝐹𝑙𝑜𝑝𝑠

46.4GB/s

MBWMEM->LDM

8GB/s

ConsideringregisterRBW

𝑃𝑒𝑟𝑓 = 742.4𝐺𝐹𝑙𝑜𝑝𝑠 · 𝐸𝐸

· 𝑓(𝑚𝑖𝑛 1,46.4GB/s

𝑅𝐵𝑊MNO→QRS

T

)

Theoretical Peak𝑃𝑒𝑟𝑓 = 742.4𝐺𝐹𝑙𝑜𝑝𝑠

Considering EE𝑃𝑒𝑟𝑓 = 742.4𝐺𝐹𝑙𝑜𝑝𝑠 · 𝐸𝐸

ConsideringRBW

𝑃𝑒𝑟𝑓 = 742.4𝐺𝐹𝑙𝑜𝑝𝑠 · 𝐸𝐸 ·

𝑓(𝑚𝑖𝑛 1,8𝐺𝐵/𝑠

𝑅𝐵𝑊ORO→QRS

T

)

GlobalMemoryAccess LDM-cacheMemoryAccess

ConsideringLDMRBW

𝑃𝑒𝑟𝑓 = 42.4𝐺𝐹𝑙𝑜𝑝𝑠 · 𝐸𝐸

· 𝑓(𝑚𝑖𝑛 1,46.4𝐺𝐵/𝑠

𝑅𝐵𝑊MNO→QRS

T

)

· 𝑓(𝑚𝑖𝑛 1,𝑀𝐵𝑊ORO→MNO

𝑅𝐵𝑊ORO→MNO

T

)

MainMemory

Considering EE𝑃𝑒𝑟𝑓 = 742.4𝐺𝐹𝑙𝑜𝑝𝑠 · 𝐸𝐸

Fig. 2 Performance model for one CG

which provides a flexible API for deep learning workloads and isneatly integrated to widely used deep learning frameworks, suchas Caffe[17], Tensorflow[18], etc.

To explore the potential of training CNNs on other off-the-shelf many-core processors, in this paper, we present our workon optimizing CNN algorithm on the SW26010 many-core pro-cessor. Based on the unique architectural features of SW26010,we propose customized direct convolution algorithm with a seriesof optimization methods to improve the performance.

3 Optimization Methods

3.1 Performance modelWe consider different factors that affect the actual performanceof one CG and propose a performance model shown in Fig. 2.The frequency of a CPE is 1.45GHz and the vectorization sizeis 4. Assuming that each CPE executes one vector floating-pointmultiplication and addition(vfmad) instruction, the peak perfor-mance of a CG can be derived as:

2× 4× 1.45× 64 = 742.4GFlops (1)

For an implementation, we define the execution efficiency(EE) as the ratio of vfmad instructions to the total execution cy-cles. Therefore, considering the loss from EE, the theoreticalperformance of an implementation is 742.4GFlops · EE.

Before a computing instruction can be executed, we needto make sure the data has been loaded into registers. For avfmad instruction, 12 double-precision floating point numbers(12 × 64 = 768bits) are needed. In Fig. 2, the required band-width (RBW) of an implementation is defined as the minimumdata access bandwidth that could overlap the data access andcomputation.

A CPE supports two data access patterns to load data intoregisters. One is the global memory access (gload instruction),which means the data is directly loaded from the main memoryto registers. Each gload instruction can load 64 bits data into

a scalar register. In this case, to guarantee the computation anddata access can be fully overlapped, the data accessed by a gloadinstruction should be involved in at least 12 vfmad instructions(768bits : 64bits). Here we define the computation to data ac-cess ratio (CDR), which represents the ratio of computation in-structions (vfmad) to data access instructions. As we can see, inglobal memory access pattern, to overlap the computation anddata access, CDR should be greater than 12, which can hardly bemet by most algorithms. Therefore, global memory access pat-tern is relatively low efficient. The performance model of globalmemory access pattern is shown in Fig. 2. The maximum mem-ory bandwidth of one CG is about 8GB/s. We donate the RBWas RBWMEM−>REG. f(·) is a monotone increasing functionwith f(1) = 1, which describes how the bandwidth limitationaffects the performance.

The other memory access pattern is to use LDM as a datacache, which means the data will be loaded first from the mainmemory into LDM, and then from LDM into registers. There aretwo stages of data accessing in this case. We donate the RBWof both stages as RBWMEM−>LDM and RBWLDM−>REG.When loading data from LDM to registers, vectorized load in-struction (vload) is supported. Each vload instruction can load256-bit (32Bytes) data into a vector register, and can be issuein every cycle, so the bandwidth between LDM and register is32Bytes× 1.45GHz = 46.4GB/s.

Data is transferred from main memory to LDM through directmemory access interface (DMA), and the theoretical maximumbandwidth of DMA is 36GB/s. However, the actual bandwidthis not a constant value and is variant with the size of continuousmemory access blocks of one CPE. We write a micro-benchmarkon one CG to measure the actual DMA bandwidth and presentthe results in Tab. 2, where Size indicates the size of continuousmemory access data block of one CPE. We donate the measuredDMA bandwidth as MBWMEM−>LDM . We can see that thebandwidth of DMA ranges from 4 GB/s to 36 GB/s. In general, a

Page 5: Optimizing Convolutional Neural Networks on Sunway ... · Supercomputer Abstract: Sunway TaihuLight supercomputer is announced in June 2016 and currently ranks the first place on

First author et al.: Optimizing Convolutional Neural Networks on Sunway TaihuLight Supercomputer 5

Algorithm 2 Matrix-multiplication-based convolution algorithm1: //Assume that IN [Bs][Ni][Ri][Ci], OUT [Bs][No][Ro][Co], CONVW [No][Ni][Kr][Kc] and b[No] are input/output feature maps,

convolutional kernels and bias2: //Kr = Kc = K represent the number of rows and columns of a 2-dimensional convolutional kernel3: //The output images OUT are initialed with the bias b4: for cRo := 0 : 1 : Ro do5: for cCo := 0 : 1 : Co do6: Do[0 : No][0 : Bs] = (OUT [0 : Bs][0 : No][cRo][cCo])

T

7: for cKr := 0 : 1 : Kr do8: for cKc := 0 : 1 : Kc do9: //Core computation: Do+ = W ×Di

10: W [0 : No][0 : Ni] = CONVW [0 : No][0 : Ni][K − 1− cKr][K − 1− cKc]

11: Di[0 : Ni][0 : Bs] = (IN [0 : Bs][0 : Ni][cRo + cKr][cCo + cKc])T

12: for cNo := 0 : 1 : No do13: for cB := 0 : 1 : Bs do14: for cNi := 0 : 1 : Ni do15: Do[cNo][cB]+ = W [cNo][cNi]×Di[cNi][cB]

16: end for17: end for18: end for19: end for20: end for21: OUT [0 : Bs][0 : No][cRo][cCo] = (Do[0 : No][0 : Bs])

T

22: end for23: end for

higher bandwidth is achieved when using a block size larger than256B and aligned in 128B.

Table 2 Measured DMA Bandwidth on one CG(GB/s)

Size(Byte) Get Put Size(Byte) Get Put

32 4.31 2.56 512 27.42 30.3464 9.00 9.20 576 25.96 28.91

128 17.25 18.83 640 29.05 32.00192 17.94 19.82 1024 29.79 33.44256 22.44 25.80 2048 31.32 35.19384 22.88 24.67 4096 32.05 36.01

Figure 2 also shows the performance model of LDM-cachememory access pattern. The required CDR is 3 (768bits :256bits), which is easier to be accomplished compared with theglobal memory access pattern. Therefore, our design is basedon LDM-cache memory access pattern. According to the per-formance model, we propose optimization methods to achievethe overlap of computation and data access, to increase theMBWMEM−>LDM , EE and reduce the RBWMEM−>LDM

and RBWLDM−>REG.

3.2 Algorithm DesignConsidering the original algorithm of a convolutional layer (Al-gorithm 1), the inner loops perform a K ×K convolution. Usu-ally, the value of K is relatively small and is odd, like 3, 5, 7, etc.Therefore, it is hard to map the inner loops onto the CPE meshand is also inefficient for the vectorization of core computation.

To improve parallelism, we re-scheduled the 7 nested loops,making the inner computation to be a matrix multiplication with

dimensions Ni, No and Bs, which are relatively large in mostconvolution layers and are suitable for mapping the inner com-putation onto the CPE mesh. Algorithm 2 shows the optimizedalgorithm based on matrix multiplication. To complete the com-putation of an output matrix of size No × Bs (Do), each CPEis responsible for a block of size No

8× Bs

8. Correspondingly,

the input data of a CPE includes a tile of the input matrix W

(of size No8

× Ni) and a tile of the input matrix Di (of sizeNi × Bs

8), both of which can be shared between the CPEs ei-

ther in the same row or in the same column. Therefore, for thecore computation, the amount of data to be accessed by a CPEis (Ni × No

8+ Ni × Bs

8+ No

8× Bs

8). The amount of vmadd

instructions is (Ni × No8

× Bs8)/4. We use vload instruction for

data access, so the theoretical CDR of the core computation is:(Ni × No

8× Bs

8)/4

(Ni × No8

+Ni × Bs8

+ No8

× Bs8)/4

(2)

Assuming that Ni, No, and Bs have the same value, the CDRcan meet the requirement (CDR ≥ 3) of LDM-cache patternwhen the value is larger than 51, which can be realized in mostof the convolution layers. For values which are not multiple of8, zero padding can be adopted and will not cause too much de-crease on the performance. Therefore, for the sake of brevity, wefocus on the configurations that are multiple of 8 in the follow-ing discussion. The following subsections will show the detailedimplementation and optimization methods based on Algorithm 2.

3.3 LDM-Related OptimizationLDM-related optimization methods are focused on an effectiveimplementation for outer loops of the algorithm. The targets areto realize the overlap of data access from main memory to LDM

Page 6: Optimizing Convolutional Neural Networks on Sunway ... · Supercomputer Abstract: Sunway TaihuLight supercomputer is announced in June 2016 and currently ranks the first place on

6 Tsinghua Science and Technology, XXX 2017, 000(0): 000-000

Algorithm 3 Optimized algorithm with LDM blocking1: //Assume that IN [Bs][Ni][Ri][Ci], OUT [Bs][No][Ro][Co], CONVW [No][Ni][Kr][Kc] and b[No] are input/output feature maps,

convolutional kernels and bias2: //Kr = Kc = K represent the number of rows and columns of a 2-dimensional convolutional kernel3: //The output images OUT are initialed with the bias b4: for cRo := 0 : 1 : Ro do5: for cCo := 0 : bC : Co do6: Do[0 : bC ][0 : No][0 : Bs] = OUT [cRo][cCo : cCo + bC ][0 : No][0 : Bs]

7: for cKr := 0 : 1 : Kr do8: for cKc := 0 : 1 : Kc do9: W [0 : No][0 : Ni] = CONVW [cKr][cKc][0 : No][0 : Ni]

10: Di[0 : bC ][0 : Ni][0 : Bs] = IN [cRo + cKr][cCo + cKc : cCo + cKr + bC ][0 : Ni][0 : Bs]

11: //Core computation: Do[0 : bC ]+ = W ×Di[0 : bC ]

12: for cbC := 0 : 1 : bC do13: for cNo := 0 : 1 : No do14: for cB := 0 : 1 : Bs do15: for cNi := 0 : 1 : Ni do16: Do[cbC ][cNo][cB]+ = W [cNo][cNi]×Di[cbC ][cNi][cB]

17: end for18: end for19: end for20: end for21: end for22: end for23: OUT [cRo][cCo : cCo + bC ][0 : No][0 : Bs] = Do[0 : bC ][0 : No][0 : Bs]

24: end for25: end for

and the core computation of the CPE mesh, so that to increaseMBWMEM−>LDM and reduce RBWMEM−>LDM .

3.3.1 Optimized Data LayoutThe input data of the core computation are part of the in-put/output feature maps and the convolutional kernels. Basedon the original data layout, data in W , Di, Do is notstored continuously in IN , OUT and CONVW , so that theMBWMEM−>LDM will be limited due to small data accessblock. To increase MBWMEM−>LDM , we re-designed thedata layout of the input/output feature maps and the convolu-tional kernels as IN [Ri][Ci][Ni][Bs], OUT [Ro][Co][No][Bs],and CONVW [Kr][Kc][No][Ni]. Besides, we rotated the con-volutional kernels on Kr and Kc dimensions to eliminate the co-ordinate transform in line 8 of Algorithm 2. For IN and OUT ,we put Bs as the lowest dimension, which can eliminate the datatransposition in line 9 and 10 of Algorithm 2, and can supportvectorized operations on Bs dimension in the core computation.

3.3.2 Double BufferingDouble buffering is adopted to overlap the data access from mainmemory to LDM and the core computation. Because DMA isasynchronized, we design two LDM buffers of the same size.While the data in one buffer is used for core computation, thedata to be used at next computation iteration can be loaded intoanother buffer. The double buffering design halves the maximumavailable space of LDM for one computation iteration, whichmeans for one CPE, only 32KB LDM is available for the corecomputation.

3.3.3 LDM BlockingWe consider the LDM usage of the core computation with differ-ent convolutional layer configurations. It can be described as:

(Ni ×No +Ni ×Bs +No ×Bs)×DataLen (3)

where DataLen is the number of bytes for the data type. As-suming Ni, No, and Bs are equal to 256, which are relativelylarge configurations for most convolutional layers, the LDM us-age of each CPE is 24KB. Therefore, for most convolutional lay-ers, 32KB LDM is enough for the core computation, and in otherwords, it is possible to take advantage of the remaining LDMspaces to improve the overall performance of the implementa-tion.

In the convolution algorithm, the convolutional kernel isshared by the computation of values in the same output image.In the core computation of Algorithm 2, the data of convolutionalkernel (W ) is only used for one matrix multiplication computa-tion corresponding to the values in the output feature maps withcoordinate (cRo, cCo). To improve the data reuse of W , and inthe meantime to improve the CDR of the core computation, wepropose an LDM blocking strategy shown in Algorithm 3.

In the core computation of Algorithm 3, we load bC timesmore data of input/output feature maps and reuse the data of con-volutional kernels to complete bC matrix-multiplication compu-tation. The RBWMEM−>LDM is reduced and the CDR of aCPE is:

bC ×Ni × No8

× Bs8/4

(Ni × No8

+ bC ×Ni × Bs8

+ bC × No8

× Bs8)/4

(4)

Page 7: Optimizing Convolutional Neural Networks on Sunway ... · Supercomputer Abstract: Sunway TaihuLight supercomputer is announced in June 2016 and currently ranks the first place on

First author et al.: Optimizing Convolutional Neural Networks on Sunway TaihuLight Supercomputer 7

which is greater than Equation (2). The larger bC we choose, thegreater CDR we can get. On the other hand, bC is limited by theavailable size of LDM, and we can maximize the value to takefully advantage of the LDM.

3.4 Register-Related OptimizationRegister-related optimization methods are mainly focused on ef-fectively mapping the core computation onto 8 × 8 CPE mesh.Two key problems are targeted in our work: (i) to realize theregister-level data sharing between CPEs, so that to reduce theRBWLDM−>REG for each CPE; (ii) to take fully use of the vec-tor register to implement the computation efficiently on a CPE.

3.4.1 Register CommunicationAs discussed in Section 3.2, a CPE is responsible for a No

8× Bs

8

block of Do, and requires an No8

×Ni tile of W and an Ni × Bs8

tile of Ni. For the matrix multiplication computation, CPEs inthe same row of the mesh share the tile of W , and CPEs inthe same row of the mesh share the tile of Ni, which perfectlymatches the register communication feature of the CPE mesh.However, there are limitations of the register communication fea-ture: (i) the send and receive buffers designed for register com-munication are just FIFOs with limited size (4 × 256bits); (ii)the data received though the register communication buses hasno information of which CPE it is from; (iii) if the send bufferand receive buffer are both full, the CPE at the sending end willhalt.

Considering the limitations, we carefully design a registercommunication strategy for the matrix multiplication computa-tion. For simplicity, we take a 4× 4 CPE mesh as an example tointroduce the design, shown in Fig. 3. We label the CPEs withcoordinates (0, 0)–(3, 3) from top left to bottom right. Di, Wand Do are divided into 4× 4 parts, and are labeled as Di(0, 0)–Di(3, 3), W (0, 0)–W (3, 3) and Do(0, 0)–Do(3, 3). For a givenpair of (i, j), the computation of Do(i, j) can be described as:

Do(i, j)+ =

3∑k=0

W (i, k)×Di(k, j) (5)

which can be done in 4 steps by CPE(i, j). Di(i, j), W (i, j)

and Do(i, j) are pre-loaded into the LDM of CPE(i, j) beforeexecuting the core computation. Without loss of generality, wetake CPE(2, 1) as an example to show the process.

Fig. 3 Register communication example on 4×4 CPE mesh

• Step 0 First, for all j ∈ {0, 1, 2, 3}, CPE(0, j) loads dataof Di(0, j) from LDM and send the data to other CPEsin the same column by register communication. Thus,CPE(2, 1) can receive the data of Di(0, 1). Then, for alli ∈ {0, 1, 2, 3}, CPE(i, 0) loads data of W (i, 0) from LDMand send the data to CPEs in the same row. CPE(2, 1)can receive the data of W (2, 0). Do(2, 1) can be loadedfrom the LDM of CPE(2, 1), so that the computation ofDo(2, 1)+ = W (2, 0)×Di(0, 1) can be done.

• Step 1 First, CPEs with coordinates (1, j) load data ofDi(1, j) from LDM and send the data to CPEs in thesame column. Then, CPEs with coordinates (i, 1) loaddata of W (i, 1) and send CPEs in the same row. Thus,CPE(2, 1) can receive the data of Di(1, 1) through col-umn register communication, and can load W (2, 1) andDo(2, 1) from LDM, so that to compute Do(2, 1)+ =

W (2, 1)×Di(1, 1).

• Step 2 CPEs with coordinates (2, j) and (i, 2) load the dataof D(2, j) and W (i, 2), and send to the same column andsame row respectively. Then, CPE(2, 1) can receive thedata of W (2, 2) through row register communication andload W (2, 2) and Do(2, 1) from LDM. The computationof Do(2, 1)+ = W (2, 2)×Di(2, 1) can be done.

• Step 3 Similarly, CPEs with coordinates (3, j) and (i, 3)

load and send the data of D(3, j) and W (i, 3) respec-tively. Correspondingly, CPE(2, 1) can receive W (2, 3)

and Di(3, 1) through row and column register communi-cation, and finally finish the computation of Do(2, 1)+ =

W (2, 3)×Di(3, 1).

Based on the proposed register communication strategy, thecore computation can be done on 8 × 8 CPE mesh following8 steps, and meanwhile, highly efficient data sharing betweenCPEs is achieved.

3.4.2 Register BlockingIn each step of the register communication process, the compu-tation task of a CPE is to calculate the matrix multiplication ofW (i, j) and Di(i, j). The size of the blocks are (No

8× Ni

8) and

(Ni8

× Bs8) respectively.

For each CPE, there are only 32 vector register, including zeroregister and stack pointer (sp) register, which means the num-ber of available registers is less than 30 for the implementation.Besides, we should consider to use vectorized computation, toimprove the data reuse in registers, and to reduce the data depen-dency in order to achieve efficient instruction flow. Therefore,we propose a register blocking strategy to implement the compu-tation in each step. Figure 4 shows the details.

We use 4 vector registers to load Di, denoted as A[0 : 3],and 4 vector registers to load W , denoted as B[0 : 3]. 16 vectorregisters are used for storing the data of Do, denoted as C[0 : 15].We define the following process as a kernel task of the registerblocking design:

• First, we load 16 values in a row of D(i, j) into A[0:3],which can be done by 4 vload instructions. We load 4 val-

Page 8: Optimizing Convolutional Neural Networks on Sunway ... · Supercomputer Abstract: Sunway TaihuLight supercomputer is announced in June 2016 and currently ranks the first place on

8 Tsinghua Science and Technology, XXX 2017, 000(0): 000-000

No/8 x Ni/8

B[0:3]

A[0:3]

Ni/8 x Bs/8

C[0:15]

Fig. 4 Register blocking strategy on one CPE

ues in a column of W (i, j) and duplicate the values to fillB[0:3], which can be done by 4 vlde instructions.

• Second, we load 4 × 16 values in Do(i, j) into C[0:15],which can be done by 16 vload instructions.

• Third, for i, j ∈ {0, 1, 2, 3}, we calculate:

C[i+ 4 ∗ j]+ = A[i]×B[j] (6)

which can be done by 16 vfmad instructions.

24 registers are used in the kernel task. As we can see fromFig. 4, to finish the calculation of 4 × 16 values of Do(i, j),Ni8

kernel tasks are required. During this process, A[0:3] andB[0:3] are reloaded for Ni

8times while C[0:15] only need to be

loaded once in the first kernel task, which improves the data reuseat register level, and thus, reduces RBWLDM−>REG. Becausethere is no data dependency between the vfmad instructions ina kernel task, one instruction can be issued in each CPU cycle,which can increase EE of the implementation.

3.5 Instruction-Related OptimizationWe adopt instruction-related optimization methods to overlap thedata loading and computation instructions and to further improvethe EE in the kernel task. Figure 5(a) shows the instruction flowbased on a direct implementation of the kernel task. It takes 26CPU cycles to issue the instructions, among which, there are 16vfmad instructions. The EE is 16/26 = 61.5%. As we cansee, in cycle 4, 8, 23 and 24, two instructions can be issued topipeline P0 and P1 simultaneously, because there is no data de-pendency and the instructions can be executed on P0 and P1 sep-arately. Only data loading instructions (vldr can load the datainto a vector register and send out through row register commu-nication) are issue in the first few cycles, which will lower theEE of the implementation.

Considering that Ni8

kernel tasks are required to calculate a4 × 16 block of Do(i, j), we unroll the Ni

8kernel tasks and re-

order the instructions to overlap the vldr instructions of a ker-nel task with the vfmad instructions at the end of the previouskernel task. The implementation after loop unrolling and instruc-tions reordering shows in Fig. 5(b), where only 17 CPU cyclesare required to finish a kernel task and the EE is improved to16/17 = 94.1%.

Fig. 5 Instruction-related optimization for the kernel task

3.6 Core-Group Level Parallel SchemeBased on the above optimization methods, the convolution algo-rithms can be mapped onto a CG efficiently. Consider there are4 CGs in a SW26010 processor, we can further design the par-allel scheme on 4 CGs. The simplest but most efficient way isto introduce parallel on the outermost loop (Ro). As discussedin Section 2.2, data can be shared by 4 CGs without extra datacopy. Therefore, we can set the data of input/output feature maps,convolutional kernel and bias to shared mode, and implement afour-CG convolution algorithms as shown in Algorithm 4.

4 Results and DiscussionBecause the arithmetic architecture of SW26010 is designed forscientific computation and does not provide an easy doubling orquadrupling of the performance by using single or even half pre-cision, we use double-precision floating-point for evaluation.

Different convolutional layer configurations, including Bs andthose listed in Table 1, will lead to different performance ofthe implementation. Since the configurations change with CNNmodels and applications, it is unnecessary to traverse all possi-bilities. Therefore, we derive the test cases according to the fol-lowing analysis.

First, Ro and Co range from tens to hundreds in different mod-els and different layers. However, in our implementation, Ro

and Co determine the length of the outer loops, which will notaffect the performance of the core computation. Therefore, toshow the optimization results on the core computation, we setRo = Co = 64 as constant configurations in our test cases.

Page 9: Optimizing Convolutional Neural Networks on Sunway ... · Supercomputer Abstract: Sunway TaihuLight supercomputer is announced in June 2016 and currently ranks the first place on

First author et al.: Optimizing Convolutional Neural Networks on Sunway TaihuLight Supercomputer 9

Algorithm 4 4-CG implementation of convolution algorithm1: //Assume that IN [Bs][Ni][Ri][Ci], OUT [Bs][No][Ro][Co], CONVW [No][Ni][Kr][Kc] and b[No] are input/output feature maps,

convolutional kernels and bias2: //Kr = Kc = K represent the number of rows and columns of a 2-dimensional convolutional kernel3: //The output images OUT are initialed with the bias b4: //Parallel execution on 4 CGs5: for cg := 0 : 1 : 4 do6: for cRo := 0 : 1 : Ro

4do

7: for cCo := 0 : bC : Co do8: Do[0 : bC ][0 : No][0 : Bs] = OUT [cg × Ro

4+Ro][cCo : cCo + bC ][0 : No][0 : Bs]

9: for cKr := 0 : 1 : Kr do10: for cKc := 0 : 1 : Kc do11: W [0 : No][0 : Ni] = CONVW [cKr][cKc][0 : No][0 : Ni]

12: Di[0 : bC ][0 : Ni][0 : Bs] = IN [cg × Ro4

+ cRo + cKr][cCo + cKc : cCo + cKr + bC ][0 : Ni][0 : Bs]

13: for cbC := 0 : 1 : bC do14: //Core computation:15: Do[cbC ][:][:]+ = W [:][:]×Di[cbC ][:][:]

16: end for17: end for18: end for19: end for20: OUT [cg × Ro

4][cCo : cCo + bC ][0 : No][0 : Bs] = Do[0 : bC ][0 : No][0 : Bs]

21: end for22: end for

Bs is not a configuration of a CNN model, but is related tothe training process. Either too large or too small will affect theconvergence of the training process. We choose Bs = 128 asa constant value, which is feasible for both stand-alone and dis-tributed training.

Algorithm 5 Configuration generation algorithm of Set 11: Ni = 128, Bs = 128 ,Ro = 64, Co = 64

2: for No = 128;No <= 256;No+ = 64 do3: for K = 3;K <= 21;K+ = 2 do4: CONV (Bs, Ni, No, Ro, Co,K);5: end for6: end for

We observe different scenarios for K, Ni, and No. In the ini-tial layers of a CNN model, the size of input/output feature mapsis large. In order not to involve too much computation and mem-ory consumption, K is usually set to a relatively large value (suchas 11), and Ni, No are relatively small (such as 64). For the fol-lowing layers , the sizeof input/output feature maps is small, thusK goes smaller (e.g. 3), and Ni, No are getting larger (e.g. 128,256 or 384), so that to provide more features for the classificationlayer. From the above, we generate two sets of test cases usingAlgorithm 5 and Algorithm 6, considering the practical config-urations for K, Ni, and No. For comparison, we run the testcases with both our implementation on SW26010 processor andthe convolution subroutine of cuDNN(v5.1) on NVIDIA K40mGPU.

Algorithm 5 generates Set 1, which is designed to show theperformance with different values of K. The results are shownin Fig. 6, where the test cases are numbered as 1 to 30 following

Fig. 6 Double-precision performance results of our imple-mentation for different filter sizes ranging from 3 × 3 to21× 21, compared with the K40m GPU with cuDNNv5.

the order that generated.Algorithm 6 generates Set 2, which is designed to show the

performance with different Ni and No. The results are shown inFig. 7 and the test cases are numbered as 1 to 102, among which,No. 1 to No. 21 have smaller Ni and No values, and No. 22 toNo. 102 have larger Ni and No values.

As we can see from Fig. 6 and Fig. 6, the performance ofour implementation decreases a little when Ni, No and K aresmall, but in general, it is relatively stable under different param-eter configurations. The average double-precision performanceis around 1.6 TFlops, which is about 54% of the peak perfor-mance of SW26010 processor. As a contrast, the performance ofcuDNN changes appreciably with different values of No, but isnot very sensitive to Ni and K, which means the optimization

Page 10: Optimizing Convolutional Neural Networks on Sunway ... · Supercomputer Abstract: Sunway TaihuLight supercomputer is announced in June 2016 and currently ranks the first place on

10 Tsinghua Science and Technology, XXX 2017, 000(0): 000-000

Fig. 7 Double-precision performance results of our convolution kernels with different (Ni,No) ranging from (64,64) to (384,384), compared with the K40m GPU results with cuDNNv5.

Algorithm 6 Configuration generation algorithm of Set 21: K = 3, Bs = 128, Ro = 64, Co = 64

2: Test case No. 1 to No. 213: for Ni = 64;Ni <= 128;Ni+ = 32 do4: for No = 64;No <= 256;No+ = 32 do5: CONV (Bs, Ni, No, Ro, Co,K);6: end for7: end for8:

9: Test case No. 22 to No. 10210: for Ni = 128;Ni <= 384;Ni+ = 32 do11: for No = 128;No <= 384;No+ = 32 do12: CONV (Bs, Ni, No, Ro, Co,K);13: end for14: end for

methods related to the loop of No are adopted by the cuDNNimplementation. The average double-precision performance ofcuDNN on K40m is less than 600GFlops, which is around 40%of the peak performance (1.43TFlops). Therefore, comparedwith cuDNN on K40m, our implementation can achieve 1.9 to9.7 times speedup on performance and about 14% improvementon hardware efficiency.

5 ConclusionsIn this paper, we present our work on optimizing the con-volutional neural network on SW26010 many-core processor,which is designed with on-chip heterogeneous techniques and isadopted in the new announce Sunway TaihuLight supercomputer.We first derive a performance model based on the characteristicsof SW26010 processor. Then we re-designed the algorithm of theconvolutional operation to efficiently map the computations ontothe many-core architecture. To further explore an optimized im-plementation, we propose optimization methods targeting LDMusage, vector register usage and instruction pipeline, guided bythe performance model. With the proposed design and optimiza-tion, we manage to achieve an average double-precision perfor-mance of 1.6 TFlops (54% of the peak performance) under 132different test cases. Compared with cuDNN on NVIDIA TeslaK40m GPU, our work results in 1.9 to 9.7 times speedup and

about 14% improvement of efficiency. Our work provides possi-bilities for training large convolutional neural networks on Sun-way TaihuLight supercomputer, taking advantage of the strongcomputation capabilities and high efficient distributed data com-munication.

References

[1] Krizhevsky, A., Sutskever, I., Hinton, G. E. (2012). Im-agenet classification with deep convolutional neural net-works. In Advances in neural information processing sys-tems (pp. 1097-1105).

[2] Haohuan Fu, Junfeng Liao, et al. The sunway taihulight su-percomputer: system and applications. Science China In-formation Sciences, pages 116, 2016.

[3] Kumar Chellapilla, Sidd Puri, Patrice Simard, et al. Highperformance convolutional neural networks for documentprocessing. In Workshop on Frontiers in HandwritingRecognition, 2006.

[4] Sharan Chetlur, Cliff Woolley, et al. cudnn: Efficient prim-itives for deep learning. arXiv preprint arXiv:1410.0759,2014.

[5] Andrew Lavin. maxdnn: an efficient convolution kernel fordeep learning with maxwell gpus. arXiv:1501.06633, 2015.

[6] Nicolas Vasilache, Jeff Johnson, at al. Fast convolutionalnets with fbfft: A gpu performance evaluation. arXivpreprint arXiv:1412.7580, 2014.

[7] Michae l Mathieu, Mikael Henaff, and Yann LeCun. Fasttraining of convolutional networks through ffts. CoRR,abs/1312.5851, 2013.

[8] Andrew Lavin. Fast algorithms for convolutional neuralnetworks. arXiv preprint arXiv:1509.09308, 2015.

[9] Chen Zhang, Peng Li, Guangyu Sun, Yijin Guan, etal. Opti- mizing fpga-based accelerator design for deepconvolutional neural networks. In Proceedings of the2015 ACM/SIGDA In- ternational Symposium on Field-Programmable Gate Arrays, pages 161170. ACM, 2015.

[10] Jiantao Qiu, Jie Wang, et al. Going deeper with embeddedfpga platform for convolutional neural network. In Proceed-ings of the 2016 ACM/SIGDA International Symposium onField-Programmable Gate Arrays, pages 2635. ACM, 2016.

Page 11: Optimizing Convolutional Neural Networks on Sunway ... · Supercomputer Abstract: Sunway TaihuLight supercomputer is announced in June 2016 and currently ranks the first place on

First author et al.: Optimizing Convolutional Neural Networks on Sunway TaihuLight Supercomputer 11

[11] Chen Zhang, Di Wu, Jiayu Sun, at al. Energy-efficientcnn implementation on a deeply pipelined fpga cluster. InProceedings of the 2016 International Symposium on LowPower Electronics and Design, pages 326331. ACM, 2016.

[12] Tianshi Chen, Zidong Du, et al. Diannao: A small-footprint high-throughput accelerator for ubiquitousmachine-learning. In ACM Sigplan Notices, volume 49,pages 269284. ACM, 2014.

[13] Yunji Chen, Tao Luo, Shaoli Liu, et al. Dadiannao: Amachine-learning supercomputer. In Proceedings of the47th Annual IEEE/ACM International Symposium onMicroarchi- tecture, pages 609622. IEEE Computer Soci-ety, 2014.

[14] Daofu Liu, Tianshi Chen, et al. Pudiannao: A polyvalentmachine learning accelerator. In ACM SIGARCH Com-puter Architecture News, volume 43, pages 369381. ACM,2015.

[15] Zidong Du, Robert Fasthuber, et al. Shidiannao: shiftingvision processing closer to the sensor. In ACM SIGARCHComputer Architecture News, volume 43, pages 92104.ACM, 2015.

[16] Sharan Chetlur, Cliff Woolley, et al. cudnn: Efficient prim-itives for deep learning. arXiv preprint arXiv:1410.0759,2014.

[17] Yangqing Jia, Evan Shelhamer, Jeff Donahue, et al. Caffe:Convolutional architecture for fast feature embedding. InProceedings of the 22nd ACM international conference onMultimedia, pages 675678. ACM, 2014.

[18] Martn Abadi, Ashish Agarwal, et al. Tensorflow: Large-scale machine learning on heterogeneous distributed sys-

tems. arXiv preprint arXiv:1603.04467, 2016.