(self, tgt, memory_bank, state, memory_lengths=None,
                step=None, cache=None)
return outputs, state, attns
See :obj:`onmt.modules.RNNDecoderBase.forward()`
See :obj:`onmt.modules.RNNDecoderBase.forward()`
See : obj : onmt . modules . RNNDecoderBase . forward ()
def forward(self, tgt, memory_bank, state, memory_lengths=None,
                step=None, cache=None):
        """
        See :obj:`onmt.modules.RNNDecoderBase.forward()`
        """
        src = state.src
        src_words = src[:, :, 0].transpose(0, 1)
        tgt_words = tgt[:, :, 0].transpose(0, 1)
        src_batch, src_len = src_words.size()
        tgt_batch, tgt_len = tgt_words.size()

        # Initialize return variables.
        outputs = []
        attns = {"std": []}
        if self._copy:
            attns["copy"] = []

        # Run the forward pass of the TransformerDecoder.
        emb = self.embeddings(tgt, step=step)
        assert emb.dim() == 3  # len x batch x embedding_dim

        output = emb.transpose(0, 1).contiguous()
        src_memory_bank = memory_bank.transpose(0, 1).contiguous()

        padding_idx = self.embeddings.word_padding_idx
        src_pad_mask = src_words.data.eq(padding_idx).unsqueeze(1) \
            .expand(src_batch, tgt_len, src_len)
        tgt_pad_mask = tgt_words.data.eq(padding_idx).unsqueeze(1) \
            .expand(tgt_batch, tgt_len, tgt_len)

        if state.cache is None:
            saved_inputs = []

        for i in range(self.num_layers):
            prev_layer_input = None
            if state.cache is None:
                if state.previous_input is not None:
                    prev_layer_input = state.previous_layer_inputs[i]
            output, attn, all_input \
                = self.transformer_layers[i](
                    output, src_memory_bank,
                    src_pad_mask, tgt_pad_mask,
                    previous_input=prev_layer_input,
                    layer_cache=state.cache["layer_{}".format(i)]
                    if state.cache is not None else None,
                    step=step)
            if state.cache is None:
                saved_inputs.append(all_input)

        if state.cache is None:
            saved_inputs = torch.stack(saved_inputs)

        output = self.layer_norm(output)

        # Process the result and update the attentions.
        outputs = output.transpose(0, 1).contiguous()
        attn = attn.transpose(0, 1).contiguous()

        attns["std"] = attn
        if self._copy:
            attns["copy"] = attn

        if state.cache is None:
            state = state.update_state(tgt, saved_inputs)

        return outputs, state, attns
def forward ( self , tgt , memory_bank , state , memory_lengths = None , step = None , cache = None ) : src = state . src src_words = src [ : , : , 0 ] . transpose ( 0 , 1 ) tgt_words = tgt [ : , : , 0 ] . transpose ( 0 , 1 ) src_batch , src_len = src_words . size ( ) tgt_batch , tgt_len = tgt_words . size ( ) # Initialize return variables. outputs = [ ] attns = { "std" : [ ] } if self . _copy : attns [ "copy" ] = [ ] # Run the forward pass of the TransformerDecoder. emb = self . embeddings ( tgt , step = step ) assert emb . dim ( ) == 3 # len x batch x embedding_dim output = emb . transpose ( 0 , 1 ) . contiguous ( ) src_memory_bank = memory_bank . transpose ( 0 , 1 ) . contiguous ( ) padding_idx = self . embeddings . word_padding_idx src_pad_mask = src_words . data . eq ( padding_idx ) . unsqueeze ( 1 ) . expand ( src_batch , tgt_len , src_len ) tgt_pad_mask = tgt_words . data . eq ( padding_idx ) . unsqueeze ( 1 ) . expand ( tgt_batch , tgt_len , tgt_len ) if state . cache is None : saved_inputs = [ ] for i in range ( self . num_layers ) : prev_layer_input = None if state . cache is None : if state . previous_input is not None : prev_layer_input = state . previous_layer_inputs [ i ] output , attn , all_input = self . transformer_layers [ i ] ( output , src_memory_bank , src_pad_mask , tgt_pad_mask , previous_input = prev_layer_input , layer_cache = state . cache [ "layer_{}" . format ( i ) ] if state . cache is not None else None , step = step ) if state . cache is None : saved_inputs . append ( all_input ) if state . cache is None : saved_inputs = torch . stack ( saved_inputs ) output = self . layer_norm ( output ) # Process the result and update the attentions. outputs = output . transpose ( 0 , 1 ) . contiguous ( ) attn = attn . transpose ( 0 , 1 ) . contiguous ( ) attns [ "std" ] = attn if self . _copy : attns [ "copy" ] = attn if state . cache is None : state = state . update_state ( tgt , saved_inputs ) return outputs , state , attns