├── .ci └── jenkins │ └── jenkins-trigger.groovy ├── .clang-format ├── .clang-tidy ├── .gitattributes ├── .gitignore ├── .pre-commit-config.yaml ├── .style.yapf ├── CMakeLists.txt ├── CONTRIB.md ├── LICENSE ├── README.md ├── cmake ├── Modules │ ├── FindGMP.cmake │ └── FindGMPXX.cmake ├── abseil.cmake ├── aby.cmake ├── boost.cmake ├── clang_flags.cmake ├── gtest.cmake ├── json.cmake ├── ngraph-tf-bfloat.patch ├── ngraph-tf.cmake ├── ngraph-tf.patch ├── ngraph.cmake ├── openmp.cmake ├── protobuf.cmake ├── sdl.cmake ├── seal.aby_patch ├── seal.cmake └── seal.patch ├── configs ├── he_seal_ckks_config_N11_L1.json ├── he_seal_ckks_config_N11_L1_small.json ├── he_seal_ckks_config_N11_L2.json ├── he_seal_ckks_config_N11_L4_debug.json ├── he_seal_ckks_config_N12_L4.json ├── he_seal_ckks_config_N12_L4_complex.json ├── he_seal_ckks_config_N13_L10_debug.json ├── he_seal_ckks_config_N13_L5_gc.json ├── he_seal_ckks_config_N13_L7.json ├── he_seal_ckks_config_N13_L7_lazy.json ├── he_seal_ckks_config_N13_L8.json ├── he_seal_ckks_config_N13_L9_debug.json └── he_seal_ckks_config_N14_L10.json ├── contrib └── docker │ ├── Dockerfile.he_transformer.centos74 │ ├── Dockerfile.he_transformer.fedora28 │ ├── Dockerfile.he_transformer.ubuntu1604 │ ├── Dockerfile.he_transformer.ubuntu1804 │ ├── Makefile │ ├── README.md │ ├── build-he-transformer-and-test.sh │ ├── docker_cleanup.sh │ ├── make-dimage.sh │ ├── make_docker_image.sh │ ├── run_as_centos_user.sh │ ├── run_as_fedora_user.sh │ └── run_as_ubuntu_user.sh ├── doc ├── CMakeLists.txt ├── README.md └── doxygen │ ├── CMakeLists.txt │ └── Doxyfile.in ├── examples ├── ImageNet │ └── MobileNetV2 │ │ ├── README.md │ │ ├── client.py │ │ ├── get_models.py │ │ ├── test.py │ │ └── util.py ├── MNIST │ ├── Cryptonets-Relu │ │ ├── __init__.py │ │ ├── model.py │ │ └── train.py │ ├── Cryptonets │ │ ├── __init__.py │ │ ├── model.py │ │ └── train.py │ ├── MLP │ │ ├── __init__.py │ │ ├── model.py │ │ └── train.py │ ├── README.md │ ├── __init__.py │ ├── mnist_util.py │ ├── pyclient_mnist.py │ └── test.py ├── README.md ├── ax.py └── pyclient.py ├── images └── nGraph_mask_1-1.png ├── maint ├── apply-code-format.sh ├── bash_lib.sh ├── check-code-format.sh ├── clang_format_lib.sh └── yapf_lib.sh ├── python ├── CMakeLists.txt ├── build_wheel.sh.in ├── pyhe_client │ ├── he_seal_client.cpp │ ├── he_seal_client.hpp │ └── pyhe_client.cpp └── setup.py ├── src ├── CMakeLists.txt ├── aby │ ├── aby_client_executor.cpp │ ├── aby_client_executor.hpp │ ├── aby_executor.cpp │ ├── aby_executor.hpp │ ├── aby_server_executor.cpp │ ├── aby_server_executor.hpp │ ├── aby_util.hpp │ └── kernel │ │ └── relu_aby.hpp ├── he_op_annotations.cpp ├── he_op_annotations.hpp ├── he_plaintext.cpp ├── he_plaintext.hpp ├── he_tensor.cpp ├── he_tensor.hpp ├── he_type.cpp ├── he_type.hpp ├── he_util.cpp ├── he_util.hpp ├── logging │ ├── ngraph_he_log.cpp │ └── ngraph_he_log.hpp ├── op │ ├── bounded_relu.cpp │ └── bounded_relu.hpp ├── pass │ ├── he_fusion.cpp │ ├── he_fusion.hpp │ ├── he_liveness.cpp │ ├── he_liveness.hpp │ ├── propagate_he_annotations.cpp │ ├── propagate_he_annotations.hpp │ ├── supported_ops.cpp │ └── supported_ops.hpp ├── protos │ ├── CMakeLists.txt │ └── message.proto ├── seal │ ├── he_seal_backend.cpp │ ├── he_seal_backend.hpp │ ├── he_seal_client.cpp │ ├── he_seal_client.hpp │ ├── he_seal_encryption_parameters.cpp │ ├── he_seal_encryption_parameters.hpp │ ├── he_seal_executable.cpp │ ├── he_seal_executable.hpp │ ├── kernel │ │ ├── add_seal.cpp │ │ ├── add_seal.hpp │ │ ├── avg_pool_seal.hpp │ │ ├── batch_norm_inference_seal.hpp │ │ ├── bounded_relu_seal.cpp │ │ ├── bounded_relu_seal.hpp │ │ ├── broadcast_seal.hpp │ │ ├── concat_seal.hpp │ │ ├── constant_seal.cpp │ │ ├── constant_seal.hpp │ │ ├── convolution_seal.cpp │ │ ├── convolution_seal.hpp │ │ ├── divide_seal.cpp │ │ ├── divide_seal.hpp │ │ ├── dot_seal.cpp │ │ ├── dot_seal.hpp │ │ ├── exp_seal.cpp │ │ ├── exp_seal.hpp │ │ ├── max_pool_seal.hpp │ │ ├── max_seal.hpp │ │ ├── minimum_seal.cpp │ │ ├── minimum_seal.hpp │ │ ├── mod_reduce_seal.hpp │ │ ├── multiply_seal.cpp │ │ ├── multiply_seal.hpp │ │ ├── negate_seal.cpp │ │ ├── negate_seal.hpp │ │ ├── pad_seal.cpp │ │ ├── pad_seal.hpp │ │ ├── power_seal.cpp │ │ ├── power_seal.hpp │ │ ├── relu_seal.cpp │ │ ├── relu_seal.hpp │ │ ├── rescale_seal.cpp │ │ ├── rescale_seal.hpp │ │ ├── reshape_seal.hpp │ │ ├── result_seal.hpp │ │ ├── reverse_seal.hpp │ │ ├── slice_seal.hpp │ │ ├── softmax_seal.cpp │ │ ├── softmax_seal.hpp │ │ ├── subtract_seal.cpp │ │ ├── subtract_seal.hpp │ │ └── sum_seal.hpp │ ├── opset_he_seal_tbl.hpp │ ├── seal_ciphertext_wrapper.cpp │ ├── seal_ciphertext_wrapper.hpp │ ├── seal_plaintext_wrapper.cpp │ ├── seal_plaintext_wrapper.hpp │ ├── seal_util.cpp │ ├── seal_util.hpp │ └── unit_test.manifest └── tcp │ ├── tcp_client.cpp │ ├── tcp_client.hpp │ ├── tcp_message.cpp │ ├── tcp_message.hpp │ ├── tcp_session.cpp │ └── tcp_session.hpp ├── test ├── .clang-tidy ├── CMakeLists.txt ├── main.cpp ├── test_aby.cpp ├── test_aby_relu.cpp ├── test_add.in.cpp ├── test_avg_pool.in.cpp ├── test_basics.in.cpp ├── test_batch_norm_inference.in.cpp ├── test_bounded_relu.cpp ├── test_broadcast.in.cpp ├── test_concat.in.cpp ├── test_constant.in.cpp ├── test_convolution.in.cpp ├── test_divide.in.cpp ├── test_dot.in.cpp ├── test_encryption_parameters.cpp ├── test_exp.in.cpp ├── test_he_fusion.cpp ├── test_he_op_annotations.cpp ├── test_he_plaintext.cpp ├── test_he_seal_executable.cpp ├── test_he_supported_ops.cpp ├── test_he_tensor.cpp ├── test_he_type.cpp ├── test_he_util.cpp ├── test_layers.in.cpp ├── test_max.in.cpp ├── test_maxpool.in.cpp ├── test_minimum.in.cpp ├── test_multiply.in.cpp ├── test_negate.in.cpp ├── test_ngraph_he_log.cpp ├── test_pad.in.cpp ├── test_perf_micro.cpp ├── test_power.in.cpp ├── test_propagate_he_annotations.cpp ├── test_protobuf.cpp ├── test_read_write.in.cpp ├── test_relu.in.cpp ├── test_rescale.in.cpp ├── test_reshape.in.cpp ├── test_result.in.cpp ├── test_reverse.in.cpp ├── test_seal.cpp ├── test_seal_plaintext_wrapper.cpp ├── test_seal_util.cpp ├── test_server_client.in.cpp ├── test_server_client_gc.in.cpp ├── test_slice.in.cpp ├── test_softmax.in.cpp ├── test_subtract.in.cpp ├── test_sum.in.cpp ├── test_tcp_client.cpp ├── test_tcp_message.cpp └── test_util.hpp └── tools └── coverage └── README.md /.ci/jenkins/jenkins-trigger.groovy: -------------------------------------------------------------------------------- 1 | // Copyright 2018-2020 Intel Corporation 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // This script acts as a trigger script for the main he-transformer-unittest.groovy 16 | // Jenkins job. This script is part of a Jenkins multi-branch pipeline job 17 | // which can trigger GitHub jobs more effectively than the GitHub Pull 18 | // Request Builder (GHPRB) plugin, in our environment. 19 | 20 | // The original he-transformer-unittest job required the following parameters. We 21 | // set these up below as global variables, so we do not need to rewrite the 22 | // original script -- we only need to provide this new trigger hook. 23 | // 24 | 25 | String JENKINS_BRANCH = "master" 26 | String TIMEOUTTIME = "3600" 27 | 28 | // Constants 29 | String JENKINS_DIR = "jenkins" 30 | 31 | timestamps { 32 | 33 | node("trigger") { 34 | 35 | deleteDir() // Clear the workspace before starting 36 | 37 | // Clone the cje-algo directory which contains our Jenkins groovy scripts 38 | def sleeptime=0 39 | retry(count: 3) { 40 | sleep sleeptime; sleeptime = 10 41 | sh "git clone -b $JENKINS_BRANCH https://gitlab.devtools.intel.com/AIPG/AlgoVal/cje-algo $JENKINS_DIR" 42 | } 43 | def heTransformerCIPreMerge = load("$JENKINS_DIR/hetransformer-lib/he-transformer-ci-premerge.groovy") 44 | heTransformerCIPreMerge(prURL: CHANGE_URL, 45 | prAuthor: CHANGE_AUTHOR, 46 | useMBPipelineSCM: 'true', 47 | checkoutBranch: '-UNDEFINED-BRANCH-' 48 | ) 49 | echo "he-transformer-ci-premerge.groovy completed" 50 | 51 | } // End: node 52 | 53 | } // End: timestamps 54 | 55 | echo "Done" 56 | -------------------------------------------------------------------------------- /.clang-format: -------------------------------------------------------------------------------- 1 | BasedOnStyle: Google 2 | DerivePointerAlignment: false 3 | -------------------------------------------------------------------------------- /.clang-tidy: -------------------------------------------------------------------------------- 1 | # Configure clang-tidy for this project. 2 | Checks: > 3 | -*, 4 | bugprone-*, 5 | -bugprone-narrowing-conversions, 6 | cppcoreguidelines-*, 7 | -cppcoreguidelines-pro-bounds-array-to-pointer-decay, 8 | -cppcoreguidelines-pro-type-static-cast-downcast, 9 | -cppcoreguidelines-pro-bounds-pointer-arithmetic, 10 | -cppcoreguidelines-pro-type-reinterpret-cast, 11 | -cppcoreguidelines-special-member-functions, 12 | -cppcoreguidelines-owning-memory, 13 | -cppcoreguidelines-pro-type-vararg, 14 | -cppcoreguidelines-interfaces-global-init, 15 | -cppcoreguidelines-macro-usage, 16 | -cppcoreguidelines-narrowing-conversions, 17 | -cppcoreguidelines-avoid-magic-numbers, 18 | -cppcoreguidelines-pro-type-member-init, 19 | google-*, 20 | -google-runtime-references, 21 | -google-default-arguments, 22 | misc-*, 23 | -misc-unused-parameters, 24 | modernize-*, 25 | -modernize-use-trailing-return-type, 26 | -modernize-use-nodiscard, 27 | performance-*, 28 | -performance-unnecessary-value-param, 29 | portability-*, 30 | readability-*, 31 | -readability-magic-numbers, 32 | -readability-uppercase-literal-suffix 33 | 34 | HeaderFilterRegex: 'src/[^/]*\.(h|hpp|cpp)$' 35 | 36 | CheckOptions: 37 | # Variables, member variables, ... 38 | - key: readability-identifier-naming.ParameterCase 39 | value: lower_case 40 | - key: readability-identifier-naming.VariableCase 41 | value: lower_case 42 | - key: readability-identifier-naming.MemberCase 43 | value: lower_case 44 | - key: readability-identifier-naming.PublicMemberCase 45 | value: lower_case 46 | - key: readability-identifier-naming.ProtectedMemberCase 47 | value: lower_case 48 | - key: readability-identifier-naming.PrivateMemberCase 49 | value: lower_case 50 | - key: readability-identifier-naming.PrivateMemberPrefix 51 | value: m_ 52 | - key: readability-identifier-naming.ProtectedMemberPrefix 53 | value: m_ 54 | - key: readability-identifier-naming.PublicMemberPrefix 55 | value: m_ 56 | - key: readability-identifier-naming.MemberPrefix 57 | value: m_ 58 | - key: readability-identifier-naming.ClassCase 59 | value: CamelCase 60 | - key: readability-identifier-naming.AbstractClassCase 61 | value: CamelCase 62 | - key: readability-identifier-naming.ClassMethodCase 63 | value: lower_case 64 | - key: readability-identifier-naming.StaticConstantCase 65 | value: lower_case 66 | - key: readability-identifier-naming.MethodCase 67 | value: lower_case 68 | - key: readability-identifier-naming.FunctionCase 69 | value: lower_case 70 | - key: readability-identifier-naming.PrivateMethodCase 71 | value: lower_case 72 | - key: readability-identifier-naming.PrivateMethodCase 73 | value: lower_case 74 | - key: readability-identifier-naming.ProtectedMethodCase 75 | value: lower_case 76 | - key: readability-identifier-naming.PublicMethodCase 77 | value: lower_case 78 | - key: readability-identifier-naming.VirtualMethodCase 79 | value: lower_case 80 | - key: readability-identifier-naming.NamespaceCase 81 | value: lower_case 82 | 83 | 84 | 85 | # Turn all the warnings from the checks above into errors. 86 | WarningsAsErrors: "*" 87 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Set the default behavior, in case people don't have core.autocrlf set. 2 | * text=auto 3 | 4 | # Explicitly declare text files you want to always be normalized and converted 5 | # to native line endings on checkout. 6 | *.cpp text 7 | *.hpp text 8 | *.py text 9 | 10 | *png binary 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/ 2 | build/ 3 | build_python 4 | _out/ 5 | .venv/ 6 | .DS_Store 7 | ._.DS_Store 8 | **/.DS_Store 9 | **/._.DS_Store 10 | *__pycache__ 11 | *.cache 12 | *.log 13 | *.pyc 14 | *.dSYM 15 | *.stylecheck 16 | *.styled 17 | *.d 18 | *.o 19 | artransformer.egg-info 20 | *.swp 21 | *.swo 22 | *.cmd 23 | *.ko 24 | *.mod.c 25 | Module.symvers 26 | modules.order 27 | .tmp_versions 28 | examples/*.js 29 | examples/cpu_codegen/ 30 | data/ 31 | log/ 32 | weights/ 33 | 34 | # Tensorflow models 35 | *.tgz 36 | *.pb 37 | *.pbtxt 38 | *.tflite 39 | *.data-*-of* 40 | *ckpt.index 41 | *ckpt.meta 42 | -------------------------------------------------------------------------------- /.pre-commit-config.yaml: -------------------------------------------------------------------------------- 1 | repos: 2 | - repo: https://github.com/pre-commit/pre-commit-hooks 3 | rev: v2.0.0 4 | hooks: 5 | - id: check-added-large-files 6 | - id: check-case-conflict 7 | - id: check-json 8 | - id: check-merge-conflict 9 | - id: check-symlinks 10 | - id: check-yaml 11 | - id: trailing-whitespace 12 | - id: end-of-file-fixer 13 | - repo: local 14 | hooks: 15 | - id: clang-format 16 | name: clang-format 17 | entry: clang-format 18 | language: system 19 | files: \.(c|cc|cxx|cpp|h|hpp|hxx|js|proto)$ 20 | args: ["-i"] 21 | - id: cpplint 22 | name: cpplint 23 | entry: cpplint 24 | language: system 25 | files: \.(c|cc|cxx|cpp|h|hpp|hxx)$ 26 | args: 27 | - --filter=-runtime/references,-readability/nolint,-build/c++11 28 | # - id: yapf 29 | # name: yapf 30 | # entry: yapf 31 | # language: system 32 | # files: \.py$ 33 | # args: [-i] 34 | -------------------------------------------------------------------------------- /.style.yapf: -------------------------------------------------------------------------------- 1 | [style] 2 | based_on_style = google 3 | -------------------------------------------------------------------------------- /CONTRIB.md: -------------------------------------------------------------------------------- 1 | Contributor Guidelines 2 | ====================== 3 | 4 | License 5 | ------- 6 | 7 | All contributed code must be compatible with the [Apache 8 | 2](https://www.apache.org/licenses/LICENSE-2.0) license, preferably by 9 | being contributed under the Apache 2 license. Code contributed with 10 | another license will need the license reviewed by Intel before it can be 11 | accepted. 12 | 13 | Code formatting 14 | --------------- 15 | 16 | All C/C++ source code in the repository, including the test code, must 17 | adhere to the source-code formatting and style guidelines described 18 | here. The coding style described here applies to the he-transformer repository. 19 | Related repositories may make adjustements to better match the coding 20 | styles of libraries they are using. 21 | 22 | 23 | #### Formatting 24 | 25 | Things that look different should look different because they are 26 | different. We use **clang format** to enforce certain formatting. 27 | Although not always ideal, it is automatically enforced and reduces 28 | merge conflicts. 29 | 30 | - The .clang-format file located in the root of the project specifies 31 | our format. 32 | - The script maint/apply-code-format.sh enforces that formatting 33 | at the C/C++ syntactic level. 34 | - The script at maint/check-code-format.sh verifies that the 35 | formatting rules are met by all C/C++ code (again, at the 36 | syntax level). The script has an exit code of `0` when code 37 | meets the standard and non-zero otherwise. This script does 38 | *not* modify the source code. 39 | -------------------------------------------------------------------------------- /cmake/Modules/FindGMP.cmake: -------------------------------------------------------------------------------- 1 | find_path(GMP_INCLUDE_DIR gmp.h) 2 | 3 | # TODO: get version 4 | 5 | find_library(GMP_LIBRARY NAMES gmp) 6 | 7 | include(FindPackageHandleStandardArgs) 8 | find_package_handle_standard_args(GMP 9 | FOUND_VAR 10 | GMP_FOUND 11 | REQUIRED_VARS 12 | GMP_LIBRARY 13 | GMP_INCLUDE_DIR) 14 | 15 | if(GMP_FOUND AND NOT TARGET GMP::GMP) 16 | add_library(GMP::GMP UNKNOWN IMPORTED) 17 | set_target_properties(GMP::GMP 18 | PROPERTIES IMPORTED_LOCATION 19 | "${GMP_LIBRARY}" 20 | INTERFACE_INCLUDE_DIRECTORIES 21 | "${GMP_INCLUDE_DIR}") 22 | endif() 23 | 24 | mark_as_advanced(GMP_INCLUDE_DIR GMP_LIBRARY) 25 | -------------------------------------------------------------------------------- /cmake/Modules/FindGMPXX.cmake: -------------------------------------------------------------------------------- 1 | find_path(GMPXX_INCLUDE_DIR gmpxx.h) 2 | 3 | # TODO: get version 4 | 5 | find_library(GMPXX_LIBRARY NAMES gmpxx) 6 | 7 | 8 | include(FindPackageHandleStandardArgs) 9 | find_package_handle_standard_args(GMPXX 10 | FOUND_VAR 11 | GMPXX_FOUND 12 | REQUIRED_VARS 13 | GMPXX_LIBRARY 14 | GMPXX_INCLUDE_DIR) 15 | 16 | if(GMPXX_FOUND AND NOT TARGET GMP::GMPXX) 17 | add_library(GMP::GMPXX UNKNOWN IMPORTED) 18 | set_target_properties(GMP::GMPXX 19 | PROPERTIES IMPORTED_LOCATION 20 | "${GMPXX_LIBRARY}" 21 | INTERFACE_INCLUDE_DIRECTORIES 22 | "${GMPXX_INCLUDE_DIR}") 23 | endif() 24 | 25 | mark_as_advanced(GMPXX_INCLUDE_DIR GMPXX_LIBRARY) 26 | -------------------------------------------------------------------------------- /cmake/abseil.cmake: -------------------------------------------------------------------------------- 1 | # ****************************************************************************** 2 | # Copyright 2018-2020 Intel Corporation 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | # use this file except in compliance with the License. You may obtain a copy of 6 | # the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | # License for the specific language governing permissions and limitations under 14 | # the License. 15 | # ****************************************************************************** 16 | 17 | include(ExternalProject) 18 | 19 | 20 | set(ABSEIL_REPO_URL https://github.com/abseil/abseil-cpp.git) 21 | set(ABSEIL_GIT_TAG master) 22 | set(ABSEIL_CXX_FLAGS "-O2 -Wformat -Wformat-security -D_FORTIFY_SOURCE=2 -fstack-protector-all -march=native") 23 | 24 | # Using header-only, so no need to build 25 | ExternalProject_Add(ext_abseil 26 | PREFIX ext_abseil 27 | GIT_REPOSITORY ${ABSEIL_REPO_URL} 28 | GIT_TAG ${ABY_GIT_TAG} 29 | CONFIGURE_COMMAND "" 30 | BUILD_COMMAND "" 31 | INSTALL_COMMAND "" 32 | UPDATE_COMMAND "" 33 | EXCLUDE_FROM_ALL TRUE) 34 | 35 | ExternalProject_Get_Property(ext_abseil SOURCE_DIR) 36 | add_library(libabseil INTERFACE) 37 | target_include_directories(libabseil SYSTEM INTERFACE ${SOURCE_DIR}) 38 | add_dependencies(libabseil ext_abseil) 39 | 40 | set(abseil_INCLUDE_DIR ${SOURCE_DIR}) 41 | -------------------------------------------------------------------------------- /cmake/boost.cmake: -------------------------------------------------------------------------------- 1 | # ****************************************************************************** 2 | # Copyright 2018-2020 Intel Corporation 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | # use this file except in compliance with the License. You may obtain a copy of 6 | # the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | # License for the specific language governing permissions and limitations under 14 | # the License. 15 | # ****************************************************************************** 16 | 17 | include(ExternalProject) 18 | 19 | set(BOOST_REPO_URL 20 | https://boostorg.jfrog.io/artifactory/main/release/1.69.0/source/boost_1_69_0.tar.gz) 21 | 22 | ExternalProject_Add( 23 | ext_boost 24 | PREFIX boost 25 | URL ${BOOST_REPO_URL} 26 | URL_HASH 27 | SHA256=9a2c2819310839ea373f42d69e733c339b4e9a19deab6bfec448281554aa4dbb 28 | # Disable install step 29 | CONFIGURE_COMMAND "" 30 | BUILD_COMMAND "" 31 | INSTALL_COMMAND "" 32 | UPDATE_COMMAND "" 33 | EXCLUDE_FROM_ALL TRUE) 34 | 35 | ExternalProject_Get_Property(ext_boost SOURCE_DIR) 36 | 37 | set(BOOST_HEADERS_PATH ${SOURCE_DIR}) 38 | add_library(libboost INTERFACE) 39 | add_dependencies(libboost ext_boost) 40 | target_include_directories(libboost SYSTEM INTERFACE ${BOOST_HEADERS_PATH}) 41 | -------------------------------------------------------------------------------- /cmake/clang_flags.cmake: -------------------------------------------------------------------------------- 1 | # ****************************************************************************** 2 | # Copyright 2018-2020 Intel Corporation 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | # use this file except in compliance with the License. You may obtain a copy of 6 | # the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | # License for the specific language governing permissions and limitations under 14 | # the License. 15 | # ****************************************************************************** 16 | 17 | add_compile_options(-Werror=return-type) 18 | add_compile_options(-Werror=inconsistent-missing-override) 19 | add_compile_options(-Werror=comment) 20 | add_compile_options(-pedantic-errors) 21 | 22 | # whitelist errors here 23 | add_compile_options(-Weverything) 24 | add_compile_options(-Wno-gnu-zero-variadic-macro-arguments) 25 | add_compile_options(-Wno-c++98-compat-pedantic) 26 | add_compile_options(-Wno-weak-vtables) 27 | add_compile_options(-Wno-global-constructors) 28 | add_compile_options(-Wno-exit-time-destructors) 29 | add_compile_options(-Wno-missing-prototypes) 30 | add_compile_options(-Wno-missing-noreturn) 31 | add_compile_options(-Wno-covered-switch-default) 32 | add_compile_options(-Wno-undef) 33 | add_compile_options(-Wno-newline-eof) 34 | if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang") 35 | if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 9.1.0) 36 | add_compile_options(-Wno-zero-as-null-pointer-constant) 37 | endif() 38 | endif() 39 | 40 | # should remove these 41 | add_compile_options(-Wno-float-conversion) 42 | add_compile_options(-Wno-sign-conversion) 43 | add_compile_options(-Wno-padded) 44 | add_compile_options(-Wno-sign-compare) 45 | add_compile_options(-Wno-unused-parameter) 46 | add_compile_options(-Wno-conversion) 47 | add_compile_options(-Wno-double-promotion) 48 | add_compile_options(-Wno-undefined-func-template) 49 | -------------------------------------------------------------------------------- /cmake/gtest.cmake: -------------------------------------------------------------------------------- 1 | # ****************************************************************************** 2 | # Copyright 2018-2020 Intel Corporation 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | # use this file except in compliance with the License. You may obtain a copy of 6 | # the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | # License for the specific language governing permissions and limitations under 14 | # the License. 15 | # ****************************************************************************** 16 | 17 | # Enable ExternalProject CMake module 18 | include(ExternalProject) 19 | 20 | # ------------------------------------------------------------------------------ 21 | # Download and install GoogleTest ... 22 | # ------------------------------------------------------------------------------ 23 | 24 | set(GTEST_GIT_REPO_URL https://github.com/google/googletest.git) 25 | set(GTEST_GIT_LABEL release-1.8.1) 26 | set(GTEST_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") 27 | 28 | ExternalProject_Add( 29 | ext_gtest 30 | PREFIX gtest 31 | GIT_REPOSITORY ${GTEST_GIT_REPO_URL} 32 | GIT_TAG ${GTEST_GIT_LABEL} 33 | INSTALL_COMMAND "" 34 | UPDATE_COMMAND "" 35 | CMAKE_ARGS 36 | ${NGRAPH_HE_FORWARD_CMAKE_ARGS} 37 | -DCMAKE_CXX_FLAGS=${GTEST_CXX_FLAGS} 38 | ${GTEST_CMAKE_ARGS} 39 | TMP_DIR "${EXTERNAL_PROJECTS_ROOT}/gtest/tmp" 40 | STAMP_DIR "${EXTERNAL_PROJECTS_ROOT}/gtest/stamp" 41 | DOWNLOAD_DIR "${EXTERNAL_PROJECTS_ROOT}/gtest/download" 42 | SOURCE_DIR "${EXTERNAL_PROJECTS_ROOT}/gtest/src" 43 | BINARY_DIR "${EXTERNAL_PROJECTS_ROOT}/gtest/build" 44 | INSTALL_DIR "${EXTERNAL_PROJECTS_ROOT}/gtest" 45 | BUILD_BYPRODUCTS 46 | "${EXTERNAL_PROJECTS_ROOT}/gtest/build/googlemock/gtest/libgtest.a" 47 | EXCLUDE_FROM_ALL TRUE) 48 | 49 | # ------------------------------------------------------------------------------ 50 | 51 | ExternalProject_Get_Property(ext_gtest SOURCE_DIR BINARY_DIR) 52 | 53 | add_library(libgtest INTERFACE) 54 | add_dependencies(libgtest ext_gtest) 55 | 56 | target_include_directories(libgtest SYSTEM 57 | INTERFACE ${SOURCE_DIR}/googletest/include) 58 | target_link_libraries(libgtest 59 | INTERFACE ${BINARY_DIR}/googlemock/gtest/libgtest.a) 60 | -------------------------------------------------------------------------------- /cmake/json.cmake: -------------------------------------------------------------------------------- 1 | # ****************************************************************************** 2 | # Copyright 2018-2020 Intel Corporation 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | # use this file except in compliance with the License. You may obtain a copy of 6 | # the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | # License for the specific language governing permissions and limitations under 14 | # the License. 15 | # ****************************************************************************** 16 | 17 | include(ExternalProject) 18 | 19 | set(JSON_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/json) 20 | set(JSON_SRC_DIR ${JSON_PREFIX}/src/ext_json) 21 | set(JSON_GIT_REPO_URL https://github.com/nlohmann/json) 22 | set(JSON_GIT_LABEL v3.5.0) 23 | 24 | ExternalProject_Add(ext_json 25 | PREFIX json 26 | GIT_REPOSITORY ${JSON_GIT_REPO_URL} 27 | GIT_TAG ${JSON_GIT_LABEL} 28 | CONFIGURE_COMMAND "" 29 | BUILD_COMMAND "" 30 | INSTALL_COMMAND "" 31 | UPDATE_COMMAND "" 32 | EXCLUDE_FROM_ALL TRUE) 33 | 34 | ExternalProject_Get_Property(ext_json SOURCE_DIR) 35 | add_library(libjson INTERFACE) 36 | target_include_directories(libjson SYSTEM INTERFACE ${SOURCE_DIR}/include) 37 | add_dependencies(libjson ext_json) 38 | 39 | set(json_INCLUDE_DIR ${SOURCE_DIR}/include) 40 | -------------------------------------------------------------------------------- /cmake/ngraph-tf-bfloat.patch: -------------------------------------------------------------------------------- 1 | diff --git a/tools/build_utils.py b/tools/build_utils.py 2 | index 8aab3a43..bb561d24 100755 3 | --- a/tools/build_utils.py 4 | +++ b/tools/build_utils.py 5 | @@ -152,7 +152,7 @@ def setup_venv(venv_dir): 6 | "setuptools", 7 | "psutil", 8 | "six>=1.10.0", 9 | - "numpy>=1.13.3", 10 | + "numpy>=1.16.0,<1.19.0", 11 | "absl-py>=0.1.6", 12 | "astor>=0.6.0", 13 | "google_pasta>=0.1.1", 14 | -------------------------------------------------------------------------------- /cmake/ngraph-tf.cmake: -------------------------------------------------------------------------------- 1 | # ****************************************************************************** 2 | # Copyright 2018-2020 Intel Corporation 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | # use this file except in compliance with the License. You may obtain a copy of 6 | # the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | # License for the specific language governing permissions and limitations under 14 | # the License. 15 | # ****************************************************************************** 16 | 17 | include(ExternalProject) 18 | 19 | set(EXTERNAL_NGRAPH_INSTALL_DIR ${EXTERNAL_INSTALL_DIR}) 20 | set(NGRAPH_TF_CMAKE_PREFIX ext_ngraph_tf) 21 | 22 | set(NGRAPH_TF_REPO_URL https://github.com/tensorflow/ngraph-bridge.git) 23 | set(NGRAPH_TF_GIT_LABEL v0.22.0-rc3) 24 | set(NGRAPH_TF_PATCH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/ngraph-tf.patch) 25 | set(NGRAPH_TF_PATCH2 ${CMAKE_CURRENT_SOURCE_DIR}/cmake/ngraph-tf-bfloat.patch) 26 | 27 | set(NGRAPH_TF_SRC_DIR 28 | ${CMAKE_BINARY_DIR}/${NGRAPH_TF_CMAKE_PREFIX}/src/${NGRAPH_TF_CMAKE_PREFIX}) 29 | set(NGRAPH_TF_BUILD_DIR ${NGRAPH_TF_SRC_DIR}/build_cmake) 30 | set(NGRAPH_TF_ARTIFACTS_DIR ${NGRAPH_TF_BUILD_DIR}/artifacts) 31 | 32 | set(NGRAPH_TF_VENV_DIR ${NGRAPH_TF_BUILD_DIR}/venv-tf-py3) 33 | 34 | if(OS_VERSION STREQUAL "centos") 35 | set(NGRAPH_TF_LIB_DIR ${NGRAPH_TF_ARTIFACTS_DIR}/lib64) 36 | else() 37 | set(NGRAPH_TF_LIB_DIR ${NGRAPH_TF_ARTIFACTS_DIR}/lib) 38 | endif() 39 | 40 | set( 41 | NGRAPH_TF_VENV_LIB_DIR 42 | ${NGRAPH_TF_VENV_DIR}/lib/${PYTHON_VENV_VERSION}/site-packages/ngraph_bridge) 43 | set(NGRAPH_TF_INCLUDE_DIR ${NGRAPH_TF_ARTIFACTS_DIR}/include) 44 | 45 | ExternalProject_Add(ext_ngraph_tf 46 | GIT_REPOSITORY ${NGRAPH_TF_REPO_URL} 47 | GIT_TAG ${NGRAPH_TF_GIT_LABEL} 48 | PREFIX ${NGRAPH_TF_CMAKE_PREFIX} 49 | CONFIGURE_COMMAND "" 50 | BUILD_IN_SOURCE 1 51 | BUILD_BYPRODUCTS ${NGRAPH_TF_CMAKE_PREFIX} 52 | PATCH_COMMAND git apply ${NGRAPH_TF_PATCH} 53 | COMMAND git apply ${NGRAPH_TF_PATCH2} 54 | BUILD_COMMAND python3 ${NGRAPH_TF_SRC_DIR}/build_ngtf.py --use_grappler_optimizer --ngraph_src_dir ${NGRAPH_SRC_DIR} 55 | INSTALL_COMMAND ln -fs ${NGRAPH_TF_VENV_DIR} 56 | ${EXTERNAL_INSTALL_DIR} 57 | UPDATE_COMMAND "") 58 | 59 | ExternalProject_Get_Property(ext_ngraph_tf SOURCE_DIR) 60 | add_library(libngraph_tf INTERFACE) 61 | add_dependencies(libngraph_tf ext_ngraph_tf) 62 | 63 | if(NOT EXISTS ${NGRAPH_TF_INCLUDE_DIR}) 64 | file(MAKE_DIRECTORY ${NGRAPH_TF_INCLUDE_DIR}) 65 | endif() 66 | 67 | install(DIRECTORY ${NGRAPH_TF_LIB_DIR}/ 68 | DESTINATION ${EXTERNAL_INSTALL_LIB_DIR} 69 | FILES_MATCHING 70 | PATTERN "*.so" 71 | PATTERN "*.so.*" 72 | PATTERN "*.a") 73 | 74 | install(DIRECTORY ${NGRAPH_TF_INCLUDE_DIR}/ 75 | DESTINATION ${EXTERNAL_INSTALL_INCLUDE_DIR} 76 | FILES_MATCHING 77 | PATTERN "*.h" 78 | PATTERN "*.hpp") 79 | -------------------------------------------------------------------------------- /cmake/ngraph-tf.patch: -------------------------------------------------------------------------------- 1 | diff --git a/python/ngraph_bridge/__init__.in.py b/python/ngraph_bridge/__init__.in.py 2 | index 0db5c524..649d7901 100644 3 | --- a/python/ngraph_bridge/__init__.in.py 4 | +++ b/python/ngraph_bridge/__init__.in.py 5 | @@ -140,7 +140,7 @@ if ngraph_classic_loaded: 6 | # Importing plaidml.settings -- if it exists -- will have read the 7 | # user's settings and configured the runtime environment 8 | # appropriately. 9 | - except ImportError: 10 | + except: 11 | pass 12 | 13 | 14 | -------------------------------------------------------------------------------- /cmake/ngraph.cmake: -------------------------------------------------------------------------------- 1 | # ****************************************************************************** 2 | # Copyright 2018-2020 Intel Corporation 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | # use this file except in compliance with the License. You may obtain a copy of 6 | # the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | # License for the specific language governing permissions and limitations under 14 | # the License. 15 | # ****************************************************************************** 16 | 17 | include(ExternalProject) 18 | 19 | set(EXTERNAL_NGRAPH_INSTALL_DIR ${EXTERNAL_INSTALL_DIR}) 20 | set(NGRAPH_CMAKE_PREFIX ext_ngraph) 21 | 22 | set(NGRAPH_REPO_URL https://github.com/NervanaSystems/ngraph.git) 23 | set(NGRAPH_GIT_LABEL v0.28.0-rc.1) 24 | 25 | set(NGRAPH_SRC_DIR 26 | ${CMAKE_BINARY_DIR}/${NGRAPH_CMAKE_PREFIX}/src/${NGRAPH_CMAKE_PREFIX}) 27 | set(NGRAPH_BUILD_DIR 28 | ${CMAKE_BINARY_DIR}/${NGRAPH_CMAKE_PREFIX}/src/${NGRAPH_CMAKE_PREFIX}-build) 29 | set(NGRAPH_TEST_INCLUDE_DIR ${NGRAPH_SRC_DIR}/test) 30 | 31 | 32 | message("NGRAPH_BUILD_DIR ${NGRAPH_BUILD_DIR}") 33 | message("NGRAPH_SRC_DIR ${NGRAPH_SRC_DIR}") 34 | 35 | ExternalProject_Add(ext_ngraph 36 | GIT_REPOSITORY ${NGRAPH_REPO_URL} 37 | GIT_TAG ${NGRAPH_GIT_LABEL} 38 | PREFIX ${NGRAPH_CMAKE_PREFIX} 39 | CMAKE_ARGS {NGRAPH_HE_FORWARD_CMAKE_ARGS} 40 | -DNGRAPH_UNIT_TEST_ENABLE=OFF 41 | -DNGRAPH_GENERIC_CPU_ENABLE=OFF 42 | -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_DIR} 43 | -DNGRAPH_GENERIC_CPU_ENABLE=OFF 44 | -DNGRAPH_CPU_ENABLE=OFF 45 | BUILD_BYPRODUCTS "${NGRAPH_BUILD_DIR}/src/ngraph/libngraph.so" 46 | "${NGRAPH_BUILD_DIR}/test/util/libngraph_test_util.a" 47 | UPDATE_COMMAND "") 48 | 49 | ExternalProject_Get_Property(ext_ngraph SOURCE_DIR) 50 | add_library(libngraph SHARED IMPORTED) 51 | add_dependencies(libngraph ext_ngraph) 52 | 53 | # Avoid non-existent path error 54 | file(MAKE_DIRECTORY ${NGRAPH_SRC_DIR}/src) 55 | file(MAKE_DIRECTORY ${NGRAPH_TEST_INCLUDE_DIR}) 56 | target_include_directories(libngraph INTERFACE ${NGRAPH_SRC_DIR}/src) 57 | set_target_properties(libngraph 58 | PROPERTIES IMPORTED_LOCATION 59 | ${NGRAPH_BUILD_DIR}/src/ngraph/libngraph.so) 60 | 61 | # ngraph test util 62 | add_library(libngraph_test_util STATIC IMPORTED) 63 | add_dependencies(libngraph_test_util ext_ngraph) 64 | set_target_properties( 65 | libngraph_test_util 66 | PROPERTIES IMPORTED_LOCATION 67 | ${NGRAPH_BUILD_DIR}/test/util/libngraph_test_util.a 68 | ) 69 | 70 | target_include_directories(libngraph_test_util 71 | INTERFACE ${NGRAPH_TEST_INCLUDE_DIR}/) 72 | -------------------------------------------------------------------------------- /cmake/openmp.cmake: -------------------------------------------------------------------------------- 1 | # ****************************************************************************** 2 | # Copyright 2018-2020 Intel Corporation 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | # use this file except in compliance with the License. You may obtain a copy of 6 | # the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | # License for the specific language governing permissions and limitations under 14 | # the License. 15 | # ****************************************************************************** 16 | 17 | include(FindOpenMP) 18 | 19 | if(OpenMP_CXX_FOUND) 20 | message(STATUS "OpenMP found") 21 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") 22 | else() 23 | message(STATUS "OpenMP not found") 24 | endif() 25 | -------------------------------------------------------------------------------- /cmake/protobuf.cmake: -------------------------------------------------------------------------------- 1 | # ****************************************************************************** 2 | # Copyright 2018-2020 Intel Corporation 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | # use this file except in compliance with the License. You may obtain a copy of 6 | # the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | # License for the specific language governing permissions and limitations under 14 | # the License. 15 | # ****************************************************************************** 16 | 17 | include(ExternalProject) 18 | 19 | set(NGRAPH_HE_PROTOBUF_GIT_REPO_URL 20 | "https://github.com/protocolbuffers/protobuf") 21 | set(NGRAPH_HE_PROTOBUF_GIT_TAG "v3.10.1") 22 | 23 | ExternalProject_Add( 24 | ext_protobuf 25 | PREFIX protobuf 26 | GIT_REPOSITORY ${NGRAPH_HE_PROTOBUF_GIT_REPO_URL} 27 | GIT_TAG ${NGRAPH_HE_PROTOBUF_GIT_TAG} 28 | UPDATE_COMMAND "" 29 | PATCH_COMMAND "" 30 | CONFIGURE_COMMAND ./autogen.sh 31 | COMMAND ./configure 32 | --prefix=${EXTERNAL_PROJECTS_ROOT}/protobuf 33 | --disable-shared 34 | CXX=${CMAKE_CXX_COMPILER} 35 | BUILD_COMMAND $(MAKE) "CXXFLAGS=-std=c++${NGRAPH_HE_CXX_STANDARD} -fPIC" 36 | TMP_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf/tmp" 37 | STAMP_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf/stamp" 38 | DOWNLOAD_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf/download" 39 | SOURCE_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf/src" 40 | BINARY_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf/src" 41 | INSTALL_DIR "${EXTERNAL_PROJECTS_ROOT}/protobuf" 42 | EXCLUDE_FROM_ALL TRUE) 43 | 44 | set(Protobuf_INSTALL_PREFIX ${EXTERNAL_PROJECTS_ROOT}/protobuf) 45 | set(Protobuf_PROTOC_EXECUTABLE ${Protobuf_INSTALL_PREFIX}/bin/protoc) 46 | set(Protobuf_INCLUDE_DIR ${Protobuf_INSTALL_PREFIX}/include) 47 | 48 | set(Protobuf_LIBRARY ${Protobuf_INSTALL_PREFIX}/lib/libprotobuf.a) 49 | set(Protobuf_LIBRARIES ${Protobuf_LIBRARY}) 50 | 51 | if(NOT TARGET protobuf::libprotobuf) 52 | add_library(protobuf::libprotobuf STATIC IMPORTED) 53 | if(NOT EXISTS ${Protobuf_INCLUDE_DIR}) 54 | file(MAKE_DIRECTORY ${Protobuf_INCLUDE_DIR}) 55 | endif() 56 | 57 | target_include_directories(protobuf::libprotobuf SYSTEM 58 | INTERFACE "${Protobuf_INCLUDE_DIR}") 59 | set_target_properties(protobuf::libprotobuf 60 | PROPERTIES IMPORTED_LOCATION "${Protobuf_LIBRARY}") 61 | add_dependencies(protobuf::libprotobuf ext_protobuf) 62 | endif() 63 | 64 | if(NOT TARGET protobuf::protoc) 65 | add_executable(protobuf::protoc IMPORTED) 66 | set_target_properties(protobuf::protoc 67 | PROPERTIES INTERFACE_SYSTEM_INCLUDE_DIRECTORIES 68 | "${Protobuf_PROTOC_EXECUTABLE}" 69 | IMPORTED_LOCATION 70 | "${Protobuf_PROTOC_EXECUTABLE}") 71 | add_dependencies(protobuf::protoc ext_protobuf) 72 | endif() 73 | 74 | set(Protobuf_FOUND TRUE) 75 | set(PROTOBUF_FOUND TRUE) 76 | -------------------------------------------------------------------------------- /cmake/sdl.cmake: -------------------------------------------------------------------------------- 1 | # ****************************************************************************** 2 | # Copyright 2018-2020 Intel Corporation 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | # use this file except in compliance with the License. You may obtain a copy of 6 | # the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | # License for the specific language governing permissions and limitations under 14 | # the License. 15 | # ****************************************************************************** 16 | 17 | # Manage secure Development Lifecycle-related compiler flags 18 | # ============================================================================== 19 | 20 | if(SDL_cmake_included) 21 | return() 22 | endif() 23 | set(SDL_cmake_included true) 24 | 25 | if(UNIX) 26 | set(SDL_CXX_FLAGS "-O2 -Wformat -Wformat-security") 27 | set(SDL_CXX_FLAGS "${SDL_CXX_FLAGS} -D_FORTIFY_SOURCE=2") 28 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_FORTIFY_SOURCE=2") 29 | if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") 30 | if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9) 31 | set(SDL_CXX_FLAGS "${SDL_CXX_FLAGS} -fstack-protector-all") 32 | else() 33 | set(SDL_CXX_FLAGS "${SDL_CXX_FLAGS} -fstack-protector-strong") 34 | endif() 35 | 36 | # GCC might be very paranoid for partial structure initialization, e.g. 37 | # struct { int a, b; } s = { 0, }; However the behavior is triggered by 38 | # `Wmissing-field-initializers` only. To prevent warnings on users' side who 39 | # use the library and turn this warning on, let's use it too. Applicable for 40 | # the library sources and interfaces only (tests currently rely on that fact 41 | # heavily) set(CMAKE_SRC_CCXX_FLAGS "${CMAKE_SRC_CCXX_FLAGS} -Wmissing- 42 | # field-initializers") set(CMAKE_EXAMPLE_CCXX_FLAGS 43 | # "${CMAKE_EXAMPLE_CCXX_FLAGS} -Wmissing-field-initializers") 44 | elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") 45 | set(SDL_CXX_FLAGS "${SDL_CXX_FLAGS} -fstack-protector-all") 46 | elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel") 47 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstack-protector") 48 | endif() 49 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SDL_CXX_FLAGS}") 50 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SDL_CXX_FLAGS}") 51 | if(APPLE) 52 | set(CMAKE_SHARED_LINKER_FLAGS 53 | "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-bind_at_load") 54 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-bind_at_load") 55 | else() 56 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie") 57 | set( 58 | CMAKE_SHARED_LINKER_FLAGS 59 | "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now") 60 | set(CMAKE_EXE_LINKER_FLAGS 61 | "${CMAKE_EXE_LINKER_FLAGS} -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now") 62 | endif() 63 | endif() 64 | -------------------------------------------------------------------------------- /cmake/seal.aby_patch: -------------------------------------------------------------------------------- 1 | diff --git a/native/src/seal/ckks.cpp b/native/src/seal/ckks.cpp 2 | index 62fd08e..8ef8b00 100644 3 | --- a/native/src/seal/ckks.cpp 4 | +++ b/native/src/seal/ckks.cpp 5 | @@ -108,7 +108,7 @@ namespace seal 6 | int coeff_bit_count = static_cast(log2(fabs(value))) + 2; 7 | if (coeff_bit_count >= context_data.total_coeff_modulus_bit_count()) 8 | { 9 | - throw invalid_argument("encoded value is too large"); 10 | + // throw invalid_argument("encoded value is too large"); 11 | } 12 | 13 | double two_pow_64 = pow(2.0, 64); 14 | @@ -237,7 +237,7 @@ namespace seal 15 | static_cast(llabs(value))) + 2; 16 | if (coeff_bit_count >= context_data.total_coeff_modulus_bit_count()) 17 | { 18 | - throw invalid_argument("encoded value is too large"); 19 | + // throw invalid_argument("encoded value is too large"); 20 | } 21 | 22 | // Resize destination to appropriate size 23 | diff --git a/native/src/seal/ckks.h b/native/src/seal/ckks.h 24 | index 73cea1f..8c4e402 100644 25 | --- a/native/src/seal/ckks.h 26 | +++ b/native/src/seal/ckks.h 27 | @@ -548,7 +548,7 @@ namespace seal 28 | } 29 | if (max_coeff_bit_count >= context_data.total_coeff_modulus_bit_count()) 30 | { 31 | - throw std::invalid_argument("encoded values are too large"); 32 | + // throw std::invalid_argument("encoded values are too large"); 33 | } 34 | 35 | double two_pow_64 = std::pow(2.0, 64); 36 | -------------------------------------------------------------------------------- /cmake/seal.patch: -------------------------------------------------------------------------------- 1 | diff --git a/native/src/CMakeLists.txt b/native/src/CMakeLists.txt 2 | index b5628f9..f8283ac 100644 3 | --- a/native/src/CMakeLists.txt 4 | +++ b/native/src/CMakeLists.txt 5 | @@ -31,7 +31,9 @@ else() 6 | endif() 7 | 8 | # Required files and directories 9 | -include(GNUInstallDirs) 10 | +if (NOT CMAKE_INSTALL_LIBDIR OR NOT CMAKE_INSTALL_INCLUDEDIR) 11 | + include(GNUInstallDirs) 12 | +endif() 13 | set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${SEAL_SOURCE_DIR}/../lib) 14 | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${SEAL_SOURCE_DIR}/../lib) 15 | set(CMAKE_LIBRARY_RUNTIME_DIRECTORY ${SEAL_SOURCE_DIR}/../bin) 16 | -------------------------------------------------------------------------------- /configs/he_seal_ckks_config_N11_L1.json: -------------------------------------------------------------------------------- 1 | { 2 | "scheme_name": "HE_SEAL", 3 | "poly_modulus_degree": 2048, 4 | "security_level": 128, 5 | "coeff_modulus": [ 6 | 54 7 | ], 8 | "complex_packing": true 9 | } 10 | -------------------------------------------------------------------------------- /configs/he_seal_ckks_config_N11_L1_small.json: -------------------------------------------------------------------------------- 1 | { 2 | "scheme_name": "HE_SEAL", 3 | "poly_modulus_degree": 2048, 4 | "security_level": 128, 5 | "coeff_modulus": [ 6 | 30 7 | ], 8 | "scale": 16777216, 9 | "complex_packing": false 10 | } 11 | -------------------------------------------------------------------------------- /configs/he_seal_ckks_config_N11_L2.json: -------------------------------------------------------------------------------- 1 | { 2 | "scheme_name": "HE_SEAL", 3 | "poly_modulus_degree": 2048, 4 | "security_level": 128, 5 | "coeff_modulus": [ 6 | 31, 7 | 23 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /configs/he_seal_ckks_config_N11_L4_debug.json: -------------------------------------------------------------------------------- 1 | { 2 | "scheme_name": "HE_SEAL", 3 | "poly_modulus_degree": 2048, 4 | "security_level": 0, 5 | "coeff_modulus": [ 6 | 30, 7 | 22, 8 | 22, 9 | 30 10 | ] 11 | } 12 | -------------------------------------------------------------------------------- /configs/he_seal_ckks_config_N12_L4.json: -------------------------------------------------------------------------------- 1 | { 2 | "scheme_name": "HE_SEAL", 3 | "poly_modulus_degree": 4096, 4 | "security_level": 128, 5 | "coeff_modulus": [ 6 | 30, 7 | 22, 8 | 22, 9 | 30 10 | ] 11 | } 12 | -------------------------------------------------------------------------------- /configs/he_seal_ckks_config_N12_L4_complex.json: -------------------------------------------------------------------------------- 1 | { 2 | "scheme_name": "HE_SEAL", 3 | "poly_modulus_degree": 4096, 4 | "security_level": 128, 5 | "coeff_modulus": [ 6 | 30, 7 | 22, 8 | 22, 9 | 30 10 | ], 11 | "complex_packing": true 12 | } 13 | -------------------------------------------------------------------------------- /configs/he_seal_ckks_config_N13_L10_debug.json: -------------------------------------------------------------------------------- 1 | { 2 | "scheme_name": "HE_SEAL", 3 | "poly_modulus_degree": 8192, 4 | "security_level": 0, 5 | "coeff_modulus": [ 6 | 30, 7 | 24, 8 | 24, 9 | 24, 10 | 24, 11 | 24, 12 | 24, 13 | 24, 14 | 24, 15 | 30 16 | ] 17 | } 18 | -------------------------------------------------------------------------------- /configs/he_seal_ckks_config_N13_L5_gc.json: -------------------------------------------------------------------------------- 1 | { 2 | "scheme_name": "HE_SEAL", 3 | "poly_modulus_degree": 8192, 4 | "security_level": 128, 5 | "coeff_modulus": [ 6 | 47, 24, 24, 24, 30 7 | ], 8 | "scale": 16777216, 9 | "complex_packing": true 10 | } 11 | -------------------------------------------------------------------------------- /configs/he_seal_ckks_config_N13_L7.json: -------------------------------------------------------------------------------- 1 | { 2 | "scheme_name": "HE_SEAL", 3 | "poly_modulus_degree": 8192, 4 | "security_level": 128, 5 | "coeff_modulus": [ 6 | 30, 7 | 24, 8 | 24, 9 | 24, 10 | 24, 11 | 24, 12 | 30 13 | ], 14 | "complex_packing": false, 15 | "scale": 16777216 16 | } 17 | -------------------------------------------------------------------------------- /configs/he_seal_ckks_config_N13_L7_lazy.json: -------------------------------------------------------------------------------- 1 | { 2 | "scheme_name": "HE_SEAL", 3 | "poly_modulus_degree": 8192, 4 | "security_level": 0, 5 | "coeff_modulus": [ 6 | 24, 7 | 22, 8 | 22, 9 | 22, 10 | 22, 11 | 22, 12 | 30 13 | ], 14 | "complex_packing": false, 15 | "scale": 4194304 16 | } 17 | -------------------------------------------------------------------------------- /configs/he_seal_ckks_config_N13_L8.json: -------------------------------------------------------------------------------- 1 | { 2 | "scheme_name": "HE_SEAL", 3 | "poly_modulus_degree": 8192, 4 | "security_level": 128, 5 | "coeff_modulus": [ 6 | 30, 7 | 24, 8 | 24, 9 | 24, 10 | 24, 11 | 24, 12 | 24, 13 | 30 14 | ], 15 | "complex_packing": false, 16 | "scale": 16777216 17 | } 18 | -------------------------------------------------------------------------------- /configs/he_seal_ckks_config_N13_L9_debug.json: -------------------------------------------------------------------------------- 1 | { 2 | "scheme_name": "HE_SEAL", 3 | "poly_modulus_degree": 8192, 4 | "security_level": 0, 5 | "coeff_modulus": [ 6 | 30, 7 | 24, 8 | 24, 9 | 24, 10 | 24, 11 | 24, 12 | 24, 13 | 24, 14 | 30 15 | ] 16 | } 17 | -------------------------------------------------------------------------------- /configs/he_seal_ckks_config_N14_L10.json: -------------------------------------------------------------------------------- 1 | { 2 | "scheme_name": "HE_SEAL", 3 | "poly_modulus_degree": 16384, 4 | "security_level": 128, 5 | "coeff_modulus": [ 6 | 30, 7 | 24, 8 | 24, 9 | 24, 10 | 24, 11 | 24, 12 | 24, 13 | 24, 14 | 24, 15 | 30 16 | ] 17 | } 18 | -------------------------------------------------------------------------------- /contrib/docker/Dockerfile.he_transformer.centos74: -------------------------------------------------------------------------------- 1 | # ****************************************************************************** 2 | # Copyright 2018-2020 Intel Corporation 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ****************************************************************************** 16 | 17 | # Environment to build and unit-test he-transformer on fedora27 18 | # with gcc 7.3.1 19 | # with cmake 3.13.5 20 | # with clang 5.0.1 21 | # with python version 3.6.8 22 | 23 | FROM centos:7 24 | 25 | RUN yum -y update && \ 26 | yum -y --enablerepo=extras install epel-release && \ 27 | yum -y install \ 28 | gcc gcc-c++ \ 29 | cmake3 make \ 30 | git \ 31 | wget patch diffutils zlib-devel ncurses-devel libtinfo-dev \ 32 | python python-devel python-setuptools \ 33 | python3 python36-devel python3-setuptools \ 34 | which unzip 35 | 36 | RUN yum -y install centos-release-scl && \ 37 | yum install -y devtoolset-7 llvm-toolset-7 38 | 39 | # For ngraph-tf integration testing 40 | RUN easy_install pip 41 | RUN pip install --upgrade pip setuptools virtualenv==16.0.0 42 | 43 | SHELL [ "/usr/bin/scl", "enable", "devtoolset-7", "llvm-toolset-7"] 44 | 45 | RUN ln -s /usr/bin/cmake3 /usr/bin/cmake 46 | 47 | RUN cmake --version 48 | RUN make --version 49 | RUN gcc --version 50 | RUN c++ --version 51 | RUN clang --version 52 | RUN clang++ --version 53 | RUN python3 --version 54 | RUN virtualenv --version 55 | 56 | # Get bazel 57 | RUN wget https://github.com/bazelbuild/bazel/releases/download/0.25.2/bazel-0.25.2-installer-linux-x86_64.sh 58 | RUN chmod +x ./bazel-0.25.2-installer-linux-x86_64.sh 59 | RUN bash ./bazel-0.25.2-installer-linux-x86_64.sh 60 | 61 | WORKDIR /home 62 | -------------------------------------------------------------------------------- /contrib/docker/Dockerfile.he_transformer.fedora28: -------------------------------------------------------------------------------- 1 | # ****************************************************************************** 2 | # Copyright 2018-2020 Intel Corporation 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ****************************************************************************** 16 | 17 | # Environment to build and unit-test he-transformer on fedora27 18 | # with g++ 8.3.1 19 | # with python 3.6.8 20 | # with cmake 3.14.4 21 | # with clang 6.0.1 22 | 23 | FROM fedora:28 24 | 25 | RUN dnf -y update && \ 26 | dnf -y install \ 27 | gcc gcc-c++ \ 28 | cmake3 make \ 29 | git python3 python3-pip \ 30 | python-setuptools \ 31 | wget unzip \ 32 | which redhat-rpm-config \ 33 | python3-devel \ 34 | patch 35 | 36 | # For ngraph-tf integration testing 37 | RUN pip install virtualenv 38 | RUN pip3 install --upgrade pip setuptools virtualenv==16.0.0 39 | RUN dnf install -y clang 40 | 41 | RUN cmake --version 42 | RUN make --version 43 | RUN gcc --version 44 | RUN c++ --version 45 | RUN clang --version 46 | RUN python3 --version 47 | RUN virtualenv --version 48 | 49 | # Get bazel 50 | RUN wget https://github.com/bazelbuild/bazel/releases/download/0.25.2/bazel-0.25.2-installer-linux-x86_64.sh 51 | RUN chmod +x ./bazel-0.25.2-installer-linux-x86_64.sh 52 | RUN bash ./bazel-0.25.2-installer-linux-x86_64.sh 53 | 54 | RUN pip install python-config 55 | WORKDIR /home 56 | -------------------------------------------------------------------------------- /contrib/docker/Dockerfile.he_transformer.ubuntu1604: -------------------------------------------------------------------------------- 1 | # ****************************************************************************** 2 | # Copyright 2018-2020 Intel Corporation 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ****************************************************************************** 16 | 17 | # Environment to build and unit-test he-transformer 18 | 19 | FROM ubuntu:16.04 20 | # with g++ 7.4.0 21 | # with clang++ 6.0.1 22 | # with python 3.5.2 23 | # with cmake 3.14.4 24 | 25 | RUN apt-get update && apt-get install -y \ 26 | python3-pip virtualenv \ 27 | python3-numpy python3-dev python3-wheel \ 28 | git \ 29 | unzip wget \ 30 | sudo \ 31 | bash-completion \ 32 | build-essential cmake \ 33 | software-properties-common \ 34 | git \ 35 | wget patch diffutils libtinfo-dev \ 36 | autoconf libtool \ 37 | doxygen graphviz \ 38 | yapf3 python3-yapf 39 | 40 | # Install clang-6 41 | RUN wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add - 42 | RUN apt-add-repository "deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial-6.0 main" 43 | RUN apt-get update 44 | RUN apt-get install -y clang-6.0 45 | 46 | RUN apt-get clean autoclean && \ 47 | apt-get autoremove -y 48 | 49 | # For ngraph-tf integration testing 50 | RUN pip3 install --upgrade pip setuptools virtualenv==16.1.0 51 | 52 | # SEAL requires newer version of CMake 53 | RUN pip3 install cmake --upgrade 54 | 55 | # Get g++-7 56 | RUN add-apt-repository ppa:ubuntu-toolchain-r/test \ 57 | && apt update \ 58 | && apt install g++-7 -y 59 | 60 | # Set g++-7 to default 61 | RUN update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 50 62 | RUN update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 50 63 | RUN update-alternatives --set g++ /usr/bin/g++-7 64 | RUN update-alternatives --set gcc /usr/bin/gcc-7 65 | 66 | RUN cmake --version 67 | RUN make --version 68 | RUN gcc --version 69 | RUN c++ --version 70 | RUN clang++-6.0 --version 71 | RUN python3 --version 72 | RUN virtualenv --version 73 | 74 | # Get bazel for ng-tf 75 | RUN wget https://github.com/bazelbuild/bazel/releases/download/0.25.2/bazel-0.25.2-installer-linux-x86_64.sh 76 | RUN chmod +x ./bazel-0.25.2-installer-linux-x86_64.sh 77 | RUN bash ./bazel-0.25.2-installer-linux-x86_64.sh 78 | WORKDIR /home 79 | -------------------------------------------------------------------------------- /contrib/docker/Dockerfile.he_transformer.ubuntu1804: -------------------------------------------------------------------------------- 1 | # ****************************************************************************** 2 | # Copyright 2018-2020 Intel Corporation 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ****************************************************************************** 16 | 17 | # Environment to build and unit-test he-transformer 18 | # with g++ 7.4.0 19 | # with clang++ 9.0.1 20 | # with python 3.6.8 21 | # with cmake 3.14.4 22 | 23 | FROM ubuntu:18.04 24 | 25 | # tzdata - do not run any interactive dialog 26 | ARG DEBIAN_FRONTEND=noninteractive 27 | 28 | RUN apt-get update && apt-get install -y \ 29 | python3-pip virtualenv \ 30 | python3-numpy python3-dev python3-wheel \ 31 | git \ 32 | unzip wget \ 33 | sudo \ 34 | bash-completion \ 35 | build-essential make cmake \ 36 | software-properties-common \ 37 | wget patch diffutils libtinfo-dev \ 38 | autoconf libtool \ 39 | doxygen graphviz \ 40 | yapf3 python3-yapf \ 41 | libmpfr-dev \ 42 | libgmp-dev \ 43 | libssl-dev 44 | 45 | # install gcc-8 46 | RUN apt-get update \ 47 | && apt-get install -y \ 48 | software-properties-common \ 49 | && add-apt-repository -y ppa:ubuntu-toolchain-r/test \ 50 | && apt-get update \ 51 | && apt-get install -y \ 52 | vim \ 53 | vim-gnome \ 54 | && apt-get install -y \ 55 | gcc-8 g++-8 gcc-8-base \ 56 | && update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 100 \ 57 | && update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-8 100 58 | 59 | # Install clang-9 60 | RUN wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add - 61 | RUN apt-add-repository "deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-9 main" 62 | RUN apt-get update && apt install -y clang-9 clang-tidy-9 clang-format-9 63 | 64 | RUN apt-get clean autoclean && \ 65 | apt-get autoremove -y 66 | 67 | # For ngraph-tf integration testing 68 | RUN pip3 install --upgrade pip setuptools virtualenv==16.1.0 69 | 70 | # SEAL requires newer version of CMake 71 | RUN pip3 install cmake --upgrade 72 | 73 | RUN cmake --version 74 | RUN make --version 75 | RUN gcc --version 76 | RUN clang++-9 --version 77 | RUN c++ --version 78 | RUN python3 --version 79 | RUN virtualenv --version 80 | 81 | # Get bazel for ng-tf 82 | RUN wget https://github.com/bazelbuild/bazel/releases/download/0.25.2/bazel-0.25.2-installer-linux-x86_64.sh 83 | RUN chmod +x ./bazel-0.25.2-installer-linux-x86_64.sh 84 | RUN bash ./bazel-0.25.2-installer-linux-x86_64.sh 85 | WORKDIR /home 86 | -------------------------------------------------------------------------------- /contrib/docker/docker_cleanup.sh: -------------------------------------------------------------------------------- 1 | 2 | #!/bin/bash 3 | 4 | # ****************************************************************************** 5 | # Copyright 2018-2020 Intel Corporation 6 | # 7 | # Licensed under the Apache License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | # ****************************************************************************** 19 | 20 | # list active docker containers 21 | echo "Active docker containers..." 22 | docker ps -a 23 | echo 24 | 25 | # clean up old docker containers 26 | echo "Removing Exited docker containers..." 27 | docker ps -a | grep Exited | cut -f 1 -d ' ' | xargs docker rm -f "${1}" 28 | echo 29 | 30 | #list docker images for he_transformer 31 | echo "Docker images for he_transformer..." 32 | docker images he_transformer 33 | echo 34 | 35 | # clean up docker images no longer in use 36 | echo "Removing docker images for he_transformer..." 37 | docker images -qa he_transformer* | xargs docker rmi -f "${1}" 38 | -------------------------------------------------------------------------------- /contrib/docker/make-dimage.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | ### 4 | # 5 | # Create a docker image that includes dependencies for building ngraph 6 | # 7 | # Uses CONTEXTDIR as the docker build context directory 8 | # Default value is '.' 9 | # 10 | # Uses ./Dockerfile.${DOCKER_TAG} 11 | # DOCKER_TAG is set to 'ngraph' if not set 12 | # 13 | # Sets the docker image name as ${DOCKER_IMAGE_NAME} 14 | # DOCKER_IMAGE_NAME is set to the ${DOCKER_TAG} if not set in the environment 15 | # The datestamp tag is automatically appended to the DOCKER_IMAGE_NAME to create the DIMAGE_ID 16 | # The ${DIMAGE_ID} docker image is created on the local server 17 | # The ${DOCKER_IMAGE_NAME}:latest tag is also created by default for reference 18 | # 19 | ### 20 | 21 | set -e 22 | #set -u 23 | set -o pipefail 24 | 25 | if [ -z $DOCKER_TAG ]; then 26 | DOCKER_TAG=build_ngraph 27 | fi 28 | 29 | if [ -z $DOCKER_IMAGE_NAME ]; then 30 | DOCKER_IMAGE_NAME=${DOCKER_TAG} 31 | fi 32 | 33 | echo "CONTEXTDIR=${CONTEXTDIR}" 34 | 35 | if [ -z ${CONTEXTDIR} ]; then 36 | CONTEXTDIR='.' # Docker image build context 37 | fi 38 | 39 | echo "CONTEXTDIR=${CONTEXTDIR}" 40 | 41 | if [ -n $DFILE ]; then 42 | DFILE="${CONTEXTDIR}/Dockerfile.${DOCKER_TAG}" 43 | fi 44 | 45 | CONTEXT='.' 46 | 47 | # A docker tag name must be valid ASCII and may contain lowercase and uppercase letters, 48 | # digits, underscores, periods and dashes. A tag name may not start with a period 49 | # or a dash and may contain a maximum of 128 characters. 50 | DIMAGE_NAME="${DOCKER_IMAGE_NAME}" 51 | DIMAGE_VERSION=`date -Iseconds | sed -e 's/:/-/g' | sed -e 's/+/-/g'` 52 | 53 | DIMAGE_ID="${DIMAGE_NAME}:${DIMAGE_VERSION}" 54 | 55 | # If proxy settings are detected in the environment, make sure they are 56 | # included on the docker-build command-line. This mirrors a similar system 57 | # in the Makefile. 58 | 59 | if [ ! -z "${http_proxy}" ] ; then 60 | DOCKER_HTTP_PROXY="--build-arg http_proxy=${http_proxy}" 61 | else 62 | DOCKER_HTTP_PROXY=' ' 63 | fi 64 | 65 | if [ ! -z "${https_proxy}" ] ; then 66 | DOCKER_HTTPS_PROXY="--build-arg https_proxy=${https_proxy}" 67 | else 68 | DOCKER_HTTPS_PROXY=' ' 69 | fi 70 | 71 | cd ${CONTEXTDIR} 72 | 73 | echo ' ' 74 | echo "Building docker image ${DIMAGE_ID} from Dockerfile ${DFILE}, context ${CONTEXT}" 75 | echo ' ' 76 | 77 | # build the docker base image 78 | docker build --rm=true \ 79 | ${DOCKER_HTTP_PROXY} ${DOCKER_HTTPS_PROXY} \ 80 | -f="${DFILE}" \ 81 | -t="${DIMAGE_ID}" \ 82 | ${CONTEXT} 83 | 84 | docker tag "${DIMAGE_ID}" "${DIMAGE_NAME}:latest" 85 | 86 | echo ' ' 87 | echo 'Docker image build completed' 88 | echo ' ' 89 | -------------------------------------------------------------------------------- /contrib/docker/make_docker_image.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | set -e 4 | # set -u # Cannot use set -u, as activate below relies on unbound variables 5 | set -o pipefail 6 | 7 | if [ -z $DOCKER_TAG ]; then 8 | DOCKER_TAG=he_transformer 9 | fi 10 | 11 | if [ -z $DOCKER_IMAGE_NAME ]; then 12 | DOCKER_IMAGE_NAME=${DOCKER_TAG} 13 | fi 14 | 15 | # Debugging to verify builds on CentOS 7.4 and Ubuntu 16.04 16 | if [ -f "/etc/centos-release" ]; then 17 | cat /etc/centos-release 18 | fi 19 | 20 | if [ -f "/etc/lsb-release" ]; then 21 | cat /etc/lsb-release 22 | fi 23 | 24 | uname -a 25 | cat /etc/os-release || true 26 | 27 | # If proxy settings are detected in the environment, make sure they are 28 | # included on the docker-build command-line. 29 | if [ ! -z "${http_proxy}" ] ; then 30 | DOCKER_HTTP_PROXY="--build-arg http_proxy=${http_proxy}" 31 | else 32 | DOCKER_HTTP_PROXY=' ' 33 | fi 34 | 35 | if [ ! -z "${https_proxy}" ] ; then 36 | DOCKER_HTTPS_PROXY="--build-arg https_proxy=${https_proxy}" 37 | else 38 | DOCKER_HTTPS_PROXY=' ' 39 | fi 40 | 41 | DFILE="Dockerfile.he-transformer" 42 | DIMAGE_NAME="${DOCKER_IMAGE_NAME}" 43 | DIMAGE_VERSION=`date -Iseconds | sed -e 's/:/-/g'` 44 | 45 | DIMAGE_ID="${DIMAGE_NAME}:${DIMAGE_VERSION}" 46 | 47 | echo "DOCKER_HTTP_PROXY ${DOCKER_HTTP_PROXY}" 48 | echo "DOCKER_HTTPS_PROXY ${DOCKER_HTTPS_PROXY}" 49 | 50 | # Set docker to docker directory 51 | CONTEXT='.' 52 | 53 | # build the docker base image 54 | docker build --rm=true \ 55 | ${GITHUB_USER} ${GITHUB_TOKEN} ${DOCKER_HTTP_PROXY} ${DOCKER_HTTPS_PROXY} \ 56 | -f="${DFILE}" \ 57 | -t="${DIMAGE_ID}" \ 58 | ${CONTEXT} 59 | 60 | docker tag "${DIMAGE_ID}" "${DIMAGE_NAME}:latest" 61 | 62 | echo 'Docker image build completed' 63 | -------------------------------------------------------------------------------- /doc/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # ****************************************************************************** 2 | # Copyright 2018-2020 Intel Corporation 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | # use this file except in compliance with the License. You may obtain a copy of 6 | # the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | # License for the specific language governing permissions and limitations under 14 | # the License. 15 | # ****************************************************************************** 16 | 17 | if(NGRAPH_HE_DOC_BUILD_ENABLE) 18 | add_custom_target(docs COMMENT "Build doxygen documentation.") 19 | add_subdirectory(doxygen) 20 | else() 21 | add_custom_target( 22 | docs 23 | COMMAND echo 24 | COMMAND 25 | echo 26 | "The 'docs' target is disabled. To enable the building of documentation, re-run cmake with the option -DNGRAPH_HE_DOC_BUILD_ENABLE=ON." 27 | COMMAND echo 28 | COMMAND false 29 | VERBATIM) 30 | endif() 31 | -------------------------------------------------------------------------------- /doc/README.md: -------------------------------------------------------------------------------- 1 | # Documentation: 2 | 3 | * Each class should contain: 4 | ```bash 5 | /// \brief Brief description 6 | ``` 7 | 8 | * Each member function and free function should contain: 9 | ```bash 10 | /// \brief Brief description 11 | /// \param[in] param_name Description of param 12 | /// \param[out] param_name Description of param 13 | /// \param[in,out] param_name Description of param 14 | /// \returns If not void, description of return value 15 | /// 16 | ``` 17 | -------------------------------------------------------------------------------- /doc/doxygen/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # ****************************************************************************** 2 | # Copyright 2018-2020 Intel Corporation 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | # use this file except in compliance with the License. You may obtain a copy of 6 | # the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | # License for the specific language governing permissions and limitations under 14 | # the License. 15 | # ****************************************************************************** 16 | 17 | find_package(Doxygen REQUIRED) 18 | 19 | if("${NGRAPH_HE_DOXYGEN_WARN_IF_UNDOCUMENTED}" MATCHES "^ON$") 20 | set(DOXYGEN_WARN_IF_UNDOCUMENTED YES) 21 | else() 22 | set(DOXYGEN_WARN_IF_UNDOCUMENTED NO) 23 | endif() 24 | 25 | if("${NGRAPH_HE_DOXYGEN_QUIET}" MATCHES "^ON$") 26 | set(DOXYGEN_QUIET YES) 27 | else() 28 | set(DOXYGEN_QUIET NO) 29 | endif() 30 | 31 | set(DOXYGEN_IN "${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in") 32 | set(DOXYGEN_OUT "${CMAKE_CURRENT_BINARY_DIR}/Doxyfile") 33 | configure_file("${DOXYGEN_IN}" "${DOXYGEN_OUT}" @ONLY) 34 | 35 | add_custom_target(doxygen-docs ALL 36 | COMMAND "${DOXYGEN_EXECUTABLE}" "${DOXYGEN_OUT}" 37 | WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" 38 | COMMENT "Generating documentation with Doxygen" 39 | VERBATIM) 40 | 41 | add_dependencies(docs doxygen-docs) 42 | 43 | install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/html/" 44 | DESTINATION "${NGRAPH_HE_INSTALL_DOC}/api-reference/html" 45 | OPTIONAL) 46 | 47 | install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/latex/" 48 | DESTINATION "${NGRAPH_HE_INSTALL_DOC}/api-reference/latex" 49 | OPTIONAL) 50 | -------------------------------------------------------------------------------- /doc/doxygen/Doxyfile.in: -------------------------------------------------------------------------------- 1 | PROJECT_NAME = "Intel® HE transformer for nGraph™" 2 | PROJECT_BRIEF = "Intel® HE transformer for nGraph™" 3 | 4 | OUTPUT_DIRECTORY = @CMAKE_CURRENT_BINARY_DIR@ 5 | INPUT = @CMAKE_SOURCE_DIR@/src 6 | RECURSIVE = YES 7 | EXCLUDE_PATTERNS = json.hpp 8 | 9 | USE_MATHJAX = YES 10 | 11 | GENERATE_XML = YES 12 | EXTRACT_ALL = YES 13 | SHOW_NAMESPACES = YES 14 | 15 | WARN_IF_UNDOCUMENTED = @DOXYGEN_WARN_IF_UNDOCUMENTED@ 16 | WARN_IF_DOC_ERROR = YES 17 | WARN_NO_PARAMDOC = YES 18 | 19 | QUIET = @DOXYGEN_QUIET@ 20 | -------------------------------------------------------------------------------- /examples/MNIST/Cryptonets-Relu/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel/he-transformer/6341ed8f04049c97a39a3d7f3f5684119095433e/examples/MNIST/Cryptonets-Relu/__init__.py -------------------------------------------------------------------------------- /examples/MNIST/Cryptonets/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel/he-transformer/6341ed8f04049c97a39a3d7f3f5684119095433e/examples/MNIST/Cryptonets/__init__.py -------------------------------------------------------------------------------- /examples/MNIST/MLP/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel/he-transformer/6341ed8f04049c97a39a3d7f3f5684119095433e/examples/MNIST/MLP/__init__.py -------------------------------------------------------------------------------- /examples/MNIST/MLP/model.py: -------------------------------------------------------------------------------- 1 | # ****************************************************************************** 2 | # Copyright 2018-2020 Intel Corporation 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ***************************************************************************** 16 | 17 | import numpy as np 18 | import tensorflow as tf 19 | from tensorflow.keras.layers import ( 20 | Dense, 21 | Conv2D, 22 | Activation, 23 | AveragePooling2D, 24 | Flatten, 25 | Convolution2D, 26 | MaxPooling2D, 27 | Reshape, 28 | ) 29 | 30 | 31 | def mnist_mlp_model(input): 32 | y = Conv2D( 33 | filters=5, 34 | kernel_size=(5, 5), 35 | strides=(2, 2), 36 | padding="same", 37 | use_bias=True, 38 | input_shape=(28, 28, 1), 39 | )(input) 40 | y = Activation("relu")(y) 41 | 42 | y = MaxPooling2D(pool_size=(3, 3))(y) 43 | 44 | y = Conv2D( 45 | filters=50, 46 | kernel_size=(5, 5), 47 | strides=(2, 2), 48 | padding="same", 49 | use_bias=True, 50 | )(y) 51 | y = Activation("relu")(y) 52 | 53 | known_shape = y.get_shape()[1:] 54 | size = np.prod(known_shape) 55 | print('size', size) 56 | 57 | # Using Keras model API with Flatten results in split ngraph at Flatten() or Reshape() op. 58 | # Use tf.reshape instead 59 | y = tf.reshape(y, [-1, size]) 60 | 61 | y = Dense(100, use_bias=True)(y) 62 | y = Activation("relu")(y) 63 | y = Dense(10, use_bias=True, name="output")(y) 64 | 65 | return y 66 | -------------------------------------------------------------------------------- /examples/MNIST/MLP/train.py: -------------------------------------------------------------------------------- 1 | # Copyright 2015 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """An MNIST classifier based on Cryptonets using convolutional layers. """ 16 | 17 | import sys 18 | import os 19 | import time 20 | import numpy as np 21 | import tensorflow as tf 22 | from tensorflow.keras.models import Model 23 | from tensorflow.keras.layers import Input 24 | from tensorflow.keras.optimizers import SGD, RMSprop, Adam, Nadam 25 | from tensorflow.keras.losses import categorical_crossentropy 26 | 27 | # Add parent directory to path 28 | sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 29 | import mnist_util 30 | import model 31 | 32 | 33 | def main(FLAGS): 34 | (x_train, y_train, x_test, y_test) = mnist_util.load_mnist_data() 35 | 36 | x = Input( 37 | shape=( 38 | 28, 39 | 28, 40 | 1, 41 | ), name="input") 42 | y = model.mnist_mlp_model(x) 43 | 44 | mlp_model = Model(inputs=x, outputs=y) 45 | print(mlp_model.summary()) 46 | 47 | def loss(labels, logits): 48 | return categorical_crossentropy(labels, logits, from_logits=True) 49 | 50 | optimizer = SGD(learning_rate=0.008, momentum=0.9) 51 | mlp_model.compile(optimizer=optimizer, loss=loss, metrics=["accuracy"]) 52 | 53 | mlp_model.fit( 54 | x_train, 55 | y_train, 56 | epochs=FLAGS.epochs, 57 | batch_size=FLAGS.batch_size, 58 | validation_data=(x_test, y_test), 59 | verbose=1) 60 | 61 | test_loss, test_acc = mlp_model.evaluate(x_test, y_test, verbose=1) 62 | print("\nTest accuracy:", test_acc) 63 | 64 | mnist_util.save_model( 65 | tf.compat.v1.keras.backend.get_session(), 66 | ["output/BiasAdd"], 67 | "./models", 68 | "mlp", 69 | ) 70 | 71 | 72 | if __name__ == "__main__": 73 | FLAGS, unparsed = mnist_util.train_argument_parser().parse_known_args() 74 | if unparsed: 75 | print("Unparsed flags: ", unparsed) 76 | exit(1) 77 | 78 | main(FLAGS) 79 | -------------------------------------------------------------------------------- /examples/MNIST/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel/he-transformer/6341ed8f04049c97a39a3d7f3f5684119095433e/examples/MNIST/__init__.py -------------------------------------------------------------------------------- /examples/MNIST/pyclient_mnist.py: -------------------------------------------------------------------------------- 1 | # ============================================================================== 2 | # Copyright 2018-2020 Intel Corporation 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ============================================================================== 16 | 17 | import time 18 | import argparse 19 | import numpy as np 20 | import sys 21 | import os 22 | 23 | from mnist_util import load_mnist_data, client_argument_parser 24 | import pyhe_client 25 | 26 | 27 | def test_network(FLAGS): 28 | (x_train, y_train, x_test, y_test) = load_mnist_data( 29 | FLAGS.start_batch, FLAGS.batch_size) 30 | data = x_test.flatten("C") 31 | 32 | client = pyhe_client.HESealClient( 33 | FLAGS.hostname, 34 | FLAGS.port, 35 | FLAGS.batch_size, 36 | {FLAGS.tensor_name: (FLAGS.encrypt_data_str, data)}, 37 | ) 38 | 39 | results = np.round(client.get_results(), 2) 40 | 41 | y_pred_reshape = np.array(results).reshape(FLAGS.batch_size, 10) 42 | with np.printoptions(precision=3, suppress=True): 43 | print(y_pred_reshape) 44 | 45 | y_pred = y_pred_reshape.argmax(axis=1) 46 | print("y_pred", y_pred) 47 | 48 | correct = np.sum(np.equal(y_pred, y_test.argmax(axis=1))) 49 | acc = correct / float(FLAGS.batch_size) 50 | print("correct", correct) 51 | print("Accuracy (batch size", FLAGS.batch_size, ") =", acc * 100.0, "%") 52 | 53 | 54 | if __name__ == "__main__": 55 | FLAGS, unparsed = client_argument_parser().parse_known_args() 56 | if unparsed: 57 | print("Unparsed flags:", unparsed) 58 | exit(1) 59 | 60 | test_network(FLAGS) 61 | -------------------------------------------------------------------------------- /examples/pyclient.py: -------------------------------------------------------------------------------- 1 | # ============================================================================== 2 | # Copyright 2018-2020 Intel Corporation 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ============================================================================== 16 | 17 | import pyhe_client 18 | import time 19 | import argparse 20 | 21 | 22 | def main(FLAGS): 23 | data = (2, 4, 6, 8) 24 | 25 | batch_size = 1 26 | 27 | client = pyhe_client.HESealClient(FLAGS.hostname, FLAGS.port, batch_size, { 28 | "client_parameter_name": ("encrypt", data) 29 | }) 30 | 31 | results = client.get_results() 32 | print("results", results) 33 | 34 | 35 | if __name__ == "__main__": 36 | parser = argparse.ArgumentParser() 37 | parser.add_argument( 38 | "--hostname", 39 | type=str, 40 | default="localhost", 41 | help="Hostname of server") 42 | parser.add_argument( 43 | "--port", 44 | type=int, 45 | default=34000, 46 | help="Port number of server", 47 | ) 48 | 49 | FLAGS, unparsed = parser.parse_known_args() 50 | 51 | print(FLAGS) 52 | main(FLAGS) 53 | -------------------------------------------------------------------------------- /images/nGraph_mask_1-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel/he-transformer/6341ed8f04049c97a39a3d7f3f5684119095433e/images/nGraph_mask_1-1.png -------------------------------------------------------------------------------- /maint/bash_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # ****************************************************************************** 4 | # Copyright 2018-2020 Intel Corporation 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # ****************************************************************************** 18 | 19 | #=================================================================================================== 20 | # A library of general-purpose Bash functions 21 | #=================================================================================================== 22 | 23 | declare _intelnervana_bash_lib_SCRIPT_NAME="${BASH_SOURCE[${#BASH_SOURCE[@]} - 1]}" 24 | declare _maint_SCRIPT_DIR="$( cd $(dirname "${_intelnervana_bash_lib_SCRIPT_NAME}") && pwd )" 25 | declare _intelnervana_bash_lib_IS_LOADED=1 26 | 27 | bash_lib_get_my_BASH_LINENO() { 28 | echo "${BASH_LINENO[${#BASH_LINENO[@]} -1 ]}" 29 | } 30 | 31 | bash_lib_get_callers_BASH_LINENO() { 32 | echo "${BASH_LINENO[${#BASH_LINENO[@]} - 2]}" 33 | } 34 | 35 | bash_lib_get_my_BASH_SOURCE() { 36 | echo "${BASH_SOURCE[${#BASH_SOURCE[@]} ]}" 37 | } 38 | 39 | bash_lib_get_callers_BASH_SOURCE() { 40 | echo "${BASH_SOURCE[${#BASH_SOURCE[@]} - 1]}" 41 | } 42 | 43 | bash_lib_status() { 44 | local CONTEXT_STRING="$(basename $(bash_lib_get_callers_BASH_SOURCE))" 45 | local TEXT_LINE 46 | local IS_FIRST_LINE=1 47 | 48 | for TEXT_LINE in "${@}"; do 49 | if (( IS_FIRST_LINE == 1 )); then 50 | IS_FIRST_LINE=0 51 | printf "%s STATUS: " "${CONTEXT_STRING}" >&2 52 | else 53 | printf " " >&2 54 | fi 55 | 56 | printf "%s\n" "${TEXT_LINE}" >&2 57 | done 58 | } 59 | 60 | bash_lib_print_error() { 61 | local CONTEXT_STRING="$(basename $(bash_lib_get_callers_BASH_SOURCE)):$(bash_lib_get_callers_BASH_LINENO)" 62 | local TEXT_LINE 63 | local IS_FIRST_LINE=1 64 | 65 | for TEXT_LINE in "${@}"; do 66 | if (( IS_FIRST_LINE == 1 )); then 67 | IS_FIRST_LINE=0 68 | printf "%s ERROR: " "${CONTEXT_STRING}" >&2 69 | else 70 | printf " " >&2 71 | fi 72 | 73 | printf "%s\n" "${TEXT_LINE}" >&2 74 | done 75 | } 76 | 77 | bash_lib_die() { 78 | bash_lib_print_error $@ 79 | exit 1 80 | } 81 | 82 | bash_lib_am_sudo_or_root() { 83 | [ "$EUID" -eq 0 ] 84 | } 85 | 86 | if bash_lib_am_sudo_or_root; then 87 | bash_lib_MAYBE_SUDO='' 88 | else 89 | bash_lib_MAYBE_SUDO='sudo --set-home' 90 | fi 91 | -------------------------------------------------------------------------------- /maint/clang_format_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # ****************************************************************************** 4 | # Copyright 2018-2020 Intel Corporation 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # ****************************************************************************** 18 | 19 | #=================================================================================================== 20 | # Provides Bash functions for dealing with clang-format. 21 | #=================================================================================================== 22 | 23 | declare _intelnervana_clang_format_lib_SCRIPT_NAME="${BASH_SOURCE[${#BASH_SOURCE[@]} - 1]}" 24 | declare _maint_SCRIPT_DIR="$( cd $(dirname "${_intelnervana_clang_format_lib_SCRIPT_NAME}") && pwd )" 25 | 26 | source "${_maint_SCRIPT_DIR}/bash_lib.sh" 27 | 28 | clang_format_lib_verify_version() { 29 | if (( $# != 2 )); then 30 | bash_lib_print_error "Usage: ${FUNCNAME[0]} " 31 | return 1 32 | fi 33 | 34 | local PROGNAME="${1}" 35 | local REQUIRED_VERSION_X_Y="${2}" 36 | 37 | if ! [[ "${REQUIRED_VERSION_X_Y}" =~ ^[0-9]+.[0-9]+$ ]]; then 38 | bash_lib_print_error "${FUNCNAME[0]}: required-version-number must have the form (number).(number)." 39 | return 1 40 | fi 41 | 42 | if ! [[ -f "${PROGNAME}" ]]; then 43 | bash_lib_print_error "Unable to find clang-format program named '${PROGNAME}'" 44 | return 1 45 | fi 46 | 47 | local VERSION_LINE 48 | if ! VERSION_LINE=$("${PROGNAME}" --version); then 49 | bash_lib_print_error "Failed invocation of command '${PROGNAME} --version'" 50 | return 1 51 | fi 52 | 53 | local SED_FLAGS 54 | if [[ "$(uname)" == 'Darwin' ]]; then 55 | SED_FLAGS='-En' 56 | else 57 | SED_FLAGS='-rn' 58 | fi 59 | 60 | local VERSION_X_Y 61 | if ! VERSION_X_Y=$(echo "${VERSION_LINE}" | sed ${SED_FLAGS} 's/^clang-format version ([0-9]+.[0-9]+).*$/\1/p') 62 | then 63 | bash_lib_print_error "Failed invocation of sed." 64 | return 1 65 | fi 66 | 67 | if [[ "${REQUIRED_VERSION_X_Y}" != "${VERSION_X_Y}" ]]; then 68 | bash_lib_print_error \ 69 | "Program '${PROGNAME}' reports version number '${VERSION_X_Y}'" \ 70 | "but we require '${REQUIRED_VERSION_X_Y}'" 71 | return 1 72 | fi 73 | } 74 | -------------------------------------------------------------------------------- /maint/yapf_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # ****************************************************************************** 4 | # Copyright 2018-2020 Intel Corporation 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # ****************************************************************************** 18 | 19 | #=================================================================================================== 20 | # Provides Bash functions for dealing with yapf. 21 | #=================================================================================================== 22 | 23 | declare _intelnervana_yapf_lib_SCRIPT_NAME="${BASH_SOURCE[${#BASH_SOURCE[@]} - 1]}" 24 | declare _maint_SCRIPT_DIR="$( cd $(dirname "${_intelnervana_yapf_lib_SCRIPT_NAME}") && pwd )" 25 | 26 | source "${_maint_SCRIPT_DIR}/bash_lib.sh" 27 | 28 | yapf_lib_verify_version() { 29 | if (( $# != 2 )); then 30 | bash_lib_print_error "Usage: ${FUNCNAME[0]} " 31 | return 1 32 | fi 33 | 34 | local PROGNAME="${1}" 35 | local REQUIRED_VERSION_X_Y_Z="${2}" 36 | 37 | if ! [[ "${REQUIRED_VERSION_X_Y_Z}" =~ ^[0-9]+.[0-9]+.[0-9]$ ]]; then 38 | bash_lib_print_error "${FUNCNAME[0]}: required-version-number must have the form (number).(number).(number)" 39 | return 1 40 | fi 41 | 42 | if ! [[ -f "${PROGNAME}" ]]; then 43 | bash_lib_print_error "Unable to find yapf program named '${PROGNAME}'" 44 | return 1 45 | fi 46 | 47 | local VERSION_LINE 48 | if ! VERSION_LINE=$("${PROGNAME}" --version); then 49 | bash_lib_print_error "Failed invocation of command '${PROGNAME} --version'" 50 | return 1 51 | fi 52 | 53 | local SED_FLAGS 54 | if [[ "$(uname)" == 'Darwin' ]]; then 55 | SED_FLAGS='-En' 56 | else 57 | SED_FLAGS='-rn' 58 | fi 59 | 60 | local VERSION_X_Y_Z 61 | if ! VERSION_X_Y_Z=$(echo "${VERSION_LINE}" | sed ${SED_FLAGS} 's/^yapf ([0-9]+.[0-9]+.[0-9]).*$/\1/p') 62 | then 63 | bash_lib_print_error "Failed invocation of sed." 64 | return 1 65 | fi 66 | 67 | if [[ "${REQUIRED_VERSION_X_Y_Z}" != "${VERSION_X_Y_Z}" ]]; then 68 | bash_lib_print_error \ 69 | "Program '${PROGNAME}' reports version number '${VERSION_X_Y_Z}'" \ 70 | "but we require '${REQUIRED_VERSION_X_Y_Z}'" 71 | return 1 72 | fi 73 | } 74 | -------------------------------------------------------------------------------- /python/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # ****************************************************************************** 2 | # Copyright 2017-2020 Intel Corporation 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | # use this file except in compliance with the License. You may obtain a copy of 6 | # the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | # License for the specific language governing permissions and limitations under 14 | # the License. 15 | # ****************************************************************************** 16 | 17 | ExternalProject_Add(pybind11 18 | GIT_REPOSITORY "https://github.com/pybind/pybind11.git" 19 | GIT_TAG master 20 | SOURCE_DIR "${CMAKE_BINARY_DIR}/pybind11" 21 | CONFIGURE_COMMAND "" 22 | BUILD_COMMAND "" 23 | INSTALL_COMMAND "" 24 | UPDATE_COMMAND "" 25 | ) 26 | 27 | set(BUILD_SH_IN "${CMAKE_SOURCE_DIR}/python/build_wheel.sh.in") 28 | set(BUILD_SH "${CMAKE_BINARY_DIR}/python/build_wheel.sh") 29 | configure_file(${BUILD_SH_IN} ${BUILD_SH} @ONLY) 30 | 31 | set(PYTHON_ARTIFACTS_DIR ${CMAKE_BINARY_DIR}/python/_install) 32 | set(PYTHON_LIB_DIR ${PYTHON_ARTIFACTS_DIR}/lib) 33 | set(PYTHON_INCLUDE_DIR ${PYTHON_ARTIFACTS_DIR}/include) 34 | 35 | # Hack to install files for python wheel When specifying DESTDIR in custom 36 | # command, this will change the destination to DESTDIR, rather than 37 | # EXTERNAL_INSTALL_INCLUDE_DIR 38 | install(DIRECTORY ${EXTERNAL_INSTALL_INCLUDE_DIR}/ 39 | DESTINATION ${EXTERNAL_INSTALL_INCLUDE_DIR} 40 | FILES_MATCHING 41 | PATTERN "*.hpp" 42 | PATTERN "*.h" 43 | PATTERN "*.inc" # For port_def.inc in protobuf 44 | ) 45 | install(DIRECTORY ${EXTERNAL_INSTALL_LIB_DIR}/ 46 | DESTINATION ${EXTERNAL_INSTALL_LIB_DIR} 47 | FILES_MATCHING 48 | PATTERN "*.so" 49 | PATTERN "*.a" 50 | ) 51 | 52 | add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/python/dist/ 53 | POST_BUILD 54 | DEPENDS he_seal_backend libngraph_tf 55 | COMMAND VERBOSE=1 56 | make 57 | -C 58 | ../ 59 | DESTDIR=python/_install 60 | install 61 | && 62 | bash 63 | build_wheel.sh) 64 | 65 | add_custom_target(python_client 66 | DEPENDS he_seal_backend ${CMAKE_BINARY_DIR}/python/dist/) 67 | -------------------------------------------------------------------------------- /python/build_wheel.sh.in: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # ****************************************************************************** 3 | # Copyright 2018-2020 Intel Corporation 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # ****************************************************************************** 17 | 18 | set -e 19 | 20 | export PYBIND_HEADERS_PATH=@CMAKE_BINARY_DIR@/pybind11 21 | export NGRAPH_HE_BUILD_PATH=@CMAKE_BINARY_DIR@/python/_install/@EXTERNAL_INSTALL_DIR@ 22 | export BOOST_HEADERS_PATH=@BOOST_HEADERS_PATH@ 23 | export SEAL_HEADERS_PATH=@SEAL_HEADERS_PATH@ 24 | export CXX_COMPILER=@CMAKE_CXX_COMPILER@ 25 | export PROJECT_ROOT_DIR=@PROJECT_ROOT_DIR@ 26 | 27 | SOURCE_DIR=@CMAKE_SOURCE_DIR@/python 28 | BUILD_DIR=@CMAKE_BINARY_DIR@/python 29 | 30 | ! PYTHON3_DETECTED=$(($(python3 -c 'import sys; print(sys.version_info.major)' 2> /dev/null) == 3)) 31 | 32 | mkdir -p build 33 | 34 | if [ "${PYTHON3_DETECTED}" == 1 ]; then 35 | echo "Building wheel for Python 3" 36 | python3 --version 37 | cd ${BUILD_DIR} 38 | virtualenv -p "$(command -v python3)" build/venv3 39 | source build/venv3/bin/activate 40 | pip install --upgrade setuptools pip wheel 41 | python ${SOURCE_DIR}/setup.py bdist_wheel 42 | python ${SOURCE_DIR}/setup.py sdist 43 | deactivate 44 | else 45 | echo "Error. Python3 not detected." 46 | fi 47 | -------------------------------------------------------------------------------- /python/pyhe_client/he_seal_client.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #include "pyhe_client/he_seal_client.hpp" 18 | 19 | #include 20 | #include 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | #include "seal/he_seal_client.hpp" 27 | 28 | namespace py = pybind11; 29 | 30 | void regclass_pyhe_client(py::module m) { 31 | py::class_ he_seal_client(m, 32 | "HESealClient"); 33 | he_seal_client.doc() = "he_seal_client wraps ngraph::he::HESealClient"; 34 | 35 | he_seal_client.def( 36 | py::init&>()); 38 | 39 | he_seal_client.def("set_seal_context", 40 | &ngraph::runtime::he::HESealClient::set_seal_context); 41 | he_seal_client.def("is_done", &ngraph::runtime::he::HESealClient::is_done); 42 | he_seal_client.def("get_results", 43 | &ngraph::runtime::he::HESealClient::get_results); 44 | he_seal_client.def("close_connection", 45 | &ngraph::runtime::he::HESealClient::close_connection); 46 | } 47 | -------------------------------------------------------------------------------- /python/pyhe_client/he_seal_client.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | 21 | namespace py = pybind11; 22 | 23 | void regclass_pyhe_client(py::module m); 24 | -------------------------------------------------------------------------------- /python/pyhe_client/pyhe_client.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #include 18 | #include 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include "he_seal_client.hpp" 26 | #include "pyhe_client/he_seal_client.hpp" 27 | #include "seal/he_seal_client.hpp" 28 | 29 | namespace py = pybind11; 30 | 31 | PYBIND11_MODULE(pyhe_client, m) { 32 | m.doc() = "Package which wraps he_seal_client"; 33 | regclass_pyhe_client(m); 34 | } 35 | -------------------------------------------------------------------------------- /src/aby/aby_client_executor.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | #include 21 | 22 | #include "aby/aby_executor.hpp" 23 | #include "he_tensor.hpp" 24 | #include "he_type.hpp" 25 | #include "logging/ngraph_he_log.hpp" 26 | #include "ngraph/except.hpp" 27 | #include "ngraph/util.hpp" 28 | #include "seal/he_seal_client.hpp" 29 | 30 | namespace ngraph::runtime::he { 31 | class HESealClient; 32 | } 33 | 34 | namespace ngraph::runtime::aby { 35 | 36 | class ABYClientExecutor : public ABYExecutor { 37 | public: 38 | ABYClientExecutor(std::string mpc_protocol, 39 | const he::HESealClient& he_seal_client, 40 | std::string hostname = std::string("localhost"), 41 | std::size_t port = 34001, uint64_t security_level = 128, 42 | uint32_t bit_length = 64, uint32_t num_threads = 1, 43 | uint32_t num_parties = 1, 44 | std::string mg_algo_str = std::string("MT_OT"), 45 | uint32_t reserve_num_gates = 100); 46 | 47 | ~ABYClientExecutor() = default; 48 | 49 | void run_aby_circuit(const std::string& function, 50 | std::shared_ptr& tensor) override; 51 | 52 | // Relu circuits 53 | void run_aby_relu_circuit(const std::string& function, 54 | std::shared_ptr& tensor); 55 | 56 | private: 57 | const he::HESealClient& m_he_seal_client; 58 | }; 59 | 60 | } // namespace ngraph::runtime::aby 61 | -------------------------------------------------------------------------------- /src/aby/aby_executor.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #include "aby/aby_executor.hpp" 18 | 19 | #include 20 | 21 | #include "seal/kernel/subtract_seal.hpp" 22 | #include "seal/seal_util.hpp" 23 | 24 | namespace ngraph::runtime::aby { 25 | 26 | ABYExecutor::ABYExecutor(const std::string& role, 27 | const std::string& mpc_protocol, 28 | const std::string& hostname, std::size_t port, 29 | uint64_t security_level, uint32_t bit_length, 30 | uint32_t num_threads, uint32_t num_parties, 31 | const std::string& mg_algo_str, 32 | uint32_t reserve_num_gates) 33 | : m_num_threads{num_threads}, 34 | m_num_parties{num_parties}, 35 | m_aby_bitlen{bit_length} { 36 | static std::map role_map{{"server", SERVER}, 37 | {"client", CLIENT}}; 38 | 39 | auto role_it = role_map.find(ngraph::to_lower(role)); 40 | NGRAPH_CHECK(role_it != role_map.end(), "Unknown role ", role); 41 | m_role = role_it->second; 42 | 43 | static std::map protocol_map{{"yao", S_YAO}, 44 | {"gmw", S_BOOL}}; 45 | 46 | auto protocol_it = protocol_map.find(ngraph::to_lower(mpc_protocol)); 47 | NGRAPH_CHECK(role_it != role_map.end(), "Unknown role ", role); 48 | NGRAPH_CHECK(protocol_it != protocol_map.end(), "Unknown mpc_protocol ", 49 | mpc_protocol); 50 | m_aby_gc_protocol = protocol_it->second; 51 | 52 | NGRAPH_CHECK(mg_algo_str == "MT_OT", "Unknown mg_algo_str ", mg_algo_str); 53 | m_mt_gen_alg = MT_OT; 54 | 55 | NGRAPH_CHECK(security_level == 128, "Unsupported security level ", 56 | security_level); 57 | m_security_level = security_level; 58 | 59 | m_ABYParties.resize(num_parties); 60 | for (size_t idx = 0; idx < num_parties; ++idx) { 61 | m_ABYParties[idx] = new ABYParty( 62 | m_role, hostname, port + idx, get_sec_lvl(m_security_level), bit_length, 63 | m_num_threads, m_mt_gen_alg, reserve_num_gates); 64 | } 65 | m_sharings.resize(num_parties); 66 | 67 | // TODO(fboemer): connect and base OTs? 68 | // m_ABYParty_server->ConnectAndBaseOTs(); 69 | NGRAPH_HE_LOG(1) << "Started ABYParty with role " << role << ", " 70 | << num_parties << " parties"; 71 | } 72 | 73 | // TODO(fboemer): delete ABYParty 74 | ABYExecutor::~ABYExecutor() = default; 75 | 76 | } // namespace ngraph::runtime::aby 77 | -------------------------------------------------------------------------------- /src/he_plaintext.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | #include 21 | 22 | #include "absl/container/inlined_vector.h" 23 | #include "ngraph/type/element_type.hpp" 24 | 25 | namespace ngraph::runtime::he { 26 | /// \brief Class representing a plaintext value 27 | 28 | class HEPlaintext : public absl::InlinedVector { 29 | public: 30 | HEPlaintext() = default; 31 | ~HEPlaintext() = default; 32 | HEPlaintext(const HEPlaintext& plain) = default; 33 | HEPlaintext(HEPlaintext&& plain) = default; 34 | 35 | HEPlaintext(std::initializer_list values) 36 | : absl::InlinedVector(values) {} 37 | 38 | explicit HEPlaintext(size_t n, double initial_value = 0) 39 | : absl::InlinedVector(n, initial_value) {} 40 | 41 | // TODO(fboemer): patch SEAL ckks_encoder to encode/decode on iterators 42 | std::vector as_double_vec() const { 43 | return std::vector(begin(), end()); 44 | } 45 | 46 | HEPlaintext& operator=(const HEPlaintext& v) = default; 47 | 48 | HEPlaintext& operator=(HEPlaintext&& v) = default; 49 | 50 | /// \brief Writes the plaintext to the target as a vector of type 51 | void write(void* target, const element::Type& element_type); 52 | 53 | /// \brief Reads plaintext to the target as a vector of type 54 | void read(void* source, size_t num_bytes, const element::Type& element_type); 55 | }; 56 | 57 | std::ostream& operator<<(std::ostream& os, const HEPlaintext& plain); 58 | } // namespace ngraph::runtime::he 59 | -------------------------------------------------------------------------------- /src/he_type.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #include "he_type.hpp" 18 | 19 | #include 20 | #include 21 | 22 | #include "he_plaintext.hpp" 23 | #include "ngraph/type/element_type.hpp" 24 | #include "protos/message.pb.h" 25 | #include "seal/he_seal_backend.hpp" 26 | #include "seal/seal_ciphertext_wrapper.hpp" 27 | 28 | namespace ngraph::runtime::he { 29 | 30 | HEType::HEType(const HEPlaintext& plain, bool complex_packing) 31 | : HEType(complex_packing, plain.size()) { 32 | m_is_plain = true; 33 | m_plain = plain; 34 | } 35 | 36 | HEType::HEType(const std::shared_ptr& cipher, 37 | bool complex_packing, size_t batch_size) 38 | : HEType(complex_packing, batch_size) { 39 | m_is_plain = false; 40 | m_cipher = cipher; 41 | } 42 | 43 | HEType HEType::load(const pb::HEType& pb_he_type, 44 | std::shared_ptr context) { 45 | if (pb_he_type.is_plaintext()) { 46 | // TODO(fboemer): HEPlaintext::load function 47 | HEPlaintext vals; 48 | for (const auto value : pb_he_type.plain()) { 49 | vals.emplace_back(static_cast(value)); 50 | } 51 | return HEType(vals, pb_he_type.complex_packing()); 52 | } 53 | 54 | auto cipher = HESealBackend::create_empty_ciphertext(); 55 | SealCiphertextWrapper::load(*cipher, pb_he_type, std::move(context)); 56 | return HEType(cipher, pb_he_type.complex_packing(), pb_he_type.batch_size()); 57 | } 58 | 59 | void HEType::save(pb::HEType& pb_he_type) const { 60 | pb_he_type.set_is_plaintext(is_plaintext()); 61 | pb_he_type.set_plaintext_packing(plaintext_packing()); 62 | pb_he_type.set_complex_packing(complex_packing()); 63 | pb_he_type.set_batch_size(batch_size()); 64 | 65 | if (is_plaintext()) { 66 | // TODO(fboemer): more efficient 67 | for (auto& elem : get_plaintext()) { 68 | pb_he_type.add_plain(static_cast(elem)); 69 | } 70 | } else { 71 | get_ciphertext()->save(pb_he_type); 72 | } 73 | } 74 | 75 | void HEType::set_plaintext(HEPlaintext plain) { 76 | m_plain = std::move(plain); 77 | m_is_plain = true; 78 | if (m_cipher != nullptr) { 79 | m_cipher->ciphertext().release(); 80 | } 81 | m_batch_size = m_plain.size(); 82 | } 83 | 84 | } // namespace ngraph::runtime::he 85 | -------------------------------------------------------------------------------- /src/he_type.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | 21 | #include "he_plaintext.hpp" 22 | #include "ngraph/type/element_type.hpp" 23 | #include "protos/message.pb.h" 24 | #include "seal/seal_ciphertext_wrapper.hpp" 25 | 26 | namespace ngraph::runtime::he { 27 | class HESealBackend; 28 | 29 | /// \brief Class representing an HE datatype, either a plaintext or a ciphertext 30 | class HEType { 31 | public: 32 | HEType() = delete; 33 | 34 | HEType(const HEPlaintext& plain, bool complex_packing); 35 | 36 | HEType(const std::shared_ptr& cipher, 37 | bool complex_packing, size_t batch_size); 38 | 39 | void save(pb::HEType& pb_he_type) const; 40 | 41 | static HEType load(const pb::HEType& pb_he_type, 42 | std::shared_ptr context); 43 | 44 | bool is_plaintext() const { return m_is_plain; } 45 | bool is_ciphertext() const { return !is_plaintext(); } 46 | 47 | bool complex_packing() const { return m_complex_packing; } 48 | bool& complex_packing() { return m_complex_packing; } 49 | 50 | bool plaintext_packing() const { return m_batch_size > 1; } 51 | 52 | size_t batch_size() const { return m_batch_size; } 53 | 54 | const HEPlaintext& get_plaintext() const { return m_plain; } 55 | HEPlaintext& get_plaintext() { return m_plain; } 56 | 57 | void set_plaintext(HEPlaintext plain); 58 | 59 | const std::shared_ptr& get_ciphertext() const { 60 | return m_cipher; 61 | } 62 | std::shared_ptr& get_ciphertext() { return m_cipher; } 63 | 64 | void set_ciphertext(const std::shared_ptr& cipher) { 65 | m_cipher = cipher; 66 | m_is_plain = false; 67 | m_plain.clear(); 68 | } 69 | 70 | private: 71 | /// \brief Constructs an empty HEType object 72 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) 73 | HEType(const bool complex_packing, const size_t batch_size) 74 | : m_complex_packing(complex_packing), m_batch_size(batch_size) {} 75 | 76 | bool m_is_plain; 77 | bool m_complex_packing; 78 | size_t m_batch_size; 79 | HEPlaintext m_plain; 80 | std::shared_ptr m_cipher; 81 | }; 82 | 83 | } // namespace ngraph::runtime::he 84 | -------------------------------------------------------------------------------- /src/logging/ngraph_he_log.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #include "logging/ngraph_he_log.hpp" 18 | 19 | #include 20 | #include 21 | 22 | #include "ngraph/log.hpp" 23 | 24 | namespace ngraph::runtime::he::logging { 25 | // Parse log level (int64) from environment variable (char*) 26 | int64_t log_level_str_to_int(const char* env_var_val) { 27 | if (env_var_val == nullptr) { 28 | return 0; 29 | } 30 | 31 | // Ideally we would use env_var / safe_strto64, but it is 32 | // hard to use here without pulling in a lot of dependencies, 33 | // so we use std:istringstream instead 34 | std::string min_log_level(env_var_val); 35 | std::istringstream ss(min_log_level); 36 | int64_t level; 37 | if (!(ss >> level)) { 38 | // Invalid vlog level setting, set level to default (0) 39 | level = 0; 40 | } 41 | 42 | return level; 43 | } 44 | 45 | int64_t min_ngraph_he_log_level() { 46 | const char* tf_env_var_val = std::getenv("NGRAPH_HE_LOG_LEVEL"); 47 | return log_level_str_to_int(tf_env_var_val); 48 | } 49 | } // namespace ngraph::runtime::he::logging 50 | -------------------------------------------------------------------------------- /src/logging/ngraph_he_log.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | #include 21 | 22 | #include "ngraph/log.hpp" 23 | 24 | namespace ngraph::runtime::he::logging { 25 | // Parse log level (int64) from environment variable (char*) 26 | int64_t log_level_str_to_int(const char* env_var_val); 27 | 28 | int64_t min_ngraph_he_log_level(); 29 | 30 | } // namespace ngraph::runtime::he::logging 31 | 32 | #define NGRAPH_HE_VLOG_IS_ON(lvl) \ 33 | ((lvl) <= ngraph::runtime::he::logging::min_ngraph_he_log_level()) 34 | 35 | #define NGRAPH_HE_LOG(lvl) \ 36 | if (NGRAPH_HE_VLOG_IS_ON(lvl)) NGRAPH_INFO 37 | // Comment to avoid backslash-newline at end of file warning 38 | -------------------------------------------------------------------------------- /src/op/bounded_relu.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #include "op/bounded_relu.hpp" 18 | 19 | #include "ngraph/util.hpp" 20 | 21 | namespace ngraph { 22 | 23 | constexpr NodeTypeInfo op::BoundedRelu::type_info; 24 | 25 | op::BoundedRelu::BoundedRelu(const Output& arg, float alpha) 26 | : UnaryElementwiseArithmetic(arg), m_alpha(alpha) { 27 | constructor_validate_and_infer_types(); 28 | set_output_type(0, arg.get_element_type(), arg.get_shape()); 29 | } 30 | 31 | std::shared_ptr op::BoundedRelu::copy_with_new_args( 32 | const NodeVector& new_args) const { 33 | if (new_args.size() != 1) { 34 | throw ngraph_error("Incorrect number of new arguments"); 35 | } 36 | return std::make_shared(new_args.at(0), m_alpha); 37 | } 38 | 39 | } // namespace ngraph 40 | -------------------------------------------------------------------------------- /src/op/bounded_relu.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | #include 21 | 22 | #include "ngraph/node.hpp" 23 | #include "ngraph/op/op.hpp" 24 | #include "ngraph/op/util/unary_elementwise_arithmetic.hpp" 25 | 26 | namespace ngraph::op { 27 | /// \brief Elementwise Minimum(Relu(arg, 0), alpha) operation. 28 | /// 29 | class BoundedRelu : public util::UnaryElementwiseArithmetic { 30 | public: 31 | static constexpr NodeTypeInfo type_info{"BoundedRelu", 0}; 32 | const NodeTypeInfo& get_type_info() const override { return type_info; } 33 | /// \brief Constructs a BoundedRelu operation. 34 | /// 35 | /// \param arg Node input to the Relu. 36 | BoundedRelu(const Output& arg, float alpha); 37 | float get_alpha() const { return m_alpha; } 38 | std::shared_ptr copy_with_new_args( 39 | const NodeVector& new_args) const override; 40 | 41 | private: 42 | float m_alpha; 43 | }; 44 | } // namespace ngraph::op 45 | -------------------------------------------------------------------------------- /src/pass/he_fusion.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include "ngraph/pass/graph_rewrite.hpp" 20 | 21 | namespace ngraph::runtime::he::pass { 22 | 23 | /// \brief performs HE-friendly fusion operations 24 | class HEFusion : public ngraph::pass::GraphRewrite { 25 | public: 26 | HEFusion() : GraphRewrite() { construct_bounded_relu(); } 27 | 28 | /// \brief Fuses Min(Relu, Constant) op into BoundedRelu(Constant) op 29 | void construct_bounded_relu(); 30 | }; 31 | } // namespace ngraph::runtime::he::pass 32 | -------------------------------------------------------------------------------- /src/pass/he_liveness.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | 21 | #include "ngraph/pass/graph_rewrite.hpp" 22 | 23 | namespace ngraph::runtime::he::pass { 24 | 25 | /// \brief An aggressive version of Liveness which will delete the parameter 26 | /// node and any constant nodes 27 | class HELiveness : public ngraph::pass::FunctionPass { 28 | public: 29 | /// \brief Performs HELiveness pass on given function 30 | /// \param[in,out] function Function to perform pass on 31 | /// \returns false, indicating the function has not been modified 32 | bool run_on_function(std::shared_ptr function) override; 33 | }; 34 | } // namespace ngraph::runtime::he::pass 35 | -------------------------------------------------------------------------------- /src/pass/propagate_he_annotations.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | #include 21 | #include 22 | 23 | #include "ngraph/pass/graph_rewrite.hpp" 24 | 25 | namespace ngraph::runtime::he::pass { 26 | /// \brief Propagates HE op annotations from parameters / constants to entire 27 | /// function 28 | class PropagateHEAnnotations : public ngraph::pass::FunctionPass { 29 | public: 30 | /// \brief Runs pass on function 31 | /// \param[in,out] function Function which to run pass on 32 | /// \returns whether or not the function has been modified 33 | bool run_on_function(std::shared_ptr function) override; 34 | }; 35 | } // namespace ngraph::runtime::he::pass 36 | -------------------------------------------------------------------------------- /src/pass/supported_ops.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #include "pass/supported_ops.hpp" 18 | 19 | #include 20 | 21 | #include "ngraph/check.hpp" 22 | #include "ngraph/function.hpp" 23 | #include "ngraph/graph_util.hpp" 24 | #include "ngraph/node.hpp" 25 | #include "ngraph/util.hpp" 26 | 27 | namespace ngraph::runtime::he { 28 | 29 | bool pass::SupportedOps::run_on_function(std::shared_ptr function) { 30 | std::list> ops = function->get_ordered_ops(); 31 | 32 | for (const auto& op : ops) { 33 | NGRAPH_CHECK(is_supported(*op), "Unsupported op ", op->description(), 34 | " with type ", op->get_element_type()); 35 | } 36 | return true; 37 | } 38 | 39 | } // namespace ngraph::runtime::he 40 | -------------------------------------------------------------------------------- /src/pass/supported_ops.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | #include 21 | 22 | #include "ngraph/pass/graph_rewrite.hpp" 23 | 24 | namespace ngraph::runtime::he::pass { 25 | /// \brief Checks whether the graph contains any ops that are not supported 26 | class SupportedOps : public ngraph::pass::FunctionPass { 27 | public: 28 | /// \param[in] is_supported Function which returns whether or not a given 29 | /// Node is supported 30 | explicit SupportedOps(std::function is_supported) 31 | : m_is_supported(is_supported) {} 32 | 33 | /// \brief Returns false, indicating the function has not been modified 34 | /// \throws ngraph_error is function is not supported 35 | /// \param[in,out] function Function which to run pass on 36 | bool run_on_function(std::shared_ptr function) override; 37 | 38 | /// \brief returns whether or not given node is supported 39 | /// \param[in] node Node which to check supported status 40 | /// \return true if node is supported, false otherwise 41 | bool is_supported(const Node& node) const { return m_is_supported(node); } 42 | 43 | private: 44 | std::function m_is_supported; 45 | }; 46 | } // namespace ngraph::runtime::he::pass 47 | -------------------------------------------------------------------------------- /src/protos/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # ****************************************************************************** 2 | # Copyright 2018-2020 Intel Corporation 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | # use this file except in compliance with the License. You may obtain a copy of 6 | # the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | # WARRANTNNPS OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | # License for the specific language governing permissions and limitations under 14 | # the License. 15 | # ****************************************************************************** 16 | 17 | if(NOT Protobuf_FOUND) 18 | message(FATAL_ERROR "Cannot find Protobuf") 19 | endif() 20 | 21 | # Generate protobuf headers for message 22 | get_filename_component(message_pb ${CMAKE_CURRENT_SOURCE_DIR}/message.proto 23 | ABSOLUTE) 24 | get_filename_component(message_pb_path "${message_pb}" PATH) 25 | set(message_pb_srcs ${CMAKE_CURRENT_BINARY_DIR}/message.pb.cc PARENT_SCOPE) 26 | set(message_pb_hdrs ${CMAKE_CURRENT_BINARY_DIR}/message.pb.h) 27 | 28 | add_custom_command(OUTPUT ${message_pb_srcs} ${message_pb_hdrs} 29 | COMMAND ${Protobuf_PROTOC_EXECUTABLE} 30 | ARGS 31 | --cpp_out 32 | ${CMAKE_CURRENT_BINARY_DIR} 33 | -I 34 | ${message_pb_path} 35 | ${message_pb} 36 | DEPENDS ${message_pb} protobuf::protoc 37 | protobuf::libprotobuf) 38 | 39 | add_custom_target(generated_pb_files_target 40 | DEPENDS ${message_pb_srcs} ${message_pb_hdrs}) 41 | 42 | # Create interface library; link against it to include the generated protobuf 43 | # files 44 | add_library(generated_pb_files INTERFACE) 45 | target_link_libraries(generated_pb_files) 46 | add_dependencies(generated_pb_files generated_pb_files_target) 47 | 48 | # Use include protos/*.pb.h 49 | target_include_directories(generated_pb_files 50 | INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/../) 51 | 52 | # Install generated protobuf files 53 | foreach(INSTALL_INCLUDE_DIR ${INSTALL_INCLUDE_DIRS}) 54 | install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/../ 55 | DESTINATION ${INSTALL_INCLUDE_DIR} 56 | FILES_MATCHING 57 | PATTERN "*.hpp" 58 | PATTERN "*.h") 59 | endforeach() 60 | 61 | # Create dummy file with almost no clang-tidy checks. This prevents the 62 | # generated protobuf files from being tidied. See https:// 63 | # gitlab.kitware.com/cmake/cmake/commit/b13bc8659f87567b1b091806d42f5023b2a6b48b 64 | file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/.clang-tidy" " 65 | --- 66 | Checks: '-*,llvm-twine-local' 67 | ... 68 | ") 69 | -------------------------------------------------------------------------------- /src/protos/message.proto: -------------------------------------------------------------------------------- 1 | 2 | syntax = "proto3"; 3 | 4 | package ngraph.runtime.he.pb; 5 | 6 | /// \brief Represents a message between the server and client. 7 | message TCPMessage { 8 | enum Type { 9 | UNKNOWN = 0; 10 | REQUEST = 1; 11 | RESPONSE = 2; 12 | } 13 | Type type = 1; 14 | Function function = 2; 15 | EncryptionParameters encryption_parameters = 3; 16 | EvaluationKey eval_key = 4; 17 | PublicKey public_key = 5; 18 | repeated HETensor he_tensors = 6; 19 | } 20 | 21 | message EncryptionParameters { 22 | bytes encryption_parameters = 1; 23 | } 24 | 25 | message EvaluationKey { 26 | bytes eval_key = 1; 27 | } 28 | 29 | message PublicKey { 30 | bytes public_key = 1; 31 | } 32 | 33 | message Function { 34 | string function = 1; 35 | } 36 | 37 | message HETensor { 38 | string name = 1; 39 | repeated uint64 shape = 2; 40 | enum ElementType { 41 | UNDEFINED = 0; 42 | DYNAMIC = 1; 43 | BOOLEAN = 2; 44 | BF16 = 3; 45 | F16 = 4; 46 | F32 = 5; 47 | F64 = 6; 48 | I8 = 7; 49 | I16 = 8; 50 | I32 = 9; 51 | I64 = 10; 52 | U1 = 11; 53 | U8 = 12; 54 | U16 = 13; 55 | U32 = 14; 56 | U64 = 15; 57 | } 58 | ElementType type = 3; 59 | bool packed = 4; 60 | uint64 offset = 5; 61 | repeated HEType data = 6; 62 | } 63 | 64 | message HEType { 65 | bool is_plaintext = 1; 66 | bool plaintext_packing = 2; 67 | bool complex_packing = 3; 68 | uint64 batch_size = 4; 69 | repeated float plain = 5; 70 | bytes ciphertext = 6; 71 | } 72 | -------------------------------------------------------------------------------- /src/seal/kernel/bounded_relu_seal.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | #include 21 | 22 | #include "he_type.hpp" 23 | #include "ngraph/type/element_type.hpp" 24 | #include "seal/he_seal_backend.hpp" 25 | #include "seal/seal_ciphertext_wrapper.hpp" 26 | #include "seal/seal_util.hpp" 27 | 28 | namespace ngraph::runtime::he { 29 | void scalar_bounded_relu_seal(const HEPlaintext& arg, HEPlaintext& out, 30 | float alpha); 31 | 32 | void scalar_bounded_relu_seal(const HEType& arg, HEType& out, float alpha, 33 | const seal::parms_id_type& parms_id, double scale, 34 | seal::CKKSEncoder& ckks_encoder, 35 | seal::Encryptor& encryptor, 36 | seal::Decryptor& decryptor, 37 | std::shared_ptr context); 38 | 39 | void scalar_bounded_relu_seal(const HEType& arg, HEType& out, float alpha, 40 | const HESealBackend& he_seal_backend); 41 | 42 | void bounded_relu_seal(const std::vector& arg, std::vector& out, 43 | float alpha, size_t count, 44 | const HESealBackend& he_seal_backend); 45 | 46 | } // namespace ngraph::runtime::he 47 | -------------------------------------------------------------------------------- /src/seal/kernel/broadcast_seal.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | #include 21 | 22 | #include "he_type.hpp" 23 | #include "ngraph/coordinate_transform.hpp" 24 | #include "ngraph/shape_util.hpp" 25 | 26 | namespace ngraph::runtime::he { 27 | inline void broadcast_seal(const std::vector& arg, 28 | std::vector& out, const Shape& in_shape, 29 | const Shape& out_shape, 30 | const AxisSet& broadcast_axes) { 31 | CoordinateTransform input_transform(in_shape); 32 | CoordinateTransform output_transform(out_shape); 33 | for (const Coordinate& output_coord : output_transform) { 34 | Coordinate input_coord = reduce(output_coord, broadcast_axes); 35 | 36 | out[output_transform.index(output_coord)] = 37 | arg[input_transform.index(input_coord)]; 38 | } 39 | } 40 | } // namespace ngraph::runtime::he 41 | -------------------------------------------------------------------------------- /src/seal/kernel/concat_seal.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | 21 | #include "he_type.hpp" 22 | #include "ngraph/coordinate_transform.hpp" 23 | #include "ngraph/shape_util.hpp" 24 | 25 | namespace ngraph::runtime::he { 26 | inline void concat_seal(const std::vector>& args, 27 | std::vector& out, 28 | const std::vector& in_shapes, 29 | const Shape& out_shape, size_t concatenation_axis) { 30 | // We will copy the inputs to the output one at a time. As we go, we will move 31 | // out along the concatenation axis, starting at 0. 32 | 33 | size_t concatenation_pos = 0; 34 | 35 | for (size_t i = 0; i < args.size(); i++) { 36 | // CoordinateTransform gets confused when the last input has a zero-size 37 | // dim, so we will just skip for zero-element tensors. 38 | if (shape_size(in_shapes[i]) == 0) { 39 | continue; 40 | } 41 | 42 | // The start coordinate for the copy is (0,...,0) except at the 43 | // concatenation axis. 44 | Coordinate out_start_coord(out_shape.size(), 0); 45 | out_start_coord[concatenation_axis] = concatenation_pos; 46 | 47 | // The end coordinate for the copy is the same as the output shape except at 48 | // the concatenation axis. 49 | Coordinate out_end_coord = out_shape; 50 | out_end_coord[concatenation_axis] = 51 | concatenation_pos + in_shapes[i][concatenation_axis]; 52 | 53 | CoordinateTransform input_transform(in_shapes[i]); 54 | CoordinateTransform output_chunk_transform(out_shape, out_start_coord, 55 | out_end_coord); 56 | 57 | NGRAPH_CHECK(shape_size(input_transform.get_target_shape()) == 58 | shape_size(output_chunk_transform.get_target_shape())); 59 | 60 | CoordinateTransform::Iterator output_chunk_it = 61 | output_chunk_transform.begin(); 62 | 63 | for (const Coordinate& input_coord : input_transform) { 64 | size_t input_index = input_transform.index(input_coord); 65 | size_t output_chunk_index = 66 | output_chunk_transform.index(*output_chunk_it); 67 | ++output_chunk_it; 68 | 69 | out[output_chunk_index] = args[i][input_index]; 70 | } 71 | 72 | concatenation_pos += in_shapes[i][concatenation_axis]; 73 | } 74 | } 75 | 76 | } // namespace ngraph::runtime::he 77 | -------------------------------------------------------------------------------- /src/seal/kernel/constant_seal.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #include "seal/kernel/constant_seal.hpp" 18 | 19 | #include 20 | #include 21 | 22 | #include "he_util.hpp" 23 | #include "seal/seal_util.hpp" 24 | 25 | namespace ngraph::runtime::he { 26 | 27 | void constant_seal(std::vector& out, const element::Type& element_type, 28 | const void* data_ptr, const HESealBackend& he_seal_backend, 29 | size_t count) { 30 | NGRAPH_CHECK(he_seal_backend.is_supported_type(element_type), 31 | "Unsupported type ", element_type); 32 | size_t type_byte_size = element_type.size(); 33 | NGRAPH_CHECK(out.size() == count, "out.size() != count for constant op"); 34 | 35 | #pragma omp parallel for 36 | for (size_t i = 0; i < count; ++i) { 37 | const void* src = static_cast(data_ptr) + i * type_byte_size; 38 | auto plaintext = HEPlaintext( 39 | std::initializer_list{type_to_double(src, element_type)}); 40 | NGRAPH_CHECK(out[i].is_plaintext(), "Don't support encrypted constants"); 41 | out[i].set_plaintext(plaintext); 42 | } 43 | } 44 | 45 | } // namespace ngraph::runtime::he 46 | -------------------------------------------------------------------------------- /src/seal/kernel/constant_seal.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | 21 | #include "he_type.hpp" 22 | #include "ngraph/type/element_type.hpp" 23 | #include "seal/he_seal_backend.hpp" 24 | 25 | namespace ngraph::runtime::he { 26 | void constant_seal(std::vector& out, const element::Type& element_type, 27 | const void* data_ptr, const HESealBackend& he_seal_backend, 28 | size_t count); 29 | 30 | } // namespace ngraph::runtime::he 31 | -------------------------------------------------------------------------------- /src/seal/kernel/convolution_seal.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | #include 21 | 22 | #include "logging/ngraph_he_log.hpp" 23 | #include "ngraph/coordinate_transform.hpp" 24 | #include "ngraph/type/element_type.hpp" 25 | #include "seal/he_seal_backend.hpp" 26 | #include "seal/kernel/add_seal.hpp" 27 | #include "seal/kernel/multiply_seal.hpp" 28 | #include "seal/seal_ciphertext_wrapper.hpp" 29 | 30 | namespace ngraph::runtime::he { 31 | 32 | void convolution_seal( 33 | const std::vector& arg0, const std::vector& arg1, 34 | std::vector& out, const Shape& arg0_shape, const Shape& arg1_shape, 35 | const Shape& out_shape, const Strides& window_movement_strides, 36 | const Strides& window_dilation_strides, const CoordinateDiff& padding_below, 37 | const CoordinateDiff& padding_above, const Strides& data_dilation_strides, 38 | size_t batch_axis_data, size_t input_channel_axis_data, 39 | size_t input_channel_axis_filters, size_t output_channel_axis_filters, 40 | size_t batch_axis_result, size_t output_channel_axis_result, 41 | const element::Type& element_type, size_t batch_size, 42 | HESealBackend& he_seal_backend, bool verbose = true); 43 | 44 | } // namespace ngraph::runtime::he 45 | -------------------------------------------------------------------------------- /src/seal/kernel/divide_seal.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | #include 21 | 22 | #include "he_type.hpp" 23 | #include "ngraph/type/element_type.hpp" 24 | #include "seal/he_seal_backend.hpp" 25 | #include "seal/seal_ciphertext_wrapper.hpp" 26 | 27 | namespace ngraph::runtime::he { 28 | void scalar_divide_seal(const HEPlaintext& arg0, const HEPlaintext& arg1, 29 | HEPlaintext& out); 30 | 31 | void scalar_divide_seal(const HEType& arg0, const HEType& arg1, HEType& out, 32 | const seal::parms_id_type& parms_id, double scale, 33 | seal::CKKSEncoder& ckks_encoder, 34 | seal::Encryptor& encryptor, seal::Decryptor& decryptor); 35 | 36 | void scalar_divide_seal(HEType& arg0, HEType& arg1, HEType& out, 37 | HESealBackend& he_seal_backend); 38 | 39 | void divide_seal(std::vector& arg0, std::vector& arg1, 40 | std::vector& out, size_t count, 41 | const element::Type& element_type, 42 | HESealBackend& he_seal_backend); 43 | 44 | } // namespace ngraph::runtime::he 45 | -------------------------------------------------------------------------------- /src/seal/kernel/dot_seal.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | 21 | #include "he_plaintext.hpp" 22 | #include "he_type.hpp" 23 | #include "ngraph/coordinate_transform.hpp" 24 | #include "seal/he_seal_backend.hpp" 25 | 26 | namespace ngraph::runtime::he { 27 | void dot_seal(const std::vector& arg0, const std::vector& arg1, 28 | std::vector& out, const Shape& arg0_shape, 29 | const Shape& arg1_shape, const Shape& out_shape, 30 | size_t reduction_axes_count, const element::Type& element_type, 31 | size_t batch_size, HESealBackend& he_seal_backend); 32 | 33 | } // namespace ngraph::runtime::he 34 | -------------------------------------------------------------------------------- /src/seal/kernel/exp_seal.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #include "seal/kernel/exp_seal.hpp" 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include "ngraph/type/element_type.hpp" 25 | #include "seal/he_seal_backend.hpp" 26 | #include "seal/seal_ciphertext_wrapper.hpp" 27 | #include "seal/seal_util.hpp" 28 | 29 | namespace ngraph::runtime::he { 30 | 31 | void scalar_exp_seal(const HEPlaintext& arg, HEPlaintext& out) { 32 | HEPlaintext out_vals(arg.size()); 33 | auto exp = [](double d) { return std::exp(d); }; 34 | std::transform(arg.begin(), arg.end(), out_vals.begin(), exp); 35 | out = std::move(out_vals); 36 | } 37 | 38 | void scalar_exp_seal(const HEType& arg, HEType& out, 39 | const seal::parms_id_type& parms_id, double scale, 40 | seal::CKKSEncoder& ckks_encoder, 41 | seal::Encryptor& encryptor, seal::Decryptor& decryptor, 42 | std::shared_ptr context) { 43 | if (arg.is_plaintext()) { 44 | out.set_plaintext(arg.get_plaintext()); 45 | scalar_exp_seal(arg.get_plaintext(), out.get_plaintext()); 46 | } else { 47 | HEPlaintext plain; 48 | decrypt(plain, *arg.get_ciphertext(), arg.complex_packing(), decryptor, 49 | ckks_encoder, context, arg.batch_size()); 50 | scalar_exp_seal(plain, plain); 51 | encrypt(out.get_ciphertext(), plain, parms_id, element::f32, scale, 52 | ckks_encoder, encryptor, arg.complex_packing()); 53 | } 54 | } 55 | 56 | void scalar_exp_seal(const HEType& arg, HEType& out, 57 | const HESealBackend& he_seal_backend) { 58 | scalar_exp_seal( 59 | arg, out, he_seal_backend.get_context()->first_parms_id(), 60 | he_seal_backend.get_scale(), *he_seal_backend.get_ckks_encoder(), 61 | *he_seal_backend.get_encryptor(), *he_seal_backend.get_decryptor(), 62 | he_seal_backend.get_context()); 63 | } 64 | 65 | void exp_seal(const std::vector& arg, std::vector& out, 66 | size_t count, const HESealBackend& he_seal_backend) { 67 | #pragma omp parallel for 68 | for (size_t i = 0; i < count; ++i) { 69 | scalar_exp_seal(arg[i], out[i], he_seal_backend); 70 | } 71 | } 72 | 73 | } // namespace ngraph::runtime::he 74 | -------------------------------------------------------------------------------- /src/seal/kernel/exp_seal.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | #include 21 | 22 | #include "he_type.hpp" 23 | #include "ngraph/type/element_type.hpp" 24 | #include "seal/he_seal_backend.hpp" 25 | #include "seal/seal_ciphertext_wrapper.hpp" 26 | #include "seal/seal_util.hpp" 27 | 28 | namespace ngraph::runtime::he { 29 | void scalar_exp_seal(const HEPlaintext& arg, HEPlaintext& out); 30 | 31 | void scalar_exp_seal(const HEType& arg, HEType& out, 32 | const seal::parms_id_type& parms_id, double scale, 33 | seal::CKKSEncoder& ckks_encoder, 34 | seal::Encryptor& encryptor, seal::Decryptor& decryptor, 35 | std::shared_ptr context); 36 | 37 | void scalar_exp_seal(const HEType& arg, HEType& out, 38 | const HESealBackend& he_seal_backend); 39 | 40 | void exp_seal(const std::vector& arg, std::vector& out, 41 | size_t count, const HESealBackend& he_seal_backend); 42 | 43 | } // namespace ngraph::runtime::he 44 | -------------------------------------------------------------------------------- /src/seal/kernel/minimum_seal.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | #include 21 | 22 | #include "he_plaintext.hpp" 23 | #include "he_type.hpp" 24 | #include "ngraph/type/element_type.hpp" 25 | #include "seal/he_seal_backend.hpp" 26 | #include "seal/seal_ciphertext_wrapper.hpp" 27 | 28 | namespace ngraph::runtime::he { 29 | 30 | void scalar_minimum_seal(const HEPlaintext& arg0, const HEPlaintext& arg1, 31 | HEPlaintext& out); 32 | 33 | void scalar_minimum_seal(HEType& arg0, HEType& arg1, HEType& out, 34 | HESealBackend& he_seal_backend); 35 | 36 | void minimum_seal(const std::vector& arg0, 37 | const std::vector& arg1, std::vector& out, 38 | size_t count, HESealBackend& he_seal_backend); 39 | 40 | } // namespace ngraph::runtime::he 41 | -------------------------------------------------------------------------------- /src/seal/kernel/mod_reduce_seal.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | 21 | #include "he_type.hpp" 22 | #include "seal/he_seal_backend.hpp" 23 | #include "seal/seal.h" 24 | #include "seal/seal_ciphertext_wrapper.hpp" 25 | 26 | namespace ngraph::runtime::he { 27 | 28 | inline void mod_reduce_seal(std::vector& arg, 29 | HESealBackend& he_seal_backend, 30 | const bool verbose = false) { 31 | if (verbose) { 32 | NGRAPH_HE_LOG(3) << "Mod-reducing " << arg.size() << " elements"; 33 | } 34 | auto t0 = std::chrono::system_clock::now(); 35 | 36 | #pragma omp parallel for 37 | for (size_t he_idx = 0; he_idx < arg.size(); ++he_idx) { 38 | if (!arg[he_idx].is_ciphertext()) { 39 | continue; 40 | } 41 | 42 | seal::Ciphertext& encrypted = arg[he_idx].get_ciphertext()->ciphertext(); 43 | auto& context_data = 44 | *he_seal_backend.get_context()->get_context_data(encrypted.parms_id()); 45 | auto& parms = context_data.parms(); 46 | auto& coeff_modulus = parms.coeff_modulus(); 47 | size_t coeff_count = parms.poly_modulus_degree(); 48 | size_t coeff_mod_count = coeff_modulus.size(); 49 | size_t encrypted_ntt_size = encrypted.size(); 50 | 51 | for (size_t i = 0; i < encrypted_ntt_size; i++) { 52 | for (size_t j = 0; j < coeff_mod_count; j++) { 53 | auto modulus = coeff_modulus[j]; 54 | const uint64_t modulus_value = modulus.value(); 55 | const uint64_t const_ratio_1 = modulus.const_ratio()[1]; 56 | 57 | for (size_t k = 0; k < coeff_count; ++k) { 58 | std::uint64_t* poly = encrypted.data(i) + (j * coeff_count) + k; 59 | //*poly = (*poly) % coeff_modulus[j].value(); via 60 | // Barrett base 2^64 reduction 61 | unsigned long long carry; 62 | seal::util::multiply_uint64_hw64(*poly, const_ratio_1, &carry); 63 | carry = *poly - carry * modulus_value; 64 | *poly = carry - (modulus_value & 65 | static_cast( 66 | -static_cast(carry >= modulus_value))); 67 | } 68 | } 69 | } 70 | } 71 | auto t1 = std::chrono::system_clock::now(); 72 | NGRAPH_HE_LOG(3) 73 | << "lazy mod-reduce took " 74 | << std::chrono::duration_cast(t1 - t0).count() 75 | << "ms"; 76 | } 77 | } // namespace ngraph::runtime::he 78 | -------------------------------------------------------------------------------- /src/seal/kernel/negate_seal.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #include "seal/kernel/negate_seal.hpp" 18 | 19 | #include 20 | #include 21 | #include 22 | 23 | namespace ngraph::runtime::he { 24 | 25 | void scalar_negate_seal(const SealCiphertextWrapper& arg, 26 | std::shared_ptr& out, 27 | const HESealBackend& he_seal_backend) { 28 | he_seal_backend.get_evaluator()->negate(arg.ciphertext(), out->ciphertext()); 29 | } 30 | 31 | void scalar_negate_seal(const HEPlaintext& arg, HEPlaintext& out) { 32 | HEPlaintext out_vals(arg.size()); 33 | std::transform(arg.begin(), arg.end(), out_vals.begin(), std::negate<>()); 34 | out = std::move(out_vals); 35 | } 36 | 37 | } // namespace ngraph::runtime::he 38 | -------------------------------------------------------------------------------- /src/seal/kernel/negate_seal.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | #include 21 | 22 | #include "ngraph/type/element_type.hpp" 23 | #include "seal/he_seal_backend.hpp" 24 | #include "seal/seal_ciphertext_wrapper.hpp" 25 | 26 | namespace ngraph::runtime::he { 27 | void scalar_negate_seal(const SealCiphertextWrapper& arg, 28 | std::shared_ptr& out, 29 | const HESealBackend& he_seal_backend); 30 | 31 | void scalar_negate_seal(const HEPlaintext& arg, HEPlaintext& out); 32 | 33 | inline void scalar_negate_seal(HEType& arg, HEType& out, 34 | const element::Type& element_type, 35 | const HESealBackend& he_seal_backend) { 36 | NGRAPH_CHECK(he_seal_backend.is_supported_type(element_type), 37 | "Unsupported type ", element_type); 38 | NGRAPH_CHECK(arg.complex_packing() == out.complex_packing(), 39 | "Complex packing types don't match"); 40 | out.complex_packing() = arg.complex_packing(); 41 | 42 | NGRAPH_CHECK((arg.is_plaintext() == out.is_plaintext()) && 43 | (arg.is_ciphertext() == out.is_ciphertext()), 44 | "Unknown argument types"); 45 | 46 | if (arg.is_ciphertext() && out.is_ciphertext()) { 47 | scalar_negate_seal(*arg.get_ciphertext(), out.get_ciphertext(), 48 | he_seal_backend); 49 | } else if (arg.is_plaintext() && out.is_plaintext()) { 50 | scalar_negate_seal(arg.get_plaintext(), out.get_plaintext()); 51 | } 52 | } 53 | 54 | inline void negate_seal(std::vector& arg, std::vector& out, 55 | size_t count, const element::Type& element_type, 56 | const HESealBackend& he_seal_backend) { 57 | NGRAPH_CHECK(he_seal_backend.is_supported_type(element_type), 58 | "Unsupported type ", element_type); 59 | NGRAPH_CHECK(count <= arg.size(), "Count ", count, 60 | " is too large for arg, with size ", arg.size()); 61 | NGRAPH_CHECK(count <= out.size(), "Count ", count, 62 | " is too large for out, with size ", out.size()); 63 | 64 | #pragma omp parallel for 65 | for (size_t i = 0; i < count; ++i) { 66 | scalar_negate_seal(arg[i], out[i], element_type, he_seal_backend); 67 | } 68 | } 69 | 70 | } // namespace ngraph::runtime::he 71 | -------------------------------------------------------------------------------- /src/seal/kernel/pad_seal.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | #include 21 | 22 | #include "ngraph/axis_vector.hpp" 23 | #include "ngraph/coordinate_transform.hpp" 24 | #include "ngraph/op/pad.hpp" 25 | #include "seal/he_seal_backend.hpp" 26 | 27 | namespace ngraph::runtime::he { 28 | void pad_seal(std::vector& arg0, 29 | std::vector& arg1, // scalar 30 | std::vector& out, const Shape& arg0_shape, 31 | const Shape& out_shape, const CoordinateDiff& padding_below, 32 | const CoordinateDiff& padding_above, op::PadMode pad_mode); 33 | 34 | } // namespace ngraph::runtime::he 35 | -------------------------------------------------------------------------------- /src/seal/kernel/power_seal.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | #include 21 | 22 | #include "he_type.hpp" 23 | #include "ngraph/type/element_type.hpp" 24 | #include "seal/he_seal_backend.hpp" 25 | #include "seal/seal_ciphertext_wrapper.hpp" 26 | 27 | namespace ngraph::runtime::he { 28 | void scalar_power_seal(const HEPlaintext& arg0, const HEPlaintext& arg1, 29 | HEPlaintext& out); 30 | 31 | void scalar_power_seal(const HEType& arg0, const HEType& arg1, HEType& out, 32 | const seal::parms_id_type& parms_id, double scale, 33 | seal::CKKSEncoder& ckks_encoder, 34 | seal::Encryptor& encryptor, seal::Decryptor& decryptor); 35 | 36 | void scalar_power_seal(HEType& arg0, HEType& arg1, HEType& out, 37 | HESealBackend& he_seal_backend); 38 | 39 | void power_seal(std::vector& arg0, std::vector& arg1, 40 | std::vector& out, size_t count, 41 | const element::Type& element_type, 42 | HESealBackend& he_seal_backend); 43 | 44 | } // namespace ngraph::runtime::he 45 | -------------------------------------------------------------------------------- /src/seal/kernel/relu_seal.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #include "seal/kernel/relu_seal.hpp" 18 | 19 | #include 20 | #include 21 | #include 22 | 23 | #include "ngraph/type/element_type.hpp" 24 | #include "seal/he_seal_backend.hpp" 25 | #include "seal/seal_ciphertext_wrapper.hpp" 26 | #include "seal/seal_util.hpp" 27 | 28 | namespace ngraph::runtime::he { 29 | 30 | void scalar_relu_seal(const HEPlaintext& arg, HEPlaintext& out) { 31 | out.resize(arg.size()); 32 | auto relu = [](double d) { return d > 0 ? d : 0.; }; 33 | std::transform(arg.begin(), arg.end(), out.begin(), relu); 34 | } 35 | 36 | void scalar_relu_seal(const HEType& arg, HEType& out, 37 | const seal::parms_id_type& parms_id, double scale, 38 | seal::CKKSEncoder& ckks_encoder, 39 | seal::Encryptor& encryptor, seal::Decryptor& decryptor, 40 | std::shared_ptr context) { 41 | if (arg.is_plaintext()) { 42 | out.set_plaintext(arg.get_plaintext()); 43 | scalar_relu_seal(arg.get_plaintext(), out.get_plaintext()); 44 | } else { 45 | HEPlaintext plain; 46 | decrypt(plain, *arg.get_ciphertext(), arg.complex_packing(), decryptor, 47 | ckks_encoder, context, arg.batch_size()); 48 | scalar_relu_seal(plain, plain); 49 | encrypt(out.get_ciphertext(), plain, parms_id, element::f32, scale, 50 | ckks_encoder, encryptor, arg.complex_packing()); 51 | out.set_ciphertext(out.get_ciphertext()); 52 | } 53 | } 54 | 55 | void scalar_relu_seal(const HEType& arg, HEType& out, 56 | const HESealBackend& he_seal_backend) { 57 | scalar_relu_seal( 58 | arg, out, he_seal_backend.get_context()->first_parms_id(), 59 | he_seal_backend.get_scale(), *he_seal_backend.get_ckks_encoder(), 60 | *he_seal_backend.get_encryptor(), *he_seal_backend.get_decryptor(), 61 | he_seal_backend.get_context()); 62 | } 63 | 64 | void relu_seal(const std::vector& arg, std::vector& out, 65 | size_t count, const HESealBackend& he_seal_backend) { 66 | #pragma omp parallel for 67 | for (size_t i = 0; i < count; ++i) { 68 | scalar_relu_seal(arg[i], out[i], he_seal_backend); 69 | } 70 | } 71 | 72 | } // namespace ngraph::runtime::he 73 | -------------------------------------------------------------------------------- /src/seal/kernel/relu_seal.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | #include 21 | 22 | #include "he_type.hpp" 23 | #include "ngraph/type/element_type.hpp" 24 | #include "seal/he_seal_backend.hpp" 25 | #include "seal/seal_ciphertext_wrapper.hpp" 26 | #include "seal/seal_util.hpp" 27 | 28 | namespace ngraph::runtime::he { 29 | void scalar_relu_seal(const HEPlaintext& arg, HEPlaintext& out); 30 | 31 | void scalar_relu_seal(const HEType& arg, HEType& out, 32 | const seal::parms_id_type& parms_id, double scale, 33 | seal::CKKSEncoder& ckks_encoder, 34 | seal::Encryptor& encryptor, seal::Decryptor& decryptor, 35 | std::shared_ptr context); 36 | 37 | void scalar_relu_seal(const HEType& arg, HEType& out, 38 | const HESealBackend& he_seal_backend); 39 | 40 | void relu_seal(const std::vector& arg, std::vector& out, 41 | size_t count, const HESealBackend& he_seal_backend); 42 | 43 | } // namespace ngraph::runtime::he 44 | -------------------------------------------------------------------------------- /src/seal/kernel/rescale_seal.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #include "seal/kernel/rescale_seal.hpp" 18 | 19 | #include 20 | #include 21 | #include 22 | 23 | namespace ngraph::runtime::he { 24 | 25 | void rescale_seal(std::vector& arg, HESealBackend& he_seal_backend, 26 | const bool verbose) { 27 | if (verbose) { 28 | NGRAPH_HE_LOG(3) << "Rescaling " << arg.size() << " elements"; 29 | } 30 | 31 | using Clock = std::chrono::high_resolution_clock; 32 | auto t1 = Clock::now(); 33 | size_t new_chain_index = std::numeric_limits::max(); 34 | 35 | bool all_plaintexts = true; 36 | for (auto& he_type : arg) { 37 | if (he_type.is_ciphertext()) { 38 | size_t curr_chain_index = 39 | he_seal_backend.get_chain_index(*he_type.get_ciphertext()); 40 | if (curr_chain_index == 0) { 41 | new_chain_index = 0; 42 | } else { 43 | new_chain_index = curr_chain_index - 1; 44 | } 45 | all_plaintexts = false; 46 | break; 47 | } 48 | } 49 | 50 | if (all_plaintexts) { 51 | if (verbose) { 52 | NGRAPH_HE_LOG(3) << "Skipping rescaling because all values are known"; 53 | } 54 | return; 55 | } 56 | 57 | NGRAPH_CHECK(new_chain_index != std::numeric_limits::max(), 58 | "Invalid new chain index in rescaling"); 59 | if (new_chain_index == 0) { 60 | if (verbose) { 61 | NGRAPH_HE_LOG(3) << "Skipping rescaling to chain index 0"; 62 | } 63 | return; 64 | } 65 | if (verbose) { 66 | NGRAPH_HE_LOG(3) << "New chain index " << new_chain_index; 67 | } 68 | 69 | #pragma omp parallel for 70 | for (size_t i = 0; i < arg.size(); ++i) { // NOLINT 71 | auto cipher = arg[i]; 72 | if (arg[i].is_ciphertext()) { 73 | he_seal_backend.get_evaluator()->rescale_to_next_inplace( 74 | arg[i].get_ciphertext()->ciphertext()); 75 | } 76 | } 77 | if (verbose) { 78 | auto t2 = Clock::now(); 79 | NGRAPH_HE_LOG(3) << "Rescale_xxx took " 80 | << std::chrono::duration_cast( 81 | t2 - t1) 82 | .count() 83 | << "ms"; 84 | } 85 | } 86 | 87 | } // namespace ngraph::runtime::he 88 | -------------------------------------------------------------------------------- /src/seal/kernel/rescale_seal.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | 21 | #include "he_type.hpp" 22 | #include "seal/he_seal_backend.hpp" 23 | #include "seal/seal_ciphertext_wrapper.hpp" 24 | 25 | namespace ngraph::runtime::he { 26 | 27 | void rescale_seal(std::vector& arg, HESealBackend& he_seal_backend, 28 | const bool verbose = false); 29 | 30 | } // namespace ngraph::runtime::he 31 | -------------------------------------------------------------------------------- /src/seal/kernel/reshape_seal.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | 21 | #include "he_type.hpp" 22 | #include "ngraph/axis_vector.hpp" 23 | #include "ngraph/coordinate_transform.hpp" 24 | 25 | namespace ngraph::runtime::he { 26 | inline void reshape_seal(const std::vector& arg, 27 | std::vector& out, const Shape& in_shape, 28 | const AxisVector& in_axis_order, 29 | const Shape& out_shape) { 30 | // Unfortunately we don't yet have a constructor for CoordinateTransform that 31 | // lets us pass only source_space_shape and source_axis_order so we have to 32 | // construct the defaults here. 33 | Shape in_start_corner(in_shape.size(), 0); // (0,...0) 34 | Strides in_strides(in_shape.size(), 1); // (1,...,1) 35 | 36 | CoordinateTransform input_transform(in_shape, in_start_corner, in_shape, 37 | in_strides, in_axis_order); 38 | 39 | CoordinateTransform output_transform(out_shape); 40 | CoordinateTransform::Iterator output_it = output_transform.begin(); 41 | 42 | NGRAPH_CHECK(shape_size(input_transform.get_target_shape()) == 43 | shape_size(output_transform.get_target_shape())); 44 | 45 | for (const Coordinate& input_coord : input_transform) { 46 | const Coordinate& output_coord = *output_it; 47 | out[output_transform.index(output_coord)] = 48 | arg[input_transform.index(input_coord)]; 49 | ++output_it; 50 | } 51 | } 52 | 53 | } // namespace ngraph::runtime::he 54 | -------------------------------------------------------------------------------- /src/seal/kernel/result_seal.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | #include 21 | 22 | #include "he_plaintext.hpp" 23 | #include "ngraph/check.hpp" 24 | #include "seal/he_seal_backend.hpp" 25 | #include "seal/seal_ciphertext_wrapper.hpp" 26 | #include "seal/seal_util.hpp" 27 | 28 | namespace ngraph::runtime::he { 29 | 30 | inline void scalar_result_seal(const HEType& arg, HEType& out, 31 | HESealBackend& /*he_seal_backend*/) { 32 | out.complex_packing() = arg.complex_packing(); 33 | 34 | if (arg.is_ciphertext() && out.is_ciphertext()) { 35 | out = HEType(arg); 36 | } else if (arg.is_ciphertext() && out.is_plaintext()) { 37 | out.set_ciphertext(arg.get_ciphertext()); 38 | } else if (arg.is_plaintext() && out.is_ciphertext()) { 39 | out.set_plaintext(arg.get_plaintext()); 40 | } else if (arg.is_plaintext() && out.is_plaintext()) { 41 | out = HEType(arg); 42 | } 43 | } 44 | 45 | inline void result_seal(const std::vector& arg, 46 | std::vector& out, const size_t count, 47 | HESealBackend& he_seal_backend) { 48 | NGRAPH_CHECK(arg.size() >= count, "Result arg size ", arg.size(), 49 | " smaller than count ", count); 50 | NGRAPH_CHECK(out.size() >= count, "Result out size ", out.size(), 51 | " smaller than count ", count); 52 | 53 | #pragma omp parallel for 54 | for (size_t i = 0; i < count; ++i) { 55 | scalar_result_seal(arg[i], out[i], he_seal_backend); 56 | } 57 | } 58 | 59 | } // namespace ngraph::runtime::he 60 | -------------------------------------------------------------------------------- /src/seal/kernel/reverse_seal.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | 21 | #include "he_type.hpp" 22 | #include "ngraph/coordinate_transform.hpp" 23 | 24 | namespace ngraph::runtime::he { 25 | inline void reverse_seal(const std::vector& arg, 26 | std::vector& out, const Shape& arg_shape, 27 | const Shape& out_shape, const AxisSet& reversed_axes) { 28 | // In fact arg_shape == out_shape, but we'll use both for stylistic 29 | // consistency with other kernels. 30 | CoordinateTransform arg_transform(arg_shape); 31 | CoordinateTransform output_transform(out_shape); 32 | 33 | for (const Coordinate& out_coord : output_transform) { 34 | Coordinate arg_coord = out_coord; 35 | 36 | for (size_t i = 0; i < arg_coord.size(); i++) { 37 | if (reversed_axes.count(i) != 0) { 38 | arg_coord[i] = arg_shape[i] - arg_coord[i] - 1; 39 | } 40 | } 41 | 42 | out[output_transform.index(out_coord)] = 43 | arg[arg_transform.index(arg_coord)]; 44 | } 45 | } 46 | 47 | } // namespace ngraph::runtime::he 48 | -------------------------------------------------------------------------------- /src/seal/kernel/slice_seal.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | 21 | #include "he_type.hpp" 22 | #include "ngraph/coordinate_transform.hpp" 23 | 24 | namespace ngraph::runtime::he { 25 | inline void slice_seal(const std::vector& arg, std::vector& out, 26 | const Shape& arg_shape, const Coordinate& lower_bounds, 27 | const Coordinate& upper_bounds, const Strides& strides, 28 | const Shape& out_shape) { 29 | CoordinateTransform input_transform(arg_shape, lower_bounds, upper_bounds, 30 | strides); 31 | CoordinateTransform output_transform(out_shape); 32 | 33 | CoordinateTransform::Iterator output_it = output_transform.begin(); 34 | 35 | NGRAPH_CHECK(shape_size(input_transform.get_target_shape()) == 36 | shape_size(output_transform.get_target_shape()), 37 | "Slice transform shape sizes don't match"); 38 | 39 | for (const Coordinate& in_coord : input_transform) { 40 | const Coordinate& out_coord = *output_it; 41 | 42 | out[output_transform.index(out_coord)] = 43 | arg[input_transform.index(in_coord)]; 44 | 45 | ++output_it; 46 | } 47 | } 48 | 49 | } // namespace ngraph::runtime::he 50 | -------------------------------------------------------------------------------- /src/seal/kernel/softmax_seal.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | 21 | #include "he_type.hpp" 22 | #include "ngraph/coordinate_transform.hpp" 23 | #include "ngraph/shape_util.hpp" 24 | #include "seal/he_seal_backend.hpp" 25 | 26 | namespace ngraph::runtime::he { 27 | void softmax_seal(std::vector& arg, std::vector& out, 28 | const Shape& shape, const AxisSet& axes, 29 | const element::Type& element_type, 30 | HESealBackend& he_seal_backend); 31 | 32 | } // namespace ngraph::runtime::he 33 | -------------------------------------------------------------------------------- /src/seal/kernel/sum_seal.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | 21 | #include "he_type.hpp" 22 | #include "ngraph/coordinate_transform.hpp" 23 | #include "ngraph/type/element_type.hpp" 24 | #include "seal/he_seal_backend.hpp" 25 | #include "seal/kernel/add_seal.hpp" 26 | 27 | namespace ngraph::runtime::he { 28 | inline void sum_seal(std::vector& arg, std::vector& out, 29 | const Shape& in_shape, const Shape& out_shape, 30 | const AxisSet& reduction_axes, 31 | const element::Type& element_type, 32 | HESealBackend& he_seal_backend) { 33 | NGRAPH_CHECK(he_seal_backend.is_supported_type(element_type), 34 | "Unsupported type ", element_type); 35 | CoordinateTransform output_transform(out_shape); 36 | 37 | bool complex_packing = !arg.empty() ? arg[0].complex_packing() : false; 38 | size_t batch_size = !arg.empty() ? arg[0].batch_size() : 1; 39 | 40 | for (const Coordinate& output_coord : output_transform) { 41 | // TODO(fboemer): batch size 42 | const auto out_coord_idx = output_transform.index(output_coord); 43 | out[out_coord_idx] = HEType(HEPlaintext(batch_size, 0), complex_packing); 44 | } 45 | 46 | CoordinateTransform input_transform(in_shape); 47 | 48 | for (const Coordinate& input_coord : input_transform) { 49 | Coordinate output_coord = reduce(input_coord, reduction_axes); 50 | 51 | auto& input = arg[input_transform.index(input_coord)]; 52 | auto& output = out[output_transform.index(output_coord)]; 53 | 54 | scalar_add_seal(input, output, output, he_seal_backend); 55 | } 56 | } 57 | 58 | } // namespace ngraph::runtime::he 59 | -------------------------------------------------------------------------------- /src/seal/opset_he_seal_tbl.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #define ID_SUFFIX(NAME) NAME 18 | #include "ngraph/opsets/opset0_tbl.hpp" 19 | NGRAPH_OP(BoundedRelu, op) 20 | #undef ID_SUFFIX 21 | 22 | #define ID_SUFFIX(NAME) NAME##_v1 23 | /* NGRAPH_OP(LessEqual, op::v1) 24 | NGRAPH_OP(LogicalAnd, op::v1) 25 | NGRAPH_OP(LogicalOr, op::v1) 26 | NGRAPH_OP(LogicalXor, op::v1) 27 | NGRAPH_OP(LogicalNot, op::v1) */ 28 | #undef ID_SUFFIX 29 | -------------------------------------------------------------------------------- /src/seal/seal_ciphertext_wrapper.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #include "seal/seal_ciphertext_wrapper.hpp" 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include "logging/ngraph_he_log.hpp" 25 | #include "ngraph/check.hpp" 26 | #include "protos/message.pb.h" 27 | #include "seal/seal.h" 28 | 29 | namespace ngraph::runtime::he { 30 | 31 | SealCiphertextWrapper::SealCiphertextWrapper() = default; 32 | 33 | void SealCiphertextWrapper::save(pb::HEType& he_type) const { 34 | size_t cipher_size = ciphertext_size(m_ciphertext); 35 | std::string cipher_str; 36 | cipher_str.resize(cipher_size); 37 | 38 | size_t save_size = ngraph::runtime::he::save( 39 | m_ciphertext, reinterpret_cast(cipher_str.data())); 40 | 41 | NGRAPH_CHECK(save_size == cipher_size, "Save size != cipher size"); 42 | 43 | he_type.set_ciphertext(std::move(cipher_str)); 44 | } 45 | 46 | void SealCiphertextWrapper::load(SealCiphertextWrapper& dst, 47 | const pb::HEType& pb_he_type, 48 | std::shared_ptr context) { 49 | NGRAPH_CHECK(!pb_he_type.is_plaintext(), 50 | "Cannot load ciphertext from plaintext HEType"); 51 | 52 | const std::string& cipher_str = pb_he_type.ciphertext(); 53 | ngraph::runtime::he::load( 54 | dst.ciphertext(), std::move(context), 55 | reinterpret_cast(cipher_str.data()), cipher_str.size()); 56 | } 57 | 58 | } // namespace ngraph::runtime::he 59 | -------------------------------------------------------------------------------- /src/seal/seal_plaintext_wrapper.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #include "seal/seal_plaintext_wrapper.hpp" 18 | 19 | #include 20 | 21 | #include "seal/seal.h" 22 | 23 | namespace ngraph::runtime::he { 24 | 25 | SealPlaintextWrapper::SealPlaintextWrapper(seal::Plaintext plain, 26 | bool complex_packing) 27 | : m_plaintext(std::move(plain)), m_complex_packing(complex_packing) {} 28 | 29 | SealPlaintextWrapper::SealPlaintextWrapper(bool complex_packing) 30 | : m_complex_packing(complex_packing) {} 31 | 32 | bool SealPlaintextWrapper::complex_packing() const { return m_complex_packing; } 33 | 34 | bool& SealPlaintextWrapper::complex_packing() { return m_complex_packing; } 35 | 36 | const seal::Plaintext& SealPlaintextWrapper::plaintext() const { 37 | return m_plaintext; 38 | } 39 | 40 | seal::Plaintext& SealPlaintextWrapper::plaintext() { return m_plaintext; } 41 | 42 | } // namespace ngraph::runtime::he 43 | -------------------------------------------------------------------------------- /src/seal/seal_plaintext_wrapper.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | 21 | #include "seal/seal.h" 22 | 23 | namespace ngraph::runtime::he { 24 | /// \brief Wrapper around Seal::Plaintext with complex packing information 25 | class SealPlaintextWrapper { 26 | public: 27 | /// \brief Constructs a SealPlaintextWrapper from a given plaintet and complex 28 | /// packing argument 29 | /// \param[in] plain Plaintext value 30 | /// \param[in] complex_packing Whether or not the plaintext uses complex 31 | /// packing 32 | explicit SealPlaintextWrapper(seal::Plaintext plain, 33 | bool complex_packing = false); 34 | 35 | /// \brief Constructs an empty plaintext wrapper using complex packing 36 | /// argument 37 | /// \param[in] complex_packing Whether or not the plaintext uses complex 38 | /// packing 39 | explicit SealPlaintextWrapper(bool complex_packing = false); 40 | /// \brief Returns whether or not underlying plaintext uses complex packing 41 | 42 | bool complex_packing() const; 43 | 44 | /// \brief Returns whether or not underlying plaintext uses complex packing 45 | bool& complex_packing(); 46 | 47 | /// \brief Returns reference to underlying plaintext 48 | const seal::Plaintext& plaintext() const; 49 | 50 | /// \brief Returns reference to underlying plaintext 51 | seal::Plaintext& plaintext(); 52 | 53 | private: 54 | seal::Plaintext m_plaintext; 55 | bool m_complex_packing; 56 | }; 57 | } // namespace ngraph::runtime::he 58 | -------------------------------------------------------------------------------- /src/tcp/tcp_client.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | #include 21 | #include 22 | 23 | #include "boost/asio.hpp" 24 | #include "logging/ngraph_he_log.hpp" 25 | #include "tcp/tcp_message.hpp" 26 | 27 | namespace ngraph::runtime::he { 28 | /// \brief Class representing a Client over a TCP connection 29 | class TCPClient { 30 | public: 31 | using data_buffer = TCPMessage::data_buffer; 32 | size_t header_length = TCPMessage::header_length; 33 | 34 | /// \brief Connects client to hostname:port and reads message 35 | /// \param[in] io_context Boost context for I/O functionality 36 | /// \param[in] endpoints Socket to connect to 37 | /// \param[in] message_handler Function to handle responses from the server 38 | TCPClient(boost::asio::io_context& io_context, 39 | const boost::asio::ip::tcp::resolver::results_type& endpoints, 40 | const std::function& message_handler); 41 | 42 | /// \brief Closes the socket 43 | void close(); 44 | 45 | /// \brief Asynchronously writes the message 46 | /// \param[in,out] message Message to write 47 | void write_message(TCPMessage&& message); 48 | 49 | private: 50 | void do_connect(const boost::asio::ip::tcp::resolver::results_type& endpoints, 51 | size_t delay_ms = 10); 52 | 53 | void do_read_header(); 54 | 55 | void do_read_body(size_t body_length); 56 | 57 | void do_write(); 58 | 59 | boost::asio::io_context& m_io_context; 60 | boost::asio::ip::tcp::socket m_socket; 61 | 62 | data_buffer m_read_buffer; 63 | data_buffer m_write_buffer; 64 | TCPMessage m_read_message; 65 | std::deque m_message_queue; 66 | 67 | static const char* s_expected_teardown_message; 68 | 69 | std::function m_message_callback; 70 | }; 71 | } // namespace ngraph::runtime::he 72 | -------------------------------------------------------------------------------- /src/tcp/tcp_message.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #include "tcp/tcp_message.hpp" 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include "ngraph/check.hpp" 25 | #include "ngraph/log.hpp" 26 | #include "ngraph/util.hpp" 27 | #include "protos/message.pb.h" 28 | 29 | namespace ngraph::runtime::he { 30 | 31 | TCPMessage::TCPMessage() = default; 32 | 33 | TCPMessage::TCPMessage(pb::TCPMessage&& pb_message) 34 | : m_pb_message(std::make_shared(std::move(pb_message))) {} 35 | 36 | std::shared_ptr TCPMessage::pb_message() const { 37 | return m_pb_message; 38 | } 39 | 40 | void TCPMessage::encode_header(TCPMessage::data_buffer& buffer, size_t size) { 41 | NGRAPH_CHECK(buffer.size() >= TCPMessage::header_length, "Buffer too small"); 42 | std::memcpy(&buffer[0], &size, TCPMessage::header_length); 43 | } 44 | 45 | size_t TCPMessage::decode_header(const TCPMessage::data_buffer& buffer) { 46 | if (buffer.size() < TCPMessage::header_length) { 47 | return 0; 48 | } 49 | size_t body_length = 0; 50 | std::memcpy(&body_length, &buffer[0], TCPMessage::header_length); 51 | return body_length; 52 | } 53 | 54 | bool TCPMessage::pack(TCPMessage::data_buffer& buffer) { 55 | NGRAPH_CHECK(m_pb_message != nullptr, "Can't pack empty proto message"); 56 | size_t msg_size = m_pb_message->ByteSize(); 57 | buffer.resize(TCPMessage::header_length + msg_size); 58 | encode_header(buffer, msg_size); 59 | return m_pb_message->SerializeToArray(&buffer[TCPMessage::header_length], 60 | msg_size); 61 | } 62 | 63 | bool TCPMessage::unpack(const TCPMessage::data_buffer& buffer) { 64 | if (!m_pb_message) { 65 | m_pb_message = std::make_shared(); 66 | } 67 | return m_pb_message->ParseFromArray( 68 | &buffer[TCPMessage::header_length], 69 | buffer.size() - TCPMessage::header_length); 70 | } 71 | 72 | } // namespace ngraph::runtime::he 73 | -------------------------------------------------------------------------------- /src/tcp/tcp_message.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | #include 21 | 22 | #include "protos/message.pb.h" 23 | 24 | namespace ngraph::runtime::he { 25 | /// \brief Represents a message. A wrapper around pb::TCPMessage 26 | class TCPMessage { 27 | public: 28 | enum { header_length = sizeof(size_t) }; 29 | using data_buffer = std::vector; 30 | 31 | /// \brief Creates empty message 32 | TCPMessage(); 33 | 34 | explicit TCPMessage(pb::TCPMessage& pb_message) = delete; 35 | 36 | /// \brief Creates message from given protobuf message 37 | /// \param[in,out] pb_message Protobuf message to populate TCPMessage 38 | explicit TCPMessage(pb::TCPMessage&& pb_message); 39 | 40 | /// \brief Returns pointer to udnerlying protobuf message 41 | std::shared_ptr pb_message() const; 42 | 43 | /// \brief Stores a size in the buffer header 44 | /// \param[in,out] buffer Buffer to write size to 45 | /// \param[in] size Size to write into buffer 46 | static void encode_header(data_buffer& buffer, size_t size); 47 | 48 | /// \brief Given a buffer storing a message with the length in the first 49 | /// header_length bytes, returns the size of the stored buffer 50 | /// \param[in] buffer Buffer storing a message 51 | /// \returns size of message stored in buffer 52 | static size_t decode_header(const data_buffer& buffer); 53 | 54 | /// \brief Writes the message to a buffer 55 | /// \param[in,out] buffer Buffer to write the message to 56 | /// \throws ngraph_error if message is empty 57 | /// \returns Whether or not the operation was successful 58 | bool pack(data_buffer& buffer); 59 | 60 | /// \brief Writes a given buffer to the message 61 | /// \param[in] buffer Buffer to read the message from 62 | /// \returns Whether or not the operation was successful 63 | bool unpack(const data_buffer& buffer); 64 | 65 | private: 66 | std::shared_ptr m_pb_message; 67 | }; 68 | } // namespace ngraph::runtime::he 69 | -------------------------------------------------------------------------------- /src/tcp/tcp_session.hpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #pragma once 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include "boost/asio.hpp" 26 | #include "logging/ngraph_he_log.hpp" 27 | #include "tcp/tcp_message.hpp" 28 | 29 | namespace ngraph::runtime::he { 30 | /// \brief Class representing a session over TCP 31 | class TCPSession : public std::enable_shared_from_this { 32 | using data_buffer = TCPMessage::data_buffer; 33 | size_t header_length = TCPMessage::header_length; 34 | 35 | public: 36 | /// \brief Constructs a session with a given message handler 37 | TCPSession(boost::asio::ip::tcp::socket socket, 38 | const std::function& message_handler); 39 | 40 | /// \brief Start the session 41 | void start() { do_read_header(); } 42 | 43 | /// \brief Reads a header 44 | void do_read_header(); 45 | 46 | /// \brief Reads message body of specified length 47 | /// \param[in] body_length Number of bytes to read 48 | void do_read_body(size_t body_length); 49 | 50 | /// \brief Adds a message to the message-writing queue 51 | /// \param[in,out] message Message to write 52 | void write_message(TCPMessage&& message); 53 | 54 | /// \brief Returns whether or not a message is queued to be written 55 | bool is_writing() const { return !m_message_queue.empty(); } 56 | 57 | /// \brief Returns a condition variable notified when the session is done 58 | /// writing a message 59 | std::condition_variable& is_writing_cond() { return m_is_writing; } 60 | 61 | private: 62 | void do_write(); 63 | 64 | private: 65 | std::deque m_message_queue; 66 | TCPMessage m_read_message; 67 | 68 | data_buffer m_read_buffer; 69 | data_buffer m_write_buffer; 70 | boost::asio::ip::tcp::socket m_socket; 71 | std::condition_variable m_is_writing; 72 | std::mutex m_write_mtx; 73 | 74 | inline static std::string s_expected_teardown_message{"End of file"}; 75 | 76 | std::function m_message_callback; 77 | }; 78 | } // namespace ngraph::runtime::he 79 | -------------------------------------------------------------------------------- /test/.clang-tidy: -------------------------------------------------------------------------------- 1 | 2 | # Configure clang-tidy for unit-tests 3 | Checks: '-*,llvm-twine-local' 4 | -------------------------------------------------------------------------------- /test/main.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #include 18 | 19 | #include 20 | #include 21 | #include 22 | 23 | #include "gtest/gtest.h" 24 | #include "seal/he_seal_backend.hpp" 25 | 26 | int main(int argc, char** argv) { 27 | ngraph_register_he_seal_backend(); 28 | 29 | ::testing::InitGoogleTest(&argc, argv); 30 | int rc = RUN_ALL_TESTS(); 31 | return rc; 32 | } 33 | -------------------------------------------------------------------------------- /test/test_aby.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #include 18 | 19 | #include "ENCRYPTO_utils/crypto/crypto.h" 20 | #include "ENCRYPTO_utils/parse_options.h" 21 | #include "aby/aby_util.hpp" 22 | #include "aby/kernel/relu_aby.hpp" 23 | #include "abycore/aby/abyparty.h" 24 | #include "abycore/circuit/booleancircuits.h" 25 | #include "abycore/circuit/share.h" 26 | #include "abycore/sharing/sharing.h" 27 | #include "gtest/gtest.h" 28 | 29 | namespace ngraph::runtime::aby { 30 | 31 | TEST(aby, trivial) { 32 | int a = 1; 33 | int b = 2; 34 | EXPECT_EQ(3, a + b); 35 | } 36 | 37 | TEST(aby, create_party) { 38 | auto a = new ABYParty(CLIENT, "localhost", 30001, get_sec_lvl(128), 32, 2, 39 | MT_OT, 100000); 40 | delete a; 41 | EXPECT_EQ(1, 1); 42 | } 43 | 44 | TEST(aby, create_unique_ptr_party) { 45 | auto a = std::make_unique(CLIENT, "localhost", 30001, 46 | get_sec_lvl(128), 32, 2, MT_OT, 100000); 47 | EXPECT_EQ(1, 1); 48 | } 49 | 50 | TEST(aby, mod_reduce_zero_centered) { 51 | // Already in range 52 | EXPECT_DOUBLE_EQ(mod_reduce_zero_centered(0.1, 2.0), 0.1); 53 | 54 | // Below range 55 | EXPECT_DOUBLE_EQ(mod_reduce_zero_centered(-1.1, 2.0), 0.9); 56 | 57 | // Far below range 58 | EXPECT_DOUBLE_EQ(mod_reduce_zero_centered(-9.1, 2.0), 0.9); 59 | 60 | // Above range 61 | EXPECT_DOUBLE_EQ(mod_reduce_zero_centered(1.1, 2.0), -0.9); 62 | 63 | // Far above range 64 | EXPECT_DOUBLE_EQ(mod_reduce_zero_centered(9.1, 2.0), -0.9); 65 | } 66 | 67 | TEST(aby, split_vector) { 68 | { 69 | auto splits = split_vector(10, 3); 70 | EXPECT_EQ(splits.size(), 3); 71 | EXPECT_EQ(splits[0].first, 0); 72 | EXPECT_EQ(splits[0].second, 4); 73 | EXPECT_EQ(splits[1].first, 4); 74 | EXPECT_EQ(splits[1].second, 7); 75 | EXPECT_EQ(splits[2].first, 7); 76 | EXPECT_EQ(splits[2].second, 10); 77 | } 78 | { 79 | auto splits = split_vector(10, 4); 80 | EXPECT_EQ(splits.size(), 4); 81 | EXPECT_EQ(splits[0].first, 0); 82 | EXPECT_EQ(splits[0].second, 3); 83 | EXPECT_EQ(splits[1].first, 3); 84 | EXPECT_EQ(splits[1].second, 6); 85 | EXPECT_EQ(splits[2].first, 6); 86 | EXPECT_EQ(splits[2].second, 8); 87 | EXPECT_EQ(splits[3].first, 8); 88 | EXPECT_EQ(splits[3].second, 10); 89 | } 90 | } 91 | 92 | } // namespace ngraph::runtime::aby 93 | -------------------------------------------------------------------------------- /test/test_bounded_relu.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #include 18 | 19 | #include "gtest/gtest.h" 20 | #include "he_tensor.hpp" 21 | #include "he_type.hpp" 22 | #include "ngraph/ngraph.hpp" 23 | #include "op/bounded_relu.hpp" 24 | #include "seal/he_seal_backend.hpp" 25 | #include "seal/he_seal_executable.hpp" 26 | #include "test_util.hpp" 27 | #include "util/test_tools.hpp" 28 | 29 | namespace ngraph::runtime::he { 30 | TEST(bounded_relu, copy) { 31 | Shape shape{1}; 32 | auto arg0 = std::make_shared(element::f32, shape); 33 | NodeVector new_args{std::make_shared(element::f32, shape)}; 34 | 35 | auto node = std::make_shared(arg0, 7.2); 36 | auto new_node = node->copy_with_new_args(new_args); 37 | 38 | EXPECT_TRUE(new_node != nullptr); 39 | EXPECT_TRUE(new_args == new_node->get_arguments()); 40 | 41 | auto new_bounded_relu = std::static_pointer_cast(new_node); 42 | EXPECT_FLOAT_EQ(new_bounded_relu->get_alpha(), node->get_alpha()); 43 | } 44 | 45 | } // namespace ngraph::runtime::he 46 | -------------------------------------------------------------------------------- /test/test_he_op_annotations.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #include "gtest/gtest.h" 18 | #include "he_op_annotations.hpp" 19 | #include "ngraph/op/parameter.hpp" 20 | 21 | namespace ngraph::runtime::he { 22 | 23 | TEST(he_op_annotations, set_get) { 24 | auto ann = HEOpAnnotations(false, false, false); 25 | EXPECT_FALSE(ann.from_client()); 26 | EXPECT_FALSE(ann.encrypted()); 27 | EXPECT_FALSE(ann.packed()); 28 | 29 | ann.set_from_client(true); 30 | EXPECT_TRUE(ann.from_client()); 31 | 32 | ann.set_from_client(false); 33 | EXPECT_FALSE(ann.from_client()); 34 | 35 | ann.set_encrypted(true); 36 | EXPECT_TRUE(ann.encrypted()); 37 | 38 | ann.set_encrypted(false); 39 | EXPECT_FALSE(ann.encrypted()); 40 | 41 | ann.set_packed(true); 42 | EXPECT_TRUE(ann.packed()); 43 | 44 | ann.set_packed(false); 45 | EXPECT_FALSE(ann.packed()); 46 | } 47 | 48 | TEST(he_op_annotations, initialize) { 49 | HEOpAnnotations annotation{false, true, false}; 50 | HEOpAnnotations annotation2{annotation}; 51 | 52 | EXPECT_EQ(annotation, annotation2); 53 | } 54 | 55 | TEST(he_op_annotations, defaults) { 56 | auto param = std::make_shared(element::f32, Shape{}); 57 | 58 | EXPECT_FALSE(HEOpAnnotations::from_client(*param)); 59 | EXPECT_FALSE(HEOpAnnotations::plaintext_packed(*param)); 60 | } 61 | 62 | } // namespace ngraph::runtime::he 63 | -------------------------------------------------------------------------------- /test/test_he_supported_ops.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #include "ngraph/ngraph.hpp" 18 | #include "seal/he_seal_backend.hpp" 19 | #include "test_util.hpp" 20 | #include "util/test_control.hpp" 21 | #include "util/test_tools.hpp" 22 | 23 | namespace ngraph::runtime::he { 24 | 25 | TEST(he_unsupported_ops, op) { 26 | auto backend = runtime::Backend::create("HE_SEAL"); 27 | 28 | Shape shape{11}; 29 | auto a = std::make_shared(element::f32, shape); 30 | auto f = std::make_shared(std::make_shared(a), 31 | ParameterVector{a}); 32 | 33 | EXPECT_THROW({ backend->compile(f); }, CheckFailure); 34 | } 35 | 36 | TEST(he_unsupported_ops, element_type) { 37 | auto backend = runtime::Backend::create("HE_SEAL"); 38 | 39 | Shape shape{11}; 40 | auto a = std::make_shared(element::i8, shape); 41 | auto b = std::make_shared(element::i8, shape); 42 | { 43 | auto f = std::make_shared(std::make_shared(a, b), 44 | ParameterVector{a, b}); 45 | EXPECT_THROW({ backend->compile(f); }, CheckFailure); 46 | } 47 | { 48 | auto f = std::make_shared(std::make_shared(a, b), 49 | ParameterVector{a, b}); 50 | EXPECT_THROW({ backend->compile(f); }, CheckFailure); 51 | } 52 | { 53 | auto f = std::make_shared(std::make_shared(a, b), 54 | ParameterVector{a, b}); 55 | EXPECT_THROW({ backend->compile(f); }, CheckFailure); 56 | } 57 | } 58 | 59 | } // namespace ngraph::runtime::he 60 | -------------------------------------------------------------------------------- /test/test_ngraph_he_log.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #include "gtest/gtest.h" 18 | #include "logging/ngraph_he_log.hpp" 19 | 20 | namespace ngraph::runtime::he { 21 | 22 | TEST(ngraph_he_log, log_level_str_to_int) { 23 | EXPECT_EQ(uint64_t{0}, logging::log_level_str_to_int(nullptr)); 24 | 25 | std::string invalid_str{"DUMMY"}; 26 | EXPECT_EQ(uint64_t{0}, logging::log_level_str_to_int(invalid_str.c_str())); 27 | } 28 | 29 | } // namespace ngraph::runtime::he 30 | -------------------------------------------------------------------------------- /test/test_protobuf.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #include 18 | 19 | #include 20 | #include 21 | 22 | #include "gtest/gtest.h" 23 | #include "protos/message.pb.h" 24 | #include "seal/he_seal_backend.hpp" 25 | #include "seal/seal.h" 26 | #include "seal/seal_ciphertext_wrapper.hpp" 27 | #include "tcp/tcp_message.hpp" 28 | #include "util/test_tools.hpp" 29 | 30 | namespace ngraph::runtime::he { 31 | 32 | TEST(protobuf, serialize_cipher) { 33 | pb::TCPMessage message; 34 | 35 | pb::Function f; 36 | f.set_function("123"); 37 | *message.mutable_function() = f; 38 | 39 | std::stringstream s; 40 | message.SerializeToOstream(&s); 41 | 42 | pb::TCPMessage deserialize; 43 | deserialize.ParseFromIstream(&s); 44 | 45 | EXPECT_TRUE( 46 | google::protobuf::util::MessageDifferencer::Equals(deserialize, message)); 47 | } 48 | 49 | } // namespace ngraph::runtime::he 50 | -------------------------------------------------------------------------------- /test/test_result.in.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #include "he_op_annotations.hpp" 18 | #include "ngraph/ngraph.hpp" 19 | #include "seal/he_seal_backend.hpp" 20 | #include "test_util.hpp" 21 | #include "util/all_close.hpp" 22 | #include "util/ndarray.hpp" 23 | #include "util/test_control.hpp" 24 | #include "util/test_tools.hpp" 25 | 26 | static const char* s_manifest = "${MANIFEST}"; 27 | 28 | namespace ngraph::runtime::he { 29 | 30 | auto result_test = [](const bool input_encrypted, const bool output_encrypted) { 31 | auto backend = runtime::Backend::create("${BACKEND_NAME}"); 32 | auto he_backend = static_cast(backend.get()); 33 | 34 | Shape shape{2, 3}; 35 | 36 | auto a = std::make_shared(element::f32, shape); 37 | auto t = std::make_shared(a); 38 | auto f = std::make_shared(t, ParameterVector{a}); 39 | 40 | const auto& config = test::config_from_flags(false, input_encrypted, false); 41 | 42 | std::string error_str; 43 | he_backend->set_config({{a->get_name(), config}}, error_str); 44 | 45 | auto t_a = 46 | test::tensor_from_flags(*he_backend, shape, input_encrypted, false); 47 | auto t_result = 48 | test::tensor_from_flags(*he_backend, shape, output_encrypted, false); 49 | 50 | std::vector input_a{-2, -1, 0, 1, 2, 3}; 51 | std::vector exp_result{0, 0, 0, 1, 2, 3}; 52 | copy_data(t_a, input_a); 53 | 54 | auto handle = backend->compile(f); 55 | handle->call_with_validate({t_result}, {t_a}); 56 | EXPECT_TRUE(test::all_close(read_vector(t_result), exp_result, 1e-3f)); 57 | }; 58 | 59 | NGRAPH_TEST(${BACKEND_NAME}, result_cipher_to_cipher) { 60 | result_test(true, true); 61 | } 62 | 63 | NGRAPH_TEST(${BACKEND_NAME}, result_cipher_to_plain) { 64 | result_test(true, false); 65 | } 66 | 67 | NGRAPH_TEST(${BACKEND_NAME}, result_plain_to_cipher) { 68 | result_test(true, false); 69 | } 70 | 71 | NGRAPH_TEST(${BACKEND_NAME}, result_plain_to_plain) { 72 | result_test(true, false); 73 | } 74 | 75 | } // namespace ngraph::runtime::he 76 | -------------------------------------------------------------------------------- /test/test_seal_plaintext_wrapper.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #include 18 | #include 19 | 20 | #include "gtest/gtest.h" 21 | #include "ngraph/type/element_type.hpp" 22 | #include "seal/he_seal_backend.hpp" 23 | #include "seal/seal_plaintext_wrapper.hpp" 24 | #include "test_util.hpp" 25 | 26 | namespace ngraph::runtime::he { 27 | 28 | TEST(seal_plaintext_wrapper, initialize) { 29 | // Default plaintext 30 | { 31 | auto plain_wrapper = SealPlaintextWrapper(true); 32 | EXPECT_TRUE(plain_wrapper.complex_packing()); 33 | 34 | plain_wrapper.complex_packing() = false; 35 | EXPECT_FALSE(plain_wrapper.complex_packing()); 36 | } 37 | // Passed plaintext 38 | { 39 | seal::Plaintext plain(100); 40 | auto plain_wrapper = SealPlaintextWrapper(plain, true); 41 | EXPECT_EQ(plain, plain_wrapper.plaintext()); 42 | } 43 | } 44 | 45 | } // namespace ngraph::runtime::he 46 | -------------------------------------------------------------------------------- /test/test_tcp_message.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************** 2 | // Copyright 2018-2020 Intel Corporation 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | //***************************************************************************** 16 | 17 | #include 18 | 19 | #include 20 | #include 21 | 22 | #include "gtest/gtest.h" 23 | #include "protos/message.pb.h" 24 | #include "seal/he_seal_backend.hpp" 25 | #include "seal/seal.h" 26 | #include "seal/seal_ciphertext_wrapper.hpp" 27 | #include "tcp/tcp_message.hpp" 28 | #include "util/test_tools.hpp" 29 | 30 | namespace ngraph::runtime::he { 31 | 32 | TEST(tcp_message, decode_small) { 33 | TCPMessage::data_buffer buffer; 34 | EXPECT_EQ(TCPMessage::decode_header(buffer), 0); 35 | } 36 | 37 | TEST(tcp_message, create) { 38 | pb::TCPMessage pb_msg; 39 | pb::Function f; 40 | f.set_function("123"); 41 | *pb_msg.mutable_function() = f; 42 | std::stringstream s; 43 | pb_msg.SerializeToOstream(&s); 44 | pb::TCPMessage tcp_message(std::move(pb_msg)); 45 | EXPECT_EQ(1, 1); 46 | } 47 | 48 | TEST(tcp_message, encode_decode) { 49 | using data_buffer = std::vector; 50 | data_buffer buffer; 51 | buffer.resize(20); 52 | 53 | size_t encode_size = 10; 54 | TCPMessage::encode_header(buffer, encode_size); 55 | size_t decoded_size = TCPMessage::decode_header(buffer); 56 | EXPECT_EQ(decoded_size, encode_size); 57 | } 58 | 59 | TEST(tcp_message, pack_unpack) { 60 | using data_buffer = std::vector; 61 | 62 | pb::TCPMessage pb_msg; 63 | pb::Function f; 64 | f.set_function("123"); 65 | *pb_msg.mutable_function() = f; 66 | std::stringstream s; 67 | pb_msg.SerializeToOstream(&s); 68 | TCPMessage message1(std::move(pb_msg)); 69 | 70 | data_buffer buffer; 71 | message1.pack(buffer); 72 | 73 | TCPMessage message2; 74 | message2.unpack(buffer); 75 | 76 | EXPECT_TRUE(google::protobuf::util::MessageDifferencer::Equals( 77 | *message1.pb_message(), *message2.pb_message())); 78 | } 79 | 80 | } // namespace ngraph::runtime::he 81 | -------------------------------------------------------------------------------- /tools/coverage/README.md: -------------------------------------------------------------------------------- 1 | # Code coverage 2 | 3 | See (here)[https://alastairs-place.net/blog/2016/05/20/code-coverage-from-the-command-line-with-clang/] for a brief introduction to the different profiling options. 4 | 5 | ```bash 6 | # Enable coverage compile options 7 | cmake .. -DCMAKE_CXX_COMPILER=clang++-9 -DCMAKE_INSTALL_PREFIX=~/mylibs -DCMAKE_C_COMPILER=clang-9 -DNGRAPH_HE_CODE_COVERAGE=ON -DNGRAPH_HE_CLANG_TIDY=OFF 8 | 9 | 10 | # Create raw coverage file 11 | # Use verbose logging to increase coverage 12 | LLVM_PROFILE_FILE="unit_test.profraw" NGRAPH_HE_LOG_LEVEL=5 NGRAPH_HE_VERBOSE_OPS=all ./test/unit-test 13 | 14 | # Convert to format llvm-cov wants 15 | llvm-profdata-9 merge -o testcov.profdata unit_test.profraw 16 | 17 | # Generate coverage report for just he-transformer src files 18 | llvm-cov-9 report src/libhe_seal_backend.so --use-color=1 --instr-profile=testcov.profdata ../src/* > coverage.report 19 | 20 | # Generate html coverage report 21 | llvm-cov-9 show src/libhe_seal_backend.so --instr-profile=testcov.profdata ../src/* -Xdemangler=c++filt --format=html > cov.html 22 | --------------------------------------------------------------------------------