Diagonal ANN Controller
Single-input primitives evaluated over channel diagonals (FHEON-D), with the interface of FHEONANNStandardController. See Diagonal Primitives (FHEON-D).
-
class FHEONANNDiagonalController : protected FHEONANNStandardController
Diagonal ANN controller: the ANN layers evaluated over channel diagonals (FHEON-D).
It offers only the optimized primitives, with the names and arguments they have in FHEONANNStandardController (the standard controller keeps every primitive and declares these exactly the same way), so a model written with them switches implementation by changing only the controller type: he_convolution: 3x3 “same” convolution over channel diagonals (padding 1), or “valid” kernelWidth x kernelWidth convolution on a strided layout (padding 0, see FHEONLayout) he_convolution_and_shortcut_optimized: stride-2 3x3 convolution and 1x1 shortcut, outputs with half the slots he_avgpool: 2x2 pooling, compacted onto layout.outputSlots slots, or left in place on a strided layout (layout.rowStride set); it returns window sums, avgpool_weight_scale() is the factor for the next weights he_globalavgpool: channel averages gathered into the first slots he_linear: diagonals with baby-step giant-step rotations, inputs anywhere (layout.inputPositions) he_relu: scalar scaling, optionally normalized output
Public Functions
-
inline FHEONANNDiagonalController(CryptoContext<DCRTPoly> &ctx)
-
vector<int> generate_convolution_rotation_positions(int inputWidth, int inputChannels, int outputChannels, int kernelWidth, int padding, int stride, const FHEONLayout &layout = FHEONLayout())
Keys of he_convolution and he_convolution_and_shortcut_optimized. padding 0: strided “valid” layout. Otherwise channel-diagonal convolution (stride 1 or 2, layout.channelGroup for stride 2); a stride-1 layer with fewer input than output channels also gets the rotations replicating its input (on layout.inputSlots slots, outputChannels * inputWidth^2 when 0).
-
vector<int> generate_avgpool_rotation_positions(int inputWidth, int kernelWidth, int stride, int inputChannels, const FHEONLayout &layout = FHEONLayout())
Keys of he_avgpool (strided layout when layout.rowStride is set, compacted onto layout.outputSlots slots otherwise) or, when kernelWidth == inputWidth, of he_globalavgpool.
-
vector<int> generate_linear_rotation_positions(int maxFCLayeroutputs, int rotationPosition, const FHEONLayout &layout = FHEONLayout())
Keys of he_linear / he_linear_optimized for maxFCLayeroutputs outputs on a layout.inputSlots-slot input (num_slots when 0); rotationPosition is unused.
-
Ctext he_convolution(Ctext &encryptedInput, vector<vector<Ptext>> &kernelData, Ptext &biasInput, int inputWidth, int inputChannels, int outputChannels, int kernelWidth, int padding = 0, int stride = 1, const FHEONLayout &layout = FHEONLayout())
Convolution. padding 1: 3x3 “same” stride-1 convolution over channel diagonals (stride 2 goes through he_convolution_and_shortcut_optimized). padding 0: “valid” kernelWidth x kernelWidth convolution on the strided layout of layout (see he_convolution_layout; kernel from encode_convolution_kernel with padding 0).
-
vector<Ctext> he_convolution_and_shortcut_optimized(const Ctext &encryptedInput, const vector<vector<Ptext>> &kernelData, const vector<Ptext> &shortcutKernelData, Ptext &biasVector, Ptext &shortcutBiasVector, int inputWidth, int inputChannels, int outputChannels, const FHEONLayout &layout = FHEONLayout())
3x3 stride-2 convolution and 1x1 stride-2 shortcut (see he_convolution_and_shortcut_diagonal); half the slots out.
-
Ctext he_avgpool(Ctext encryptedInput, int imgCols, int outputChannels, int kernelWidth = 2, int stride = 2, const FHEONLayout &layout = FHEONLayout())
2x2 stride-2 pooling returning window sums (avgpool_weight_scale gives the 1/4 for the next weights): left in place on the strided layout when layout.rowStride is set (rows layout.rowStride slots apart), compacted onto layout.outputSlots slots (a quarter of the input when 0) otherwise.
-
Ctext he_globalavgpool(Ctext &encryptedInput, int inputWidth, int outputChannels, int kernelWidth, int rotatePositions, const FHEONLayout &layout = FHEONLayout())
Global average pooling: the average of channel c in slot c (two levels). kernelWidth and rotatePositions are unused.
-
Ctext he_linear(Ctext &encryptedInput, vector<Ptext> &weightMatrix, Ptext &biasInput, int inputSize, int outputSize, int rotatePositions, const FHEONLayout &layout = FHEONLayout())
Linear layer over the flattened diagonals of encode_linear_weights (see he_linear_diagonal); the output has weightMatrix.size() slots. rotatePositions is unused.
-
Ctext he_relu(Ctext &encryptedInput, double scale, int vectorSize, int polyDegree = 59, bool normalizedOutput = false)
ReLU: scale * ReLU(x / scale) with a scalar scaling, or ReLU(x / scale) within [0, 1] with normalizedOutput. vectorSize is unused.
-
int convolution_kernel_level(const Ctext &encryptedInput, int inputWidth, int stride = 1, const FHEONLayout &layout = FHEONLayout())
-
double avgpool_weight_scale(int kernelWidth)
The poolings return window sums: the next layer multiplies its weights by 1 / kernelWidth^2.
-
vector<vector<Ptext>> encode_convolution_kernel(FHEONHEController &heController, vector<vector<vector<vector<double>>>> &kernelData, int inputWidth, int numSlots, int encodeLevel, int stride = 1, int padding = 1, const FHEONLayout &layout = FHEONLayout())
Encode a convolution kernel. padding 0: strided “valid” layout of he_convolution (flattened [sub shift][diagonal]). Otherwise channel diagonals: numSlots is the slot count of the input ciphertext; stride 2 encodes for the half-slot output of he_convolution_and_shortcut_optimized; a stride-1 layer with fewer input than output channels gets a replicated input.
-
Ptext encode_convolution_bias(FHEONHEController &heController, vector<double> &biasData, int outputWidth, int numSlots, int encodeLevel, const FHEONLayout &layout = FHEONLayout())
Encode a convolution bias for an output of numSlots slots (the strided layout when layout.rowStride is set).
-
vector<Ptext> encode_shortcut_kernel(FHEONHEController &heController, vector<vector<double>> &kernelData, int inputWidth, int numSlots, int encodeLevel, const FHEONLayout &layout = FHEONLayout())
Encode the 1x1 stride-2 shortcut kernel as diagonals; numSlots is the slot count of the input ciphertext.
-
vector<Ptext> encode_linear_weights(FHEONHEController &heController, vector<vector<double>> &weightData, int numSlots, int encodeLevel, const FHEONLayout &layout = FHEONLayout())
Encode a linear layer as its diagonals, flattened for he_linear_optimized: input i in the slots layout.inputPositions[i] (slot i when empty) of a numSlots-slot ciphertext, weights times layout.weightScale.
-
Ptext encode_linear_bias(FHEONHEController &heController, vector<double> &biasData, int numSlots, int encodeLevel, const FHEONLayout &layout = FHEONLayout())
Encode the bias of he_linear_optimized (output k in slot k of the output ciphertext).
-
void setContext(CryptoContext<DCRTPoly> &in_context)
FHE Controller for defining and managing homomorphic ANN functions.
This class provides different methods for convolution, pooling, fully connected relu, etc for neural network development on encrypted data using FHE.
Set the global crypto context.
- Parameters:
in_context – The crypto context to set.
-
inline void setNumSlots(int numSlots)
Public Members
-
int num_slots
-
inline FHEONANNDiagonalController(CryptoContext<DCRTPoly> &ctx)