Rubik's Optical Neural Networks: Multi-task Learning with Physics-aware
  Rotation Architecture
Recently, there are increasing efforts on advancing optical neural networks
(ONNs), which bring significant advantages for machine learning (ML) in terms
of power efficiency, parallelism, and computational speed. With the
considerable benefits in computation speed and energy efficiency, there are
significant interests in leveraging ONNs into medical sensing, security
screening, drug detection, and autonomous driving. However, due to the
challenge of implementing reconfigurability, deploying multi-task learning
(MTL) algorithms on ONNs requires re-building and duplicating the physical
diffractive systems, which significantly degrades the energy and cost
efficiency in practical application scenarios. This work presents a novel ONNs
architecture, namely, \textit{RubikONNs}, which utilizes the physical
properties of optical systems to encode multiple feed-forward functions by
physically rotating the hardware similarly to rotating a \textit{Rubik's Cube}.
To optimize MTL performance on RubikONNs, two domain-specific physics-aware
training algorithms \textit{RotAgg} and \textit{RotSeq} are proposed. Our
experimental results demonstrate more than 4$\times$ improvements in energy and
cost efficiency with marginal accuracy degradation compared to the
state-of-the-art approaches.
Yingjie Li, Weilu Gao, Cunxi Yu
# Rubik's Optical Neural Networks:

###### Abstract

Recently, there are increasing efforts on advancing optical neural networks (ONNs), which bring significant advantages for machine learning (ML) in terms of power efficiency, parallelism, and computational speed. With the considerable benefits in computation speed and energy efficiency, there are significant interests in leveraging ONNs into medical sensing, security screening, drug detection, and autonomous driving. However, due to the challenge of implementing reconfigurability, deploying multi-task learning (MTL) algorithms on ONNs requires re-building and duplicating the physical diffractive systems, which significantly degrades the energy and cost efficiency in practical application scenarios. This work presents a novel ONNs architecture, namely, _RubikONNs_, which utilizes the physical properties of optical systems to encode multiple feed-forward functions by physically rotating the hardware similarly to rotating a _Rubik's Cube_. To optimize MTL performance on RubikONNs, two domain-specific physics-aware training algorithms _RotAgg_ and _RotSeq_ are proposed. Our experimental results demonstrate more than 4\(\times\) improvements in energy and cost efficiency with marginal accuracy degradation compared to the state-of-the-art approaches.

## 1 Introduction

Recently, use of Deep Neural Networks (DNNs) shows significant advantages in many applications, including large-scale computer vision, natural language processing, and data mining tasks. However, DNNs have substantial computational and memory requirements, which greatly limit their training and deployment in resource-constrained (e.g., computation, I/O, and memory bounded) environments [18, 19, 20, 21, 22]. More importantly, it is identified that training large DNN models produces significant carbon dioxide, e.g., recent studies estimated 626,000 pounds of planet-warming carbon dioxide, equal to the lifetime emissions of five cars, produced in training Transformer network [23]. As models grow bigger, their demand for computing increases, as well as the carbon footprint produced by those computations. To address these challenges and make the computation more eco-friendly, there has been a significant trend in building novel high-performance DNNs platforms, especially the increasing efforts on implementing novel DNNs in optical domain, i.e., optical neural network (ONNs) that mimic conventional feed-forward neural network functions using light propagation [15, 24, 25, 26, 27, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]. Unlike directly accelerating conventional DNNs, algorithms for training and deploying ONNs need to be customized in order to precisely represent the whole physics properties of light propagation. Specifically, the equivalent numerical representations of inputs, intermediate results, and propagation functions in optical domain are complex values and complex-valued functions. Additionally, due to the limitations from nature physics, implementing reconfigurability and deploying multi-task learning (MTL) algorithms on many ONNs systems requires re-building and duplicating the physical hardware systems, which significantly degrades the energy and cost efficiency in practical application scenarios.

This work proposes a novel architecture **RubikONNs**, which utilizes the physical properties of optical systems to encode multiple feed-forward functions by physically rotating the systems similarly as rotating a _Rubik's Cube_. With the realization of MTL in optical systems, the computational carbon footprint can be significantly reduced while maintaining the system performance. The paper is organized as follows: in Section 2, we introduce Diffractive Deep Neural Networks (D\({}^{2}\)NN) and its physical implementations; in Section 3, we first formulate the forward functions in D\({}^{2}\)NN systems for MTL. Furthermore, to optimize the MTL performance of RubikONNs, we propose two novel domain-specific physics-aware training algorithms, **RotAgg** and **RotSeq**; in Section 4, we demonstrate four-task MTL on RubikONNs with implementation cost and energy efficiencies improved more than **4\(\times\)**. Finally, a comprehensive RubikONNs design space exploration analysis and explainability are provided to offer concrete design methodologies for practical uses.
## 2 Background

**Diffractive Deep Neural Networks (D\({}^{2}\)NN)** - Recently, there are increasing efforts on optical neural networks and optical computing based DNNs hardware, which bring significant advantages for machine learning systems in terms of their power efficiency, parallelism, and computational speed, demonstrated at various optical computing systems by [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]. Among them, free-space _diffractive deep neural networks_ (D\({}^{2}\)NNs), which is based on the light diffraction and phase modulation of the light signal provided by diffractive layers (L1-L5 in Figure 1), featuring millions of neurons in each layer interconnected with neurons in neighboring layers. This ultrahigh density and parallelism make this system possess fast and high throughput computing capability. Additionally, the D\({}^{2}\)NN system is implemented with passive optical devices, where the devices function without additional maintaining power required, thus significantly reducing the consumption power for solving deep learning tasks with orders of magnitude energy efficiency advantages over low-power digital devices ([12, 13, 14, 15, 16, 17, 18, 19, 20]). More importantly, [12, 13, 14, 15, 16, 17, 18, 19] demonstrated that diffractive propagation controlled by phase modulation are differentiable, which means that such parameters can be optimized with conventional backpropagation algorithms using conventional automatic differentiation (autograd) engine implemented in modern compilers such as PyTorch and Tensorflow.

In conventional DNNs, forward propagation is computed by generating the feature representation with floating-point weights associated with each neural layer. While in D\({}^{2}\)NNs, such floating-point weights are encoded in the phase modulation of each neuron in diffractive phase masks, which is acquired by and multiplied onto the light wavefunction as it propagates through the neuron. Similar to conventional DNNs, the final output class is predicted based on generating labels according to a given one-hot representation, e.g., the max energy reading over the output signals of the last layer observed by detectors. Specific examples of the system at training and inference can be found in the next section.

Once the training of a D\({}^{2}\)NN system is completed on the digital computation platform, the trained D\({}^{2}\)NN is deployed on the optical platform with non-configurable fabricated phase masks such as 3D printed phase masks, as diffractive layers for all-optical inference. Thus, D\({}^{2}\)NNs lack reconfigurability for the weight parameters, which will bring significant energy and system cost overhead in practical application scenarios, especially for MTL.

## 3 Approach

To overcome the aforementioned limitations of existing D\({}^{2}\)NN systems, we propose a novel neural architecture, namely **RubikONNs**, that utilizes the physical rotation properties of existing D\({}^{2}\)NN systems to realize MTL with few overheads, in which case, a single-task D\({}^{2}\)NN system can be used to encode multiple feed-forward functions by rotating its underlying structure just like rotating a _Rubik's Cube_.

### Forward function for a single-task D\({}^{2}\)NN

D\({}^{2}\)NN system is designed with three major components (Figure 1): (1) laser source encoding the input images, (2) diffractive layers encoding trainable phase modulation, and (3) detectors capturing the output of the forward propagation. Specifically, the input image is first encoded with the laser

Figure 2: Example of a four-task RubikONNs rotation architecture.
source. The information-encoded light signal is diffracted in the free space between diffractive layers and modulated via phase modulation at each layer. Finally, the diffraction pattern after light propagation w.r.t light intensity distribution will be captured at the detector plane for predictions.

From the beginning of the system, the input information (e.g., an image) is encoded on the coherent light signal from the laser source, its wavefunction can be expressed as \(f^{0}(x_{0},y_{0})\). The wavefunction after light diffraction from the input plane to the first diffractive layer over diffraction distance \(z\) can be seen as the summation of the outputs at the input plane, i.e.,

\[f^{1}(x,y)=\iint f^{0}(x_{0},y_{0})h(x-x_{0},y-y_{0},z)dx_{0}dy_{0} \tag{1}\]

where \((x,y)\) is the coordinate on the receiver plane, i.e., the first diffractive layer, \(h\) is the impulse response function of free space. Here we use Fresnel approximation, thus the impulse response function \(h\) is

\[h(x,y,z)=\frac{\exp(ikz)}{i\lambda z}\exp\{\frac{ik}{2z}(x^{2}+y^{2})\} \tag{2}\]

where \(i=\sqrt{-1}\), \(\lambda\) is the wavelength of the laser source, \(k=2\pi/\lambda\) is free-space wavenumber.

Equation 1 can be calculated with spectral algorithm, where we employ Fast Fourier Transform (FFT) for fast and differentiable computation, i.e.,

\[U^{1}(\alpha,\beta)=U^{0}(\alpha,\beta)H(\alpha,\beta,z) \tag{3}\]

where \(U\) and \(H\) are the Fourier transformation of \(f\) and \(h\) respectively.

After light diffraction, the wavefunction resulting in Equation 3 \(U^{1}(\alpha,\beta)\) is first transformed to time domain with inverse FFT (iFFT). Then the phase modulation \(W(x,y)\) provided by the diffractive layer is applied to the light wavefunction in time domain by matrix multiplication, i.e.,

\[f^{2}(x,y)=\text{iFFT}(U^{1}(\alpha,\beta))\times W_{1}(x,y) \tag{4}\]

where \(W_{1}(x,y)\) is the phase modulation in the first diffractive layer, \(f^{2}(x,y)\) is then the input light wavefunction for the light diffraction between the first diffractive layer and the second diffractive layer.

We enclose one computation round of light diffraction and phase modulation at one diffractive layer as a computation module named **DiffMod**, i.e.,

\[\text{DiffMod}(f(x,y),W)=L(f(x,y),z)\times W(x,y) \tag{5}\]

where \(f(x,y)\) is the input wavefunction, \(W(x,y)\) is the phase modulation, \(L(f(x,y),z)\) is the wavefunction after light diffraction over a constant distance \(z\) in time domain, i.e., \(\text{iFFT}(U(\alpha,\beta))\) in Equation 4.

As a result, in a multiple diffractive layer constructed D\({}^{2}\)NN system, the forward function can be computed iteratively for the stacked diffractive layers. For example, for the 5-layer system shown in Figure 1, the forward function can be expressed as,

\[\begin{split} I(f^{0}(x,y),W)=\text{DiffMod}(\text{DiffMod}( \text{DiffMod}\\ (\text{DiffMod}(f^{0}(x,y),W_{1}(x,y)),W_{2}(x,y)),\\ W_{3}(x,y)),W_{4}(x,y)),W_{5}(x,y))\end{split} \tag{6}\]

where \(f^{0}(x,y)\) is the input wavefunction to the system and \(W_{1-5}\) is phase modulation provided at each diffractive layer.

The final diffraction pattern w.r.t the light intensity \(I\) in Equation 6 is projected to the detector plane. We can design arbitrary detector patterns for classes in different tasks by setting the corresponding coordinates of the detector region at the full detector plane for each class by the user's definition. For example, for MNIST datasets, the output plane is divided into **ten** detector regions to mimic the output of conventional neural networks for predicting **ten** classes. The final class will be produced by argmax function with the ten intensity sums of the ten detector regions as input. For example, in Figure 1, based on the label indices of the ten detector regions for image "2", we can see that the 3\({}^{rd}\) region on the first row has the highest energy. Then, the predicted class is class "2". Similarly, the predicted classes "1", "8", and "9" of other three datasets can be generated by applying argmax on the detector. With the one-hot represented ground truth class \(t\), the loss function \(L\) can be acquired with **MSELoss** as,

\[L=\parallel\text{Softmax}(I)-t\parallel_{2} \tag{7}\]

Thus, the whole system is designed to be differentiable and compatible with conventional automatic differential engines.

### RubikONNs Architecture for MTL

To deal with multiple tasks with minimum system overhead, an ideal system should be designed to encode different forward functions without changing the single-task system. Note that the diffractive layers are mostly designed with 3D printed materials, such that the phase parameters (weights) carried by these layers are non-reconfigurable after 3D printing. However, as demonstrated by [18, 19], the layers are portable in D\({}^{2}\)NNs and they are in square shapes. This means that we are able to rotate each layer by close-wise \(90^{\circ}\), \(180^{\circ}\), or \(270^{\circ}\), and place the layer back in the system without any other changes. While each layer carries specific trained phase parameters, by rotating one or multiple layers, the forward function will be different since the weights of the model are changed. In optical domain, this means that the modulation of the light changes accordingly w.r.t specific rotation patterns. This offers the main motivation of designing RubikONNs that aims to enable MTL in existing single-task D\({}^{2}\)NN systems. As a result, RubikONNs enables MTL by simply **(1)** pulling out the layer, **(2)** rotating it to the specific rotation pattern as designed, and **(3)** plugging the layer back to the original location, without changing the rest of the system.

To illustrate the rotation architecture RubikONNs, an example of encoding four tasks with the last two layers (L4, L5) as _rotation layers_ is shown in Figure 2. The designed rotation type applied to these layers is the _rotation angle_ (clockwise \(90^{\circ}\) in this example). To summarize the forward function of RubikONN architecture, we introduce two Boolean variables to indicate the rotation patterns of L4 and L5 layers. When \(s_{0}=1\), L4 will be rotated clockwise \(90^{\circ}\), otherwise, L4 will remain unchanged; similarly, \(s_{1}\) indicates the rotation pattern of L5. Thus, for the first task, \(s_{0}s_{1}=00\), both layers are unchanged; for the second task, \(s_{0}s_{1}=01\), L5 will be rotated clockwise \(90^{\circ}\); for the third task, \(s_{0}s_{1}=11\), both layers will
be rotated clockwise \(90^{\circ}\); for the fourth task, \(s_{0}s_{1}=10\), L4 will be rotated clockwise \(90^{\circ}\). The forward function is expressed as follows:

\[I=\text{DiffMod}(\text{DiffMod}(\begin{subarray}{c}I\\ 1\leq i\leq 3\end{subarray}(f^{0},W_{i}),\begin{cases}W_{4}),W_{5}),&s_{0}s_{1}=0 0\\ W_{4},\text{Rot}(W_{5})),&s_{0}s_{1}=01\\ \text{Rot}(W_{4})),\text{Rot}(W_{5})),&s_{0}s_{1}=11\\ \text{Rot}(W_{4})),W_{5}),&s_{0}s_{1}=10\end{cases} \tag{8}\]

We take \(s_{0}s_{1}=01\) as an example for the quantitative analysis w.r.t the rotation in the system, where only the fifth layer is rotated for 90\({}^{\circ}\). Thus, the forward function is

\[I_{01}=\text{DiffMod}((\begin{subarray}{c}I\\ 1\leq i\leq 4\end{subarray}(f^{0},W_{i}),\text{Rot}(W_{5})) \tag{9}\]

where \(\text{DiffMod}(f(x,y),W)=L(f(x,y),z)\times W(x,y),x\in[n,n],y\in[n,n]\) as shown in Equation 5, assuming \(W_{5}\) is trained as \(W_{n,n}\) and the diffraction result is \(L_{n,n}\), i.e.,

\[W_{n,n}=\begin{pmatrix}w_{1}^{1}&w_{2}^{1}&\cdots&w_{n}^{1}\\ w_{1}^{2}&w_{2}^{2}&\cdots&w_{n}^{2}\\ \vdots&\vdots&\ddots&\vdots\\ w_{1}^{n}&w_{2}^{n}&\cdots&w_{n}^{n}\end{pmatrix}L_{n,n}=\begin{pmatrix}l_{1}^{ 1}&l_{2}^{1}&l_{2}^{2}&\cdots&l_{n}^{1}\\ l_{1}^{2}&l_{2}^{2}&\cdots&l_{n}^{2}\\ \vdots&\vdots&\ddots&\vdots\\ l_{1}^{n}&l_{2}^{n}&\cdots&l_{n}^{n}\end{pmatrix}\]

Thus, the wavefunction after \(\text{DiffMod}(f,W)\)**without rotation**, i.e., \(I_{00}\), is calculated as

\[I_{00}=\begin{pmatrix}l_{1}^{1}*w_{1}^{1}&l_{2}^{1}*w_{2}^{1}&\cdots&l_{1}^{1} *w_{n}^{1}\\ l_{1}^{2}*w_{1}^{2}&l_{2}^{2}*w_{2}^{2}&\cdots&l_{n}^{2}*w_{n}^{2}\\ \vdots&\vdots&\ddots&\vdots\\ l_{1}^{n}*w_{1}^{n}&l_{2}^{n}*w_{2}^{n}&\cdots&l_{n}^{n}*w_{n}^{n}\end{pmatrix}\]

When the rotation pattern applied to \(W_{5}\) is \(90^{\circ}\), the corresponding \(\text{Rot}(W_{5})\)is

\[\text{Rot}(W_{5})=\begin{pmatrix}w_{1}^{n}&\cdots&w_{1}^{2}&w_{1}^{1}\\ w_{2}^{n}&\cdots&w_{2}^{n}&w_{2}^{1}\\ \vdots&\ddots&\vdots&\vdots\\ w_{n}^{n}&\cdots&w_{n}^{2}&w_{n}^{1}\end{pmatrix}\]

While \(L(f,z)\) remains the same, the correponding rotated \(I_{01}\) is thus altered to

\[I_{01}=\begin{pmatrix}l_{1}^{1}*w_{1}^{n}&\cdots&l_{n-1}^{1}*w_{2}^{2}&l_{n}^ {1}*w_{1}^{1}\\ l_{1}^{2}*w_{2}^{n}&\cdots&l_{n-1}^{2}*w_{2}^{2}&l_{n}^{2}*w_{2}^{2}\\ \vdots&\ddots&\vdots&\vdots&\vdots\\ l_{1}^{n}*w_{n}^{n}&\cdots&l_{n-1}^{n}*w_{n}^{2}&l_{n}^{n}*w_{n}^{1}\end{pmatrix}\]

As a result, by rotating the weights matrix \(W\) (phase modulation) to different angles, the information-carried light signal is modulated with different applied phase modulations w.r.t different datasets. The full propagation figures in Figure 4 show the light patterns when different rotation patterns are applied to the same input light signal.

Note that the proposed architecture can go beyond four tasks by adding more rotation patterns. For example, each layer has four rotation states, \(0^{\circ}\), \(90^{\circ}\), \(180^{\circ}\), or \(270^{\circ}\), such that the maximum number of different forward functions is \(1+3\times 4=13\) with two rotation layers; when add another rotation layer, i.e., three rotation layers with four rotation states in the system, the system can deal with up to 29 tasks.. Discussions about choices of rotation layers and different rotation angles are included in Section 4.

While the RubikONNs architecture enables zero-overhead MTL on D\({}^{2}\)NN systems, the training algorithms that are aware of physical rotations and light diffraction do not exist. Specifically, the training algorithms should be able to learn structural weight parameters w.r.t specific rotation patterns and given datasets. Thus, we introduce two novel MTL algorithms for training RubikONNs, i.e., _rotated aggregation algorithm (RotAgg)_ and _rotated sequence Algorithm (RotSeq)_.

```
Result:\(W=\{W_{C}^{1,2,3},W_{R}^{1},W_{R}^{5}\}\) for the rotation model
1Initialization: Weights \({W_{0}}^{1,2,3}=\{{W_{C0}}^{1,2,3},{W_{R0}}^{4},{W_{R0}}^{5}\}\) for the model
2while\(i\leq\text{training iterations}\)do
3\(W_{1,2,3,4}\leftarrow{W_{0}}^{i}:\)
4\(W_{1}^{i}\leftarrow\{W_{C_{i}^{1,2}}^{3},W_{R_{i}^{1}}^{4}\};\)
5\(W_{1}^{i+1}\leftarrow\)\(\{W_{1}^{i}-\eta{W_{1}}^{i}\};\)\(\triangleright\) training w.r.t task 1 (D1) w/o rotation
6\(W_{2}^{i}\leftarrow\{W_{C_{i}^{2,3}}^{2},W_{R_{i}^{1}},\text{rotate}(W_{R_{i}^{ 1}}^{6})\};\)\(W_{2}^{i+1}\leftarrow\)\(D2\)\(W_{2}^{i}-\eta{W_{2}}^{i}\)\(\triangleright\) task 2 (D2) update w 5\({}^{th}\) layer rotated 90\({}^{\circ}\)
7\(W_{3}^{i}\leftarrow\{W_{C_{i}^{1,2,3}}^{1,3},\text{rotate}(W_{R_{i}^{ 1}}^{6}),\text{rotate}(W_{R_{i}^{1}}^{6})\};\)
8\(W_{3}^{i+1}\leftarrow\)\(\{W_{C_{i}^{1,2,3}}^{1,3},\text{rotate}(W_{R_{i}^{ 1}}^{6}),\text{rotate}(W_{R_{i}^{1}}^{6})\};\)
9\(W_{3}^{i+1}\leftarrow\)\(\{W_{C_{i}^{1,2,3}}^{1,3},\text{rotate}(W_{R_{i}^{1}}^{4}),W_{R_{i}^{ 1}}^{6}\};\)
10\(W_{4}^{i+1}\leftarrow\)\(\{W_{C_{i}^{1,2,3}}^{1,3},\text{rotate}(W_{R_{i}^{1}}^{4}),W_{R_{i}^{ 1}}^{6}\};\)
11\(W_{4}^{i+1}\leftarrow\)\(\{W_{C_{i}^{1,2,3}}^{1,3},\text{rotate}(W_{R_{i}^{1}}^{4}),W_{R_{i}^{ 1}}^{6}\};\)
12\(W_{4}^{i+1}\leftarrow\)\(\{W_{C_{i}^{1,2,3}}^{1,3},\text{rotate}(W_{R_{i}^{1}}^{4}),\text{ rotate}-\text{back}(W_{R_{i}^{1}}^{5})\};\)\(W_{3}^{i}\leftarrow\)\(\{W_{C_{i}^{1,2,3}}^{1,3},\text{rotate}-\text{back}(W_{R_{i}^{1}}^{4}),\text{rotate}-\text{back}(W_{R_{i}^{ 1}}^{5})\};\)\(W_{4}^{i}\leftarrow\)\(\{W_{C_{i}^{1,2,3}}^{1,3},\text{rotate}(W_{R_{i}^{1}}^{4}),W_{R_{i}^{ 1}}^{5}\};\)\(\triangleright\) reversely rotating virtual models aggregation
13\(W_{0}^{i+i+1}\leftarrow\)\((W_{4}^{i+1}+W_{2}^{i+1}+W_{3}^{i+1}+W_{4}^{i+1})/4;\)
14\(i\gets i+1\)
15 end while
```

**Algorithm 1**Rotated Aggregation Algorithm (RotAgg) for training RubikONNs.

### Algorithm 1: Rotated Aggregation Training

The Rotated Aggregation Training (**RotAgg**) algorithm shown in Alg. 1 aims to update the parameters of RubikONNs by averagely aggregating gradients generated from all tasks, while the gradient of each task is computed by including the rotations in every training iteration. Therefore, the training iterations are fully aware of physical rotations of the RubikONNs architecture. We illustrate RotAgg using the same rotation designs shown in Figure 2, where the first three layers are shared layers, named as \(W_{C}^{1,2,3}\), that will not be rotated during training and inference, and the rotations layers are denoted as \(W_{R}^{A}\) and \(W_{R}^{5}\). First, RotAgg algorithm initializes one model for aggregation, and four virtual models to store temporary updates w.r.t specific rotation patterns and dataset (line 1). In every training iteration, RotAgg will first update the parameters in the four virtual models, \(W_{1
example, the first update is performed for task 1 w.r.t dataset D1 (line 4), where the model is rotated based on \(W_{1}^{i}\) with the rotation pattern of \([0^{\circ},0^{\circ}]\) as shown in Figure 2. The second update will then be performed w.r.t to task 2 dataset D2, where the virtual model \(W_{2}^{i}\) will be initialized by rotating parameters in rotation layers (L4 and L5) with the rotation pattern \([0^{\circ},90^{\circ}]\) (line 5). Similarly, the virtual models for task 3 (D3) and task 4 (D4) will be performed. Before the final weight aggregation, the four virtual models will be reverse-rotated back to the initial position (line 8). Finally, RotAgg averagged aggregates the weights from all four virtual models (line 9), and return \(W_{0}^{i+1}\) for next iteration or as final model.

### Algorithm 2: Sequential Rotation Training

The second training algorithm Sequential Rotation Training (**RotSeq**) shown in Alg. 2 aims to update the parameters of RubikONNs by sequentially updating the model w.r.t a given sequence of task orders in order to incorporate the physical rotations in the training process. Here, we illustrate Alg. 2 using a specific order of updates, i.e., D1\(\rightarrow\)D2\(\rightarrow\)D3\(\rightarrow\)D4. In the illustration example, for the first task, the model will be updated w.r.t dataset D1 without rotating the rotation layers (line 4). Unlike the RotAgg algorithm, the model will be directly updated to \(W^{i}\) after the training of the first task. Next, the weights will be rotated with the rotation pattern \([0^{\circ},90^{\circ}]\), i.e., rotating \(W_{R_{1}}^{5}\) clockwise \(90^{\circ}\) before the gradient update process for task 2 (line 5). Note that the model rotated before training for task 2 has already been updated w.r.t D1. Similarly, the model will be trained in the same sequential updating fashion according to the rotation patterns designed for task 3 (line 7) and task 4 (line 9). Note that the inner loop update order can be fixed for all iterations or can be dynamically changed through the training process. Therefore, in addition to other training parameters, RotSeq could also be impacted by the inner loop update orders. In Section 4, a comprehensive analysis of the update orders is provided.

## 4 Results

**System Setup** - The model explored in our experiments is designed with five diffractive layers as it is shown in Figure 1. The system size is set to be \(200\times 200\), i.e., the size of the diffractive layers and the total detector plane is \(200\times 200\). The input image whose original size is \(28\times 28\) will be enlarged to \(200\times 200\) and encoded on the laser light signal with the _wavelength_ = 532 \(nm\). The physical distances between layers, first layer to source, and final layer to detector, are set to be \(30\)\(cm\). The architecture is designed with the rotation patterns shown in Figure 2. The detector collects the light intensity of the ten pre-defined regions for ten classes with each size of \(20\times 20\) (Figure 1), where the sums of intensity of these ten regions are equivalent to a 1\(\times\)10 vector in float32 type. The final prediction results will be then generated using argmax.

**Training Parameters and Datasets** - To evaluate the proposed RubikONNs architecture and RotAgg and RotSeq training algorithms, we select four public image classification datasets, including 1) _MNIST-10_ (MNIST) ([14]), 2) _Fashion-MNIST_ (FMNIST) ([13]), 3) _Kuzushiji-MNIST_ (KMNIST) ([12]), and 4) _Extension-MNIST-Letters_ (EMNIST) ([12]), an extension of MNIST to handwritten letters. Specifically, for EMNIST, we customize the dataset to have the first ten classes (i.e., A-J) to match the D\({}^{2}\)NN physical system, with 48000 training examples and 8000 testing examples. In the rest of this section, the total number of training iterations of all experiments is set to 150. Within each training iteration, each dataset is trained with 1 training epoch. The learning rate in the training process is set to be \(0.01\) for all experiments cross all four datasets using Adam. The implementations are constructed using PyTorch v1.5.1. All experiments are conducted on a Nvidia 2080 Ti GPU.

### Evaluations of RubikONNs with RotAgg and RotSeq Algorithms

**RotSeq and training permutations, and RotAgg** - We first evaluate RotSeq algorithm (Alg. 2) on MTL using the four selected datasets. As discussed in Section 3, the performance of RotSeq can vary with different gradient update orders (i.e., lines 4-10 in Alg. 2). Therefore, we evaluate RotSeq algorithm with four different permutations of the gradient update sequences, shown in second column of Table 1. With such recurring permutation of training orders, each task can be trained at each position in the training order. First, for each dataset, we can see that RotSeq offers a small accuracy boost for the given task/dataset used as the last gradient update in one RotSeq training iteration. This is because RotSeq (Alg. 2) updates the parameters in a given sequence to all training tasks, where testing accuracy is basically obtained right after the gradient updates of the last task. Second, when a dataset is trained at the beginning of each training iteration (first task in the training sequence), the prediction performance of this task might slightly degrade. For example, MNIST accuracy collected using model trained with D1 \(\rightarrow\) D2 \(\rightarrow\) D3 \(\rightarrow\) D4 sequence is 0.0006/0.0006/0.0025 lower than the other three permutations.

As for the comparison between RotAgg and RotSeq rotation training algorithms, the training loss curves for two al
gorithms are shown in Figure 3. Both algorithms can converge efficiently and produce similarly decent accuracy performance, while the training loss for Algorithm 2, which trains the rotation MTL system with sequential datasets, shows more fluctuations. As a result, the model trained with RotAgg algorithm shows overall performance and robustness since the training is not impacted by orders of gradient updates. Instead, RotAgg averages the gradients obtained independently for all tasks. The advantages of RotAgg can be summarized in two: **(1)** The training hyperparameter space is much limited than RotSeq since its performance is not influenced by the gradient update order; **(2)** The algorithm is expected to be more robust than RotSeq as RotSeq has slight training bias w.r.t the gradient update order. While RotSeq performs similarly to RotAgg with these four datasets, the bias training characteristics in RotSeq can potentially result in requiring more training setup exploration. Thus, in the rest of the result section, we use RotAgg as default algorithm for RubikONN architecture exploration analysis.

**Prediction performance comparisons** - To fully demonstrate the effectiveness of the proposed approaches, we first compare the prediction performance with two existing approaches. First of all, a straightforward method to enable MTL on a fixed single-task D\({}^{2}\)NNs architecture is to simply train a D\({}^{2}\)NNs while merging the four datasets as one. Thus, we implement a straightforward baseline algorithm by extending the approach proposed by [11], where the training dataset consists of fully shuffled training samples from all four datasets, namely _BaselineMTL_, and its depth is set to be five and system size is set to be \(200\times 200\), which is the same setup as our rotation system. The evaluation result of this baseline algorithm is shown in the last row of Table 1. Next, we compare our approaches to a specific MTL D\({}^{2}\)NNs architecture. Specifically, [10] proposes a novel D\({}^{2}\)NN architecture that utilizes transfer learning concepts from conventional neural networks, which includes shared diffractive layers (shared weights) and independent diffractive layers at the output stage for each task. To make a fair comparison, we extend that architecture to deal with four tasks and set three layers for the shared diffractive layers and two layers for the independent diffractive layers in each channel for four tasks and the same system size (\(200\times 200\)) as our system.

As shown in Table 1, we can see that by utilizing the physical rotation properties with the proposed training algorithms, RubikONNs offers better prediction accuracy for all datasets. We can see that with RotAgg and RotSeq, RubikONNs performs significantly better than both baseline approaches. For example, with RotAgg algorithm, our approach offers about 2.5% accuracy increases for MNIST and FMNIST, 6.5% increases for EMNIST and 8.4% for KMNIST, compared to _BaselineMTL_[11]; compared to [10], RotAgg offers 3.5% accuracy increases on EMNIST, and performs similarly for other three tasks. This demonstrates that by utilizing the physical rotations into D\({}^{2}\)NN architecture, RubikONNs offers clear prediction improvements over other approaches, while system cost, energy consumption, and complexity into the comparisons are not yet in

\begin{table}
\begin{tabular}{c c c c c c} \hline \hline Algorithm & Permutation & MNIST\({}_{\text{D1}}\) & FMNIST\({}_{\text{D2}}\) & KMNIST\({}_{\text{D3}}\) & EMNIST\({}_{\text{D4}}\) \\ \hline \multirow{4}{*}{RotSeq (Alg. 2)} & D1 \(\rightarrow\) D2 \(\rightarrow\) D3 \(\rightarrow\)**D4** & 0.9533 & 0.8384 & 0.8275 & **0.8885** \\  & D4 \(\rightarrow\) D1 \(\rightarrow\) D2 \(\rightarrow\)**D3** & 0.9539 & 0.8394 & **0.8353** & 0.8811 \\  & D3 \(\rightarrow\) D4 \(\rightarrow\) D1 \(\rightarrow\)**D2** & 0.9539 & **0.8430** & 0.8270 & 0.8824 \\  & D2 \(\rightarrow\) D3 \(\rightarrow\) D4 \(\rightarrow\)**D1** & **0.9558** & 0.8386 & 0.8272 & 0.8834 \\ \hline RotAgg (Alg. 1) & n/a & 0.9524 & 0.8412 & 0.8272 & 0.8819 \\ \hline \hline
[10] & n/a & 0.9532 & 0.8370 & 0.8277 & 0.8464 \\ BaselineMTL & n/a & 0.9262 & 0.8176 & 0.7430 & 0.8174 \\ \hline \end{tabular}
\end{table}
Table 1: Evaluations of prediction performance on four-task multi-task learning using datasets. MNIST(D1), FMNIST(D2), KMNIST(D3), and EMNIST(D4), optimized with the proposed RotSeq and RotAgg algorithms.

Figure 3: Training loss curves with 150 epochs for two algorithms.
cluded in the comparisons.

**Accuracy-Efficiency Comparison** - To fully evaluate the efficiency of the models regarding the system cost, complexity, and energy efficiency, we introduce an accuracy-cost evaluation metric, where hardware cost is the sum of diffractive layer cost and detector cost1. In Table 2, single-task cost metric is set as the baseline (unit 1), and the improvement of the architectures is calculated using Equation 10. Note that in Table 2, the baseline results are collected using single-task implementation with five layers and \(200\times 200\) system size, and our results are generated using RotAgg algorithm. We can see that our approach offers **more than 4.0\(\times\) and 2.0\(\times\) hardware cost efficiency improvements** compared to [11] and [12], respectively. Regarding energy efficiency, we evaluate the power consumption per task. Our approach demonstrates **2.7\(\times\) and 5.3\(\times\) energy efficiency improvements** compared to [11] and [12], respectively. Note that the power consumption of DONNs is orders of magnitude more efficient than conventional digital platforms. Thus, we only compared to DONNs baselines in this work since the advantages of DONNs over conventional DNN hardware have been demonstrated.

Footnote 1: The layer fabrication cost is \(\sim\)$100 and detector cost is $1,500 - $10,000. We formulate the cost of $100 as unit 1, thus, the layer cost for a 5-layer ONN is 5 and one detector cost is 10 for the cost efficiency estimation.

\[\text{Acc-Efficiency Metric} =\frac{Acc_{\text{MTL}}}{Acc_{\text{baseline}}}\cdot\frac{Cost_{ \text{MTL}}}{Cost_{\text{baseline}}};\] \[Cost =\text{\#. Detectors or }\mu W\text{/fps/task} \tag{10}\]

### Design Space of RubikONNs Architecture

With the proposed architecture and training algorithms, the rotation architecture can be designed in many different variants. Specifically, the rotation angles of the rotation layers, and the index of rotation layers to be rotated, which are independent to all other system and algorithm specifications. For example, instead of rotating the layers clockwise \(90^{\circ}\), the layers can also be rotated \(180^{\circ}\) and \(270^{\circ}\) (\(-90^{\circ}\)). Similarly, the architecture can also be designed by selecting other layers other than the 4\({}^{th}\) and 5\({}^{th}\) layers to be rotated. Thus, in this section, we provide experimental analysis of other variants of the proposed architecture by evaluating different rotation angles and various rotation layer selections. To limit the exploration space on the algorithm side for RubikONN architecture exploration, we explore the rotation angles and rotation layers only using **RotAgg** algorithm.

**Analysis of different rotation angles** - Since each diffractive layer can rotate close-wise \(90^{\circ}\), \(180^{\circ}\), and \(270^{\circ}\) (\(-90^{\circ}\)), the rotation angle can be independent from layer to layer, e.g., rotating 4\({}^{th}\) layer \(90^{\circ}\) and rotating 5\({}^{th}\) layer by \(180^{\circ}\). To evaluate the impacts of rotation angles, the experiments shown in Table 3 are conducted with fixed selection of rotation layers, i.e., 4\({}^{th}\) and 5\({}^{th}\) layers. Table 3 shows the accuracy of four datasets in the model trained with RotAgg when different rotation angles are applied to last two layers. Specifically, we evaluate two different rotation angle settings: (1) same rotation angles for both layers; or (2) different rotation angles for the two layers. For example, (\(90^{\circ}\), \(180^{\circ}\)) means that if the 4\({}^{th}\) layer is designed to be rotated for a given task, it rotates \(90^{\circ}\); and 5\({}^{th}\) layer is rotated \(180^{\circ}\) if needed. In general, with different rotation angles, RubikONNs shows little fluctuation in terms of accuracy.

**Analysis of rotated layer selections** - Let the number of tasks be 4 and each rotation layer can only rotate clock-wise \(90^{\circ}\), the total number of layer selections is \(\mathbf{C}_{5}^{2}\)=\(10\). According to studies of conventional neural networks, the layers close to the inputs are usually very important for feature extractions, while the layers close to outputs are crucial for generating the final prediction class. Thus, we evaluate three combinations, including (1) the last two layers, (2) the first two layers, and (3) the first and the last layers. The results are shown in Table 4. We can see that (a) the models trained with RotAgg algorithm perform almost the same, regardless of which layers are selected as rotation layers; (2) including the last layer (5\({}^{th}\)) in the rotation layers performs slightly better on average.

In summary, Table 3 and Table 4 results suggest the follows: **(1)** The rotation layers are preferred to be selected close to the output. **(2)** The prediction performance is not restricted to specific rotation angles, which offers possibility to encode more forward functions, and it is the key to enable larger number of tasks for MTL.

### RubikONNs MTL Explainability

To understand the impacts of rotations for MTL, we measure the internal propagation of RubikONNs between the source, layers and detectors. Specifically, we measure the intensity of the light in the RubikONNs at inference phase, shown in Figures 4. The visualizations of the forward propagation shown in Figures 4 are organized by applying a same image from one dataset using all rotation patterns, following the designed rotation patterns shown in Figure 2. It is known that the main idea of DNNs is that layers close to the input focus on extracting features, and layers close to the output focus on finalizing the predictions using the extracted features. The intuition of RubikONNs architecture is relatively the same, and has been demonstrated based on the propagation measurements. For example, in Figure 4, the input image is from MNIST dataset, where four complete propagation measurements are included w.r.t the rotation patterns for task MNIST, FMNIST, KMNIST, and EMNIST, respectively. We can see that the outputs of the first three layers are identical for all four cases, since the first three layers are not the rotation layers. The differences of forward propagation are observed starting from the 4\({}^{th}\) layer, which is rotated clockwise \(90^{\circ}\) for MNIST and KMNIST tasks, and remains un-rotated for FMNIST and EMNIST tasks. Similarly, since the 5\({}^{th}\) layer is also designed to be rotated as well, the outputs collected by the detectors clearly show four different intensity distributions. Additional outputs of 4\({}^{th}\) and 5\({}^{th}\) layers w.r.t other three datasets are shown in Figure 5, which further confirms that RubikONNs is able to successfully encode four different forward functions, which are properly optimized for four tasks using the proposed training algorithms.
## 5 Conclusions

This work proposes a novel optical neural architecture **RubikONNs** architecture, which utilizes the physical properties of optical computing systems to encode multiple feed-forward functions by rotating the non-reconfigurable hardware system. To optimize the MTL performance of RubikONNs, two novel domain-specific physics-aware training algorithms RotAgg and RotSeq are proposed, such that RubikONNs offers **4\(\times\)** implementation cost and energy efficiencies improvements, with marginal accuracy degradation. Finally, a comprehensive RubikONNs design space exploration analysis and explainability are provided to offer concrete design methodologies for practical uses.

The ONNs have the potential to handle more complex image tasks, including image classification tasks and graph tasks [22] with outstanding energy efficiency advantage over conventional NNs (e.g., CNNs), which can already be observed with simple datasets like MNIST (\(\sim\)3 orders compared to CPU/GPU in our setups). It is believed that more significant improvements can be achieved when dealing with more complex tasks requiring more computation resources, as there is no extra energy cost for computations with the modulations of the light signal in ONNs.

Future works will focus on dealing with more complex datasets. For example, we can do MTL with RubikONNs for RGB image classification by extending the optical channels for "R", "G", "B" channels with rotation scheme in each channel to realize the MTL for RGB images.

**Acknowledgement** This work is funded by National Science Foundation (NSF) under NSF-2008144, NSF-2019306, NSF-2019336, and NSF-2047176.

\begin{table}
\begin{tabular}{c c c c c} \hline \hline \multirow{2}{*}{
\begin{tabular}{c} Rotated \\ Layers \\ \end{tabular} } & \multicolumn{4}{c}{**RotAgg** (Alg. 1) & \multicolumn{1}{c}{} \\ \cline{2-5}  & MNIST & FMNIST & KMNIST & EMNIST \\ \hline \(4^{th}\), \(5^{th}\) & 0.9524 & 0.8412 & 0.8272 & 0.8819 \\ \(1^{st}\), \(2^{nd}\) & 0.9536 & 0.8490 & 0.8199 & 0.8813 \\ \(1^{st}\), \(5^{th}\) & 0.9510 & 0.8473 & 0.8210 & 0.8865 \\ \hline \hline \end{tabular}
\end{table}
Table 4: Design space explorations with different selections of rotation layers with rotation angle \(90^{\circ}\).

Figure 4: Visualization of light propagation patterns at inference with MNIST image “1” as input, using all four rotations of RubikONNs.

\begin{table}
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|} \hline  & \multicolumn{3}{c|}{[Lin _et al._, 2018]} & \multicolumn{3}{c|}{[Li _et al._, 2021]} & \multicolumn{3}{c|}{**This work**} \\ \cline{2-11}  & D1 & D2 & D3 & D4 & D1 & D2 & D3 & D4 & D1 & D2 & D3 & D4 \\ \hline Layer Cost & 5 & 5 & 5 & 5 & \multicolumn{3}{c|}{11} & \multicolumn{3}{c|}{5} \\ \hline Detector Cost* & 10 & 10 & 10 & 10 & 20 & \multicolumn{3}{c|}{10} \\ \hline _Acc.(\%)_ & 96.4 & 86.5 & 86.1 & 90.9 & 95.6 & 83.1 & 81.4 & 84.3 & 95.2 & 84.1 & 82.7 & 88.2 \\ \hline Cost Efficiency & 1 & 1 & 1 & 1 & 2.0\(\times\) & 2.1\(\times\) & 1.9\(\times\) & **4.0\(\times\)** & **4.1\(\times\)** & **4.1\(\times\)** \\ \hline Power (\(\mu W\)/fps/task) & \multicolumn{3}{c|}{4.67\(\times\)\(10^{-7}\)} & \multicolumn{3}{c|}{8.86\(\times\)\(10^{-7}\)} & \multicolumn{3}{c|}{**1.7\(\times\)\(10^{-7}\)**} \\ \hline \hline \end{tabular}
\end{table}
Table 2: Evaluations of hardware efficiency on multi-task learning compared with [17] and [16], using datasets.

MNIST(D1), FMNIST(D2), KMNIST(D3), and EMNIST(D4).

Figure 5: Visualization of light propagation measurements (input, and outputs of 4\({}^{th}\) and 5\({}^{th}\) layers) at inference phase with FMNIST, KMNIST, and EMNIST images, using all four rotations of RubikONNs.

\begin{table}
\begin{tabular}{c c c c c} \hline \hline \multirow{2}{*}{
\begin{tabular}{c} Rotation \\ Angle \\ \end{tabular} } & \multicolumn{3}{c}{**RotAgg** (Alg. 1) w rot layers = 4\({}^{th}\), 5\({}^{th}\) \\ \cline{2-5}  & MNIST & FMNIST & KMNIST & EMNIST \\ \hline \(90^{\circ}\), \(90^{\circ}\) & 0.9524 & 0.8412 & 0.8272 & 0.8819 \\ \(180^{\circ}\), \(180^{\circ}\) & 0.9532 & 0.8514 & 0.8313 & 0.8809 \\ \(270^{\circ}\), \(270^{\circ}\) & 0.9527 & 0.8353 & 0.8240 & 0.8845 \\ \(90^{\circ}\), \(-90^{\circ}\) & 0.9518 & 0.8427 & 0.8272 & 0.8879 \\ \(90^{\circ}\), \(180^{\circ}\) & 0.9514 & 0.8413 & 0.8227 & 0.8851 \\ \hline \hline \end{tabular}
\end{table}
Table 3: Explorations with various rotation angles (clockwise) with the 4\({}^{th}\) and 5\({}^{th}\) layers as rotation layers.