SwiftSyft makes it easy for you to train and inference PySyft models on iOS devices. This allows you to utilize training data located directly on the device itself, bypassing the need to send a user's data to a central server. This is known as federated learning.
- โ๏ธ Training and inference of any PySyft model written in PyTorch or TensorFlow
- ๐ค Allows all data to stay on the user's device
- ๐ Support for delegation to background task scheduler
- ๐ Support for JWT authentication to protect models from Sybil attacks
- ๐ Host of inbuilt best practices to prevent apps from over using device resources.
- ๐ Charge detection to allow background training only when device is connected to charger
- ๐ค Sleep and wake detection so that the app does not occupy resource when user starts using the device
- ๐ธ Wifi and metered network detection to ensure the model updates do not use all the available data quota
- ๐ All of these smart defaults are easily are overridable
- ๐ Support for both reactive and callback patterns so you have your freedom of choice (in progress)
- ๐ Support for secure multi-party computation and secure aggregation protocols using peer-to-peer WebRTC connections (in progress).
There are a variety of additional privacy-preserving protections that may be applied, including differential privacy, muliti-party computation, and secure aggregation.
OpenMined set out to build the world's first open-source ecosystem for federated learning on web and mobile. SwiftSyft is a part of this ecosystem, responsible for bringing secure federated learning to iOS devices. You may also train models on Android devices using KotlinSyft or in web browsers using syft.js.
If you want to know how scalable federated systems are built, Towards Federated Learning at Scale is a fantastic introduction!
We have not currently made our initial release. SwiftSyft would soon be available on Cocoapods.
As a developer, there are few steps to building your own secure federated learning system upon the OpenMined infrastructure:
- ๐ค Generate your secure ML model using PySyft. By design, PySyft is built upon PyTorch and TensorFlow so you don't need to learn a new ML framework. You will also need to write a training plan (training code the worker runs) and an averaging plan (code that PyGrid runs to average the model diff).
- ๐ Host your model and plans on PyGrid which will deal with all the federated learning components of your pipeline. You will need to set up a PyGrid server somewhere, please see their installation instructions on how to do this.
- ๐ Start training on the device!
๐ The entire workflow and process is described in greater detail in our project roadmap.
You can use SwiftSyft as a front-end or as a background service. The following is a quick start example usage:
// This is a demonstration of how to use SwiftSyft with PyGrid to train a plan on local data on an iOS device
// Create a client with a PyGrid server URL
if let syftClient = SyftClient(url: URL(string: "ws://127.0.0.1:5000")!) {
// Store the client as a property so it doesn't get deallocated during training.
self.syftClient = syftClient
// Create a new federated learning job with the model name and version
self.syftJob = syftClient.newJob(modelName: "mnist", version: "1.0.0")
// This function is called when SwiftSyft has downloaded the plans and model parameters from PyGrid
// You are ready to train your model on your data
// plan - Use this to generate diffs using our training data
// clientConfig - contains the configuration for the training cycle (batchSize, learning rate) and
// metadata for the model (name, version)
// modelReport - Used as a completion block and reports the diffs to PyGrid.
self.syftJob?.onReady(execute: { plan, clientConfig, modelReport in
do {
// This returns a lazily evaluated sequence for each MNIST image and the corresponding label
// It divides the training data and the label by batches
let (mnistData, labels) = try MNISTLoader.load(setType: .train, batchSize: clientConfig.batchSize)
// Iterate through each batch of MNIST data and label
for case let (batchData, labels) in zip(mnistData, labels) {
// We need to create an autorelease pool to release the training data from memory after each loop
try autoreleasepool {
// Preprocess MNIST data by flattening all of the MNIST batch data as a single array
let flattenedBatch = MNISTLoader.flattenMNISTData(batchData)
// Preprocess the label ( 0 to 9 ) by creating one-hot features and then flattening the entire thing
let oneHotLabels = MNISTLoader.oneHotMNISTLabels(labels: labels).compactMap { Float($0)}
// Since we don't have native tensor wrappers in Swift yet, we use
// `TrainingData` and `ValidationData` classes to store the data and shape.
let trainingData = try TrainingData(data: flattenedBatch, shape: [clientConfig.batchSize, 784])
let validationData = try ValidationData(data: oneHotLabels, shape: [clientConfig.batchSize, 10])
// Execute the plan with the training data and validation data. `plan.execute()`
// returns the loss and you can use it if you want to (plan.execute()
// has the @discardableResult attribute)
let loss = plan.execute(trainingData: trainingData,
validationData: validationData,
clientConfig: clientConfig)
}
}
// Generate diff data and report the final diffs as
let diffStateData = try plan.generateDiffData()
modelReport(diffStateData)
} catch let error {
// Handle any error from the training cycle
debugPrint(error.localizedDescription)
}
})
// This is the error handler for any job exeuction errors like connecting to PyGrid
self.syftJob?.onError(execute: { error in
print(error)
})
// Start the job. You can set that the job should only execute if the device is being charge and there is
// a WiFi connection. These options are on by default if you don't specify them.
self.syftJob?.start(chargeDetection: true, wifiDetection: true)
}
A mini tutorial on how to run SwiftSyft
on iOS using the background task scheduler can be found here
The demo app fetches the plans, protocols and model weights from PyGrid server hosted locally. The plans are then deserialized and executed using libtorch.
Follow these steps to setup an environment to run the demo app:
- Clone the repo PyGrid and change directory to it
git clone https://github.com/OpenMined/PyGrid
cd PyGrid
- Install docker
- Install docker-compose.
- Execute
docker-compose
in the command line to start pygrid server.
docker-compose up
- Install PySyft
v0.2.5
in the virtual environment.
virtualenv -p python3 venv
source venv/bin/activate
pip install syft==0.2.5, jupyter==1.0.0, notebook==5.7.8
- Host Jupyter Notebook
jupyter notebook
- Open a browser and navigate to localhost:8888. You should be able to see the pysyft notebook console.
- In the Jupyter Notebook, navigate to
examples/experimental/FL Training Plan
- Run the notebooks
Create Plan
. It should save three files in theFL Training Plan
folder - Run the notebook
Host Plan
. Now PyGrid is setup and the model is hosted over it.
syft.base_url="<IP_address_from_step_16>:5000"
- Set-up demo project using Cocoapods
- Install Cocoapods
gem install cocoapods
- Install the dependencies of the project.
pod install # On the root directory of this project
- Open the file
SwiftSyft.xcworkspace
in Xcode. - Run the
SwiftSyft
project. It automatically uses127.0.0.1:5000
as the PyGrid URL.
You can work on the project by running pod install
in the root directory. Then open the file SwiftSyft.xcworkspace
in Xcode. When the project is open on Xcode, you can work on the SwiftSyft
pod itself in Pods/Development Pods/SwiftSyft/Classes/*
- Star, for and clone the repo
- Open the project in Xcode
- Check out current issues and Github. For newcomers, check out issues labeled
Good first issue
- Do your work
- Push your fork
- Submit a PR to OpenMined/SwiftSyft
Read the contribution guide as a good starting place. Additionally, we welcome you to the slack for queries related to the library and contribution in general. The Slack channel #lib_swift_syft
is specific to SwiftSyft development, the Slack channel #lib_syft_mobile
is meant for both Android and iOS teams. See you there!
Thanks goes to these wonderful people (emoji key):
Mark Jimenez ๐ป ๐ |
Madalin Mamuleanu ๐ป |
This project follows the all-contributors specification. Contributions of any kind welcome!