Federated Node Classification with Homomorphic Encryption Example

Federated Node Classification with FedGCN and Homomorphic Encryption on the Cora dataset.

(Time estimate: 3 minutes)

Load libraries

import attridict

from fedgraph.federated_methods import run_fedgraph

Specify the Node Classification with Homomorphic Encryption configuration

config = {
    # Task, Method, and Dataset Settings
    "fedgraph_task": "NC",
    "dataset": "cora",
    "method": "FedGCN",  # Federated learning method, e.g., "FedGCN"
    "iid_beta": 10000,  # Dirichlet distribution parameter for label distribution among clients
    "distribution_type": "average",  # Distribution type among clients
    # Training Configuration
    "global_rounds": 100,
    "local_step": 3,
    "learning_rate": 0.5,
    "n_trainer": 2,
    "batch_size": -1,  # -1 indicates full batch training
    # Model Structure
    "num_layers": 2,
    "num_hops": 1,  # Number of n-hop neighbors for client communication
    # Resource and Hardware Settings
    "gpu": False,
    "num_cpus_per_trainer": 1,
    "num_gpus_per_trainer": 0,
    # Logging and Output Configuration
    "logdir": "./runs",
    # Security and Privacy
    "use_encryption": True,  # Whether to use Homomorphic Encryption for secure aggregation
    # Dataset Handling Options
    "use_huggingface": False,  # Load dataset directly from Hugging Face Hub
    "saveto_huggingface": False,  # Save partitioned dataset to Hugging Face Hub
    # Scalability and Cluster Configuration
    "use_cluster": False,  # Use Kubernetes for scalability if True
}

Run fedgraph method

config = attridict(config)
run_fedgraph(config)
config:  {'fedgraph_task': 'NC', 'dataset': 'cora', 'method': 'FedGCN', 'iid_beta': 10000, 'distribution_type': 'average', 'global_rounds': 100, 'local_step': 3, 'learning_rate': 0.5, 'n_trainer': 2, 'batch_size': -1, 'num_layers': 2, 'num_hops': 1, 'gpu': False, 'num_cpus_per_trainer': 1, 'num_gpus_per_trainer': 0, 'logdir': './runs', 'use_encryption': True, 'use_huggingface': False, 'saveto_huggingface': False, 'use_cluster': False}
File already exists: ./data/cora/raw/ind.cora.x
File already exists: ./data/cora/raw/ind.cora.tx
File already exists: ./data/cora/raw/ind.cora.allx
File already exists: ./data/cora/raw/ind.cora.y
File already exists: ./data/cora/raw/ind.cora.ty
File already exists: ./data/cora/raw/ind.cora.ally
File already exists: ./data/cora/raw/ind.cora.graph
File already exists: ./data/cora/raw/ind.cora.test.index
(Trainer pid=1541) Trainer 0 loaded HE context
Loaded HE context with secret key.
Starting encrypted feature aggregation...
(Trainer pid=1541) WARNING: The input does not fit in a single ciphertext, and some operations will be disabled.
(Trainer pid=1541) The following operations are disabled in this setup: matmul, matmul_plain, enc_matmul_plain, conv2d_im2col.
(Trainer pid=1541) If you need to use those operations, try increasing the poly_modulus parameter, to fit your input.
(Trainer pid=1542) Trainer 1 loaded HE context

Pre-training Phase Metrics:
Total Pre-training Time: 24.82 seconds
Pre-training Upload: 604.43 MB
Pre-training Download: 604.44 MB
Total Pre-training Communication Cost: 1208.87 MB
global_rounds 100
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0278s
(Trainer pid=1541) WARNING: The input does not fit in a single ciphertext, and some operations will be disabled. [repeated 2x across cluster] (Ray deduplicates logs by default. Set RAY_DEDUP_LOGS=0 to disable log deduplication, or see https://docs.ray.io/en/master/ray-observability/user-guides/configure-logging.html#log-deduplication for more options.)
(Trainer pid=1541) The following operations are disabled in this setup: matmul, matmul_plain, enc_matmul_plain, conv2d_im2col. [repeated 2x across cluster]
(Trainer pid=1541) If you need to use those operations, try increasing the poly_modulus parameter, to fit your input. [repeated 2x across cluster]
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0237s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0247s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0240s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0238s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0245s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0246s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0240s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0251s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0240s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0243s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0255s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0248s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0241s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0252s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0239s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0243s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0253s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0240s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0235s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0257s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0239s
Starting encrypted parameter aggregation...
(Trainer pid=1541) WARNING: The input does not fit in a single ciphertext, and some operations will be disabled. [repeated 44x across cluster]
(Trainer pid=1541) The following operations are disabled in this setup: matmul, matmul_plain, enc_matmul_plain, conv2d_im2col. [repeated 44x across cluster]
(Trainer pid=1541) If you need to use those operations, try increasing the poly_modulus parameter, to fit your input. [repeated 44x across cluster]
Parameter aggregation completed in 0.0236s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0240s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0241s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0254s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0238s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0237s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0250s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0238s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0243s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0269s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0238s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0248s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0258s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0244s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0237s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0268s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0235s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0235s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0264s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0241s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0234s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0287s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0239s
(Trainer pid=1541) WARNING: The input does not fit in a single ciphertext, and some operations will be disabled. [repeated 44x across cluster]
(Trainer pid=1541) The following operations are disabled in this setup: matmul, matmul_plain, enc_matmul_plain, conv2d_im2col. [repeated 44x across cluster]
(Trainer pid=1541) If you need to use those operations, try increasing the poly_modulus parameter, to fit your input. [repeated 44x across cluster]
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0240s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0237s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0238s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0252s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0239s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0236s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0251s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0244s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0242s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0240s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0241s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0241s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0249s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0253s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0245s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0255s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0244s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0240s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0250s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0237s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0257s
Starting encrypted parameter aggregation...
(Trainer pid=1541) WARNING: The input does not fit in a single ciphertext, and some operations will be disabled. [repeated 44x across cluster]
(Trainer pid=1541) The following operations are disabled in this setup: matmul, matmul_plain, enc_matmul_plain, conv2d_im2col. [repeated 44x across cluster]
(Trainer pid=1541) If you need to use those operations, try increasing the poly_modulus parameter, to fit your input. [repeated 44x across cluster]
Parameter aggregation completed in 0.0241s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0238s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0235s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0238s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0238s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0253s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0237s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0249s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0242s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0238s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0260s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0243s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0237s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0262s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0236s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0238s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0255s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0252s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0241s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0251s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0241s
Starting encrypted parameter aggregation...
(Trainer pid=1541) WARNING: The input does not fit in a single ciphertext, and some operations will be disabled. [repeated 42x across cluster]
(Trainer pid=1541) The following operations are disabled in this setup: matmul, matmul_plain, enc_matmul_plain, conv2d_im2col. [repeated 42x across cluster]
(Trainer pid=1541) If you need to use those operations, try increasing the poly_modulus parameter, to fit your input. [repeated 42x across cluster]
Parameter aggregation completed in 0.0266s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0235s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0236s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0252s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0237s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0236s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0254s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0239s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0240s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0251s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0244s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0243s
Starting encrypted parameter aggregation...
Parameter aggregation completed in 0.0249s

Training Phase Metrics:
Total Training Time: 24.08 seconds
Training Upload: 573.83 MB
Training Download: 404.01 MB
Total Training Communication Cost: 977.84 MB

Overall Totals:
Total Execution Time: 54.86 seconds
Total Upload: 1178.26 MB
Total Download: 1008.45 MB
Total Communication Cost: 2186.71 MB
Pre-training Time %: 45.2%
Training Time %: 43.9%
Pre-training Comm %: 55.3%
Training Comm %: 44.7%
average_final_test_loss, 0.9379583368301392
Average test accuracy, 0.791
(Trainer pid=1542) WARNING: The input does not fit in a single ciphertext, and some operations will be disabled. [repeated 25x across cluster]
(Trainer pid=1542) The following operations are disabled in this setup: matmul, matmul_plain, enc_matmul_plain, conv2d_im2col. [repeated 25x across cluster]
(Trainer pid=1542) If you need to use those operations, try increasing the poly_modulus parameter, to fit your input. [repeated 25x across cluster]

Total running time of the script: (0 minutes 59.254 seconds)

Gallery generated by Sphinx-Gallery