def _apply_mask_and_bias(self, alphas):
    bias = self.relative_attention(None)
    if self.mask:
      bias += self._mask

    expand_bias_dims = -self.attention_dim_q - 3
    if expand_bias_dims:
      bias = tf.reshape(bias, [-1] + [1] * expand_bias_dims +
                        list(bias.shape[1:]))
    return alphas + bias