Training Support Controller

Storage of encrypted parameters and caches, and the rotation-key generators of the standard training primitives.

class FHEONTrainSupportController

Shared services of the standard training controllers.

Storage of the encrypted parameters and caches under training_data_dir (serialize_and_store, deserialize_and_load with Kaiming / zero / one initialization of a missing parameter, import_scalar_param for pretrained values) and the rotation-key generators of the training primitives.

Subclassed by FHEONTrainBackwardPassController, FHEONTrainForwardPassController

Public Functions

inline FHEONTrainSupportController(CryptoContext<DCRTPoly> ctx)
void setContext(CryptoContext<DCRTPoly> &in_context)

ANN Batch Controller for managing homomorphic batched ANN operations.

This class provides high-level functions for performing batched neural network operations in the encrypted domain using homomorphic encryption (FHE). It supports operations such as:

  • Batched convolution layers

  • Batched average and global pooling

  • Batched fully connected (linear) layers

  • Batched activation functions (e.g., ReLU)

The class handles multiple input channels and multiple batches simultaneously, using optimized rotation and packing strategies to minimize the number of homomorphic operations while preserving the correct layout of data.

Note

  • All operations assume ciphertext packing follows a contiguous layout: [batch0: channel_i feature map][batch1: channel_i feature map]…

  • Designed for high-throughput FHE-based ANN inference pipelines.

inline void setEncryptedZeros(Ctext &zero)
inline void setEncryptedOnes(Ctext &ones)
void welcomeToTrainingMessage()
vector<int> generate_convolution_batch_rotation_positions(int batchSize, int inputWidth, int kernelWidth, int padding = 0, int stride = 1)

Generates rotation positions for the convolution operation in the encrypted domain. This function calculates the necessary rotation indices based on the batch size, input width, kernel width, padding, and stride. The generated rotation positions are used to align the encrypted data correctly during the convolution process.

Parameters:
  • batchSize – The number of batches encoded in the input ciphertexts.

  • inputWidth – The width (and height) of the input feature maps (assumed square).

  • kernelWidth – The width (and height) of the convolution kernel (assumed square).

  • padding – The number of zero-padding slots applied around the input feature map.

  • stride – The stride length used for the convolution operation.

Returns:

vector<int> A vector of rotation positions required for the convolution operation.

vector<int> generate_avgpool_batch_optimized_rotation_positions(int batchSize, int inputWidth, int kernelWidth, int stride = 2, bool globalPooling = false, int rotationIndex = 16)

Generate rotation positions for optimized average pooling operations.

This function calculates the rotation positions required for performing average pooling operations on batched encrypted data. It supports both global pooling and standard average pooling, optimizing the rotation indices based on the input dimensions, kernel size, and stride.

Parameters:
  • batchSize – Number of batches encoded in the input ciphertexts.

  • inputWidth – Width (and height) of the input feature maps (assumed square).

  • kernelWidth – Width (and height) of the pooling kernel (assumed square).

  • stride – Stride length used for the pooling operation.

  • globalPooling – Flag indicating whether global pooling is used.

  • rotationIndex – Index for rotation optimization.

Returns:

vector<int> Vector of rotation positions required for the pooling operation.

vector<int> generate_linear_batch_rotation_positions(int batchSize, vector<int> outputSizes, vector<int> inputSizes, int rotationIndex = 100)

Generate rotation positions for batched fully connected layers.

This function calculates the rotation positions required for performing fully connected layer operations on batched encrypted data. It considers the input and output sizes, as well as the batch size, to determine the optimal rotation indices for aligning data during the operation.

Parameters:
  • batchSize – Number of batches encoded in the input ciphertexts.

  • outputSizes – Vector of output sizes for each layer.

  • inputSizes – Vector of input sizes for each layer.

  • rotationIndex – Index for rotation optimization.

Returns:

vector<int> Vector of rotation positions required for the fully connected layer.

vector<int> generate_normalization_rotation_positions(int inputSize)

Generate rotation positions for normalization layers.

This function calculates the rotation positions required for performing normalization operations (e.g., batch normalization) on encrypted data. It generates indices based on the input size to optimize the rotations needed for computing mean and variance across the feature maps.

Parameters:

inputSize – Size of the input feature maps (width × height).

Returns:

vector<int> Vector of rotation positions required for normalization.

vector<int> generate_backpass_convolution_rotation_positions(int inputSize, int kernelWidth)

Generate rotation positions for backpropagation in convolution layers.

This function calculates the rotation positions required for performing backpropagation operations in convolution layers on encrypted data. It generates indices based on the input size and kernel width to optimize the rotations needed for computing gradients during the training process.

Parameters:
  • inputSize – Size of the input feature maps (width × height).

  • kernelWidth – Width (and height) of the convolution kernel (assumed square).

Returns:

vector<int> Vector of rotation positions required for backpropagation in convolution layers.

void serialize_and_store(Ctext &encryptedInput, string &fileName)

Serialization and storage function. This function serializes an encrypted ciphertext and stores it in a file for later retrieval.

Deliberately does NOT catch/swallow failures here (std::filesystem remove / create_directory throwing, or SerializeToFile returning false all propagate/throw out of this call) &#8212; a caller that silently ignores a failed write here leaves whatever ciphertext was PREVIOUSLY at that path (from an earlier, possibly incompatible run) sitting there unchanged, ready to be deserialize_and_load’d later as if it were fresh. On a shared filesystem (NFS/scratch storage under concurrent jobs) that silent failure mode is exactly what produced a real “Ciphertexts were not created in the same CryptoContext” crash, hours into a run, in a spot with no obvious connection to this function &#8212; failing loudly and immediately here, at the actual point of failure, is far cheaper to diagnose than that.

Parameters:
  • encryptedInput – The encrypted ciphertext to be serialized and stored.

  • fileName – The name of the file where the encrypted ciphertext will be stored.

Ctext deserialize_and_load(string &fileName, double fan_in = 1.0, int vectorSize = -1)
void import_scalar_param(double value, string fileName, int vectorSize = -1)