Skip to content

Commit

Permalink
refactor(framework) Update NumPy template (#4292)
Browse files Browse the repository at this point in the history
  • Loading branch information
jafermarq authored Oct 8, 2024
1 parent 5c88e49 commit 3e45bfd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/py/flwr/cli/new/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ def new(
MlFramework.HUGGINGFACE.value,
MlFramework.MLX.value,
MlFramework.TENSORFLOW.value,
MlFramework.NUMPY.value,
]
if framework_str in frameworks_with_tasks:
files[f"{import_name}/task.py"] = {
Expand Down
9 changes: 4 additions & 5 deletions src/py/flwr/cli/new/templates/app/code/client.numpy.py.tpl
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
"""$project_name: A Flower / $framework_str app."""

from flwr.client import NumPyClient, ClientApp
from flwr.client import ClientApp, NumPyClient
from flwr.common import Context
import numpy as np
from $import_name.task import get_dummy_model


class FlowerClient(NumPyClient):
def get_parameters(self, config):
return [np.ones((1, 1))]

def fit(self, parameters, config):
return ([np.ones((1, 1))], 1, {})
model = get_dummy_model()
return [model], 1, {}

def evaluate(self, parameters, config):
return float(0.0), 1, {"accuracy": float(1.0)}
Expand Down
9 changes: 7 additions & 2 deletions src/py/flwr/cli/new/templates/app/code/server.numpy.py.tpl
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
"""$project_name: A Flower / $framework_str app."""

from flwr.common import Context
from flwr.common import Context, ndarrays_to_parameters
from flwr.server import ServerApp, ServerAppComponents, ServerConfig
from flwr.server.strategy import FedAvg
from $import_name.task import get_dummy_model


def server_fn(context: Context):
# Read from config
num_rounds = context.run_config["num-server-rounds"]

# Initial model
model = get_dummy_model()
dummy_parameters = ndarrays_to_parameters([model])

# Define strategy
strategy = FedAvg()
strategy = FedAvg(initial_parameters=dummy_parameters)
config = ServerConfig(num_rounds=num_rounds)

return ServerAppComponents(strategy=strategy, config=config)
Expand Down
7 changes: 7 additions & 0 deletions src/py/flwr/cli/new/templates/app/code/task.numpy.py.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""$project_name: A Flower / $framework_str app."""

import numpy as np


def get_dummy_model():
return np.ones((1, 1))

0 comments on commit 3e45bfd

Please sign in to comment.