TTFunctionEvaluator#
- class TTFunctionEvaluator#
This class is used to evaluate different available functions on CTileTensor objects, such as polynomial evaluation and sigmoid.
- compare(self: pyhelayers.TTFunctionEvaluator, a: pyhelayers.CTileTensor, b: pyhelayers.TileTensor, g_rep: int, f_rep: int, max_diff: float) pyhelayers.CTileTensor #
The same as FunctionEvaluator::compare, but works on tile tensors elementwise. The first argument must be a CTileTensor and the second a TileTensor (encrypted or plain). The usual broadcasting rules of binary operations apply.
For more details on the comparison operator, see documentation of FunctionEvaluator::compare
- Parameters:
a (CTileTensor) – First CTileTensor to compare.
b (TileTensor) – Second TileTensor to compare.
g_rep (int) – Controls the accuracy of the result. A higher g_rep value increases accuracy on the account of slower and deeper computation.
f_rep (int) – Controls the accuracy of the result. A higher f_rep value increases accuracy on the account of slower and deeper computation.
max_diff – An upper bound on |a-b|.
- Ptype max_diff:
float
- Raises:
ValueError – If “a” and “b” don’t have the same shape.
- inverse_positive(self: pyhelayers.TTFunctionEvaluator, src: pyhelayers.CTileTensor, lower_bound: float, upper_bound: float, bit_resolution: int = 5) pyhelayers.CTileTensor #
The same as FunctionEvaluator::inversePositive, but works on a CTileTensor object as source, CTile-wise. For more details, see documentation of FunctionEvaluator::inversePositive.
- Parameters:
src (CTileTensor) – CTileTensor to calculate its inverse.
lower_bound (double) – a lower bound on the tiles of src. The tighter this bound is, the more accurate the result will be. This lower bound must be non-negative.
upper_bound (double) – an upper upper bound on the tiles src. The tighter this bound is, the more accurate the result will be
bit_resolution (int) – Controls the accuracy of the result. A higher bit_resolution value will increase the accuracy of the result at the account of consuming more multiplication depth. Defaults to 5.
- Return type:
- poly_eval_in_place(*args, **kwargs)#
Overloaded function.
poly_eval_in_place(self: pyhelayers.TTFunctionEvaluator, src: pyhelayers.CTileTensor, coefs: numpy.ndarray[numpy.float64], type: pyhelayers.EvalType = <EvalType.MIN_DEPTH: 2>) -> None
Polynomial evaluation, in place. Evaluates the given plain polynomial on input “src”, and stores the result in “src”.
- param src:
The input of the polynomial. This will contain the result of the evaluation at the end of the function execution.
- type src:
CTileTensor
- param coefs:
The coefficients of the polynomial. coefs[0] is the free coefficient.
- type coefs:
numpy array of floats
- param type:
The type of the evaluation algorithm. See also the documentation of “EvalType”. Defaults to MIN_DEPTH.
- type type:
EvalType
poly_eval_in_place(self: pyhelayers.TTFunctionEvaluator, src: pyhelayers.CTileTensor, coefs: pyhelayers.CTileVector, normalized: bool = False) -> None
Polynomial evaluation, in place. Receives a polynomial with encrypted coefficients, evaluates it on the input “src” and stores the result in “src”.
- param src:
The input of the polynomial. This will contain the result of the evaluation at the end of the function execution.
- type src:
CTileTensor
- param coefs:
The (encrypted) coefficients of the polynomial. coefs[0] is the free coefficient.
- type coefs:
numpy array of floats
- param normalized:
If False, the polynomial to evaluate will be composed from the coefficients in “coeffs” vector only. Otherwise, an extra term, whose coefficient is 1 and whose power is len(coefs), will be added. Defaults to False.
- type normalized:
bool
- pow_in_place(self: pyhelayers.TTFunctionEvaluator, src: pyhelayers.CTileTensor, degree: int) None #
Computes src^degree, in place.
- Parameters:
src (CTileTensor) – A CTileTensor to calculate its power.
degree (int) – The required exponent.
- sigmoid_in_place(self: pyhelayers.TTFunctionEvaluator, src: pyhelayers.CTileTensor, type: pyhelayers.SigmoidType) None #
An approximation of sigmoid, in place. :param src: A CTileTensor to calculate its sigmoid. :type src: CTileTensor :param type: Specifies the degree of the approximating polynomial.
See also FunctionEvaluator::SigmoidType documentation.
- sign_in_place(self: pyhelayers.TTFunctionEvaluator, src: pyhelayers.CTileTensor, gRep: int, fRep: int, max_abs_val: float = 1, binary_res: bool = False) None #
Computes the (approximated) sign of src, in place. The sign is computed as g(g(…g(f(f(…f(x)))))), where g(x) and f(x) are two degree 7 polynomials. The number of times g(x) and f(x) appear in the composition can be controlled by gRep and fRep arguments, respectively. all values in src must be in the range [-max_abs_val, max_abs_val].
- Parameters:
src (CTileTensor) – A CTileTensor to calculate its sign. All of its values must be in the range [-1, 1].
gRep (int) – How many repetitions of g(x).
fRep (int) – How many repetitions of f(x).
max_abs_val (float) – An upper bound on the absolute value of src. Defaults to 1.
binary_res (bool) – If True, the result will be close to 0 when src < 0 and close to 1 when src > 0. If False, the result will be close to -1 when src < 0 and close to 1 when src > 0. Defaults to False.
-
class TTFunctionEvaluator#
This class is used to evaluate different available functions on CTileTensor objects, such as polynomial evaluation and sigmoid.
Public Functions
-
TTFunctionEvaluator(const HeContext &he)#
Construct a new TT function evaluator object.
- Parameters:
he – the context
-
~TTFunctionEvaluator()#
Destroy the TT function evaluator object.
-
TTFunctionEvaluator(const TTFunctionEvaluator &src) = delete#
Deleted copy constructor.
- Parameters:
src – Source object
-
TTFunctionEvaluator &operator=(const TTFunctionEvaluator &src) = delete#
Deleted operator=.
- Parameters:
src – Source object
-
void polyEvalInPlace(CTileTensor &src, const std::vector<double> &coefs, EvalType type = DEFAULT) const#
The same as FunctionEvaluator::polyEvalInPlace, but works on a CTileTensor object as source, CTile-wise.
For more details, see documentation of FunctionEvaluator::polyEvalInPlace.
-
void polyEvalInPlace(CTileTensor &src, const std::vector<CTile> &coefs, bool normalized = false) const#
The same as FunctionEvaluator::polyEvalInPlace, but works on a CTileTensor object as source, CTile-wise.
For more details, see documentation of FunctionEvaluator::polyEvalInPlace.
-
void powInPlace(CTileTensor &src, int degree) const#
The same as FunctionEvaluator::powInPlace, but works on a CTileTensor object as source, CTile-wise.
For more details, see documentation of FunctionEvaluator::powInPlace.
-
void sigmoidInPlace(CTileTensor &src, SigmoidType type) const#
A polynomial approximation of sigmoid, in place.
- Parameters:
src – Ciphertext to calculate its sigmoid. The result will be stored here.
type – Specifies the degree of the approximating polynomial. See also SigmoidType documentation.
-
void signInPlace(CTileTensor &src, int gRep, int fRep, double maxAbsVal = 1, bool binaryRes = false) const#
The same as FunctionEvaluator::signInPlace, but works on a CTileTensor object as source, CTile-wise.
For more details, see documentation of FunctionEvaluator::signInPlace.
-
CTileTensor compare(const CTileTensor &a, const TileTensor &b, int gRep, int fRep, double maxPossibleAbsOfDiff) const#
Comparison of “a” and “b”, elementwise.
This method compares a CTileTensor a and a TileTensor b, and returns a CTileTensor having the same shape as a. The returned CTileTensor will approximately contian 1 in places where a>b, 0.5 in places where a=b and 0 in places where a<b.
- Parameters:
a – First CTileTensor to compare.
b – Second TileTensor to compare.
gRep – Controls the accuracy of the result. Increasing gRep will increase accuracy on the account of higher multiplication depth.
fRep – Controls the accuracy of the result. Increasing fRep will increase accuracy on the account of higher multiplication depth.
maxPossibleAbsOfDiff – An upper upper bound on |a-b|.
-
void inversePositive(CTileTensor &src, double lowerBound, double upperBound, int bitResolution = 5) const#
The same as FunctionEvaluator::inversePositive, but works on a CTileTensor object as source, CTile-wise.
For more details, see documentation of FunctionEvaluator::inversePositive.
-
void goldschmitInverseSqrt(CTileTensor &src, DimInt iterations, double maxAbsVal = 1, double inputScale = 1, double reuiredOutputScale = 1) const#
Calculates (an approximation of) requiredOutputScale/sqrt(src/inputScale), elementwise and in place.
The values of “src” must be positive values not larger than maxAbsVal.
- Parameters:
src – The values to calculate their inverse. The output will be stored in place.
iterations – Controls the accuracy of the result. A higher number of iterations increases the accuracy at the expense of slower and more deep computations.
maxAbsVal – A positive value specifying the maximum absolute value of src/inputScale.
inputScale – The scale of the input “src”. The actual values represented by “src” are the encrypted values divided by this scale.
requiredOutputScale – The output of the function is the actual 1/sqrt result multiplied by this “requiredOutputScale” value.
- Throws:
invalid_argument – If “maxAbsVal” is not positive.
-
void relu(CTileTensor &src, double minVal = -8.0, double maxVal = 8.0) const#
Calculates (an approximation of) ReLU(src) in place assuming that src is in [minVal,maxVal].
- Parameters:
src – The values to calculate ReLU on. The output will be stored in place.
minVal – minimal expected value in src
maxVal – maximal expected value in src
-
void sigmoidBySignScaled(CTileTensor &x, double maxAbsVal, double scale) const#
An approximation of sigmoid via an approximation of sign and supporting a pre-scaling of the input.
- Parameters:
x – Cipher to calculate its sigmoid. This will contain the sigmoid result at the end of the function execution.
maxAbsVal – An upper bound on the absolute value of src.
scale – x will be mutiplied by scale before computing its sigmoid.
-
void geluBySigmoid(CTileTensor &x, double maxAbsVal) const#
Calculates (an approximation of) GeLU(src) in place assuming that src is in [-maxAbsVal,maxAbsVal].
- Parameters:
x – The values to calculate GeLU on. The output will be stored in place
maxAbsVal – Maximal expected absolute value of value in the slots of x
-
void sqrt(CTileTensor &src, int bitResolution) const#
The same as FunctionEvaluator::sqrt, but works on a CTileTensor object as source, CTile-wise.
For more details, see documentation of FunctionEvaluator::sqrt.
-
void reshapeVectorHorizontalToVertical(CTileTensor &res, const CTileTensor &src) const#
Given a one dimensional (possibly interleaved) CTileTensor src of shape n/t, 1, return a CTileTensor res of shape 1/t, n s.t.
res[0,i] = src[i,0].
- Parameters:
res – an empty CTileTensor
src – one dimensional binary CTileTensor.
-
void partialSumsIndicatorsGetLayerZero(CTileTensor &res, const CTileTensor &indicators) const#
see partialSumsIndicatorsGetLayer
- Parameters:
res – an empty CTileTensor
indicators – one dimensional interleaved CTileTensor of shape [1~ / slotCount, n] that contains binary values (either 0 or 1)
-
void partialSumsIndicatorsGetLayerOne(CTileTensor &res, const CTileTensor &indicators) const#
see partialSumsIndicatorsGetLayer
- Parameters:
res – an empty CTileTensor
indicators – one dimensional interleaved CTileTensor of shape [1~ / slotCount, n] that contains binary values (either 0 or 1)
-
void partialSumsIndicatorsGetLayer(CTileTensor &res, const CTileTensor &layerOne, const CTileTensor &prevLayer, int len) const#
Given a one dimensional interleaved CTileTensor indicators of shape [1~ / slotCount, n] that contains binary values (either 0 or 1) and an integer len>=2, calculates a CTileTensor of shape [n~ / slotCount, n] s.t.
res[i,j]=1 iff indicators[i:i+len] contains exactly len 1’s, and 0 otherwise. To run this function with len=n you must run it before with len=n-1, and transfer to this function the result of the previous run (denoted by prevLayer). Furthermore, you need to transfer the result of partialSumsIndicatorsGetLayerOne which returns the result for len=1.
- Parameters:
res – an empty CTileTensor
layerOne – the result of partialSumsIndicatorsGetLayerOne
prevLayer – the result of running this function with len-1
len – number of elements to include in the partial sum calculation. len >= 2
-
void computeLagrangeBasis(CTileTensor &res, const CTileTensor &src, const std::vector<double> &data, const int index) const#
Computes Lagrange basis polynomial over a set of points, in a specific point.
- Parameters:
res – Output CTileTensor
src – point to compute the interpolation at
data – list of the points to interpolation on (the basis is 0 in all the points expect for one)
index – the index in which the interpolation should be 1
-
void sortAlongDim(CTileTensor &res, const SortNetwork &sortNetwork, DimInt dim, int gRep, int fRep, double maxPossibleAbsOfDiff = 1.0) const#
Sorts the given CTileTensor along the specified dimension in a decreasing order.
- Parameters:
res – The CTileTensor to sort. The result will be stored in place.
sortNetwork – An object specifying the network to sort with. This network consists of a set of binary comparison gates.
dim – The dimension to sort along.
gRep – Controls the accuracy of the comparisons. Higher gRep increases accuracy on the account of higher computation depth.
fRep – Controls the accuracy of the comparisons. Higher fRep increases accuracy on the account of higher computation depth.
maxPossibleAbsOfDiff – The maximum absolute difference between any two values in “res” that have the same index in “dim”.
-
void multiplyMany(CTileTensor &output, const std::vector<CTileTensor> &ctts) const#
Multiplies all CTileTensors in “ctts” and saves the resulting product “output”.
The multiplications are performed in a way that aims to minimize the number of bootstrapping operations and maximize the chain index of the result.
- Parameters:
output – The resulting product will be stored here.
ctts – The CTileTensors to multiply.
Public Static Functions
-
static void bootstrapAheadOfUncomposedComputationOfGivenDepth(CTileTensor &x, int depth)#
Bootstrap ahead of uncomposed computation to avoid multiple bootstraps inside the computation.
- Parameters:
x – The input for the computation that may be bootstrapped now.
depth – The depth of the expected uncomposed computation.
-
TTFunctionEvaluator(const HeContext &he)#