-
Notifications
You must be signed in to change notification settings - Fork 15
refactor(aggregation): Make Stateful an iff and add Stochastic #656
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
Open
PierreQuinton
wants to merge
11
commits into
main
Choose a base branch
from
refactor-stateful
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b291573
Make Stateful an iff
PierreQuinton 56ea02a
Add test of stateful/stateless (all must be one or the other).
PierreQuinton a6735c0
Make GradVac, GradDrop, PCGrad and Random Stochastic (borken).
PierreQuinton b2541f4
Merge branch 'main' into refactor-stateful
ValerianRey df450a7
Fix mistakes when resolving merge conflict
ValerianRey b6c6d8b
Fix
ValerianRey 0bf7626
Fix nashmtl test
ValerianRey 1bc6be2
Fix nahsmtl
ValerianRey 18aef6c
Make Stochastic public
ValerianRey 62a269f
Fix doc
ValerianRey 477f19f
Fix asserts
ValerianRey 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
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
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 |
|---|---|---|
| @@ -1,9 +1,60 @@ | ||
| from abc import ABC, abstractmethod | ||
|
|
||
| import torch | ||
|
|
||
|
|
||
| class Stateful(ABC): | ||
| """Mixin adding a reset method.""" | ||
| r""" | ||
| Mixin for stateful mappings. | ||
|
|
||
| A maping implements `Stateful` **if and only if** its behavior depends on an internal | ||
| state. | ||
|
|
||
| Formally, a stateless mapping is a function :math:`f : x \mapsto y` whereas a stateful | ||
| maping is a transition map :math:`A : (x, s) \mapsto (y, s')` where :math:`s` is the | ||
| internal state, :math:`s'` the updated state, and :math:`y` the output. | ||
| There exists an initial state :math:`s_0`, and the method `reset()` restores the state to | ||
| :math:`s_0`. A `Stateful` mapping must be constructed with the intial state :math:`s_0`. | ||
| """ | ||
|
|
||
| @abstractmethod | ||
| def reset(self) -> None: | ||
| """Resets the internal state.""" | ||
| """Resets the internal state :math:`s_0`.""" | ||
|
|
||
|
|
||
| class Stochastic(Stateful, ABC): | ||
| r""" | ||
| Stateful mixin that represents mappings that have inherent randomness. | ||
|
|
||
| Internally, a ``Stochastic`` mapping holds a :class:`torch.Generator` that serves as an | ||
| independent random number stream. Implementing classes must pass this generator to all torch | ||
| random functions via their ``generator`` argument, e.g.: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| torch.rand(n, generator=self.generator) | ||
| torch.randn(n, generator=self.generator) | ||
| torch.randperm(n, generator=self.generator) | ||
|
|
||
| :param seed: Seed for the internal :class:`torch.Generator`. If ``None``, a seed is drawn | ||
| from the global PyTorch RNG to fork an independent stream. | ||
| :param generator: An existing :class:`torch.Generator` to share, typically from a companion | ||
| :class:`Stochastic` instance (e.g. a :class:`Weighting` sharing the generator of its | ||
| :class:`Aggregator`). Mutually exclusive with ``seed``. | ||
| """ | ||
|
|
||
| def __init__(self, seed: int | None = None, generator: torch.Generator | None = None) -> None: | ||
| if generator is not None and seed is not None: | ||
| raise ValueError("Parameters `seed` and `generator` are mutually exclusive.") | ||
| if generator is not None: | ||
| self.generator = generator | ||
| else: | ||
| self.generator = torch.Generator() | ||
| if seed is None: | ||
| seed = int(torch.randint(0, 2**62, size=(1,), dtype=torch.int64).item()) | ||
| self.generator.manual_seed(seed) | ||
| self._initial_rng_state = self.generator.get_state() | ||
|
|
||
| def reset(self) -> None: | ||
| """Resets the random number generator to its initial state.""" | ||
| self.generator.set_state(self._initial_rng_state) | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generator requires a device, so this wont work for cuda I think. And we don't know the device at that point, so I don't think it's easy to fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This leads to
RuntimeError: Expected a 'cuda' device type for generator but found 'cpu'Need a different implementation for Stochastic I think.