ResNet-20 (Diagonal)
This page demonstrates ResNet-20 on CIFAR-10 with the diagonal primitives. Two variants differ only in how they manage the keys.
Overview
Every 3x3 convolution is evaluated over channel diagonals (8 hoisted rotations, one rotation per diagonal, one level).
The first block of stages 2 and 3 evaluates the stride-2 convolution and the 1x1 shortcut together
(he_convolution_and_shortcut_optimized), which halves the width and returns ciphertexts with half the slots. The
global average pooling and the linear layer end the network.
Every call uses the interface shared with the standard controller, so FHEONANNStandardController can be dropped in
by changing the controller type.
A bootstrapping is followed either by a ReLU and a convolution or by a stride-2 block. When the two channel stages of the stride-2 downsampling do not fit in those levels, a single stage is used. ReLU degree 27 shortens the chain by one level and is about 8% faster than degree 59.
Variants
ResNet20Diagonal keeps one key file per stage: the bootstrapping keys for the slot count of the stage, the rotations of its convolutions and of the stride-2 block that ends it. Only the current stage is in memory.
ResNet20OptimizedDiagonal generates all the keys at the start and keeps them in memory for the whole inference: more memory, no reloading between the stages.
Key Functions
The main functions used in this example include:
encode_convolution_kernel() / encode_convolution_bias()– Diagonal encoding of the 3x3 kernels, at the level they are multiplied at.encode_shortcut_kernel()– Diagonal encoding of the 1x1 shortcut.he_convolution()– 3x3 “same” convolution over diagonals.he_convolution_and_shortcut_optimized()– Stride-2 convolution and shortcut on shared downsampled taps.he_sum_two_ciphertexts()– Residual connection.he_linear()– Final linear layer over diagonals.generate_bootstrapping_and_rotation_keys()– Key file of a stage.
Full Example Source
You can view and download the full source code of this example: