Skip to content

Commit 8d56340

Browse files
committed
Add vivado simulation support and SDK
1 parent c2b1e4d commit 8d56340

120 files changed

Lines changed: 133571 additions & 122 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

DAC_2023_CHAM.pdf

842 KB
Binary file not shown.

LEGAL.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Legal Disclaimer
2+
3+
Within this source code, the comments in Chinese shall be the original, governing version. Any comment in other languages are for reference only. In the event of any conflict between the Chinese language version comments and other language version comments, the Chinese language version shall prevail.
4+
5+
法律免责声明
6+
7+
关于代码注释部分,中文注释为官方版本,其它语言注释仅做参考。中文注释可能与其它语言注释存在不一致,当中文注释与其它语言注释存在不一致时,请以中文注释为准。

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2023 Alibaba Damo Academy
3+
Copyright (c) 2023 Alibaba Damo Academy and Ant Group
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ The __CHAM__ project is initiated by Alibaba DAMO Academy CTL and Ant Group's An
55

66
___CHAM___ is a customized homomorphic encryption accelerator for fast matrix-vector product. Homomorphic encryption (HE), enabling computation on encrypted data, suffers from poor performance due to enlarged data size and exploded amount of computation. Existing HE accelerators either target small HE operations or fail to support fast-evolving HE algorithms. To better support various privacy-preserving applications (e.g., logistic regression and neural-network inference), we propose CHAM for high-performance matrix-vector product, which can be easily extended to 2D/3D convolutions.
77

8-
The code in this repository is released by Alibaba DAMO Academy CTL and Ant Group's AntChain under MIT License.
8+
The code in this repository is released by Alibaba DAMO Academy CTL, Ant Group's AntChain and Ant Research under MIT License.
99

1010
The distribution is developed as part of our DAC2023 paper called *"CHAM: A Customized Homomorphic Encryption Accelerator for Fast Matrix-Vector Product"*. This distribution is an open-source version of our commercial accelerator and not ready for production use.
1111

@@ -25,30 +25,42 @@ The directory structure is as follows:
2525
* [__sim__](sim): simulation environment
2626
* [__tv__](tv): test vectors
2727
* [__xilinx_vcs_lib__](xilinx_vcs_lib): xilinx vcs simulation library
28-
28+
* [__chisel__](chisel): Chisel source files
29+
* [__mvp_sdk__](mvp_sdk): SDK for CHAM users
2930

3031
## Build guide
3132

33+
You need install Python3 and Vivado before simulation
34+
35+
If you use VCS, you may need to run as follows:
3236
1. Generate xilinx vcs lib in ./xilinx_vcs_lib
3337
2. Modify sim/vcs/tb_top.sh line 48 to your vcs path
3438
3. cd sim
3539
4. make clean //remove the previous run_dir
36-
5. ./run_case <test name>//test name is showing below in Test cases
37-
test_stg_0_0 test single stage at stage 0, and test only ksk ram init path
38-
test_stg_0_1 test single stage at stage 0, and test only preprocess init path
39-
test_stg_1 test single stage at stage 1
40-
test_stg_2 test single stage at stage 2
41-
test_stg_3 test single stage at stage 3
42-
test_stg_4 test single stage at stage 4
43-
test_stg_5 test single stage at stage 5
44-
test_stg_6 test single stage at stage 6
45-
test_stg_7 test single stage at stage 7
46-
test_stg_8 test single stage at stage 8
47-
test_stg_9 test single stage at stage 9
48-
test_stg_10 test single stage at stage 10
49-
test_row test all stages in a row
50-
test_full_no_uram_4x4096 test full case, the test vector is 4x4096. we use testbench to init ksk ram instead of designtest_full_4x4096 test full case, the test vector is 4x4096.
51-
test_full_4x4096 test full case, the test vector is 4x4096.
40+
5. ./run_case <test name> //test name is showing below in Test cases
41+
42+
test_stg_0_0: test single stage at stage 0, and test only ksk ram init path
43+
test_stg_0_1: test single stage at stage 0, and test only preprocess init path
44+
test_stg_1: test single stage at stage 1
45+
test_stg_2: test single stage at stage 2
46+
test_stg_3: test single stage at stage 3
47+
test_stg_4: test single stage at stage 4
48+
test_stg_5: test single stage at stage 5
49+
test_stg_6: test single stage at stage 6
50+
test_stg_7: test single stage at stage 7
51+
test_stg_8: test single stage at stage 8
52+
test_stg_9: test single stage at stage 9
53+
test_stg_10: test single stage at stage 10
54+
test_row: test all stages in a row
55+
test_full_no_uram_4x4096: test full case, the test vector is 4x4096. we use testbench to init ksk ram instead of design
56+
test_full_4x4096: test full case, the test vector is 4x4096.
57+
test_full_4x4096: test full case, the test vector is 4x4096.
58+
59+
If you use Vivado XSIM, you may need to run as follows:
60+
1. cd sim
61+
2. make partial_sim or make full_sim
62+
3. make clean
63+
5264

5365
## License
5466

mvp_sdk/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
deps/cpu_features-*/
2+
deps/GSL-*/
3+
deps/hexl-*/
4+
deps/SEAL-*/
5+
deps/zstd-*/
6+
build/
7+
.idea/
8+
.DS_Store

mvp_sdk/.gitmodules

Whitespace-only changes.

mvp_sdk/CMakeLists.txt

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
3+
project(mvp_pubilc)
4+
include(CMakeFindDependencyMacro)
5+
6+
set(CMAKE_CXX_STANDARD 17)
7+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
8+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
9+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
10+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -fPIC")
11+
12+
option(MVP_ENABLE_TESTS "enable gtest" ON)
13+
option(MVP_ENABLE_PYTHON_WRAPPER "enable python wrapper build" ON)
14+
15+
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
16+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
17+
endif()
18+
19+
## SEAL
20+
find_package(SEAL REQUIRED PACKAGE_FIND_VERSION_MAJOR 3)
21+
if (SEAL_STATIC_FOUND)
22+
set(SEAL_LIB SEAL::seal)
23+
elseif (SEAL_SHARED_FOUND)
24+
set(SEAL_LIB SEAL::seal_shared)
25+
endif()
26+
## MS-GSL
27+
find_package(Microsoft.GSL REQUIRED)
28+
## Threads
29+
find_package(Threads REQUIRED)
30+
## SPDLOG
31+
find_package(spdlog REQUIRED)
32+
## Pybind11
33+
if (MVP_ENABLE_PYTHON_WRAPPER)
34+
find_package(pybind11)
35+
endif()
36+
37+
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/include)
38+
39+
set(MVP_PUBLIC mvp_public)
40+
add_library(${MVP_PUBLIC})
41+
add_library(MVP::public ALIAS ${MVP_PUBLIC})
42+
add_subdirectory(include/mvp_public)
43+
target_link_libraries(${MVP_PUBLIC} SEAL::seal_shared Microsoft.GSL::GSL spdlog::spdlog_header_only Threads::Threads)
44+
45+
set(MVP_LIBS ${MVP_PUBLIC})
46+
47+
48+
if (MVP_ENABLE_TESTS)
49+
enable_testing()
50+
find_package(GTest REQUIRED)
51+
add_executable(mvp_pub_tests)
52+
add_subdirectory(tests/)
53+
target_link_libraries(mvp_pub_tests GTest::gtest_main GTest::gtest ${MVP_LIBS})
54+
endif()
55+
56+
if (MVP_ENABLE_PYTHON_WRAPPER)
57+
add_subdirectory(python)
58+
endif()
59+
60+
## build CLI
61+
add_subdirectory(cli)
62+
63+
macro(add_exe FILE)
64+
add_executable(${FILE}_cc api_examples/${FILE}.cc)
65+
target_link_libraries(${FILE}_cc ${MVP_LIBS})
66+
target_compile_definitions(${FILE}_cc PUBLIC)
67+
endmacro()
68+
69+
if (MVP_ENABLE_DEMO)
70+
add_exe(demo_standalone)
71+
add_exe(demo_end_to_end)
72+
add_exe(demo_user_key)
73+
endif()
74+
75+

mvp_sdk/README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# CHAM SDK
2+
3+
## 项目结构
4+
5+
```
6+
mvp_public
7+
├── api_examples
8+
├── coverage
9+
│   └── html
10+
│   └── mvp_public
11+
│   └── util
12+
├── python
13+
├── deps
14+
├── include
15+
│   ├── mvp_private
16+
│   │   └── lib
17+
│   │   ├── linux
18+
│   │   └── mac
19+
│   └── mvp_public
20+
│   ├── F3
21+
│   └── util
22+
├── scripts
23+
└── tests
24+
25+
```
26+
27+
* `api_examples` 包含了三个 demo 样例
28+
* `coverage` 包含了 SDK 的单元测试下的覆盖率
29+
* `python` 包含了一层简单的 Python Wrapper 调用 SDK
30+
* `deps` 包含了依赖的三方包的源码。包括
31+
* `microsoft/SEAL` (版本 3.7.2 )基础的全同态加密库。本 SDK 涉及的所有同态加密的所有操作都基于 SEAL
32+
* `microsoft/GSL` (版本 3.1.0)提供 `gsl::span` 接口。
33+
* `intel/hexl`(版本1.2.2)为 SEAL 提供基于 AVX512 的加速。
34+
* `google/cpu_features` (版本 0.7.0) hexl 的依赖库。用于获取 CPU 支持的指令。
35+
* `google/googletests` 用于单元测试
36+
* `facebook/zstd`(版本 1.5.0) 用于 SEAL 中密文对象的序列化/反序列化。
37+
38+
* `include/mvp_public` 下则是本 SDK 的源码
39+
* `scripts` 包含两个用于编译的脚本
40+
* `tests` 单元测试。需要 Googletest
41+
42+
## 编译
43+
44+
### 前置需求
45+
* C++ compiler 支持 c++17
46+
* cmake (>=3.10)
47+
48+
### 执行编译
49+
* `bash scripts/build-deps.sh` 会编译所有依赖的三方库 `deps/`默认会使用HEXL库,如CPU不支持,在编译时使用`bash script/build-deps.sh 0`将其关闭
50+
* `bash scripts/build.sh` 会编译本 SDK
51+
* `scripts/build.sh` 提供四个可变更项
52+
* `MVP_ENABLE_TESTS=ON` 设置 `OFF` 则跳过单元测试的编译
53+
* `MVP_ENABLE_DEMO=ON` 设置 `OFF` 则跳过 demo 样例的编译
54+
* `MVP_ENABLE_PYTHON_WRAPPER=ON` 设置 `OFF` 则跳过 Python wrapper 的编译
55+
* `PYTHON_EXE` 设置具体的 Python 版本
56+
57+
### 产出物
58+
59+
* SDK `libmvp_public.a` 存放在 `build/lib/` 目录下
60+
* Python wrapper `_mvp_py.cpython-*.so` 存放在 `build/lib/` 目录下
61+
* demo 样例存放在 `build/bin/` 目录下
62+
* 单元测试 `mvp_pub_tests` 存放在 `build/bin/` 目录下
63+
64+
### Demo 1:
65+
66+
`api_examples/demo_standalone.cc` 样例展示了如何使用本 SDK 完成一次双方的矩阵向量乘。demo 参数: `nrows ncols is_v12 num_threads`
67+
68+
分别代表矩阵行数(2 <=nrows<=4096);矩阵列数 (1<=ncols);是否用 v1.2 功能;以及最大线程数目。
69+
70+
`build/bin/demo_standalone_cc 256 1024 1 4` 则会利用 FPGAv1.2 的功能 和 4 个线程去计算 256x1024 的矩阵向量乘。
71+
72+
### Demo 2:
73+
74+
`demo_standalone.cc` 的基础上。我们模拟网络通信。
75+
76+
`api_examples/demo_end_to_end.cc` 样例展示了如何使用本 SDK 完成一次双方的矩阵向量乘。即一方持有矩阵;而向量则以秘密分享的形式被两方持有。demo 参数: `nrows ncols is_matrix_holder` 分别代表矩阵行数(2 <=nrows<=4096);矩阵列数 (1<=ncols);以及是否是矩阵的持有方 (is_matrix_holder=0/1)。本样例需要启动 2 个进程去模拟协议执行双方。如
77+
78+
`build/bin/demo_end_to_end_cc 256 1024 0 & build/bin/demo_end_to_end_cc 256 1024 1`
79+
80+
本样例会通过 `127.0.0.1:12345` 端口去模拟网络传输。请确保端口 `12345` 是可使用的。
81+
82+
### Demo 3:
83+
`api_examples/demo_user_key.cc` 样例展示了使用 `api::ConvertUserKey` 接口直接利用用户已有的 SEAL 密钥进行运算。 如 H1 可以作为已有的 SEAL 应用的一部分;而无需生成新的密钥。减轻密钥管理的负担。即使既有的 SEAL 密钥是基于非 CKKS 方案也是可行的。
84+
85+
### Demo 4:
86+
`python_example.py` 展示了 Python API 的使用。以联邦学习中为例子, 计算 mat*(vec0+vec1)+0.1*w.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
3+
project(mvp_pubilc_api_examples)
4+
include(CMakeFindDependencyMacro)
5+
6+
set(CMAKE_CXX_STANDARD 17)
7+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../lib)
8+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../lib)
9+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../bin)
10+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g")
11+
12+
option(MVP_ENABLE_FPGA "enable fpga" OFF)
13+
14+
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
15+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
16+
endif()
17+
18+
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include)
19+
set(mvp_private ${CMAKE_CURRENT_SOURCE_DIR}/../include/mvp_private/lib/mac/libmvp_private.a)
20+
set(mvp_public ${CMAKE_CURRENT_SOURCE_DIR}/../build/lib/libmvp_public.a)
21+
22+
## SEAL
23+
find_package(SEAL REQUIRED PACKAGE_FIND_VERSION_MAJOR 3)
24+
## MS-GSL
25+
find_package(Microsoft.GSL REQUIRED)
26+
## Threads
27+
find_package(Threads REQUIRED)
28+
29+
set(LIBS SEAL::seal_shared Microsoft.GSL::GSL Threads::Threads ${mvp_private} ${mvp_public})
30+
if (MVP_ENABLE_FPGA)
31+
set(LIBS "${LIBS} ${CMAKE_CURRENT_SOURCE_DIR}/../include/mvp_private/linux/libndd.so")
32+
endif()
33+
34+
macro(add_demo FILE)
35+
add_executable(${FILE}_cc ${FILE}.cc)
36+
target_link_libraries(${FILE}_cc ${LIBS})
37+
target_compile_definitions(${FILE}_cc PUBLIC)
38+
endmacro()
39+
40+
add_demo(demo_standalone)
41+
add_demo(demo_end_to_end)
42+
add_demo(demo_user_key)

0 commit comments

Comments
 (0)