Standard Primitives

The standard primitives are the layers of FHEON as originally defined. They are provided for single inputs by FHEONANNStandardController (sequential) and for batches of images by FHEONANNStandardBatchController, which the batch models evaluate with its multithreaded version (FHEON’s Controllers). The standard training controllers build on the same layouts (Encrypted Training).

Single-input layout

A tensor of C channels of width w is held in one ciphertext as C consecutive blocks of w^2 slots (channel c in slots c w^2 ... (c+1) w^2 - 1). The weights are plaintexts encoded once per layer (encode_kernel, encode_kernel_optimized, encode_bais_input).

Convolution. The kernel taps are rotations of the input. he_convolution_optimized (3x3, padding 1) computes the 8 rotations once, with a hoisted precomputation (by ±1, ±w, ±(w±1)). For every output channel it then:

  1. multiplies the taps by the kernel plaintexts, which hold the weights of all the input channels in their blocks, with the padding masks folded in;

  2. folds the input-channel blocks into the first one (C_in - 1 rotations by w^2) and masks it;

  3. downsamples it when the stride is 2;

  4. places it at its block with the single rotation key -w_out^2 (Horner form).

he_convolution and he_convolution_advanced handle other kernel sizes, paddings and strides.

Variants for wide layers. he_convolution_optimized_with_multiple_channels and he_convolution_and_shortcut_optimized_with_multiple_channels keep several channels per ciphertext. They downsample whole groups of channels at once (downsample_with_multiple_channels), so a stride-2 block costs one downsampling per group instead of one per channel. he_shortcut_convolution and he_convolution_and_shortcut_optimized evaluate the 1x1 shortcuts of residual networks, and he_sum_two_ciphertexts the residual sum.

Pooling. he_avgpool_optimzed sums the 2x2 windows with shifts of the slots, scales them and downsamples (he_avgpool_optimzed_with_multiple_channels for groups of channels; he_avgpool and he_avgpool_advanced for other windows and paddings). he_globalavgpool reduces every channel to one value.

Linear and ReLU. he_linear multiplies the input by one weight plaintext per output and sums it with EvalSum, then merges the outputs; he_linear_optimized uses pre-encoded weights. he_relu evaluates a Chebyshev approximation of ReLU on the input divided by its scale (optionally returning the normalized output, see Standard ANN Controller).

Shared interface. FHEONANNDiagonalController declares the optimized primitives with the same names and arguments, so a model written with them and with the encode_* helpers switches to the diagonal primitives by changing only the controller type. The optional FHEONLayout argument carries the layout options that only the diagonal primitives use.

Batch layout

FHEONANNStandardBatchController evaluates a batch of B images: every ciphertext holds one channel of the whole batch, image after image ([image 0 feature map][image 1 feature map]...). The batch is the SIMD dimension, so one evaluation classifies all its images.

  • he_batch_convolution (any kernel, as LeNet-5 uses it), he_batch_convolution_optimized and he_batch_convolution_shortcut_optimized encode the raw weights inside the primitive, at the level of the inputs; strided convolutions downsample all the images of a channel at once.

  • he_optimzed_batch_avgPool (2x2 pooling) and he_batch_globalpool (global average pooling).

  • he_batch_inputs_converter (and _memory_efficient) turn the per-channel ciphertexts of the last convolution into the per-image inputs of the linear layers.

  • he_batch_linear (one output ciphertext), he_batch_linear_multiple_outputs (one ciphertext per image), he_batch_linear_memory_efficient and he_batch_linear_packed (the packed global pooling + linear layers of the pipelines, with encode_batch_linear_packed_masks / _bias), he_batch_relu and he_batch_sum_ciphertexts complete the networks.

  • FHEONHEController::batch_bootstrap_function bootstraps all the channels of a layer, in parallel.

The images per ciphertext follow the slots of the context: at a standard security level the batch models fill every slot of the ring (HEConfig.fillSlots) and multiply their batch sizes by FHEONHEController::slot_scale, e.g. 32 images instead of 16 at 128 bits (Batch Input Examples, Security Levels, Ring Dimension and Packing).

Rotation keys

Every primitive has a generator that returns exactly the rotations it uses: generate_convolution_rotation_positions, generate_optimized_convolution_rotation_positions, generate_avgpool_optimized_rotation_positions, generate_linear_rotation_positions and generate_sum_rotation_positions (the EvalSum rotations), and the generate_*_batch_* generators of the batch controller. The models collect the generators of the layers of a stage into one key file per stage, or into a single key set that stays in memory (the “joined keys” batch models).

Standard and diagonal convolutions

The two layouts differ mostly in how a convolution mixes the channels:

Standard (he_convolution_optimized)

Diagonal (he_convolution)

Tap rotations

8 per layer (hoisted)

8 per layer (hoisted)

Channel mixing

C_in - 1 rotations per output channel, then one placement rotation

One rotation per diagonal (C per layer), Horner form

Plaintext products

9 per output channel

9 per diagonal

Levels

Product and the block mask (plus the downsampling when strided)

One (the masks are folded into the diagonals)

See Evaluation for measured runtimes of both.