-
Notifications
You must be signed in to change notification settings - Fork 286
Add Dish activation #719
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
Add Dish activation #719
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5b0876e
Add Ops.(backprop_)dish and CUDA kernel
danieldk 8a9498a
Make mypy happy
danieldk ff9dfa1
test_compare_activations_to_torch: test with different dY
danieldk b75422e
test_compare_activations_to_torch: be slightly more (absolute) tolerant
danieldk 842bd32
doc fix
danieldk e9d8f40
Merge remote-tracking branch 'upstream/master' into dish
danieldk e96f959
Update dish types to use `FloatsXdT`
danieldk 9fa7136
docs: add version tag to `(backprop_)dish`
danieldk 953a0ab
Add Dish Thinc layer
danieldk f2d6d5e
Add Dish layer docs
danieldk e7a946f
Fix dish description
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| from typing import Tuple, Optional, Callable, cast | ||
|
|
||
| from ..config import registry | ||
| from ..model import Model | ||
| from .chain import chain | ||
| from .layernorm import LayerNorm | ||
| from .dropout import Dropout | ||
| from ..types import Floats1d, Floats2d | ||
| from ..util import partial, get_width | ||
| from ..initializers import he_normal_init, zero_init | ||
|
|
||
|
|
||
| @registry.layers("Dish.v1") | ||
| def Dish( | ||
| nO: Optional[int] = None, | ||
| nI: Optional[int] = None, | ||
| *, | ||
| init_W: Callable = he_normal_init, | ||
| init_b: Callable = zero_init, | ||
| dropout: Optional[float] = None, | ||
| normalize: bool = False, | ||
| ) -> Model[Floats2d, Floats2d]: | ||
| model: Model[Floats2d, Floats2d] = Model( | ||
| "dish", | ||
| forward, | ||
| init=partial(init, init_W, init_b), | ||
| dims={"nO": nO, "nI": nI}, | ||
| params={"W": None, "b": None}, | ||
| ) | ||
| if normalize: | ||
| model = chain(model, LayerNorm(nI=nO)) | ||
| if dropout is not None: | ||
| model = chain(model, cast(Model[Floats2d, Floats2d], Dropout(dropout))) | ||
| return model | ||
|
|
||
|
|
||
| def forward( | ||
| model: Model[Floats2d, Floats2d], X: Floats2d, is_train: bool | ||
| ) -> Tuple[Floats2d, Callable]: | ||
| W = cast(Floats2d, model.get_param("W")) | ||
| b = cast(Floats1d, model.get_param("b")) | ||
| Y_preact = model.ops.affine(X, W, b) | ||
| Y = model.ops.dish(Y_preact) | ||
|
|
||
| def backprop(dY: Floats2d) -> Floats2d: | ||
| dY = model.ops.backprop_dish(dY, X, inplace=False) | ||
| model.inc_grad("b", dY.sum(axis=0)) | ||
| model.inc_grad("W", model.ops.gemm(dY, X, trans1=True)) | ||
| return model.ops.gemm(dY, W) | ||
|
|
||
| return Y, backprop | ||
|
|
||
|
|
||
| def init( | ||
| init_W: Callable, | ||
| init_b: Callable, | ||
| model: Model[Floats2d, Floats2d], | ||
| X: Optional[Floats2d] = None, | ||
| Y: Optional[Floats2d] = None, | ||
| ) -> None: | ||
| if X is not None: | ||
| model.set_dim("nI", get_width(X)) | ||
| if Y is not None: | ||
| model.set_dim("nO", get_width(Y)) | ||
| model.set_param("W", init_W(model.ops, (model.get_dim("nO"), model.get_dim("nI")))) | ||
| model.set_param("b", init_b(model.ops, (model.get_dim("nO"),))) | ||
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
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.