def hybrid_forward(self, F, inputs, states=None,<EOL>valid_length=None, steps=None,<EOL>position_weight=None):<EOL>
length = self._seq_length<EOL>C_in = self._input_size<EOL>if valid_length is not None:<EOL><INDENT>arange = F.arange(length)<EOL>mask = F.broadcast_lesser(<EOL>arange.reshape((<NUM_LIT:1>, -<NUM_LIT:1>)),<EOL>valid_length.reshape((-<NUM_LIT:1>, <NUM_LIT:1>)))<EOL>mask = F.broadcast_axes(F.expand_dims(mask, axis=<NUM_LIT:1>), axis=<NUM_LIT:1>, size=length)<EOL>if states is None:<EOL><INDENT>states = [mask]<EOL><DEDENT>else:<EOL><INDENT>states.append(mask)<EOL><DEDENT><DEDENT>if self._scale_embed:<EOL><INDENT>inputs = inputs * math.sqrt(C_in)<EOL><DEDENT>steps = F.arange(length)<EOL>if states is None:<EOL><INDENT>states = [steps]<EOL><DEDENT>else:<EOL><INDENT>states.append(steps)<EOL><DEDENT>if states is not None:<EOL><INDENT>steps = states[-<NUM_LIT:1>]<EOL>positional_embed = F.Embedding(steps, position_weight, self._max_length, self._units)<EOL>inputs = F.broadcast_add(inputs, F.expand_dims(positional_embed, axis=<NUM_LIT:0>))<EOL><DEDENT>if self._dropout:<EOL><INDENT>if self._use_layer_norm_before_dropout:<EOL><INDENT>inputs = self.layer_norm(inputs)<EOL>inputs = self.dropout_layer(inputs)<EOL><DEDENT>else:<EOL><INDENT>inputs = self.dropout_layer(inputs)<EOL>inputs = self.layer_norm(inputs)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>inputs = self.layer_norm(inputs)<EOL><DEDENT>outputs = inputs<EOL>if valid_length is not None:<EOL><INDENT>mask = states[-<NUM_LIT:2>]<EOL><DEDENT>else:<EOL><INDENT>mask = None<EOL><DEDENT>all_encodings_outputs = []<EOL>additional_outputs = []<EOL>for cell in self.transformer_cells:<EOL><INDENT>outputs, attention_weights = cell(inputs, mask)<EOL>inputs = outputs<EOL>if self._output_all_encodings:<EOL><INDENT>if valid_length is not None:<EOL><INDENT>outputs = F.SequenceMask(outputs, sequence_length=valid_length,<EOL>use_sequence_length=True, axis=<NUM_LIT:1>)<EOL><DEDENT>all_encodings_outputs.append(outputs)<EOL><DEDENT>if self._output_attention:<EOL><INDENT>additional_outputs.append(attention_weights)<EOL><DEDENT><DEDENT>if valid_length is not None:<EOL><INDENT>outputs = F.SequenceMask(outputs, sequence_length=valid_length,<EOL>use_sequence_length=True, axis=<NUM_LIT:1>)<EOL><DEDENT>if self._output_all_encodings:<EOL><INDENT>return all_encodings_outputs, additional_outputs<EOL><DEDENT>else:<EOL><INDENT>return outputs, additional_outputs<EOL><DEDENT>
Parameters
----------
inputs : NDArray, Shape(batch_size, length, C_in)
states : list of NDArray
valid_length : NDArray
steps : NDArray
    Stores value [0, 1, ..., length].
    It is used for lookup in positional encoding matrix

Returns
-------
outputs : NDArray
    The output of the encoder. Shape is (batch_size, length, C_out)
additional_outputs : list
    Either be an empty list or contains the attention weights in this step.
    The attention weights will have shape (batch_size, length, length) or
    (batch_size, num_heads, length, length)