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(self: pyhelayers.TTFunctionEvaluator, src: pyhelayers.CTileTensor, lower_bound: float, upper_bound: float, bit_resolution: int = 5) pyhelayers.CTileTensor #
The same as FunctionEvaluator::inverse, but works on a CTileTensor object as source, CTile-wise. For more details, see documentation of FunctionEvaluator::inverse.
- 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 consumig 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 documnetation 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.
- sigmoid3_in_place(self: pyhelayers.TTFunctionEvaluator, src: pyhelayers.CTileTensor) None #
An approximation of sigmoid by an order 3 polynomial, in place.
- Parameters:
src (CTileTensor) – A CTileTensor to calculate its sigmoid.
- sigmoid7_in_place(self: pyhelayers.TTFunctionEvaluator, src: pyhelayers.CTileTensor) None #
An approximation of sigmoid by an order 7 polynomial, in place.
- Parameters:
src (CTileTensor) – A CTileTensor to calculate its sigmoid.
- sigmoid9_in_place(self: pyhelayers.TTFunctionEvaluator, src: pyhelayers.CTileTensor) None #
An approximation of sigmoid by an order 9 polynomial, in place.
- Parameters:
src (CTileTensor) – A CTileTensor to calculate its sigmoid.
- 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 sigmoid3InPlace(CTileTensor &src) const#
The same as FunctionEvaluator::sigmoid3InPlace, but works on a CTileTensor object as source, CTile-wise.
For more details, see documentation of FunctionEvaluator::sigmoid3InPlace.
- Parameters:
src – The input tensor
-
void sigmoid7InPlace(CTileTensor &src) const#
The same as FunctionEvaluator::sigmoid7InPlace, but works on a CTileTensor object as source, CTile-wise.
For more details, see documentation of FunctionEvaluator::sigmoid7InPlace.
-
void sigmoid9InPlace(CTileTensor &src) const#
The same as FunctionEvaluator::sigmoid9InPlace, but works on a CTileTensor object as source, CTile-wise.
For more details, see documentation of FunctionEvaluator::sigmoid9InPlace.
-
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)#
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.
-
void inverse(CTileTensor &src, double lowerBound, double upperBound, int bitResolution = 5) const#
The same as FunctionEvaluator::inverse, but works on a CTileTensor object as source, CTile-wise.
For more details, see documentation of FunctionEvaluator::inverse.
-
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:
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
-
TTFunctionEvaluator(const HeContext &he)#