wmt_dataloader_test.py 4.87 KB
Newer Older
Hongkun Yu's avatar
Hongkun Yu committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Lint as: python3
# Copyright 2020 The TensorFlow 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.
# ==============================================================================
"""Tests for official.nlp.data.wmt_dataloader."""
import os
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
18
from absl.testing import parameterized
Hongkun Yu's avatar
Hongkun Yu committed
19
20
21

import tensorflow as tf

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
22
from sentencepiece import SentencePieceTrainer
Hongkun Yu's avatar
Hongkun Yu committed
23
24
25
from official.nlp.data import wmt_dataloader


A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
26
27
28
29
def _generate_line_file(filepath, lines):
  with tf.io.gfile.GFile(filepath, 'w') as f:
    for l in lines:
      f.write('{}\n'.format(l))
Hongkun Yu's avatar
Hongkun Yu committed
30
31


A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def _generate_record_file(filepath, src_lines, tgt_lines, unique_id=False):
  writer = tf.io.TFRecordWriter(filepath)
  for i, (src, tgt) in enumerate(zip(src_lines, tgt_lines)):
    features = {
        'en': tf.train.Feature(
            bytes_list=tf.train.BytesList(
                value=[src.encode()])),
        'reverse_en': tf.train.Feature(
            bytes_list=tf.train.BytesList(
                value=[tgt.encode()])),
    }
    if unique_id:
      features['unique_id'] = tf.train.Feature(
          int64_list=tf.train.Int64List(value=[i])),
    example = tf.train.Example(
        features=tf.train.Features(
            feature=features))
    writer.write(example.SerializeToString())
Hongkun Yu's avatar
Hongkun Yu committed
50
51
52
  writer.close()


A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
53
54
55
56
57
58
59
60
def _train_sentencepiece(input_path, vocab_size, model_path, eos_id=1):
  argstr = ' '.join([
      f'--input={input_path}', f'--vocab_size={vocab_size}',
      '--character_coverage=0.995',
      f'--model_prefix={model_path}', '--model_type=bpe',
      '--bos_id=-1', '--pad_id=0', f'--eos_id={eos_id}', '--unk_id=2'
  ])
  SentencePieceTrainer.Train(argstr)
Hongkun Yu's avatar
Hongkun Yu committed
61

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

class WMTDataLoaderTest(tf.test.TestCase, parameterized.TestCase):

  def setUp(self):
    super(WMTDataLoaderTest, self).setUp()
    self._temp_dir = self.get_temp_dir()
    src_lines = [
        'abc ede fg',
        'bbcd ef a g',
        'de f a a g'
    ]
    tgt_lines = [
        'dd cc a ef  g',
        'bcd ef a g',
        'gef cd ba'
    ]
    self._record_train_input_path = os.path.join(self._temp_dir, 'train.record')
    _generate_record_file(self._record_train_input_path, src_lines, tgt_lines)
    self._record_test_input_path = os.path.join(self._temp_dir, 'test.record')
    _generate_record_file(self._record_test_input_path, src_lines, tgt_lines,
                          unique_id=True)
    self._sentencepeice_input_path = os.path.join(self._temp_dir, 'inputs.txt')
    _generate_line_file(self._sentencepeice_input_path, src_lines + tgt_lines)
    sentencepeice_model_prefix = os.path.join(self._temp_dir, 'sp')
    _train_sentencepiece(self._sentencepeice_input_path, 20,
                         sentencepeice_model_prefix)
    self._sentencepeice_model_path = '{}.model'.format(
        sentencepeice_model_prefix)

  @parameterized.named_parameters(
      ('train_static', True, True, 100, (2, 35)),
      ('train_non_static', True, False, 100, (12, 7)),
      ('non_train_static', False, True, 3, (3, 35)),
      ('non_train_non_static', False, False, 50, (2, 7)),)
  def test_load_dataset(
      self, is_training, static_batch, batch_size, expected_shape):
Hongkun Yu's avatar
Hongkun Yu committed
98
    data_config = wmt_dataloader.WMTDataConfig(
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
99
100
        input_path=self._record_train_input_path
        if is_training else self._record_test_input_path,
Hongkun Yu's avatar
Hongkun Yu committed
101
        max_seq_length=35,
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
102
103
104
105
106
107
        global_batch_size=batch_size,
        is_training=is_training,
        static_batch=static_batch,
        src_lang='en',
        tgt_lang='reverse_en',
        sentencepiece_model_path=self._sentencepeice_model_path)
Hongkun Yu's avatar
Hongkun Yu committed
108
109
110
    dataset = wmt_dataloader.WMTDataLoader(data_config).load()
    examples = next(iter(dataset))
    inputs, targets = examples['inputs'], examples['targets']
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
111
112
    self.assertEqual(inputs.shape, expected_shape)
    self.assertEqual(targets.shape, expected_shape)
Hongkun Yu's avatar
Hongkun Yu committed
113
114
115
116

  def test_load_dataset_raise_invalid_window(self):
    batch_tokens_size = 10  # this is too small to form buckets.
    data_config = wmt_dataloader.WMTDataConfig(
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
117
        input_path=self._record_train_input_path,
Hongkun Yu's avatar
Hongkun Yu committed
118
        max_seq_length=100,
Hongkun Yu's avatar
Hongkun Yu committed
119
        global_batch_size=batch_tokens_size,
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
120
121
122
123
124
        is_training=True,
        static_batch=False,
        src_lang='en',
        tgt_lang='reverse_en',
        sentencepiece_model_path=self._sentencepeice_model_path)
Hongkun Yu's avatar
Hongkun Yu committed
125
126
127
128
129
130
131
    with self.assertRaisesRegex(
        ValueError, 'The token budget, global batch size, is too small.*'):
      _ = wmt_dataloader.WMTDataLoader(data_config).load()


if __name__ == '__main__':
  tf.test.main()