Flower Next 업그레이드#

Flower에서 Flower Next로의 업데이트를 위한 이동 가이드에 오신 것을 환영합니다! 이 가이드는 숙련된 사용자든 이제 막 시작한 사용자든 상관없이 기존 설정을 원활하게 전환하여 버전 1.8부터 Flower Next의 최신 기능 및 개선 사항을 활용할 수 있도록 도와드립니다.

참고

이 가이드에서는 Flower Next의 *호환성 레이어*를 사용하여 최소한의 코드 변경으로 1.8 이전의 Flower 코드를 재사용하는 방법을 보여줍니다. 다른 가이드에서는 순수한 Flower Next API로 Flower Next를 end-to-end로 실행하는 방법을 보여드리겠습니다.

자세히 알아봅시다!

업데이트 설치#

pip 사용#

기존에 설치된 Flower to Flower Next를 ``pip``으로 업데이트하는 방법은 다음과 같습니다:

$ python -m pip install -U flwr

또는 시뮬레이션이 포함된 Flower Next가 필요한 경우:

$ python -m pip install -U "flwr[simulation]"

``requirements.txt``에서 다음 버전 제약 조건을 설정했는지 확인하세요

# Without simulation support
flwr>=1.8,<2.0

# With simulation support
flwr[simulation]>=1.8, <2.0

또는 pyproject.toml:

# Without simulation support
dependencies = ["flwr>=1.8,2.0"]

# With simulation support
dependencies = ["flwr[simulation]>=1.8,2.0"]

Poetry 사용#

pyproject.toml``에서 ``flwr 의존성를 업데이트한 다음 다시 설치하세요(``poetry install``을 실행하기 전에 ``rm poetry.lock``을 통해 ``poetry.lock``을 삭제하는 것을 잊지 마세요).

``pyproject.toml``에 다음 버전 제약 조건을 설정했는지 확인하세요:

[tool.poetry.dependencies]
python = "^3.8"

# Without simulation support
flwr = ">=1.8,<2.0"

# With simulation support
flwr = { version = ">=1.8,<2.0", extras = ["simulation"] }

필수 변경 사항#

Flower Next에서는 *infrastructure*와 *application layers*가 분리되었습니다. 코드에서 ``start_client()``를 통해 클라이언트를 시작하는 대신, 명령줄을 통해 |clientapp_link|_를 생성하여 시작합니다. 코드에서 ``start_server()``를 통해 서버를 시작하는 대신 |serverapp_link|_를 생성하고 명령줄을 통해 서버를 시작합니다. 서버와 클라이언트의 장기 실행 컴포넌트를 SuperLink와 SuperNode라고 합니다. 수동 업데이트가 필요하지 않고 기존 방식과 Flower Next 방식 모두에서 프로젝트를 실행할 수 있는 non-breaking 변경 사항은 다음과 같습니다:

ClientApp()#

  • |clientapp_link|_를 통해 실행하는 대신 기존 클라이언트를 |clientapp_link|_로 래핑하세요. 다음은 예시입니다:

# Flower 1.8
def client_fn(cid: str):
    return flwr.client.FlowerClient().to_client()

app = flwr.client.ClientApp(
   client_fn=client_fn,
)

# Flower 1.7
if __name__ == "__main__":
    flwr.client.start_client(
       server_address="127.0.0.1:8080",
       client=flwr.client.FlowerClient().to_client(),
    )

ServerApp()#

  • 서버를 시작하려면 |startserver_link|_를 통해 서버를 시작하는 대신 기존 전략을 |serverapp_link|_로 래핑하세요. 다음은 예시입니다:

# Flower 1.8
app = flwr.server.ServerApp(
    config=config,
    strategy=strategy,
)

# Flower 1.7
if __name__ == "__main__":
    flwr.server.start_server(
        server_address="0.0.0.0:8080",
        config=config,
        strategy=strategy,
    )

배포#

  • 실행하기 전에 |flowernext_superlink_link|_를 사용하여 ``SuperLink``를 실행한 후 |flowernext_clientapp_link|_(2회) 및 |flowernext_serverapp_link|_를 순서대로 실행합니다. ‘client.py’와 ‘server.py’를 Python 스크립트로 실행할 필요는 없습니다.

  • 다음은 HTTPS 없이 서버를 시작하는 예제입니다(프로토타이핑용으로만 사용):

# Start a Superlink
$ flower-superlink --insecure

# In a new terminal window, start a long-running SuperNode
$ flower-client-app client:app --insecure

# In another terminal window, start another long-running SuperNode (at least 2 SuperNodes are required)
$ flower-client-app client:app --insecure

# In yet another terminal window, run the ServerApp (this starts the actual training run)
$ flower-server-app server:app --insecure
  • 다음은 HTTPS로 시작하는 또 다른 예제입니다. ‘–ssl-ca-certfile`, ‘–ssl-certfile`, ‘–ssl-keyfile` 명령줄 옵션을 사용하여 (CA 인증서, 서버 인증서 및 서버 개인 키)의 경로를 전달합니다.

# Start a secure Superlink
$ flower-superlink \
    --ssl-ca-certfile <your-ca-cert-filepath> \
    --ssl-certfile <your-server-cert-filepath> \
    --ssl-keyfile <your-privatekey-filepath>

# In a new terminal window, start a long-running secure SuperNode
$ flower-client-app client:app \
    --root-certificates <your-ca-cert-filepath> \
    --superlink 127.0.0.1:9092

# In another terminal window, start another long-running secure SuperNode (at least 2 SuperNodes are required)
$ flower-client-app client:app \
    --root-certificates <your-ca-cert-filepath> \
    --superlink 127.0.0.1:9092

# In yet another terminal window, run the ServerApp (this starts the actual training run)
$ flower-server-app server:app \
    --root-certificates <your-ca-cert-filepath> \
    --superlink 127.0.0.1:9091

CLI 시뮬레이션#

  • 기존 클라이언트와 전략을 각각 |clientapp_link|_와 |serverapp_link|_로 래핑하세요. 더 이상 |startsim_link|_를 사용할 필요가 없습니다. 다음은 예시입니다:

# Regular Flower client implementation
class FlowerClient(NumPyClient):
    # ...

# Flower 1.8
def client_fn(cid: str):
    return FlowerClient().to_client()

client_app = flwr.client.ClientApp(
   client_fn=client_fn,
)

server_app = flwr.server.ServerApp(
    config=config,
    strategy=strategy,
)

# Flower 1.7
if __name__ == "__main__":
    hist = flwr.simulation.start_simulation(
        num_clients=100,
        ...
    )
  • CLI에서 |flower_simulation_link|_를 실행하고 Python 스크립트를 실행하는 대신 코드에서 server_app / client_app 개체를 가리키세요. 다음은 예제입니다(server_appclient_app 객체가 sim.py 모듈에 있다고 가정):

# Flower 1.8
$ flower-simulation \
    --server-app=sim:server_app \
    --client-app=sim:client_app \
    --num-supernodes=100
# Flower 1.7
$ python sim.py
  • |startsim_link|_에서 client_resources 인수를 설정하는 대신 --backend-config 명령줄 인수를 사용하여 각 |clientapp_link|_에 대한 기본 리소스를 설정하세요. 다음은 예시입니다:

# Flower 1.8
$ flower-simulation \
    --client-app=sim:client_app \
    --server-app=sim:server_app \
    --num-supernodes=100 \
    --backend-config='{"client_resources": {"num_cpus": 2, "num_gpus": 0.25}}'
# Flower 1.7 (in `sim.py`)
if __name__ == "__main__":
    hist = flwr.simulation.start_simulation(
        num_clients=100,
        client_resources = {'num_cpus': 2, "num_gpus": 0.25},
        ...
    )

Notebook에서 시뮬레이션#

  • notebook에서 start_simulation() 대신 |runsim_link|_를 실행하세요. 다음은 예시입니다:

NUM_CLIENTS = <specify-an-integer>

def client_fn(cid: str):
    # ...
    return FlowerClient().to_client()

client_app = flwr.client.ClientApp(
   client_fn=client_fn,
)

server_app = flwr.server.ServerApp(
    config=config,
    strategy=strategy,
)

backend_config = {"client_resources": {"num_cpus": 2, "num_gpus": 0.25}}

# Flower 1.8
flwr.simulation.run_simulation(
    server_app=server_app,
    client_app=client_app,
    num_supernodes=NUM_CLIENTS,
    backend_config=backend_config,
)

# Flower 1.7
flwr.simulation.start_simulation(
    client_fn=client_fn,
    num_clients=NUM_CLIENTS,
    config=config,
    strategy=strategy,
    client_resources=backend_config["client_resources"],
)

추가 도움말#

일부 공식 Flower 코드 예제 <https://flower.ai/docs/examples/>`_는 이미 플라워 넥스트에 업데이트되어 있으므로 플라워 넥스트 API를 사용하는 참고할 있습니다. 궁금한 점이 있다면 ``플라워 슬랙 <https://flower.ai/join-slack/>`_에 가입하고 ``#questions 채널을 이용하세요. 또한, ``Flower Discuss <https://discuss.flower.ai/>`_에 참여하여 질문에 대한 답변을 확인하거나 다른 사람들과 Flower Next로의 이동에 대해 공유하고 배울 수 있습니다.

중요

Flower Next는 빠른 속도로 지속적으로 개선되고 있으므로 이 가이드는 주기적으로 업데이트될 예정입니다. 피드백이 있으면 언제든지 공유해 주세요!

행복한 마이그레이션! 🚀