├── .bazelversion ├── spu ├── exported_symbols.lds ├── version_script.lds ├── ops │ └── __init__.py ├── utils │ ├── __init__.py │ ├── polyfill.py │ └── distributed.proto ├── spu_pb2.py ├── version.py ├── experimental │ └── __init__.py ├── tests │ ├── data │ │ └── BUILD.bazel │ ├── jnp_aby3_r64_test.py │ ├── jnp_aby3_r128_test.py │ ├── jnp_ref2k_r64_test.py │ ├── jnp_semi2k_r64_test.py │ ├── jnp_semi2k_r128_test.py │ └── jnp_cheetah_r64_test.py └── intrinsic │ ├── __init__.py │ └── README.md ├── examples └── python │ ├── ml │ ├── flax_vit │ │ ├── .OPENSOURCE-CLEANUP-RMDIR │ │ ├── requirements.txt │ │ ├── README.md │ │ └── BUILD.bazel │ ├── README.md │ ├── requirements.txt │ ├── flax_bert │ │ ├── README.md │ │ └── BUILD.bazel │ └── flax_gpt2 │ │ ├── README.md │ │ └── BUILD.bazel │ ├── imagenet │ ├── n01440764_tench.JPEG │ ├── n01531178_goldfinch.JPEG │ ├── n01737021_water_snake.JPEG │ └── BUILD.bazel │ ├── conf │ ├── ds_breast_cancer_basic.json │ ├── ds_mock_regression_basic.json │ └── BUILD.bazel │ ├── microbench │ └── README.md │ ├── ir_dump │ ├── README.md │ └── BUILD.bazel │ └── README.md ├── .github ├── ISSUE_TEMPLATE │ └── config.yml ├── pull_request_template.md └── workflows │ ├── black.yml │ ├── stale.yml │ ├── yaml-linter.yml │ ├── license-check.yml │ ├── buildifier.yml │ ├── clang-format-linter.yml │ ├── cla.yml │ └── publish.yml ├── docs ├── reference │ ├── change_log.rst │ ├── cpp_api.rst │ ├── pphlo_doc.rst │ ├── index.rst │ ├── py_api.rst │ ├── update_pphlo_doc.sh │ ├── update_np_op_status_doc.sh │ └── update_complexity_doc.sh ├── SPU_gudience.pdf ├── imgs │ ├── spu_arch.png │ ├── vm_arch.png │ ├── workflow.png │ ├── policy_sgd.png │ ├── sgd_problem.png │ ├── sgd_precision.png │ ├── policy_naive_auc.png │ ├── augmented_share_types.png │ └── policy_sgd_precision.png ├── _static │ ├── favicon.ico │ ├── logo-dark.png │ ├── logo-light.png │ ├── js │ │ └── custom.js │ └── css │ │ └── custom.css ├── getting_started │ ├── install.rst │ ├── issue.rst │ └── introduction.rst ├── tutorials │ ├── index.rst │ └── cpp_lr_example.rst ├── requirements.txt └── development │ └── index.rst ├── setup.cfg ├── requirements.txt ├── renovate.json ├── libspu ├── device │ ├── README.md │ └── pphlo │ │ └── pphlo_intrinsic_executor.h ├── compiler │ ├── tests │ │ ├── passes │ │ │ ├── hlo2pphlo │ │ │ │ ├── empty_function.mlir │ │ │ │ ├── dynamic_slice.mlir │ │ │ │ ├── complex_p.mlir │ │ │ │ ├── complex_s.mlir │ │ │ │ ├── tenary_ops_p.mlir │ │ │ │ ├── gather_s.mlir │ │ │ │ ├── sort_p.mlir │ │ │ │ ├── while_p.mlir │ │ │ │ ├── tenary_ops_s_1.mlir │ │ │ │ ├── tenary_ops_s_2.mlir │ │ │ │ ├── while_s.mlir │ │ │ │ └── sort_s.mlir │ │ │ └── optimizations │ │ │ │ ├── pphlo_simple_dealloc.mlir │ │ │ │ ├── optimize_denominator_with_bcst.mlir │ │ │ │ ├── reduce_truncation.mlir │ │ │ │ ├── convert_push_down.mlir │ │ │ │ ├── no_expand_secret_gather.mlir │ │ │ │ └── expand_secret_gather.mlir │ │ ├── interpret │ │ │ ├── test_json │ │ │ │ ├── popcnt.json │ │ │ │ ├── exponential_minus_one.json │ │ │ │ ├── round_afz.json │ │ │ │ ├── sqrt.json │ │ │ │ ├── log_plus_one.json │ │ │ │ ├── rsqrt.json │ │ │ │ ├── log.json │ │ │ │ ├── logistic.json │ │ │ │ ├── exponential.json │ │ │ │ ├── arshift.json │ │ │ │ ├── rshift.json │ │ │ │ ├── atan2.json │ │ │ │ ├── sign.json │ │ │ │ └── abs.json │ │ │ ├── broadcast.mlir │ │ │ ├── reverse.mlir │ │ │ ├── concatenate.mlir │ │ │ ├── clamp.mlir │ │ │ ├── ring_cast.mlir │ │ │ ├── slice.mlir │ │ │ ├── reduce.mlir │ │ │ ├── popcnt.mlir │ │ │ ├── pad.mlir │ │ │ ├── dynamic_update_slice.mlir │ │ │ ├── template │ │ │ │ └── basic_unary.template │ │ │ ├── sort.mlir │ │ │ └── exponential_minus_one.mlir │ │ └── lit.site.cfg.py.in │ ├── .clang-format │ ├── readme.md │ ├── utils │ │ ├── utils.h │ │ ├── utils.cc │ │ └── BUILD.bazel │ ├── compile.h │ ├── codegen │ │ ├── codegen.h │ │ ├── BUILD.bazel │ │ └── codegen.cc │ ├── core │ │ ├── BUILD.bazel │ │ └── core.h │ └── BUILD.bazel ├── core │ ├── parallel_utils.cc │ └── config.h ├── mpc │ ├── utils │ │ └── linalg.cc │ ├── standard_shape │ │ └── protocol.h │ ├── aby3 │ │ ├── arithmetic_gpu_ext.h │ │ ├── protocol.h │ │ ├── type.cc │ │ └── io_test.cc │ ├── cheetah │ │ ├── type.cc │ │ ├── protocol.h │ │ ├── nonlinear │ │ │ └── README.md │ │ ├── alg.h │ │ └── rlwe │ │ │ └── types.h │ ├── securenn │ │ ├── type.cc │ │ ├── protocol.h │ │ └── state.h │ ├── semi2k │ │ ├── type.cc │ │ ├── beaver │ │ │ └── beaver_impl │ │ │ │ └── trusted_party │ │ │ │ └── BUILD.bazel │ │ └── protocol.h │ ├── spdz2k │ │ ├── type.cc │ │ ├── protocol.h │ │ └── io_test.cc │ ├── ab_api_test.h │ └── tools │ │ └── complexity.proto ├── dialect │ ├── pphlo │ │ ├── IR │ │ │ ├── base_enums.cc │ │ │ ├── dialect.h │ │ │ ├── interface.h │ │ │ ├── canonicalization_patterns.td │ │ │ ├── base_enums.h │ │ │ └── interface.td │ │ └── transforms │ │ │ ├── pass_details.h │ │ │ └── register_passes.h │ └── utils │ │ ├── BUILD.bazel │ │ ├── utils.h │ │ └── utils.cc ├── version.h ├── kernel │ ├── hal │ │ ├── debug.h │ │ ├── complex.h │ │ ├── intrinsic │ │ │ └── nn │ │ │ │ └── activation.h │ │ └── type_cast.h │ ├── hlo │ │ ├── rand.h │ │ ├── rand.cc │ │ ├── shuffle.h │ │ ├── basic_ternary.h │ │ ├── casting.h │ │ ├── shift.h │ │ └── const.h │ └── BUILD.bazel └── cuda_support │ └── kernels.h ├── .clang-format ├── .vscode ├── extensions.json └── tasks.json ├── LEGAL.md ├── requirements-dev.txt ├── BUILD.bazel ├── sml ├── ensemble │ ├── BUILD.bazel │ ├── tests │ │ └── BUILD.bazel │ └── emulations │ │ └── BUILD.bazel ├── metrics │ └── classification │ │ └── __init__.py ├── neighbors │ ├── BUILD.bazel │ ├── emulations │ │ └── BUILD.bazel │ └── tests │ │ └── BUILD.bazel ├── tree │ ├── BUILD.bazel │ ├── emulations │ │ └── BUILD.bazel │ └── tests │ │ └── BUILD.bazel ├── cluster │ ├── BUILD.bazel │ ├── emulations │ │ └── BUILD.bazel │ └── tests │ │ └── BUILD.bazel ├── naive_bayes │ ├── BUILD.bazel │ ├── emulations │ │ └── BUILD.bazel │ └── tests │ │ └── BUILD.bazel ├── feature_selection │ ├── BUILD.bazel │ ├── emulations │ │ └── BUILD.bazel │ └── tests │ │ └── BUILD.bazel ├── preprocessing │ ├── BUILD.bazel │ ├── emulations │ │ └── BUILD.bazel │ └── tests │ │ └── BUILD.bazel ├── svm │ ├── emulations │ │ └── BUILD.bazel │ ├── BUILD.bazel │ └── tests │ │ └── BUILD.bazel ├── decomposition │ ├── BUILD.bazel │ └── tests │ │ └── BUILD.bazel ├── utils │ ├── tests │ │ └── BUILD.bazel │ ├── emulations │ │ └── BUILD.bazel │ └── BUILD.bazel ├── gaussian_process │ ├── tests │ │ └── BUILD.bazel │ ├── BUILD.bazel │ └── emulations │ │ └── BUILD.bazel └── linear_model │ └── utils │ └── BUILD.bazel ├── pyproject.toml ├── bazel ├── patches │ └── emp-tool-cmake.patch ├── local_openmp_macos.BUILD ├── xtl.BUILD ├── xtensor.BUILD ├── BUILD.bazel ├── nvidia_cutlass.BUILD └── emp-tool.BUILD ├── benchmark ├── run_bench.sh └── BUILD.bazel └── .circleci └── run-nn.sh /.bazelversion: -------------------------------------------------------------------------------- 1 | 6.5.0 2 | -------------------------------------------------------------------------------- /spu/exported_symbols.lds: -------------------------------------------------------------------------------- 1 | _PyInit_* -------------------------------------------------------------------------------- /examples/python/ml/flax_vit/.OPENSOURCE-CLEANUP-RMDIR: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: false 2 | -------------------------------------------------------------------------------- /examples/python/ml/flax_vit/requirements.txt: -------------------------------------------------------------------------------- 1 | transformers 2 | datasets -------------------------------------------------------------------------------- /docs/reference/change_log.rst: -------------------------------------------------------------------------------- 1 | .. include:: ../../CHANGELOG.md 2 | :parser: myst_parser.sphinx_ -------------------------------------------------------------------------------- /docs/SPU_gudience.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AntCPLab/OpenBumbleBee/HEAD/docs/SPU_gudience.pdf -------------------------------------------------------------------------------- /docs/imgs/spu_arch.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AntCPLab/OpenBumbleBee/HEAD/docs/imgs/spu_arch.png -------------------------------------------------------------------------------- /docs/imgs/vm_arch.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AntCPLab/OpenBumbleBee/HEAD/docs/imgs/vm_arch.png -------------------------------------------------------------------------------- /docs/imgs/workflow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AntCPLab/OpenBumbleBee/HEAD/docs/imgs/workflow.png -------------------------------------------------------------------------------- /docs/_static/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AntCPLab/OpenBumbleBee/HEAD/docs/_static/favicon.ico -------------------------------------------------------------------------------- /docs/getting_started/install.rst: -------------------------------------------------------------------------------- 1 | .. include:: ../../INSTALLATION.md 2 | :parser: myst_parser.sphinx_ 3 | -------------------------------------------------------------------------------- /docs/imgs/policy_sgd.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AntCPLab/OpenBumbleBee/HEAD/docs/imgs/policy_sgd.png -------------------------------------------------------------------------------- /docs/imgs/sgd_problem.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AntCPLab/OpenBumbleBee/HEAD/docs/imgs/sgd_problem.png -------------------------------------------------------------------------------- /docs/_static/logo-dark.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AntCPLab/OpenBumbleBee/HEAD/docs/_static/logo-dark.png -------------------------------------------------------------------------------- /docs/_static/logo-light.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AntCPLab/OpenBumbleBee/HEAD/docs/_static/logo-light.png -------------------------------------------------------------------------------- /docs/imgs/sgd_precision.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AntCPLab/OpenBumbleBee/HEAD/docs/imgs/sgd_precision.png -------------------------------------------------------------------------------- /docs/imgs/policy_naive_auc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AntCPLab/OpenBumbleBee/HEAD/docs/imgs/policy_naive_auc.png -------------------------------------------------------------------------------- /docs/imgs/augmented_share_types.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AntCPLab/OpenBumbleBee/HEAD/docs/imgs/augmented_share_types.png -------------------------------------------------------------------------------- /docs/imgs/policy_sgd_precision.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AntCPLab/OpenBumbleBee/HEAD/docs/imgs/policy_sgd_precision.png -------------------------------------------------------------------------------- /examples/python/imagenet/n01440764_tench.JPEG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AntCPLab/OpenBumbleBee/HEAD/examples/python/imagenet/n01440764_tench.JPEG -------------------------------------------------------------------------------- /examples/python/imagenet/n01531178_goldfinch.JPEG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AntCPLab/OpenBumbleBee/HEAD/examples/python/imagenet/n01531178_goldfinch.JPEG -------------------------------------------------------------------------------- /examples/python/imagenet/n01737021_water_snake.JPEG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AntCPLab/OpenBumbleBee/HEAD/examples/python/imagenet/n01737021_water_snake.JPEG -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [pep8] 2 | max-line-length = 80 3 | 4 | [pycodestyle] 5 | max-line-length = 80 6 | 7 | [yapf] 8 | based_on_style = pep8 9 | column_limit = 80 10 | -------------------------------------------------------------------------------- /examples/python/ml/README.md: -------------------------------------------------------------------------------- 1 | # Examples 2 | 3 | This directory contains examples demonstrating how to use SPU to write privacy-preserving machine learning programs. 4 | -------------------------------------------------------------------------------- /spu/version_script.lds: -------------------------------------------------------------------------------- 1 | VERS_1.0 { 2 | # Export symbols in pybind. 3 | global: 4 | PyInit_*; 5 | 6 | # Hide everything else. 7 | local: 8 | *; 9 | }; 10 | -------------------------------------------------------------------------------- /examples/python/ml/requirements.txt: -------------------------------------------------------------------------------- 1 | dm-haiku 2 | plotnine 3 | jraph 4 | optax 5 | torch==2.3.0 6 | torch_xla==2.3.0 7 | torchvision 8 | jax[cpu] 9 | tensorflow_datasets 10 | keras 11 | -------------------------------------------------------------------------------- /examples/python/conf/ds_breast_cancer_basic.json: -------------------------------------------------------------------------------- 1 | { 2 | "use_mock_data": false, 3 | "problem_type": "classification", 4 | "builtin_dataset_name": "breast_cancer", 5 | "left_slice_feature_ratio": 0.5 6 | } -------------------------------------------------------------------------------- /examples/python/microbench/README.md: -------------------------------------------------------------------------------- 1 | This directory contains examples demonstrating how to benchmark the proposed building blocks in BumbleBee, including the matrix multipliaction, softmax, and piecewise activations. 2 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | grpcio>=1.42.0,!=1.48.0 2 | numpy>=1.22.0, < 2 3 | protobuf>=4, <5 4 | cloudpickle>=2.0.0 5 | multiprocess>=0.70.12.2 6 | cachetools>=5.0.0 7 | jax[cpu]>=0.4.16, <=0.4.26 # FIXME 8 | termcolor>=2.0.0 9 | -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | # Pull Request 2 | 3 | ## What problem does this PR solve? 4 | 5 | Issue Number: Fixed # 6 | 7 | ## Possible side effects? 8 | 9 | - Performance: 10 | 11 | - Backward compatibility: 12 | -------------------------------------------------------------------------------- /docs/getting_started/issue.rst: -------------------------------------------------------------------------------- 1 | Reporting an Issue 2 | ================== 3 | 4 | Please create an issue at `Github Issues `_. 5 | 6 | We will look into issues and get back to you soon. 7 | 8 | -------------------------------------------------------------------------------- /examples/python/conf/ds_mock_regression_basic.json: -------------------------------------------------------------------------------- 1 | { 2 | "use_mock_data": true, 3 | "n_samples": 500000, 4 | "n_features": 100, 5 | "problem_type": "classification", 6 | "random_seed": 9237, 7 | "hardness": 0.1, 8 | "left_slice_feature_ratio": 0.5 9 | } -------------------------------------------------------------------------------- /.github/workflows/black.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Python Linter 3 | on: 4 | push: 5 | branches: 6 | - main 7 | pull_request: 8 | branches: 9 | - main 10 | permissions: 11 | contents: read 12 | jobs: 13 | python-linter: 14 | uses: secretflow/.github/.github/workflows/python-linter.yml@main 15 | -------------------------------------------------------------------------------- /.github/workflows/stale.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Mark stale issues and pull requests 3 | on: 4 | workflow_dispatch: 5 | schedule: 6 | - cron: 40 9 * * * 7 | permissions: 8 | pull-requests: write 9 | issues: write 10 | jobs: 11 | stale: 12 | uses: secretflow/.github/.github/workflows/stale.yml@main 13 | -------------------------------------------------------------------------------- /.github/workflows/yaml-linter.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Yaml Lint 3 | on: 4 | push: 5 | branches: 6 | - main 7 | pull_request: 8 | branches: 9 | - main 10 | permissions: 11 | contents: read 12 | jobs: 13 | yaml-linter: 14 | uses: secretflow/.github/.github/workflows/yaml-linter.yml@main 15 | -------------------------------------------------------------------------------- /.github/workflows/license-check.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: License Check 3 | on: 4 | push: 5 | branches: 6 | - main 7 | pull_request: 8 | branches: 9 | - main 10 | permissions: 11 | contents: read 12 | jobs: 13 | license-checker: 14 | uses: secretflow/.github/.github/workflows/license-check.yml@main 15 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://docs.renovatebot.com/renovate-schema.json", 3 | "extends": [ 4 | "config:recommended" 5 | ], 6 | "labels": [ 7 | "dependencies" 8 | ], 9 | "ignoreDeps": [ 10 | "bazel", 11 | "protobuf" 12 | ], 13 | "reviewers": [ 14 | "team:spu-dev" 15 | ] 16 | } 17 | -------------------------------------------------------------------------------- /.github/workflows/buildifier.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bazel files linter 3 | on: 4 | push: 5 | branches: 6 | - main 7 | pull_request: 8 | branches: 9 | - main 10 | permissions: 11 | contents: read 12 | jobs: 13 | bazel-formatting-check: 14 | uses: secretflow/.github/.github/workflows/bazel-linter.yml@main 15 | -------------------------------------------------------------------------------- /.github/workflows/clang-format-linter.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Run clang-format Linter 3 | on: 4 | push: 5 | branches: 6 | - main 7 | pull_request: 8 | branches: 9 | - main 10 | permissions: 11 | contents: read 12 | jobs: 13 | run-clang-format: 14 | uses: secretflow/.github/.github/workflows/clang-format.yml@main 15 | -------------------------------------------------------------------------------- /.github/workflows/cla.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: CLA Assistant 3 | on: 4 | issue_comment: 5 | types: [created] 6 | pull_request_target: 7 | types: [opened, closed, synchronize] 8 | permissions: 9 | pull-requests: write 10 | jobs: 11 | CLAssistant: 12 | uses: secretflow/.github/.github/workflows/cla.yml@main 13 | secrets: inherit 14 | -------------------------------------------------------------------------------- /libspu/device/README.md: -------------------------------------------------------------------------------- 1 | # SPU Device: A MLIR-PPHLO virtual device 2 | 3 | This implements an interpreter for MLIR-PPHLO. 4 | 5 | Coding practice and conventions in this repository follow the [MLIR Developer Guide](https://mlir.llvm.org/getting_started/DeveloperGuide/) 6 | in this repo as part of the intent to act as an incubator for technology to upstream. 7 | -------------------------------------------------------------------------------- /docs/reference/cpp_api.rst: -------------------------------------------------------------------------------- 1 | C++ API Reference 2 | ================= 3 | 4 | This part is not ready. Please check later. 5 | 6 | .. note:: 7 | For low-level access, you can also use SPU's *C++ API*, but C++ API is **not guaranteed to be stable**. 8 | For more stable API, consider using :ref:`frontend API `. 9 | 10 | -------------------------------------------------------------------------------- /docs/tutorials/index.rst: -------------------------------------------------------------------------------- 1 | Tutorials 2 | ========= 3 | 4 | .. note:: 5 | We will add more tutorials here! At this moment, please check `examples `_ folder of source code on you own. 6 | 7 | 8 | .. toctree:: 9 | :maxdepth: 1 10 | 11 | quick_start 12 | spu_inside 13 | cpp_lr_example 14 | develop_your_first_mpc_application 15 | -------------------------------------------------------------------------------- /.clang-format: -------------------------------------------------------------------------------- 1 | # Use the Google style in this project. 2 | BasedOnStyle: Google 3 | 4 | IncludeBlocks: Regroup 5 | IncludeCategories: 6 | - Regex: '^<.*\.h>' 7 | Priority: 1 8 | - Regex: "^<.*" 9 | Priority: 2 10 | - Regex: '.*\.pb\.h"$' 11 | Priority: 6 12 | - Regex: '^"libspu.*' 13 | Priority: 4 14 | - Regex: '^"psi.*' 15 | Priority: 5 16 | - Regex: '^".*' 17 | Priority: 3 18 | -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": [ 3 | "BazelBuild.vscode-bazel", 4 | "eamodio.gitlens", 5 | "matepek.vscode-catch2-test-adapter", 6 | "ms-python.python", 7 | "ms-vscode.cpptools", 8 | "jgclark.vscode-todo-highlight", 9 | "ms-vscode-remote.remote-ssh", 10 | "zxh404.vscode-proto3", 11 | "llvm-vs-code-extensions.vscode-clangd", 12 | ] 13 | } -------------------------------------------------------------------------------- /LEGAL.md: -------------------------------------------------------------------------------- 1 | # Legal Disclaimer 2 | 3 | Within this source code, the comments in Chinese shall be the original, governing version. 4 | Any comment in other languages are for reference only. 5 | In the event of any conflict between the Chinese language version comments and other 6 | language version comments, the Chinese language version shall prevail. 7 | 8 | 法律免责声明 9 | 10 | 关于代码注释部分,中文注释为官方版本,其它语言注释仅做参考。中文注释可能与其它语言注释存在不一致,当中文注释与其它语言注释存在不一致时,请以中文注释为准。 11 | -------------------------------------------------------------------------------- /docs/reference/pphlo_doc.rst: -------------------------------------------------------------------------------- 1 | PPHlo API reference 2 | =================== 3 | 4 | PPHlo is short for (SPU High level ops), it's the assembly language of SPU. 5 | 6 | PPHlo is built on `MLIR `_ infrastructure, the concrete ops definition could be found :spu_code_host:`here `. 7 | 8 | Op List 9 | ~~~~~~~ 10 | 11 | .. include:: pphlo_op_doc.md 12 | :parser: myst_parser.sphinx_ 13 | -------------------------------------------------------------------------------- /libspu/compiler/tests/passes/hlo2pphlo/empty_function.mlir: -------------------------------------------------------------------------------- 1 | // RUN: spu-opt --hlo-legalize-to-pphlo=input_vis_list=VIS_SECRET,VIS_SECRET --lower-conversion-cast %s --split-input-file | FileCheck %s 2 | 3 | func.func @main(%arg0: tensor<10xf64>, %arg1: tensor<10xf64>) -> (tensor<10xf64>, tensor<10xf64>) { 4 | // CHECK: return %arg0, %arg1 : tensor<10x!pphlo.secret>, tensor<10x!pphlo.secret> 5 | return %arg0, %arg1 : tensor<10xf64>, tensor<10xf64> 6 | } -------------------------------------------------------------------------------- /docs/requirements.txt: -------------------------------------------------------------------------------- 1 | myst-parser==3.0.1 2 | rstcheck==6.2.1 3 | sphinx==7.3.7 4 | nbsphinx==0.9.4 5 | sphinx-autobuild==2024.4.16 6 | sphinx-markdown-parser==0.2.4 7 | sphinxcontrib-actdiag==3.0.0 8 | sphinxcontrib-blockdiag==3.0.0 9 | sphinxcontrib-mermaid==0.9.2 10 | sphinxcontrib-nwdiag==2.0.0 11 | sphinxcontrib-seqdiag==3.0.0 12 | pytablewriter==1.2.0 13 | linkify-it-py==2.0.3 14 | mdutils==1.6.0 15 | spu>=0.3.1b0 16 | pydata-sphinx-theme 17 | tabulate 18 | xlwt 19 | xlsxwriter 20 | -------------------------------------------------------------------------------- /libspu/compiler/.clang-format: -------------------------------------------------------------------------------- 1 | # Use LLVM style for compiler related code 2 | BasedOnStyle: LLVM 3 | AlwaysBreakTemplateDeclarations: Yes 4 | 5 | IncludeBlocks: Regroup 6 | IncludeCategories: 7 | - Regex: '^<.*\.h>' 8 | Priority: 1 9 | - Regex: '^<.*' 10 | Priority: 2 11 | - Regex: '.*\.pb\.h"$' 12 | Priority: 5 13 | - Regex: '^"libspu.*' 14 | Priority: 4 15 | - Regex: '^".*' 16 | Priority: 3 17 | -------------------------------------------------------------------------------- /libspu/compiler/readme.md: -------------------------------------------------------------------------------- 1 | # MLIR-PPHLO: A MLIR based Secure HLO compiler 2 | 3 | This implements a self-contained compiler for a privacy preserving linear algebra set of operations inspired by XLA HLO IR using MLIR components. 4 | It is designed to provide an compiler engine for SPU. 5 | 6 | Coding practice and conventions in this repository follow the [MLIR Developer Guide](https://mlir.llvm.org/getting_started/DeveloperGuide/) in 7 | this repo as part of the intent to act as an incubator for technology to upstream. 8 | -------------------------------------------------------------------------------- /examples/python/ml/flax_bert/README.md: -------------------------------------------------------------------------------- 1 | # Bumblebee's Flax BERT Example 2 | 3 | 1. Install huggingface transformers library 4 | 5 | ```sh 6 | pip install 'transformers[flax]' 7 | ``` 8 | 9 | 2. Launch SPU backend runtime 10 | 11 | ```sh 12 | bazel run -c opt //examples/python/utils:nodectl -- --config `pwd`/examples/python/conf/2pc.json up 13 | ``` 14 | 15 | 3. Run `flax_gpt2` example 16 | 17 | ```sh 18 | bazel run -c opt //examples/python/ml/flax_bert -- --config `pwd`/examples/python/conf/2pc.json 19 | 20 | ``` 21 | -------------------------------------------------------------------------------- /docs/development/index.rst: -------------------------------------------------------------------------------- 1 | .. _development: 2 | 3 | Development 4 | =========== 5 | 6 | This part contains design principles of SPU. 7 | 8 | .. toctree:: 9 | :maxdepth: 1 10 | :caption: Design of SPU 11 | 12 | basic_concepts 13 | Pipeline 14 | Compiler 15 | Runtime 16 | type_system 17 | 18 | .. toctree:: 19 | :maxdepth: 1 20 | :caption: Advanced Topics 21 | 22 | ir_dump 23 | policy_sgd_insight 24 | fxp 25 | 26 | .. toctree:: 27 | :maxdepth: 1 28 | :caption: Extending SPU 29 | 30 | add_protocols 31 | -------------------------------------------------------------------------------- /requirements-dev.txt: -------------------------------------------------------------------------------- 1 | # basic deps 2 | grpcio==1.66.0 3 | numpy>=1.22.0, <2 # FIXME: for SF compatibility 4 | protobuf==5.27.3 5 | cloudpickle>=2.0.0 6 | multiprocess>=0.70.12.2 7 | cachetools>=5.0.0 8 | jax[cpu]==0.4.26 9 | termcolor>=2.0.0 10 | pandas>=1.4.2 11 | scikit-learn<1.6.0 12 | # LLVM 13 | transformers[cpu]==4.31.0 14 | # for tests 15 | absl-py>=1.1.0 16 | tensorflow-cpu==2.18.0; sys_platform == "linux" and platform_machine == 'x86_64' 17 | tensorflow==2.16.2; sys_platform != "linux" or platform_machine != 'x86_64' 18 | h5py!=3.11.0; platform_machine == 'aarch64' 19 | -------------------------------------------------------------------------------- /libspu/compiler/tests/passes/hlo2pphlo/dynamic_slice.mlir: -------------------------------------------------------------------------------- 1 | // RUN: spu-opt --hlo-legalize-to-pphlo=input_vis_list=VIS_PUBLIC,VIS_SECRET --lower-conversion-cast --split-input-file %s | FileCheck %s 2 | 3 | func.func @main(%arg0: tensor<15xi32>,%arg1: tensor) -> (tensor<1xi32>) { 4 | // CHECK: %0 = pphlo.dynamic_slice %arg0, %arg1 sizes = [1] : (tensor<15xi32>, tensor>) -> tensor<1x!pphlo.secret> 5 | %0 = "stablehlo.dynamic_slice"(%arg0, %arg1) {slice_sizes = array} : (tensor<15xi32>, tensor) -> tensor<1xi32> 6 | return %0 : tensor<1xi32> 7 | } 8 | -------------------------------------------------------------------------------- /docs/reference/index.rst: -------------------------------------------------------------------------------- 1 | .. _reference: 2 | 3 | Reference 4 | ========= 5 | 6 | This part contains detailed explanation of background, configs and APIs. 7 | 8 | 9 | .. toctree:: 10 | :maxdepth: 1 11 | :caption: API 12 | 13 | py_api 14 | cpp_api 15 | 16 | .. toctree:: 17 | :maxdepth: 1 18 | :caption: Config 19 | 20 | runtime_config 21 | 22 | 23 | .. toctree:: 24 | :maxdepth: 1 25 | :caption: Status 26 | 27 | np_op_status 28 | xla_status 29 | pphlo_doc 30 | mpc_status 31 | 32 | .. toctree:: 33 | :maxdepth: 1 34 | :caption: Misc. 35 | 36 | change_log 37 | -------------------------------------------------------------------------------- /libspu/compiler/tests/interpret/test_json/popcnt.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "popcnt", 3 | "template": "basic_unary", 4 | "testcases": [ 5 | { 6 | "inputs": [ 7 | { 8 | "data": "[0, 1, 2, 127]", 9 | "shape": "4", 10 | "dtype": "i64" 11 | } 12 | ], 13 | "expected": [ 14 | { 15 | "data": "[0, 1, 1, 7]", 16 | "shape": "4", 17 | "dtype": "i64" 18 | } 19 | ] 20 | } 21 | ] 22 | } -------------------------------------------------------------------------------- /BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Ant Group Co., Ltd. 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 | -------------------------------------------------------------------------------- /libspu/compiler/tests/passes/optimizations/pphlo_simple_dealloc.mlir: -------------------------------------------------------------------------------- 1 | // RUN: spu-opt --insert-deallocation --split-input-file %s | FileCheck %s 2 | 3 | func.func @main() -> (tensor) { 4 | %0 = pphlo.constant dense<0xFF800000> : tensor 5 | %1 = pphlo.constant dense<0.000000e+00> : tensor 6 | %2 = pphlo.less %0, %1: (tensor, tensor) -> tensor 7 | %3 = pphlo.select %2, %0, %1: (tensor, tensor, tensor) -> tensor 8 | //CHECK: pphlo.free %1 9 | //CHECK: pphlo.free %0 10 | //CHECK: pphlo.free %2 11 | //CHECK-NOT: pphlo.free %3 12 | return %3: tensor 13 | } 14 | -------------------------------------------------------------------------------- /spu/ops/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Ant Group Co., Ltd. 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 | -------------------------------------------------------------------------------- /spu/utils/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Ant Group Co., Ltd. 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 | -------------------------------------------------------------------------------- /sml/ensemble/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | -------------------------------------------------------------------------------- /sml/ensemble/tests/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | -------------------------------------------------------------------------------- /sml/ensemble/emulations/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | -------------------------------------------------------------------------------- /sml/metrics/classification/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [tool.black] 2 | skip-string-normalization = true 3 | 4 | [tool.isort] 5 | profile = "black" 6 | skip_glob = ["external/*", ".cache/*", "bazel-*/*"] 7 | 8 | [tool.rstcheck] 9 | report_level = "ERROR" 10 | ignore_directives = [ 11 | "include", 12 | "mermaid", 13 | "autoclass", 14 | "autofunction", 15 | ] 16 | ignore_roles = [ 17 | "spu_code_host", 18 | ] 19 | ignore_languages = [ 20 | "cpp" 21 | ] 22 | 23 | [tool.pyright] 24 | include = [ 25 | "spu", 26 | "sml", 27 | "examples", 28 | ] 29 | 30 | reportMissingImports = true 31 | reportMissingTypeStubs = false 32 | 33 | pythonVersion = "3.9" 34 | pythonPlatform = "Linux" 35 | -------------------------------------------------------------------------------- /spu/spu_pb2.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Ant Group Co., Ltd. 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 | from libspu.spu_pb2 import * 16 | -------------------------------------------------------------------------------- /docs/reference/py_api.rst: -------------------------------------------------------------------------------- 1 | Python API Reference 2 | ==================== 3 | 4 | Python API is used to control & access SPU, for example, to do data infeed/outfeed, to compile an XLA program to PPHlo, or to fire a PPHlo on an SPU runtime. 5 | 6 | 7 | Runtime Setup 8 | ------------- 9 | 10 | .. autoclass:: spu.Runtime 11 | :members: 12 | 13 | 14 | Runtime IO 15 | ---------- 16 | 17 | .. autoclass:: spu.Io 18 | :members: 19 | 20 | Compiler 21 | -------- 22 | 23 | .. autofunction:: spu.compile 24 | 25 | Simulation 26 | ---------- 27 | 28 | .. autoclass:: spu.utils.simulation.Simulator 29 | :members: 30 | :undoc-members: 31 | 32 | .. autofunction:: spu.utils.simulation.sim_jax 33 | -------------------------------------------------------------------------------- /libspu/compiler/tests/interpret/test_json/exponential_minus_one.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "exponential_minus_one", 3 | "template": "basic_unary", 4 | "testcases": [ 5 | { 6 | "inputs": [ 7 | { 8 | "data": "[0.0, 1.0]", 9 | "shape": "2", 10 | "dtype": "f64" 11 | } 12 | ], 13 | "expected": [ 14 | { 15 | "data": "[0.0, 1.7182818284590451]", 16 | "shape": "2", 17 | "dtype": "f64" 18 | } 19 | ], 20 | "checker": "expect_almost_eq" 21 | } 22 | ] 23 | } -------------------------------------------------------------------------------- /libspu/compiler/tests/interpret/test_json/round_afz.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "round_nearest_afz", 3 | "template": "basic_unary", 4 | "testcases": [ 5 | { 6 | "inputs": [ 7 | { 8 | "data": "[-2.5, 0.4, 0.5, 0.6, 2.5]", 9 | "shape": "5", 10 | "dtype": "f64" 11 | } 12 | ], 13 | "expected": [ 14 | { 15 | "data": "[-3.0, 0.0, 1.0, 1.0, 3.0]", 16 | "shape": "5", 17 | "dtype": "f64" 18 | } 19 | ], 20 | "checker": "expect_almost_eq" 21 | } 22 | ] 23 | } -------------------------------------------------------------------------------- /libspu/compiler/tests/interpret/broadcast.mlir: -------------------------------------------------------------------------------- 1 | // RUN: spu-translate --protocol_kind=1 --interpret -split-input-file %s 2 | // RUN: spu-translate --protocol_kind=2 --interpret -split-input-file %s 3 | // RUN: spu-translate --protocol_kind=3 --interpret -split-input-file %s 4 | 5 | func.func @broadcast_in_dim() { 6 | %operand = pphlo.constant dense<[[1], [2], [3]]> : tensor<3x1xi64> 7 | %result = pphlo.broadcast %operand, dims = [0, 2] : (tensor<3x1xi64>) -> tensor<3x2x2xi64> 8 | %expected = pphlo.constant dense<[[[1, 1], [1, 1]], [[2, 2], [2, 2]], [[3, 3], [3, 3]]]> : tensor<3x2x2xi64> 9 | pphlo.custom_call @expect_eq(%result, %expected) : (tensor<3x2x2xi64>,tensor<3x2x2xi64>)->() 10 | func.return 11 | } 12 | -------------------------------------------------------------------------------- /libspu/compiler/tests/interpret/reverse.mlir: -------------------------------------------------------------------------------- 1 | // RUN: spu-translate --protocol_kind=1 --interpret -split-input-file %s 2 | // RUN: spu-translate --protocol_kind=2 --interpret -split-input-file %s 3 | // RUN: spu-translate --protocol_kind=3 --interpret -split-input-file %s 4 | 5 | func.func @reverse() { 6 | %operand = pphlo.constant dense<[[1, 2], [3, 4], [5, 6]]> : tensor<3x2xi64> 7 | %result = "pphlo.reverse"(%operand) { 8 | dimensions = array 9 | } : (tensor<3x2xi64>) -> tensor<3x2xi64> 10 | %expected = pphlo.constant dense<[[6, 5], [4, 3], [2, 1]]> : tensor<3x2xi64> 11 | pphlo.custom_call @expect_eq (%result, %expected) : (tensor<3x2xi64>,tensor<3x2xi64>)->() 12 | func.return 13 | } 14 | -------------------------------------------------------------------------------- /examples/python/ir_dump/README.md: -------------------------------------------------------------------------------- 1 | # How to dump IR to DAG in SPU 2 | 3 | In this directory, we provide an example of how to dump the IR (Intermediate Representation, generated by [XLA](https://www.tensorflow.org/xla/architecture)) 4 | of executed code to a DAG (Directed Acyclic Graph) that illistrates the computation graph. 5 | 6 | Please refer to [spu.proto](../../../libspu/spu.proto) for more descriptions of the compiler options for dumping IR. 7 | 8 | 1. Launch SPU backend runtime 9 | 10 | ``` shell 11 | bazel run -c opt //examples/python/utils:nodectl -- up 12 | ``` 13 | 14 | 2. Run `ir_dump` example 15 | 16 | ``` shell 17 | bazel run -c opt //examples/python/ir_dump:ir_dump 18 | ``` 19 | -------------------------------------------------------------------------------- /bazel/patches/emp-tool-cmake.patch: -------------------------------------------------------------------------------- 1 | diff --git a/CMakeLists.txt b/CMakeLists.txt 2 | index d9abb31..4c2c171 100755 3 | --- a/CMakeLists.txt 4 | +++ b/CMakeLists.txt 5 | @@ -56,11 +56,14 @@ find_package(OpenSSL REQUIRED) 6 | include_directories(${OPENSSL_INCLUDE_DIR}) 7 | 8 | 9 | -add_library(${NAME} SHARED ${sources}) 10 | +add_library(${NAME} STATIC ${sources}) 11 | 12 | install(DIRECTORY emp-tool DESTINATION include/) 13 | install(DIRECTORY cmake/ DESTINATION cmake/) 14 | install(TARGETS ${NAME} DESTINATION lib) 15 | 16 | -ENABLE_TESTING() 17 | -ADD_SUBDIRECTORY(test) 18 | +option(ENABLE_TESTS "Enable tests" OFF) 19 | +if (${ENABLE_TESTS}) 20 | + ENABLE_TESTING() 21 | + ADD_SUBDIRECTORY(test) 22 | +endif() 23 | -------------------------------------------------------------------------------- /libspu/compiler/tests/interpret/test_json/sqrt.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sqrt", 3 | "template": "basic_unary", 4 | "testcases": [ 5 | { 6 | "inputs": [ 7 | { 8 | "data": "[[0.0, 1.0], [4.0, 9.0]]", 9 | "shape": "2x2", 10 | "dtype": "f64" 11 | } 12 | ], 13 | "expected": [ 14 | { 15 | "data": "[[0.000000e+00, 1.000000e+00], [2.000000e+00, 3.000000e+00]]", 16 | "shape": "2x2", 17 | "dtype": "f64" 18 | } 19 | ], 20 | "checker": "expect_almost_eq" 21 | } 22 | ] 23 | } -------------------------------------------------------------------------------- /libspu/compiler/tests/passes/optimizations/optimize_denominator_with_bcst.mlir: -------------------------------------------------------------------------------- 1 | // RUN: spu-opt --optimize-denominator-with-broadcast --split-input-file %s | FileCheck %s 2 | 3 | func.func @main(%arg0: tensor<16x!pphlo.secret>, %arg1: tensor<16x10000x!pphlo.secret>) -> (tensor<16x10000x!pphlo.secret>) { 4 | //CHECK: %0 = pphlo.reciprocal %arg0 5 | //CHECK: %1 = pphlo.broadcast %0 6 | //CHECK: %2 = pphlo.multiply %arg1, %1 7 | //CHECK: return %2 8 | %0 = pphlo.broadcast %arg0, dims = [0] : (tensor<16x!pphlo.secret>) -> tensor<16x10000x!pphlo.secret> 9 | %1 = pphlo.divide %arg1, %0 : tensor<16x10000x!pphlo.secret> 10 | return %1 : tensor<16x10000x!pphlo.secret> 11 | } 12 | -------------------------------------------------------------------------------- /examples/python/README.md: -------------------------------------------------------------------------------- 1 | # Welcome to SPU python examples 2 | 3 | To run a certain example, we have to first specify the distributed environment with: 4 | 5 | > bazel run -c opt //examples/python/utils:nodectl -- up 6 | 7 | To use a specific layout configuration (i.e. change MPC protocol, change outsourcing/colocated), change the configuration file accordingly. 8 | 9 | > bazel run -c opt //examples/python/utils:nodectl -- -c examples/python/conf/2pc.json up 10 | 11 | Then please check the comment of each example to run. 12 | 13 | ## Examples for PSI/PIR 14 | 15 | Please check tests at this moment: 16 | 17 | - spu/tests/legacy_psi_test.py 18 | - spu/tests/psi_test.py 19 | - spu/tests/ub_psi_test.py 20 | - spu/tests/pir_test.py 21 | -------------------------------------------------------------------------------- /libspu/compiler/tests/interpret/test_json/log_plus_one.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "log_plus_one", 3 | "template": "basic_unary", 4 | "testcases": [ 5 | { 6 | "inputs": [ 7 | { 8 | "data": "[0.0, -0.999, 7.0, 6.38905621, 15.0]", 9 | "shape": "5", 10 | "dtype": "f64" 11 | } 12 | ], 13 | "expected": [ 14 | { 15 | "data": "[0.0, -6.90776825, 2.07944155, 2.0, 2.77258873]", 16 | "shape": "5", 17 | "dtype": "f64" 18 | } 19 | ], 20 | "checker": "expect_almost_eq" 21 | } 22 | ] 23 | } -------------------------------------------------------------------------------- /libspu/compiler/tests/interpret/test_json/rsqrt.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rsqrt", 3 | "template": "basic_unary", 4 | "testcases": [ 5 | { 6 | "inputs": [ 7 | { 8 | "data": "[[1.0, 4.0], [9.0, 25.0]]", 9 | "shape": "2x2", 10 | "dtype": "f64" 11 | } 12 | ], 13 | "expected": [ 14 | { 15 | "data": "[[1.000000e+00, 5.000000e-01], [0.33333333333333331, 2.000000e-01]]", 16 | "shape": "2x2", 17 | "dtype": "f64" 18 | } 19 | ], 20 | "checker": "expect_almost_eq" 21 | } 22 | ] 23 | } -------------------------------------------------------------------------------- /spu/version.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | from . import libspu # type: ignore 16 | 17 | __version__ = libspu._get_version() 18 | -------------------------------------------------------------------------------- /libspu/compiler/tests/interpret/test_json/log.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "log", 3 | "template": "basic_unary", 4 | "testcases": [ 5 | { 6 | "inputs": [ 7 | { 8 | "data": "[[1.0, 2.0], [3.0, 4.0]]", 9 | "shape": "2x2", 10 | "dtype": "f64" 11 | } 12 | ], 13 | "expected": [ 14 | { 15 | "data": "[[0.000000e+00, 0.69314718055994529], [1.0986122886681098, 1.3862943611198906]]", 16 | "shape": "2x2", 17 | "dtype": "f64" 18 | } 19 | ], 20 | "checker": "expect_almost_eq" 21 | } 22 | ] 23 | } -------------------------------------------------------------------------------- /benchmark/run_bench.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/bash 2 | # 3 | # Copyright 2023 Ant Group Co., Ltd. 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 | bazel-bin/benchmark/"$@" -c .circleci/benchmark.json 19 | -------------------------------------------------------------------------------- /libspu/compiler/tests/interpret/test_json/logistic.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "logistic", 3 | "template": "basic_unary", 4 | "testcases": [ 5 | { 6 | "inputs": [ 7 | { 8 | "data": "[[1.0, 2.0], [3.0, 4.0]]", 9 | "shape": "2x2", 10 | "dtype": "f64" 11 | } 12 | ], 13 | "expected": [ 14 | { 15 | "data": "[[0.73105857863000488, 0.88079707797788244],[0.95257412682243322, 0.98201379003790844]]", 16 | "shape": "2x2", 17 | "dtype": "f64" 18 | } 19 | ], 20 | "checker": "expect_almost_eq" 21 | } 22 | ] 23 | } -------------------------------------------------------------------------------- /examples/python/ml/flax_gpt2/README.md: -------------------------------------------------------------------------------- 1 | # Flax GPT2 Example 2 | 3 | This example demonstrates how to use SPU to run private inference on a pre-trained 4 | [GPT2](https://cdn.openai.com/better-language-models/language_models_are_unsupervised_multitask_learners.pdf) model. 5 | 6 | 1. Install huggingface transformers library 7 | 8 | ```sh 9 | pip install 'transformers[flax]' 10 | ``` 11 | 12 | 2. Launch SPU backend runtime 13 | 14 | ```sh 15 | bazel run -c opt //examples/python/utils:nodectl -- --config `pwd`/examples/python/ml/flax_gpt2/3pc.json up 16 | ``` 17 | 18 | 3. Run `flax_gpt2` example 19 | 20 | ```sh 21 | bazel run -c opt //examples/python/ml/flax_gpt2 -- --config `pwd`/examples/python/ml/flax_gpt2/3pc.json 22 | ``` 23 | -------------------------------------------------------------------------------- /libspu/core/parallel_utils.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Ant Group Co., Ltd. 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 | #include "libspu/core/parallel_utils.h" 16 | 17 | namespace spu {} // namespace spu 18 | -------------------------------------------------------------------------------- /libspu/compiler/tests/interpret/concatenate.mlir: -------------------------------------------------------------------------------- 1 | // RUN: spu-translate --protocol_kind=1 --interpret -split-input-file %s 2 | // RUN: spu-translate --protocol_kind=2 --interpret -split-input-file %s 3 | // RUN: spu-translate --protocol_kind=3 --interpret -split-input-file %s 4 | 5 | func.func @concatenate() { 6 | %input0 = pphlo.constant dense<[[1, 2], [3, 4], [5, 6]]> : tensor<3x2xi64> 7 | %input1 = pphlo.constant dense<[[7, 8]]> : tensor<1x2xi64> 8 | %result = pphlo.concatenate %input0, %input1 dim = 0 : (tensor<3x2xi64>, tensor<1x2xi64>) -> tensor<4x2xi64> 9 | %expected = pphlo.constant dense<[[1, 2], [3, 4] , [5, 6], [7, 8]]> : tensor<4x2xi64> 10 | pphlo.custom_call @expect_eq (%result, %expected) : (tensor<4x2xi64>,tensor<4x2xi64>)->() 11 | func.return 12 | } -------------------------------------------------------------------------------- /docs/_static/js/custom.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Ant Group Co., Ltd. 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 | $(document).ready(function () { 18 | $('a.external').attr('target', '_blank'); 19 | }); 20 | -------------------------------------------------------------------------------- /libspu/compiler/tests/interpret/test_json/exponential.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "exponential", 3 | "template": "basic_unary", 4 | "testcases": [ 5 | { 6 | "inputs": [ 7 | { 8 | "data": "[[0.0, 1.0], [2.0, 3.0]]", 9 | "shape": "2x2", 10 | "dtype": "f64" 11 | } 12 | ], 13 | "expected": [ 14 | { 15 | "data": "[[1.000000e+00, 2.7182818284590451], [7.3890560989306504, 20.085536923187668]]", 16 | "shape": "2x2", 17 | "dtype": "f64" 18 | } 19 | ], 20 | "checker": "expect_almost_eq", 21 | "tol": 0.4 22 | } 23 | ] 24 | } -------------------------------------------------------------------------------- /libspu/compiler/tests/interpret/clamp.mlir: -------------------------------------------------------------------------------- 1 | // RUN: spu-translate --protocol_kind=1 --interpret -split-input-file %s 2 | // RUN: spu-translate --protocol_kind=2 --interpret -split-input-file %s 3 | // RUN: spu-translate --protocol_kind=3 --interpret -split-input-file %s 4 | 5 | func.func @clamp_op_test_si64() { 6 | %min = pphlo.constant dense<[1, 5, -5]> : tensor<3xi64> 7 | %operand = pphlo.constant dense<[2, 3, -1]> : tensor<3xi64> 8 | %max = pphlo.constant dense<[3, 7, -3]> : tensor<3xi64> 9 | %result = pphlo.clamp %min, %operand, %max : (tensor<3xi64>, tensor<3xi64>, tensor<3xi64>) -> tensor<3xi64> 10 | %expected = pphlo.constant dense<[2, 5, -3]> : tensor<3xi64> 11 | pphlo.custom_call @expect_eq(%result, %expected) : (tensor<3xi64>,tensor<3xi64>)->() 12 | func.return 13 | } 14 | -------------------------------------------------------------------------------- /spu/utils/polyfill.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Ant Group Co., Ltd. 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 | import multiprocess 16 | 17 | # https://github.com/google/jax/issues/18852 18 | Process = multiprocess.get_context("forkserver").Process 19 | -------------------------------------------------------------------------------- /libspu/compiler/tests/interpret/test_json/arshift.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "shift_right_arithmetic", 3 | "template": "basic_binary", 4 | "testcases": [ 5 | { 6 | "inputs": [ 7 | { 8 | "data": "[-1, 0, 8]", 9 | "shape": "3", 10 | "dtype": "i64" 11 | }, 12 | { 13 | "data": "[1, 2, 3]", 14 | "shape": "3", 15 | "dtype": "i64" 16 | } 17 | ], 18 | "expected": [ 19 | { 20 | "data": "[-1, 0, 1]", 21 | "shape": "3", 22 | "dtype": "i64" 23 | } 24 | ] 25 | } 26 | ] 27 | } -------------------------------------------------------------------------------- /libspu/mpc/utils/linalg.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Ant Group Co., Ltd. 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 | #include "libspu/mpc/utils/linalg.h" 16 | 17 | namespace spu::mpc::linalg::detail {} // namespace spu::mpc::linalg::detail 18 | -------------------------------------------------------------------------------- /libspu/compiler/tests/interpret/test_json/rshift.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "shift_right_logical", 3 | "template": "basic_binary", 4 | "testcases": [ 5 | { 6 | "inputs": [ 7 | { 8 | "data": "[-1, 0, 8]", 9 | "shape": "3", 10 | "dtype": "i64" 11 | }, 12 | { 13 | "data": "[1, 2, 3]", 14 | "shape": "3", 15 | "dtype": "i64" 16 | } 17 | ], 18 | "expected": [ 19 | { 20 | "data": "[9223372036854775807, 0, 1]", 21 | "shape": "3", 22 | "dtype": "i64" 23 | } 24 | ] 25 | } 26 | ] 27 | } -------------------------------------------------------------------------------- /examples/python/imagenet/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Ant Group Co., Ltd. 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 | package(default_visibility = ["//visibility:public"]) 16 | 17 | filegroup( 18 | name = "imagenet", 19 | srcs = glob(["*.JPEG"]), 20 | ) 21 | -------------------------------------------------------------------------------- /libspu/dialect/pphlo/IR/base_enums.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Ant Group Co., Ltd. 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 | #include "libspu/dialect/pphlo/IR/base_enums.h" // IWYU pragma: keep 16 | 17 | #include "libspu/dialect/pphlo/IR/base_enums.cc.inc" 18 | -------------------------------------------------------------------------------- /libspu/compiler/tests/passes/optimizations/reduce_truncation.mlir: -------------------------------------------------------------------------------- 1 | // RUN: spu-opt --reduce-truncation --split-input-file %s | FileCheck %s 2 | 3 | func.func @main(%arg0: tensor<31x1xf32>, %arg1: tensor<31x1x!pphlo.secret>) -> (tensor<31x1x!pphlo.secret>) { 4 | //CHECK: %[[MUL0:.*]] = pphlo.multiply %arg0, %arg0 : tensor<31x1xf32> 5 | //CHECK: %[[MUL1:.*]] = pphlo.multiply %[[MUL0]], %arg1 : (tensor<31x1xf32>, tensor<31x1x!pphlo.secret>) -> tensor<31x1x!pphlo.secret> 6 | //CHECK: return %[[MUL1]] 7 | %0 = pphlo.multiply %arg0, %arg1 : (tensor<31x1xf32>, tensor<31x1x!pphlo.secret>) -> tensor<31x1x!pphlo.secret> 8 | %1 = pphlo.multiply %0, %arg0 : (tensor<31x1x!pphlo.secret>, tensor<31x1xf32>) -> tensor<31x1x!pphlo.secret> 9 | return %1 : tensor<31x1x!pphlo.secret> 10 | } 11 | -------------------------------------------------------------------------------- /libspu/compiler/tests/passes/hlo2pphlo/complex_p.mlir: -------------------------------------------------------------------------------- 1 | // RUN: spu-opt --hlo-legalize-to-pphlo=input_vis_list=VIS_PUBLIC --lower-conversion-cast --split-input-file %s | FileCheck %s 2 | 3 | func.func @main(%arg0: tensor<3xcomplex>) -> tensor<3xcomplex> { 4 | // CHECK: %[[REAL:.+]] = pphlo.real %[[ARG0:.+]] : (tensor<3xcomplex>) -> tensor<3xf32> 5 | %0 = stablehlo.real %arg0 : (tensor<3xcomplex>) -> tensor<3xf32> 6 | // CHECK: %[[IMAG:.+]] = pphlo.imag %[[ARG1:.+]] : (tensor<3xcomplex>) -> tensor<3xf32> 7 | %1 = stablehlo.imag %arg0 : (tensor<3xcomplex>) -> tensor<3xf32> 8 | // CHECK: pphlo.complex %[[REAL]], %[[IMAG]] : (tensor<3xf32>, tensor<3xf32>) -> tensor<3xcomplex> 9 | %2 = stablehlo.complex %0, %1 : tensor<3xcomplex> 10 | return %2 : tensor<3xcomplex> 11 | } 12 | -------------------------------------------------------------------------------- /examples/python/ml/flax_vit/README.md: -------------------------------------------------------------------------------- 1 | # Bumblebee's Flax VIT Example 2 | 3 | This example demonstrates how to use SPU to perform inference and fine-tune based on the [ViT](https://arxiv.org/abs/2010.11929) model privately. 4 | 5 | This example comes from Hugging Face x Flax official github repo: 6 | 7 | 8 | 9 | 1. Install packages 10 | 11 | ```sh 12 | pip install -r requirements.txt 13 | ``` 14 | 15 | 2. Launch SPU backend runtime 16 | 17 | ```sh 18 | bazel run -c opt //examples/python/utils:nodectl -- -c `pwd`/examples/python/conf/2pc.json up 19 | ``` 20 | 21 | 3. Run `flax_vit_inference` example 22 | 23 | ```sh 24 | bazel run -c opt //examples/python/ml/flax_vit:flax_vit_inference -- --config `pwd`/examples/python/conf/2pc.json 25 | 26 | ``` 27 | -------------------------------------------------------------------------------- /docs/tutorials/cpp_lr_example.rst: -------------------------------------------------------------------------------- 1 | C++ Example: Logistic Regression 2 | ================================ 3 | 4 | To use SPU C++ API, we have to first :ref:`getting_started/install:From Source`, this document shows how to write a privacy preserving logistic regression program with SPU C++ API. 5 | 6 | 7 | Logistic Regression 8 | ------------------- 9 | 10 | .. literalinclude:: ../../examples/cpp/simple_lr.cc 11 | :language: cpp 12 | 13 | 14 | Run it 15 | ------ 16 | 17 | Start two terminals. 18 | 19 | In the first terminal. 20 | 21 | .. code-block:: bash 22 | 23 | bazel run //examples/cpp:simple_lr -- -rank 0 -dataset examples/cpp/perfect_logit_a.csv -has_label=true 24 | 25 | In the second terminal. 26 | 27 | .. code-block:: bash 28 | 29 | bazel run //examples/cpp:simple_lr -- -rank 1 -dataset examples/cpp/perfect_logit_b.csv 30 | 31 | -------------------------------------------------------------------------------- /libspu/compiler/tests/interpret/ring_cast.mlir: -------------------------------------------------------------------------------- 1 | // RUN: spu-translate --protocol_kind=1 --interpret -split-input-file %s 2 | // RUN: spu-translate --protocol_kind=2 --interpret -split-input-file %s 3 | // RUN: spu-translate --protocol_kind=3 --interpret -split-input-file %s 4 | 5 | func.func @cast_1() { 6 | %c0 = pphlo.constant dense<[[1, 2], [3, 4], [5, 6]]> : tensor<3x2xi32> 7 | %operand = pphlo.convert %c0 : (tensor<3x2xi32>)->tensor<3x2x!pphlo.secret> 8 | %r = pphlo.convert %operand : (tensor<3x2x!pphlo.secret>) -> tensor<3x2x!pphlo.secret> 9 | %expected = pphlo.constant dense<[[1, 2], [3, 4], [5, 6]]> : tensor<3x2xi64> 10 | %result = pphlo.convert %r : (tensor<3x2x!pphlo.secret>)->tensor<3x2xi64> 11 | pphlo.custom_call @expect_eq (%result, %expected) : (tensor<3x2xi64>,tensor<3x2xi64>)->() 12 | func.return 13 | } 14 | -------------------------------------------------------------------------------- /spu/experimental/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Ant Group Co., Ltd. 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 | from .drop_cached_var_impl import drop_cached_var 16 | from .make_cached_var_impl import make_cached_var 17 | 18 | __all__ = [ 19 | "make_cached_var", 20 | "drop_cached_var", 21 | ] 22 | -------------------------------------------------------------------------------- /sml/neighbors/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_library") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_library( 20 | name = "knn", 21 | srcs = ["knn.py"], 22 | ) 23 | -------------------------------------------------------------------------------- /sml/tree/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_library") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_library( 20 | name = "tree", 21 | srcs = ["tree.py"], 22 | ) 23 | -------------------------------------------------------------------------------- /libspu/mpc/standard_shape/protocol.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | #include "libspu/core/context.h" 18 | 19 | namespace spu::mpc { 20 | 21 | void regStandardShapeOps(SPUContext* ctx); 22 | 23 | } // namespace spu::mpc 24 | -------------------------------------------------------------------------------- /sml/cluster/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_library") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_library( 20 | name = "kmeans", 21 | srcs = ["kmeans.py"], 22 | ) 23 | -------------------------------------------------------------------------------- /sml/naive_bayes/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_library") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_library( 20 | name = "gnb", 21 | srcs = ["gnb.py"], 22 | ) 23 | -------------------------------------------------------------------------------- /libspu/version.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 Ant Group Co., Ltd. 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 | #define SPU_VERSION "0.9.2.dev$$DATE$$" 16 | 17 | #include 18 | 19 | namespace spu { 20 | 21 | inline std::string_view getVersionStr() { return SPU_VERSION; } 22 | 23 | } // namespace spu 24 | -------------------------------------------------------------------------------- /spu/tests/data/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Ant Group Co., Ltd. 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 | package(default_visibility = ["//visibility:public"]) 16 | 17 | filegroup( 18 | name = "data", 19 | data = [ 20 | "alice.csv", 21 | "bob.csv", 22 | "carol.csv", 23 | ], 24 | ) 25 | -------------------------------------------------------------------------------- /libspu/compiler/tests/interpret/test_json/atan2.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "atan2", 3 | "template": "basic_binary", 4 | "testcases": [ 5 | { 6 | "inputs": [ 7 | { 8 | "data": "[0.0, 1.0, -1.0]", 9 | "shape": "3", 10 | "dtype": "f64" 11 | }, 12 | { 13 | "data": "[0.0, 0.0, 0.0]", 14 | "shape": "3", 15 | "dtype": "f64" 16 | } 17 | ], 18 | "expected": [ 19 | { 20 | "data": "[0.0, 1.5707963267948966, -1.5707963267948966]", 21 | "shape": "3", 22 | "dtype": "f64" 23 | } 24 | ], 25 | "checker": "expect_almost_eq" 26 | } 27 | ] 28 | } -------------------------------------------------------------------------------- /libspu/mpc/aby3/arithmetic_gpu_ext.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | #include "libspu/core/ndarray_ref.h" 18 | 19 | namespace spu::mpc::aby3 { 20 | 21 | void matmul_aa_gpu(const NdArrayRef& x, const NdArrayRef& y, NdArrayRef& ret); 22 | } 23 | -------------------------------------------------------------------------------- /libspu/compiler/utils/utils.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | #include "mlir/Support/LogicalResult.h" 18 | 19 | namespace mlir::spu { 20 | 21 | mlir::LogicalResult argparser_error_handler(const llvm::Twine &msg); 22 | 23 | } // namespace mlir::spu 24 | -------------------------------------------------------------------------------- /libspu/compiler/tests/interpret/slice.mlir: -------------------------------------------------------------------------------- 1 | // RUN: spu-translate --protocol_kind=1 --interpret -split-input-file %s 2 | // RUN: spu-translate --protocol_kind=2 --interpret -split-input-file %s 3 | // RUN: spu-translate --protocol_kind=3 --interpret -split-input-file %s 4 | 5 | func.func @slice_op() { 6 | %operand = pphlo.constant dense<[[0, 0, 1, 0, 0, 1], 7 | [0, 0, 0, 0, 0, 0], 8 | [0, 0, 1, 0, 0, 1]]> : tensor<3x6xi64> 9 | %result = "pphlo.slice"(%operand) { 10 | start_indices = array, 11 | limit_indices = array, 12 | strides = array 13 | } : (tensor<3x6xi64>) -> tensor<2x2xi64> 14 | %expected = pphlo.constant dense<[[1, 1], [1, 1]]> : tensor<2x2xi64> 15 | pphlo.custom_call @expect_eq (%result, %expected) : (tensor<2x2xi64>,tensor<2x2xi64>)->() 16 | func.return 17 | } 18 | -------------------------------------------------------------------------------- /sml/feature_selection/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_library") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_library( 20 | name = "univariate_selection", 21 | srcs = ["univariate_selection.py"], 22 | ) 23 | -------------------------------------------------------------------------------- /libspu/compiler/tests/interpret/reduce.mlir: -------------------------------------------------------------------------------- 1 | // RUN: spu-translate --protocol_kind=1 --interpret -split-input-file %s 2 | // RUN: spu-translate --protocol_kind=2 --interpret -split-input-file %s 3 | // RUN: spu-translate --protocol_kind=3 --interpret -split-input-file %s 4 | 5 | func.func @reduce() { 6 | %input = pphlo.constant dense<[[0, 1, 2, 3, 4, 5]]> : tensor<1x6xi64> 7 | %init_value = pphlo.constant dense<0> : tensor 8 | %result = "pphlo.reduce"(%input, %init_value) ({ 9 | ^bb0(%arg0: tensor, %arg1: tensor): 10 | %0 = pphlo.add %arg0, %arg1 : tensor 11 | pphlo.return %0 : tensor 12 | }) { 13 | dimensions = array 14 | } : (tensor<1x6xi64>, tensor) -> tensor<1xi64> 15 | %expected = pphlo.constant dense<[15]> : tensor<1xi64> 16 | pphlo.custom_call @expect_eq (%result, %expected) : (tensor<1xi64>,tensor<1xi64>)->() 17 | func.return 18 | } 19 | -------------------------------------------------------------------------------- /docs/reference/update_pphlo_doc.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | # 3 | # Copyright 2022 Ant Group Co., Ltd. 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 | SCRIPT=`realpath $0` 19 | SCRIPTPATH=`dirname $SCRIPT` 20 | 21 | bazel build //libspu/dialect/pphlo/IR:op_doc 22 | 23 | cp `bazel info workspace`/bazel-bin/libspu/dialect/pphlo/IR/op_doc.md $SCRIPTPATH/pphlo_op_doc.md 24 | 25 | -------------------------------------------------------------------------------- /libspu/compiler/compile.h: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | #include "libspu/spu.pb.h" 18 | 19 | namespace spu::compiler { 20 | 21 | std::string compile(const CompilationSource &source, 22 | const CompilerOptions &copts); 23 | 24 | } // namespace spu::compiler 25 | -------------------------------------------------------------------------------- /libspu/compiler/tests/passes/hlo2pphlo/complex_s.mlir: -------------------------------------------------------------------------------- 1 | // RUN: spu-opt --hlo-legalize-to-pphlo=input_vis_list=VIS_SECRET --lower-conversion-cast --split-input-file %s | FileCheck %s 2 | 3 | func.func @main(%arg0: tensor<3xcomplex>) -> tensor<3xcomplex> { 4 | // CHECK: %[[REAL:.+]] = pphlo.real %[[ARG0:.+]] : (tensor<3x!pphlo.secret>>) -> tensor<3x!pphlo.secret> 5 | %0 = stablehlo.real %arg0 : (tensor<3xcomplex>) -> tensor<3xf32> 6 | // CHECK: %[[IMAG:.+]] = pphlo.imag %[[ARG1:.+]] : (tensor<3x!pphlo.secret>>) -> tensor<3x!pphlo.secret> 7 | %1 = stablehlo.imag %arg0 : (tensor<3xcomplex>) -> tensor<3xf32> 8 | // CHECK: pphlo.complex %[[REAL]], %[[IMAG]] : (tensor<3x!pphlo.secret>, tensor<3x!pphlo.secret>) -> tensor<3x!pphlo.secret>> 9 | %2 = stablehlo.complex %0, %1 : tensor<3xcomplex> 10 | return %2 : tensor<3xcomplex> 11 | } 12 | -------------------------------------------------------------------------------- /sml/preprocessing/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_library") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_library( 20 | name = "preprocessing", 21 | srcs = ["preprocessing.py"], 22 | deps = [ 23 | "//sml/cluster:kmeans", 24 | ], 25 | ) 26 | -------------------------------------------------------------------------------- /libspu/compiler/codegen/codegen.h: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | #include 18 | 19 | #include "mlir/IR/BuiltinOps.h" 20 | 21 | namespace spu::compiler { 22 | 23 | class CodeGen final { 24 | public: 25 | static std::string doit(mlir::ModuleOp module); 26 | }; 27 | 28 | } // namespace spu::compiler 29 | -------------------------------------------------------------------------------- /bazel/local_openmp_macos.BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Ant Group Co., Ltd. 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 | load("@rules_cc//cc:defs.bzl", "cc_library") 16 | 17 | cc_library( 18 | name = "openmp", 19 | srcs = [ 20 | "lib/libomp.a", 21 | ], 22 | hdrs = ["include/omp.h"], 23 | includes = [ 24 | "include/", 25 | ], 26 | visibility = ["//visibility:public"], 27 | ) 28 | -------------------------------------------------------------------------------- /sml/svm/emulations/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_binary") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_binary( 20 | name = "svm_emul", 21 | srcs = ["svm_emul.py"], 22 | deps = [ 23 | "//sml/svm", 24 | "//sml/utils:emulation", 25 | ], 26 | ) 27 | -------------------------------------------------------------------------------- /sml/tree/emulations/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_binary") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_binary( 20 | name = "tree_emul", 21 | srcs = ["tree_emul.py"], 22 | deps = [ 23 | "//sml/tree", 24 | "//sml/utils:emulation", 25 | ], 26 | ) 27 | -------------------------------------------------------------------------------- /bazel/xtl.BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Ant Group Co., Ltd. 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 | load("@spulib//bazel:spu.bzl", "spu_cmake_external") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | filegroup( 20 | name = "all_srcs", 21 | srcs = glob(["**"]), 22 | ) 23 | 24 | spu_cmake_external( 25 | name = "xtl", 26 | lib_source = ":all_srcs", 27 | out_headers_only = True, 28 | ) 29 | -------------------------------------------------------------------------------- /sml/decomposition/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_library") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_library( 20 | name = "pca", 21 | srcs = ["pca.py"], 22 | deps = ["//sml/utils:extmath"], 23 | ) 24 | 25 | py_library( 26 | name = "nmf", 27 | srcs = ["nmf.py"], 28 | ) 29 | -------------------------------------------------------------------------------- /sml/neighbors/emulations/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_binary") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_binary( 20 | name = "knn_emul", 21 | srcs = ["knn_emul.py"], 22 | deps = [ 23 | "//sml/neighbors:knn", 24 | "//sml/utils:emulation", 25 | ], 26 | ) 27 | -------------------------------------------------------------------------------- /sml/svm/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_library") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_library( 20 | name = "smo", 21 | srcs = ["smo.py"], 22 | ) 23 | 24 | py_library( 25 | name = "svm", 26 | srcs = ["svm.py"], 27 | deps = [ 28 | ":smo", 29 | ], 30 | ) 31 | -------------------------------------------------------------------------------- /sml/svm/tests/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_test") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_test( 20 | name = "svm_test", 21 | srcs = ["svm_test.py"], 22 | deps = [ 23 | "//sml/svm", 24 | "//spu:init", 25 | "//spu/utils:simulation", 26 | ], 27 | ) 28 | -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "label": "Build and Test all (Debug)", 5 | "type": "shell", 6 | "command": "bazel test //... -c dbg --spawn_strategy=standalone --strip=never", 7 | "group": { 8 | "kind": "build", 9 | }, 10 | "problemMatcher": "$gcc", 11 | }, 12 | { 13 | "label": "Build and Test all (Fast)", 14 | "type": "shell", 15 | "command": "bazel test //...", 16 | "group": { 17 | "kind": "build", 18 | }, 19 | "problemMatcher": "$gcc", 20 | }, 21 | { 22 | "label": "Build and Test all (Release)", 23 | "type": "shell", 24 | "command": "bazel test //... -c opt", 25 | "group": { 26 | "kind": "build", 27 | }, 28 | "problemMatcher": "$gcc", 29 | }, 30 | ] 31 | } -------------------------------------------------------------------------------- /sml/cluster/emulations/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_binary") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_binary( 20 | name = "kmeans_emul", 21 | srcs = ["kmeans_emul.py"], 22 | deps = [ 23 | "//sml/cluster:kmeans", 24 | "//sml/utils:emulation", 25 | ], 26 | ) 27 | -------------------------------------------------------------------------------- /sml/naive_bayes/emulations/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_binary") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_binary( 20 | name = "gnb_emul", 21 | srcs = ["gnb_emul.py"], 22 | deps = [ 23 | "//sml/naive_bayes:gnb", 24 | "//sml/utils:emulation", 25 | ], 26 | ) 27 | -------------------------------------------------------------------------------- /sml/tree/tests/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_test") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_test( 20 | name = "tree_test", 21 | srcs = ["tree_test.py"], 22 | deps = [ 23 | "//sml/tree", 24 | "//spu:init", 25 | "//spu/utils:simulation", 26 | ], 27 | ) 28 | -------------------------------------------------------------------------------- /libspu/dialect/pphlo/IR/dialect.h: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | #include "mlir/IR/Dialect.h" 18 | 19 | //===----------------------------------------------------------------------===// 20 | // PPHLO Dialect 21 | //===----------------------------------------------------------------------===// 22 | 23 | #include "libspu/dialect/pphlo/IR/dialect.h.inc" 24 | -------------------------------------------------------------------------------- /libspu/dialect/utils/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Ant Group Co., Ltd. 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 | load("//bazel:spu.bzl", "spu_cc_library") 16 | 17 | spu_cc_library( 18 | name = "utils", 19 | srcs = glob([ 20 | "*.cc", 21 | ]), 22 | hdrs = glob([ 23 | "*.h", 24 | ]), 25 | deps = [ 26 | "@llvm-project//mlir:FuncDialect", 27 | "@llvm-project//mlir:IR", 28 | ], 29 | ) 30 | -------------------------------------------------------------------------------- /sml/utils/tests/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_test") 15 | 16 | package(default_visibility = ["//visibility:public"]) 17 | 18 | py_test( 19 | name = "extmath_test", 20 | srcs = ["extmath_test.py"], 21 | deps = [ 22 | "//sml/utils:extmath", 23 | "//spu:init", 24 | "//spu/utils:simulation", 25 | ], 26 | ) 27 | -------------------------------------------------------------------------------- /libspu/kernel/hal/debug.h: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | #include "libspu/core/context.h" 18 | #include "libspu/core/value.h" 19 | 20 | namespace spu::kernel::hal { 21 | 22 | /// Debug print a value, this action may reveal secret. 23 | // @param v, value to print 24 | void dbg_print(SPUContext* ctx, const Value& v); 25 | 26 | } // namespace spu::kernel::hal 27 | -------------------------------------------------------------------------------- /sml/naive_bayes/tests/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_test") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_test( 20 | name = "gnb_test", 21 | srcs = ["gnb_test.py"], 22 | deps = [ 23 | "//sml/naive_bayes:gnb", 24 | "//spu:init", 25 | "//spu/utils:simulation", 26 | ], 27 | ) 28 | -------------------------------------------------------------------------------- /sml/neighbors/tests/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_test") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_test( 20 | name = "knn_test", 21 | srcs = ["knn_test.py"], 22 | deps = [ 23 | "//sml/neighbors:knn", 24 | "//spu:init", 25 | "//spu/utils:simulation", 26 | ], 27 | ) 28 | -------------------------------------------------------------------------------- /libspu/compiler/utils/utils.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2024 Ant Group Co., Ltd. 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 | #include "libspu/compiler/utils/utils.h" 16 | 17 | #include "llvm/ADT/Twine.h" 18 | #include "spdlog/spdlog.h" 19 | 20 | namespace mlir::spu { 21 | 22 | mlir::LogicalResult argparser_error_handler(const llvm::Twine &msg) { 23 | SPDLOG_ERROR(msg.str()); 24 | return mlir::failure(); 25 | } 26 | 27 | } // namespace mlir::spu 28 | -------------------------------------------------------------------------------- /libspu/cuda_support/kernels.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | #include 18 | 19 | namespace spu::cuda { 20 | 21 | // uint64 implementation 22 | void matmul(int64_t M, int64_t N, int64_t K, const uint64_t* A, uint64_t* B, 23 | uint64_t* C); 24 | 25 | void add(uint64_t* A, const uint64_t* B, int64_t numel); 26 | 27 | } // namespace spu::cuda 28 | -------------------------------------------------------------------------------- /sml/cluster/tests/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_test") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_test( 20 | name = "kmeans_test", 21 | srcs = ["kmeans_test.py"], 22 | deps = [ 23 | "//sml/cluster:kmeans", 24 | "//spu:init", 25 | "//spu/utils:simulation", 26 | ], 27 | ) 28 | -------------------------------------------------------------------------------- /sml/preprocessing/emulations/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_binary") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_binary( 20 | name = "preprocessing_emul", 21 | srcs = ["preprocessing_emul.py"], 22 | deps = [ 23 | "//sml/preprocessing", 24 | "//sml/utils:emulation", 25 | ], 26 | ) 27 | -------------------------------------------------------------------------------- /examples/python/conf/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Ant Group Co., Ltd. 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 | package(default_visibility = ["//visibility:public"]) 16 | 17 | filegroup( 18 | name = "conf", 19 | srcs = [ 20 | "2pc.json", 21 | "2pc_semi2k.json", 22 | "3pc.json", 23 | "3pc_colocated.json", 24 | "ds_breast_cancer_basic.json", 25 | "ds_mock_regression_basic.json", 26 | ], 27 | ) 28 | -------------------------------------------------------------------------------- /examples/python/ir_dump/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_binary") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_binary( 20 | name = "ir_dump", 21 | srcs = ["ir_dump.py"], 22 | data = [ 23 | "//examples/python/conf", 24 | ], 25 | deps = [ 26 | "//spu/utils:distributed", 27 | ], 28 | ) 29 | -------------------------------------------------------------------------------- /libspu/dialect/pphlo/IR/interface.h: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | #include "mlir/IR/Types.h" // IWYU pragma: keep 18 | 19 | //===----------------------------------------------------------------------===// 20 | // PPHLO Interface 21 | //===----------------------------------------------------------------------===// 22 | #include "libspu/dialect/pphlo/IR/interface.h.inc" 23 | -------------------------------------------------------------------------------- /libspu/kernel/hlo/rand.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | #include "libspu/core/value.h" 18 | 19 | namespace spu { 20 | class SPUContext; 21 | } 22 | 23 | namespace spu::kernel::hlo { 24 | 25 | spu::Value Uniform_rand(SPUContext *ctx, const spu::Value &lo, 26 | const spu::Value &hi, const Shape &to_shape); 27 | 28 | } // namespace spu::kernel::hlo 29 | -------------------------------------------------------------------------------- /sml/feature_selection/emulations/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_binary") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_binary( 20 | name = "chi2_emul", 21 | srcs = ["chi2_emul.py"], 22 | deps = [ 23 | "//sml/feature_selection:univariate_selection", 24 | "//sml/utils:emulation", 25 | ], 26 | ) 27 | -------------------------------------------------------------------------------- /sml/gaussian_process/tests/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_test") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_test( 20 | name = "gpc_test", 21 | srcs = ["gpc_test.py"], 22 | deps = [ 23 | "//sml/gaussian_process:_gpc", 24 | "//spu:init", 25 | "//spu/utils:simulation", 26 | ], 27 | ) 28 | -------------------------------------------------------------------------------- /sml/preprocessing/tests/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_test") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_test( 20 | name = "preprocessing_test", 21 | srcs = ["preprocessing_test.py"], 22 | deps = [ 23 | "//sml/preprocessing", 24 | "//spu:init", 25 | "//spu/utils:simulation", 26 | ], 27 | ) 28 | -------------------------------------------------------------------------------- /libspu/compiler/codegen/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Ant Group Co., Ltd. 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 | load("//bazel:spu.bzl", "spu_cc_library") 16 | 17 | package( 18 | default_visibility = ["//visibility:public"], 19 | ) 20 | 21 | spu_cc_library( 22 | name = "codegen", 23 | srcs = ["codegen.cc"], 24 | hdrs = ["codegen.h"], 25 | deps = [ 26 | "//libspu:version", 27 | "//libspu/dialect/pphlo/IR:dialect", 28 | ], 29 | ) 30 | -------------------------------------------------------------------------------- /libspu/kernel/hal/complex.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | #include "libspu/core/context.h" 18 | #include "libspu/core/value.h" 19 | 20 | namespace spu::kernel::hal { 21 | 22 | Value real(SPUContext* ctx, const Value& v); 23 | 24 | Value imag(SPUContext* ctx, const Value& v); 25 | 26 | Value complex(SPUContext* ctx, const Value& r, const Value& i); 27 | 28 | } // namespace spu::kernel::hal 29 | -------------------------------------------------------------------------------- /sml/feature_selection/tests/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_test") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_test( 20 | name = "chi2_test", 21 | srcs = ["chi2_test.py"], 22 | deps = [ 23 | "//sml/feature_selection:univariate_selection", 24 | "//spu:init", 25 | "//spu/utils:simulation", 26 | ], 27 | ) 28 | -------------------------------------------------------------------------------- /bazel/xtensor.BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Ant Group Co., Ltd. 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 | load("@spulib//bazel:spu.bzl", "spu_cmake_external") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | filegroup( 20 | name = "all_srcs", 21 | srcs = glob(["**"]), 22 | ) 23 | 24 | spu_cmake_external( 25 | name = "xtensor", 26 | lib_source = ":all_srcs", 27 | out_headers_only = True, 28 | deps = ["@com_github_xtensor_xtl//:xtl"], 29 | ) 30 | -------------------------------------------------------------------------------- /docs/_static/css/custom.css: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Ant Group Co., Ltd. 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 "../basic.css"; 18 | 19 | html[data-theme="light"] { 20 | --pst-color-primary: rgb(22 119 255); 21 | --pst-color-secondary: rgb(22 255 201); 22 | } 23 | 24 | html[data-theme="dark"] { 25 | --pst-color-primary: rgb(22 119 255); 26 | --pst-color-secondary: rgb(22 255 201); 27 | --pst-color-background: rgb(56, 56, 56); 28 | } -------------------------------------------------------------------------------- /examples/python/ml/flax_bert/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_binary") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_binary( 20 | name = "flax_bert", 21 | srcs = ["flax_bert.py"], 22 | data = ["//examples/python/conf"], 23 | deps = [ 24 | "//spu/intrinsic:all_intrinsics", 25 | "//spu/utils:distributed", 26 | ], 27 | ) 28 | -------------------------------------------------------------------------------- /examples/python/ml/flax_gpt2/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_binary") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_binary( 20 | name = "flax_gpt2", 21 | srcs = ["flax_gpt2.py"], 22 | data = ["//examples/python/conf"], 23 | deps = [ 24 | "//spu/intrinsic:all_intrinsics", 25 | "//spu/utils:distributed", 26 | ], 27 | ) 28 | -------------------------------------------------------------------------------- /libspu/dialect/pphlo/transforms/pass_details.h: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | #include "mlir/Dialect/Func/IR/FuncOps.h" 18 | 19 | #include "libspu/dialect/pphlo/IR/dialect.h" 20 | #include "libspu/dialect/pphlo/transforms/passes.h" 21 | 22 | namespace mlir::spu::pphlo { 23 | 24 | #define GEN_PASS_CLASSES 25 | #include "libspu/dialect/pphlo/transforms/passes.h.inc" 26 | 27 | } // namespace mlir::spu::pphlo 28 | -------------------------------------------------------------------------------- /libspu/kernel/hlo/rand.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Ant Group Co., Ltd. 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 | #include "libspu/kernel/hlo/rand.h" 16 | 17 | #include "libspu/kernel/hal/random.h" 18 | 19 | namespace spu::kernel::hlo { 20 | 21 | spu::Value Uniform_rand(SPUContext *ctx, const spu::Value &lo, 22 | const spu::Value &hi, const Shape &to_shape) { 23 | return hal::rng_uniform(ctx, lo, hi, to_shape); 24 | } 25 | 26 | } // namespace spu::kernel::hlo 27 | -------------------------------------------------------------------------------- /libspu/compiler/tests/passes/hlo2pphlo/tenary_ops_p.mlir: -------------------------------------------------------------------------------- 1 | // RUN: spu-opt --hlo-legalize-to-pphlo=input_vis_list=VIS_PUBLIC,VIS_PUBLIC,VIS_PUBLIC --lower-conversion-cast --split-input-file %s | FileCheck %s 2 | 3 | func.func @main(%arg0: tensor<1024x1xi1>, %arg1: tensor<1024x1xf32>, %arg2: tensor<1024x1xf32>) -> (tensor<1024x1xf32>) { 4 | // CHECK: pphlo.select %arg0, %arg1, %arg2 : (tensor<1024x1xi1>, tensor<1024x1xf32>, tensor<1024x1xf32>) -> tensor<1024x1xf32> 5 | %0 = "stablehlo.select"(%arg0, %arg1, %arg2) : (tensor<1024x1xi1>, tensor<1024x1xf32>, tensor<1024x1xf32>) -> tensor<1024x1xf32> 6 | return %0 : tensor<1024x1xf32> 7 | } 8 | 9 | // ----- 10 | 11 | func.func @main(%arg0: tensor<1024x1xf32>, %arg1: tensor<1024x1xf32>, %arg2: tensor<1024x1xf32>) -> (tensor<1024x1xf32>) { 12 | // CHECK: %0 = pphlo.clamp %arg0, %arg1, %arg2 : tensor<1024x1xf32> 13 | %0 = "stablehlo.clamp"(%arg0, %arg1, %arg2) : (tensor<1024x1xf32>, tensor<1024x1xf32>, tensor<1024x1xf32>) -> tensor<1024x1xf32> 14 | return %0 : tensor<1024x1xf32> 15 | } 16 | -------------------------------------------------------------------------------- /libspu/compiler/utils/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Ant Group Co., Ltd. 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 | load("//bazel:spu.bzl", "spu_cc_library") 16 | 17 | package( 18 | default_visibility = ["//visibility:public"], 19 | licenses = ["notice"], 20 | ) 21 | 22 | spu_cc_library( 23 | name = "utils", 24 | srcs = ["utils.cc"], 25 | hdrs = ["utils.h"], 26 | deps = [ 27 | "@llvm-project//llvm:Support", 28 | "@llvm-project//mlir:Support", 29 | ], 30 | ) 31 | -------------------------------------------------------------------------------- /libspu/compiler/tests/lit.site.cfg.py.in: -------------------------------------------------------------------------------- 1 | # Copyright 2020 The TensorFlow Authors. All Rights Reserved. 2 | # Copyright 2022 The StableHLO Authors. 3 | # Copyright 2023 SecretFlow Authors. 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 | @LIT_SITE_CFG_IN_HEADER@ 16 | import lit.llvm 17 | lit.llvm.initialize(lit_config, config) 18 | config.llvm_tools_dir = "@LLVM_TOOLS_DIR@" 19 | config.pphlo_tools_dir = "@PPHLO_TOOLS_DIR@" 20 | lit_config.load_config(config, "@PPHLO_SOURCE_DIR@" + "/libspu/compiler/tests/lit.cfg.py") 21 | -------------------------------------------------------------------------------- /bazel/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Ant Group Co., Ltd. 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 | package(default_visibility = ["//visibility:public"]) 16 | 17 | config_setting( 18 | name = "spu_build_as_release", 19 | values = {"compilation_mode": "opt"}, 20 | ) 21 | 22 | config_setting( 23 | name = "spu_build_as_debug", 24 | values = {"compilation_mode": "dbg"}, 25 | ) 26 | 27 | config_setting( 28 | name = "spu_build_as_fast", 29 | values = {"compilation_mode": "fastbuild"}, 30 | ) 31 | -------------------------------------------------------------------------------- /libspu/compiler/core/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Ant Group Co., Ltd. 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 | load("//bazel:spu.bzl", "spu_cc_library") 16 | 17 | spu_cc_library( 18 | name = "core", 19 | srcs = ["core.cc"], 20 | hdrs = ["core.h"], 21 | visibility = ["//visibility:public"], 22 | deps = [ 23 | "//libspu/compiler/common:compilation_context", 24 | "//libspu/dialect/pphlo/transforms:all_passes", 25 | "@llvm-project//mlir:Transforms", 26 | ], 27 | ) 28 | -------------------------------------------------------------------------------- /libspu/dialect/pphlo/IR/canonicalization_patterns.td: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Ant Group Co., Ltd. 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 | 16 | // Canonicalization patterns for the MHLO dialect. 17 | 18 | include "mlir/IR/PatternBase.td" 19 | include "libspu/dialect/pphlo/IR/ops.td" 20 | 21 | // select(not(p), t, f) => select(p, f, t) 22 | def FusePredNegIntoSelect : Pat< 23 | (PPHLO_SelectOp (PPHLO_NotOp $pred), $on_true, $on_false), 24 | (PPHLO_SelectOp $pred, $on_false, $on_true) 25 | >; 26 | -------------------------------------------------------------------------------- /libspu/dialect/pphlo/transforms/register_passes.h: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | #include "mlir/Pass/Pass.h" 18 | 19 | #include "libspu/dialect/pphlo/transforms/passes.h" 20 | 21 | namespace mlir::spu::pphlo { 22 | 23 | #define GEN_PASS_REGISTRATION 24 | #include "libspu/dialect/pphlo/transforms/passes.h.inc" 25 | 26 | inline void registerAllPPHloPasses() { registerPasses(); } 27 | 28 | } // namespace mlir::spu::pphlo 29 | -------------------------------------------------------------------------------- /sml/linear_model/utils/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_library") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_library( 20 | name = "link", 21 | srcs = ["link.py"], 22 | ) 23 | 24 | py_library( 25 | name = "loss", 26 | srcs = ["loss.py"], 27 | deps = [":link"], 28 | ) 29 | 30 | py_library( 31 | name = "solver", 32 | srcs = ["solver.py"], 33 | ) 34 | -------------------------------------------------------------------------------- /docs/reference/update_np_op_status_doc.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | # 3 | # Copyright 2022 Ant Group Co., Ltd. 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 | SCRIPT=`realpath $0` 19 | SCRIPTPATH=`dirname $SCRIPT` 20 | REPOPATH=`realpath $SCRIPTPATH/../../` 21 | 22 | bazel run //spu/tests:np_op_status -- --out="$REPOPATH/docs/reference/np_op_status.json" 23 | python $REPOPATH/docs/reference/gen_np_op_status_doc.py --in="$REPOPATH/docs/reference/np_op_status.json" --out="$REPOPATH/docs/reference/np_op_status.md" 24 | 25 | -------------------------------------------------------------------------------- /libspu/mpc/cheetah/type.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Ant Group Co., Ltd. 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 | #include "libspu/mpc/cheetah/type.h" 16 | 17 | #include 18 | 19 | #include "libspu/mpc/common/pv2k.h" 20 | 21 | namespace spu::mpc::cheetah { 22 | 23 | void registerTypes() { 24 | regPV2kTypes(); 25 | 26 | static std::once_flag flag; 27 | std::call_once(flag, []() { 28 | TypeContext::getTypeContext()->addTypes(); 29 | }); 30 | } 31 | 32 | } // namespace spu::mpc::cheetah -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | name: "Publish Python Package" 16 | on: 17 | release: 18 | types: [published] 19 | permissions: read-all 20 | jobs: 21 | trigger-circleci: 22 | runs-on: ubuntu-latest 23 | steps: 24 | - name: secretflow-spu-deploy 25 | id: secretflow-spu-deploy 26 | uses: CircleCI-Public/trigger-circleci-pipeline-action@v1.2.0 27 | env: 28 | CCI_TOKEN: ${{ secrets.CCI_TOKEN }} 29 | -------------------------------------------------------------------------------- /docs/reference/update_complexity_doc.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | # 3 | # Copyright 2022 Ant Group Co., Ltd. 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 | SCRIPT=`realpath $0` 19 | SCRIPTPATH=`dirname $SCRIPT` 20 | REPOPATH=`realpath $SCRIPTPATH/../../` 21 | 22 | bazel run //libspu/mpc/tools:complexity -- --out="$REPOPATH/docs/reference/complexity.json" 23 | python $REPOPATH/docs/reference/gen_complexity_md.py --in="$REPOPATH/docs/reference/complexity.json" --out="$REPOPATH/docs/reference/complexity.md" 24 | 25 | # echo $REPOPATH 26 | -------------------------------------------------------------------------------- /libspu/compiler/tests/passes/hlo2pphlo/gather_s.mlir: -------------------------------------------------------------------------------- 1 | // RUN: spu-opt --hlo-legalize-to-pphlo=input_vis_list=VIS_SECRET,VIS_SECRET --lower-conversion-cast --split-input-file %s | FileCheck %s 2 | 3 | func.func @main(%arg0 : tensor<3x4x2xi64>, %arg1 : tensor<2x3x2xi64>) -> tensor<2x3x2x2xi64> { 4 | // CHECK: pphlo.custom_call @spu.gather(%arg0, %arg1) {pphlo.attributes = {collapsed_slice_dims = array, index_vector_dim = 2 : i64, offset_dims = array, slice_sizes = array, start_index_map = array}} : (tensor<3x4x2x!pphlo.secret>, tensor<2x3x2x!pphlo.secret>) -> tensor<2x3x2x2x!pphlo.secret> 5 | %result = "stablehlo.gather"(%arg0, %arg1) { 6 | dimension_numbers = #stablehlo.gather< 7 | offset_dims = [2, 3], 8 | collapsed_slice_dims = [0], 9 | start_index_map = [1, 0], 10 | index_vector_dim = 2>, 11 | slice_sizes = array, 12 | indices_are_sorted = false 13 | } : (tensor<3x4x2xi64>, tensor<2x3x2xi64>) -> tensor<2x3x2x2xi64> 14 | return %result : tensor<2x3x2x2xi64> 15 | } 16 | -------------------------------------------------------------------------------- /libspu/mpc/securenn/type.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Ant Group Co., Ltd. 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 | #include "libspu/mpc/securenn/type.h" 16 | 17 | #include 18 | 19 | #include "libspu/mpc/common/pv2k.h" 20 | 21 | namespace spu::mpc::securenn { 22 | 23 | void registerTypes() { 24 | regPV2kTypes(); 25 | 26 | static std::once_flag flag; 27 | std::call_once(flag, []() { 28 | TypeContext::getTypeContext()->addTypes(); 29 | }); 30 | } 31 | 32 | } // namespace spu::mpc::securenn 33 | -------------------------------------------------------------------------------- /libspu/mpc/semi2k/type.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Ant Group Co., Ltd. 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 | #include "libspu/mpc/semi2k/type.h" 16 | 17 | #include 18 | 19 | #include "libspu/mpc/common/pv2k.h" 20 | 21 | namespace spu::mpc::semi2k { 22 | 23 | void registerTypes() { 24 | regPV2kTypes(); 25 | 26 | static std::once_flag flag; 27 | std::call_once(flag, []() { 28 | TypeContext::getTypeContext()->addTypes(); 29 | }); 30 | } 31 | 32 | } // namespace spu::mpc::semi2k 33 | -------------------------------------------------------------------------------- /libspu/mpc/spdz2k/type.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Ant Group Co., Ltd. 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 | #include "libspu/mpc/spdz2k/type.h" 16 | 17 | #include 18 | 19 | #include "libspu/mpc/common/pv2k.h" 20 | 21 | namespace spu::mpc::spdz2k { 22 | 23 | void registerTypes() { 24 | regPV2kTypes(); 25 | 26 | static std::once_flag flag; 27 | 28 | std::call_once(flag, []() { 29 | TypeContext::getTypeContext()->addTypes(); 30 | }); 31 | } 32 | 33 | } // namespace spu::mpc::spdz2k 34 | -------------------------------------------------------------------------------- /examples/python/ml/flax_vit/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_binary") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_binary( 20 | name = "flax_vit", 21 | srcs = ["flax_vit.py"], 22 | data = [ 23 | "//examples/python/conf", 24 | "//examples/python/imagenet", 25 | ], 26 | deps = [ 27 | "//spu/intrinsic:all_intrinsics", 28 | "//spu/utils:distributed", 29 | ], 30 | ) 31 | -------------------------------------------------------------------------------- /libspu/mpc/ab_api_test.h: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Ant Group Co., Ltd. 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 | #include "gtest/gtest.h" 16 | #include "yacl/link/link.h" 17 | 18 | #include "libspu/mpc/api_test_params.h" 19 | 20 | namespace spu::mpc::test { 21 | 22 | class ArithmeticTest : public ::testing::TestWithParam {}; 23 | 24 | class BooleanTest : public ::testing::TestWithParam {}; 25 | 26 | class ConversionTest : public ::testing::TestWithParam {}; 27 | 28 | } // namespace spu::mpc::test 29 | -------------------------------------------------------------------------------- /libspu/mpc/semi2k/beaver/beaver_impl/trusted_party/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Ant Group Co., Ltd. 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 | load("//bazel:spu.bzl", "spu_cc_library") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | spu_cc_library( 20 | name = "trusted_party", 21 | srcs = ["trusted_party.cc"], 22 | hdrs = ["trusted_party.h"], 23 | deps = [ 24 | "//libspu/mpc/common:prg_tensor", 25 | "//libspu/mpc/utils:permute", 26 | "//libspu/mpc/utils:ring_ops", 27 | ], 28 | ) 29 | -------------------------------------------------------------------------------- /libspu/mpc/tools/complexity.proto: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright 2021 Ant Group Co., Ltd. 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 | syntax = "proto3"; 18 | 19 | package spu.mpc.internal; 20 | 21 | message ComplexityEntry { 22 | string kernel = 1; 23 | 24 | string latency = 2; 25 | 26 | string comm = 3; 27 | } 28 | 29 | message SingleComplexityReport { 30 | string protocol = 1; 31 | 32 | repeated ComplexityEntry entries = 2; 33 | } 34 | 35 | message ComplexityReport { 36 | repeated SingleComplexityReport reports = 1; 37 | } 38 | -------------------------------------------------------------------------------- /libspu/mpc/aby3/protocol.h: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | #include "yacl/link/link.h" 18 | 19 | #include "libspu/core/context.h" 20 | 21 | namespace spu::mpc { 22 | 23 | std::unique_ptr makeAby3Protocol( 24 | const RuntimeConfig& conf, 25 | const std::shared_ptr& lctx); 26 | 27 | void regAby3Protocol(SPUContext* ctx, 28 | const std::shared_ptr& lctx); 29 | 30 | } // namespace spu::mpc 31 | -------------------------------------------------------------------------------- /libspu/mpc/aby3/type.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Ant Group Co., Ltd. 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 | #include "libspu/mpc/aby3/type.h" 16 | 17 | #include 18 | 19 | #include "libspu/mpc/common/pv2k.h" 20 | 21 | namespace spu::mpc::aby3 { 22 | 23 | void registerTypes() { 24 | regPV2kTypes(); 25 | 26 | static std::once_flag flag; 27 | std::call_once(flag, []() { 28 | TypeContext::getTypeContext() 29 | ->addTypes(); 30 | }); 31 | } 32 | 33 | } // namespace spu::mpc::aby3 34 | -------------------------------------------------------------------------------- /sml/utils/emulations/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_binary") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_binary( 20 | name = "svd_emul", 21 | srcs = ["svd_emul.py"], 22 | data = [":conf"], 23 | deps = [ 24 | "//sml/utils:emulation", 25 | "//sml/utils:extmath", 26 | ], 27 | ) 28 | 29 | filegroup( 30 | name = "conf", 31 | srcs = [ 32 | "3pc_128.json", 33 | ], 34 | ) 35 | -------------------------------------------------------------------------------- /libspu/mpc/spdz2k/protocol.h: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | #include "yacl/link/link.h" 18 | 19 | #include "libspu/core/context.h" 20 | 21 | namespace spu::mpc { 22 | 23 | void regSpdz2kProtocol(SPUContext* ctx, 24 | const std::shared_ptr& lctx); 25 | 26 | std::unique_ptr makeSpdz2kProtocol( 27 | const RuntimeConfig& conf, 28 | const std::shared_ptr& lctx); 29 | 30 | } // namespace spu::mpc 31 | -------------------------------------------------------------------------------- /bazel/nvidia_cutlass.BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Ant Group Co., Ltd. 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 | load("@spulib//bazel:spu.bzl", "spu_cc_library") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | filegroup( 20 | name = "all", 21 | srcs = glob(["**"]), 22 | ) 23 | 24 | spu_cc_library( 25 | name = "cutlass", 26 | srcs = [], 27 | hdrs = glob([ 28 | "include/**/*.h", 29 | "include/**/*.hpp", 30 | ]), 31 | strip_include_prefix = "include", 32 | visibility = ["//visibility:public"], 33 | ) 34 | -------------------------------------------------------------------------------- /libspu/mpc/cheetah/protocol.h: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | #include "yacl/link/link.h" 18 | 19 | #include "libspu/core/context.h" 20 | 21 | namespace spu::mpc { 22 | 23 | void regCheetahProtocol(SPUContext* ctx, 24 | const std::shared_ptr& lctx); 25 | 26 | std::unique_ptr makeCheetahProtocol( 27 | const RuntimeConfig& conf, 28 | const std::shared_ptr& lctx); 29 | 30 | } // namespace spu::mpc 31 | -------------------------------------------------------------------------------- /libspu/mpc/semi2k/protocol.h: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | #include "yacl/link/context.h" 18 | 19 | #include "libspu/core/context.h" 20 | 21 | namespace spu::mpc { 22 | 23 | void regSemi2kProtocol(SPUContext* ctx, 24 | const std::shared_ptr& lctx); 25 | 26 | std::unique_ptr makeSemi2kProtocol( 27 | const RuntimeConfig& conf, 28 | const std::shared_ptr& lctx); 29 | 30 | } // namespace spu::mpc 31 | -------------------------------------------------------------------------------- /sml/gaussian_process/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_library") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_library( 20 | name = "_gpc", 21 | srcs = ["_gpc.py"], 22 | deps = [ 23 | ":kernels", 24 | ":ovo_ovr", 25 | ], 26 | ) 27 | 28 | py_library( 29 | name = "kernels", 30 | srcs = ["kernels.py"], 31 | ) 32 | 33 | py_library( 34 | name = "ovo_ovr", 35 | srcs = ["ovo_ovr.py"], 36 | ) 37 | -------------------------------------------------------------------------------- /libspu/mpc/securenn/protocol.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | #include "yacl/link/link.h" 18 | 19 | #include "libspu/core/context.h" 20 | 21 | namespace spu::mpc { 22 | 23 | void regSecurennProtocol(SPUContext* ctx, 24 | const std::shared_ptr& lctx); 25 | 26 | std::unique_ptr makeSecurennProtocol( 27 | const RuntimeConfig& conf, 28 | const std::shared_ptr& lctx); 29 | 30 | } // namespace spu::mpc 31 | -------------------------------------------------------------------------------- /sml/gaussian_process/emulations/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_binary") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | filegroup( 20 | name = "conf", 21 | srcs = [ 22 | "3pc.json", 23 | ], 24 | ) 25 | 26 | py_binary( 27 | name = "gpc_emul", 28 | srcs = ["gpc_emul.py"], 29 | data = ["conf"], 30 | deps = [ 31 | "//sml/gaussian_process:_gpc", 32 | "//sml/utils:emulation", 33 | ], 34 | ) 35 | -------------------------------------------------------------------------------- /libspu/compiler/tests/interpret/test_json/sign.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sign", 3 | "template": "basic_unary", 4 | "testcases": [ 5 | { 6 | "inputs": [ 7 | { 8 | "data": "[-1, 0, 1]", 9 | "shape": "3", 10 | "dtype": "i64" 11 | } 12 | ], 13 | "expected": [ 14 | { 15 | "data": "[-1, 0, 1]", 16 | "shape": "3", 17 | "dtype": "i64" 18 | } 19 | ] 20 | }, 21 | { 22 | "inputs": [ 23 | { 24 | "data": "[-1.0, 0.0, 1.0]", 25 | "shape": "3", 26 | "dtype": "f64" 27 | } 28 | ], 29 | "expected": [ 30 | { 31 | "data": "[-1.0, 0.0, 1.0]", 32 | "shape": "3", 33 | "dtype": "f64" 34 | } 35 | ], 36 | "checker": "expect_almost_eq" 37 | } 38 | ] 39 | } -------------------------------------------------------------------------------- /libspu/compiler/tests/passes/optimizations/convert_push_down.mlir: -------------------------------------------------------------------------------- 1 | // RUN: spu-opt --convert-push-down --cse --split-input-file %s | FileCheck %s 2 | 3 | func.func @main(%arg0: tensor<4xi32>, %arg1: tensor<2x2xf32>) -> (tensor<2x2xf32>) { 4 | // CHECK: %0 = pphlo.reshape %arg0 : (tensor<4xi32>) -> tensor<2x2xi32> 5 | // CHECK: %1 = pphlo.convert %0 : (tensor<2x2xi32>) -> tensor<2x2xf32> 6 | %0 = pphlo.convert %arg0 : (tensor<4xi32>) -> tensor<4xf32> 7 | %1 = pphlo.reshape %0 : (tensor<4xf32>) -> tensor<2x2xf32> 8 | %2 = pphlo.multiply %1, %arg1 : tensor<2x2xf32> 9 | return %2 : tensor<2x2xf32> 10 | } 11 | 12 | // ----- 13 | 14 | func.func @main(%arg0: tensor<2x3xi32>, %arg1: tensor<2x3xf32>) -> (tensor<3x3xf32>) { 15 | // CHECK: %0 = pphlo.transpose %arg0 16 | // CHECK: %1 = pphlo.convert %0 17 | %0 = pphlo.convert %arg0 : (tensor<2x3xi32>) -> tensor<2x3xf32> 18 | %1 = pphlo.transpose %0, dims =[1,0] : (tensor<2x3xf32>) -> tensor<3x2xf32> 19 | %2 = pphlo.dot %1, %arg1 : (tensor<3x2xf32>, tensor<2x3xf32>) -> tensor<3x3xf32> 20 | return %2 : tensor<3x3xf32> 21 | } 22 | 23 | // ----- 24 | -------------------------------------------------------------------------------- /libspu/compiler/tests/interpret/test_json/abs.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "abs", 3 | "template": "basic_unary", 4 | "testcases": [ 5 | { 6 | "inputs": [ 7 | { 8 | "data": "[-2, 0, 2]", 9 | "shape": "3", 10 | "dtype": "i64" 11 | } 12 | ], 13 | "expected": [ 14 | { 15 | "data": "[ 2, 0, 2]", 16 | "shape": "3", 17 | "dtype": "i64" 18 | } 19 | ] 20 | }, 21 | { 22 | "inputs": [ 23 | { 24 | "data": "[23.1, -23.1, 0.0]", 25 | "shape": "3", 26 | "dtype": "f64" 27 | } 28 | ], 29 | "expected": [ 30 | { 31 | "data": "[23.1, 23.1, 0.0]", 32 | "shape": "3", 33 | "dtype": "f64" 34 | } 35 | ], 36 | "checker": "expect_almost_eq" 37 | } 38 | ] 39 | } -------------------------------------------------------------------------------- /libspu/compiler/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Ant Group Co., Ltd. 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 | load("//bazel:spu.bzl", "spu_cc_binary", "spu_cc_library") 16 | 17 | package( 18 | default_visibility = ["//visibility:public"], 19 | ) 20 | 21 | spu_cc_library( 22 | name = "compile", 23 | srcs = ["compile.cc"], 24 | hdrs = ["compile.h"], 25 | deps = [ 26 | "//libspu/compiler/codegen", 27 | "//libspu/compiler/common:compilation_context", 28 | "//libspu/compiler/core", 29 | "//libspu/compiler/front_end:fe", 30 | ], 31 | ) 32 | -------------------------------------------------------------------------------- /libspu/kernel/hlo/shuffle.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | #include "absl/types/span.h" 18 | 19 | #include "libspu/core/context.h" 20 | #include "libspu/core/value.h" 21 | 22 | namespace spu::kernel::hlo { 23 | 24 | // secret shuffle which means the order is kept secret between parties. 25 | std::vector Shuffle(SPUContext* ctx, 26 | absl::Span inputs, 27 | int64_t axis = 0); 28 | 29 | } // namespace spu::kernel::hlo 30 | -------------------------------------------------------------------------------- /spu/intrinsic/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | from .example_binary_impl import example_binary 16 | from .example_impl import example 17 | 18 | # DO-NOT-EDIT:ADD_IMPORT 19 | from .spu_gelu_impl import spu_gelu 20 | from .spu_vit_gelu_impl import spu_vit_gelu 21 | from .spu_nexp_impl import spu_neg_exp 22 | from .spu_silu_impl import spu_silu 23 | 24 | __all__ = [ 25 | "spu_gelu", 26 | "spu_vit_gelu", 27 | "spu_silu", 28 | "spu_neg_exp", 29 | # "example", 30 | # "example_binary", 31 | # DO-NOT-EDIT:EOL 32 | ] 33 | -------------------------------------------------------------------------------- /libspu/compiler/tests/passes/hlo2pphlo/sort_p.mlir: -------------------------------------------------------------------------------- 1 | // RUN: spu-opt --hlo-legalize-to-pphlo=input_vis_list=VIS_PUBLIC --lower-conversion-cast --split-input-file %s | FileCheck %s 2 | 3 | func.func @main(%arg0: tensor<20xi32>) -> (tensor<20xi32>) { 4 | %0 = stablehlo.iota dim = 0 : tensor<20xi32> 5 | // CHECK: %1:2 = "pphlo.sort"(%arg0, %0) ({ 6 | // CHECK: ^bb0(%arg1: tensor, %arg2: tensor, %arg3: tensor, %arg4: tensor): 7 | // CHECK: %2 = pphlo.less %arg1, %arg2 : (tensor, tensor) -> tensor 8 | // CHECK: pphlo.return %2 : tensor 9 | // CHECK: }) {dimension = 0 : i64, is_stable = true} : (tensor<20xi32>, tensor<20xi32>) -> (tensor<20xi32>, tensor<20xi32>) 10 | %1:2 = "stablehlo.sort"(%arg0, %0) ({ 11 | ^bb0(%arg1: tensor, %arg2: tensor, %arg3: tensor, %arg4: tensor): 12 | %2 = stablehlo.compare LT, %arg1, %arg2 : (tensor, tensor) -> tensor 13 | stablehlo.return %2 : tensor 14 | }) {dimension = 0 : i64, is_stable = true} : (tensor<20xi32>, tensor<20xi32>) -> (tensor<20xi32>, tensor<20xi32>) 15 | return %1#1: tensor<20xi32> 16 | } 17 | -------------------------------------------------------------------------------- /libspu/kernel/hlo/basic_ternary.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | #include "libspu/core/value.h" 18 | 19 | namespace spu { 20 | class SPUContext; 21 | } 22 | 23 | namespace spu::kernel::hlo { 24 | 25 | spu::Value Select(SPUContext *ctx, const spu::Value &pred, 26 | const spu::Value &on_true, const spu::Value &on_false); 27 | 28 | spu::Value Clamp(SPUContext *ctx, const spu::Value &operand, 29 | const spu::Value &min, const spu::Value &max); 30 | 31 | } // namespace spu::kernel::hlo 32 | -------------------------------------------------------------------------------- /libspu/compiler/tests/passes/hlo2pphlo/while_p.mlir: -------------------------------------------------------------------------------- 1 | // RUN: spu-opt -hlo-legalize-to-pphlo=input_vis_list=VIS_PUBLIC --lower-conversion-cast %s --split-input-file | FileCheck %s 2 | 3 | func.func @main(%arg0: tensor) -> tensor { 4 | //CHECK: %0 = pphlo.while(%arg1 = %arg0) : tensor 5 | //CHECK: cond { 6 | //CHECK: %1 = pphlo.less %arg1, %arg1 : (tensor, tensor) -> tensor 7 | //CHECK: pphlo.return %1 : tensor 8 | //CHECK: } do { 9 | //CHECK: %1 = pphlo.add %arg1, %arg1 {name = "compare.0"} : tensor 10 | //CHECK: pphlo.return %1 : tensor 11 | //CHECK: } 12 | %0 = "stablehlo.while"(%arg0) ( { 13 | ^bb0(%arg1: tensor): 14 | %1 = "stablehlo.compare"(%arg1, %arg1) {comparison_direction = #stablehlo, name = "compare.2"} : (tensor, tensor) -> tensor 15 | "stablehlo.return"(%1) : (tensor) -> () 16 | }, { 17 | ^bb0(%arg1: tensor): 18 | %1 = stablehlo.add %arg1, %arg1 {name = "compare.0"} : tensor 19 | "stablehlo.return"(%1) : (tensor) -> () 20 | }) : (tensor) -> (tensor) 21 | 22 | return %0 : tensor 23 | } -------------------------------------------------------------------------------- /libspu/dialect/utils/utils.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | #include "llvm/Support/raw_ostream.h" 18 | #include "mlir/Dialect/Func/IR/FuncOps.h" 19 | #include "mlir/IR/BuiltinOps.h" 20 | 21 | namespace mlir::spu { 22 | 23 | template 24 | std::string mlirObjectToString(T&& mlir_obj) { 25 | std::string buf; 26 | llvm::raw_string_ostream rss(buf); 27 | rss << mlir_obj; 28 | rss.flush(); 29 | return buf; 30 | } 31 | 32 | mlir::func::FuncOp get_entrypoint(ModuleOp op); 33 | 34 | } // namespace mlir::spu 35 | -------------------------------------------------------------------------------- /libspu/compiler/tests/passes/hlo2pphlo/tenary_ops_s_1.mlir: -------------------------------------------------------------------------------- 1 | // RUN: spu-opt --hlo-legalize-to-pphlo=input_vis_list=VIS_SECRET,VIS_PUBLIC,VIS_PUBLIC --lower-conversion-cast --split-input-file %s | FileCheck %s 2 | 3 | func.func @main(%arg0: tensor<1024x1xi1>, %arg1: tensor<1024x1xf32>, %arg2: tensor<1024x1xf32>) -> (tensor<1024x1xf32>) { 4 | // CHECK:pphlo.select %arg0, %arg1, %arg2 : (tensor<1024x1x!pphlo.secret>, tensor<1024x1xf32>, tensor<1024x1xf32>) -> tensor<1024x1x!pphlo.secret> 5 | %0 = "stablehlo.select"(%arg0, %arg1, %arg2) : (tensor<1024x1xi1>, tensor<1024x1xf32>, tensor<1024x1xf32>) -> tensor<1024x1xf32> 6 | return %0 : tensor<1024x1xf32> 7 | } 8 | 9 | // ----- 10 | 11 | func.func @main(%arg0: tensor<1024x1xf32>, %arg1: tensor<1024x1xf32>, %arg2: tensor<1024x1xf32>) -> (tensor<1024x1xf32>) { 12 | // CHECK: %0 = pphlo.clamp %arg0, %arg1, %arg2 : (tensor<1024x1x!pphlo.secret>, tensor<1024x1xf32>, tensor<1024x1xf32>) -> tensor<1024x1x!pphlo.secret> 13 | %0 = "stablehlo.clamp"(%arg0, %arg1, %arg2) : (tensor<1024x1xf32>, tensor<1024x1xf32>, tensor<1024x1xf32>) -> tensor<1024x1xf32> 14 | return %0 : tensor<1024x1xf32> 15 | } 16 | -------------------------------------------------------------------------------- /libspu/compiler/tests/passes/hlo2pphlo/tenary_ops_s_2.mlir: -------------------------------------------------------------------------------- 1 | // RUN: spu-opt --hlo-legalize-to-pphlo=input_vis_list=VIS_PUBLIC,VIS_SECRET,VIS_PUBLIC --lower-conversion-cast --split-input-file %s | FileCheck %s 2 | 3 | func.func @main(%arg0: tensor<1024x1xi1>, %arg1: tensor<1024x1xf32>, %arg2: tensor<1024x1xf32>) -> (tensor<1024x1xf32>) { 4 | // CHECK:pphlo.select %arg0, %arg1, %arg2 : (tensor<1024x1xi1>, tensor<1024x1x!pphlo.secret>, tensor<1024x1xf32>) -> tensor<1024x1x!pphlo.secret> 5 | %0 = "stablehlo.select"(%arg0, %arg1, %arg2) : (tensor<1024x1xi1>, tensor<1024x1xf32>, tensor<1024x1xf32>) -> tensor<1024x1xf32> 6 | return %0 : tensor<1024x1xf32> 7 | } 8 | 9 | // ----- 10 | 11 | func.func @main(%arg0: tensor<1024x1xf32>, %arg1: tensor<1024x1xf32>, %arg2: tensor<1024x1xf32>) -> (tensor<1024x1xf32>) { 12 | // CHECK: %0 = pphlo.clamp %arg0, %arg1, %arg2 : (tensor<1024x1xf32>, tensor<1024x1x!pphlo.secret>, tensor<1024x1xf32>) -> tensor<1024x1x!pphlo.secret> 13 | %0 = "stablehlo.clamp"(%arg0, %arg1, %arg2) : (tensor<1024x1xf32>, tensor<1024x1xf32>, tensor<1024x1xf32>) -> tensor<1024x1xf32> 14 | return %0 : tensor<1024x1xf32> 15 | } 16 | -------------------------------------------------------------------------------- /libspu/compiler/tests/passes/optimizations/no_expand_secret_gather.mlir: -------------------------------------------------------------------------------- 1 | // RUN: spu-opt --expand-secret-gather --split-input-file %s | FileCheck %s 2 | func.func @main(%arg0: tensor<2x!pphlo.secret>, %arg1: tensor<1xi32>) -> (tensor>) { 3 | //CHECK-NOT: pphlo.while 4 | //CHECK : spu.gather 5 | %0 = pphlo.custom_call @spu.gather(%arg0, %arg1) {pphlo.attributes = {offset_dims = array, collapsed_slice_dims = array, start_index_map = array, index_vector_dim = 0 : i64, slice_sizes = array}} : (tensor<2x!pphlo.secret>, tensor<1xi32>) -> tensor> 6 | return %0: tensor> 7 | } 8 | 9 | // ----- 10 | func.func @main(%arg0: tensor<3x3xi32>, %arg1: tensor<2xi32>) -> (tensor<2x3xi32>) { 11 | //CHECK-NOT: pphlo.while 12 | //CHECK : spu.gather 13 | %0 = pphlo.custom_call @spu.gather(%arg0, %arg1) {pphlo.attributes = {offset_dims = array, collapsed_slice_dims = array, start_index_map = array, index_vector_dim = 1 : i64, slice_sizes = array}} : (tensor<3x3xi32>, tensor<2xi32>) -> tensor<2x3xi32> 14 | return %0 : tensor<2x3xi32> 15 | } -------------------------------------------------------------------------------- /libspu/core/config.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | #include "libspu/spu.pb.h" 18 | 19 | namespace spu { 20 | 21 | // Fill attribute with default value with a implementation defined value. 22 | // 23 | // Required field must be filled, or exception will be raised. 24 | // Optional field will not be touched. 25 | // Field with (implementation defined) default value will be changed 26 | void populateRuntimeConfig(RuntimeConfig& cfg); 27 | 28 | RuntimeConfig makeFullRuntimeConfig(const RuntimeConfig& cfg); 29 | 30 | } // namespace spu 31 | -------------------------------------------------------------------------------- /libspu/device/pphlo/pphlo_intrinsic_executor.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | #include "libspu/core/value.h" 18 | #include "libspu/dialect/pphlo/IR/ops.h" 19 | 20 | namespace spu { 21 | 22 | class SPUContext; 23 | 24 | } 25 | 26 | namespace spu::device::pphlo { 27 | 28 | std::vector intrinsic_dispatcher(SPUContext* ctx, 29 | mlir::spu::pphlo::CustomCallOp& call, 30 | absl::Span inputs); 31 | 32 | } // namespace spu::device::pphlo 33 | -------------------------------------------------------------------------------- /spu/tests/jnp_aby3_r64_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Ant Group Co., Ltd. 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 | 16 | import unittest 17 | 18 | import numpy as np 19 | 20 | import spu.spu_pb2 as spu_pb2 21 | import spu.utils.simulation as ppsim 22 | from spu.tests.jnp_testbase import JnpTests 23 | 24 | 25 | class JnpTestAby3FM64(JnpTests.JnpTestBase): 26 | def setUp(self): 27 | self._sim = ppsim.Simulator.simple( 28 | 3, spu_pb2.ProtocolKind.ABY3, spu_pb2.FieldType.FM64 29 | ) 30 | self._rng = np.random.RandomState() 31 | 32 | 33 | if __name__ == "__main__": 34 | unittest.main() 35 | -------------------------------------------------------------------------------- /libspu/mpc/cheetah/nonlinear/README.md: -------------------------------------------------------------------------------- 1 | # Performance Stats 2 | 3 | | | bit width | Send & Recv (bits) | 4 | | ----------------------------- | -------------- | ------------------ | 5 | | Millionare (radix = 4) | 32 | 348.377 | 6 | | Millionare (radix = 4) | 40 | 428.377 | 7 | | Millionare (radix = 4) | 64 | 690.503 | 8 | | TruncatePr (unknown sign bit) | k=32, fxp = 12 | 369.503 | 9 | | TruncatePr (known sign bit) | k=32, fxp = 12 | 32.252 | 10 | | TruncatePr (unknown sign bit) | k=64, fxp = 12 | 723.754 | 11 | | TruncatePr (known sign bit) | k=64, fxp = 12 | 36.252 | 12 | 13 | * Note: The perfermance stats here are average from at least 2^17 input length. 14 | 15 | * _Millionare_ Protocol implements the tree-based protocol from [CrypTFlow2](https://eprint.iacr.org/2020/1002)). 16 | We set the comparison radix as `4` 17 | * _TruncatePr_ Protocol implements the 1-bit approximated truncate protocol from [Cheetah](https://eprint.iacr.org/2022/207.pdf)). 18 | We also set the comparison radix as `4` by default. 19 | -------------------------------------------------------------------------------- /spu/tests/jnp_aby3_r128_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Ant Group Co., Ltd. 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 | 16 | import unittest 17 | 18 | import numpy as np 19 | 20 | import spu.spu_pb2 as spu_pb2 21 | import spu.utils.simulation as ppsim 22 | from spu.tests.jnp_testbase import JnpTests 23 | 24 | 25 | class JnpTestAby3FM128(JnpTests.JnpTestBase): 26 | def setUp(self): 27 | self._sim = ppsim.Simulator.simple( 28 | 3, spu_pb2.ProtocolKind.ABY3, spu_pb2.FieldType.FM128 29 | ) 30 | self._rng = np.random.RandomState() 31 | 32 | 33 | if __name__ == "__main__": 34 | unittest.main() 35 | -------------------------------------------------------------------------------- /spu/tests/jnp_ref2k_r64_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Ant Group Co., Ltd. 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 | 16 | import unittest 17 | 18 | import numpy as np 19 | 20 | import spu.spu_pb2 as spu_pb2 21 | import spu.utils.simulation as ppsim 22 | from spu.tests.jnp_testbase import JnpTests 23 | 24 | 25 | class JnpTestRef2kFM64(JnpTests.JnpTestBase): 26 | def setUp(self): 27 | self._sim = ppsim.Simulator.simple( 28 | 3, spu_pb2.ProtocolKind.REF2K, spu_pb2.FieldType.FM64 29 | ) 30 | self._rng = np.random.RandomState() 31 | 32 | 33 | if __name__ == "__main__": 34 | unittest.main() 35 | -------------------------------------------------------------------------------- /spu/tests/jnp_semi2k_r64_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Ant Group Co., Ltd. 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 | 16 | import unittest 17 | 18 | import numpy as np 19 | 20 | import spu.spu_pb2 as spu_pb2 21 | import spu.utils.simulation as ppsim 22 | from spu.tests.jnp_testbase import JnpTests 23 | 24 | 25 | class JnpTestSemi2kFM64(JnpTests.JnpTestBase): 26 | def setUp(self): 27 | self._sim = ppsim.Simulator.simple( 28 | 3, spu_pb2.ProtocolKind.SEMI2K, spu_pb2.FieldType.FM64 29 | ) 30 | self._rng = np.random.RandomState() 31 | 32 | 33 | if __name__ == "__main__": 34 | unittest.main() 35 | -------------------------------------------------------------------------------- /spu/tests/jnp_semi2k_r128_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Ant Group Co., Ltd. 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 | 16 | import unittest 17 | 18 | import numpy as np 19 | 20 | import spu.spu_pb2 as spu_pb2 21 | import spu.utils.simulation as ppsim 22 | from spu.tests.jnp_testbase import JnpTests 23 | 24 | 25 | class JnpTestSemi2kFM128(JnpTests.JnpTestBase): 26 | def setUp(self): 27 | self._sim = ppsim.Simulator.simple( 28 | 3, spu_pb2.ProtocolKind.SEMI2K, spu_pb2.FieldType.FM128 29 | ) 30 | self._rng = np.random.RandomState() 31 | 32 | 33 | if __name__ == "__main__": 34 | unittest.main() 35 | -------------------------------------------------------------------------------- /libspu/compiler/tests/interpret/popcnt.mlir: -------------------------------------------------------------------------------- 1 | // RUN: spu-translate --protocol_kind=1 --interpret -split-input-file %s 2 | // RUN: spu-translate --protocol_kind=2 --interpret -split-input-file %s 3 | // RUN: spu-translate --protocol_kind=3 --interpret -split-input-file %s 4 | // AUTO GENERATED, DO NOT EDIT 5 | 6 | func.func @popcnt_op_test_i64_i64_p() { 7 | %0 = pphlo.constant dense<[0, 1, 2, 127]> : tensor<4xi64> 8 | %1 = pphlo.popcnt %0 : (tensor<4xi64>)->tensor<4xi64> 9 | %2 = pphlo.constant dense<[0, 1, 1, 7]> : tensor<4xi64> 10 | pphlo.custom_call @expect_eq(%1, %2) : (tensor<4xi64>, tensor<4xi64>)->() 11 | func.return 12 | } 13 | 14 | // ----- 15 | 16 | func.func @popcnt_op_test_i64_i64_s() { 17 | %0 = pphlo.constant dense<[0, 1, 2, 127]> : tensor<4xi64> 18 | %1 = pphlo.convert %0 : (tensor<4xi64>)->tensor<4x!pphlo.secret> 19 | %2 = pphlo.popcnt %1 : (tensor<4x!pphlo.secret>)->tensor<4x!pphlo.secret> 20 | %3 = pphlo.constant dense<[0, 1, 1, 7]> : tensor<4xi64> 21 | %4 = pphlo.convert %2 : (tensor<4x!pphlo.secret>)->tensor<4xi64> 22 | // pphlo.custom_call @expect_eq(%3, %4) : (tensor<4xi64>, tensor<4xi64>)->() 23 | func.return 24 | } 25 | -------------------------------------------------------------------------------- /libspu/dialect/pphlo/IR/base_enums.h: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | #include "llvm/ADT/DenseMap.h" 18 | #include "llvm/ADT/StringRef.h" 19 | #include "llvm/ADT/StringSwitch.h" 20 | #include "mlir/IR/BuiltinAttributes.h" 21 | #include "mlir/IR/BuiltinTypes.h" 22 | 23 | // Order matters, this .inc header is not self-contained, and relies on the 24 | // #includes above. 25 | 26 | #include "libspu/dialect/pphlo/IR/base_enums.h.inc" 27 | 28 | namespace mlir::spu::pphlo { 29 | 30 | enum class Visibility { 31 | PUBLIC = 0, 32 | SECRET, 33 | }; 34 | 35 | } 36 | -------------------------------------------------------------------------------- /libspu/mpc/cheetah/alg.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | #include 18 | 19 | #include "libspu/core/context.h" 20 | 21 | namespace spu::mpc::cheetah { 22 | 23 | // Compute a batch of less than between a secret and a batch of public value 24 | // [x], y0, y1, ..., yB => less(x < y0), less(x < y1), .., less(x < yB) 25 | std::vector BatchLessThan(KernelEvalContext* kctx, 26 | const NdArrayRef& x, 27 | absl::Span y); 28 | } // namespace spu::mpc::cheetah 29 | -------------------------------------------------------------------------------- /spu/intrinsic/README.md: -------------------------------------------------------------------------------- 1 | # SPU Intrinsic 2 | 3 | ## How to create a new intrinsic function 4 | 5 | To add a new intrinsic, please follow steps here 6 | 7 | ### Generate necessary boilerplate 8 | 9 | Simply using run `add_new_intrinsic.py` 10 | 11 | ```python 12 | python ./add_new_intrinsic.py new_intrinsic 13 | ``` 14 | 15 | Replace `new_intrinsic` with the name of intrinsic to be added 16 | 17 | ### Filling python part 18 | 19 | Open `new_intrinsic_impl.py` file under this folder, and provide necessary implementation to all unimplemented function. 20 | 21 | In `_example_lowering` function, the prepopulated lowering may not fit the new intrinsic, make modifications when necessary. 22 | 23 | ### Add C++ dispatch code 24 | 25 | Open `libspu/device/pphlo/pphlo_intrinsic_executor.cc`, find the if branch that matches your intrinsic name, fill proper dispatch code. 26 | 27 | ### (Optional) Add compile time visibility inference rule 28 | 29 | By default, compiler uses a common inference rule to deduce output visibility. 30 | If your new intrinsic requires some special treatment, please open `libspu/dialect/pphlo/transforms/visibility_inference.cc`, 31 | and update `inferIntrinsic` method. 32 | -------------------------------------------------------------------------------- /libspu/compiler/tests/interpret/pad.mlir: -------------------------------------------------------------------------------- 1 | // RUN: spu-translate --protocol_kind=1 --interpret -split-input-file %s 2 | // RUN: spu-translate --protocol_kind=2 --interpret -split-input-file %s 3 | // RUN: spu-translate --protocol_kind=3 --interpret -split-input-file %s 4 | 5 | func.func @pad() { 6 | %operand = pphlo.constant dense<[[0, 0, 0, 0], 7 | [0, 1, 2, 0], 8 | [0, 3, 4, 0], 9 | [0, 5, 6, 0], 10 | [0, 0, 0, 0]]> : tensor<5x4xi64> 11 | %padding_value = pphlo.constant dense<-1> : tensor 12 | %result = pphlo.pad %operand, %padding_value, low = [1, -1], high = [1, -1], interior = [0, 1] 13 | : (tensor<5x4xi64>, tensor) -> tensor<7x5xi64> 14 | %expected = pphlo.constant dense<[[-1, -1, -1, -1, -1], [-1, 0, -1, 0, -1], [-1, 1, -1, 2, -1], 15 | [-1, 3, -1, 4, -1], [-1, 5, -1, 6, -1], [-1, 0, -1, 0, -1], 16 | [-1, -1, -1, -1, -1]]> : tensor<7x5xi64> 17 | pphlo.custom_call @expect_eq (%result, %expected) : (tensor<7x5xi64>,tensor<7x5xi64>)->() 18 | func.return 19 | } 20 | -------------------------------------------------------------------------------- /libspu/mpc/securenn/state.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | #include 18 | 19 | #include "libspu/mpc/common/communicator.h" 20 | 21 | namespace spu::mpc { 22 | 23 | class SecurennState : public State { 24 | public: 25 | SecurennState() = default; 26 | 27 | static constexpr char kBindName[] = "SecurennState"; 28 | 29 | std::unique_ptr fork() override { 30 | auto ret = std::unique_ptr(new SecurennState); 31 | // ret->beaver_ = beaver_->Spawn(); 32 | return ret; 33 | } 34 | }; 35 | 36 | } // namespace spu::mpc 37 | -------------------------------------------------------------------------------- /docs/getting_started/introduction.rst: -------------------------------------------------------------------------------- 1 | What is SPU? 2 | ============ 3 | 4 | SPU (Secure Processing Unit) is a domain-specific **compiler and runtime suite**, that aims to provide a **secure computation** service with **provable security**. 5 | 6 | SPU compiler uses `XLA `_ as its front-end Intermediate Representation (IR), therefore, SPU supports the XLA IR outputs from Tensorflow, Jax, or PyTorch. Internally, the SPU compiler compiles XLA to an MPC specific MLIR dialect which is later interpreted by SPU runtime. Currently, SPU team highly recommends using `JAX `_ and its sibling - `Flax `_. 7 | 8 | SPU runtime implements various `MPC `_ protocols (as the back-end) to provide **provable security**. SPU runtime is designed to be **highly extensible**, protocol developers can hack into MPC protocols with minimum effort and let the SPU compiler/runtime translate and interpret complicated frontend logic on it. 9 | 10 | 11 | .. figure:: ../imgs/spu_arch.png 12 | :scale: 80 % 13 | 14 | High-level system architecture of SPU 15 | -------------------------------------------------------------------------------- /.circleci/run-nn.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/bash 2 | # 3 | # Copyright 2023 Ant Group Co., Ltd. 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 | for net in network_a network_b network_c network_d; do 19 | for opt in sgd; do 20 | echo "Start training "${net}" "${opt}" "${start_ts}"" 21 | python3 examples/python/ml/stax_nn/stax_nn.py --model ${net} --optimizer ${opt} -e 1 -c .circleci/benchmark.json 22 | echo "Finish training "${net}" "${opt}" "${end_ts}"" 23 | done 24 | done 25 | 26 | # adam is slow so run network_a only 27 | python3 examples/python/ml/stax_nn/stax_nn.py --model network_a --optimizer adam -e 1 -c .circleci/benchmark.json 28 | -------------------------------------------------------------------------------- /libspu/compiler/tests/interpret/dynamic_update_slice.mlir: -------------------------------------------------------------------------------- 1 | // RUN: spu-translate --protocol_kind=1 --interpret -split-input-file %s 2 | // RUN: spu-translate --protocol_kind=2 --interpret -split-input-file %s 3 | // RUN: spu-translate --protocol_kind=3 --interpret -split-input-file %s 4 | 5 | func.func @dynamic_update_slice() { 6 | %operand = pphlo.constant dense<[[1, 1, 1, 1], 7 | [1, 1, 1, 1], 8 | [1, 2, 2, 2], 9 | [1, 2, 2, 2]]> : tensor<4x4xi64> 10 | %update = pphlo.constant dense<[[1, 1, 1], 11 | [1, 1, 1]]> : tensor<2x3xi64> 12 | %start_indices0 = pphlo.constant dense<4> : tensor 13 | %start_indices1 = pphlo.constant dense<4> : tensor 14 | %result = pphlo.dynamic_update_slice %operand, %update, %start_indices0, %start_indices1 : 15 | (tensor<4x4xi64>, tensor<2x3xi64>, tensor, tensor) -> tensor<4x4xi64> 16 | %expected = pphlo.constant dense<[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]> : tensor<4x4xi64> 17 | pphlo.custom_call @expect_eq (%result, %expected) : (tensor<4x4xi64>,tensor<4x4xi64>)->() 18 | func.return 19 | } 20 | -------------------------------------------------------------------------------- /libspu/kernel/hal/intrinsic/nn/activation.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 Ant Group Co., Ltd. 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 | #pragma once 15 | #include "libspu/kernel/hal/fxp_base.h" 16 | namespace spu::kernel::hal::intrinsic::nn { 17 | 18 | // exp(clip(x, -14)) for x < 0 19 | Value f_neg_exp_taylor(SPUContext* ctx, const Value& x); 20 | 21 | // gelu(x) 22 | Value f_seg3_gelu(SPUContext* ctx, const Value& x, 23 | bool small_ring_compare = true); 24 | 25 | // silu(x) 26 | Value f_seg4_silu(SPUContext* ctx, const Value& x, 27 | bool small_ring_compare = true); 28 | 29 | } // namespace spu::kernel::hal::intrinsic::nn 30 | -------------------------------------------------------------------------------- /libspu/kernel/hlo/casting.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | #include "libspu/core/value.h" 18 | 19 | namespace spu { 20 | class SPUContext; 21 | } 22 | 23 | namespace spu::kernel::hlo { 24 | 25 | spu::Value Cast(SPUContext *ctx, const spu::Value &in, Visibility dst_vtype, 26 | DataType dst_dtype); 27 | 28 | spu::Value Bitcast(SPUContext *ctx, const spu::Value &in, DataType dst_dtype); 29 | 30 | spu::Value Reveal(SPUContext *ctx, const spu::Value &in); 31 | 32 | spu::Value Seal(SPUContext *ctx, const spu::Value &in); 33 | 34 | } // namespace spu::kernel::hlo 35 | -------------------------------------------------------------------------------- /sml/decomposition/tests/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_test") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_test( 20 | name = "pca_test", 21 | srcs = ["pca_test.py"], 22 | deps = [ 23 | "//sml/decomposition:pca", 24 | "//spu:init", 25 | "//spu/utils:simulation", 26 | ], 27 | ) 28 | 29 | py_test( 30 | name = "nmf_test", 31 | srcs = ["nmf_test.py"], 32 | deps = [ 33 | "//sml/decomposition:nmf", 34 | "//spu:init", 35 | "//spu/utils:simulation", 36 | ], 37 | ) 38 | -------------------------------------------------------------------------------- /libspu/compiler/tests/passes/hlo2pphlo/while_s.mlir: -------------------------------------------------------------------------------- 1 | // RUN: spu-opt -hlo-legalize-to-pphlo=input_vis_list=VIS_SECRET --lower-conversion-cast %s --split-input-file | FileCheck %s 2 | 3 | func.func @main(%arg0: tensor) -> tensor { 4 | //CHECK: %0 = pphlo.while(%arg1 = %arg0) : tensor> 5 | //CHECK: cond { 6 | //CHECK: %1 = pphlo.less %arg1, %arg1 : (tensor>, tensor>) -> tensor> 7 | //CHECK: pphlo.return %1 : tensor> 8 | //CHECK: } do { 9 | //CHECK: %1 = pphlo.add %arg1, %arg1 : tensor> 10 | //CHECK: pphlo.return %1 : tensor> 11 | //CHECK: } 12 | %0 = "stablehlo.while"(%arg0) ( { 13 | ^bb0(%arg1: tensor): 14 | %1 = "stablehlo.compare"(%arg1, %arg1) {comparison_direction = #stablehlo, name = "compare.2"} : (tensor, tensor) -> tensor 15 | "stablehlo.return"(%1) : (tensor) -> () 16 | }, { 17 | ^bb0(%arg1: tensor): 18 | %1 = stablehlo.add %arg1, %arg1 : tensor 19 | "stablehlo.return"(%1) : (tensor) -> () 20 | }) : (tensor) -> (tensor) 21 | 22 | return %0 : tensor 23 | } -------------------------------------------------------------------------------- /spu/tests/jnp_cheetah_r64_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Ant Group Co., Ltd. 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 | 16 | import unittest 17 | 18 | import numpy as np 19 | 20 | import spu.spu_pb2 as spu_pb2 21 | import spu.utils.simulation as ppsim 22 | from spu.tests.jnp_testbase import JnpTests 23 | 24 | 25 | @unittest.skip("too slow, last run succeed") 26 | class JnpTestAby3FM64(JnpTests.JnpTestBase): 27 | def setUp(self): 28 | self._sim = ppsim.Simulator.simple( 29 | 2, spu_pb2.ProtocolKind.CHEETAH, spu_pb2.FieldType.FM64 30 | ) 31 | self._rng = np.random.RandomState() 32 | 33 | 34 | if __name__ == "__main__": 35 | unittest.main() 36 | -------------------------------------------------------------------------------- /libspu/compiler/tests/passes/optimizations/expand_secret_gather.mlir: -------------------------------------------------------------------------------- 1 | // RUN: spu-opt --expand-secret-gather --split-input-file %s | FileCheck %s 2 | func.func @main(%arg0: tensor<2xi32>, %arg1: tensor<1x!pphlo.secret>) -> (tensor>) { 3 | //CHECK-NOT: spu.gather 4 | //CHECK : pphlo.while 5 | %0 = pphlo.custom_call @spu.gather(%arg0, %arg1) {pphlo.attributes = {offset_dims = array, collapsed_slice_dims = array, start_index_map = array, index_vector_dim = 0 : i64, slice_sizes = array}} : (tensor<2xi32>, tensor<1x!pphlo.secret>) -> tensor> 6 | return %0: tensor> 7 | } 8 | // ----- 9 | func.func @main(%arg0: tensor<3x3xi32>, %arg1: tensor<2x!pphlo.secret>) -> (tensor<2x3x!pphlo.secret>) { 10 | //CHECK-NOT: spu.gather 11 | //CHECK : pphlo.while 12 | %0 = pphlo.custom_call @spu.gather(%arg0, %arg1) {pphlo.attributes = {offset_dims = array, collapsed_slice_dims = array, start_index_map = array, index_vector_dim = 1 : i64, slice_sizes = array}} : (tensor<3x3xi32>, tensor<2x!pphlo.secret>) -> tensor<2x3x!pphlo.secret> 13 | return %0 : tensor<2x3x!pphlo.secret> 14 | } -------------------------------------------------------------------------------- /spu/utils/distributed.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Ant Group Co., Ltd. 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 | syntax = "proto3"; 16 | 17 | package spu.utils.distributed; 18 | 19 | service NodeService { 20 | // run a routine on node's context, save the return values as object refs, 21 | // return the object refs to the caller. 22 | rpc Run(stream RunRequest) returns (stream RunResponse) {} 23 | 24 | // run a routine on node's context, return the object directly. 25 | rpc RunReturn(stream RunRequest) returns (stream RunResponse) {} 26 | } 27 | 28 | message RunRequest { 29 | bytes data = 1; 30 | } 31 | 32 | message RunResponse { 33 | bytes data = 1; 34 | } 35 | -------------------------------------------------------------------------------- /libspu/kernel/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Ant Group Co., Ltd. 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 | load("//bazel:spu.bzl", "spu_cc_library") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | spu_cc_library( 20 | name = "test_util", 21 | srcs = ["test_util.cc"], 22 | hdrs = ["test_util.h"], 23 | deps = [ 24 | "//libspu/core:context", 25 | "//libspu/core:value", 26 | "//libspu/kernel/hal:constants", # BAD 27 | "//libspu/kernel/hal:prot_wrapper", # BAD 28 | "//libspu/kernel/hal:public_helper", # BAD 29 | "//libspu/mpc:factory", 30 | "@com_google_googletest//:gtest", 31 | ], 32 | ) 33 | -------------------------------------------------------------------------------- /benchmark/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_binary") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_binary( 20 | name = "binary_op_bench", 21 | srcs = ["binary_op_bench.py"], 22 | data = [ 23 | "//examples/python/conf", 24 | ], 25 | deps = [ 26 | "//spu/utils:distributed", 27 | ], 28 | ) 29 | 30 | py_binary( 31 | name = "unary_op_bench", 32 | srcs = ["unary_op_bench.py"], 33 | data = [ 34 | "//examples/python/conf", 35 | ], 36 | deps = [ 37 | "//spu/utils:distributed", 38 | ], 39 | ) 40 | -------------------------------------------------------------------------------- /libspu/dialect/utils/utils.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2024 Ant Group Co., Ltd. 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 | #include "libspu/dialect/utils/utils.h" 16 | 17 | #include "llvm/ADT/Twine.h" 18 | #include "spdlog/spdlog.h" 19 | 20 | namespace mlir::spu { 21 | 22 | mlir::func::FuncOp get_entrypoint(ModuleOp op) { 23 | // Get the main function 24 | auto entry_func = op.lookupSymbol("main"); 25 | if (!entry_func) { 26 | auto funcs = op.getOps(); 27 | if (std::distance(funcs.begin(), funcs.end()) == 1) { 28 | entry_func = *funcs.begin(); 29 | } 30 | } 31 | 32 | return entry_func; 33 | } 34 | 35 | } // namespace mlir::spu 36 | -------------------------------------------------------------------------------- /libspu/compiler/core/core.h: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | namespace mlir { 18 | 19 | class PassManager; 20 | class ModuleOp; 21 | 22 | } // namespace mlir 23 | 24 | namespace spu::compiler { 25 | 26 | class CompilationContext; 27 | 28 | class Core final { 29 | public: 30 | explicit Core(CompilationContext *ctx); 31 | 32 | void doit(mlir::ModuleOp module); 33 | 34 | private: 35 | CompilationContext *ctx_; 36 | 37 | #ifdef EXPOSE_PIPELINE_BUILDER 38 | public: 39 | #else 40 | private: // NOLINT: 41 | #endif 42 | void buildPipeline(mlir::PassManager *pm); 43 | }; 44 | 45 | } // namespace spu::compiler 46 | -------------------------------------------------------------------------------- /libspu/kernel/hlo/shift.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | #include "libspu/core/value.h" 18 | 19 | namespace spu { 20 | class SPUContext; 21 | } 22 | 23 | namespace spu::kernel::hlo { 24 | 25 | spu::Value Lshift(SPUContext *ctx, const spu::Value &operand, 26 | const spu::Value &bits_to_shift); 27 | 28 | spu::Value Rshift(SPUContext *ctx, const spu::Value &operand, 29 | const spu::Value &bits_to_shift); 30 | 31 | spu::Value ARshift(SPUContext *ctx, const spu::Value &operand, 32 | const spu::Value &bits_to_shift); 33 | 34 | } // namespace spu::kernel::hlo 35 | -------------------------------------------------------------------------------- /libspu/compiler/tests/interpret/template/basic_unary.template: -------------------------------------------------------------------------------- 1 | func.func @%OP%_op_test_%IN0_DTYPE%_%OUT0_DTYPE%_p() { 2 | %0 = pphlo.constant dense<%INPUT0%> : tensor<%IN0_SHAPE%x%IN0_DTYPE%> 3 | %1 = pphlo.%OP% %0 : (tensor<%IN0_SHAPE%x%IN0_DTYPE%>)->tensor<%OUT0_SHAPE%x%OUT0_DTYPE%> 4 | %2 = pphlo.constant dense<%EXPECTED0%> : tensor<%OUT0_SHAPE%x%OUT0_DTYPE%> 5 | pphlo.custom_call @%CHECKER%(%1, %2) %ATTR%: (tensor<%OUT0_SHAPE%x%OUT0_DTYPE%>, tensor<%OUT0_SHAPE%x%OUT0_DTYPE%>)->() 6 | func.return 7 | } 8 | 9 | // ----- 10 | 11 | func.func @%OP%_op_test_%IN0_DTYPE%_%OUT0_DTYPE%_s() { 12 | %0 = pphlo.constant dense<%INPUT0%> : tensor<%IN0_SHAPE%x%IN0_DTYPE%> 13 | %1 = pphlo.convert %0 : (tensor<%IN0_SHAPE%x%IN0_DTYPE%>)->tensor<%IN0_SHAPE%x!pphlo.secret<%IN0_DTYPE%>> 14 | %2 = pphlo.%OP% %1 : (tensor<%IN0_SHAPE%x!pphlo.secret<%IN0_DTYPE%>>)->tensor<%OUT0_SHAPE%x!pphlo.secret<%OUT0_DTYPE%>> 15 | %3 = pphlo.constant dense<%EXPECTED0%> : tensor<%OUT0_SHAPE%x%OUT0_DTYPE%> 16 | %4 = pphlo.convert %2 : (tensor<%OUT0_SHAPE%x!pphlo.secret<%OUT0_DTYPE%>>)->tensor<%OUT0_SHAPE%x%OUT0_DTYPE%> 17 | pphlo.custom_call @%CHECKER%(%3, %4) %ATTR%: (tensor<%OUT0_SHAPE%x%OUT0_DTYPE%>, tensor<%OUT0_SHAPE%x%OUT0_DTYPE%>)->() 18 | func.return 19 | } 20 | -------------------------------------------------------------------------------- /libspu/compiler/codegen/codegen.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Ant Group Co., Ltd. 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 | #include "libspu/compiler/codegen/codegen.h" 16 | 17 | #include "llvm/Support/raw_ostream.h" 18 | 19 | #include "libspu/version.h" 20 | 21 | namespace spu::compiler { 22 | 23 | std::string CodeGen::doit(mlir::ModuleOp module) { 24 | // Add ir version attr 25 | module->setAttr("pphlo.version", 26 | mlir::StringAttr::get(module->getContext(), getVersionStr())); 27 | // Emit module 28 | std::string ir_dump; 29 | llvm::raw_string_ostream stream(ir_dump); 30 | module.print(stream); 31 | 32 | return stream.str(); 33 | } 34 | 35 | } // namespace spu::compiler 36 | -------------------------------------------------------------------------------- /libspu/dialect/pphlo/IR/interface.td: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Ant Group Co., Ltd. 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 | 16 | #ifndef SPU_DIALECT_PPHLO_INTERFACE 17 | #define SPU_DIALECT_PPHLO_INTERFACE 18 | 19 | include "mlir/IR/OpBase.td" 20 | 21 | def PPHloTypeInterface : TypeInterface<"PPHloTypeInterface"> { 22 | let cppNamespace = "mlir::spu::pphlo"; 23 | 24 | let description = [{ 25 | Interface for encapsulating the common properties of pphlo types. 26 | }]; 27 | 28 | let methods = [ 29 | InterfaceMethod< 30 | /*description=*/"Get bit-width of the type.", 31 | /*retTy=*/"unsigned", 32 | /*methodName=*/"getWidth" 33 | > 34 | ]; 35 | } 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /libspu/mpc/cheetah/rlwe/types.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | #include 18 | 19 | #include "seal/ciphertext.h" 20 | #include "seal/plaintext.h" 21 | #include "seal/secretkey.h" 22 | #include "seal/serialization.h" 23 | 24 | namespace spu::mpc::cheetah { 25 | 26 | using RLWESecretKey = seal::SecretKey; 27 | 28 | using RLWEPublicKey = seal::PublicKey; 29 | 30 | using KSwitchKeys = seal::KSwitchKeys; 31 | 32 | using GaloisKeys = seal::GaloisKeys; 33 | 34 | using RLWECt = seal::Ciphertext; 35 | 36 | using RLWEPt = seal::Plaintext; 37 | 38 | class LWECt; 39 | 40 | class PhantomLWECt; 41 | 42 | } // namespace spu::mpc::cheetah -------------------------------------------------------------------------------- /bazel/emp-tool.BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Ant Group Co., Ltd. 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 | load("@spulib//bazel:spu.bzl", "spu_cmake_external") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | filegroup( 20 | name = "all_srcs", 21 | srcs = glob(["**"]), 22 | ) 23 | 24 | spu_cmake_external( 25 | name = "emp-tool", 26 | cache_entries = { 27 | "OPENSSL_ROOT_DIR": "$EXT_BUILD_DEPS/openssl", 28 | "BUILD_TESTING": "OFF", 29 | }, 30 | lib_source = ":all_srcs", 31 | out_data_dirs = ["cmake"], 32 | out_static_libs = [ 33 | "libemp-tool.a", 34 | ], 35 | deps = [ 36 | "@com_github_openssl_openssl//:openssl", 37 | ], 38 | ) 39 | -------------------------------------------------------------------------------- /libspu/mpc/spdz2k/io_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Ant Group Co., Ltd. 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 | #include "libspu/mpc/io_test.h" 16 | 17 | #include "libspu/mpc/spdz2k/io.h" 18 | 19 | namespace spu::mpc::spdz2k { 20 | 21 | INSTANTIATE_TEST_SUITE_P( 22 | Spdz2kIoTest, IoTest, 23 | testing::Combine(testing::Values(makeSpdz2kIo), // 24 | testing::Values(2), // 25 | testing::Values(FieldType::FM64)), 26 | [](const testing::TestParamInfo& info) { 27 | return fmt::format("{}x{}", std::get<1>(info.param), 28 | std::get<2>(info.param)); 29 | }); 30 | 31 | } // namespace spu::mpc::spdz2k 32 | -------------------------------------------------------------------------------- /sml/utils/BUILD.bazel: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Ant Group Co., Ltd. 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 | load("@rules_python//python:defs.bzl", "py_library") 16 | 17 | package(default_visibility = ["//visibility:public"]) 18 | 19 | py_library( 20 | name = "emulation", 21 | srcs = [ 22 | "emulation.py", 23 | "//libspu:spu_py_proto", 24 | ], 25 | data = [ 26 | "//examples/python/conf", # FIXME 27 | ], 28 | deps = [ 29 | "//spu:init", 30 | "//spu/utils:distributed", 31 | ], 32 | ) 33 | 34 | py_library( 35 | name = "fxp_approx", 36 | srcs = ["fxp_approx.py"], 37 | ) 38 | 39 | py_library( 40 | name = "extmath", 41 | srcs = ["extmath.py"], 42 | ) 43 | -------------------------------------------------------------------------------- /libspu/compiler/tests/interpret/sort.mlir: -------------------------------------------------------------------------------- 1 | // RUN: spu-translate --protocol_kind=1 --interpret -split-input-file %s 2 | // RUN: spu-translate --protocol_kind=2 --interpret -split-input-file %s 3 | // RUN: spu-translate --protocol_kind=3 --interpret -split-input-file %s 4 | 5 | func.func @sort_stable() { 6 | %input0 = pphlo.constant dense<[[1, 2, 3], [3, 2, 1]]> : tensor<2x3xi64> 7 | %input1 = pphlo.constant dense<[[3, 2, 1], [1, 2, 3]]> : tensor<2x3xi64> 8 | %result0, %result1 = "pphlo.sort"(%input0, %input1) ({ 9 | ^bb0(%arg0: tensor, %arg1: tensor, %arg2: tensor, %arg3: tensor): 10 | %predicate = pphlo.greater %arg0, %arg1 : (tensor, tensor) -> tensor 11 | pphlo.return %predicate : tensor 12 | }) { 13 | dimension = 0 : i64, 14 | is_stable = true 15 | } : (tensor<2x3xi64>, tensor<2x3xi64>) -> (tensor<2x3xi64>, tensor<2x3xi64>) 16 | %expected0 = pphlo.constant dense<[[3, 2, 3], [1, 2, 1]]> : tensor<2x3xi64> 17 | %expected1 = pphlo.constant dense<[[1, 2, 1], [3, 2, 3]]> : tensor<2x3xi64> 18 | pphlo.custom_call @expect_eq (%result0, %expected0) : (tensor<2x3xi64>,tensor<2x3xi64>)->() 19 | pphlo.custom_call @expect_eq (%result1, %expected1) : (tensor<2x3xi64>,tensor<2x3xi64>)->() 20 | func.return 21 | } 22 | -------------------------------------------------------------------------------- /libspu/kernel/hal/type_cast.h: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | #include "libspu/core/value.h" 18 | 19 | namespace spu { 20 | class SPUContext; 21 | } 22 | 23 | namespace spu::kernel::hal { 24 | 25 | /// cast dtype 26 | // @param in, the input value 27 | // @param to_type, destination dtype 28 | Value dtype_cast(SPUContext* ctx, const Value& in, DataType to_type); 29 | 30 | /// cast public to secret 31 | // @param in, the input value 32 | Value seal(SPUContext* ctx, const Value& x); 33 | 34 | /// reveal a secret 35 | // @param in, the input value 36 | Value reveal(SPUContext* ctx, const Value& x); 37 | 38 | } // namespace spu::kernel::hal 39 | -------------------------------------------------------------------------------- /libspu/compiler/tests/passes/hlo2pphlo/sort_s.mlir: -------------------------------------------------------------------------------- 1 | // RUN: spu-opt --hlo-legalize-to-pphlo=input_vis_list=VIS_SECRET --lower-conversion-cast --split-input-file %s | FileCheck %s 2 | 3 | func.func @main(%arg0: tensor<20xi32>) -> (tensor<20xi32>) { 4 | %0 = stablehlo.iota dim = 0 : tensor<20xi32> 5 | // CHECK: %1:2 = "pphlo.sort"(%arg0, %0) ({ 6 | // CHECK: ^bb0(%arg1: tensor>, %arg2: tensor>, %arg3: tensor, %arg4: tensor): 7 | // CHECK: %2 = pphlo.less %arg1, %arg2 : (tensor>, tensor>) -> tensor> 8 | // CHECK: pphlo.return %2 : tensor> 9 | // CHECK: }) {dimension = 0 : i64, is_stable = true} : (tensor<20x!pphlo.secret>, tensor<20xi32>) -> (tensor<20x!pphlo.secret>, tensor<20x!pphlo.secret>) 10 | %1:2 = "stablehlo.sort"(%arg0, %0) ({ 11 | ^bb0(%arg1: tensor, %arg2: tensor, %arg3: tensor, %arg4: tensor): 12 | %2 = stablehlo.compare LT, %arg1, %arg2 : (tensor, tensor) -> tensor 13 | stablehlo.return %2 : tensor 14 | }) {dimension = 0 : i64, is_stable = true} : (tensor<20xi32>, tensor<20xi32>) -> (tensor<20xi32>, tensor<20xi32>) 15 | return %1#1: tensor<20xi32> 16 | } 17 | -------------------------------------------------------------------------------- /libspu/compiler/tests/interpret/exponential_minus_one.mlir: -------------------------------------------------------------------------------- 1 | // RUN: spu-translate --protocol_kind=1 --interpret -split-input-file %s 2 | // RUN: spu-translate --protocol_kind=2 --interpret -split-input-file %s 3 | // RUN: spu-translate --protocol_kind=3 --interpret -split-input-file %s 4 | // AUTO GENERATED, DO NOT EDIT 5 | 6 | func.func @exponential_minus_one_op_test_f64_f64_p() { 7 | %0 = pphlo.constant dense<[0.0, 1.0]> : tensor<2xf64> 8 | %1 = pphlo.exponential_minus_one %0 : (tensor<2xf64>)->tensor<2xf64> 9 | %2 = pphlo.constant dense<[0.0, 1.7182818284590451]> : tensor<2xf64> 10 | pphlo.custom_call @expect_almost_eq(%1, %2) : (tensor<2xf64>, tensor<2xf64>)->() 11 | func.return 12 | } 13 | 14 | // ----- 15 | 16 | func.func @exponential_minus_one_op_test_f64_f64_s() { 17 | %0 = pphlo.constant dense<[0.0, 1.0]> : tensor<2xf64> 18 | %1 = pphlo.convert %0 : (tensor<2xf64>)->tensor<2x!pphlo.secret> 19 | %2 = pphlo.exponential_minus_one %1 : (tensor<2x!pphlo.secret>)->tensor<2x!pphlo.secret> 20 | %3 = pphlo.constant dense<[0.0, 1.7182818284590451]> : tensor<2xf64> 21 | %4 = pphlo.convert %2 : (tensor<2x!pphlo.secret>)->tensor<2xf64> 22 | pphlo.custom_call @expect_almost_eq(%3, %4) : (tensor<2xf64>, tensor<2xf64>)->() 23 | func.return 24 | } 25 | -------------------------------------------------------------------------------- /libspu/kernel/hlo/const.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Ant Group Co., Ltd. 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 | #pragma once 16 | 17 | #include "libspu/core/pt_buffer_view.h" 18 | #include "libspu/core/value.h" 19 | 20 | namespace spu { 21 | class SPUContext; 22 | } 23 | 24 | namespace spu::kernel::hlo { 25 | 26 | spu::Value Constant(SPUContext *ctx, const PtBufferView &view, 27 | const Shape &out_shape); 28 | 29 | spu::Value Iota(SPUContext *ctx, DataType dtype, int64_t numel); 30 | 31 | // Returns the SPU epsilon, the positive distance between two fixed point value. 32 | // 33 | // The result visibility is public. 34 | spu::Value Epsilon(SPUContext *ctx, DataType dtype); 35 | 36 | } // namespace spu::kernel::hlo 37 | -------------------------------------------------------------------------------- /libspu/mpc/aby3/io_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Ant Group Co., Ltd. 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 | #include "libspu/mpc/io_test.h" 16 | 17 | #include "libspu/mpc/aby3/io.h" 18 | 19 | namespace spu::mpc::aby3 { 20 | 21 | INSTANTIATE_TEST_SUITE_P( 22 | Aby3IoTest, IoTest, 23 | testing::Combine(testing::Values(makeAby3Io), // 24 | testing::Values(3), // 25 | testing::Values(FieldType::FM32, FieldType::FM64, 26 | FieldType::FM128)), 27 | [](const testing::TestParamInfo& p) { 28 | return fmt::format("{}x{}", std::get<1>(p.param), std::get<2>(p.param)); 29 | }); 30 | 31 | } // namespace spu::mpc::aby3 32 | --------------------------------------------------------------------------------