Skip to main content

Hadal: Privacy preserving machine learning with Tensorflow and the SHELL encryption library, built for python 3.10.

Project description

hadal-flow

Build Formatting

Train ML models with strong privacy.

hadal_flow is a TensorFlow extension that uses Homomorphic Encryption (HE) to train models with centralized label differential privacy (Label DP) guarantees, without requiring a trusted third party.

It's built for the "vertically partitioned" scenario where one party has the features and another party has the labels. This library implements the protocols from the Hadal research paper to securely train a model without the feature-holder ever seeing the plain-text labels.

This is not an officially supported Google product.

Getting Started

pip install hadal-flow

See ./examples/ for how to use the library.

The Problem: Centralized DP without a Trusted Party

When training with high privacy requirements (e.g., $\epsilon \le 1$), centralized DP (like DP-SGD) provides much higher model accuracy than local DP (like randomized response). However, centralized DP traditionally requires a trusted third-party "curator" that can see all the data (or at least the secret labels) to compute and noise the gradients. This is a problem when data is vertically partitioned: Party F has the features and wants to train the model. Party L has the secret labels and cannot share them. How can Party F train its model using Party L's labels without a trusted intermediary?

The Solution: HE-based Backpropagation

hadal-flow uses Homomorphic Encryption (via Google's SHELL library) to cryptographically "simulate" the trusted curator. The core technical idea is based on the Features-And-Model-vs-Labels (FAML) data partitioning:

  1. Forward Pass (Plaintext): Party F (with features and the model) computes the entire forward pass in plaintext, right up to the final layer's logits.
  2. Encrypted Labels: Party L encrypts its batch of labels using HE and sends the single ciphertext to Party F.
  3. Backward Pass (Encrypted): The gradient of the loss (e.g., CCE with Softmax) is often a simple affine function of the labels (like $\hat{y} - y$). Party F can compute this step homomorphically using its plaintext logits and Party L's encrypted labels.
  4. Model Update: Party F finishes the backpropagation, adds the required DP noise, and updates its model weights.

The result is a model trained with the high utility of centralized DP, but Party F never sees Party L's individual labels.

What's Included?

The library is split into two packages:

  • hadal_flow: The base package. It integrates TensorFlow with the SHELL library, providing a ShellTensor type for basic HE-enabled computations.
  • hadal_ml: The machine learning library. It implements two different protocols for the encrypted backpropagation step:
    • POSTSCALE: A novel protocol that is highly efficient for models with a low number of output classes (e.g., binary classification).
    • HE-DP-SGD: A more direct HE implementation of backpropagation, which is better suited for models with many output classes.

Building

Build From Source

  1. Install bazel and python3 or use the devcontainer.

  2. Run the tests.

    bazel test //hadal_flow/...
    bazel test //hadal_ml/...  # Large tests, requires 128GB of memory.
    
  3. Build the code.

    bazel build //:wheel
    bazel run //:wheel_rename
    
  4. (Optional) Install the wheel, e.g. to try out the ./examples/. You may first need to copy the wheel out of the devcontainer's filesystem.

    cp -f bazel-bin/*.whl ./  # Run in devcontainer if using.
    

    Then install.

    pip install --force-reinstall hadal_flow-*.whl  # Run in target environment.
    

Note the cpython api is not compatible across minor python versions (e.g. 3.10, 3.11) so the wheel must be rebuilt for each python version.

Code Formatters and Counters

bazel run //:bazel_formatter
bazel run //:python_formatter
bazel run //:clang_formatter
cloc ./ --fullpath --not-match-d='/(bazel-.*|.*\.venv)/'

Update Python Dependencies

Update requirements.in and run the following to update the requirements files for each python version.

for ver in 3_9 3_10 3_11 3_12 3_13; do
  rm requirements_${ver}.txt
  touch requirements_${ver}.txt
  bazel run //:requirements_${ver}.update
done

bazel clean --expunge

If updating the tensorflow dependency, other dependencies may also need to change, e.g. abseil (see MODULE.bazel). This issue usually manifests as a missing symbols error in the tests when trying to import the tensorflow DSO. In this case, c++filt will help to decode the mangled symbol name and nm --defined-only .../libtensorflow_framework.so | grep ... may help find what the symbol changed to, and which dependency is causing the error.

Contributing

See CONTRIBUTING.md for details.

License

Apache 2.0; see LICENSE for details.

Disclaimer

Convolutions on AMD-based platforms may fail due to known limitations of TensorFlow. This will resulting in the following error when running tests:

CPU implementation of Conv3D currently only supports dilated rates of 1.

This project is not an official Google project. It is not supported by Google and Google specifically disclaims all warranties as to its quality, merchantability, or fitness for a particular purpose.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

hadal_flow-0.8.0-cp313-cp313-manylinux_2_39_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.39+ x86-64

hadal_flow-0.8.0-cp313-cp313-manylinux_2_35_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.35+ x86-64

hadal_flow-0.8.0-cp312-cp312-manylinux_2_39_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.39+ x86-64

hadal_flow-0.8.0-cp312-cp312-manylinux_2_35_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.35+ x86-64

hadal_flow-0.8.0-cp311-cp311-manylinux_2_39_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.39+ x86-64

hadal_flow-0.8.0-cp311-cp311-manylinux_2_35_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.35+ x86-64

hadal_flow-0.8.0-cp310-cp310-manylinux_2_39_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.39+ x86-64

hadal_flow-0.8.0-cp310-cp310-manylinux_2_35_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.35+ x86-64

File details

Details for the file hadal_flow-0.8.0-cp313-cp313-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for hadal_flow-0.8.0-cp313-cp313-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 eb30c9eec00978e983ba2b23eafc4f4e36cfbddea5532160b00bd495f09b1a7f
MD5 6f9ec41bf83145e23d50bf65b530da48
BLAKE2b-256 26f5ac720542bfe5fc07f1cd76a10f312ef9511c4ff46179a6acf34393c638b6

See more details on using hashes here.

Provenance

The following attestation bundles were made for hadal_flow-0.8.0-cp313-cp313-manylinux_2_39_x86_64.whl:

Publisher: wheel.yaml on google/hadal-flow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hadal_flow-0.8.0-cp313-cp313-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for hadal_flow-0.8.0-cp313-cp313-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 028b71ee75c9c31dc416d00a82d31478a56d6efbc4f626bfb5a05e36c77c137a
MD5 faa9b5214b55e243b6d69439b1003f93
BLAKE2b-256 5684602e5873ba5e7025743be6d5b11a36c233639ace46baaf0ead112885b13e

See more details on using hashes here.

Provenance

The following attestation bundles were made for hadal_flow-0.8.0-cp313-cp313-manylinux_2_35_x86_64.whl:

Publisher: wheel.yaml on google/hadal-flow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hadal_flow-0.8.0-cp312-cp312-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for hadal_flow-0.8.0-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 a00f538a415b7a2e63141a71e254cbb604ba64d931d18147b55db046e6ce8ce4
MD5 50221262f77cb3db23fcb4fe1c3bed3f
BLAKE2b-256 791ea94259cd7a77712ac02d4ec77e322d21d04f0dedb5bf3d5a64225d3f24ed

See more details on using hashes here.

Provenance

The following attestation bundles were made for hadal_flow-0.8.0-cp312-cp312-manylinux_2_39_x86_64.whl:

Publisher: wheel.yaml on google/hadal-flow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hadal_flow-0.8.0-cp312-cp312-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for hadal_flow-0.8.0-cp312-cp312-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 91271ae149723114e162fc3d80f04c1727c88a131225d6663396172af0159752
MD5 e3ccde3f276b7b3d09748ca214cae085
BLAKE2b-256 6721cebda1ac9c1eba031bf54ec124f902123fc81cca705f2d66b8b743acf12a

See more details on using hashes here.

Provenance

The following attestation bundles were made for hadal_flow-0.8.0-cp312-cp312-manylinux_2_35_x86_64.whl:

Publisher: wheel.yaml on google/hadal-flow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hadal_flow-0.8.0-cp311-cp311-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for hadal_flow-0.8.0-cp311-cp311-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 b2f257704f7a3558e2c962ff3e3c32663231c20eaa73b145744866636c6eeae6
MD5 7d4bb66e3fc40b5113df805c3a89b8c1
BLAKE2b-256 4658293dfec51f06e68134761f6527cb3878d0d875f281e74428f70966d153d3

See more details on using hashes here.

Provenance

The following attestation bundles were made for hadal_flow-0.8.0-cp311-cp311-manylinux_2_39_x86_64.whl:

Publisher: wheel.yaml on google/hadal-flow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hadal_flow-0.8.0-cp311-cp311-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for hadal_flow-0.8.0-cp311-cp311-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 d68afd4a40e07c72f4334307dc8c5555f49e7cc591a040349a646c19556130f5
MD5 42b24d21f8b383e1cd58585fbb51f3ed
BLAKE2b-256 8cded04f3ba39c6694b27eb0107954c76ec0c660de3407b618c24a1cdd4df36d

See more details on using hashes here.

Provenance

The following attestation bundles were made for hadal_flow-0.8.0-cp311-cp311-manylinux_2_35_x86_64.whl:

Publisher: wheel.yaml on google/hadal-flow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hadal_flow-0.8.0-cp310-cp310-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for hadal_flow-0.8.0-cp310-cp310-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 bc8d99e473494eaa65caeba3f0ba8467a381f72d1efbad4c07d9201a2c475936
MD5 11c502e094520f3eb1fb4e8064aab403
BLAKE2b-256 aae6ffeea126d6f976ebf917f1eb0cc17977047449c8ff9f12d82042e7367ec9

See more details on using hashes here.

Provenance

The following attestation bundles were made for hadal_flow-0.8.0-cp310-cp310-manylinux_2_39_x86_64.whl:

Publisher: wheel.yaml on google/hadal-flow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hadal_flow-0.8.0-cp310-cp310-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for hadal_flow-0.8.0-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 980d8e47524b9412bebdd82fc33a1d8de7312cf6ce6dcace04dbb0e9c33ddcba
MD5 40364049c50835b2c44ee2b77c1b4d1b
BLAKE2b-256 5a34b548cad32536e30f8c46aa33f110cbe20f1a01d650a46d90c35ecd6462d4

See more details on using hashes here.

Provenance

The following attestation bundles were made for hadal_flow-0.8.0-cp310-cp310-manylinux_2_35_x86_64.whl:

Publisher: wheel.yaml on google/hadal-flow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page