-
Notifications
You must be signed in to change notification settings - Fork 58
Add Trainer Protocol #533
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
allenwang28
wants to merge
8
commits into
meta-pytorch:main
Choose a base branch
from
allenwang28:interfaces
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
Add Trainer Protocol #533
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8141cef
add trainer protocol
189b242
remove some get_* i'm not convinced on yet
d89bc1b
bulk changes
83f3a8f
forwardbackwardresult
4d11e4e
add custom loss
1d341eb
Merge branch 'main' into interfaces
8b988a9
Merge branch 'main' into interfaces
76995e5
address comments
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # All rights reserved. | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| # Forge library modules |
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,114 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # All rights reserved. | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| """Trainer protocol. | ||
|
|
||
| This file defines the unified training interface compatible | ||
| with all supported torchforge trainers. | ||
| """ | ||
|
|
||
| from typing import Any, Protocol, runtime_checkable | ||
|
|
||
| import torch | ||
|
|
||
|
|
||
| @runtime_checkable | ||
| class Trainer(Protocol): | ||
| """Protocol for all trainers in torchforge.""" | ||
|
|
||
| async def accumulate_gradients( | ||
| self, microbatch: dict[str, torch.Tensor] | ||
| ) -> dict[str, Any]: | ||
| """Accumulate gradients from one microbatch. | ||
|
|
||
| Does NOT clear gradients - they accumulate on top of existing. | ||
| Can be called multiple times before optim_step(). | ||
|
|
||
| Returns: | ||
| dict with keys: | ||
| - loss: float | ||
| - metrics: dict[str, float] | ||
| """ | ||
| ... | ||
|
|
||
| async def optim_step(self, params: dict[str, Any] | None = None) -> dict[str, Any]: | ||
| """Apply optimizer step and clear gradients after. | ||
|
|
||
| Returns: | ||
| dict with keys: | ||
| - step: int | ||
| - learning_rate: float | ||
| - accumulated_microbatches: int | ||
| """ | ||
| ... | ||
|
|
||
| async def clear_gradients(self) -> None: | ||
| """Clear accumulated gradients without applying.""" | ||
| ... | ||
|
|
||
| async def forward(self, inputs: dict[str, torch.Tensor]) -> dict[str, torch.Tensor]: | ||
| """Run forward pass, no backward. | ||
|
|
||
| Returns: | ||
| dict with key: | ||
| - logits: torch.Tensor | ||
| """ | ||
| ... | ||
|
|
||
| async def forward_backward( | ||
| self, data: list[dict[str, torch.Tensor]] | ||
allenwang28 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) -> dict[str, Any]: | ||
| """Clear first, then forward+backward on all items in data. | ||
allenwang28 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Convenience wrapper equivalent to: | ||
| clear_gradients() + accumulate_gradients() for each item | ||
|
|
||
| Does NOT call optim_step() - you must call it separately. | ||
|
|
||
| Returns: | ||
| dict with keys: | ||
| - loss: float | ||
| - metrics: dict[str, float] | ||
| """ | ||
| ... | ||
|
|
||
| async def save_state(self, name: str) -> dict[str, Any]: | ||
| """Save the checkpoint. | ||
|
|
||
| Returns: | ||
| dict with keys: | ||
| - path: str | ||
| - step: int | ||
| """ | ||
| ... | ||
|
|
||
| async def load_state(self, path: str) -> dict[str, Any]: | ||
| """Load checkpoint. | ||
|
|
||
| Returns: | ||
| dict with keys: | ||
| - step: int | ||
| - learning_rate: float | ||
allenwang28 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| ... | ||
|
|
||
| async def save_weights_for_sampler(self, name: str) -> dict[str, Any]: | ||
| """Export weights for inference. | ||
|
|
||
| Returns: | ||
| dict with keys: | ||
| - path: str | ||
| - version: str or int | ||
| """ | ||
| ... | ||
allenwang28 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def get_tokenizer(self): | ||
| """Get the tokenizer. | ||
|
|
||
| Returns: | ||
| PreTrainedTokenizer | ||
| """ | ||
| ... | ||
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.