Compiler IR
Compile represents a compiled function as an MLIR module whose operations belong to FHElium dialects. The compiler is a native MLIR library built from csrc/compiler and loaded in Python as fhelium._mlir; its dialects, passes, type inference, and verifiers run in native code, and Python reaches them through the MLIR Python bindings. This page defines the module structure, the dialects, the typed CKKS value state, and the materials through which a function reads values that are not its arguments.
Module structure
A captured module contains one public function, @entry, whose arguments are the Ciphertext arguments of the call and whose results are the CKKS values the function returns. The function attribute fhelium_ckks.chain holds the modulus chain of the CKKS configuration. Preparation outlining adds a second public function, @entry_prepare, named by the attribute fhelium_ckks.preparation of @entry. A bootstrap adds private functions that hold the bootstrap algorithm.
The following module multiplies a ciphertext by a plaintext encoded from a public Tensor and rescales the product. It is the module after the default pipeline and preparation outlining; the chain alias and the type parameters are printed as the compiler prints them, with long lines truncated:
func.func @entry(%arg0: !fhelium_ckks.ciphertext<chain = #chain, depth = 0, scale = 34608298348641.012 : f64, domain = coefficient, residues = standard, components = 2>) -> !fhelium_ckks.ciphertext<chain = #chain, depth = 1, scale = 1105505280162.5217 : f64, domain = ntt, residues = montgomery, components = 2> attributes {fhelium_ckks.chain = #chain, fhelium_ckks.preparation = @entry_prepare} {
%0 = fhelium_ckks.material "prepared_0" : !fhelium_ckks.plaintext<chain = #chain, depth = 0, scale = 34608298348641.012 : f64, encoding = rns, domain = ntt, residues = montgomery>
%1 = fhelium_ckks.to_ntt %arg0 : ...
%2 = fhelium_ckks.multiply_plaintext %1, %0 : ...
%3 = fhelium_ckks.rescale %2 : ...
return %3 : ...
}
func.func @entry_prepare() -> !fhelium_ckks.plaintext<...> attributes {fhelium_ckks.chain = #chain} {
%0 = fhelium_ckks.material "tensor_0" : tensor<8192xf64>
%1 = fhelium_ckks.tensor_call "aten::mul.Tensor"(%0) {descriptor = "..."} : (tensor<8192xf64>) -> tensor<8192xf64>
%2 = fhelium_ckks.encode %1 {chain = #chain, depth = 0 : i64, scale = 34608298348641.012 : f64} : ...
%3 = fhelium_ckks.to_rns %2 : ...
%4 = fhelium_ckks.to_montgomery %3 : ...
%5 = fhelium_ckks.to_ntt %4 : ...
return %5 : ...
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
CapturedFunction.to_text() prints the module, and CapturedFunction.module exposes it as an MLIR Module for traversal and rewriting.
Dialects
| Dialect | Operations and types |
|---|---|
fhelium_ckks | CKKS ciphertext, plaintext, compressed-plaintext, and key types; the modulus-chain attribute; evaluator operations, representation transitions, rescale and modulus switching, bootstrap calls, materials, and calls of PyTorch operators on public Tensors |
fhelium_logical | Slot-vector arithmetic on ciphertexts and public Tensors (add, subtract, multiply, negate, add_scalar, multiply_scalar, roll, sum) whose relinearizations, rescales, and alignments are placed by the pipeline; a result type is the state after every pending step |
fhelium_rns | Residue arithmetic, component packing, rescale and basis conversion, key-switch digit products, numerical tables, and fused regions |
fhelium_ntt | Forward and inverse negacyclic NTT transitions and the NTT-domain automorphism |
fhelium_dist | The process-group type and the ciphertext all-reduce all_reduce_add_ciphertext |
fhelium_memory | transfer, which copies a value's payload to a device or host memory space |
exec | Execution schedules over typed storage buffers: operator calls, kernel launches, allocations, views, copies, materials, collectives, and calls |
Capture produces fhelium_ckks operations, together with fhelium_dist and fhelium_memory operations for collectives and transfers in the source. fhelium_logical serves modules constructed directly at the slot-vector level. Backend lowering rewrites CKKS operations into fhelium_rns and fhelium_ntt operations, and linking produces exec functions. Upstream MLIR dialects such as func, arith, and scf appear alongside them.
Typed value state
A fhelium_ckks value type records the state that determines how its residues are interpreted:
| Type | Parameters |
|---|---|
!fhelium_ckks.ciphertext | chain, depth, actual scale, polynomial domain (coefficient or ntt), residue representation (standard or montgomery), component count, modulus basis (Q or QP), batch shape |
!fhelium_ckks.plaintext | chain, depth, actual scale, encoding (integer_coefficients or rns), polynomial domain, residue representation, modulus basis, batch shape |
!fhelium_ckks.compressed_plaintext | chain, depth, actual scale, domain, residue representation, compression layout (cyclic, contiguous, or strided_sparse), unique count, modulus basis, batch shape |
!fhelium_ckks.key | key role, prime ids of the key rows, and the digit partition of the Q prime ids |
Depth is the consumed-depth index of the modulus chain: depth #fhelium_ckks.chain lists the Q and P primes, the Q basis of every depth as prime ids, the default scale of each depth, the binary64 rescale ratio
Depth fhelium_logical and in encodes whose placement is left to depth and scale assignment; captured modules carry assigned states, because capture records the state Eager assigns.
Every fhelium_ckks evaluator operation infers its result type from its operand types and attributes, so constructing an operation computes the same state transition that the corresponding Eager method applies. A multiplication of NTT/Montgomery ciphertexts at scales relinearize produces two components; rescale advances the depth and divides the scale by
Materials
A fhelium_ckks.material operation reads a value that the function does not receive as a Ciphertext argument. Its result names a symbol, and the symbol is bound to a live object when the function runs:
| Bound object | Material type |
|---|---|
Evaluation key (RotationKey, RelinearizationKey, ConjugationKey, KeySwitchKey) | !fhelium_ckks.key with its role and layout |
| Public Tensor | Builtin ranked tensor type |
Fixed Ciphertext, Plaintext, or CompressedPlaintext | The value's fhelium_ckks type |
| Process group | !fhelium_dist.group |
| Result of the preparation function | The prepared value's type, under the symbol prepared_<i> |
CapturedFunction.materials maps symbols to the bound objects. A symbol is generated from the value's role, for example rotation_key_3, relinearization_key, or tensor_0, unless material_names assigns one.
fhelium_ckks.tensor_call records a PyTorch ATen operator applied to public Tensors whose result an Engine operation consumes. Its target names the operator overload, for example aten::mul.Tensor, and its descriptor encodes the positional and keyword arguments as JSON, in which {"operand": i} stands for operand i. Recording these calls lets the function recompute derived Tensors from its materials instead of storing their values.
Preparation function
An operation is ciphertext-independent when none of its operands depends on an argument of @entry; its results depend only on materials and constants. Preparation outlining moves the ciphertext-independent operations whose results the ciphertext computation uses into @entry_prepare, returns those results, and replaces each use in @entry with the material prepared_<i> of the same type. Plaintext encoding from public Tensors, Tensor arithmetic on weights, and compressed-plaintext preparation therefore run in the preparation function, while @entry contains only operations on ciphertexts and prepared materials.
The preparation function runs on the host Torch path through the Eager Engine. It runs once when a specialization is prepared and again when a material it reads is replaced or modified. Plaintext encoding rounds from the Engine's random stream in the order of the captured calls.
Bootstrap callees
fhelium_ckks.bootstrap @callee(%x) refreshes a ciphertext through the private function @callee, which holds the captured bootstrap algorithm: modulus raising, CoeffsToSlots, EvalMod, and SlotsToCoeffs over its own materials; preparation outlining leaves private functions unchanged. The result type is the callee's result type, so the depth transition
Lowered operations and tables
After Backend lowering, the function computes with fhelium_rns and fhelium_ntt operations on the same fhelium_ckks value types. Their numerical operands are fhelium_rns.table values, each described by the chain and the prime ids it serves: RNS parameters of a basis, NTT tables, rescale tables from depth fhelium_rns.fused operation holds a region of RNS and NTT operations and names the generated-kernel implementation that executes it.
Reference semantics
fhelium.compile.mlir.execute(captured, engine, arguments) interprets a fhelium_ckks function by calling, for each operation, the Engine method it was captured from, and runs the preparation function first when its results are not bound. With check_types=True, it compares the Eager state of each result with its MLIR type and raises StateMismatch on a difference. The interpreter defines the meaning of the dialect, and a compiled function's residues equal those of the interpreter, except where rotation hoisting changes the rounding order of key switching.
Related concepts
- Compile pipeline describes capture, the pass pipeline, lowering, linking, and specialization.
- Value-state transitions defines the state each type records.
- Configuration and modulus chain defines depths, prime groups, and rescale ratios.
- Compiled execution describes the exec schedules that linking produces.