Skip to content

Commit 88ac3cc

Browse files
add openfhe tracer (w/ end-to-end example)
Note: `run_tracing_example` target works up to expected functional modeler issue `key not found: partQHatInvModq_0_0` which is due to the fact that partQHatInvModq is no longer present in recent OpenFHE versions.
1 parent 464dbc0 commit 88ac3cc

File tree

8 files changed

+1246
-1
lines changed

8 files changed

+1246
-1
lines changed

.typos.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
# variation of params
99
parms = "parms"
1010
bload = "bload"
11+
ser = "ser"
12+
SerType = "SerType"
1113

1214
[files]
1315
extend-exclude = [

p-isa_tools/CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ else()
2222
set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of Build" FORCE)
2323
endif()
2424

25-
option(ENABLE_DATA_FORMATS "Enable support for the data formats library" OUTPUT_NAME_<CONFIG>)
25+
option(ENABLE_DATA_FORMATS "Enable support for the data formats library" ON)
2626
message(ENABLE_DATA_FORMATS="${ENABLE_DATA_FORMATS}")
2727

2828
option(ENABLE_FUNCTIONAL_MODELER "Enable building of functional modeler" ON)
2929
message(ENABLE_FUNCTIONAL_MODELER="${ENABLE_FUNCTIONAL_MODELER}")
3030

31+
option(ENABLE_KERNGEN "Enable kerngen (dependencies only)" ON)
32+
message(ENABLE_KERNGEN="${ENABLE_KERNGEN}")
33+
3134
option(ENABLE_PROGRAM_MAPPER "Enable building of program mapper" ON)
3235
message(ENABLE_PROGRAM_MAPPER="${ENABLE_PROGRAM_MAPPER}")
3336

@@ -63,6 +66,9 @@ file(GLOB_RECURSE IDE_HEADERS program_mapper/*.h functional_modeler/*.h dependen
6366

6467
# Build sub-directories
6568
add_subdirectory(common)
69+
if(ENABLE_KERNGEN)
70+
add_subdirectory(kerngen)
71+
endif()
6672
if(ENABLE_DATA_FORMATS)
6773
add_subdirectory(data_formats)
6874
endif()

p-isa_tools/data_formats/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,8 @@ add_custom_command(
5050
POST_BUILD
5151
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_LIST_DIR}/python ${PROJECT_BINARY_DIR}/python/
5252
)
53+
54+
option(ENABLE_OPENFHE_TRACER "Build the OpenFHE tracer" ON)
55+
if(ENABLE_OPENFHE_TRACER)
56+
add_subdirectory(tracers/openfhe)
57+
endif()
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Include FetchContent module
2+
include(FetchContent)
3+
4+
# Add OpenFHE via FetchContent
5+
FetchContent_Declare(
6+
OpenFHE
7+
GIT_REPOSITORY https://github.com/AlexanderViand/openfhe-development.git
8+
GIT_TAG 7e2eafc49b6b4c1a0000b1722a8749797ec277d9 # head of `tracing` branch (2025-08-07)
9+
)
10+
11+
# Set OpenFHE build options before making it available
12+
set(BUILD_UNITTESTS OFF CACHE BOOL "" FORCE)
13+
set(BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
14+
set(BUILD_BENCHMARKS OFF CACHE BOOL "" FORCE)
15+
set(ENABLE_TRACER ON CACHE BOOL "" FORCE)
16+
17+
message(STATUS "Fetching OpenFHE, this may take a while...")
18+
FetchContent_MakeAvailable(OpenFHE)
19+
message(STATUS "Finished configuring OpenFHE")
20+
21+
FetchContent_GetProperties(openfhe)
22+
23+
# Create executable from tracing_example.cpp
24+
add_executable(tracing_example tracing_example.cpp tracer.h)
25+
# Set C++ standard
26+
target_compile_features(tracing_example PRIVATE cxx_std_17)
27+
# Link with OpenFHE libraries
28+
target_link_libraries(tracing_example PRIVATE HERACLES_DATA_FORMATS::heracles_data_formats OPENFHEcore OPENFHEpke OPENFHEbinfhe)
29+
30+
target_include_directories(tracing_example PRIVATE
31+
# Third Party Includes
32+
$<BUILD_INTERFACE:${openfhe_SOURCE_DIR}/third-party/include>
33+
$<BUILD_INTERFACE:${openfhe_SOURCE_DIR}/third-party/cereal/include>
34+
$<BUILD_INTERFACE:${openfhe_SOURCE_DIR}/third-party/google-test/googletest>
35+
$<BUILD_INTERFACE:${openfhe_SOURCE_DIR}/third-party/google-test/googletest/include>
36+
# public headers that sit in the repo
37+
$<BUILD_INTERFACE:${openfhe_SOURCE_DIR}/src/core/include>
38+
$<BUILD_INTERFACE:${openfhe_SOURCE_DIR}/src/pke/include>
39+
$<BUILD_INTERFACE:${openfhe_SOURCE_DIR}/src/binfhe/include>
40+
# generated header (configure_file → config_core.h)
41+
$<BUILD_INTERFACE:${openfhe_BINARY_DIR}/src/core>)
42+
43+
44+
# Set compiler flags for optimization and debug info
45+
target_compile_options(tracing_example PRIVATE
46+
$<$<CONFIG:Release>:-O3>
47+
$<$<CONFIG:Debug>:-g -O0>
48+
)
49+
50+
51+
# define a custom target that runs tracing, then submits the trace to the program mapper, finally sending the pisa and mem file to the functional modeler
52+
add_custom_target(
53+
run_tracing_example
54+
# Run the actual example, which will generate the traces as end-to-end-test/tracing_example.bin and end-to-end-test/tracing_example_data.bin
55+
COMMAND tracing_example
56+
# Run the program mapper on the instruction trace, generating end-to-end-test/tracing_example.bin.csv
57+
COMMAND env VIRTUAL_ENV=${VENV_PATH} PATH=${VENV_PATH}/bin:$ENV{PATH} PYTHONPATH=${VENV_SITE_PACKAGES}:$ENV{PYTHONPATH} $<TARGET_FILE:program_mapper> ${CMAKE_BINARY_DIR}/end-to-end-test/tracing_example.bin ${CMAKE_SOURCE_DIR}/kerngen/kerngen.py
58+
COMMAND $<TARGET_FILE:functional_modeler> ${CMAKE_BINARY_DIR}/end-to-end-test/tracing_example_pisa.csv --verbose --hec_dataformats_mode --hec_dataformats_poly_program_location ${CMAKE_BINARY_DIR}/end-to-end-test/tracing_example.bin --hec_dataformats_data ${CMAKE_BINARY_DIR}/end-to-end-test/tracing_example_data.bin
59+
# TODO: Next step: assemble the *.pisa.csv and the *tw.mem using the assembler from https://github.com/IntelLabs/HERACLES-HGCF
60+
# TODO: Then it's a dead end :( as we don't have any tooling that can support non-toy sized workloads
61+
DEPENDS tracing_example program_mapper functional_modeler create-end-to-end-test-dir
62+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/end-to-end-test
63+
)
64+
65+
add_custom_target(create-end-to-end-test-dir
66+
COMMAND ${CMAKE_COMMAND} -E remove_directory ${CMAKE_BINARY_DIR}/end-to-end-test
67+
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/end-to-end-test
68+
)
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# OpenFHE Tracer
2+
3+
This directory contains the HERACLES tracer implementation for OpenFHE, which enables extraction of FHE computation traces for use with the p-ISA tools.
4+
5+
## Overview
6+
7+
The OpenFHE tracer captures homomorphic encryption operations performed by OpenFHE and generates trace files that can be processed by the p-ISA toolchain. This enables:
8+
9+
- Extraction of FHE instruction sequences
10+
- Generation of data traces for polynomial operations
11+
- End-to-end compilation from FHE programs to hardware accelerator instructions
12+
13+
## Files
14+
15+
- `tracer.h` - Main tracer implementation that hooks into OpenFHE's tracing infrastructure
16+
- `tracing_example.cpp` - Example program demonstrating tracer usage with basic FHE operations
17+
- `CMakeLists.txt` - Build configuration that fetches OpenFHE with tracing support enabled
18+
19+
## Building
20+
21+
From the repository root:
22+
23+
```bash
24+
# Configure the project
25+
mkdir -p build
26+
cmake -B build -S p-isa_tools
27+
28+
# Build the tracing example
29+
cmake --build build --target tracing_example
30+
```
31+
32+
## Running the Example
33+
34+
To run the complete end-to-end tracing pipeline:
35+
36+
```bash
37+
cmake --build build --target run_tracing_example
38+
```
39+
40+
This will:
41+
1. Execute the tracing example, generating trace files in `build/end-to-end-test/`:
42+
- `tracing_example.bin` - FHE instruction trace
43+
- `tracing_example_data.bin` - Data trace with polynomial values
44+
2. Run the program mapper on the instruction trace to generate `tracing_example.bin.csv`
45+
(this internally uses the kernel generator)
46+
3. Run the functional modeler to process the traces (see note below)
47+
48+
### Output Files
49+
50+
After running, you'll find the following in `build/end-to-end-test/`:
51+
- `tracing_example.bin` - Binary FHE instruction trace
52+
- `tracing_example_data.bin` - Binary data trace
53+
- `tracing_example.bin.csv` - Mapped instruction sequence
54+
- `tracing_example_pisa.csv` - p-ISA instructions (if functional modeler succeeds)
55+
56+
## Known Issues
57+
58+
> **Note:** The functional modeler may fail with an error about missing `partQHatInvModq`. This is currently expected as recent versions of OpenFHE no longer use this parameter internally, but the p-isa_tools still expect it.
59+
60+
## Usage in Your Own Code
61+
62+
To use the tracer in your OpenFHE application:
63+
64+
```cpp
65+
#include "tracer.h"
66+
67+
// After creating your CryptoContext
68+
auto cc = GenCryptoContext(parameters);
69+
70+
// Create and attach the tracer
71+
IF_TRACE(auto tracer = std::make_shared<HeraclesTracer<DCRTPoly>>("output_name", cc));
72+
IF_TRACE(cc->setTracer(tracer));
73+
74+
// Your FHE operations will now be traced
75+
auto result = cc->EvalAdd(cipher1, cipher2);
76+
77+
// Save trace files
78+
IF_TRACE(tracer->saveBinaryTrace());
79+
IF_TRACE(tracer->saveJsonTrace()); // for debugging/manual inspection
80+
```
81+
82+
## Dependencies
83+
84+
Note: these are automatically fetched/created by CMake when using `run_tracing_example`
85+
86+
- OpenFHE
87+
- HERACLES_DATA_FORMATS library (built as part of p-isa_tools)
88+
- Python environment with kerngen for program mapping

0 commit comments

Comments
 (0)