utils.py 4.48 KB
Newer Older
Myle Ott's avatar
Myle Ott committed
1
2
3
4
5
6
7
8
9
10
# Copyright (c) 2017-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the license found in the LICENSE file in
# the root directory of this source tree. An additional grant of patent rights
# can be found in the PATENTS file in the same directory.

import torch
from torch.autograd import Variable

alexeib's avatar
alexeib committed
11
12
13
from fairseq.data.language_pair_dataset import collate
from fairseq import utils
from fairseq.data import dictionary
Myle Ott's avatar
Myle Ott committed
14
15
16
17
18
19
20
21
22
23
24
25
from fairseq.models import (
    FairseqEncoder,
    FairseqIncrementalDecoder,
    FairseqModel,
)


def dummy_dictionary(vocab_size, prefix='token_'):
    d = dictionary.Dictionary()
    for i in range(vocab_size):
        token = prefix + str(i)
        d.add_symbol(token)
Myle Ott's avatar
Myle Ott committed
26
    d.finalize(padding_factor=1)  # don't add extra padding symbols
Myle Ott's avatar
Myle Ott committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
    return d


def dummy_dataloader(
    samples,
    padding_idx=1,
    eos_idx=2,
    batch_size=None,
):
    if batch_size is None:
        batch_size = len(samples)

    # add any missing data to samples
    for i, sample in enumerate(samples):
        if 'id' not in sample:
            sample['id'] = i

    # create dataloader
    dataset = TestDataset(samples)
    dataloader = torch.utils.data.DataLoader(
        dataset,
        batch_size=batch_size,
        collate_fn=(
alexeib's avatar
alexeib committed
50
            lambda samples: collate(
Myle Ott's avatar
Myle Ott committed
51
52
53
                samples,
                padding_idx,
                eos_idx,
alexeib's avatar
alexeib committed
54
                has_target=True,
Myle Ott's avatar
Myle Ott committed
55
56
57
58
59
60
61
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
            )
        ),
    )
    return iter(dataloader)


class TestDataset(torch.utils.data.Dataset):

    def __init__(self, data):
        super().__init__()
        self.data = data

    def __getitem__(self, index):
        return self.data[index]

    def __len__(self):
        return len(self.data)


class TestModel(FairseqModel):
    def __init__(self, encoder, decoder):
        super().__init__(encoder, decoder)

    @classmethod
    def build_model(cls, args, src_dict, dst_dict):
        encoder = TestEncoder(args, src_dict)
        decoder = TestIncrementalDecoder(args, dst_dict)
        return cls(encoder, decoder)


class TestEncoder(FairseqEncoder):
    def __init__(self, args, dictionary):
        super().__init__(dictionary)
        self.args = args

    def forward(self, src_tokens, src_lengths):
        return src_tokens


class TestIncrementalDecoder(FairseqIncrementalDecoder):
    def __init__(self, args, dictionary):
        super().__init__(dictionary)
97
        assert hasattr(args, 'beam_probs') or hasattr(args, 'probs')
Myle Ott's avatar
Myle Ott committed
98
99
100
        args.max_decoder_positions = getattr(args, 'max_decoder_positions', 100)
        self.args = args

101
102
    def forward(self, prev_output_tokens, encoder_out, incremental_state=None):
        if incremental_state is not None:
Myle Ott's avatar
Myle Ott committed
103
104
105
106
107
108
109
            prev_output_tokens = prev_output_tokens[:, -1:]
        bbsz = prev_output_tokens.size(0)
        vocab = len(self.dictionary)
        src_len = encoder_out.size(1)
        tgt_len = prev_output_tokens.size(1)

        # determine number of steps
110
        if incremental_state is not None:
Myle Ott's avatar
Myle Ott committed
111
            # cache step number
112
            step = utils.get_incremental_state(self, incremental_state, 'step')
Myle Ott's avatar
Myle Ott committed
113
114
            if step is None:
                step = 0
115
            utils.set_incremental_state(self, incremental_state, 'step', step + 1)
Myle Ott's avatar
Myle Ott committed
116
117
118
119
120
            steps = [step]
        else:
            steps = list(range(tgt_len))

        # define output in terms of raw probs
121
122
123
124
125
126
127
128
129
130
131
132
133
        if hasattr(self.args, 'probs'):
            assert self.args.probs.dim() == 3, \
                'expected probs to have size bsz*steps*vocab'
            probs = self.args.probs.index_select(1, torch.LongTensor(steps))
        else:
            probs = torch.FloatTensor(bbsz, len(steps), vocab).zero_()
            for i, step in enumerate(steps):
                # args.beam_probs gives the probability for every vocab element,
                # starting with eos, then unknown, and then the rest of the vocab
                if step < len(self.args.beam_probs):
                    probs[:, i, self.dictionary.eos():] = self.args.beam_probs[step]
                else:
                    probs[:, i, self.dictionary.eos()] = 1.0
Myle Ott's avatar
Myle Ott committed
134
135
136
137
138
139

        # random attention
        attn = torch.rand(bbsz, src_len, tgt_len)

        return Variable(probs), Variable(attn)

alexeib's avatar
alexeib committed
140
    def get_normalized_probs(self, net_output, log_probs, _):
Myle Ott's avatar
Myle Ott committed
141
        # the decoder returns probabilities directly
Myle Ott's avatar
Myle Ott committed
142
        probs = net_output[0]
Myle Ott's avatar
Myle Ott committed
143
        if log_probs:
Myle Ott's avatar
Myle Ott committed
144
            return probs.log()
Myle Ott's avatar
Myle Ott committed
145
        else:
Myle Ott's avatar
Myle Ott committed
146
            return probs
Myle Ott's avatar
Myle Ott committed
147
148
149

    def max_positions(self):
        return self.args.max_decoder_positions