Source-linked AI summary
MOAI: Module-Optimizing Architecture for non-interactive secure transformer Inference
Linru Zhang, Xiangning Wang, Jun Jie Sim, Zhicong Huang, Jiahao Zhong, Huaxiong Wang, Pu Duan, Kwok-Yan Lam
TL;DR
Homomorphic encryption enables secure inference over encrypted inputs, but its computational overhead remains a bottleneck. MOAI combines consistent matrix packing, rotation-free Softmax and LayerNorm, and interleaved batching; it reports lower evaluation times than THOR and Powerformer.
Problem
Homomorphic-encryption inference protects encrypted inputs, but its high computational overhead limits practicality, particularly when many inputs must be processed.
Source: §1 · p. 1Method
MOAI is a plug-and-play FHE framework that combines consistent matrix packing, rotation-free Softmax and LayerNorm, and interleaved batching without modifying transformer components.
Source: §1 · pp. 2–3Results
MOAI reports 52.8% lower evaluation time than THOR and 55.7% lower time than Powerformer in the stated comparisons.
Source: §1 · p. 2Takeaways & Limitations
MOAI’s evaluation flow and algorithms are applicable to revised transformer architectures such as Powerformer and are extended to LLaMA-3-8B.
Source: §1 · p. 2, §7 · p. 10The paper reports a precision problem when using NEXUS’s piecewise-polynomial approximation for GELU.
Source: §4 · p. 7
Abstract
from arXiv · showhide
Privacy concerns have been raised in Large Language Models (LLM) inference when models are deployed in Cloud Service Providers (CSP). Homomorphic encryption (HE) offers a promising solution by enabling secure inference directly over encrypted inputs. However, the high computational overhead of HE remains a major bottleneck. To address this challenge, we propose MOAI, an efficient HE-based, non-interactive framework for secure transformer inference. MOAI gains significant efficiency improvement from: (1) a novel evaluation flow that combines column and diagonal packing with consistent strategies across all layers, eliminating expensive format conversions. (2) rotation-free algorithms for Softmax and LayerNorm that significantly reduce the number of costly HE rotations, removing 2,448 HE rotations in BERT-base inference. (3) Column packing removes rotations in plaintext-ciphertext matrix multiplications and interleaved batching further reduces the rotations in ciphertext-ciphertext matrix multiplications. MOAI uses at least 1.7x fewer HE rotations compared to the state-of-the-art works across all matrix multiplications of BERT-base. As a result, we achieve a $52.8 \%$ reduction in evaluation time compared to the state-of-the-art HE-based non-interactive secure transformer inference, THOR (Moon et al., CCS 25). We then apply MOAI on the Powerformer's framework and achieve a $55.7 \%$ reduction in evaluation time compared to Powerformer (Park et al., ACL 25), which approximates Softmax and LayerNorm with simpler functions in transformer and proposes HE-based non-interactive transformer inference. We report an amortized time of 2.36 minutes per input on a single GPU environment. We show the extendibility by applying MOAI in LLaMA-3-8B. Our implementation is publicly available as open source.
1 INTRODUCTION
MOAI addresses privacy risks and the high computational overhead of non-interactive homomorphic-encryption inference with a plug-and-play transformer framework. It combines consistent packing, rotation-reducing algorithms, and batching, reporting lower evaluation times than THOR and Powerformer.
- FHE enables non-interactive evaluation on encrypted client inputs, but its computational overhead limits practicality, especially when processing many inputs.The introduction identifies document classification as a representative application of encoder-only models such as BERT.Source: §1 · p. 1
- MOAI leaves transformer components unchanged, avoiding retraining through knowledge distillation.This plug-and-play design contrasts with Powerformer, which requires retraining through knowledge distillation and may be less generalizable to other architectures.Source: §1 · p. 2
- 52.8% lower evaluation time than THOR and 55.7% lower time than Powerformer are reported for MOAI in the same environment comparisons.MOAI reports an amortized 2.36 minutes per input on a single GPU and extends its methods to LLaMA-3-8B.Source: §1 · p. 2
- MOAI combines column and diagonal packing across layers to avoid format conversions and a separate encrypted matrix transpose.Its evaluation flow keeps feedforward outputs in column packing for the next layer.Source: §1 · p. 2
- Rotation-free Softmax and LayerNorm algorithms save 2,448 HE rotations in BERT-base inference by placing summation inputs in the same slots of different ciphertexts.The cited prior approaches use rotations to sum data packed in a ciphertext.Source: §1 · p. 2
- Column packing removes rotations in plaintext-ciphertext matrix multiplication, while interleaved batching further reduces rotations in ciphertext-ciphertext multiplication.MOAI uses at least 1.7x fewer HE rotations than state-of-the-art works across transformer matrix multiplications.Source: §1 · p. 3
2 PRELIMINARIES
The preliminaries establish notation and describe CKKS operations and transformer inference over encrypted data. They frame matrix multiplication, nonlinear functions, and encrypted token processing as core parts of the inference pipeline.
- The paper defines vector, matrix, rotation, and diagonal notation used in its later descriptions of encrypted matrix operations.Rot_k denotes a left rotation for positive k and a right rotation for nonpositive k; Diag_i denotes an upper diagonal of a square matrix.Source: §2 · p. 3
2.1 FULLY HOMOMORPHIC ENCRYPTION - CKKS SCHEME
- CKKS supports approximate arithmetic over complex vectors and is the encryption scheme briefly introduced for the paper’s FHE setting.The scheme is described as enabling computation on encrypted data.Source: §2.1 · p. 3
- A fresh ciphertext begins at level L, loses a level after homomorphic multiplication, and needs bootstrapping at level zero to continue evaluation.CKKS packs N/2 messages in one ciphertext for parallel operations across slots.Source: §2.1 · p. 3
2.2 TRANSFORMERS AND FHE-BASED SECURE TRANSFORMER INFERENCE
- The encoder architecture used as a benchmark has repeated attention, normalization, and feed-forward blocks, with matrix multiplication and nonlinear functions among its core computations.BERT is given as an encoder example.Source: §2.2 · p. 3
- Encrypted transformer inference takes encrypted tokens and returns encrypted results after homomorphic evaluation of the model.The described computations include plaintext-ciphertext and ciphertext-ciphertext matrix multiplication, plus Softmax, LayerNorm, and GELU.Source: §2.2 · p. 3
3 MATRIX PACKING, MULTIPLICATIONS AND INTERLEAVED BATCHING
MOAI combines column and diagonal matrix packing with interleaved batching to organize encrypted matrix operations and reduce rotations. Its comparison reports fewer matrix-multiplication rotations than prior FHE-based transformer inference works.
3.1 MATRIX PACKING AND MULTIPLICATIONS
- Column packing represents a matrix with ciphertexts for its columns, while diagonal packing represents a square matrix with ciphertexts for its diagonals.The paper combines these formats to transition between encrypted matrix operations.Source: §3.1 · p. 4
- The matrix-vector product in column packing sums columnwise products with replicated vector entries, while diagonal packing multiplies diagonals by rotated vector entries.The latter approach’s rotation count is reduced to O(√m) by the cited baby-step giant-step method.Source: §3.1 · p. 4
- MOAI computes QK⊺ from column-packed encrypted Q and K without transposing K, producing a diagonally packed result for the subsequent attention operations.The resulting diagonal packing matches the stated format for applying Softmax and then multiplying by column-packed V.Source: §3.1 · p. 4
3.2 INTERLEAVED BATCHING
- Interleaved batching arranges corresponding entries of multiple vectors together, and MOAI performs an internal rotation with one HE rotation instead of two.This halves the HE rotations needed for that functionality relative to the naive batching method described.Source: §3.2 · p. 5
3.3 COMPLEXITY COMPARISON
- 9648 HE rotations are used by MOAI across all matrix multiplications in BERT, 22.9x, 2.3x, and 1.7x fewer than NEXUS, THOR, and Powerformer, respectively.The comparison focuses on total HE rotations, identified as an expensive underlying operation.Source: §3.3 · p. 5
4 NON-LINEAR FUNCTION EVALUATION
MOAI’s nonlinear evaluations avoid HE rotations in Softmax and LayerNorm, saving 2,448 rotations with BERT-base parameters. Its Softmax preserves diagonal packing, while the section also specifies LayerNorm and GELU evaluation details.
- 2,448 HE rotations are saved in total by MOAI’s rotation-free Softmax and LayerNorm evaluations with BERT-base parameters.The section attributes the efficiency improvement to avoiding rotations in both nonlinear functions.Source: §4 · p. 5
- Softmax takes and returns diagonal-packed data, preserving the format needed by the next module while eliminating rotations.The algorithm uses the equality between column and diagonal sums as its key insight.Source: §4 · p. 6
- LayerNorm computes encrypted means and variances, then uses Goldschmidt division to obtain the inverse square root of the variance.The two scalar multiplications by 1/d can be merged to save one FHE level.Source: §4 · p. 6
- GELU evaluation approximates tanh with a degree-23 polynomial on [−20, 10], with domain extension available for larger input intervals.The passage says the degree-23 approximation is sufficient in the reported experiment.Source: §4 · p. 6
- MOAI’s Softmax exponentiates the packed values, sums them, applies Goldschmidt division to invert the sum, and normalizes each component.The algorithm describes the output as diagonal-packed.Source: §4 · p. 7
- The paper reports a precision problem with NEXUS’s piecewise-polynomial approximation of GELU.It refers readers to Appendix F.2 for the analysis.Source: §4 · p. 7
5 MOAI: FULL FLOW
MOAI’s full flow uses consistent packing formats across modules and avoids conversions between column and diagonal packing. It also optimizes bootstrapping placement, and the paper reports lower rotation counts than prior approaches.
- MOAI avoids conversions between column and diagonal packing, maintaining consistent formats across the full flow.The paper describes this flow as applicable to other transformer models, including decoder-only LLaMA-3-8B.Source: §5 · p. 7
- 9,648 HE rotations in the full flow are 23.2×, 2.56×, and 1.46× fewer than NEXUS, THOR, and Powerformer, respectively.MOAI preserves Softmax and LayerNorm rather than bypassing rotations by replacing those functions.Source: §5 · p. 7
- Bootstrapping ctsum before Goldschmidt division reduces Softmax depth from 20 to 10 layers, allowing preceding matrix multiplications to run at a lower level.The paper reports that one bootstrap on one ciphertext substantially decreases multiplication and overall runtime.Source: §5 · p. 7
- MOAI’s full diagram uses BERT-base-uncased parameters, with 12 transformer layers and 12 heads per layer.The caption also defines c as the number of classes.Source: Figure 2 · p. 8
6 EXPERIMENTAL RESULTS
MOAI reduces encrypted transformer inference time through packing and rotation optimizations, while preserving inference accuracy in the reported evaluation. The experiments compare its performance with THOR and Powerformer and assess end-to-end runtime and accuracy.
- The evaluation uses BERT-base fine-tuned on SST-2, QNLI, and RTE; GPU comparisons use a single A100.The model has 12 attention heads and layers, an embedding dimension of 768, and input length of 128 tokens.Source: §6 · p. 8, §6.2 · p. 9
- Up to 22× speedup for Softmax and 151× for LayerNorm versus prior works in the microbenchmark.The microbenchmark results are reported in Appendix H.1.Source: §6 · p. 8
6.1 END-TO-END INFERENCE EVALUATION
- 2.36 minutes per input on GPU and 9.6 minutes per input on CPU for BERT evaluation with up to 128 tokens.These are amortized results over 256 inputs; the evaluation uses a 12-layer BERT model.Source: §6.1 · p. 8, Table 4 · p. 9
6.2 COMPARISON WITH SOTAS.
- 52.8% reduction in total time versus THOR on a single A100 GPU, with the same ring dimension and slot size.The comparison uses MOAI’s rotation reduction and consistent packing strategies.Source: §6.2 · p. 9, Table 5 · p. 10
- 55.7% reduction in total evaluation time versus Powerformer under its parameter setting.MOAI’s packing methods and evaluation algorithms are applied to Powerformer, which replaces Softmax and LayerNorm with FHE-friendly functions and retrains the model.Source: §6.2 · p. 9, Table 6 · p. 10
6.3 ACCURACY.
- Encrypted-evaluation error grows over the first 6–7 layers and then stabilizes; the reported GLUE results show negligible impact on inference accuracy.The analysis compares encrypted layer outputs with plaintext results.Source: §6.3 · p. 10, Figure 3 · p. 10
7 CONCLUSION
MOAI combines consistent matrix packing, rotation-free Softmax and LayerNorm, and interleaved batching for encrypted transformer inference. The paper reports performance gains over THOR and applies the techniques to Powerformer.
- MOAI combines consistent matrix packing, rotation-free Softmax and LayerNorm evaluations, and interleaved batching to minimize HE rotations and remove format conversions.The paper reports that these techniques outperform THOR and can be applied to other revised transformer architectures.Source: §7 · p. 10
- MOAI’s techniques also apply to Powerformer, speeding up secure inference in this FHE-friendly transformer architecture.The paper identifies Powerformer as an example of another revised transformer-based architecture.Source: §7 · p. 10
8 REPRODUCIBILITY STATEMENT
The paper describes its reproducibility resources, discloses limited use of ChatGPT, and provides extended BERT-model preliminaries. The preliminaries specify the model components and key operations used in the paper.
- The implementation is linked through an anonymous GitHub repository, the experimental datasets are public, and the paper says its results are reproducible.Parameter settings, libraries, models, datasets, and hardware are detailed in Section 6.Source: §8 · p. 10
A USAGE OF LARGE LANGUAGE MODELS
- ChatGPT was used only to improve grammar and wording; the authors reviewed and revised the content and take responsibility for it.Source: A USAGE OF LARGE LANGUAGE MODELS · p. 14
B EXTENDED PRELIMINARIES: THE BERT MODEL
- The BERT preliminaries describe embeddings, attention, feed-forward blocks, LayerNorm, and classification, including the 12-layer BERT-base-uncased configuration.The text also defines GELU and notes its tanh approximation.Source: B EXTENDED PRELIMINARIES: THE BERT MODEL · p. 14
C.1 THE BABY-STEP-GIANT-STEP METHOD
MOAI uses baby-step-giant-step rotations, explicit packing-specific matrix-multiplication algorithms, and hoisting to reduce computation; it also examines naive batching’s incompatibility with ciphertext-ciphertext multiplication.
- Baby-step-giant-step reduces HE rotations to O(√m) by writing m = g × b and reusing b initial rotations to construct later ones.For k ≥ b, write k = αb + r with r < b.Source: §C.1 · p. 14
C.2 PSEUDO CODES IN SECTION 3
- The ciphertext-plaintext matrix-multiplication algorithm takes column-packed encrypted X and returns column-packed encrypted XW + B.The described implementation assumes m = N/2; sparse packing is noted for m < N/2 when m divides N/2.Source: §C.2 · p. 15
- For ciphertext-ciphertext multiplication, the specified algorithms map column-packed Q and K to diagonal-packed QK⊺, and diagonal-packed C with column-packed V to column-packed CV.These algorithms use baby-step-giant-step rotation scheduling for the encrypted matrix products.Source: §C.2 · pp. 15–16
C.3 FURTHER OPTIMIZATION BY HOISTING
- Hoisting improves amortized inference time by at most 2.29 seconds out of 141.3 seconds, a 1.62% improvement.In transformer inference, repeated rotations can improve by 16%–26% when the gadget-vector length m is 10–20.Source: §C.3 · p. 16
D.1 NAIVE BATCHING AND INTERLEAVED BATCHING
- Naive batching concatenates vectors contiguously and supports ciphertext-plaintext multiplication, but is incompatible with ciphertext-ciphertext multiplication because rotations must act internally on each vector.Moon et al. (2024) obtain internal rotations using two CKKS rotations and two plaintext-ciphertext multiplications.Source: §D.1 · p. 17
D.2 PROOF OF LEMMA 3.2
The appendix proves a batching lemma and describes interleaved packing and matrix-multiplication algorithms, including why row-wise nonlinear evaluations remain compatible with the rearrangement.
- The proof of Lemma 3.2 concludes by applying the lemma’s relation to each rotated vector.The supplied proof passage states that replacing each x(r) by Rotj(x(r)) completes the proof.Source: §D.2 · p. 17
D.3 INTERLEAVED BATCHING FOR MATRICES
- Interleaved batching stores entries from multiple matrices in shared interleaved vectors, with separate column and diagonal packing definitions.The appendix defines interleaved column packing for matrices and interleaved diagonal packing for square matrices.Source: §D.3 · p. 18
- Interleaved column and diagonal packing are compatible with all three homomorphic matrix-multiplication algorithms, whose inputs and outputs retain interleaved batching.The appendix provides modified algorithms for these products using interleaved batching.Source: §D.3 · p. 18
- Softmax, LayerNorm, and GELU algorithms remain unchanged under interleaved batching because they evaluate rows independently and preserve row permutations.Interleaved batching is a permutation of matrix rows.Source: §D.3 · p. 18
E COMPLEXITY COMPARISON
The complexity comparison counts key switches for BERT-base matrix multiplications across MOAI and prior FHE-based transformer systems. MOAI has the fewest key switches across the matrix multiplications evaluated in one transformer layer.
- Key switches count relinearizations, rotations, conjugations, and other operations requiring a key switch, measured per BERT-base transformer layer.The comparison covers NEXUS, THOR, and Powerformer.Source: E COMPLEXITY COMPARISON · p. 20
- MOAI achieves the fewest key switches across all matrix multiplications evaluated in one transformer layer.The comparison is for BERT-base inference.Source: Table 8 · p. 20, E COMPLEXITY COMPARISON · p. 20
- NEXUS is excluded from the comparison for softmax(QK⊺)V because its evaluation procedure for that operation is unspecified.The comparison also notes that NEXUS omits how it evaluates the third line.Source: E COMPLEXITY COMPARISON · p. 20
F DETAILS OF NON-LINEAR EVALUATIONS
MOAI details its encrypted LayerNorm, GELU and Softmax evaluations, including range-aware approximations and optimized division. Its Softmax and LayerNorm implementations achieve large speedups over NEXUS and THOR, and the framework is also applied to LLaMA-3-8B.
F.1 LAYERNORM DETAILS
- LayerNorm is computed row by row, with different formulas selected after attention and feed-forward blocks to improve inverse-square-root precision.The authors state that NEXUS’s LayerNorm input exceeds the supported data range for BERT-base-uncased, preventing its algorithm from computing LayerNorm for that model.Source: §F.1 · p. 21
F.2 GELU APPROXIMATION
- A tanh approximation with a degree-23 polynomial on [−20, 10] is extended to wider input ranges using domain-extension polynomials.The paper describes composing the approximation polynomial with domain-extension functions to cover a larger interval.Source: §F.2 · pp. 21, 23
- NEXUS’s piecewise GELU method is vulnerable to nonzero approximate segment bits multiplied by large polynomial values, while smaller sign-function error can raise evaluation time.The paper reports an observed input range of [−20, 10], beyond NEXUS’s stated [−8, 8] range.Source: §F.2 · p. 23
F.3 REMARKS ON OTHER NON-LINEAR FUNCTIONS
- MOAI’s approximation approach uses fewer multiplicative levels than NEXUS and THOR, extends to wider input intervals with little depth consumption, and supports substitutable nonlinear functions.The framework can be adapted to other activation functions by changing the approximation method.Source: §F.3 · p. 23
G OPTIMIZED SOFTMAX EVALUATION ALGORITHM
- Goldschmidt division’s absolute error grows rapidly near zero, so the Softmax input range must stay away from zero and the algorithm needs at least 10 iterations.The optimized evaluation bootstraps the summed exponentials after the third algorithm line, which the authors say can reduce preceding matrix-multiplication modulus and time cost.Source: G OPTIMIZED SOFTMAX EVALUATION ALGORITHM · p. 24
H.1 MICRO-BENCHMARK EVALUATION OF SOFTMAX AND LAYERNORM
- SoftMax is at least 4× faster than NEXUS and 22× faster than THOR, while LayerNorm is at least 87× and 151× faster, respectively.The comparison reports amortized times for both functions; MOAI eliminates all rotations during their evaluation.Source: Table 9 · p. 25, §H.1 · p. 25
H.2 INFERENCE TIME ON LLAMA3-8B MODEL
- LLaMA-3-8B evaluation uses rotation-free causal Softmax and RMSNorm, polynomial SiLU, and consistent matrix packing without format conversions or transpositions.The evaluation uses 8-token inputs and incorporates NEXUS’s ArgMax algorithm; the authors report that FHE introduces negligible additional error on GLUE scores.Source: §H.2 · p. 25
H.3 ERROR ANALYSIS IN ONE TRANSFORMER LAYER
The first-layer error grows slowly during evaluation, with final-output medians between 0.01 and 0.03. The appendix also outlines CKKS setup, key generation, encryption, and decryption operations.
- Final-output error medians fall in [0.01, 0.03] after evaluating the first transformer layer with 44 input tokens.The authors say this slow error growth is consistent with their analysis over 12 layers.Source: §H.3 · p. 25, Figure 6 · p. 27
I SET UP OPERATIONS OF THE CKKS SCHEME
- CKKS setup outputs parameters including ciphertext modulus Q and polynomial degree N; key generation produces secret and public keys, and encryption and decryption map between vectors and ciphertexts.The scheme description also includes auxiliary evaluation keys in key generation.Source: I SET UP OPERATIONS OF THE CKKS SCHEME · pp. 26–27