### Setup Seed and Logger Source: https://github.com/alibaba/federatedscope/blob/master/materials/notebook/01_quick_start.ipynb Initializes the random seed for reproducibility and sets up the logging mechanism for monitoring the federated learning process. This ensures consistent results and provides visibility into the training progress. ```python from federatedscope.core.auxiliaries.utils import setup_seed from federatedscope.core.auxiliaries.logging import update_logger setup_seed(cfg.seed) update_logger(cfg) ``` -------------------------------- ### Install FederatedScope Organizer Client Source: https://github.com/alibaba/federatedscope/blob/master/federatedscope/organizer/README.md Installs the FederatedScope organizer components for the client. This step is necessary for clients to connect to the organizer server. Note the comment regarding `source ~/.bashrc` for non-interactive environments. ```bash # For Client pip install -e .[org] # Not full login ssh, will conduct `source ~/.bashrc` first. # ** If not running interactively, `source ~/.bashrc` might fail ** # ** due to: `[ -z "$PS1" ] && return`, please comment this line ** ``` -------------------------------- ### Prepare and Load Datasets Source: https://github.com/alibaba/federatedscope/blob/master/materials/notebook/01_quick_start.ipynb Configures and loads a dataset for federated learning using FederatedScope's DataZoo. It demonstrates setting dataset type, splits, batch size, subsampling, and transformations. The function returns the prepared data and any modifications to the configuration. ```python from federatedscope.core.auxiliaries.data_builder import get_data cfg.data.type = 'femnist' cfg.data.splits = [0.6, 0.2, 0.2] cfg.data.batch_size = 10 cfg.data.subsample = 0.05 cfg.data.transform = [['ToTensor'], ['Normalize', {'mean': [0.9637], 'std': [0.1592]}]] data, modified_cfg = get_data(cfg.clone()) cfg.merge_from_other_cfg(modified_cfg) ``` -------------------------------- ### Setup Wandb and FederatedScope in Container (Bash) Source: https://github.com/alibaba/federatedscope/blob/master/benchmark/pFL-Bench/README.md Installs and configures Weights & Biases (wandb) and the FederatedScope library within a Docker container. This includes logging into a wandb server and installing the package using setup.py. ```bash wandb login --host=http://xx.xx.xx.xx:8080/ python setup.py install ``` -------------------------------- ### Configure Model Architecture Source: https://github.com/alibaba/federatedscope/blob/master/materials/notebook/01_quick_start.ipynb Sets the model architecture for federated training. This example specifies 'convnet2' as the model type and sets the number of output channels. FederatedScope's ModelZoo provides pre-implemented architectures. ```python cfg.model.type = 'convnet2' cfg.model.out_channels = 62 ``` -------------------------------- ### Install FederatedScope Organizer Server and Redis Source: https://github.com/alibaba/federatedscope/blob/master/federatedscope/organizer/README.md Installs the FederatedScope organizer components for the server and starts a Redis instance for broker communication. Ensure Redis is running before starting the Celery worker. ```bash # For Server pip install -e .[org] docker run -d -p 6379:6379 redis ``` -------------------------------- ### Load and Print Global Configuration Source: https://github.com/alibaba/federatedscope/blob/master/materials/notebook/01_quick_start.ipynb Loads the global configuration object from FederatedScope and prints its current state. This is a foundational step for customizing FL experiments. ```python from federatedscope.core.configs.config import global_cfg cfg = global_cfg.clone() print(cfg) ``` -------------------------------- ### Install FederatedScope Source: https://github.com/alibaba/federatedscope/blob/master/benchmark/FedHPOBench/README.md Installs the FederatedScope library, a prerequisite for FedHPO-Bench. This involves cloning the repository, checking out a specific commit, and installing the package in editable mode. ```bash git clone https://github.com/alibaba/FederatedScope.git cd FederatedScope git checkout a653102c6b8d5421d2874077594df0b2e29401a1 pip install -e . ``` -------------------------------- ### Install HPOBench Source: https://github.com/alibaba/federatedscope/blob/master/benchmark/FedHPOBench/README.md Installs the HPOBench library, a popular HPO framework. This involves cloning the HPOBench repository and installing it using pip. ```bash git clone https://github.com/automl/HPOBench.git cd HPOBench pip install . ``` -------------------------------- ### Install FederatedScope Source: https://github.com/alibaba/federatedscope/blob/master/materials/notebook/(EN)CIKMCUP2022.ipynb Installs the FederatedScope library in editable mode using pip. This command is essential for setting up the running environment after cloning or copying the repository. ```python # Install FS as follows !pip install -e . --user ``` -------------------------------- ### Run Standalone Federated Learning Task Source: https://github.com/alibaba/federatedscope/blob/master/materials/notebook/01_quick_start.ipynb Initializes and runs the federated learning task in standalone mode using FederatedScope. It instantiates the FedRunner with the prepared data, server and client classes, and the final configuration, then starts the training process. ```python from federatedscope.core.fed_runner import FedRunner from federatedscope.core.auxiliaries.worker_builder import get_server_cls, get_client_cls Fed_runner = FedRunner(data=data, server_class=get_server_cls(cfg), client_class=get_client_cls(cfg), config=cfg.clone()) Fed_runner.run() ``` -------------------------------- ### Run FederatedScope in Distributed Mode (Bash) Source: https://github.com/alibaba/federatedscope/blob/master/README.md These commands illustrate how to launch the FederatedScope in distributed mode. The first set of commands shows the general structure for starting a server and multiple clients, specifying configuration files, data paths, and network addresses. The second set provides a concrete example using generated toy data. ```bash # For server python federatedscope/main.py --cfg scripts/distributed_scripts/distributed_configs/distributed_server.yaml data.file_path 'PATH/TO/DATA' distribute.server_host x.x.x.x distribute.server_port xxxx # For clients python federatedscope/main.py --cfg scripts/distributed_scripts/distributed_configs/distributed_client_1.yaml data.file_path 'PATH/TO/DATA' distribute.server_host x.x.x.x distribute.server_port xxxx distribute.client_host x.x.x.x distribute.client_port xxxx python federatedscope/main.py --cfg scripts/distributed_scripts/distributed_configs/distributed_client_2.yaml data.file_path 'PATH/TO/DATA' distribute.server_host x.x.x.x distribute.server_port xxxx distribute.client_host x.x.x.x distribute.client_port xxxx python federatedscope/main.py --cfg scripts/distributed_scripts/distributed_configs/distributed_client_3.yaml data.file_path 'PATH/TO/DATA' distribute.server_host x.x.x.x distribute.server_port xxxx distribute.client_host x.x.x.x distribute.client_port xxxx ``` ```bash # Generate the toy data python scripts/distributed_scripts/gen_data.py # Firstly start the server that is waiting for clients to join in python federatedscope/main.py --cfg scripts/distributed_scripts/distributed_configs/distributed_server.yaml data.file_path toy_data/server_data distribute.server_host 127.0.0.1 distribute.server_port 50051 # Start the client #1 (with another process) python federatedscope/main.py --cfg scripts/distributed_scripts/distributed_configs/distributed_client_1.yaml data.file_path toy_data/client_1_data distribute.server_host 127.0.0.1 distribute.server_port 50051 distribute.client_host 127.0.0.1 distribute.client_port 50052 # Start the client #2 (with another process) python federatedscope/main.py --cfg scripts/distributed_scripts/distributed_configs/distributed_client_2.yaml data.file_path toy_data/client_2_data distribute.server_host 127.0.0.1 distribute.server_port 50051 distribute.client_host 127.0.0.1 distribute.client_port 50053 # Start the client #3 (with another process) python federatedscope/main.py --cfg scripts/distributed_scripts/distributed_configs/distributed_client_3.yaml data.file_path toy_data/client_3_data distribute.server_host 127.0.0.1 distribute.server_port 50051 distribute.client_host 127.0.0.1 distribute.client_port 50054 ``` -------------------------------- ### Configure Federated Learning Task Source: https://github.com/alibaba/federatedscope/blob/master/materials/notebook/01_quick_start.ipynb Configures the parameters for a standard federated learning task in standalone mode. This includes settings for GPU usage, evaluation frequency, metrics, federated learning mode, client sampling, training parameters, and loss function. ```python cfg.use_gpu = False cfg.eval.freq = 10 cfg.eval.metrics = ['acc', 'loss_regular'] cfg.federate.mode = 'standalone' cfg.federate.local_update_steps = 5 cfg.federate.total_round_num = 20 cfg.federate.sample_client_num = 5 cfg.federate.client_num = 10 cfg.train.optimizer.lr = 0.001 cfg.train.optimizer.weight_decay = 0.0 cfg.grad.grad_clip = 5.0 cfg.criterion.type = 'CrossEntropyLoss' cfg.trainer.type = 'cvtrainer' cfg.seed = 123 ``` -------------------------------- ### Install FederatedScope for Development Source: https://github.com/alibaba/federatedscope/blob/master/README.md Installs FederatedScope in editable mode with development dependencies and sets up pre-commit hooks. This is intended for users contributing to the project. ```bash pip install -e .[dev] pre-commit install ``` -------------------------------- ### Reproduce Benchmark Results with FederatedScope (Bash) Source: https://github.com/alibaba/federatedscope/blob/master/benchmark/Backdoor-bench/README.md This script demonstrates the steps to clone the FederatedScope repository, set up the environment, switch to the 'backdoor-bench' branch, and run a sample backdoor attack configuration. It requires installing specific Python packages and assumes the user has followed the general FederatedScope setup. ```bash # Step-1. clone the repository git clone https://github.com/alibaba/FederatedScope.git # Step-2. follow https://github.com/alibaba/FederatedScope/blob/master/README.md to build the running environment # Step-3. install packages required by the benchmark pip install opencv-python matplotlib pympler scikit-learn # Step-3. switch to the branch `backdoor-bench` for the benchmark git fetch git switch backdoor-bench # Step-4. run the baseline (taking attacking FedAvg with Edge type trigger as an example) cd FederatedScope python federatedscope/main.py --cfg scripts/backdoor_scripts/attack_config/backdoor_fedavg_resnet18_on_cifar10_small.yaml ``` -------------------------------- ### Run FederatedScope Organizer Client Source: https://github.com/alibaba/federatedscope/blob/master/federatedscope/organizer/README.md Launches the FederatedScope organizer client. Before running, the `server_ip` in `federatedscope/organizer/cfg_client.py` must be configured to point to the organizer server. The 'help' command is available for client interaction. ```python # For Client # Modify `server_ip` in federatedscope/organizer/cfg_client.py python federatedscope/organizer/client.py help ``` -------------------------------- ### Run FederatedScope-GNN Example (Bash) Source: https://github.com/alibaba/federatedscope/blob/master/federatedscope/gfl/README.md Executes a two-layer GCN model on the FedCora dataset using the FedAvg algorithm with a predefined configuration file. This is a quick start example for familiarizing users with FS-G. ```bash python federatedscope/main.py --cfg federatedscope/gfl/baseline/example.yaml ``` -------------------------------- ### Instantiate and Setup ClientData in Python Source: https://github.com/alibaba/federatedscope/blob/master/federatedscope/core/data/README.md Demonstrates how to instantiate a ClientData object for each client and how to set up its DataLoader with custom configurations like batch size. ClientData is a dictionary-like object holding DataLoaders for training, validation, and testing. ```python from federatedscope.core.data.base_data import ClientData from torch.utils.data import DataLoader # Assume train_data and test_data are prepared datasets and cfg is a configuration object # Instantiate client_data for each Client client_data = ClientData(DataLoader, cfg, train=train_data, val=None, test=test_data) # other_cfg with different batch size other_cfg = cfg.clone() # Example: create a modified config other_cfg.data.batch_size = 64 client_data.setup(other_cfg) print(client_data) >> {'train': DataLoader(train_data), 'test': DataLoader(test_data)} ``` -------------------------------- ### Install FedHPOBench Source: https://github.com/alibaba/federatedscope/blob/master/benchmark/FedHPOBench/README.md Installs the FedHPOBench library directly from its git repository. This involves cloning the repository and setting the PYTHONPATH environment variable to include the FedHPOBench directory. ```bash git clone https://github.com/alibaba/FederatedScope.git cd FederatedScope/benchmark/FedHPOBench export PYTHONPATH=~/FedHPOBench:$PYTHONPATH ``` -------------------------------- ### Run Cross-Backend FL: TF Server, PyTorch Clients Source: https://github.com/alibaba/federatedscope/blob/master/federatedscope/cross_backends/README.md This configuration sets up a federated learning scenario where the server runs on Tensorflow and clients operate on PyTorch. It requires generating toy data first, then starting the Tensorflow server, followed by launching PyTorch clients with specified configurations. ```shell # Generate toy data python ../../scripts/distributed_scripts/gen_data.py # Server python ../main.py --cfg distributed_tf_server.yaml # Clients python ../main.py --cfg ../../scripts/distributed_scripts/distributed_configs/distributed_client_1.yaml python ../main.py --cfg ../../scripts/distributed_scripts/distributed_configs/distributed_client_2.yaml python ../main.py --cfg ../../scripts/distributed_scripts/distributed_configs/distributed_client_3.yaml ``` -------------------------------- ### Install NLP Dependencies for FederatedScope Source: https://github.com/alibaba/federatedscope/blob/master/federatedscope/nlp/hetero_tasks/README.md Installs necessary Python packages and downloads external tools for NLP tasks within the FederatedScope framework. This includes NLTK, Transformers, ROUGE, and METEOR. Note that ROUGE installation involves cloning a repository, running setup, and configuring paths, while METEOR requires downloading and extracting package files. ```bash pip install nltk pip install transformers==4.21.0 # Install ROUGE git clone https://github.com/bheinzerling/pyrouge cd pyrouge pip install -e . git clone https://github.com/andersjo/pyrouge.git rouge pyrouge_set_rouge_path $(realpath rouge/tools/ROUGE-1.5.5/) sudo apt-get install libxml-parser-perl cd rouge/tools/ROUGE-1.5.5/data rm WordNet-2.0.exc.db ./WordNet-2.0-Exceptions/buildExeptionDB.pl ./WordNet-2.0-Exceptions ./smart_common_words.txt ./WordNet-2.0.exc.db python -m pyrouge.test # Download METEOR packages wget -c http://www.cs.cmu.edu/~alavie/METEOR/download/meteor-1.5.tar.gz tar -zxvf meteor-1.5.tar.gz mkdir ABSOLUTE/PATH/TO/federatedscope/nlp/metric/meteor/data/ mv meteor-1.5/data/paraphrase-en.gz ABSOLUTE/PATH/TO/federatedscope/nlp/metric/meteor/data/ mv meteor-1.5/meteor-1.5.jar ABSOLUTE/PATH/TO/federatedscope/nlp/metric/meteor/ ``` -------------------------------- ### Example: StandaloneMultiGPURunner vs. StandaloneRunner (YAML) Source: https://github.com/alibaba/federatedscope/blob/master/federatedscope/core/parallel/README.md This YAML configuration illustrates the setup for comparing the performance of `StandaloneMultiGPURunner` (using 8 GPUs) against `StandaloneRunner` in FederatedScope. It includes common training parameters such as data loading, model definition, and training settings, highlighting the `process_num` difference. ```yaml use_gpu: True device: 0 early_stop: patience: 5 seed: 12345 federate: mode: standalone client_num: 100 total_round_num: 10 sample_client_rate: 0.4 share_local_model: False # use StandaloneMultiGPURunner with 8 GPUs process_num: 8 # use StandaloneRunner # process_num: 1 data: root: data/ type: femnist splits: [0.6,0.2,0.2] batch_size: 10 subsample: 0.05 num_workers: 0 transform: [['ToTensor'], ['Normalize', {'mean': [0.1307], 'std': [0.3081]}]] model: type: convnet2 hidden: 2048 out_channels: 62 train: local_update_steps: 1 batch_or_epoch: epoch optimizer: lr: 0.01 weight_decay: 0.0 grad: grad_clip: 5.0 criterion: type: CrossEntropyLoss trainer: type: cvtrainer eval: freq: 10 metrics: ['acc', 'correct'] ``` -------------------------------- ### Run FederatedScope Organizer Server Worker Source: https://github.com/alibaba/federatedscope/blob/master/federatedscope/organizer/README.md Starts the Celery worker for the FederatedScope organizer server. This command is essential for processing tasks. The `--loglevel=info` flag provides verbose logging. Instructions for starting multiple workers are also included. ```bash # For Server cd federatedscope/organizer celery -A server worker --loglevel=info ## For multi-worker # celery multi start w1 -A server -l info # celery multi start w2 -A server -l info # ... ``` -------------------------------- ### Setup FederatedScope for CIKM 2022 Competition Source: https://github.com/alibaba/federatedscope/blob/master/materials/notebook/(EN)CIKMCUP2022.ipynb This Python snippet demonstrates how to copy the FederatedScope repository and switch to the stable branch required for the CIKM 2022 competition. It ensures the correct version of the library is used for the contest. ```python # Copy FederatedScope and switch to the stable branch cikm22competitionfor competition !cp -r fs_latest FederatedScope # `cd FederatedScope` if you are NOT in the playground. import os os.chdir('FederatedScope') !git checkout cikm22competition ``` -------------------------------- ### Install and Configure Pre-commit Hooks for FederatedScope Source: https://github.com/alibaba/federatedscope/blob/master/README.md Installs the pre-commit package and configures it to run on all files within the FederatedScope project. This helps maintain code quality by automatically checking for issues before commits. ```bash pip install pre-commit pre-commit install pre-commit run --all-files ``` -------------------------------- ### Install FederatedScope in Playground Source: https://github.com/alibaba/federatedscope/blob/master/materials/notebook/Welcome.ipynb Python code snippet for installing the FederatedScope library within the playground environment. It involves copying the source code and then installing it using pip. This ensures the correct version is available for use. ```python # Installation is REQUIRED in the playground. # You should execute the following code to install the stable version of FederatedScope. # `git clone https://github.com/alibaba/FederatedScope.git` if you are NOT in the playground. !cp -r fs_latest FederatedScope # `cd FederatedScope` if you are NOT in the playground. import os os.chdir('FederatedScope') !pip install -e . --user ``` -------------------------------- ### Install FederatedScope via Docker (PyTorch 1.10) Source: https://github.com/alibaba/federatedscope/blob/master/README.md Builds a Docker image for FederatedScope with PyTorch 1.10 and CUDA 11, then runs it in a container. This method isolates the environment and ensures specific dependencies are met. ```bash docker build -f environment/docker_files/federatedscope-torch1.10.Dockerfile -t alibaba/federatedscope:base-env-torch1.10 . docker run --gpus device=all --rm -it --name "fedscope" -w $(pwd) alibaba/federatedscope:base-env-torch1.10 /bin/bash ``` -------------------------------- ### Install FederatedScope for Apple M1 Chips (Conda) Source: https://github.com/alibaba/federatedscope/blob/master/README.md Installs PyTorch, torchvision, and torchaudio using Conda for Apple M1 chips and then downgrades torchvision to version 0.11.3 to avoid potential segmentation faults. Finally, installs FederatedScope from source. ```bash conda install pytorch torchvision torchaudio -c pytorch python -m pip install torchvision==0.11.3 pip install -e . ``` -------------------------------- ### Print Hello FederatedScope Source: https://github.com/alibaba/federatedscope/blob/master/materials/notebook/Welcome.ipynb A simple Python example demonstrating how to print a string to the console within the FederatedScope Playground. This is a basic test to confirm code execution capabilities. ```python print("Hello FederatedScope") ``` -------------------------------- ### Download and Prepare Contest Data Source: https://github.com/alibaba/federatedscope/blob/master/materials/notebook/(EN)CIKMCUP2022.ipynb This Python snippet shows how to create a 'data' directory and copy the pre-downloaded contest data into it. It prepares the data for use with FederatedScope by organizing it under 'FederatedScope/data/CIKM22Competition'. ```python # We have downloaded the data in advance in `data`, unzip the contest data as follows: !mkdir -p data !cp -r ../data/CIKM22Competition ./data/ ``` -------------------------------- ### Basic FedHPOBench Usage Source: https://github.com/alibaba/federatedscope/blob/master/benchmark/FedHPOBench/README.md Demonstrates the basic usage of FedHPO-Bench in Python. It shows how to instantiate a TabularBenchmark, retrieve the configuration and fidelity spaces, sample a configuration, and run the benchmark to get results. ```python from fedhpobench.config import fhb_cfg from fedhpobench.benchmarks import TabularBenchmark benchmark = TabularBenchmark('cnn', 'femnist', 'avg') # get hyperparameters space config_space = benchmark.get_configuration_space(CS=True) # get fidelity space fidelity_space = benchmark.get_fidelity_space(CS=True) # get results res = benchmark(config_space.sample_configuration(), fidelity_space.sample_configuration(), fhb_cfg=fhb_cfg, seed=12345) print(res) ``` -------------------------------- ### Run Distributed Federated Learning (Multi-Process) Source: https://context7.com/alibaba/federatedscope/llms.txt Sets up and runs a multi-process distributed federated learning experiment. This involves generating synthetic data, starting the server process with its configuration, and then launching multiple client processes, each with their own configuration and assigned data path. ```bash # Step 1: Generate synthetic data for distributed mode python scripts/distributed_scripts/gen_data.py # Step 2: Start the server (in one terminal) python federatedscope/main.py \ --cfg scripts/distributed_scripts/distributed_configs/distributed_server.yaml \ data.file_path toy_data/server_data \ distribute.server_host 127.0.0.1 \ distribute.server_port 50051 # Step 3: Start client #1 (in another terminal) python federatedscope/main.py \ --cfg scripts/distributed_scripts/distributed_configs/distributed_client_1.yaml \ data.file_path toy_data/client_1_data \ distribute.server_host 127.0.0.1 \ distribute.server_port 50051 \ distribute.client_host 127.0.0.1 \ distribute.client_port 50052 # Step 4: Start client #2 (in another terminal) python federatedscope/main.py \ --cfg scripts/distributed_scripts/distributed_configs/distributed_client_2.yaml \ data.file_path toy_data/client_2_data \ distribute.server_host 127.0.0.1 \ distribute.server_port 50051 \ distribute.client_host 127.0.0.1 \ distribute.client_port 50053 # Expected output on server: # INFO: Server: Listen to 127.0.0.1:50051... # INFO: Server has been set up ... # INFO: Client (address 127.0.0.1:50052) is assigned with #1. # INFO: ----------- Starting a new training round (Round #1) ------------- # Expected output on clients: # INFO: Client: Listen to 127.0.0.1:50052... # INFO: Client has been set up ... # {'Role': 'Client #1', 'Round': 0, 'Results_raw': {'train_avg_loss': 4.546, 'train_total': 64}} ``` -------------------------------- ### Start Wandb Sweep (Bash) Source: https://github.com/alibaba/federatedscope/blob/master/benchmark/pFL-Bench/README.md Initiates a wandb sweep using a specified YAML configuration file. This command is typically run on the sweep machine to define the hyperparameter search space and configuration. It outputs the sweep ID for monitoring. ```bash wandb sweep my_hpo.yaml ``` -------------------------------- ### Download and Prepare Data Files Source: https://github.com/alibaba/federatedscope/blob/master/benchmark/FedHPOBench/README.md Provides instructions for downloading and organizing data files required by FedHPO-Bench. It explains the naming convention for data URLs on AliyunOSS and specifies the target directories for tabular and surrogate data. ```bash # Example URL pattern: # https://federatedscope.oss-cn-beijing.aliyuncs.com/fedhpob_{BENCHMARK}_{MODE}.zip # Place downloaded and unzipped data under: # ~/data/tabular_data/ or ~/data/surrogate_model/ ``` -------------------------------- ### Run pFL-Bench Experiments with Searched Configurations Source: https://github.com/alibaba/federatedscope/blob/master/benchmark/pFL-Bench/README.md This snippet shows how to execute pFL experiments using the `federatedscope/main.py` script with pre-defined YAML configuration files. These configurations represent hyper-parameter searches for specific methods and datasets. The output metrics are logged and sent to a wandb monitor. ```bash # Clone the FederatedScope repository git clone https://github.com/alibaba/FederatedScope.git cd FederatedScope # Switch to the branch for paper experiments (optional) git switch Feature/pfl_bench # Run an experiment using a specific YAML configuration file python federatedscope/main.py --cfg benchmark/pfl_bench/yaml_best_runs/FedBN_FEMNIST-s02.yaml # Customize configuration via command line arguments python federatedscope/main.py --cfg benchmark/pfl_bench/yaml_best_runs/FedBN_FEMNIST-s02.yaml federate.local_update_steps 1 ``` -------------------------------- ### Install FederatedScope with Conda Source: https://context7.com/alibaba/federatedscope/llms.txt Installs FederatedScope using a conda environment. This involves cloning the repository, creating and activating a conda environment, installing PyTorch with specified CUDA version, and finally installing FederatedScope in editable mode. Optional dependencies for specific applications can also be installed. ```bash # Clone the repository git clone https://github.com/alibaba/FederatedScope.git cd FederatedScope # Create and activate conda environment conda create -n fs python=3.9 conda activate fs # Install PyTorch (adjust CUDA version as needed) conda install -y pytorch=1.10.1 torchvision=0.11.2 torchaudio=0.10.1 cudatoolkit=11.3 -c pytorch -c conda-forge # Install FederatedScope pip install -e . # Optional: Install additional dependencies for graph, NLP, and other applications bash environment/extra_dependencies_torch1.10-application.sh ``` -------------------------------- ### Build Docker Image for FederatedScope Source: https://github.com/alibaba/federatedscope/blob/master/benchmark/pFL-Bench/README.md Instructions to build a Docker image for FederatedScope using a provided Dockerfile. This image is used for conducting experiments within a consistent environment. Alternatively, a pre-built image can be downloaded and loaded. ```bash # Example of building the docker image docker build -t alibaba/federatedscope:app-env-torch1.8 -f environment/docker_files/federatedscope-torch1.8-application.Dockerfile . # Example of loading a pre-built docker image docker load < federatedscope_cuda10_torch18_app.tar docker tag 188b4 alibaba/federatedscope:app-env-torch1.8 ``` -------------------------------- ### Initialize FederatedScope Runner (Python) Source: https://github.com/alibaba/federatedscope/blob/master/federatedscope/core/data/README.md This code snippet demonstrates the core initialization process for the FederatedScope framework. It loads data based on configuration and instantiates the FedRunner with specified server and client classes. Dependencies include the 'federatedscope' library and its configuration system. ```python data, cfg = get_data(cfg) runner = FedRunner(data=data, server_class=get_server_cls(cfg), client_class=get_client_cls(cfg), config=cfg.clone()) ``` -------------------------------- ### Federated Learning Workflow in FederatedScope Source: https://context7.com/alibaba/federatedscope/llms.txt This code snippet demonstrates the typical workflow for setting up and running a federated learning experiment using the FederatedScope library. It covers configuration, data loading, runner initialization, and execution of the federated learning process. ```python from federatedscope.core import setup_seed from federatedscope.core.data import get_data from federatedscope.core.trainers import get_runner from federatedscope.core.trainers import get_server_cls from federatedscope.core.trainers import get_client_cls # Setup setup_seed(cfg.seed) cfg.freeze() # Load data data, modified_cfg = get_data(config=cfg) # Create runner with server and client classes runner = get_runner( data=data, server_class=get_server_cls(cfg), client_class=get_client_cls(cfg), config=cfg ) # Run federated learning results = runner.run() print(f"Final test accuracy: {results['test_acc']:.4f}") print(f"Best round: {results['best_round']}") ``` -------------------------------- ### Run Federated Learning Simulation with FedRunner Source: https://github.com/alibaba/federatedscope/blob/master/materials/notebook/02_start_your_own_case.ipynb This snippet initializes and runs a federated learning simulation using `FedRunner`. It sets up the data, logger, random seed, and instantiates the server and client classes based on the provided configuration. The `run()` method starts the federated training process. ```python from federatedscope.core.auxiliaries.data_builder import get_data from federatedscope.core.auxiliaries.utils import setup_seed from federatedscope.core.auxiliaries.logging import update_logger from federatedscope.core.fed_runner import FedRunner from federatedscope.core.auxiliaries.worker_builder import get_server_cls, get_client_cls setup_seed(cfg.seed) update_logger(cfg) data, modified_cfg = get_data(cfg) cfg.merge_from_other_cfg(modified_cfg) Fed_runner = FedRunner(data=data, server_class=get_server_cls(cfg), client_class=get_client_cls(cfg), config=cfg.clone()) Fed_runner.run() ``` -------------------------------- ### Install FederatedScope via Conda (PyTorch 1.10, CUDA 11.3) Source: https://github.com/alibaba/federatedscope/blob/master/README.md Sets up a Conda virtual environment named 'fs', installs PyTorch 1.10.1 with CUDA 11.3 support, and then installs FederatedScope in editable mode. This is a recommended approach for managing Python dependencies. ```bash conda create -n fs python=3.9 conda activate fs conda install -y pytorch=1.10.1 torchvision=0.11.2 torchaudio=0.10.1 torchtext=0.11.1 cudatoolkit=11.3 -c pytorch -c conda-forge pip install -e . ``` -------------------------------- ### Run Federated Learning with Specific Aggregators Source: https://context7.com/alibaba/federatedscope/llms.txt These bash commands demonstrate how to run federated learning experiments with specific aggregation methods like FedProx and Krum. They use the main entry point script and override configuration parameters directly on the command line. ```bash # Run with FedProx python federatedscope/main.py \ --cfg scripts/example_configs/femnist.yaml \ federate.method fedprox \ federate.fedprox.mu 0.1 # Run with robust Krum aggregator python federatedscope/main.py \ --cfg scripts/example_configs/femnist.yaml \ federate.method krum ``` -------------------------------- ### Check FederatedScope Version Source: https://github.com/alibaba/federatedscope/blob/master/materials/notebook/Welcome.ipynb Python code to import the FederatedScope library and display its installed version. This is useful for verifying the installation and understanding compatibility with documentation. ```python import federatedscope federatedscope.__version__ ``` -------------------------------- ### Install Missing Packages (Conda) Source: https://github.com/alibaba/federatedscope/blob/master/benchmark/pFL-Bench/README.md Installs essential Python packages that might be missing in the Docker image, such as fvcore, pympler, and iopath. These are often required for specific functionalities within FederatedScope or its dependencies. ```bash conda install fvcore pympler iopath ``` -------------------------------- ### Load and Inspect Contest Data with PyTorch Source: https://github.com/alibaba/federatedscope/blob/master/materials/notebook/(EN)CIKMCUP2022.ipynb Demonstrates how to load individual client data splits (train, test, validate) using PyTorch's `torch.load`. It also shows how to inspect the first sample, its label, and its data index, which is crucial for understanding data structure and for submission formatting. ```python import torch # The train split of client 1 train_data_client1 = torch.load('./data/CIKM22Competition/1/train.pt') # Check the first sample print(train_data_client1[0]) # Check the label of the first sample print(train_data_client1[0].y) # Check the index of the first sample as ${sample_id} print(train_data_client1[0].data_index) ``` -------------------------------- ### Install FederatedScope Application Dependencies (Torch 1.10) Source: https://github.com/alibaba/federatedscope/blob/master/README.md Installs additional dependencies required for application-level tasks such as graph FL, NLP, and speech, using a provided shell script. This is an optional step for advanced use cases. ```bash bash environment/extra_dependencies_torch1.10-application.sh ``` -------------------------------- ### Install FederatedScope-GNN Dependencies (Bash) Source: https://github.com/alibaba/federatedscope/blob/master/federatedscope/gfl/README.md Installs necessary Python packages for FederatedScope-GNN, including PyTorch Geometric, RDKit, and NLTK, using Conda. These are extra dependencies for the application version of FGL. ```bash conda install -y pyg==2.0.4 -c pyg conda install -y rdkit=2021.09.4=py39hccf6a74_0 -c conda-forge conda install -y nltk ``` -------------------------------- ### Reproduce Paper Results (Figure 11) Source: https://github.com/alibaba/federatedscope/blob/master/benchmark/FedHPOBench/README.md Provides bash commands to run experiments for reproducing results, specifically for Figure 11, by executing the `run_mode.sh` script with different datasets and settings. It assumes results are stored in `~/exp_results`. ```bash cd scripts/exp bash run_mode.sh cora raw gcn 0 avg bash run_mode.sh citeseer raw gcn 1 avg bash run_mode.sh pubmed raw gcn 1 avg ``` -------------------------------- ### Install Optional HPO Benchmarking Tools Source: https://github.com/alibaba/federatedscope/blob/master/benchmark/FedHPOBench/README.md Installs optional dependencies for reproducing paper results, including hpbandster, smac3, optuna via conda, and DEHB via git. It also sets up Dask for distributed computing and configures the PYTHONPATH for DEHB. ```bash # hpbandster, smac3, optuna via conda conda install hpbandster smac3 optuna -c conda-forge # dehb via git conda install dask distributed -c conda-forge git clone https://github.com/automl/DEHB.git cd DEHB git checkout b8dcba7b38bf6e7fc8ce3e84ea567b66132e0eb5 export PYTHONPATH=~/DEHB:$PYTHONPATH ``` -------------------------------- ### Run Distributed LR Training Script Source: https://github.com/alibaba/federatedscope/blob/master/scripts/README.md Executes a script to train a Linear Regression model in a distributed mode using FederatedScope. This setup involves a server and multiple clients, simulating a federated environment on a single device. For multi-device setups, IP addresses and ports need to be configured in YAML files. ```shell bash distributed_scripts/run_distributed_lr.sh ``` -------------------------------- ### Clone FederatedScope Repository Source: https://github.com/alibaba/federatedscope/blob/master/README.md Clones the FederatedScope project from GitHub and navigates into the project directory. This is the initial step for both Docker and Conda installations. ```bash git clone https://github.com/alibaba/FederatedScope.git cd FederatedScope ```