-
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 all 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
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 |
|---|---|---|
| @@ -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) | ||
kadarakos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,6 +64,9 @@ def torch_hard_swish_mobilenet(x): | |
| def torch_sigmoid(x): | ||
| return torch.sigmoid(x) | ||
|
|
||
| def torch_dish(x): | ||
| return 0.5 * x * (x / (1 + x * x).sqrt() + 1) | ||
|
Comment on lines
+67
to
+68
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is |
||
|
|
||
| # https://github.com/huggingface/transformers/blob/master/src/transformers/activations.py#L37 | ||
| def torch_gelu_approx(x): | ||
| return ( | ||
|
|
@@ -89,6 +92,7 @@ def torch_gelu(x): | |
| ("swish", torch_swish), | ||
| ("hard_swish", torch_hard_swish), | ||
| ("hard_swish_mobilenet", torch_hard_swish_mobilenet), | ||
| ("dish", torch_dish), | ||
| ("gelu_approx", torch_gelu_approx), | ||
| ("gelu", torch_gelu), | ||
| ("sigmoid", torch_sigmoid), | ||
|
|
@@ -1043,6 +1047,7 @@ def test_mish(ops, X): | |
| "op", | ||
| [ | ||
| "backprop_clipped_linear", | ||
| "backprop_dish", | ||
| "backprop_gelu", | ||
| "backprop_gelu_approx", | ||
| "backprop_hard_sigmoid", | ||
|
|
@@ -1160,6 +1165,16 @@ def test_gelu_approx(ops, X): | |
| assert not ops.xp.isnan(Y).any() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("ops", ALL_OPS) | ||
| @settings(max_examples=MAX_EXAMPLES, deadline=None) | ||
| @given(X=strategies.arrays_BI()) | ||
| def test_dish(ops, X): | ||
| X = ops.asarray(X) | ||
| Y = ops.dish(X) | ||
| assert Y.shape == X.shape | ||
| assert not ops.xp.isnan(Y).any() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("ops", ALL_OPS) | ||
| @settings(max_examples=MAX_EXAMPLES, deadline=None) | ||
| @given(X=strategies.arrays_BI()) | ||
|
|
@@ -1350,8 +1365,8 @@ def test_ngrams(): | |
| @pytest.mark.parametrize("dtype", ["float32", "float64"]) | ||
| @pytest.mark.parametrize("torch_func", TORCH_FUNCS) | ||
| @settings(max_examples=MAX_EXAMPLES, deadline=None) | ||
| @given(x=strategies.floats(min_value=-30, max_value=30)) | ||
| def test_compare_activations_to_torch(ops, dtype, x, torch_func): | ||
| @given(x=strategies.floats(min_value=-30, max_value=30), dY=strategies.floats(min_value=-1, max_value=1)) | ||
| def test_compare_activations_to_torch(ops, dtype, x, dY, torch_func): | ||
| import torch | ||
|
|
||
| func_name, pytorch_func = torch_func | ||
|
|
@@ -1369,9 +1384,9 @@ def test_compare_activations_to_torch(ops, dtype, x, torch_func): | |
| y_think_inplace = forward(x_thinc, inplace=True) | ||
| assert y_think_inplace is x_thinc | ||
| assert ops.xp.isclose(y_thinc, y_think_inplace, atol=1e-06) | ||
| assert ops.xp.isclose(y_thinc, y.detach(), atol=1e-06) | ||
| assert ops.xp.isclose(y_thinc, y.detach(), atol=1e-05) | ||
| x_thinc = ops.asarray([x], dtype=dtype) | ||
| dY_thinc = ops.asarray([1.0], dtype=dtype) | ||
| dY_thinc = ops.asarray([dY], dtype=dtype) | ||
| dY_thinc_inplace = dY_thinc.copy() | ||
|
|
||
| s = inspect.signature(backward) | ||
|
|
@@ -1386,23 +1401,23 @@ def test_compare_activations_to_torch(ops, dtype, x, torch_func): | |
| ) | ||
| assert dx_thinc_inplace is dY_thinc_inplace | ||
| assert ops.xp.isclose(dx_thinc, dx_thinc_inplace) | ||
| assert ops.xp.isclose(x_torch.grad.item(), float(dx_thinc), atol=1e-06) | ||
| assert ops.xp.isclose(x_torch.grad.item() * dY, float(dx_thinc), atol=1e-06) | ||
| elif params == {"Y", "dY"}: | ||
| dx_thinc = backward(dY_thinc, Y=y_thinc) | ||
| assert dx_thinc.dtype == x_thinc.dtype | ||
| assert ops.xp.isclose( | ||
| dx_thinc, | ||
| backward(dY=dY_thinc_inplace, Y=y_thinc, inplace=True), | ||
| ) | ||
| assert ops.xp.isclose(x_torch.grad.item(), float(dx_thinc), atol=1e-06) | ||
| assert ops.xp.isclose(x_torch.grad.item() * dY, float(dx_thinc), atol=1e-06) | ||
| elif params == {"dY", "X"}: | ||
| dx_thinc = backward(dY_thinc, X=x_thinc) | ||
| assert dx_thinc.dtype == x_thinc.dtype | ||
| assert ops.xp.isclose( | ||
| dx_thinc, backward(dY=dY_thinc_inplace, X=x_thinc, inplace=True) | ||
| ) | ||
| assert ops.xp.isclose( | ||
| x_torch.grad.item(), float(backward(dY_thinc, X=x_thinc)), atol=1e-06 | ||
| x_torch.grad.item() * dY, float(backward(dY_thinc, X=x_thinc)), atol=1e-06 | ||
| ) | ||
| else: | ||
| raise NotImplementedError( | ||
|
|
||
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.
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.