Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
6ccad39
Add layout support
sxjscience Jul 8, 2020
2a5bae7
fix test
sxjscience Jul 8, 2020
326630e
Update transformer.py
sxjscience Jul 9, 2020
7a61c23
Update transformer.py
sxjscience Jul 9, 2020
e94d6e9
Update README.md
sxjscience Jul 9, 2020
3caba81
Merge remote-tracking branch 'upstream/numpy' into backbone_layout
sxjscience Jul 10, 2020
1801807
try to add set_layout
sxjscience Jul 11, 2020
55885bb
update test case
sxjscience Jul 11, 2020
e3dce2f
fix
sxjscience Jul 11, 2020
03d6fd7
update
sxjscience Jul 13, 2020
d030d34
update
sxjscience Jul 13, 2020
695c1dc
update
sxjscience Jul 13, 2020
1f252a5
Update bert.py
sxjscience Jul 13, 2020
334db84
fix bug
sxjscience Jul 13, 2020
84008a4
update
sxjscience Jul 13, 2020
c66c82c
Update test_models_bert.py
sxjscience Jul 13, 2020
531f4cd
Merge remote-tracking branch 'upstream/numpy' into backbone_layout
sxjscience Jul 13, 2020
8d89c65
Update tokenizers.py
sxjscience Jul 13, 2020
f25708a
add compute layout
sxjscience Jul 13, 2020
b75ff17
Update xlmr.py
sxjscience Jul 13, 2020
5c5a278
Update test_models_bert.py
sxjscience Jul 13, 2020
6b5814a
revise test cases
sxjscience Jul 13, 2020
d4f0a1f
Update layers.py
sxjscience Jul 14, 2020
add993c
move jieba to try import
sxjscience Jul 14, 2020
b16592f
Merge remote-tracking branch 'upstream/numpy' into backbone_layout
sxjscience Jul 25, 2020
5a93090
fix
sxjscience Jul 26, 2020
9713fd8
Update transformer.py
sxjscience Jul 26, 2020
d7b1416
fix
sxjscience Jul 26, 2020
d19d4c1
Update bert.py
sxjscience Jul 27, 2020
5fe094d
Update setup.py
sxjscience Jul 27, 2020
b484897
Update test_models_bert.py
sxjscience Jul 27, 2020
05958bd
Update test_models_bert.py
sxjscience Jul 27, 2020
d4d4b70
fix
sxjscience Jul 27, 2020
b22e490
update
sxjscience Jul 27, 2020
ee4b19b
Revise
sxjscience Jul 27, 2020
98c7549
Update electra.py
sxjscience Jul 27, 2020
f5a4a08
Update electra.py
sxjscience Jul 27, 2020
90025e1
Update test_models_electra.py
sxjscience Jul 27, 2020
cab2c5b
fix
sxjscience Jul 27, 2020
461a138
fix bug
sxjscience Jul 27, 2020
54f136a
Update test_models_albert.py
sxjscience Jul 27, 2020
c503d12
add more testcases
sxjscience Jul 27, 2020
3624af8
fix
sxjscience Jul 27, 2020
8530e08
Update albert.py
sxjscience Jul 27, 2020
a076b7a
Update albert.py
sxjscience Jul 27, 2020
cc34106
fix bug
sxjscience Jul 27, 2020
b3a823a
fix testcase
sxjscience Jul 27, 2020
dd2b83d
Update test_models_electra.py
sxjscience Jul 27, 2020
cb38d75
Update bert.py
sxjscience Jul 27, 2020
9ddec18
update
sxjscience Jul 27, 2020
fc458df
Update test_models_electra.py
sxjscience Jul 27, 2020
68e50eb
Update mobilebert.py
sxjscience Jul 27, 2020
a23a17d
Update mobilebert.py
sxjscience Jul 27, 2020
b8f0e0d
update mobilebert
sxjscience Jul 27, 2020
8e2a253
Update test_models_mobilebert.py
sxjscience Jul 27, 2020
16156ed
Update mobilebert.py
sxjscience Jul 27, 2020
a2c36bc
fix bug
sxjscience Jul 28, 2020
f3ca027
Update roberta.py
sxjscience Jul 28, 2020
612ef47
fix roberta
sxjscience Jul 28, 2020
e330b11
update
sxjscience Jul 28, 2020
bf9074c
update
sxjscience Jul 28, 2020
c79b53d
fix import
sxjscience Jul 28, 2020
6f6fb46
fix bug
sxjscience Jul 28, 2020
d681f5c
update
sxjscience Jul 28, 2020
ad22e0d
reduce test workloads
sxjscience Jul 28, 2020
8d9059a
address comment
sxjscience Jul 28, 2020
e4f8f10
address comment
sxjscience Jul 28, 2020
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ First of all, install the latest MXNet. You may use the following commands:
```bash

# Install the version with CUDA 10.1
pip install -U --pre mxnet-cu101>=2.0.0b20200604 -f https://dist.mxnet.io/python
pip install -U --pre "mxnet-cu101>=2.0.0b20200604,<=2.0.0b20200619" -f https://dist.mxnet.io/python
Comment thread
sxjscience marked this conversation as resolved.
Outdated

# Install the cpu-only version
pip install -U --pre mxnet>=2.0.0b20200604 -f https://dist.mxnet.io/python
pip install -U --pre "mxnet>=2.0.0b20200604,<=2.0.0b20200619" -f https://dist.mxnet.io/python
```


Expand Down
3 changes: 1 addition & 2 deletions scripts/conversion_toolkits/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ Notice: pleas set up the `--electra_path` with the cloned path or get this elect

```bash
# Need to use TF 1.13.2 to use contrib layer
pip uninstall tensorflow
pip install tensorflow==1.13.2
pip install tensorflow==1.13.2 --upgrade --force-reinstall

# Actual conversion
bash convert_electra.sh
Expand Down
79 changes: 59 additions & 20 deletions src/gluonnlp/attention_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
def gen_self_attn_mask(F, data,
valid_length=None,
dtype: type = np.float32,
attn_type: str = 'full'):
attn_type: str = 'full',
layout: str = 'NT'):
"""Generate the mask used for the encoder, i.e, self-attention.

In our implementation, 1 --> not masked, 0 --> masked
Expand Down Expand Up @@ -99,38 +100,50 @@ def gen_self_attn_mask(F, data,

Parameters
----------
F :
data :
The data. Shape (batch_size, seq_length, C)
valid_length :
F
data
The data.
- layout = 'NT'
Shape (batch_size, seq_length, C)
- layout = 'TN'
Shape (seq_length, batch_size, C)
valid_length
Shape (batch_size,)
dtype
Data type of the mask
attn_type : str
attn_type
Can be 'full' or 'causal'
layout
The layout of the data

Returns
-------
mask
Shape (batch_size, seq_length, seq_length)
"""
if layout == 'NT':
batch_axis, time_axis = 0, 1
elif layout == 'TN':
batch_axis, time_axis = 1, 0
else:
raise NotImplementedError('Unsupported layout={}'.format(layout))
if attn_type == 'full':
if valid_length is not None:
valid_length = valid_length.astype(dtype)
steps = F.npx.arange_like(data, axis=1) # (seq_length,)
steps = F.npx.arange_like(data, axis=time_axis) # (seq_length,)
mask1 = (F.npx.reshape(steps, (1, 1, -1))
< F.npx.reshape(valid_length, (-2, 1, 1)))
mask2 = (F.npx.reshape(steps, (1, -1, 1))
< F.npx.reshape(valid_length, (-2, 1, 1)))
mask = mask1 * mask2
else:
# TODO(sxjscience) optimize
seq_len_ones = F.np.ones_like(F.npx.arange_like(data, axis=1)) # (seq_length,)
batch_ones = F.np.ones_like(F.npx.arange_like(data, axis=0)) # (batch_size,)
seq_len_ones = F.np.ones_like(F.npx.arange_like(data, axis=time_axis)) # (seq_length,)
batch_ones = F.np.ones_like(F.npx.arange_like(data, axis=batch_axis)) # (batch_size,)
mask = batch_ones.reshape((-1, 1, 1)) * seq_len_ones.reshape((1, -1, 1))\
* seq_len_ones.reshape((1, 1, -1))
elif attn_type == 'causal':
steps = F.npx.arange_like(data, axis=1)
steps = F.npx.arange_like(data, axis=time_axis)
# mask: (seq_length, seq_length)
# batch_mask: (batch_size, seq_length)
mask = (F.np.expand_dims(steps, axis=0) <= F.np.expand_dims(steps, axis=1)).astype(dtype)
Expand All @@ -139,15 +152,17 @@ def gen_self_attn_mask(F, data,
batch_mask = (F.np.expand_dims(steps, axis=0) < F.np.expand_dims(valid_length, axis=-1)).astype(dtype)
mask = mask * F.np.expand_dims(batch_mask, axis=-1)
else:
batch_ones = F.np.ones_like(F.npx.arange_like(data, axis=0), dtype=np.float32) # (batch_size,)
batch_ones = F.np.ones_like(F.npx.arange_like(data, axis=batch_axis),
dtype=np.float32) # (batch_size,)
Comment thread
sxjscience marked this conversation as resolved.
Outdated
mask = mask * batch_ones.reshape((-1, 1, 1))
else:
raise NotImplementedError
mask = mask.astype(dtype)
return mask


def gen_mem_attn_mask(F, mem, mem_valid_length, data, data_valid_length=None, dtype=np.float32):
def gen_mem_attn_mask(F, mem, mem_valid_length, data, data_valid_length=None,
dtype=np.float32, layout: str = 'NT'):
"""Generate the mask used for the decoder. All query slots are attended to the memory slots.

In our implementation, 1 --> not masked, 0 --> masked
Expand Down Expand Up @@ -182,34 +197,48 @@ def gen_mem_attn_mask(F, mem, mem_valid_length, data, data_valid_length=None, dt
Parameters
----------
F :
mem :
Shape (batch_size, mem_length, C_mem)
mem
- layout = 'NT'
Shape (batch_size, mem_length, C_mem)
- layout = 'TN'
Shape (mem_length, batch_size, C_mem)
mem_valid_length :
Shape (batch_size,)
data :
Shape (batch_size, query_length, C_data)
data
- layout = 'NT'
Shape (batch_size, query_length, C_data)
- layout = 'TN'
Shape (query_length, batch_size, C_data)
data_valid_length :
Shape (batch_size,)
dtype : type
dtype
Data type of the mask
layout
Layout of the data + mem tensor

Returns
-------
mask :
Shape (batch_size, query_length, mem_length)
"""
if layout == 'NT':
batch_axis, time_axis = 0, 1
elif layout == 'TN':
batch_axis, time_axis = 1, 0
else:
raise NotImplementedError('Unsupported layout={}'.format(layout))
mem_valid_length = mem_valid_length.astype(dtype)
mem_steps = F.npx.arange_like(mem, axis=1) # (mem_length,)
mem_steps = F.npx.arange_like(mem, axis=time_axis) # (mem_length,)
data_steps = F.npx.arange_like(data, axis=time_axis) # (query_length,)
mem_mask = (F.npx.reshape(mem_steps, (1, 1, -1))
< F.npx.reshape(mem_valid_length, (-2, 1, 1))).astype(dtype) # (B, 1, mem_length)
if data_valid_length is not None:
data_valid_length = data_valid_length.astype(dtype)
data_steps = F.npx.arange_like(data, axis=1) # (query_length,)
data_mask = (F.npx.reshape(data_steps, (1, -1, 1))
< F.npx.reshape(data_valid_length, (-2, 1, 1))).astype(dtype) # (B, query_length, 1)
mask = mem_mask * data_mask
else:
query_length_ones = F.np.ones_like(F.npx.arange_like(data, axis=1)) # (query_length,)
query_length_ones = F.np.ones_like(data_steps)
mask = query_length_ones.reshape((1, -1, 1)) * mem_mask
return mask

Expand Down Expand Up @@ -611,6 +640,7 @@ def __init__(self, query_units=None, num_heads=None, attention_dropout=0.0,
self._normalized = normalized
self._eps = eps
self._dtype = dtype
assert layout in ['NTK', 'NKT', 'TNK']
self._layout = layout
self._use_einsum = use_einsum
if self._query_units is not None:
Expand All @@ -621,6 +651,10 @@ def __init__(self, query_units=None, num_heads=None, attention_dropout=0.0,
else:
self._query_head_units = None

@property
def layout(self):
return self._layout

def hybrid_forward(self, F, query, key, value, mask=None, edge_scores=None):
return multi_head_dot_attn(F, query=query, key=key, value=value,
mask=mask, edge_scores=edge_scores,
Expand Down Expand Up @@ -788,6 +822,11 @@ def __init__(self, query_units,
else:
raise NotImplementedError('method="{}" is currently not supported!'.format(method))

@property
def layout(self) -> str:
"""Layout of the cell"""
return self._layout

def hybrid_forward(self, F, rel_positions, query=None):
"""

Expand Down
6 changes: 3 additions & 3 deletions src/gluonnlp/data/tokenizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,18 +382,18 @@ def __init__(self, lang: Optional[str] = 'en', model: Optional[str] = None,
model = 'xx_ent_wiki_sm'
try:
self._nlp = spacy.load(model, disable=['parser', 'tagger', 'ner'])
except IOError:
except Exception as loading_err:
from spacy.cli import download
try:
download(model, False, '--user')
self._nlp = spacy.load(model, disable=['parser', 'tagger', 'ner'])
except:
except Exception as download_err:
print('SpaCy Model for the specified model="{model}" has not been '
'successfully loaded. You need to check the installation guide in '
'https://spacy.io/usage/models. Usually, the installation command '
'should be `python -m spacy download {model}`.\n'
'Compete Error Message: {err_msg}'.format(model=model,
err_msg=sys.exc_info()[0]))
err_msg=str(download_err)))
raise

def encode(self, sentences, output_type=str):
Expand Down
2 changes: 1 addition & 1 deletion src/gluonnlp/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def get_backbone(model_name: str,
model_name
The name of the pretrained model
root
The
Downloaded directory of the model zoo

Returns
-------
Expand Down
Loading