Skip to main content

This package helps to reduce the amount of boilerplate code when creating Airflow DAGs from Python callables.

Project description

PyPI version pipeline coverage

pytensor-federated

This package implements federated computing with PyTensor.

Using pytensor-federated, differentiable cost functions can be computed on federated nodes. Inputs and outputs are transmitted in binary via a bidirectional gRPC stream.

A client side LogpGradOp is provided to conveniently embed federated compute operations in PyTensor graphs such as a PyMC model.

The example code implements a simple Bayesian linear regression to data that is "private" to the federated compute process.

Run each command in its own terminal:

python demo_node.py
python demo_model.py

Architecture

pytensor-federated is designed to be a very generalizable framework for federated computing with gRPC, but it comes with implementations for PyTensor, and specifically for use cases of Bayesian inference. This is reflected in the actual implementation, where the most basic gRPC service implementation -- the ArraysToArraysService -- is wrapped by a few implementation flavors, specifically for common use cases in Bayesian inference.

At the core, everything is built around an ArraysToArrays gRPC service, which takes any number of (NumPy) arrays as parameters, and returns any number of (NumPy) arrays as outputs. The arrays can have arbitrary dtype or shape, as long as the buffer interface is supported (meaning dtype=object doesn't work, but datetime dtypes are ok).

This ArraysToArraysService can be used to wrap arbitrary model functions, thereby enabling to run model simulations and MCMC/optimization on different machines. The protobuf files that specify the data types and gRPC interface can be compiled to other programming languages, such that the model implementation could be C++, while MCMC/optimization run in Python.

For the Bayesian inference or optimization use case, it helps to first understand the inputs and outputs of the undelying computation graph. For example, parameter estimation with a differential equation model requires...

  • observations to which the model should be fitted
  • timepoints at which there were observations
  • parameters (including initial states) theta, some of which are to be estimated

From timepoints and parameters theta, the model predicts trajectories. Together with observations, these predictions are fed into some kind of likelihood function, which produces a scalar log-likelihood log-likelihood as the output.

Different sub-graphs of this example could be wrapped by an ArraysToArraysService:

  • [theta,] -> [log-likelihood,]
  • [timepoints, theta] -> [trajectories,]
  • [timepoints, observations, theta] -> [log-likelihood,]

If the entire model is differentiable, one can even return gradients. For example, with a linear model: [slope, intercept] -> [LL, dLL_dslope, dLL_dintercept].

The role of PyTensor here is purely technical: PyTensor is a graph computation framework that implements auto-differentiation. Wrapping the ArraysToArraysServiceClient in PyTensor Ops simply makes it easier to build more sophisticated compute graphs. PyTensor is also the computatation backend for PyMC, which is the most popular framework for Bayesian inference in Python.

Installation & Contributing

conda env create -f environment.yml

Additional dependencies are needed to compile the protobufs:

conda install -c conda-forge libprotobuf-static
pip install --pre betterproto[compiler]
python protobufs/generate.py

Set up pre-commit for automated code style enforcement:

pip install pre-commit
pre-commit install

Project details


Download files

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

Source Distribution

pytensor_federated-1.0.2.tar.gz (35.3 kB view details)

Uploaded Source

Built Distribution

pytensor_federated-1.0.2-py3-none-any.whl (38.9 kB view details)

Uploaded Python 3

File details

Details for the file pytensor_federated-1.0.2.tar.gz.

File metadata

  • Download URL: pytensor_federated-1.0.2.tar.gz
  • Upload date:
  • Size: 35.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.10

File hashes

Hashes for pytensor_federated-1.0.2.tar.gz
Algorithm Hash digest
SHA256 36f73bee8885970b2b79d2bada63818ab3f408eb266f0c342a8a4afee9911c9a
MD5 81c26f1e2bf1d7f95ecab6339a728635
BLAKE2b-256 95bb3c48d30450493def31df20548701531135818e63a3d986bbc32c5f771a34

See more details on using hashes here.

File details

Details for the file pytensor_federated-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for pytensor_federated-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c185e71b6f200d0f84899ffd54546df36e72a6bb3eb9a3d6ba1811c2cfd88da6
MD5 57b2cab665061a14d4386fe7cd64c0ae
BLAKE2b-256 468c5b9434e241c98beae2b4c6350767ec3889d098db90b227df9e2b5a03731e

See more details on using hashes here.

Supported by

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