Unverified Commit 94dae690 authored by Myle Ott's avatar Myle Ott Committed by GitHub
Browse files

Merge pull request #77 from facebookresearch/oss-merge-internal

parents d74f200a 0a836276
Loading
Loading
Loading
Loading
+3 −0
Changes for fairseq/models/fairseq_decoder.py: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -18,3 +18,6 @@ class FairseqDecoder(nn.Module):
    def max_positions(self):
        """Maximum input length supported by the decoder."""
        raise NotImplementedError

    def upgrade_state_dict(self, state_dict):
        return state_dict
+3 −0
Changes for fairseq/models/fairseq_encoder.py: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -18,3 +18,6 @@ class FairseqEncoder(nn.Module):
    def max_positions(self):
        """Maximum input length supported by the encoder."""
        raise NotImplementedError

    def upgrade_state_dict(self, state_dict):
        return state_dict
+5 −0
Changes for fairseq/models/fairseq_model.py: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -43,6 +43,11 @@ class FairseqModel(nn.Module):
        """Maximum output length supported by the decoder."""
        return self.decoder.max_positions()

    def upgrade_state_dict(self, state_dict):
        state_dict = self.encoder.upgrade_state_dict(state_dict)
        state_dict = self.decoder.upgrade_state_dict(state_dict)
        return state_dict

    def make_generation_fast_(self, **kwargs):
        """Optimize model for faster generation."""
        if self._is_generation_fast:
+13 −2
Changes for fairseq/models/fconv.py: 13 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -58,7 +58,7 @@ class FConvEncoder(FairseqEncoder):
        self.projections = nn.ModuleList()
        self.convolutions = nn.ModuleList()
        for (out_channels, kernel_size) in convolutions:
            pad = (kernel_size - 1) // 2
            pad = (kernel_size - 1) / 2
            self.projections.append(Linear(in_channels, out_channels)
                                    if in_channels != out_channels else None)
            self.convolutions.append(
@@ -154,6 +154,7 @@ class FConvDecoder(FairseqIncrementalDecoder):
                 max_positions=1024, convolutions=((512, 3),) * 20,
                 attention=True, dropout=0.1):
        super().__init__()
        self.register_buffer('version', torch.Tensor([2]))
        self.dictionary = dictionary
        self.dropout = dropout

@@ -265,6 +266,16 @@ class FConvDecoder(FairseqIncrementalDecoder):
        """Maximum output length supported by the decoder."""
        return self.embed_positions.num_embeddings - self.dictionary.pad() - 1

    def upgrade_state_dict(self, state_dict):
        if state_dict.get('decoder.version', torch.Tensor([1]))[0] < 2:
            # old models use incorrect weight norm dimension
            for i, conv in enumerate(self.convolutions):
                # reconfigure weight norm
                nn.utils.remove_weight_norm(conv)
                self.convolutions[i] = nn.utils.weight_norm(conv, dim=0)
            state_dict['decoder.version'] = torch.Tensor([1])
        return state_dict

    def _split_encoder_out(self, encoder_out):
        """Split and transpose encoder outputs.

@@ -307,7 +318,7 @@ def LinearizedConv1d(in_channels, out_channels, kernel_size, dropout=0, **kwargs
    std = math.sqrt((4 * (1.0 - dropout)) / (m.kernel_size[0] * in_channels))
    m.weight.data.normal_(mean=0, std=std)
    m.bias.data.zero_()
    return nn.utils.weight_norm(m)
    return nn.utils.weight_norm(m, dim=2)


def ConvTBC(in_channels, out_channels, kernel_size, dropout=0, **kwargs):
+1 −1
Changes for fairseq/modules/conv_tbc.py: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -59,7 +59,7 @@ class ConvTBCFunction(Function):
        kernel_size = weight_size[0]

        output = input.new(
            input_size[0] - kernel_size + 1 + pad * 2,
            input_size[0] - kernel_size + 1 + int(pad * 2),
            input_size[1],
            weight_size[2])

Loading