NVIDIA logo

NVIDIA FLARE

NVIDIA FLARE™ (NVIDIA Federated Learning Application Runtime Environment) is a domain-agnostic, open-source, and extensible SDK for Federated Learning. It allows researchers and data scientists to adapt existing ML/DL workflow to a federated paradigm and enables platform developers to build a secure, privacy-preserving offering for a distributed multi-party collaboration.

Getting Started with NVIDIA FLARE

Begin your NVIDIA FLARE journey with these guides designed to help you quickly grasp essential concepts. Follow along with the videos below, and try it out yourself.

Example Code

Try out these example code blocks below, where we showcase how simple it is to adapt a popular machine learning framework to a federated learning scenario with NVIDIA FLARE. For more details, refer to the getting started walkthrough guide above.

NVIDIA logo Run in Google Colab View on Github

Installation

Install the required TensorFlow dependencies for this example.

pip install nvflare tensorflow

Client Code (cifar10_tf_fl.py)

We use the Client API to convert the centralized training TensorFlow code into federated learning code with only a few lines of changes highlighted below. Essentially the client will receive a model from NVIDIA FLARE, perform local training and validation, and then send the model back.

from tensorflow.keras import datasets
from tensorflow.keras import Model, layers, losses


class TFNet(Model):
    def __init__(self, input_shape):
        super().__init__()
        self._input_shape = input_shape  # Required to get constructor arguments in job config
        self.conv1 = layers.Conv2D(6, 5, activation="relu")
        self.pool = layers.MaxPooling2D((2, 2), 2)
        self.conv2 = layers.Conv2D(16, 5, activation="relu")
        self.flatten = layers.Flatten()
        self.fc1 = layers.Dense(120, activation="relu")
        self.fc2 = layers.Dense(84, activation="relu")
        self.fc3 = layers.Dense(10)
        loss_fn = losses.SparseCategoricalCrossentropy(from_logits=True)
        self.compile(optimizer="sgd", loss=loss_fn, metrics=["accuracy"])
        self.build(input_shape)

    def call(self, x):
        x = self.pool(self.conv1(x))
        x = self.pool(self.conv2(x))
        x = self.flatten(x)
        x = self.fc1(x)
        x = self.fc2(x)
        x = self.fc3(x)
        return x


# (1) import nvflare client API
import nvflare.client as flare

PATH = "./tf_model.ckpt"


def main():
    (train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()

    # Normalize pixel values to be between 0 and 1
    train_images, test_images = train_images / 255.0, test_images / 255.0

    model = TFNet(input_shape=(None, 32, 32, 3))
    model.summary()

    # (2) initializes NVFlare client API
    flare.init()

    # (3) gets FLModel from NVFlare
    while flare.is_running():
        input_model = flare.receive()
        print(f"current_round={input_model.current_round}")

        # (optional) print system info
        system_info = flare.system_info()
        print(f"NVFlare system info: {system_info}")

        # (4) loads model from NVFlare
        for k, v in input_model.params.items():
            model.get_layer(k).set_weights(v)

        # (5) evaluate aggregated/received model
        _, test_global_acc = model.evaluate(test_images, test_labels, verbose=2)
        print(
            f"Accuracy of the received model on round {input_model.current_round} on the 10000 test images:
            {test_global_acc * 100} %"
        )

        model.fit(train_images, train_labels, epochs=1, validation_data=(test_images, test_labels))

        print("Finished Training")

        model.save_weights(PATH)

        _, test_acc = model.evaluate(test_images, test_labels, verbose=2)
        print(f"Accuracy of the model on the 10000 test images: {test_acc * 100} %")

        # (6) construct trained FL model (A dict of {layer name: layer weights} from the keras model)
        output_model = flare.FLModel(
            params={layer.name: layer.get_weights() for layer in model.layers},
            metrics={"accuracy": test_global_acc}
        )
        # (7) send model back to NVFlare
        flare.send(output_model)


if __name__ == "__main__":
    main()

Run the Job

To run the job with the simulator, copy the code and execute the job script, or run in Google Colab. Alternatively, export the job to a configuration and run the job in other modes.

python3 fedavg_cifar10_tf_job.py

Tutorials

These progressive tutorial series cover various aspects of NVIDIA FLARE from core concepts and basic tools to advanced algorithms and deployments. For a comprehensive list of all tutorials, view the tutorial catalog below.

Why NVIDIA FLARE?

Research & Productivity

Provides user-friendly APIs for client and server programming. Use the Simulator and POC modes to quickly simulate a federated learning application.

Framework Agnostic

Designed as a federated computing platform agnostic to frameworks, workloads, datasets, and domains. Federated learning apps are built on this foundation.

Open Architecture Design

Flexible open architecture allows for extensive customization, with a modular design that ensures each layer can be easily pluggable with custom components.

Security & Privacy

Prioritizes security with secure provisioning, event-based security plugins, authorization control, data filtering, audit logs, and advanced privacy-preserving algorithms.

3rd Party Integration

Enables a seamless integration with external 3rd-party systems with the FlareAgent to receive tasks and submit results to the NVIDIA FLARE server.

Cloud Deployment

Supports cross-cloud deployment with various management tools. Designed for robust, production-scale deployment in real-world federated learning scenarios.

Learn More

What is Federated Learning?

Federated Learning is a distributed learning paradigm where training occurs across multiple clients, each with their own local datasets. This enables the creation of common robust models without sharing sensitive local data, helping solve issues of data privacy and security.

Read more

Blogs & Articles

NVIDIA Federated Learning Technical Blogs covering various applications and topics.

Read more

Publications & Talks

List of NVIDIA FLARE related papers and publications along with videos from blogs and talks.

Read more