コード例 #1
0
ファイル: test_utils.py プロジェクト: nguyenlab/fairseq-py
    def test_make_variable(self):
        t = [{'k': torch.rand(5, 5)}]

        v = utils.make_variable(t)[0]['k']
        self.assertTrue(isinstance(v, Variable))
        self.assertFalse(v.data.is_cuda)

        v = utils.make_variable(t, cuda=True)[0]['k']
        self.assertEqual(v.data.is_cuda, torch.cuda.is_available())
コード例 #2
0
    def generate_batched_itr(self, data_itr, beam_size=None, maxlen_a=0.0, maxlen_b=None,
                             cuda=False, timer=None, prefix_size=0):
        """Iterate over a batched dataset and yield individual translations.

        Args:
            maxlen_a/b: generate sequences of maximum length ax + b,
                where x is the source sentence length.
            cuda: use GPU for generation
            timer: StopwatchMeter for timing generations.
        """
        if maxlen_b is None:
            maxlen_b = self.maxlen

        for sample in data_itr:
            s = utils.make_variable(sample, volatile=True, cuda=cuda)
            input = s['net_input']
            srclen = input['src_tokens'].size(1)
            if timer is not None:
                timer.start()
            with utils.maybe_no_grad():
                hypos = self.generate(
                    input['src_tokens'],
                    input['src_lengths'],
                    beam_size=beam_size,
                    maxlen=int(maxlen_a*srclen + maxlen_b),
                    prefix_tokens=s['target'][:, :prefix_size] if prefix_size > 0 else None,
                )
            if timer is not None:
                timer.stop(sum([len(h[0]['tokens']) for h in hypos]))
            for i, id in enumerate(s['id'].data):
                src = input['src_tokens'].data[i, :]
                # remove padding from ref
                ref = utils.strip_pad(s['target'].data[i, :], self.pad) if s['target'] is not None else None
                yield id, src, ref, hypos[i]
コード例 #3
0
 def score_batched_itr(self, data_itr, cuda=False, timer=None):
     """Iterate over a batched dataset and yield scored translations."""
     for sample in data_itr:
         s = utils.make_variable(sample, volatile=True, cuda=cuda)
         if timer is not None:
             timer.start()
         pos_scores, attn = self.score(s)
         if timer is not None:
             timer.stop(s['ntokens'])
         for i, id in enumerate(s['id'].data):
             src = s['net_input']['src_tokens'].data[i, :]
             # remove padding from ref
             ref = utils.strip_pad(s['target'].data[i, :], self.pad)
             tgt_len = ref.numel()
             pos_scores_i = pos_scores[i][:tgt_len]
             score_i = pos_scores_i.sum() / tgt_len
             attn_i = attn[i]
             _, alignment = attn_i.max(dim=0)
             hypos = [{
                 'tokens': ref,
                 'score': score_i,
                 'attention': attn_i,
                 'alignment': alignment,
                 'positional_scores': pos_scores_i,
             }]
             # return results in the same format as SequenceGenerator
             yield id, src, ref, hypos
コード例 #4
0
ファイル: trainer.py プロジェクト: zsquaredz/XSum
 def _prepare_sample(self, sample, volatile):
     if sample is None or len(sample) == 0:
         return None
     if hasattr(torch.cuda, 'empty_cache'):
         # clear the caching allocator if this is the largest sample we've seen
         if sample['target'].size(0) > self._max_bsz_seen:
             self._max_bsz_seen = sample['target'].size(0)
             torch.cuda.empty_cache()
     return utils.make_variable(sample, volatile=volatile, cuda=True)
コード例 #5
0
    def generate_batched_itr(
        self,
        data_itr,
        beam_size=None,
        maxlen_a=0.0,
        maxlen_b=None,
        cuda=False,
        timer=None,
        prefix_size=0,
    ):
        """Iterate over a batched dataset and yield individual translations.

        Args:
            maxlen_a/b: generate sequences of maximum length ax + b,
                where x is the source sentence length.
            cuda: use GPU for generation
            timer: StopwatchMeter for timing generations.
        """
        if maxlen_b is None:
            maxlen_b = self.maxlen

        for sample in data_itr:
            s = utils.make_variable(sample, volatile=True, cuda=cuda)
            input = s["net_input"]
            # Take the max source length to compute the max target length
            srclen = input["src_tokens"].size(1)
            # FIXME: handle characters properly
            if self.use_char_source:
                raise ValueError(
                    "Character level encoder is not supported yet for "
                    "multisource sentences."
                )
            encoder_inputs = (input["src_tokens"], input["src_lengths"])
            if timer is not None:
                timer.start()
            with utils.maybe_no_grad():
                hypos = self.generate(
                    encoder_inputs,
                    srcs_ids=input["src_ids"],
                    beam_size=beam_size,
                    maxlen=int(maxlen_a * srclen + maxlen_b),
                    prefix_tokens=s["target"][:, :prefix_size]
                    if prefix_size > 0
                    else None,
                )
            if timer is not None:
                timer.stop(s["ntokens"])
            for i, id in enumerate(s["id"]):
                src = input["src_tokens"].index_select(
                    0,
                    input['src_ids'][self.align_to]
                )
                # remove padding from ref
                ref = utils.strip_pad(s["target"][i, :], self.pad)
                yield id, src, ref, hypos[i]
コード例 #6
0
    def _async_prepare_sample(self, rank, device_id, sample, volatile):
        if sample is None:
            self._sample = None
        else:
            if hasattr(torch.cuda, 'empty_cache'):
                # clear the caching allocator if this is the largest sample we've seen
                if sample['target'].size(0) > self._max_bsz_seen:
                    self._max_bsz_seen = sample['target'].size(0)
                    torch.cuda.empty_cache()

            self._sample = utils.make_variable(sample, volatile=volatile, cuda_device=device_id)
コード例 #7
0
    def generate_batched_itr(
        self,
        data_itr,
        beam_size=None,
        maxlen_a=0.0,
        maxlen_b=None,
        cuda=False,
        timer=None,
        prefix_size=0,
    ):
        """Iterate over a batched dataset and yield individual translations.

        Args:
            maxlen_a/b: generate sequences of maximum length ax + b,
                where x is the source sentence length.
            cuda: use GPU for generation
            timer: StopwatchMeter for timing generations.
        """
        if maxlen_b is None:
            maxlen_b = self.maxlen

        for sample in data_itr:
            s = utils.make_variable(sample, volatile=True, cuda=cuda)
            input = s["net_input"]
            srclen = input["src_tokens"].size(1)
            if self.use_char_source:
                encoder_input = (
                    input["src_tokens"],
                    input["src_lengths"],
                    input["char_inds"],
                    input["word_lengths"],
                )
            else:
                encoder_input = (input["src_tokens"], input["src_lengths"])
            if timer is not None:
                timer.start()
            with utils.maybe_no_grad():
                hypos = self.generate(
                    encoder_input,
                    beam_size=beam_size,
                    maxlen=int(maxlen_a * srclen + maxlen_b),
                    prefix_tokens=s["target"][:, :prefix_size]
                    if prefix_size > 0
                    else None,
                )
            if timer is not None:
                timer.stop(s["ntokens"])
            for i, id in enumerate(s["id"].data):
                src = input["src_tokens"].data[i, :]
                # remove padding from ref
                ref = utils.strip_pad(s["target"].data[i, :], self.pad)
                yield id, src, ref, hypos[i]
コード例 #8
0
    def generate_batched_itr(self,
                             data_itr,
                             beam_size=None,
                             maxlen_a=0.0,
                             maxlen_b=None,
                             cuda_device=None,
                             timer=None):
        """Iterate over a batched dataset and yield individual translations.

        Args:
            maxlen_a/b: generate sequences of maximum length ax + b,
                where x is the source sentence length.
            cuda_device: GPU on which to do generation.
            timer: StopwatchMeter for timing generations.
        """
        if maxlen_b is None:
            maxlen_b = self.maxlen

        for sample in data_itr:
            s = utils.make_variable(sample,
                                    volatile=True,
                                    cuda_device=cuda_device)
            input = s['net_input']
            srclen = input['src_tokens'].size(1)
            if timer is not None:
                timer.start()
            with utils.maybe_no_grad():
                hypos = self.generate(input['src_tokens'],
                                      beam_size=beam_size,
                                      maxlen=int(maxlen_a * srclen + maxlen_b))
            if timer is not None:
                timer.stop(s['ntokens'])
            for i, id in enumerate(s['id'].data):
                src = input['src_tokens'].data[i, :]
                # remove padding from ref
                ref = utils.strip_pad(s['target'].data[i, :], self.pad)
                yield id, src, ref, hypos[i]