Skip to content
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

Merged
merged 37 commits into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
cc24cdd
add glu op
Ikkyu321 Aug 26, 2021
74de30a
Merge branch 'master' into dev_glu_op
Ikkyu321 Aug 26, 2021
9e8e58b
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow into …
Ikkyu321 Aug 26, 2021
4ea4c15
Merge branch 'master' into dev_glu_op
Ikkyu321 Aug 26, 2021
a61d18f
del glu_op export,align with torch
Ikkyu321 Aug 26, 2021
d769766
Merge branch 'dev_glu_op' of https://github.com/Oneflow-Inc/oneflow i…
Ikkyu321 Aug 26, 2021
e93cc42
Merge branch 'master' into dev_glu_op
Ikkyu321 Aug 27, 2021
5b62347
Merge branch 'master' into dev_glu_op
Ikkyu321 Aug 27, 2021
d78bee5
Merge branch 'master' into dev_glu_op
Ikkyu321 Aug 27, 2021
520f61b
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow into …
Ikkyu321 Aug 27, 2021
0a21498
Merge branch 'master' into dev_glu_op
Ikkyu321 Aug 27, 2021
d77709e
mod glu_op
Ikkyu321 Aug 27, 2021
32c613f
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow into …
Ikkyu321 Aug 27, 2021
532081c
Merge branch 'dev_glu_op' of https://github.com/Oneflow-Inc/oneflow i…
Ikkyu321 Aug 27, 2021
61c041a
Merge branch 'master' into dev_glu_op
Ikkyu321 Aug 27, 2021
1b6f2cc
Merge branch 'master' into dev_glu_op
Ikkyu321 Sep 1, 2021
b30f661
mov op logic to C++
Ikkyu321 Sep 1, 2021
e5130b5
Merge branch 'master' into dev_glu_op
Ikkyu321 Sep 2, 2021
edbe1f5
Merge branch 'master' into dev_glu_op
Ikkyu321 Sep 3, 2021
01be855
Solve problems
Ikkyu321 Sep 3, 2021
8a08ca7
solve conflict
Ikkyu321 Sep 6, 2021
0bdeb81
Merge branch 'master' into dev_glu_op
Ikkyu321 Sep 6, 2021
90d5f37
conflict fixed
Ikkyu321 Sep 6, 2021
44711ff
delete gradient functor
Ikkyu321 Sep 7, 2021
f52e5e6
add ndim check
Ikkyu321 Sep 7, 2021
dc8d3e1
Merge branch 'master' into dev_glu_op
Ikkyu321 Sep 7, 2021
3219ba4
Merge branch 'master' into dev_glu_op
oneflow-ci-bot Sep 7, 2021
1bb4711
Merge branch 'master' into dev_glu_op
oneflow-ci-bot Sep 7, 2021
e42eea9
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow into …
Ikkyu321 Sep 7, 2021
197d7c7
Merge branch 'dev_glu_op' of https://github.com/Oneflow-Inc/oneflow i…
Ikkyu321 Sep 7, 2021
35ed600
add GLU test
Ikkyu321 Sep 7, 2021
2735a51
delete blank line
Ikkyu321 Sep 7, 2021
96e185a
format
hengzi Sep 7, 2021
27c0b36
Merge branch 'master' into dev_glu_op
oneflow-ci-bot Sep 7, 2021
9b9d673
Merge branch 'master' into dev_glu_op
oneflow-ci-bot Sep 7, 2021
2058bfa
Merge branch 'master' into dev_glu_op
oneflow-ci-bot Sep 7, 2021
9556072
Merge branch 'master' into dev_glu_op
oneflow-ci-bot Sep 7, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/source/functional.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Functional operations for neural networks
.. autofunction:: prelu
.. autofunction:: log_sigmoid
.. autofunction:: gelu
.. autofunction:: glu
.. autofunction:: softsign
.. autofunction:: softmax
.. autofunction:: softplus
Expand Down
1 change: 1 addition & 0 deletions docs/source/nn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Operators for neural networks
Embedding,
Flatten,
GELU,
GLU,
GroupNorm,
Hardsigmoid,
Hardswish,
Expand Down
4 changes: 4 additions & 0 deletions oneflow/core/functional/functional_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@
signature: "Tensor (Tensor dy, Tensor x) => GeluGrad"
bind_python: False

- name: "glu"
signature: "Tensor (Tensor input, Int64 dim=-1) => Glu"
bind_python: True

- name: "sigmoid"
signature: "Tensor (Tensor x) => Sigmoid"
bind_python: True
Expand Down
23 changes: 23 additions & 0 deletions oneflow/core/functional/impl/activation_functor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ limitations under the License.
#include "oneflow/core/functional/function_library.h"
#include "oneflow/core/functional/scalar.h"
#include "oneflow/core/autograd/autograd_mode.h"
#include "oneflow/core/functional/functional.h"

namespace oneflow {
namespace one {
Expand Down Expand Up @@ -164,6 +165,27 @@ class GeluGradFunctor : public BinaryFunctor {
}
};

class GluFunctor {
public:
GluFunctor() {}
Maybe<Tensor> operator()(const std::shared_ptr<one::Tensor>& input, int64_t dim) const {
auto ndim = input->ndim();
Copy link
Contributor

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";

CHECK_GT_OR_RETURN(ndim, 0) << "glu does not support 0-dimensional tensors";
CHECK_OR_RETURN(dim >= -ndim && dim < ndim)
<< ", Dimension out of range (expected to be in range of [" << -ndim << ", " << ndim - 1
<< "], but got " << dim << ")";
if (dim < 0) { dim += ndim; }
int64_t nc = input->dim(dim);
CHECK_EQ_OR_RETURN(nc % 2, 0) << "Halving dimension must be even, but dimension " << dim
<< " is size " << nc;
nc = nc / 2;
std::vector<int64_t> split_sizes(2, nc);
auto split_x = JUST(SplitWithSize(input, split_sizes, dim));
auto sgmd_x1 = JUST(Sigmoid(split_x->at(1)));
return Mul(split_x->at(0), sgmd_x1);
}
};

class HardSigmoidFunctor {
public:
HardSigmoidFunctor() {
Expand Down Expand Up @@ -322,6 +344,7 @@ ONEFLOW_FUNCTION_LIBRARY(m) {
m.add_functor<impl::EluGradFunctor>("EluGrad");
m.add_functor<impl::GeluFunctor>("Gelu");
m.add_functor<impl::GeluGradFunctor>("GeluGrad");
m.add_functor<impl::GluFunctor>("Glu");
m.add_functor<impl::HardSigmoidFunctor>("HardSigmoid");
m.add_functor<impl::HardSigmoidGradFunctor>("HardSigmoidGrad");
m.add_functor<impl::SoftmaxFunctor>("Softmax");
Expand Down
30 changes: 30 additions & 0 deletions python/oneflow/framework/docstr/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,33 @@
tensor([1.0507, 2.1014, 3.1521], dtype=oneflow.float32)
""",
)
add_docstr(
oneflow._C.glu,
"""
glu(input: Tensor, dim: int) -> Tensor

The equation 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
>>> x = flow.tensor([[1, 2, 3, 4], [5, 6, 7, 8]], dtype=flow.float32)
>>> y = nn.functional.glu(x)
>>> y
tensor([[0.9526, 1.9640],
[4.9954, 5.9980]], dtype=oneflow.float32)

See
:class:`~oneflow.nn.GLU` for more details.

""",
)
1 change: 1 addition & 0 deletions python/oneflow/nn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from oneflow.nn.modules.activation import (
ELU,
GELU,
GLU,
Hardsigmoid,
Hardswish,
Hardtanh,
Expand Down
1 change: 1 addition & 0 deletions python/oneflow/nn/functional/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from oneflow._C import sigmoid
from oneflow._C import prelu
from oneflow._C import gelu
from oneflow._C import glu
from oneflow._C import log_sigmoid as logsigmoid
from oneflow._C import log_sigmoid
from oneflow._C import softsign
Expand Down
44 changes: 44 additions & 0 deletions python/oneflow/nn/modules/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,50 @@ def forward(self, x):
return flow._C.softsign(x)


class GLU(Module):
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)

Copy link
Contributor

@Flowingsun007 Flowingsun007 Aug 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需要导出GLU的module:class GLU(Module) (pytorch源码里也导出了,可参考)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

函数逻辑可以注册op/kernel,并通过functor接口导出,flow.F.glu(xxx)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需要导出GLU的module:class GLU(Module) (pytorch源码里也导出了,可参考)

GLU已导出,在c++层使用算子拼接实现


if __name__ == "__main__":
import doctest

Expand Down
47 changes: 47 additions & 0 deletions python/oneflow/test/modules/test_glu.py
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
Copy link
Contributor

Choose a reason for hiding this comment

The 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()