Skip to main content

"Python SDK library for using the Modalic Federated Learning Operations Platform."

Project description

Modalic Logo


Python SDK

Python SDK library for using the Modalic Federated Learning Operations Platform.

The SDK library serves as convenient interface for performing Federated Learning with the most common Machine Learning frameworks like Pytorch or Tensorflow written in the Python programming language. As an additional software layer within a Machine Learning pipeline, the SKD enables an individual client application to take part within a Federated Learning setup. The coordination of a distributed Machine Learning process solving a particular problem, is done by a central server or service provider which can be started via Python script using the SDK.

As the main entrypoint to a production ready FLOps Platform, this software package aims for all developers and ML practitioners that want to run ML use cases in distributed fashion.

Usage

In order to run a Federated Learning procedure two main entities have to instantiated. The client logic and the aggregation server application. Both can be started via the SDK. Currently Pytorch & Tensorflow are supported as framework to construct the ML architecture.

Run the Aggregation Server

# (1) Run the aggregation server with configuration using .toml
cfg = toml.load("${configPATH}.toml")
modalic.run_server(cfg)

The .toml file can be used to control hyperparameters for the aggregation server.

# -c configs/config.toml

# REST API settings.
[api]
# The address to which the REST API of the server
# will be bound. All requests should be sent to this address.
server_address = "127.0.0.1:8080"

# Hyperparameter controlling the Federated Learning training process.
[protocol]
# Defines the number of training rounds (global epochs) 
# that will be performed.
training_rounds = 10
# Sets the number of participants & local models 
# one global epoch should at least contain.
participants = 2

For implementing the client logic a framework of choice can be used.

Pytorch

# (2) Construct the client logic.
import modalic

# Define a FLClient object that implements all the ML logic and will
# used as an input to an internal modalic client which enables the 
# program to connect to the server an perform training in distributed fashion.
class FLClient(modalic.Client):

  def __init__(self, dataset, ...):
    self.model = Net()
    self.dataset = torch.utils.data.DataLoader(dataset, batch_size=32)
    ...

  def train(self):
    for epoch in range(0, self.epochs):
        for images, labels in self.trainloader:
            ...

    return self.model

  def serialize_local_model(self, model):
      return modalic.serialize_torch_model(model)

  def deserialize_global_model(self, global_model):
      self.model = modalic.deserialize_torch_model(
          self.model, global_model, self._get_model_shape()
      )

  def get_model_shape(self):
      return modalic.get_torch_model_shape(self.model)

  def get_model_dtype(self):
      ...

# Construct the client layer..
client = FLClient(...)

# (3) Run training for single client.
modalic.run_client(client)

Tensorflow

# (2) Construct the client logic.

class FLClient(modalic.Client):

  def __init__(self, dataset, ...):
    # Initialize & compile the MobileNetV2 model.
    self.model = tf.keras.applications.MobileNetV2((32, 32, 3), classes=10, weights=None)
    # Load the CIFAR-10 dataset using tf.keras.
    (self.x_train, self.y_train), (_, _) = tf.keras.datasets.cifar10.load_data()
    ...

  def train(self):
    ...
    self.model.fit(self.x_train, self.y_train, batch_size=self.batch_size, epochs=self.epochs)
    ...
    return self.model

  def serialize_local_model(self, model):
      return modalic.serialize_tf_keras_model(model)

  def deserialize_global_model(self, global_model):
      self.model = modalic.deserialize_tf_keras_model(
          self.model, global_model, self._get_model_shape()
      )

  def get_model_shape(self):
      return modalic.get_tf_keras_model_shape(self.model)

  def get_model_dtype(self):
      ...

# Construct the client layer..
client = FLClient(...)

# (3) Run training for single client.
modalic.run_client(client)

Please keep in mind that this code snippet shows only the logic and the general idea. For more details, check out the /examples folder that contains more in-depth and complete instruction sets and examples that are actually actionable.

Installation

The latest release of Modalic Python SDK can be installed via pip:

pip install modalic

Documentation

See the Python SDK docs for more information. Additionally, some examples for starting with Modalic are provided in this repository under the examples folder. Any Questions? Reach out to us on modalic.ai.

Development

Find more information on contributing to the open source stack and the development process in general here.

License

The Modalic Python SDK is distributed under the terms of the Apache License Version 2.0. A complete version of the license is available in LICENSE.

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

modalic-0.2.0-cp310-cp310-musllinux_1_1_x86_64.whl (8.9 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

modalic-0.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.8 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

modalic-0.2.0-cp310-cp310-manylinux_2_12_i686.manylinux2010_i686.manylinux_2_17_i686.manylinux2014_i686.whl (9.1 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.12+ i686 manylinux: glibc 2.17+ i686

modalic-0.2.0-cp310-cp310-macosx_11_0_arm64.whl (5.8 MB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

modalic-0.2.0-cp310-cp310-macosx_10_9_x86_64.whl (6.0 MB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

modalic-0.2.0-cp39-cp39-musllinux_1_1_x86_64.whl (8.9 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

modalic-0.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.8 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

modalic-0.2.0-cp39-cp39-manylinux_2_12_i686.manylinux2010_i686.manylinux_2_17_i686.manylinux2014_i686.whl (9.1 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.12+ i686 manylinux: glibc 2.17+ i686

modalic-0.2.0-cp39-cp39-macosx_11_0_arm64.whl (5.8 MB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

modalic-0.2.0-cp39-cp39-macosx_10_9_x86_64.whl (6.0 MB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

modalic-0.2.0-cp38-cp38-musllinux_1_1_x86_64.whl (8.9 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

modalic-0.2.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.8 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

modalic-0.2.0-cp38-cp38-manylinux_2_12_i686.manylinux2010_i686.manylinux_2_17_i686.manylinux2014_i686.whl (9.1 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.12+ i686 manylinux: glibc 2.17+ i686

modalic-0.2.0-cp38-cp38-macosx_11_0_arm64.whl (5.8 MB view hashes)

Uploaded CPython 3.8 macOS 11.0+ ARM64

modalic-0.2.0-cp38-cp38-macosx_10_9_x86_64.whl (6.0 MB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

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