Fedland: Loss Landscapes in Federated Learning
Summary:
Loss landscapes in the federated learning [1] setting are relatively more complex and often fragmented as compared to the centralized setting, owing to variations in local data distributions [2]. The project will study and contrast local and global loss landscapes to derive insights about a local client’s contribution to the overall loss landscape. Understanding these dynamics can potentially enhance our ability to optimize federated learning systems.
Task:
-
Training a neural network model in two settings:
- In a centralized environment
- In a federated environment, including:
- Balanced/unbalanced
- IID/non-IID
-
Conduct experiments with the federated setting focusing on analyzing the loss landscapes of the models trained in these settings, including both the global model and individual client-side local models.
Project Layout:
Since I am using FEDn, the project follows their recommended project structure.
I have added pyproject.toml for managing dependencies. I have written it to use Poetry. You probably want to use an virtual environment or tool to manage your virtualenv locally. This will give you code completion and allow you to run main.py and any other scripts outside docker (if you want). To install the dependencies using poetry, simply run poetry install. I set it to use python 3.11
project
├ client
│ ├ fedn.yaml
│ ├ python_env.yaml
│ ├ model.py
│ ├ data.py
│ ├ train.py
│ └ validate.py
├ data
│ ├ /db/mongo.files
│ └ mnist.npz
├ fedland (module)
│ └ __init__.py
├ README.md
├ pyproject.toml
├ scripts / notebooks
├ compose.yaml
└ Dockerfile
What is /fedland?
The idea behind writing fedland into its own package is it would abstract repetitive calculatations and functionality into a tiny library available on PyPi in order to pull it into clients environments.
I also thought it would be nice to define and queue experiments in a declarative fashion so you can also log everything to the centralized store (mongo since it's already there in FEDn's stack). This way anyone who wants to run tests can define them all log their experiments to the same place and make sure calculations and data processing are consistent across experiments.
If you just want to use fedland metrics and such (its pretty bare rn) instal with pip install fedland
Running the project:
In docker (psuedo distributed mode)
- Start everything
cd fedland # This repo's root, not the fedland package
docker compose up --build -d
- Set the combiner host so local clients can resolve to docker
macOS/Linux
# Edit hosts file with sudo privileges
sudo nano /etc/hosts
# Add this line to the file:
127.0.0.1 combiner
# Save and exit:
# For nano: Ctrl + X, then Y, then Enter
# For vi: Esc, then :wq, then Enter
# Test the configuration
ping combiner
Windows
# Open PowerShell as Administrator and edit hosts file
notepad C:\Windows\System32\drivers\etc\hosts
# Add this line to the file:
127.0.0.1 combiner
# Save the file (may need to confirm overwrite)
# Test the configuration
ping combiner
- Define the
Experimentsyou want to run inmain.py
# main.py
ROUNDS = 10 # How many communication rounds to do
CLIENT_LEVEL = 3
EXPERIMENTS = [
Experiment(
id=str(uuid.uuid4()),
description="EXAMPLE: CIFAR-10, uneven classes",
dataset_name=DatasetIdentifier.CIFAR.value,
model="CifarFedNet",
timestamp=datetime.now().isoformat(),
target_balance_ratios=[
[0.01] * 10,
[
float(x)
for x in (
np.exp(-0.07 * np.arange(10)) / sum(np.exp(-0.07 * np.arange(10)))
)
],
],
client_stats=[],
aggregator="fedavg" # OR "fedopt"
),
]
- Run main.py
python main.py
# Clients get started and are run based on your env
- Wait and watch
- You can verify the process is running by seeing the clients' logs which are dumped to
debug.log - You can also watch the output in
results/results/experiments.jsonis the key for experiment ids which each experiment has directory linked to its ID. Within each experiment directory there are subdirs marking the client id.- within
results/<experiemnt id>/<client id>/there are the training results as well as the clients info so we can verify the results are what we expect them to be.
- Your cpu will likely go brr on validation rounds (good way to tell whats happening while you work on other things and wait.)
Locally (plus with FEDn Studio)
I have not tested this in a while Requirements: poetry
- Setup a venv
- Run
poetry install --with devin venv - Create and fill in the .env fields based on .env.example
- run:
cp .env.example .env
- run:
- Run whatever scripts you want in /scripts
- eg:
python scripts/centralized_base.py
- eg:
- Follow FEDn's getting started as if this home dir were a directory in the pytorch-mnist example
Helpful Commands:
Running clients manually (for debugging)
RESULTS_DIR=/your/abs/path/fedland/results TEST_ID=<TARGET TEST UUID> CLIENT_ID=0 fedn client start -n 0 --init settings-client-local.yaml
Running tests with:
poetry run pytest tests
OR in docker container with
docker compose run --build client poetry run pytest tests/
Running Mongo Shell (mongo db instance)
The default db is the same as the network id (fedn-network)
mongosh "mongodb://0.0.0.0:6534/?authSource=admin" --apiVersion 1 --username fedn_admin
The db can also be viewed using Mongo Express webapp here
Backup/Restore the database inside the docker container with: Backup File Here 10/31 Spooky
# backup
mongodump -d fedn-network -u fedn_admin -p password --port=6534 --out=/data/db/backup
# restore
mongorestore /data/backup/fedn-network --port=6534 -p password
- Note that from within the proj dir, we have the shared volume mounted to ./data
As a footnote: When I started writing this I thought "That'll be easy to automate the experiments. Then I can just define them and then let them run overnight and lazily scale the clients and rerun". It didn't turn out to be so easy so sorry for the complexity and code slop. It just turned into "Make it work" about 3/4 the way through.
Metadata
Release files for fedland 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| fedland-0.3.0.tar.gz | 17.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fedland-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 33.0 kB
Release files / fedland-0.3.0.tar.gz
| Download URL | fedland-0.3.0.tar.gz |
|---|---|
| Size | 17.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
12f2217e99446612dd5f47c06557f495330b91f919b9bb218b7c471c7c3bb00d
|
|
BLAKE2b-256 checksum How to use checksums |
3192b55beb754bae94246150490fbb0d55a5f2b36611fd36132d3d0eeaaf4cba
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.4 CPython/3.12.7 Linux/6.11.6-zen1-1-zen
|
Release files / fedland-0.3.0-py3-none-any.whl
| Download URL | fedland-0.3.0-py3-none-any.whl |
|---|---|
| Size | 15.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3173ef5e2d6b82d000ec4e78f580c466d82b0348ae3f92646f54ac51b158ea94
|
|
BLAKE2b-256 checksum How to use checksums |
942aaed0543c1fcae57ae356d31f44af62de408649114c87f19700526e997395
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.4 CPython/3.12.7 Linux/6.11.6-zen1-1-zen
|