Spaces:
Sleeping
Sleeping
mohdelgaar
commited on
Commit
•
9f22f23
1
Parent(s):
435bd3d
add missing file
Browse files
utils.py
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import T5Tokenizer, T5EncoderModel
|
2 |
+
import torch
|
3 |
+
def sequence_mask(lengths, max_len=None):
|
4 |
+
"""
|
5 |
+
Creates a boolean mask from sequence lengths.
|
6 |
+
:param lengths: 1d tensor [batch_size]
|
7 |
+
:param max_len: int
|
8 |
+
"""
|
9 |
+
batch_size = lengths.numel()
|
10 |
+
max_len = max_len or lengths.max()
|
11 |
+
return (torch.arange(0, max_len, device=lengths.device)
|
12 |
+
.type_as(lengths)
|
13 |
+
.repeat(batch_size, 1)
|
14 |
+
.lt(lengths.unsqueeze(1))
|
15 |
+
.long())
|
16 |
+
|