LeNet-5 (Diagonal)
This page demonstrates LeNet-5 on MNIST with the diagonal primitives and a strided layout
(LeNet5Diagonal), and its low-memory variant (LeNet5KeyOptimizedDiagonal).
Overview
No layer compacts its output: the rows keep the 28-slot stride of the image all the way through the network.
conv1
(1,28,28) → (6,24,24): channelcat slot672 c, value(y, x)at28 y + x.pool1: window sums left in place (value
(Y, X)at56 Y + 2 X); the 1/4 of the average goes into the conv2 weights.conv2
(6,12,12) → (16,8,8): a multiplexed convolution. Its 16 output channels are packed as groups ofchannelStride(672) slots, the channels of a group interleaved in the gaps left by pool1 (subShifts). With2^12slots 4 groups of 4 channels fit (4 passes of6 + 4 - 1diagonals); with 16384 slots or more the 16 channels run as 16 parallel groups (one pass of6 + 16 - 1diagonals).pool2 is fused into fc1, which reads the 4 slots of every window (
inputPositions).fc1, fc2, fc3: diagonals, outputs in the first slots of ciphertexts of 128, 128 and 16 slots.
The strided (“valid”) convolutions are described to the controller with FHEONLayout (row, column and channel
strides, groups, sub shifts). The ReLUs use a degree-27 Chebyshev approximation, and the fully connected layers return
their outputs already divided by the scale of the next ReLU.
Parameters per security level
Performance efficient |
128-bit (and the other standard levels) |
|
|---|---|---|
Chain |
one bootstrapping after fc2 (faster than a leveled chain there: 2.2 s against 2.6 s per image) |
leveled, 28 levels, no bootstrapping ( |
Ring, slots |
|
|
Scale |
46 bits, first modulus 50 |
ring optimization down to |
The bootstrapping needs the 46-bit scale: with 30, 32 or 40 bits the bootstrapped result does not decrypt, so the
bootstrapped chain stays at 2^16 at 128 bits. Without bootstrapping the 28-level chain gives the same predictions
down to a 22-bit scale, and 26 bits is the smallest scale with enough NTT primes in a 2^15 ring. See
Security Levels, Ring Dimension and Packing.
At 128 bits (first MNIST test images):
Program |
Chain |
Time per image |
Peak memory |
|---|---|---|---|
|
leveled, |
18 s (9.1 s of circuit), 10/10 correct |
9.0 GB |
|
leveled, |
9.8 s, 3/3 correct |
13.5 GB |
Rotation Keys
Every layer’s generator returns exactly the rotations it uses (generate_convolution_rotation_positions,
generate_avgpool_rotation_positions, generate_linear_rotation_positions). LeNet5Diagonal keeps all of them
in one key set; with the performance-efficient parameters the bootstrapping keys are generated for the 128 slots of fc2
only (generate_bootstrapping_keys_for_slots).
Low-Memory Variant (LeNet5KeyOptimizedDiagonal)
LeNet5KeyOptimizedDiagonal runs the same circuit and keeps only what the current layers need in memory:
the context generates no bootstrapping keys (
contextBootstrapKeys = false) and no EvalSum keys (contextSumKeys = false; the diagonal primitives do not use them);the keys are split into files, generated one after the other and cleared in between:
lenet5_conv.bin(conv1, pool1, conv2),lenet5_fc.bin(fc1, fc2, fc3) and, with the bootstrapped chain only,lenet5_boot.bin(bootstrapping for 128 slots, fc3). During the inference the rotation keys in memory are cleared (ClearEvalAutomorphismKeys, memory returned withmalloc_trim) and the file of the next layers is loaded withload_rotation_keys; the context and the relinearization key are kept;every layer encodes its weights right before it runs and releases them afterwards.
With the performance-efficient parameters it takes about 5 s per image and 1.9 GB (LeNet5Diagonal: 2.2 s and
4.8 GB).
cmake -DCMAKE_BUILD_TYPE=Release -DMODE=DIAGONAL_SINGLE_INPUT -DMODEL=LeNet5 -DSECURITY_LEVEL=128 ..
make -j$(nproc) LeNet5Diagonal LeNet5KeyOptimizedDiagonal
./LeNet5KeyOptimizedDiagonal --test-size 10
Key Functions
The main functions used in this example include:
he_convolution()– Strided “valid” convolution over diagonals (layout given by FHEONLayout).he_avgpool()– Window sums left in place; avgpool_weight_scale() is the factor for the next weights.he_linear()– Fully connected layer over diagonals, inputs anywhere (FHEONLayout::inputPositions).he_relu()– Chebyshev approximation of ReLU.bootstrap_function()– CKKS bootstrapping (a single iteration), performance-efficient parameters only.
Full Example Source
You can view and download the full source code of this example: