-
Notifications
You must be signed in to change notification settings - Fork 826
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
add glu op #6065
add glu op #6065
Changes from all commits
cc24cdd
74de30a
9e8e58b
4ea4c15
a61d18f
d769766
e93cc42
5b62347
d78bee5
520f61b
0a21498
d77709e
32c613f
532081c
61c041a
1b6f2cc
b30f661
e5130b5
edbe1f5
01be855
8a08ca7
0bdeb81
90d5f37
44711ff
f52e5e6
dc8d3e1
3219ba4
1bb4711
e42eea9
197d7c7
35ed600
2735a51
96e185a
27c0b36
9b9d673
2058bfa
9556072
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ Operators for neural networks | |
Embedding, | ||
Flatten, | ||
GELU, | ||
GLU, | ||
GroupNorm, | ||
Hardsigmoid, | ||
Hardswish, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
from oneflow.nn.modules.activation import ( | ||
ELU, | ||
GELU, | ||
GLU, | ||
Hardsigmoid, | ||
Hardswish, | ||
Hardtanh, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -962,6 +962,50 @@ def forward(self, x): | |
return flow._C.softsign(x) | ||
|
||
|
||
class GLU(Module): | ||
hengzi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
r"""The GLU activation. | ||
|
||
Args: | ||
input (Tensor, float): input tensor. | ||
dim (int, optional): dimension on which to split the input. Default: -1 | ||
|
||
Shape: | ||
- Input: :math:`(\ast_1, N, \ast_2)` where `*` means, any number of additional | ||
dimensions | ||
- Output: :math:`(\ast_1, M, \ast_2)` where :math:`M=N/2` | ||
|
||
The formula is: | ||
|
||
.. math:: | ||
|
||
GLU(input) = GLU(a, b) = a \otimes sigmoid(b) | ||
|
||
.. note:: | ||
where input is split in half along dim to form a and b, ⊗ is the element-wise product between matrices. | ||
|
||
For example: | ||
|
||
.. code-block:: python | ||
|
||
>>> import oneflow as flow | ||
>>> import oneflow.nn as nn | ||
>>> m = nn.GLU() | ||
>>> x = flow.tensor([[1, 2, 3, 4], [5, 6, 7, 8]], dtype=flow.float32) | ||
>>> y = m(x) | ||
>>> y | ||
tensor([[0.9526, 1.9640], | ||
[4.9954, 5.9980]], dtype=oneflow.float32) | ||
|
||
""" | ||
|
||
def __init__(self, dim: Optional[int] = -1): | ||
super().__init__() | ||
self.dim = dim | ||
|
||
def forward(self, input): | ||
return flow._C.glu(input, self.dim) | ||
|
||
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. 需要导出GLU的module: 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. 函数逻辑可以注册op/kernel,并通过functor接口导出, 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.
GLU已导出,在c++层使用算子拼接实现 |
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
""" | ||
Copyright 2020 The OneFlow Authors. All rights reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
""" | ||
|
||
import unittest | ||
from automated_test_util import * | ||
|
||
import oneflow as flow | ||
import oneflow.unittest | ||
|
||
|
||
@flow.unittest.skip_unless_1n1d() | ||
class TestGluModule(flow.unittest.TestCase): | ||
@autotest(n=5) | ||
def test_glu_module_with_random_data(test_case): | ||
device = random_device() | ||
dim = random(-3, 3).to(int) | ||
m = torch.nn.functional.glu | ||
x = random_pytorch_tensor(ndim=3, dim0=2, dim1=4, dim2=6).to(device) | ||
y = m(x, dim) | ||
return y | ||
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. 导出 nn.GLU接口, 应该也要加上对应的测试 |
||
|
||
@autotest(n=5) | ||
def test_GLU_module_with_random_data(test_case): | ||
device = random_device() | ||
m = torch.nn.GLU() | ||
m.train(random()) | ||
m.to(device) | ||
x = random_pytorch_tensor(ndim=3, dim0=2, dim1=4, dim2=6).to(device) | ||
y = m(x) | ||
return y | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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.
和torch对齐,这里需要check input的ndim>0:
CHECK_GT_OR_RETURN(ndim, 0) << "glu does not support 0-dimensional tensors";