# rishikrishnah/fl architecture

> Architecture diagram of the GitHub repository rishikrishnah/fl, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rishikrishnah/fl
- Repository: https://github.com/rishikrishnah/fl

## Overview

#### Purpose and Entrypoints
FAFT is a PyTorch/Flower federated-learning system for privacy-preserving binary deepfake detection. Its primary runtime entrypoint is `flwr run . --stream` from `quickstart-pytorch/`, configured by `quickstart-pytorch/pyproject.toml`. Pipeline and standalone evaluation entrypoints are `quickstart-pytorch/run_experiment.py`, `quickstart-pytorch/evaluate_saved_model.py`, and `quickstart-pytorch/evaluate_holdout.py`.

#### Federated Application Boundary
Flower simulation hosts five Ray-backed client actors and one server. Client/server registration and round control live in `quickstart-pytorch/pytorchexample/client_app.py` and `quickstart-pytorch/pytorchexample/server_app.py`. Clients retain local image partitions and transmit parameters/metrics only; raw data never crosses the client-server boundary.

#### Model Layer
`quickstart-pytorch/pytorchexample/model.py` defines FAFTNet and selectable baseline/hybrid architectures. FAFT combines ResNet18 spatial features, FFT/CNN frequency features, artifact-guided fusion, and a custom transformer whose attention receives artifact-derived bias. The model emits real/fake logits and maintains 768-dimensional real/fake prototype embeddings used as forensic memory.

#### Local Training and Data Access
`quickstart-pytorch/pytorchexample/task.py` selects the configured dataset root, loads each `client{id}` train/validation/test partition, and implements training, testing, checkpoint serialization, mixed precision, and metrics. Dataset preparation creates those partitions outside the repository: `quickstart-pytorch/split.py` handles the 140K faces layout, while `quickstart-pytorch/preprocess_ffpp.py` and `quickstart-pytorch/preprocess_openforensics.py` produce FF++ and OpenForensics layouts.

#### Round Data Flow and Aggregation
Each round, `FlowerClient.fit()` loads global model parameters, trains locally, updates class prototypes, and returns model weights plus—only for FAFT—the two prototype tensors. `FAFTStrategy` in `quickstart-pytorch/pytorchexample/server_app.py` example-count-weights model updates, EMA-aggregates real/fake prototypes into global memory, checkpoints the resulting model, and broadcasts weights plus global prototypes for the next round. Non-FAFT architectures use `SaveModelStrategy` for normal FedAvg and checkpointing. Optional malicious-client parameter noise is applied through `quickstart-pytorch/pytorchexample/utils.py`.

#### Outputs and Evaluation Boundary
Server rounds produce runtime checkpoints at `quickstart-pytorch/saved_models/global_model_round_<N>.pth`. `quickstart-pytorch/evaluate_saved_model.py` detects the checkpoint architecture and evaluates it over all local client test sets. `quickstart-pytorch/evaluate_holdout.py` evaluates cross-dataset FF++ holdouts; holdout construction is handled by `quickstart-pytorch/recreate_holdout_eval.py` and `quickstart-pytorch/fix_holdout_real.py`.

#### Runtime and Infrastructure
Core runtime dependencies are Python 3.11, PyTorch/TorchVision, Flower 1.13 simulation, and Ray. CUDA is used when available, with CPU fallback. `quickstart-pytorch/pytorchexample/gpu_manager.py` coordinates Ray actor GPU placement through NVML and a file-locked `/tmp/fl_gpu_lock.json`; `FORCE_GPU` can override placement. `quickstart-pytorch/run_experiment.py` gates execution on GPU availability, retries train/evaluation stages, and writes status artifacts under `quickstart-pytorch/experiment_logs/`.

Main entry points: [`quickstart-pytorch/pytorchexample/client_app.py`](https://github.com/rishikrishnah/fl/blob/main/quickstart-pytorch/pytorchexample/client_app.py) (Flower client), [`quickstart-pytorch/pytorchexample/server_app.py`](https://github.com/rishikrishnah/fl/blob/main/quickstart-pytorch/pytorchexample/server_app.py) (Server strategies), [`quickstart-pytorch/run_experiment.py`](https://github.com/rishikrishnah/fl/blob/main/quickstart-pytorch/run_experiment.py) (Experiment runner), [`quickstart-pytorch/pytorchexample/task.py`](https://github.com/rishikrishnah/fl/blob/main/quickstart-pytorch/pytorchexample/task.py) (Local training task)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Federated Runtime"]
  node_flwr{{"Flower simulation<br/>runtime"}}
  node_client_app["Flower client<br/>client application<br/>[client_app.py]"]
  node_server_app["Server strategies<br/>aggregation service<br/>[server_app.py]"]
  node_gpu_manager["GPU placement manager<br/>Ray GPU scheduler<br/>[gpu_manager.py]"]
end

subgraph group_model["Model and Local Learning"]
  node_model_def["FAFTNet and baselines<br/>PyTorch model<br/>[model.py]"]
  node_local_task["Local training task<br/>training and data access<br/>[task.py]"]
  node_prototype_memory["Real/fake prototypes<br/>forensic memory"]
  node_malicious_noise["Malicious-client noise<br/>optional perturbation<br/>[utils.py]"]
end

subgraph group_data["Data Preparation and Evaluation"]
  node_client_partitions[("Client image partitions<br/>local dataset")]
  node_dataset_split["140K faces splitter<br/>dataset preparation<br/>[split.py]"]
  node_dataset_preprocessors["FF++ and OpenForensics prep<br/>dataset preparation<br/>[preprocess_ffpp.py]"]
  node_checkpoints[("Global round checkpoints<br/>model artifacts")]
  node_saved_model_eval["Saved-model evaluation<br/>evaluation entrypoint"]
  node_holdout_eval["Cross-dataset holdout evaluation<br/>evaluation entrypoint"]
  node_holdout_builder["Holdout construction<br/>dataset repair"]
end

subgraph group_ops["Experiment Operations"]
  node_experiment_runner["Experiment runner<br/>pipeline entrypoint<br/>[run_experiment.py]"]
  node_experiment_logs["Experiment status logs<br/>runtime artifacts"]
  node_project_config["Flower project config<br/>runtime configuration<br/>[pyproject.toml]"]
end

node_project_config -->|"configures"| node_flwr
node_flwr -->|"hosts five actors"| node_client_app
node_flwr -->|"hosts server"| node_server_app
node_gpu_manager -.->|"places GPU actors"| node_flwr
node_client_app -->|"runs local fit/test"| node_local_task
node_local_task -->|"reads local images"| node_client_partitions
node_local_task -->|"trains and tests"| node_model_def
node_client_app -->|"parameters and metrics only"| node_server_app
node_client_app -->|"updates FAFT prototypes"| node_prototype_memory
node_prototype_memory -->|"FAFT tensors"| node_server_app
node_server_app -->|"EMA global memory"| node_prototype_memory
node_server_app -->|"broadcasts weights and prototypes"| node_client_app
node_malicious_noise -.->|"optional parameter perturbation"| node_client_app
node_server_app -->|"saves each round"| node_checkpoints
node_dataset_split -->|"creates partitions"| node_client_partitions
node_dataset_preprocessors -->|"prepares source layouts"| node_client_partitions
node_checkpoints -->|"loads"| node_saved_model_eval
node_client_partitions -->|"supplies client test sets"| node_saved_model_eval
node_holdout_builder -->|"constructs holdouts"| node_holdout_eval
node_checkpoints -->|"evaluates model"| node_holdout_eval
node_experiment_runner -->|"runs training stage"| node_flwr
node_experiment_runner -->|"runs evaluation stage"| node_saved_model_eval
node_experiment_runner -->|"writes status"| node_experiment_logs

click node_client_app "https://github.com/rishikrishnah/fl/blob/main/quickstart-pytorch/pytorchexample/client_app.py"
click node_server_app "https://github.com/rishikrishnah/fl/blob/main/quickstart-pytorch/pytorchexample/server_app.py"
click node_gpu_manager "https://github.com/rishikrishnah/fl/blob/main/quickstart-pytorch/pytorchexample/gpu_manager.py"
click node_model_def "https://github.com/rishikrishnah/fl/blob/main/quickstart-pytorch/pytorchexample/model.py"
click node_local_task "https://github.com/rishikrishnah/fl/blob/main/quickstart-pytorch/pytorchexample/task.py"
click node_malicious_noise "https://github.com/rishikrishnah/fl/blob/main/quickstart-pytorch/pytorchexample/utils.py"
click node_dataset_split "https://github.com/rishikrishnah/fl/blob/main/quickstart-pytorch/split.py"
click node_dataset_preprocessors "https://github.com/rishikrishnah/fl/blob/main/quickstart-pytorch/preprocess_ffpp.py"
click node_saved_model_eval "https://github.com/rishikrishnah/fl/blob/main/quickstart-pytorch/evaluate_saved_model.py"
click node_holdout_eval "https://github.com/rishikrishnah/fl/blob/main/quickstart-pytorch/evaluate_holdout.py"
click node_holdout_builder "https://github.com/rishikrishnah/fl/blob/main/quickstart-pytorch/recreate_holdout_eval.py"
click node_experiment_runner "https://github.com/rishikrishnah/fl/blob/main/quickstart-pytorch/run_experiment.py"
click node_project_config "https://github.com/rishikrishnah/fl/blob/main/quickstart-pytorch/pyproject.toml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_flwr,node_client_app,node_server_app,node_gpu_manager toneBlue
class node_model_def,node_local_task,node_prototype_memory,node_malicious_noise toneAmber
class node_client_partitions,node_dataset_split,node_dataset_preprocessors,node_checkpoints,node_saved_model_eval,node_holdout_eval,node_holdout_builder toneMint
class node_experiment_runner,node_experiment_logs,node_project_config toneRose
```

## Components

### Federated Runtime

Flower simulation with Ray-backed clients and server.

- **Flower simulation**: runtime · Runs one server and five Ray-backed client actors.
- [Flower client](https://github.com/rishikrishnah/fl/blob/main/quickstart-pytorch/pytorchexample/client_app.py): client application · Loads local partitions and returns parameters, metrics, and FAFT prototypes. · `quickstart-pytorch/pytorchexample/client_app.py`
- [Server strategies](https://github.com/rishikrishnah/fl/blob/main/quickstart-pytorch/pytorchexample/server_app.py): aggregation service · Controls rounds, aggregation, prototype memory, and checkpoints. · `quickstart-pytorch/pytorchexample/server_app.py`
- [GPU placement manager](https://github.com/rishikrishnah/fl/blob/main/quickstart-pytorch/pytorchexample/gpu_manager.py): Ray GPU scheduler · Coordinates actor placement using NVML and a locked host file. · `quickstart-pytorch/pytorchexample/gpu_manager.py`

### Model and Local Learning

- [FAFTNet and baselines](https://github.com/rishikrishnah/fl/blob/main/quickstart-pytorch/pytorchexample/model.py): PyTorch model · FAFT fuses spatial, frequency, artifact, and transformer features with prototype memory. · `quickstart-pytorch/pytorchexample/model.py`
- [Local training task](https://github.com/rishikrishnah/fl/blob/main/quickstart-pytorch/pytorchexample/task.py): training and data access · Loads client partitions and implements train, test, metrics, and checkpoint serialization. · `quickstart-pytorch/pytorchexample/task.py`
- **Real/fake prototypes**: forensic memory · Two 768-dimensional embeddings exchanged only by FAFT clients.
- [Malicious-client noise](https://github.com/rishikrishnah/fl/blob/main/quickstart-pytorch/pytorchexample/utils.py): optional perturbation · Optionally modifies client parameters before submission. · `quickstart-pytorch/pytorchexample/utils.py`

### Data Preparation and Evaluation

- **Client image partitions**: local dataset · Per-client train, validation, and test images remain local.
- [140K faces splitter](https://github.com/rishikrishnah/fl/blob/main/quickstart-pytorch/split.py): dataset preparation · Creates the client partition layout outside the repository. · `quickstart-pytorch/split.py`
- [FF++ and OpenForensics prep](https://github.com/rishikrishnah/fl/blob/main/quickstart-pytorch/preprocess_ffpp.py): dataset preparation · Produces dataset layouts for federated training. · `quickstart-pytorch/preprocess_ffpp.py`
- **Global round checkpoints**: model artifacts · Saved as global_model_round_<N>.pth.
- [Saved-model evaluation](https://github.com/rishikrishnah/fl/blob/main/quickstart-pytorch/evaluate_saved_model.py): evaluation entrypoint · Detects checkpoint architecture and tests all client test sets. · `quickstart-pytorch/evaluate_saved_model.py`
- [Cross-dataset holdout evaluation](https://github.com/rishikrishnah/fl/blob/main/quickstart-pytorch/evaluate_holdout.py): evaluation entrypoint · Evaluates FF++ holdouts. · `quickstart-pytorch/evaluate_holdout.py`
- [Holdout construction](https://github.com/rishikrishnah/fl/blob/main/quickstart-pytorch/recreate_holdout_eval.py): dataset repair · Recreates and fixes FF++ holdout data. · `quickstart-pytorch/recreate_holdout_eval.py`

### Experiment Operations

- [Experiment runner](https://github.com/rishikrishnah/fl/blob/main/quickstart-pytorch/run_experiment.py): pipeline entrypoint · Gates on GPU availability, retries stages, and records status. · `quickstart-pytorch/run_experiment.py`
- **Experiment status logs**: runtime artifacts
- [Flower project config](https://github.com/rishikrishnah/fl/blob/main/quickstart-pytorch/pyproject.toml): runtime configuration · Configures the flwr run entrypoint. · `quickstart-pytorch/pyproject.toml`

## Connections

- Flower project config → Flower simulation: configures
- Flower simulation → Flower client: hosts five actors
- Flower simulation → Server strategies: hosts server
- GPU placement manager → Flower simulation: places GPU actors
- Flower client → Local training task: runs local fit/test
- Local training task → Client image partitions: reads local images
- Local training task → FAFTNet and baselines: trains and tests
- Flower client → Server strategies: parameters and metrics only · Raw images do not cross this boundary.
- Flower client → Real/fake prototypes: updates FAFT prototypes
- Real/fake prototypes → Server strategies: FAFT tensors
- Server strategies → Real/fake prototypes: EMA global memory
- Server strategies → Flower client: broadcasts weights and prototypes
- Malicious-client noise → Flower client: optional parameter perturbation
- Server strategies → Global round checkpoints: saves each round
- 140K faces splitter → Client image partitions: creates partitions
- FF++ and OpenForensics prep → Client image partitions: prepares source layouts
- Global round checkpoints → Saved-model evaluation: loads
- Client image partitions → Saved-model evaluation: supplies client test sets
- Holdout construction → Cross-dataset holdout evaluation: constructs holdouts
- Global round checkpoints → Cross-dataset holdout evaluation: evaluates model
- Experiment runner → Flower simulation: runs training stage
- Experiment runner → Saved-model evaluation: runs evaluation stage
- Experiment runner → Experiment status logs: writes status
