35 KiB
module concrete.fhe.mlir.context
Declaration of Context class.
Global Variables
- MAX_EXTRACTABLE_BIT
- MIN_EXTRACTABLE_BIT
- MAXIMUM_TLU_BIT_WIDTH
class Context
Context class, to perform operations on conversions.
method __init__
__init__(context: <locals>Context, graph: Graph, configuration: Configuration)
method add
add(resulting_type: ConversionType, x: Conversion, y: Conversion) → Conversion
method array
array(resulting_type: ConversionType, elements: List[Conversion]) → Conversion
method assign_static
assign_static(
resulting_type: ConversionType,
x: Conversion,
y: Conversion,
index: Sequence[Union[int, integer, slice]]
)
method attribute
attribute(resulting_type: ConversionType, value: Any) → Attribute
Create an MLIR attribute.
Args: resulting_type (ConversionType): type of the attribute
value (Any): value of the attribute
Returns: MlirAttribute: resulting MLIR attribute
method best_chunk_ranges
best_chunk_ranges(
x: Conversion,
x_offset: int,
y: Conversion,
y_offset: int
) → List[Tuple[int, int]]
Calculate best chunk ranges for given operands.
Args: x (Conversion) lhs of the operation
x_offset (int) lhs offset
y (Conversion) rhs of the operation
y_offset (int) rhs offset
Returns: List[Tuple[int, int]]: best chunk ranges for the arguments
method bitwise
bitwise(
resulting_type: ConversionType,
x: Conversion,
y: Conversion,
operation: Callable[[int, int], int]
) → Conversion
method bitwise_and
bitwise_and(
resulting_type: ConversionType,
x: Conversion,
y: Conversion
) → Conversion
method bitwise_or
bitwise_or(
resulting_type: ConversionType,
x: Conversion,
y: Conversion
) → Conversion
method bitwise_xor
bitwise_xor(
resulting_type: ConversionType,
x: Conversion,
y: Conversion
) → Conversion
method broadcast_to
broadcast_to(x: Conversion, shape: Tuple[int, ])
method cast
cast(resulting_type: ConversionType, x: Conversion) → Conversion
method cast_to_original_bit_width
cast_to_original_bit_width(value: Conversion) → Conversion
Cast a value to its original bit width using multiplication and reinterpretation.
method compare_with_subtraction
compare_with_subtraction(
resulting_type: ConversionType,
subtraction: Conversion,
accept: Set[Comparison]
) → Conversion
Apply the final comparison table and return comparison result.
method comparison
comparison(
resulting_type: ConversionType,
x: Conversion,
y: Conversion,
accept: Set[Comparison]
) → Conversion
Compare two encrypted values.
Args: resulting_type (ConversionType): resulting type
x (Conversion): lhs of comparison
y (Conversion): rhs of comparison
accept (Set[Comparison]): set of accepted comparison outcomes
Returns: Conversion: result of comparison
method comparison_with_chunks
comparison_with_chunks(
resulting_type: ConversionType,
x: Conversion,
y: Conversion,
accept: Set[Comparison]
) → Conversion
Compare encrypted values using chunks.
Idea: split x and y into small chunks compare the chunks using table lookups reduce chunk comparisons to a final result
method comparison_with_chunks_equals
comparison_with_chunks_equals(
resulting_type: ConversionType,
x: Conversion,
y: Conversion,
accept: Set[Comparison],
x_offset: int,
y_offset: int,
x_was_signed: bool,
y_was_signed: bool,
chunk_ranges: List[Tuple[int, int]]
) → Conversion
Check equality of encrypted values using chunks.
method comparison_with_subtraction_trick
comparison_with_subtraction_trick(
resulting_type: ConversionType,
x: Conversion,
y: Conversion,
accept: Set[Comparison],
x_minus_y_dtype: Integer
) → Conversion
Compare encrypted values using subtraction trick.
Idea: x [.] y <==> (x - y) [.] 0 where [.] is one of <,<=,==,!=,>=,>
Additional Args: x_minus_y_dtype (Integer): minimal dtype that can be used to store x - y without overflows
method concatenate
concatenate(
resulting_type: ConversionType,
xs: List[Conversion],
axis: Optional[int]
) → Conversion
method constant
constant(resulting_type: ConversionType, data: Any) → Conversion
method conv2d
conv2d(
resulting_type: ConversionType,
x: Conversion,
weight: Conversion,
bias: Optional[Conversion],
strides: Sequence[int],
dilations: Sequence[int],
pads: Sequence[int],
group: int
)
method convert_to_chunks_and_map
convert_to_chunks_and_map(
resulting_scalar_type: ConversionType,
resulting_shape: Tuple[int, ],
chunk_ranges: List[Tuple[int, int]],
x: Conversion,
x_offset: int,
y: Conversion,
y_offset: int,
mapper: Callable
) → List[Conversion]
Extract the chunks of two values, pack them in a single integer and map the integer.
Args: resulting_scalar_type (ConversionType): scalar type of the results
resulting_shape (ConversionType): shape of the output of the operation
chunk_ranges (List[Tuple[int, int]]): chunks ranges for the operation
x (Conversion): first operand
y (Conversion): second operand
mapper (Callable): mapping function
x_offset (int, default = 0): optional offset for x during chunk extraction
y_offset (int, default = 0): optional offset for x during chunk extraction
Returns: List[Conversion]: result of mapping chunks of x and y
method dot
dot(resulting_type: ConversionType, x: Conversion, y: Conversion) → Conversion
method dynamic_tlu
dynamic_tlu(
resulting_type: ConversionType,
on: Conversion,
table: Conversion
) → Conversion
method eint
eint(width: int) → ConversionType
Get encrypted unsigned integer type (e.g., !FHE.eint<3>, !FHE.eint<5>).
method encrypt
encrypt(resulting_type: ConversionType, x: Conversion) → Conversion
method equal
equal(resulting_type: ConversionType, x: Conversion, y: Conversion) → Conversion
method error
error(highlights: Mapping[Node, Union[str, List[str]]])
Fail compilation with an error.
Args: highlights (Mapping[Node, Union[str, List[str]]]): nodes to highlight along with messages
method esint
esint(width: int) → ConversionType
Get encrypted signed integer type (e.g., !FHE.esint<3>, !FHE.esint<5>).
method extract_bits
extract_bits(
resulting_type: ConversionType,
x: Conversion,
bits: Union[int, integer, slice]
) → Conversion
method flatten
flatten(x: Conversion) → Conversion
method greater
greater(
resulting_type: ConversionType,
x: Conversion,
y: Conversion
) → Conversion
method greater_equal
greater_equal(
resulting_type: ConversionType,
x: Conversion,
y: Conversion
) → Conversion
method i
i(width: int) → ConversionType
Get clear signless integer type (e.g., i3, i5).
method identity
identity(resulting_type: ConversionType, x: Conversion) → Conversion
method index_static
index_static(
resulting_type: ConversionType,
x: Conversion,
index: Sequence[Union[int, integer, slice, ndarray, list]]
) → Conversion
method index_static_fancy
index_static_fancy(
resulting_type: ConversionType,
x: Conversion,
index: Sequence[Union[int, integer, slice, ndarray, list]]
) → Conversion
method index_type
index_type() → Type
Get index type.
method is_bit_width_compatible
is_bit_width_compatible(*args: Optional[ConversionType, Conversion]) → bool
Check if conversion types are compatible in terms of bit-width.
method less
less(resulting_type: ConversionType, x: Conversion, y: Conversion) → Conversion
method less_equal
less_equal(
resulting_type: ConversionType,
x: Conversion,
y: Conversion
) → Conversion
method location
location() → Location
Create an MLIR location from the node that is being converted.
method lsb
lsb(resulting_type: ConversionType, x: Conversion) → Conversion
method matmul
matmul(
resulting_type: ConversionType,
x: Conversion,
y: Conversion
) → Conversion
method maximum
maximum(
resulting_type: ConversionType,
x: Conversion,
y: Conversion
) → Conversion
method maxpool2d
maxpool2d(
resulting_type: ConversionType,
x: Conversion,
kernel_shape: Tuple[int, ],
strides: Sequence[int],
dilations: Sequence[int]
)
method minimum
minimum(
resulting_type: ConversionType,
x: Conversion,
y: Conversion
) → Conversion
method minimum_maximum_with_chunks
minimum_maximum_with_chunks(
resulting_type: ConversionType,
x: Conversion,
y: Conversion,
operation: str
) → Conversion
Calculate minimum or maximum between two encrypted values using chunks.
method minimum_maximum_with_trick
minimum_maximum_with_trick(
resulting_type: ConversionType,
x: Conversion,
y: Conversion,
x_minus_y_dtype: Integer,
intermediate_table: List[int]
) → Conversion
Calculate minimum or maximum between two encrypted values using minimum or maximum trick.
Idea: min(x, y) <==> min(x - y, 0) + y max(x, y) <==> max(x - y, 0) + y
Additional Args: x_minus_y_dtype (Integer): minimal dtype that can be used to store x - y without overflows
method mul
mul(resulting_type: ConversionType, x: Conversion, y: Conversion) → Conversion
method multi_tlu
multi_tlu(
resulting_type: ConversionType,
on: Conversion,
tables: Any,
mapping: Any
)
method multiplication_with_boolean
multiplication_with_boolean(
boolean: Conversion,
value: Conversion,
resulting_bit_width: int,
chunk_size: int,
inverted: bool = False
)
Calculate boolean * value using bits.
method multivariate_multi_tlu
multivariate_multi_tlu(
resulting_type: ConversionType,
xs: List[Conversion],
tables: Any,
mapping: Any
)
method multivariate_tlu
multivariate_tlu(
resulting_type: ConversionType,
xs: List[Conversion],
table: Sequence[int]
) → Conversion
method neg
neg(resulting_type: ConversionType, x: Conversion) → Conversion
method not_equal
not_equal(
resulting_type: ConversionType,
x: Conversion,
y: Conversion
) → Conversion
method ones
ones(resulting_type: ConversionType) → Conversion
method operation
operation(
operation: Callable,
resulting_type: ConversionType,
*args,
original_bit_width: Optional[int] = None,
**kwargs
) → Conversion
Create a conversion from an MLIR operation.
Args: operation (Callable): MLIR operation to create (e.g., fhe.AddEintOp)
resulting_type (ConversionType): type of the output of the operation
*args (Any): args to pass to the operation
original_bit_width (Optional[int], default = None): original bit width of the resulting conversion
*kwargs (Any): kwargs to pass to the operation
Returns: Conversion: resulting conversion
method pack_multivariate_inputs
pack_multivariate_inputs(xs: List[Conversion]) → Conversion
Packs inputs of multivariate table lookups.
Args: xs (List[Conversion]): operands
Returns: Conversion: packed operands
method reinterpret
reinterpret(
x: Conversion,
bit_width: int,
signed: Optional[bool] = None
) → Conversion
method relu
relu(resulting_type: ConversionType, x: Conversion) → Conversion
method reshape
reshape(x: Conversion, shape: Tuple[int, ]) → Conversion
method round_bit_pattern
round_bit_pattern(
resulting_type: ConversionType,
x: Conversion,
lsbs_to_remove: int,
exactness: Exactness,
overflow_detected: bool
) → Conversion
method shift
shift(
resulting_type: ConversionType,
x: Conversion,
b: Conversion,
orientation: str,
original_resulting_bit_width: int
) → Conversion
method sub
sub(resulting_type: ConversionType, x: Conversion, y: Conversion) → Conversion
method sum
sum(
resulting_type: ConversionType,
x: Conversion,
axes: Optional[int, Sequence[int]] = (),
keep_dims: bool = False
) → Conversion
method tensor
tensor(element_type: ConversionType, shape: Tuple[int, ]) → ConversionType
Get tensor type (e.g., tensor<5xi3>, tensor<3x2x!FHE.eint<5>>).
method tensorize
tensorize(x: Conversion) → Conversion
method tlu
tlu(resulting_type: ConversionType, on: Conversion, table: Sequence[int])
method to_signed
to_signed(x: Conversion) → Conversion
method to_signedness
to_signedness(x: Conversion, of: ConversionType) → Conversion
method to_unsigned
to_unsigned(x: Conversion) → Conversion
method transpose
transpose(
resulting_type: ConversionType,
x: Conversion,
axes: Sequence[int] = ()
)
method tree_add
tree_add(resulting_type: ConversionType, xs: List[Conversion]) → Conversion
method truncate_bit_pattern
truncate_bit_pattern(x: Conversion, lsbs_to_remove: int) → Conversion
method try_comparison_with_clipping_trick
try_comparison_with_clipping_trick(
resulting_type: ConversionType,
x: Conversion,
y: Conversion,
accept: Set[Comparison]
) → Optional[Conversion]
Compare encrypted values using clipping trick.
Idea: x [.] y <==> (clipped(x) - y) [.] 0 where [.] is one of <,<=,==,!=,>=,> or x [.] y <==> (x - clipped(y)) [.] 0 where [.] is one of <,<=,==,!=,>=,> where clipped(value) = np.clip(value, smaller.min() - 1, smaller.max() + 1)
Additional Args: smaller_minus_clipped_bigger_dtype (Integer): minimal dtype that can be used to store smaller - clipped(bigger) without overflows
clipped_bigger_minus_smaller_dtype (Integer): minimal dtype that can be used to store clipped(bigger) - smaller without overflows
smaller_bounds (Tuple[int, int]): bounds of smaller
smaller_is_lhs (bool): whether smaller is lhs of the comparison
smaller_is_rhs (bool): whether smaller is rhs of the comparison
method typeof
typeof(value: Union[ValueDescription, Node]) → ConversionType
Get type corresponding to a value or a node.
method where
where(
resulting_type: ConversionType,
condition: Conversion,
when_true: Conversion,
when_false: Conversion
) → Conversion
method zeros
zeros(resulting_type: ConversionType) → Conversion