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

Update dataloader.py and dataset.py #305

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
45 changes: 29 additions & 16 deletions uer/utils/dataloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,27 @@ def __iter__(self):
masked_words_num = 0

for ins in instances:
src_single, pad_num = ins[0]
for _ in range(pad_num):
src_single.append(self.vocab.get(PAD_TOKEN))

if len(ins) == 4:
src.append(ins[0])
src.append(src_single)
masked_words_num += len(ins[1])
tgt_mlm.append([0] * len(ins[0]))
tgt_mlm.append([0] * len(src_single))
for mask in ins[1]:
tgt_mlm[-1][mask[0]] = mask[1]
is_next.append(ins[2])
seg.append([1] * ins[3][0] + [2] * (ins[3][1] - ins[3][0]) + [0] * (len(ins[0]) - ins[3][1]))
seg.append([1] * ins[3][0] + [2] * (ins[3][1] - ins[3][0]) + [0] * (pad_num))
else:
src_single, tgt_mlm_single = mask_seq(ins[0], self.tokenizer, self.whole_word_masking, self.span_masking, self.span_geo_prob, self.span_max_length)
tgt_mlm.append([0] * len(src_single))
src_single, tgt_mlm_single = mask_seq(src_single, self.tokenizer, self.whole_word_masking, self.span_masking, self.span_geo_prob, self.span_max_length)
masked_words_num += len(tgt_mlm_single)
src.append(src_single)
tgt_mlm.append([0] * len(ins[0]))
for mask in tgt_mlm_single:
tgt_mlm[-1][mask[0]] = mask[1]
is_next.append(ins[1])
seg.append([1] * ins[2][0] + [2] * (ins[2][1] - ins[2][0]) + [0] * (len(ins[0]) - ins[2][1]))
seg.append([1] * ins[2][0] + [2] * (ins[2][1] - ins[2][0]) + [0] * (pad_num))

if masked_words_num == 0:
continue
Expand Down Expand Up @@ -118,21 +122,25 @@ def __iter__(self):
masked_words_num = 0

for ins in instances:
src_single, pad_num = ins[0]
for _ in range(pad_num):
src_single.append(self.vocab.get(PAD_TOKEN))

if len(ins) == 3:
src.append(ins[0])
src.append(src_single)
masked_words_num += len(ins[1])
tgt.append([0] * len(ins[0]))
tgt.append([0] * len(src_single))
for mask in ins[1]:
tgt[-1][mask[0]] = mask[1]
seg.append([1] * ins[2][0] + [0] * (len(ins[0]) - ins[2][0]))
seg.append([1] * ins[2][0] + [0] * (pad_num))
else:
src_single, tgt_single = mask_seq(ins[0], self.tokenizer, self.whole_word_masking, self.span_masking, self.span_geo_prob, self.span_max_length)
tgt.append([0] * len(src_single))
src_single, tgt_single = mask_seq(src_single, self.tokenizer, self.whole_word_masking, self.span_masking, self.span_geo_prob, self.span_max_length)
masked_words_num += len(tgt_single)
src.append(src_single)
tgt.append([0] * len(ins[0]))
for mask in tgt_single:
tgt[-1][mask[0]] = mask[1]
seg.append([1] * ins[1][0] + [0] * (len(ins[0]) - ins[1][0]))
seg.append([1] * ins[1][0] + [0] * (pad_num))

if masked_words_num == 0:
continue
Expand All @@ -142,6 +150,7 @@ def __iter__(self):
torch.LongTensor(seg)



class AlbertDataloader(BertDataloader):
'''
AlbertDataloader can reuse the code of BertDataloader.
Expand All @@ -166,12 +175,16 @@ def __iter__(self):
seg = []

for ins in instances:
src.append(ins[0][:-1])
tgt.append(ins[0][1:])
if ins[1] == len(ins[0]):
src_single, pad_num = ins[0]
if ins[1] == len(src_single):
seg.append([1] * (ins[1] - 1))
else:
seg.append([1] * ins[1] + [0] * (len(ins[0]) - 1 - ins[1]))
for _ in range(pad_num):
src_single.append(self.vocab.get(PAD_TOKEN))
seg.append([1] * ins[1] + [0] * (pad_num - 1))

src.append(src_single[:-1])
tgt.append(src_single[1:])

yield torch.LongTensor(src), \
torch.LongTensor(tgt), \
Expand Down
22 changes: 15 additions & 7 deletions uer/utils/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,16 @@ def create_ins_from_doc(self, all_documents, document_index):
src.append(self.vocab.get(SEP_TOKEN))
seg_pos.append(len(src))

while len(src) != self.seq_length:
src.append(self.vocab.get(PAD_TOKEN))

pad_num = 0
if len(src) != self.seq_length:
pad_num = self.seq_length - len(src)

if not self.dynamic_masking:
src, tgt_mlm = mask_seq(src, self.tokenizer, self.whole_word_masking, self.span_masking, self.span_geo_prob, self.span_max_length)
src = (src, pad_num)
instance = (src, tgt_mlm, is_random_next, seg_pos)
else:
src = (src, pad_num)
instance = (src, is_random_next, seg_pos)

instances.append(instance)
Expand Down Expand Up @@ -290,22 +293,26 @@ def build_instances(self, all_documents):

if not self.dynamic_masking:
src, tgt = mask_seq(src, self.tokenizer, self.whole_word_masking, self.span_masking, self.span_geo_prob, self.span_max_length)
src = (src, 0)
instance = (src, tgt, seg_pos)
else:
src = (src, 0)
instance = (src, seg_pos)

instances.append(instance)

src = all_documents[instances_num * self.seq_length:]
seg_pos = [len(src)]

while len(src) != self.seq_length:
src.append(self.vocab.get(PAD_TOKEN))
if len(src) != self.seq_length:
pad_num = self.seq_length - len(src)

if not self.dynamic_masking:
src, tgt = mask_seq(src, self.tokenizer, self.whole_word_masking, self.span_masking, self.span_geo_prob, self.span_max_length)
src = (src, pad_num)
instance = (src, tgt, seg_pos)
else:
src = (src, pad_num)
instance = (src, seg_pos)

instances.append(instance)
Expand Down Expand Up @@ -442,13 +449,14 @@ def worker(self, proc_id, start, end):
for i in range(instances_num):
src = document[i * (self.seq_length + 1): (i + 1) * (self.seq_length + 1)]
seg_pos = self.seq_length
src = (src, 0)
pickle.dump((src, seg_pos), dataset_writer)

src = document[instances_num * (self.seq_length + 1):]
if len(src) > 0:
seg_pos = len(src)
while len(src) != self.seq_length + 1:
src.append(self.vocab.get(PAD_TOKEN))
pad_num = self.seq_length - len(src) + 1
src = (src, pad_num)
pickle.dump((src, seg_pos), dataset_writer)

if pos >= end:
Expand Down