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 longformer tokenizer #502

Merged
merged 48 commits into from
May 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
098a0ed
裘博航创建文件
Yicorner Mar 8, 2023
4a679fb
裘博航———创建要求文件
Yicorner Mar 8, 2023
947c987
Merge branch 'mindspore-lab:master' into master
Yicorner Mar 8, 2023
8dc7cd7
Merge branch 'mindspore-lab:master' into master
Yicorner Mar 10, 2023
b119ecf
create longformer model
Yicorner Mar 10, 2023
4570cd2
Merge branch 'mindspore-lab:master' into master
Yicorner Mar 12, 2023
a94338b
Merge branch 'mindspore-lab:master' into master
Yicorner Mar 13, 2023
9220659
add longformer embedding
Yicorner Mar 13, 2023
a5f46e9
Add Longformer Embedding model
Yicorner Mar 13, 2023
fe5dd04
add longformer embedding
Yicorner Mar 13, 2023
26f2c25
Merge branch 'mindspore-lab:master' into master
Yicorner Mar 14, 2023
ae55a61
add cumsum to _legacy/functional and longformer_embedding
Yicorner Mar 14, 2023
d7a2aa0
add cumsum to _legacy/functional and longformer_embedding
Yicorner Mar 14, 2023
90de573
Merge branch 'mindspore-lab:master' into master
Yicorner Mar 14, 2023
96384c2
Merge branch 'mindspore-lab:master' into master
Yicorner Mar 15, 2023
7e32218
add cumsum to functional && embedding class to longformer
Yicorner Mar 15, 2023
9cd93df
modify accroding to review
Yicorner Mar 16, 2023
eb04d89
modify accroding to review
Yicorner Mar 16, 2023
fe97d3f
Merge branch 'mindspore-lab:master' into master
Yicorner Mar 21, 2023
2aacf1b
Merge branch 'mindspore-lab:master' into master
Yicorner Mar 30, 2023
d86bbce
add longformer selfAttention class
Yicorner Mar 30, 2023
b41b35f
Merge branch 'mindspore-lab:master' into master
Yicorner Apr 5, 2023
d6b8d8a
selfoutput
Yicorner Mar 30, 2023
91c3d16
attention
Yicorner Mar 30, 2023
55b1227
Intermediate
Yicorner Mar 30, 2023
6489fd1
OutPut
Yicorner Mar 30, 2023
9842fb2
Layer
Yicorner Mar 31, 2023
0d14290
Encoder
Yicorner Mar 31, 2023
12e8f7c
Pooler
Yicorner Mar 31, 2023
2653594
LMHead
Yicorner Mar 31, 2023
690d020
LongformerModel
Yicorner Mar 31, 2023
d6aeb52
LongformerForMaskedLM
Yicorner Apr 1, 2023
de5c26b
LongformerForSequenceClassification
Yicorner Apr 1, 2023
276d29c
addtestLongformerClassificationHead
Yicorner Apr 1, 2023
3181848
LongformerForQuestionAnswering
Yicorner Apr 1, 2023
297678c
LongformerForTokenClassification
Yicorner Apr 1, 2023
f60590b
LongformerForMultipleChoice
Yicorner Apr 1, 2023
efe69eb
pylint OK
Yicorner Apr 1, 2023
e8c6524
pylint OK
Yicorner Apr 1, 2023
7922953
Merge branch 'master' of https://github.com/Yicorner/mindnlp
Yicorner Apr 5, 2023
773a20f
Merge branch 'master' of https://github.com/Yicorner/mindnlp
Yicorner Apr 5, 2023
cdc9352
Merge branch 'master' of https://github.com/Yicorner/mindnlp
Yicorner Apr 5, 2023
6eba36c
Merge branch 'mindspore-lab:master' into master
Yicorner Apr 17, 2023
8dbe625
Merge branch 'mindspore-lab:master' into master
Yicorner May 13, 2023
20986eb
addtokenizer
Yicorner May 13, 2023
25d1130
addtokenizer2
Yicorner May 13, 2023
063f08e
add longformer tokenizer
Yicorner May 13, 2023
eb6c494
add longformer tokenizer2
Yicorner May 13, 2023
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
Prev Previous commit
Next Next commit
add cumsum to _legacy/functional and longformer_embedding
  • Loading branch information
Yicorner committed Mar 14, 2023
commit ae55a61f2cb1d99f42ee1ad0fa55c02e807e0a25
56 changes: 56 additions & 0 deletions mindnlp/_legacy/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
from mindspore.common import dtype as mstype
from mindspore.ops._primitive_cache import _get_cache_prim
from packaging import version
from mindspore.ops import operations as P

MS_COMPATIBLE_VERSION = '1.10.1'

cast_ = ops.Cast()
scalar_to_tensor_ = ops.ScalarToTensor()
cumsum_ = P.CumSum()

def kl_div(inputs, target, reduction='none', log_target=False):
"""KLDiv function."""
Expand Down Expand Up @@ -642,3 +644,57 @@ def arange(start=0, end=None, step=1, *, dtype=None):
if dtype is not None:
data = cast_(data, dtype)
return data

def cumsum(x, axis, dtype=None):
"""
Computes the cumulative sum of input Tensor along `axis`.

.. math::

y_i = x_1 + x_2 + x_3 + ... + x_i

Note:
On Ascend, the dtype of `x` only support :int8, uint8, int32, float16 or float32 in case of static shape.
For the case of dynamic shape, the dtype of `x` only support int32, float16 or float32.

Args:
x (Tensor): The input Tensor to accumulate.
axis (int): Axis along which the cumulative sum is computed.
dtype (:class:`mindspore.dtype`, optional): The desired dtype of returned Tensor. If specified,
the input Tensor will be cast to `dtype` before the computation. This is useful for preventing overflows.
If not specified, stay the same as original Tensor. Default: None.

Returns:
Tensor, the shape of the output Tensor is consistent with the input Tensor's.

Raises:
TypeError: If `x` is not a Tensor.
ValueError: If the axis is out of range.

Supported Platforms:
``Ascend`` ``GPU`` ``CPU``

Examples:
>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor
>>> import mindspore.ops as ops
>>> x = Tensor(np.array([[3, 4, 6, 10], [1, 6, 7, 9], [4, 3, 8, 7], [1, 3, 7, 9]]).astype(np.float32))
>>> # case 1: along the axis 0
>>> y = ops.cumsum(x, 0)
>>> print(y)
[[ 3. 4. 6. 10.]
[ 4. 10. 13. 19.]
[ 8. 13. 21. 26.]
[ 9. 16. 28. 35.]]
>>> # case 2: along the axis 1
>>> y = ops.cumsum(x, 1)
>>> print(y)
[[ 3. 7. 13. 23.]
[ 1. 7. 14. 23.]
[ 4. 7. 15. 22.]
[ 1. 4. 11. 20.]]
"""
if dtype is not None and x.dtype != dtype:
x = x.astype(dtype, copy=False)
return cumsum_(x, axis)
4 changes: 2 additions & 2 deletions mindnlp/models/longformer/longformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# pylint:
import mindspore
from mindspore import nn

from ..._legacy import functional
activation_map = {
'relu': nn.ReLU(),
'gelu': nn.GELU(False),
Expand Down Expand Up @@ -83,7 +83,7 @@ def create_position_ids_from_input_ids(input_ids, padding_idx):
"""
# The series of casts and type-conversions here are carefully balanced to both work with ONNX export and XLA.
mask = mindspore.ops.not_equal(input_ids, padding_idx).astype(mindspore.int32)
incremental_indices = mindspore.ops.cumsum(mask, axis=1, dtype=mask.dtype) * mask
incremental_indices = functional.cumsum(mask, axis=1, dtype=mask.dtype) * mask
return incremental_indices.long() + padding_idx


Expand Down