### Install Python Dependencies Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/README.md Installs the necessary Python packages for the project from a requirements file. This is a standard step for setting up Python projects. ```bash pip install -r training/requirements.txt ``` -------------------------------- ### CMake Build with GPU Enabled Example Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/docs/en/build-guide.md An example of configuring the CMake build for Latti-AI with GPU acceleration enabled and specifying the CUDA architecture. This is a common configuration step for GPU builds. ```bash cmake -B build -DINFERENCE_SDK_ENABLE_GPU=ON -DLATTISENSE_CUDA_ARCH=89 ``` -------------------------------- ### Running Pre-Built Encrypted Inference Examples with Bash Source: https://context7.com/cipherflow-fhe/latti-ai/llms.txt Instructions for executing pre-compiled example models for encrypted inference. This involves first generating low-level instructions using Python scripts and then running the inference executable, with options for CPU or GPU acceleration. ```bash # Generate low-level instructions python inference/interface/gen_mega_ag.py --task-dir examples/test_mnist/task python inference/interface/gen_mega_ag.py --task-dir examples/test_cifar10/task python inference/interface/gen_mega_ag.py --task-dir examples/test_imagenet/task # Run encrypted inference (CPU) ./build/examples/inference \ --task-dir examples/test_cifar10/task \ --input examples/test_cifar10/task/client/img.csv # Run encrypted inference (GPU accelerated) ./build/examples/inference \ --task-dir examples/test_cifar10/task \ --input examples/test_cifar10/task/client/img.csv \ --gpu # Example output: # ========== Encrypted Inference ========== # Task directory: examples/test_cifar10/task # Input file: examples/test_cifar10/task/client/img.csv # [Client] Generating CKKS context and keys... # [Client] Bootstrapping: Yes # [Client] Done. # [Server] Loading model... ``` -------------------------------- ### Run Encrypted Inference Examples (Bash) Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/README.md Executes pre-built encrypted inference examples for various datasets (MNIST, CIFAR-10, ImageNet). These commands require a built project and pre-prepared task directories, and can optionally utilize GPU acceleration. ```bash # MNIST python inference/interface/gen_mega_ag.py --task-dir examples/test_mnist/task ./build/examples/inference --task-dir examples/test_mnist/task --input examples/test_mnist/task/client/img.csv ./build/examples/inference --task-dir examples/test_mnist/task --input examples/test_mnist/task/client/img.csv --gpu # CIFAR-10 python inference/interface/gen_mega_ag.py --task-dir examples/test_cifar10/task ./build/examples/inference --task-dir examples/test_cifar10/task --input examples/test_cifar10/task/client/img.csv ./build/examples/inference --task-dir examples/test_cifar10/task --input examples/test_cifar10/task/client/img.csv --gpu # ImageNet python inference/interface/gen_mega_ag.py --task-dir examples/test_imagenet/task ./build/examples/inference --task-dir examples/test_imagenet/task --input examples/test_imagenet/task/client/img.csv ./build/examples/inference --task-dir examples/test_imagenet/task --input examples/test_imagenet/task/client/img.csv --gpu ``` -------------------------------- ### Clone and Build Latti-AI (CPU-Only) Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/docs/en/build-guide.md Clones the Latti-AI repository, initializes submodules, and builds the project for CPU-only execution. This process is suitable when GPU acceleration is not required. ```bash git clone https://github.com/cipherflow-fhe/latti-ai.git cd latti-ai git submodule update --init git -C inference/lattisense submodule update --init fhe_ops_lib/lattigo cmake -B build cmake --build build -j$(nproc) ``` -------------------------------- ### Build Latti-AI with GPU Acceleration Enabled Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/docs/en/build-guide.md Builds the Latti-AI project with GPU acceleration enabled. This command requires the HEonGPU library to be built and installed previously, and the CUDA architecture to be specified. ```bash cd ../../.. # Return to project root cmake -B build -DINFERENCE_SDK_ENABLE_GPU=ON -DLATTISENSE_CUDA_ARCH= cmake --build build -j$(nproc) ``` -------------------------------- ### Build and Install HEonGPU for GPU Acceleration Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/docs/en/build-guide.md Builds and installs the HEonGPU library, which provides GPU acceleration for FHE operations. This step requires specifying CUDA architecture and compiler paths. Ensure CUDA Toolkit is installed. ```bash cd inference/lattisense/HEonGPU cmake -B build \ -DCMAKE_CUDA_ARCHITECTURES= \ -DCMAKE_CUDA_COMPILER=/bin/nvcc \ -DCMAKE_INSTALL_PREFIX=/install cmake --build build --parallel $(nproc) --target install ``` -------------------------------- ### Clone and Build Latti-AI Project Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/CONTRIBUTING.md Steps to clone the Latti-AI repository recursively and build the project using CMake and Make. This setup is essential for development and testing. ```bash git clone --recursive https://github.com/cipherflow-fhe/latti-ai.git cd latti-ai mkdir build && cd build cmake .. make -j$(nproc) ``` -------------------------------- ### Install and Configure Git Hooks with pre-commit Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/CONTRIBUTING.md Installs and configures pre-commit hooks for the Latti-AI project. These hooks automate code formatting, linting, commit message validation, and branch naming checks before commits and pushes. ```bash pip install pre-commit pre-commit install pre-commit install --hook-type commit-msg pre-commit install --hook-type pre-push ``` -------------------------------- ### Example Branch Names for Latti-AI Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/CONTRIBUTING.md Demonstrates the required format for Git branch names in the Latti-AI project. Branches should follow a `/` pattern, where `` is a predefined category and `` is a concise, hyphen-separated name. ```git feat/add-relu-layer fix/cifar10-input-size refactor/move-test-folder docs/update-contributing ``` -------------------------------- ### Add Subdirectories for Build (CMake) Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/CMakeLists.txt Includes the 'inference' and 'examples' subdirectories into the build process. This allows CMake to process CMakeLists.txt files within these directories. ```cmake add_subdirectory(inference) add_subdirectory(examples) ``` -------------------------------- ### Clone Latti-AI Repository (GPU Build) Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/docs/en/build-guide.md Clones the Latti-AI repository with all its recursive submodules, which is the first step for setting up a GPU-accelerated build. This command may take a significant amount of time due to the size of the submodules. ```bash git clone --recursive https://github.com/cipherflow-fhe/latti-ai.git # This may take ~6 minutes cd latti-ai ``` -------------------------------- ### End-to-End Encrypted Inference Workflow in C++ Source: https://context7.com/cipherflow-fhe/latti-ai/llms.txt Provides a comprehensive example of the client-server encrypted inference workflow. It covers client-side key generation, input encryption, server-side evaluation, and client-side decryption. The example also includes optional plaintext verification for debugging purposes. ```cpp #include "interface/inference_client.h" #include "interface/inference_server.h" #include int main() { std::string task_dir = "./runs/cifar10/task"; std::string input_csv = "./task/client/img.csv"; bool use_gpu = true; // === Client Side === // Generate keys and encrypt input InferenceClient client(task_dir + "/client"); client.setup(); auto eval_ctx = client.export_eval_context(); auto encrypted_input = client.encrypt(input_csv); // In production: send eval_ctx and encrypted_input to server over network // === Server Side === // Import public keys, load model, run inference InferenceServer server(task_dir + "/server", use_gpu); server.import_eval_context(eval_ctx); server.load_model(); auto encrypted_output = server.evaluate(encrypted_input); // In production: send encrypted_output back to client over network // === Client Side === // Decrypt result auto result = client.decrypt(encrypted_output); // Print classification results std::cout << "Encrypted inference results:" << std::endl; for (int i = 0; i < result.num_outputs; i++) { std::cout << " Class " << i << ": " << result.output[i] << std::endl; } // === Verification (optional) === auto plaintext_output = server.evaluate_plaintext(input_csv); std::cout << "\nPlaintext verification:" << std::endl; for (size_t i = 0; i < plaintext_output.size(); i++) { std::cout << " Class " << i << ": " << plaintext_output[i] << std::endl; } return 0; } ``` -------------------------------- ### Example Commit Messages for Latti-AI Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/CONTRIBUTING.md Illustrates the Conventional Commits format required for commit messages in Latti-AI. This format helps in automatically generating changelogs and understanding the nature of changes. ```git feat: add batch normalization layer fix(cifar10): correct input_size in train.py docs: update build instructions ``` -------------------------------- ### Initialize Encrypted Inference Process (C++) Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/docs/en/APIs_Reference.md The InitInferenceProcess class is responsible for loading compiled computation graphs and model parameters. It initializes FHE layer objects with their corresponding weights, preparing the system for encrypted inference. Key parameters include the project path and whether to use bootstrapping. ```cpp class InitInferenceProcess { InitInferenceProcess(const string& project_path_in, bool is_fpga = true); virtual void init_parameters(bool is_bootstrapping = false); virtual void load_model_prepare(); }; // Member descriptions: // `project_path`: Root path of the compiled task // `task_type`: Task type identifier // `pack_style`: Packing style // `ckks_parameters`: CKKS parameter sets // `is_lazy`: Enable lazy weight preparation ``` -------------------------------- ### MultConv2DPackedDepthwiseLayer Initialization (Python) Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/docs/en/APIs_Reference.md Initializes a packed depthwise convolutional layer for FHE. It requires input and output channel counts, spatial dimensions, kernel size, stride, and packing configurations. Raises ValueError if spatial dimensions or stride are not powers of 2. ```python MultConv2DPackedDepthwiseLayer(n_out_channel, n_in_channel, input_shape, kernel_shape, stride, skip, n_channel_per_ct, n_packed_in_channel, n_packed_out_channel, upsample_factor=[1, 1]) ``` -------------------------------- ### Server-Side Inference with InferenceServer in C++ Source: https://context7.com/cipherflow-fhe/latti-ai/llms.txt Demonstrates how to initialize and use the InferenceServer for model loading and encrypted inference on the server side. It requires a model directory and a boolean flag for GPU usage, and it imports evaluation context and runs encrypted inference, returning encrypted output. Optional plaintext verification is available for debugging. ```cpp #include "interface/inference_server.h" // Initialize server with model directory and GPU flag InferenceServer server("./task/server", true); // true = use GPU // Import evaluation context from client (public keys only) server.import_eval_context(eval_ctx); // [Server] Importing evaluation context... // [Server] Bootstrapping: Yes // [Server] Done. // Load model weights and computation graph server.load_model(); // [Server] Loading model... // [Server] Done. // Run encrypted inference Bytes encrypted_output = server.evaluate(encrypted_input); // [Server] Running encrypted inference... // [Server] Device: GPU // Encrypted inference time: 82.4 s // [Server] Done. // ... return encrypted_output to client ... // Optional: plaintext verification (for debugging only) std::vector plaintext_result = server.evaluate_plaintext("./task/client/img.csv"); ``` -------------------------------- ### Fix Missing cstdint Header in HEonGPU Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/docs/en/build-guide.md A code modification to resolve a missing `cstdint` header error during the HEonGPU build. This involves adding the necessary include directive at the beginning of a specific C++ header file. ```cpp #include ``` -------------------------------- ### Fix CCCL Package Hang in CMake Build Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/docs/en/build-guide.md Troubleshooting step to resolve build hangs related to the CCCL package. It involves modifying a specific line in the `versions.json` file to point to an alternative Git repository. ```bash 1. Edit `build/_deps/rapids-cmake-src/rapids-cmake/cpm/versions.json` 2. Change line 16 `git_url` to: `"git_url": "https://gitee.com/Vizl/cccl.git"` 3. Re-run the cmake command ``` -------------------------------- ### Initialize Compiler Configuration Programmatically Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/docs/en/APIs_Reference.md Initializes the compiler configuration using a Python function, allowing overrides of default settings from `config.json`. Parameters like polynomial modulus degree, computation style, and graph type can be specified. ```python init_config_with_args(poly_n=None, style=None, graph_type=None) ``` -------------------------------- ### UpsampleNearestLayer Initialization (Python) Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/docs/en/APIs_Reference.md Initializes a nearest-neighbor upsampling layer. Requires spatial dimensions, skip values, upsampling factor, channels per ciphertext, and CKKS multiplicative level. Input shape and skip values must be powers of 2, raising ValueError otherwise. ```python UpsampleNearestLayer(shape, skip, upsample_factor, n_channel_per_ct, level) ``` -------------------------------- ### PolyReluLayer Initialization (Python) Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/docs/en/APIs_Reference.md Initializes a polynomial ReLU activation layer. Requires input spatial dimensions, polynomial order, skip values, and channels per ciphertext. Input shape, skip, and derived block shape must be powers of 2. Raises ValueError otherwise. ```python PolyReluLayer(input_shape, order, skip, n_channel_per_ct, upsample_factor=[1, 1], block_expansion=[1, 1]) ``` -------------------------------- ### CIFAR-10 Training Pipeline with Python Source: https://context7.com/cipherflow-fhe/latti-ai/llms.txt Details the two-step training process for CIFAR-10 models. The first step trains a baseline model with standard ReLU activations. The second step replaces activations, fine-tunes for FHE using polynomial approximations, and exports the model in ONNX and H5 formats. ```bash # Step 1: Train baseline model with standard ReLU activations python examples/test_cifar10/train.py \ --epochs 150 \ --batch-size 128 \ --lr 0.1 \ --output-dir ./runs/cifar10/model \ --input-shape 3 32 32 # Output: Best accuracy: 91.52% -> ./runs/cifar10/model/train_baseline.pth # Step 2: Replace activations and fine-tune for FHE python examples/test_cifar10/train.py \ --poly_model_convert \ --pretrained ./runs/cifar10/model/train_baseline.pth \ --epochs 10 \ --batch-size 36 \ --lr 0.001 \ --input-dir ./runs/cifar10/model \ --export-dir ./runs/cifar10/task/server \ --input-shape 3 32 32 \ --degree 4 \ --upper-bound 3.0 \ --poly-module RangeNormPoly2d # Output: # Device: cuda:0 | MaxPool2d 0 -> AvgPool2d 0 # Device: cuda:0 | ReLU 19 -> Poly 19 (ub=3.0, deg=4) # [1/10] lr=0.0010 train 0.2341/89.12% test 0.1876/90.85% # ... # Best accuracy: 90.45% -> ./runs/cifar10/model/train_poly.pth # ONNX saved: ./runs/cifar10/model/trained_poly.onnx # Fused H5 saved: ./runs/cifar10/task/server/model_parameters.h5 ``` -------------------------------- ### Configure Inference Executable and Link Libraries (CMake) Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/examples/CMakeLists.txt Defines the main 'inference' executable by compiling 'inference.cpp' and links it with several internal and external libraries. It includes setting source path definitions and managing library groups for proper linking. ```cmake include_directories(${CMAKE_SOURCE_DIR}/inference) include_directories(${CMAKE_SOURCE_DIR}/inference/lib) add_definitions(-DSOURCE_PATH="${CMAKE_SOURCE_DIR}/inference") add_executable(inference inference.cpp) target_link_libraries(inference PUBLIC -Wl,--start-group inference_interface inference_task fhe_layers log -Wl,--end-group lattisense ${HDF5_CXX_LIBRARIES} ) ``` -------------------------------- ### Avgpool_layer Initialization (Python) Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/docs/en/APIs_Reference.md Initializes an average pooling layer. It requires stride and shape parameters, which must be powers of 2. Optional parameters include channel count and skip values. Raises ValueError if shape, stride, or skip are not powers of 2. ```python Avgpool_layer(stride, shape, channel=1, skip=[1, 1]) ``` -------------------------------- ### Compile Model using CLI Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/docs/en/APIs_Reference.md Command-line interface for compiling models. Accepts ONNX or JSON input files and allows configuration of polynomial modulus degree, computation style, graph type, and parallel processing parameters. ```bash python run_compile.py -i [-o ] [options] # Example: Compile from JSON python run_compile.py -i pt.json -o ./output --poly_n 65536 --style ordinary ``` -------------------------------- ### InitInferenceProcess Class Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/docs/en/APIs_Reference.md Handles the initialization of the inference process by loading the compiled graph and model parameters. It sets up all FHE layer objects with their corresponding weights. ```APIDOC ## InitInferenceProcess Class ### Description Loads the compiled graph and model parameters from disk. Initializes all FHE layer objects with their weights. ### Class Definition ```cpp class InitInferenceProcess { InitInferenceProcess(const string& project_path_in, bool is_fpga = true); virtual void init_parameters(bool is_bootstrapping = false); virtual void load_model_prepare(); }; ``` ### Members | Member | Type | Description | |--------|------|-------------| | `project_path` | `filesystem::path` | Root path of the compiled task | | `task_type` | `string` | Task type identifier | | `pack_style` | `string` | Packing style | | `ckks_parameters` | `map>` | CKKS parameter sets | | `is_lazy` | `bool` | Enable lazy weight preparation | ### Held Layer Instances The class holds maps of all initialized FHE layer instances: - `ckks_conv2ds` — `Conv2DPackedLayer` instances - `ckks_dw_conv2ds` — `Conv2DPackedDepthwiseLayer` instances - `ckks_denses` — `DensePackedLayer` instances - `ckks_poly_relu` — `PolyRelu` instances - `ckks_adds` — `AddLayer` instances - `ckks_avgpool` — `Avgpool2DLayer` instances - `ckks_concat` — `ConcatLayer` instances - `ckks_upsample` — `UpsampleLayer` instances - `ckks_multiplexed_conv2ds` — `ParMultiplexedConv2DPackedLayer` instances ``` -------------------------------- ### Avgpool2DLayer Constructor (C++) Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/docs/en/APIs_Reference.md Initializes an Avgpool2DLayer for encrypted average pooling. This layer computes the average of elements within a sliding window across the spatial dimensions of the input. It requires the pooling window shape and the stride for the pooling operation. ```cpp Avgpool2DLayer(const Duo& shape, const Duo& stride); ``` -------------------------------- ### CMake Project Configuration Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/inference/CMakeLists.txt Configures include directories, preprocessor definitions, and library settings for the CMake build system. It sets up the project to include source directories and defines specific preprocessor macros. ```cmake include_directories(${CMAKE_CURRENT_SOURCE_DIR}) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/lib) add_definitions(-DOPENSSL_API_COMPAT=10100) add_definitions(-DSOURCE_PATH="${CMAKE_CURRENT_SOURCE_DIR}") set(GLOBAL_NUM 1) add_library(log STATIC ${CMAKE_CURRENT_SOURCE_DIR}/lib/log/log.c) add_subdirectory(lattisense) add_subdirectory(fhe_layers) add_subdirectory(inference_task) add_subdirectory(interface) add_subdirectory(unittests) ``` -------------------------------- ### Build LattiAI (GPU Acceleration) Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/README.md This multi-step process builds LattiAI with GPU acceleration. It involves cloning the repository, building the HEonGPU library with specified CUDA architectures and compiler paths, and then building the main LattiAI project with GPU support enabled. ```bash git clone --recursive https://github.com/cipherflow-fhe/latti-ai.git cd latti-ai cd inference/lattisense/HEonGPU cmake -B build \ -DCMAKE_CUDA_ARCHITECTURES= \ -DCMAKE_CUDA_COMPILER=/bin/nvcc \ -DCMAKE_INSTALL_PREFIX=/install cmake --build build --parallel $(nproc) --target install cd ../../.. cmake -B build -DINFERENCE_SDK_ENABLE_GPU=ON -DLATTISENSE_CUDA_ARCH= cmake --build build -j$(nproc) ``` -------------------------------- ### ParMultiplexedConv2DPackedLayer Constructor (C++) Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/docs/en/APIs_Reference.md Initializes a ParMultiplexedConv2DPackedLayer for multiplexed 2D convolution with parallel channel packing. It supports upsampling by interleaving zero-valued slots, which is useful for changing resolution within the convolution operation. Key parameters include input shape, weights, bias, stride, skip factor, channels per ciphertext, and CKKS multiplicative level. ```cpp ParMultiplexedConv2DPackedLayer(const CkksParameter& param, const Duo& input_shape, const Array& weight, const Array& bias, const Duo& stride, const Duo& skip, uint32_t n_channel_per_ct, uint32_t level, double residual_scale = 1.0, const Duo& upsample_factor = {1, 1}); ``` -------------------------------- ### DensePackedLayer Initialization (Python) Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/docs/en/APIs_Reference.md Initializes a packed dense (fully connected) layer. Key parameters include channel counts, spatial dimensions, packing factor, and packed feature counts. Input shape and skip values must be powers of 2, otherwise a ValueError is raised. ```python DensePackedLayer(n_out_channel, n_in_channel, input_shape, skip, pack, n_packed_in_feature, n_packed_out_feature) ``` -------------------------------- ### Configure CMake Build Options (CMake) Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/CMakeLists.txt Sets up essential CMake options and variables for the latti-ai project. This includes enabling GPU acceleration, setting the build mode to SHARED, and ensuring compile commands are exported. ```cmake cmake_minimum_required(VERSION 3.13) option(INFERENCE_SDK_ENABLE_GPU "Enable GPU acceleration support" OFF) message(STATUS "INFERENCE_SDK_ENABLE_GPU: ${INFERENCE_SDK_ENABLE_GPU}") # Configure lattisense options before add_subdirectory set(LATTISENSE_ENABLE_GPU ${INFERENCE_SDK_ENABLE_GPU} CACHE BOOL "Enable GPU in lattisense" FORCE) set(LATTISENSE_BUILD_MODE "SHARED" CACHE STRING "Lattisense build mode" FORCE) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) ``` -------------------------------- ### Find and Configure Libraries (CMake) Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/CMakeLists.txt Locates and configures required libraries: Threads, HDF5 (with C++ and HL components), and OpenMP. If OpenMP is found, its flags are added to the C and C++ compiler and linker flags. ```cmake find_package(Threads REQUIRED) find_package(HDF5 COMPONENTS CXX HL) find_package(OpenMP REQUIRED) if(OPENMP_FOUND) message("OPENMP FOUND") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}") endif() include_directories(${HDF5_INCLUDE_DIRS}) ``` -------------------------------- ### Lint and Format Python Code with Ruff Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/CONTRIBUTING.md Uses Ruff to check for linting errors and format Python code in the Latti-AI project according to the defined style guidelines. ```bash ruff check . ruff format . ``` -------------------------------- ### Generate Low-Level Inference Instructions Source: https://context7.com/cipherflow-fhe/latti-ai/llms.txt Generates GPU-accelerated inference instructions from a compiled task directory. This script is essential for preparing the model for runtime execution on the target hardware. It takes the path to the task directory as input. ```bash # Generate low-level instructions for runtime execution python inference/interface/gen_mega_ag.py --task-dir ./runs/cifar10/task ``` -------------------------------- ### Encrypted Inference Runtime Execution (C++) Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/README.md Demonstrates the client-server interaction for encrypted inference using the `InferenceClient` and `InferenceServer` interfaces. It covers key generation, encryption, server-side evaluation, and decryption. ```cpp #include "interface/inference_client.h" #include "interface/inference_server.h" // Client: generate keys and encrypt input InferenceClient client("./task/client"); client.setup(); auto eval_ctx = client.export_eval_context(); // serialize public keys (Bytes) auto encrypted_input = client.encrypt("./task/client/img.csv"); // encrypt and serialize ciphertext (Bytes) // Server: import public keys, load model, run inference on serialized ciphertext InferenceServer server("./task/server", use_gpu); server.import_eval_context(eval_ctx); // deserialize public keys server.load_model(); auto encrypted_output = server.evaluate(encrypted_input); // deserialize input, inference, serialize output // Client: decrypt result auto result = client.decrypt(encrypted_output); // decrypt and deserialize output ciphertext, with secret key ``` -------------------------------- ### Initialize Encrypted Conv2D Layer (C++) Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/docs/en/APIs_Reference.md Constructs an encrypted 2D convolution layer with channel packing. This layer optimizes ciphertext usage by packing multiple channels into a single ciphertext, reducing the overall number of ciphertexts processed. It requires CKKS parameters, input shape, weights, bias, stride, skip factor, channels per ciphertext, and multiplicative level. ```cpp Conv2DPackedLayer(const CkksParameter& param, const Duo& input_shape, const Array& weight, const Array& bias, const Duo& stride, const Duo& skip, uint32_t n_channel_per_ct, uint32_t level, double residual_scale = 1.0); ``` -------------------------------- ### Model Adaptation - replace_activation_with_poly() Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/docs/en/APIs_Reference.md Replace all instances of a specified activation class with a new module factory (e.g., RangeNormPoly2d) in-place within a PyTorch model. ```APIDOC ## replace_activation_with_poly() ### Description Replace all instances of `old_cls` activation with `RangeNormPoly2d` in-place. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python from nn_tools import replace_activation_with_poly import torch.nn as nn # Assuming resnet20() is a defined function that returns a PyTorch model # model = resnet20() # replace_activation_with_poly(model, old_cls=nn.ReLU) # or replace SiLU activations # replace_activation_with_poly(model, old_cls=nn.SiLU, degree=4) ``` ### Response #### Success Response (200) - **model** (`nn.Module`) - The same model with activations replaced. #### Response Example None ### Parameters - **model** (`nn.Module`) - Required - PyTorch model (modified in-place) - **old_cls** (`Type[nn.Module]`) - Optional - Activation class to replace. Supported: `nn.ReLU`, `nn.SiLU` - **new_module_factory** (`Callable`) - Optional - Constructor `(upper_bound, degree, activation) -> nn.Module` - **upper_bound** (`float`) - Optional - Normalization upper bound - **degree** (`int`) - Optional - Polynomial degree (2 or 4) ### Returns The same model with activations replaced. ### Raises `ValueError` if `old_cls` is not `nn.ReLU` or `nn.SiLU`. ``` -------------------------------- ### Configure HDF5 and Build Inference Library (CMake) Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/inference/inference_task/CMakeLists.txt This snippet configures the build system to find the HDF5 library and then defines a library target named 'inference_task' using the 'inference_process.cpp' source file. It specifies the public linkage for the library and lists its dependencies. ```cmake find_package(HDF5 REQUIRED) add_library(inference_task inference_process.cpp) target_link_libraries(inference_task PUBLIC lattisense fhe_layers log ${HDF5_CXX_LIBRARIES}) ``` -------------------------------- ### Generate Packed Conv2D Instructions (Python) Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/docs/en/APIs_Reference.md Generates instructions for packed 2D convolution operations. Requires input and kernel shapes, stride, and packing parameters. Raises ValueError if spatial dimensions or stride are not powers of 2. ```python class Conv2DPackedLayer: def __init__(self, n_out_channel, n_in_channel, input_shape, kernel_shape, stride, skip, pack, n_packed_in_channel, n_packed_out_channel): # ... implementation ... pass def call(self, x, weight_pt, bias_pt) -> list[CkksCiphertextNode]: # Generate convolution instructions pass def call_custom_compute(self, x, conv_data_source) -> list[CkksCiphertextNode]: # Generate with on-demand weight encoding pass ``` -------------------------------- ### Compile Model to Encrypted Graph (ONNX/JSON) Source: https://context7.com/cipherflow-fhe/latti-ai/llms.txt Compiles ONNX models or JSON computation graphs into encrypted inference DAGs. This tool automatically handles CKKS parameter selection and bootstrapping placement. It can accept either an ONNX file or a pre-converted JSON graph as input, outputting various configuration and encrypted model files. ```bash # Compile from ONNX (auto-converts to JSON first) python training/run_compile.py \ --input ./output/model.onnx \ --output ./runs/cifar10/ \ --poly_n 65536 \ --style multiplexed # Compile from pre-converted JSON python training/run_compile.py \ --input ./runs/cifar10/model/pt.json \ --output ./runs/cifar10/ \ --poly_n 65536 \ --style ordinary \ --num_experiments 256 \ --num_workers 32 ``` -------------------------------- ### Model Compiler CLI Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/docs/en/APIs_Reference.md Command-line interface for compiling encrypted graphs from ONNX or JSON model files. ```APIDOC ## Model Compiler CLI ### Description Compiles a model using the graph splitter tool. Supports both ONNX model files (`.onnx`) and pre-converted JSON files (`.json`). ### Method `run_compile.py` (command-line script) ### Endpoint `python run_compile.py -i [-o ] [options]` ### Parameters #### Arguments - **-i/--input** (str) - Required - Input `.onnx` or `.json` file - **-o/--output** (str) - Optional - Output directory (defaults to input directory) - **--poly_n** (int) - Optional - Polynomial modulus degree: `8192`, `16384`, or `65536` (defaults to config) - **--style** (str) - Optional - Computation style: `ordinary` or `multiplexed` (defaults to config) - **--graph_type** (str) - Optional - Graph type: `btp` (defaults to config) - **--num_experiments** (int) - Optional - Number of parallel compilation experiments (defaults to `128`) - **--num_workers** (int) - Optional - Number of worker processes (defaults to `16`) - **--temperature** (float) - Optional - Randomization temperature (defaults to `1.0`) ### Request Example ```bash # Compile from JSON python run_compile.py -i pt.json -o ./output --poly_n 65536 --style ordinary # Compile from ONNX python run_compile.py -i model.onnx -o ./compiled_models --poly_n 16384 --style multiplexed --num_workers 32 ``` ### Response #### Success Response (200) - **output directory** - The compilation process generates output files within the specified or default output directory. #### Response Example ```json { "status": "success", "message": "Model compilation completed.", "output_path": "./compiled_models" } ``` ``` -------------------------------- ### Square_layer Initialization (Python) Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/docs/en/APIs_Reference.md Initializes a layer for element-wise squaring of ciphertexts. It takes a single parameter, 'level', which specifies the CKKS multiplicative level for the operation. ```python Square_layer(level) ``` -------------------------------- ### Format C/C++ Code with clang-format Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/CONTRIBUTING.md Applies clang-format to C/C++ source files in the Latti-AI project to enforce consistent code style. This command modifies files in-place. ```bash clang-format -i ``` -------------------------------- ### Add Inference Interface Library (CMake) Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/inference/interface/CMakeLists.txt Adds the inference interface library to the CMake build system, specifying the source files for the client and server components. This is a foundational step for building the inference functionality. ```cmake add_library(inference_interface inference_client.cpp inference_server.cpp) ``` -------------------------------- ### Generate Test Data and Run C++ Unit Tests Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/CONTRIBUTING.md Executes Python scripts to generate necessary test data and then runs the C++ unit tests for the Latti-AI project using the Catch2 framework. ```bash cd latti-ai/build/unittests python test_gen_layers.py # Generate test data ./test_fhe_layers_hetero # Run tests ``` -------------------------------- ### Generate Low-Level FHE Instructions (Bash) Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/README.md Generates hardware-specific low-level instructions from a pre-compiled FHE task directory. This step is crucial for preparing the model for runtime execution. ```bash python inference/interface/gen_mega_ag.py --task-dir ./runs/cifar10/task ``` -------------------------------- ### MultScalarLayer Initialization (Python) Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/docs/en/APIs_Reference.md Initializes a layer for scalar multiplication. This layer has no constructor parameters and is used for performing element-wise multiplication of a ciphertext with a scalar. ```python MultScalarLayer() ``` -------------------------------- ### Client-Side Encryption Interface for Inference Source: https://context7.com/cipherflow-fhe/latti-ai/llms.txt Handles key generation, input encryption, and output decryption on the client side, ensuring the secret key remains with the client. This C++ interface initializes the client with task configuration, sets up cryptographic keys and context, exports public evaluation context, encrypts input data, and decrypts the server's output. ```cpp #include "interface/inference_client.h" // Initialize client with task configuration InferenceClient client("./task/client"); // Generate CKKS keys and crypto context client.setup(); // [Client] Generating CKKS context and keys... // [Client] Bootstrapping: Yes // [Client] Poly degree: N=65536 // [Client] Done. // Export public evaluation context for server (no secret key) Bytes eval_ctx = client.export_eval_context(); // [Client] Exporting evaluation context... // [Client] Serializing BTP context... // [Client] Done. // Encrypt input image from CSV file Bytes encrypted_input = client.encrypt("./task/client/img.csv"); // [Client] Encrypting input... // [Client] Done. // ... send eval_ctx and encrypted_input to server ... // ... receive encrypted_output from server ... // Decrypt inference result DecryptedOutput result = client.decrypt(encrypted_output); // [Client] Decrypting output... // [Client] Done. // Access results for (int i = 0; i < result.num_outputs; i++) { std::cout << "Class " << i << ": " << result.output[i] << std::endl; } ``` -------------------------------- ### Execute Encrypted Inference (C++) Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/docs/en/APIs_Reference.md The InferenceProcess class executes encrypted inference using layers initialized by InitInferenceProcess. It provides methods to run the inference task, set and retrieve encrypted features, and supports plaintext execution for debugging or comparison. The class manages intermediate results and CKKS contexts. ```cpp class InferenceProcess { InferenceProcess(InitInferenceProcess* fp_in, bool is_fpga_in); void run_task(bool is_mpc = false); void run_task_sdk(bool is_mpc = false); void run_task_plaintext(bool is_mpc = false); void set_feature(const string& feature_id, unique_ptr feature); const FeatureEncrypted& get_feature(const string& feature_id); }; // Member descriptions: // `fp`: Pointer to initialization process // `compute_device`: Execution device (default: CPU) // `intermediate_result`: Feature storage during inference // `ckks_contexts`: CKKS contexts ``` -------------------------------- ### Compile Model with Custom Parallelism Settings Source: https://github.com/cipherflow-fhe/latti-ai/blob/main/docs/en/APIs_Reference.md Compiles a JSON model with specified parallelism settings, allowing control over the number of experiments and worker processes. This is useful for optimizing compilation performance. ```python python run_compile.py -i pt.json --num_experiments 256 --num_workers 32 ```