reader.py 11.9 KB
Newer Older
Z
Zeyu Chen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# Copyright (c) 2020 PaddlePaddle 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 sys
import os
import io
import itertools
from functools import partial

import numpy as np
from paddle.io import BatchSampler, DataLoader, Dataset
L
liu zhengxi 已提交
23
import paddle.distributed as dist
Z
Zeyu Chen 已提交
24
from paddlenlp.data import Pad
25 26 27 28 29 30 31 32 33 34 35
from paddlenlp.datasets import WMT14ende
from paddlenlp.data.sampler import SamplerHelper


def min_max_filer(data, max_len, min_len=0):
    # 1 for special tokens.
    data_min_len = min(len(data[0]), len(data[1])) + 1
    data_max_len = max(len(data[0]), len(data[1])) + 1
    return (data_min_len >= min_len) and (data_max_len <= max_len)


36
def create_data_loader(args, places=None, use_all_vocab=False):
37
    root = None if args.root == "None" else args.root
38 39 40 41 42 43 44
    if not use_all_vocab:
        WMT14ende.VOCAB_INFO = (os.path.join(
            "WMT14.en-de", "wmt14_ende_data_bpe",
            "vocab_all.bpe.33712"), os.path.join(
                "WMT14.en-de", "wmt14_ende_data_bpe", "vocab_all.bpe.33712"),
                                "de485e3c2e17e23acf4b4b70b54682dd",
                                "de485e3c2e17e23acf4b4b70b54682dd")
45
    (src_vocab, trg_vocab) = WMT14ende.get_vocab(root=root)
46 47 48 49 50
    padding_vocab = (
        lambda x: (x + args.pad_factor - 1) // args.pad_factor * args.pad_factor
    )
    args.src_vocab_size = padding_vocab(len(src_vocab))
    args.trg_vocab_size = padding_vocab(len(trg_vocab))
51 52 53
    transform_func = WMT14ende.get_default_transform_func(root=root)
    datasets = [
        WMT14ende.get_datasets(
54 55
            mode=m, root=root, transform_func=transform_func)
        for m in ["train", "dev"]
56 57 58 59 60 61 62
    ]

    data_loaders = [(None)] * 2
    for i, dataset in enumerate(datasets):
        dataset = dataset.filter(
            partial(
                min_max_filer, max_len=args.max_length))
L
liu zhengxi 已提交
63 64
        batch_sampler = TransformerBatchSampler(
            dataset=dataset,
65
            batch_size=args.batch_size,
L
liu zhengxi 已提交
66 67 68 69 70 71 72 73
            pool_size=args.pool_size,
            sort_type=args.sort_type,
            shuffle=args.shuffle,
            shuffle_batch=args.shuffle_batch,
            use_token_batch=True,
            max_length=args.max_length,
            distribute_mode=True if i == 0 else False,
            world_size=dist.get_world_size(),
74 75 76
            rank=dist.get_rank(),
            pad_seq=args.pad_seq,
            bsz_multi=args.bsz_multi)
L
liu zhengxi 已提交
77

Z
Zeyu Chen 已提交
78 79
        data_loader = DataLoader(
            dataset=dataset,
L
Leo Chen 已提交
80
            places=places,
Z
Zeyu Chen 已提交
81 82 83 84 85
            batch_sampler=batch_sampler,
            collate_fn=partial(
                prepare_train_input,
                bos_idx=args.bos_idx,
                eos_idx=args.eos_idx,
86 87
                pad_idx=args.bos_idx,
                pad_seq=args.pad_seq),
L
Leo Chen 已提交
88
            num_workers=0)
89
        data_loaders[i] = (data_loader)
Z
Zeyu Chen 已提交
90 91 92
    return data_loaders


93
def create_infer_loader(args, use_all_vocab=False):
94
    root = None if args.root == "None" else args.root
95 96 97 98 99 100 101
    if not use_all_vocab:
        WMT14ende.VOCAB_INFO = (os.path.join(
            "WMT14.en-de", "wmt14_ende_data_bpe",
            "vocab_all.bpe.33712"), os.path.join(
                "WMT14.en-de", "wmt14_ende_data_bpe", "vocab_all.bpe.33712"),
                                "de485e3c2e17e23acf4b4b70b54682dd",
                                "de485e3c2e17e23acf4b4b70b54682dd")
102
    (src_vocab, trg_vocab) = WMT14ende.get_vocab(root=root)
103 104 105 106 107
    padding_vocab = (
        lambda x: (x + args.pad_factor - 1) // args.pad_factor * args.pad_factor
    )
    args.src_vocab_size = padding_vocab(len(src_vocab))
    args.trg_vocab_size = padding_vocab(len(trg_vocab))
108 109
    transform_func = WMT14ende.get_default_transform_func(root=root)
    dataset = WMT14ende.get_datasets(
110
        mode="test", root=root, transform_func=transform_func).filter(
111 112 113 114 115 116 117 118 119 120 121 122 123
            partial(
                min_max_filer, max_len=args.max_length))

    batch_sampler = SamplerHelper(dataset).batch(
        batch_size=args.infer_batch_size, drop_last=False)

    data_loader = DataLoader(
        dataset=dataset,
        batch_sampler=batch_sampler,
        collate_fn=partial(
            prepare_infer_input,
            bos_idx=args.bos_idx,
            eos_idx=args.eos_idx,
124 125
            pad_idx=args.bos_idx,
            pad_seq=args.pad_seq),
126 127 128 129 130
        num_workers=0,
        return_list=True)
    return data_loader, trg_vocab.to_tokens


131
def prepare_train_input(insts, bos_idx, eos_idx, pad_idx, pad_seq=1):
Z
Zeyu Chen 已提交
132 133 134 135
    """
    Put all padded data needed by training into a list.
    """
    word_pad = Pad(pad_idx)
136 137 138 139 140 141 142 143 144 145
    src_max_len = (
        max([len(inst[0]) for inst in insts]) + pad_seq) // pad_seq * pad_seq
    trg_max_len = (
        max([len(inst[1]) for inst in insts]) + pad_seq) // pad_seq * pad_seq
    src_word = word_pad([
        inst[0] + [eos_idx] + [pad_idx] * (src_max_len - 1 - len(inst[0]))
        for inst in insts
    ])
    trg_word = word_pad([[bos_idx] + inst[1] + [pad_idx] *
                         (trg_max_len - 1 - len(inst[1])) for inst in insts])
Z
Zeyu Chen 已提交
146
    lbl_word = np.expand_dims(
147 148 149 150 151
        word_pad([
            inst[1] + [eos_idx] + [pad_idx] * (trg_max_len - 1 - len(inst[1]))
            for inst in insts
        ]),
        axis=2)
Z
Zeyu Chen 已提交
152 153 154 155 156 157

    data_inputs = [src_word, trg_word, lbl_word]

    return data_inputs


158
def prepare_infer_input(insts, bos_idx, eos_idx, pad_idx, pad_seq=1):
Z
Zeyu Chen 已提交
159 160 161 162
    """
    Put all padded data needed by beam search decoder into a list.
    """
    word_pad = Pad(pad_idx)
163 164 165 166 167 168
    src_max_len = (
        max([len(inst[0]) for inst in insts]) + pad_seq) // pad_seq * pad_seq
    src_word = word_pad([
        inst[0] + [eos_idx] + [pad_idx] * (src_max_len - 1 - len(inst[0]))
        for inst in insts
    ])
Z
Zeyu Chen 已提交
169 170 171 172 173 174 175 176

    return [src_word, ]


class SortType(object):
    GLOBAL = 'global'
    POOL = 'pool'
    NONE = "none"
L
liu zhengxi 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192


class SentenceBatchCreator(object):
    def __init__(self, batch_size):
        self.batch = []
        self._batch_size = batch_size

    def append(self, info):
        self.batch.append(info)
        if len(self.batch) == self._batch_size:
            tmp = self.batch
            self.batch = []
            return tmp


class TokenBatchCreator(object):
193
    def __init__(self, batch_size, bsz_multi=1):
L
liu zhengxi 已提交
194 195 196
        self._batch = []
        self.max_len = -1
        self._batch_size = batch_size
197
        self._bsz_multi = bsz_multi
L
liu zhengxi 已提交
198 199 200 201 202

    def append(self, info):
        cur_len = info.max_len
        max_len = max(self.max_len, cur_len)
        if max_len * (len(self._batch) + 1) > self._batch_size:
203 204 205 206 207 208 209 210
            # Make sure the batch size won't be empty. 
            mode_len = max(
                len(self._batch) // self._bsz_multi * self._bsz_multi,
                len(self._batch) % self._bsz_multi)
            result = self._batch[:mode_len]
            self._batch = self._batch[mode_len:]
            self._batch.append(info)
            self.max_len = max([b.max_len for b in self._batch])
L
liu zhengxi 已提交
211 212 213 214 215 216 217 218 219 220 221
            return result
        else:
            self.max_len = max_len
            self._batch.append(info)

    @property
    def batch(self):
        return self._batch


class SampleInfo(object):
222
    def __init__(self, i, lens, pad_seq=1):
L
liu zhengxi 已提交
223 224
        self.i = i
        # Take bos and eos into account
225 226 227
        self.min_len = min(lens[0], lens[1]) + 1
        self.max_len = (max(lens[0], lens[1]) + pad_seq) // pad_seq * pad_seq
        self.seq_max_len = max(lens[0], lens[1]) + 1
L
liu zhengxi 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
        self.src_len = lens[0] + 1
        self.trg_len = lens[1] + 1


class TransformerBatchSampler(BatchSampler):
    def __init__(self,
                 dataset,
                 batch_size,
                 pool_size=10000,
                 sort_type=SortType.NONE,
                 min_length=0,
                 max_length=100,
                 shuffle=False,
                 shuffle_batch=False,
                 use_token_batch=False,
                 clip_last_batch=False,
                 distribute_mode=True,
                 seed=0,
                 world_size=1,
247 248 249
                 rank=0,
                 pad_seq=1,
                 bsz_multi=8):
L
liu zhengxi 已提交
250 251 252 253 254 255 256 257 258 259 260 261
        for arg, value in locals().items():
            if arg != "self":
                setattr(self, "_" + arg, value)
        self._random = np.random
        self._random.seed(seed)
        # for multi-devices
        self._distribute_mode = distribute_mode
        self._nranks = world_size
        self._local_rank = rank
        self._sample_infos = []
        for i, data in enumerate(self._dataset):
            lens = [len(data[0]), len(data[1])]
262
            self._sample_infos.append(SampleInfo(i, lens, self._pad_seq))
L
liu zhengxi 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282

    def __iter__(self):
        # global sort or global shuffle
        if self._sort_type == SortType.GLOBAL:
            infos = sorted(self._sample_infos, key=lambda x: x.trg_len)
            infos = sorted(infos, key=lambda x: x.src_len)
        else:
            if self._shuffle:
                infos = self._sample_infos
                self._random.shuffle(infos)
            else:
                infos = self._sample_infos

            if self._sort_type == SortType.POOL:
                reverse = True
                for i in range(0, len(infos), self._pool_size):
                    # To avoid placing short next to long sentences
                    reverse = not reverse
                    infos[i:i + self._pool_size] = sorted(
                        infos[i:i + self._pool_size],
283
                        key=lambda x: x.seq_max_len,
L
liu zhengxi 已提交
284 285 286 287
                        reverse=reverse)

        batches = []
        batch_creator = TokenBatchCreator(
288 289
            self._batch_size,
            self._bsz_multi) if self._use_token_batch else SentenceBatchCreator(
L
liu zhengxi 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
                self._batch_size * self._nranks)

        for info in infos:
            batch = batch_creator.append(info)
            if batch is not None:
                batches.append(batch)

        if not self._clip_last_batch and len(batch_creator.batch) != 0:
            batches.append(batch_creator.batch)

        if self._shuffle_batch:
            self._random.shuffle(batches)

        if not self._use_token_batch:
            # When producing batches according to sequence number, to confirm
            # neighbor batches which would be feed and run parallel have similar
            # length (thus similar computational cost) after shuffle, we as take
            # them as a whole when shuffling and split here
            batches = [[
                batch[self._batch_size * i:self._batch_size * (i + 1)]
                for i in range(self._nranks)
            ] for batch in batches]
            batches = list(itertools.chain.from_iterable(batches))
        self.batch_number = (len(batches) + self._nranks - 1) // self._nranks

        # for multi-device
        for batch_id, batch in enumerate(batches):
            if not self._distribute_mode or (
                    batch_id % self._nranks == self._local_rank):
                batch_indices = [info.i for info in batch]
                yield batch_indices
        if self._distribute_mode and len(batches) % self._nranks != 0:
            if self._local_rank >= len(batches) % self._nranks:
                # use previous data to pad
                yield batch_indices

    def __len__(self):
        if hasattr(self, "batch_number"):  #
            return self.batch_number
        if not self._use_token_batch:
            batch_number = (
                len(self._dataset) + self._batch_size * self._nranks - 1) // (
                    self._batch_size * self._nranks)
        else:
            # For uncertain batch number, the actual value is self.batch_number
            batch_number = sys.maxsize
        return batch_number