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.7.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.7.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.7.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.7.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.7.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.7.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.7.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.7.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.7.0-cp313-cp313-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for hadal_flow-0.7.0-cp313-cp313-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 1781b40bc0e248aad65552e59b544c2dde979bb2c6011327691132c08f0c2404
MD5 48a5c6d4973d90b728d59df94b8f56be
BLAKE2b-256 6866eff0c9edb930992bd282b46707e03c7a0a0b5aff267c30da7b83aa30f974

See more details on using hashes here.

Provenance

The following attestation bundles were made for hadal_flow-0.7.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.7.0-cp313-cp313-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for hadal_flow-0.7.0-cp313-cp313-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 d5d732cd4f645733d6088243df733d87164bde79a8d2849f846b1ea8896d2843
MD5 7def7ade9d4bf82173a126f850c9166a
BLAKE2b-256 ab0978958cb3bb769073d545528ed68374caf6996608d929196485528b1b9b47

See more details on using hashes here.

Provenance

The following attestation bundles were made for hadal_flow-0.7.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.7.0-cp312-cp312-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for hadal_flow-0.7.0-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 79f25262385715df77d62d2dc3f09b1cf0f151f5aea279c924fc955a12d7354b
MD5 49be01907423fff30a99936111e39ac9
BLAKE2b-256 aef8a9141d78e68ce71e5c007b79300b82a646ac142048510dd625ee202ea77c

See more details on using hashes here.

Provenance

The following attestation bundles were made for hadal_flow-0.7.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.7.0-cp312-cp312-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for hadal_flow-0.7.0-cp312-cp312-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 9cf5e516957bbf7d28d35a247e74d6ff6fb0b18e167541d70f7bc0cbfa4d69bc
MD5 18bc6d629b38a826d02cb49e654c1bf1
BLAKE2b-256 81222e1534ccbb728a588844a75e2548969000305b245b24e4715afc94220004

See more details on using hashes here.

Provenance

The following attestation bundles were made for hadal_flow-0.7.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.7.0-cp311-cp311-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for hadal_flow-0.7.0-cp311-cp311-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 2f42369eaf4ce0cd07ec4f4ce7c463b46003d007f64445dd9f12642af662b8bb
MD5 14761c8a6cd4e7806620b48627180d30
BLAKE2b-256 1f7232cf0d423dd4b69d317c2f97e9d58ef00e30fb70978a13c7486b454450e7

See more details on using hashes here.

Provenance

The following attestation bundles were made for hadal_flow-0.7.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.7.0-cp311-cp311-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for hadal_flow-0.7.0-cp311-cp311-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 133e57bc55e04823611ec83b4c27270c93a4b866ad8598b52ac6dc9c8147325a
MD5 dbf7119c9496c15e45292b84a7266930
BLAKE2b-256 19ba5ab0af497ad4fc6d05867740f84945131e1fbec029360d1979e212440c7c

See more details on using hashes here.

Provenance

The following attestation bundles were made for hadal_flow-0.7.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.7.0-cp310-cp310-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for hadal_flow-0.7.0-cp310-cp310-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 c3cb44c79d1ffa51c779488d61e517ac6d664504b7b6215599a1ee54f2310af9
MD5 ef251b03f6e2f1611d05ee5ac28a48af
BLAKE2b-256 7e6d5f225419c45cfd6f1b4360172e3d9f19123d2183fb57250826da1d89ec3e

See more details on using hashes here.

Provenance

The following attestation bundles were made for hadal_flow-0.7.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.7.0-cp310-cp310-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for hadal_flow-0.7.0-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 5dce0f1e23150139878d2a4e12af47f030c0e123ea477334328c23224b5e368d
MD5 7ec800ad448fe5c584e6ad59bcd3d27c
BLAKE2b-256 89f70235913424ed85d2f52192f9fec52990a83f94a44ae423e9c62735812341

See more details on using hashes here.

Provenance

The following attestation bundles were made for hadal_flow-0.7.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