-
Notifications
You must be signed in to change notification settings - Fork 2
[1/3] Federation implementation #262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
75c5d29
minor logging
ipmach 8cdc84c
move component to _basic_component
ipmach 39c5c12
start integrating prototype code into production
ipmach 67c3f18
add initial tests
ipmach c469bba
add warning tests
ipmach 6c5a0e4
update overall architecture
ipmach 81c60ee
first version of docs
ipmach 68fdda7
add automated examples in federated docs
ipmach 0faee06
Improve clarity and fix typos in federation.md
ipmach 6be9eb5
address comments
ipmach File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
|
|
||
| # --8<-- [start:example_1] | ||
| from detectmatelibrary.common.core import CoreComponent | ||
| import struct | ||
|
|
||
|
|
||
| class NewComponent(CoreComponent): # Inherent from CoreComponent | ||
| def __init__(self, elems): | ||
| self.elems = elems | ||
| super().__init__(name="FedExample") | ||
|
|
||
| def aggregate_strategy(self, components): | ||
| final_list = [] | ||
| for component in components: | ||
| final_list.extend(component.elems) | ||
|
|
||
| final_list = list(set(final_list)) | ||
| for component in components: | ||
| component.elems = final_list | ||
|
|
||
| def to_binary(self): | ||
| return struct.pack(f">{len(self.elems)}h", *self.elems) | ||
|
|
||
| def from_binary(self, binary): | ||
| num_ints = len(binary) // 2 | ||
| elems = list(struct.unpack(f">{num_ints}h", binary)) | ||
| return NewComponent(elems=elems) | ||
|
|
||
|
|
||
| # --8<-- [end:example_1] | ||
| # --8<-- [start:example_2] | ||
| detector1 = NewComponent([1, 2, 3]) | ||
| detector2 = NewComponent([4, 5]) | ||
| detector3 = NewComponent([6]) | ||
|
|
||
| detector1 + detector2 + detector3 | ||
|
|
||
| detector2.aggregate() # Detector 2 is used as centralize node | ||
|
|
||
| print("Dectector 3", detector3.elems) # All detectors have been updated | ||
|
|
||
| # --8<-- [end:example_2] | ||
|
|
||
| # --8<-- [start:example_3] | ||
| detector1 = NewComponent([1, 2, 3]) | ||
| detector2 = NewComponent([4, 5]) | ||
| detector3 = NewComponent([6]) | ||
|
|
||
| detector1 + detector2 + detector3 | ||
|
|
||
| detector2.aggregate() # Detector 2 is used as centralize node | ||
|
|
||
| print("Dectector 3", detector3.elems) # All detectors have been updated | ||
| # --8<-- [end:example_3] | ||
|
|
||
| # --8<-- [start:example_4] | ||
| detector1 = NewComponent([1, 2, 3]) | ||
| detector2 = NewComponent([4, 5]) | ||
| detector3 = NewComponent([6]) | ||
|
|
||
| detector1.stack([detector2, detector3]) | ||
|
|
||
| detector2.aggregate() | ||
| print("Detector 3", detector3.elems) # It is not longer combine but stack, so it will not work | ||
|
|
||
| detector1.aggregate() | ||
| print("Detector 3", detector3.elems) # Now it will work | ||
|
|
||
| # --8<-- [end:example_4] | ||
|
|
||
| # --8<-- [start:example_5] | ||
| detector1 = NewComponent([1, 2, 3]) | ||
|
|
||
| binary2 = NewComponent([4, 5]).to_binary() | ||
| binary3 = (detector3 := NewComponent([6])).to_binary() | ||
| print("Binary of Detector 3", binary3) | ||
|
|
||
| detector1.stack([binary2, binary3]) | ||
| output = detector1.aggregate(unstack=True) # unstack = True will free memory | ||
|
|
||
| print("Detector 1", detector1.elems) # Detector 1 has been updated | ||
| print("Output binary", output) # Output that we send to other componets | ||
|
|
||
| # We update detector 3 now | ||
| print("Detector 3", detector3.elems) # Now detector 3 is not in the share memory so it will not work | ||
| detector3 = detector3.from_binary(output) | ||
| print("Detector 3", detector3.elems) # Now it will work | ||
| # --8<-- [end:example_5] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # Federation | ||
|
|
||
| This section explains how to use the federation setup. For a component to support federation, it must implement the following methods: | ||
|
|
||
| ```python | ||
| def to_binary(self) -> bytes | None: | ||
| """(Federation only) Serialize to bytes for federation operations.""" | ||
|
|
||
| def from_binary(self, binary: bytes) -> object: | ||
| """(Federation only) Deserialize from bytes for federation operations.""" | ||
|
|
||
| def aggregate_strategy(self, components: set["FedOperations"]) -> None: | ||
| """(Federation only) Define how to aggregate a set of federated components.""" | ||
| ``` | ||
|
|
||
| There are two main ways to use federation: | ||
|
|
||
| - **Combine first**: This can only be used when all components run locally. The main idea is to simplify the process by allowing components to share memory. | ||
| - **Stack later**: A more standard federated approach where the "weights" or state of each component are combined at the end. | ||
|
|
||
| ## Example class | ||
|
|
||
| For all the examples below, we will use this code: | ||
|
|
||
| ```python | ||
| --8<-- "docs/examples/others/federation.py:example_1" | ||
| ``` | ||
|
|
||
| ## Combine first | ||
|
|
||
| The diagram below shows the workflow: | ||
|
|
||
|  | ||
|
|
||
| Example 1: | ||
|
|
||
| ```python | ||
| --8<-- "docs/examples/others/federation.py:example_2" | ||
| ``` | ||
|
|
||
| Example 2: | ||
|
|
||
| ```python | ||
| --8<-- "docs/examples/others/federation.py:example_3" | ||
| ``` | ||
|
|
||
| ## Stack | ||
|
|
||
| The diagram below shows the workflow: | ||
|
|
||
|  | ||
|
|
||
| Example 1: | ||
|
|
||
| ```python | ||
| --8<-- "docs/examples/others/federation.py:example_4" | ||
| ``` | ||
|
|
||
| Example 2: | ||
|
|
||
| ```python | ||
| --8<-- "docs/examples/others/federation.py:example_5" | ||
| ``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| from detectmatelibrary.utils.persistency.component_interfaces import Stoppable | ||
| from detectmatelibrary.schemas import BaseSchema | ||
|
|
||
| from detectmatelibrary.common._config import BasicConfig | ||
|
|
||
| from typing import Any, Dict, List | ||
|
|
||
|
|
||
| class Component: | ||
| """Empty methods.""" | ||
| def __init__( | ||
| self, | ||
| name: str, | ||
| type_: str = "Core", | ||
| config: BasicConfig = BasicConfig(), | ||
| ) -> None: | ||
| self.name, self.type_, self.config = name, type_, config | ||
| self.saver: Stoppable | None = None | ||
|
|
||
| def __repr__(self) -> str: | ||
| return f"<{self.type_}> {self.name}: {self.config}" | ||
|
|
||
| def run( | ||
| self, input_: List[BaseSchema] | BaseSchema, output_: BaseSchema | ||
| ) -> bool: | ||
| return False | ||
|
|
||
| def train( | ||
| self, input_: List[BaseSchema] | BaseSchema, | ||
| ) -> None: | ||
| pass | ||
|
|
||
| def configure( | ||
| self, input_: List[BaseSchema] | BaseSchema, | ||
| ) -> None: | ||
| pass | ||
|
|
||
| def set_configuration(self) -> None: | ||
| pass | ||
|
|
||
| def post_train(self) -> None: | ||
| pass | ||
|
|
||
| def get_config(self) -> Dict[str, Any]: | ||
| return self.config.get_config() | ||
|
|
||
| def update_config(self, new_config: Dict[str, Any]) -> None: | ||
| self.config.update_config(new_config) | ||
|
|
||
| def __enter__(self) -> "Component": | ||
| return self | ||
|
|
||
| def __exit__(self, *_: Any) -> None: | ||
| if self.saver is not None: | ||
| self.saver.stop() |
108 changes: 108 additions & 0 deletions
108
src/detectmatelibrary/common/_core_op/_fed_component.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
|
|
||
| import warnings | ||
| from typing import Self, overload | ||
|
|
||
|
|
||
| class IncompatibleFed(Exception): | ||
| def __init__(self) -> None: | ||
| super().__init__("Instances are incompatible") | ||
|
|
||
|
|
||
| class _CompOp: | ||
| @staticmethod | ||
| def is_compatible(main_inst: object, other_inst: object) -> None: | ||
| if not isinstance(other_inst, type(main_inst)): | ||
| raise IncompatibleFed() | ||
|
|
||
| @staticmethod | ||
| def reset(main_inst: object, attr: str) -> None: | ||
| main_inst.__setattr__(attr, {main_inst}) | ||
|
|
||
| @staticmethod | ||
| def combine(main_inst: object, attr: str, other_inst: object) -> None: | ||
| _CompOp.is_compatible(main_inst, other_inst) | ||
| set_main: set[object] = getattr(main_inst, attr) | ||
|
|
||
| set_main.update(getattr(other_inst, attr)) | ||
| for elem in set_main: | ||
| getattr(elem, attr).update(set_main) | ||
|
|
||
| @staticmethod | ||
| def uncombine(main_inst: object, attr: str, other_inst: object) -> None: | ||
| _CompOp.is_compatible(main_inst, other_inst) | ||
| set_main: set[object] = getattr(main_inst, attr) | ||
|
|
||
| for elem in list(set_main): | ||
| if elem == other_inst: | ||
| _CompOp.reset(other_inst, attr) | ||
| else: | ||
| elem._components.remove(other_inst) # type: ignore | ||
|
|
||
| @staticmethod | ||
| def stack(main_inst: object, attr: str, list_other_inst: list[object]) -> None: | ||
| set_main: set[object] = getattr(main_inst, attr) | ||
| for other_inst in list_other_inst: | ||
| _CompOp.is_compatible(main_inst, other_inst) | ||
| set_main.add(other_inst) | ||
|
|
||
|
|
||
| class FedOperations: | ||
| """Operations related to the federation learning / agregation.""" | ||
| __COMPONENT: str = "_components" | ||
|
|
||
| def __init__(self) -> None: | ||
| self._components: set["FedOperations"] | ||
| _CompOp.reset(self, self.__COMPONENT) | ||
|
|
||
| def __add__(self, other: object) -> Self: | ||
| """Add other components to do the aggregation in a combine first | ||
| approach.""" | ||
| _CompOp.combine(self, attr=self.__COMPONENT, other_inst=other) | ||
|
|
||
| return self | ||
|
|
||
| def __sub__(self, other: object) -> Self: | ||
| """Remove other components to do the aggregation in a combine first | ||
| approach.""" | ||
| _CompOp.uncombine(self, attr=self.__COMPONENT, other_inst=other) | ||
|
|
||
| return self | ||
|
|
||
| @overload | ||
| def stack(self, other: bytes | list[bytes]) -> None: | ||
| pass | ||
|
|
||
| @overload | ||
| def stack(self, other: object | list[object]) -> None: | ||
| """Stack other components to do the aggregation in a stack later | ||
| approach.""" | ||
| pass | ||
|
|
||
| def stack(self, other: object | list[object | bytes] | bytes) -> None: | ||
| if not isinstance(other, list): | ||
| other = [other] | ||
| _CompOp.stack( | ||
| self, attr=self.__COMPONENT, list_other_inst=[ | ||
| self.from_binary(inst) if isinstance(inst, bytes) else inst for inst in other | ||
| ] | ||
| ) | ||
|
|
||
| def aggregate(self, unstack: bool = False) -> None | bytes: | ||
| self.aggregate_strategy(self._components) | ||
|
|
||
| if unstack: | ||
| _CompOp.reset(self, self.__COMPONENT) | ||
|
|
||
| return self.to_binary() | ||
|
|
||
| def to_binary(self) -> bytes | None: | ||
| warnings.warn("To binary not implemented, return None") | ||
| return None | ||
|
|
||
| def from_binary(self, binary: bytes) -> object: | ||
| warnings.warn(f"From binary not implemented, return None for {binary!r}") | ||
| return None | ||
|
|
||
| def aggregate_strategy(self, components: set["FedOperations"]) -> None: | ||
| """Aggregation strategy use by the component.""" | ||
| warnings.warn(f"No strategy found, aggregations does nothing for {components}") | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.