NVFlare icon indicating copy to clipboard operation
NVFlare copied to clipboard

FOX API

Open yanchengnv opened this issue 11 months ago • 1 comments

Fixes # .

Description

This PR introduces federated object exchange - a new way to develop FL applications.

Types of changes

  • [x] Non-breaking change (fix or new feature that would not break existing functionality).
  • [ ] Breaking change (fix or new feature that would cause existing functionality to change).
  • [ ] New tests added to cover the changes.
  • [ ] Quick tests passed locally by running ./runtest.sh.
  • [ ] In-line docstrings updated.
  • [ ] Documentation updated.

yanchengnv avatar Dec 11 '25 23:12 yanchengnv

Greptile Summary

Introduces the FOX (Federated Object Exchange) API - a new programming model for building federated learning applications in NVFlare. This is a major feature addition (~7700 lines) that provides an alternative to the existing workflow-based approach.

Key Changes:

  • New nvflare.fox package with API layer (fox.api), system integration (fox.sys), and simulation backend (fox.sim)
  • Decorator-based programming model using @collab, @algo, @init, @final for marking federated methods
  • Proxy pattern for transparent remote method invocation between server and clients
  • Filter chains for preprocessing calls and results (incoming/outgoing)
  • Context management using thread-local storage for accessing site information
  • Two backends: FlareBackend (production via F3 messaging) and SimBackend (local testing)
  • Support for hierarchical client topologies and group calls with parallel execution control
  • Integration with existing FLARE infrastructure (minimal changes to cell.py and server_engine.py)
  • Comprehensive examples for numpy and PyTorch workflows

Architecture:

  • App/ServerApp/ClientApp classes manage application lifecycle and configuration
  • Proxy objects provide typed remote method calls with interface discovery
  • Backend abstraction handles actual communication (F3 cells for FLARE, in-memory for simulation)
  • Context provides call-time information (caller, callee, workspace, abort signal)
  • facade class offers convenient static API for accessing context within collab methods

Notable Design Decisions:

  • Exceptions returned as values rather than raised in proxy calls to support async patterns
  • Thread-local context with proper parent context restoration
  • Interface introspection converts positional args to kwargs for filter processing
  • Deepcopy fallback with make_client_app method for client app instantiation

Confidence Score: 4/5

  • This PR introduces a well-architected new API with solid error handling and comprehensive examples, safe to merge with minor considerations
  • The FOX API is well-designed with proper separation of concerns, error handling, and resource cleanup. The code follows good patterns (context managers, thread executors with proper shutdown, proxy pattern). Integration with existing FLARE is minimal and non-breaking. However, this is a large feature (~7700 lines) with limited test coverage mentioned in PR description, and the production readiness would benefit from additional integration tests and documentation
  • No critical issues found, but nvflare/fox/sys/controller.py and nvflare/fox/sys/executor.py handle production integration and should be thoroughly tested

Important Files Changed

Filename Overview
nvflare/fox/api/app.py Core App class manages FOX applications with proxy setup, filter chains, and collab object registration - solid design with proper validation
nvflare/fox/api/proxy.py Proxy implementation for remote calls with interface discovery and argument adjustment - exception handling returns exceptions as values correctly
nvflare/fox/api/facade.py Convenient facade API using classproperties for accessing context, proxies, and workspace - clean interface design
nvflare/fox/api/dec.py Decorator implementations for marking collab methods, filters, and lifecycle hooks - proper attribute setting and introspection
nvflare/fox/sys/controller.py Server-side FOX controller integrates with FLARE, handles client sync, and sets up proxies - proper initialization and error handling
nvflare/fox/sys/executor.py Client-side FOX executor handles sync task and proxy setup with lifecycle management - proper thread executor cleanup
nvflare/fox/sys/backend.py FLARE backend implementation using F3 cell messaging for remote calls with proper timeout and error handling
nvflare/fox/sim/simulator.py Simulation backend for testing FOX apps locally with support for hierarchical client topologies and proper deepcopy handling
nvflare/fox/api/group.py Group call implementation supports parallel/sequential remote calls with result aggregation and proper semaphore-based concurrency control
nvflare/fox/api/ctx.py Thread-local context management with proper parent context restoration in __exit__ - clean context manager pattern

Sequence Diagram

sequenceDiagram
    participant User as User Code
    participant Server as ServerApp
    participant Proxy as Client Proxy
    participant Backend as FlareBackend
    participant Cell as F3 Cell
    participant ClientBackend as Client Backend
    participant ClientApp as ClientApp
    participant Method as Collab Method

    Note over User,Server: Initialization Phase
    User->>Server: Create ServerApp(server_obj)
    Server->>Server: Register @algo methods
    User->>Server: Setup filters/props
    
    Note over User,Method: Execution Phase
    Server->>Proxy: call client method via proxy
    Proxy->>Proxy: adjust_func_args() - convert args to kwargs
    Proxy->>Server: apply_outgoing_call_filters()
    Proxy->>Backend: call_target(context, target, func_name, kwargs)
    Backend->>Cell: send_request(channel, target_fqcn, request)
    
    Note over Cell,ClientBackend: Network Communication
    Cell-->>ClientBackend: Message via F3
    
    ClientBackend->>ClientApp: _call_app_method(request)
    ClientApp->>ClientApp: apply_incoming_call_filters()
    ClientApp->>Method: invoke collab method with context
    Method-->>ClientApp: return result
    ClientApp->>ClientApp: apply_outgoing_result_filters()
    ClientApp-->>ClientBackend: reply with result
    
    ClientBackend-->>Cell: Return message
    Cell-->>Backend: Reply message
    Backend->>Proxy: return result
    Proxy->>Server: apply_incoming_result_filters()
    Proxy-->>Server: filtered result
    Server-->>User: final result

greptile-apps avatar Dec 11 '25 23:12 greptile-apps

Superseded by #4901, which carries the same content from a fork branch owned by the current driver of this work (the Collab API evolution of the original FOX API). Closing in favor of that PR.

nvidianz avatar Jul 14 '26 14:07 nvidianz