def forward(self,
                query,
                key,
                value,
                residual=None,
                attn_mask=None,
                reference_points=None,
                value_spatial_shapes=None,
                value_level_start_index=None,
                **kwargs):
        """Forward Function of MultiScaleDeformAttention.

        Args:
            query (Tensor): Query of Transformer with shape
                (num_query, bs, embed_dims).
            key (Tensor): The key tensor with shape (num_key, bs, embed_dims).
            value (Tensor): The value tensor with shape
                (num_key, bs, embed_dims).
            residual (Tensor): The tensor used for addition, with the
                same shape as `x`. Default None. If None, `x` will be used.
            reference_points (Tensor):  The normalized reference points with
                shape (bs, num_query, num_levels, K*2), all elements is range
                in [0, 1], top-left (0,0), bottom-right (1, 1), including
                padding area.
            attn_mask (Tensor): ByteTensor for `query`, with
                shape [bs, num_key].
            value_spatial_shapes (Tensor): Spatial shape of features in
                different level. With shape  (num_levels, 2),
                last dimension represent (h, w).
            value_level_start_index (Tensor): The start index of each level.
                A tensor has shape (num_levels) and can be represented
                as [0, h_0*w_0, h_0*w_0+h_1*w_1, ...].

        Returns:
            Tensor: forwarded results with shape [num_query, bs, embed_dims].
        """

        if key is None:
            key = query
        if value is None:
            value = key

        bs, num_query, _ = query.shape
        bs, num_key, _ = value.shape
        assert (value_spatial_shapes[:, 0].numpy() *
                value_spatial_shapes[:, 1].numpy()).sum() == num_key

        value = self.value_proj(value)
        if attn_mask is not None:
            # value = value.masked_fill(attn_mask[..., None], 0.0)
            value *= attn_mask.unsqueeze(-1)
        value = value.reshape([bs, num_key, self.num_heads, -1])
        sampling_offsets = self.sampling_offsets(query).reshape([
            bs, num_query, self.num_heads, self.num_levels, self.num_points, 2
        ])
        attention_weights = self.attention_weights(query).reshape(
            [bs, num_query, self.num_heads, self.num_levels * self.num_points])
        attention_weights = F.softmax(attention_weights, axis=-1)

        attention_weights = attention_weights.reshape(
            [bs, num_query, self.num_heads, self.num_levels, self.num_points])
        if reference_points.shape[-1] == self.num_points * 2:
            reference_points_reshape = reference_points.reshape(
                (bs, num_query, self.num_levels, -1, 2)).unsqueeze(2)
            x1 = reference_points[:, :, :, 0::2].min(axis=-1, keepdim=True)
            y1 = reference_points[:, :, :, 1::2].min(axis=-1, keepdim=True)
            x2 = reference_points[:, :, :, 0::2].max(axis=-1, keepdim=True)
            y2 = reference_points[:, :, :, 1::2].max(axis=-1, keepdim=True)
            w = paddle.clip(x2 - x1, min=1e-4)
            h = paddle.clip(y2 - y1, min=1e-4)
            wh = paddle.concat([w, h], axis=-1)[:, :, None, :, None, :]

            sampling_locations = reference_points_reshape \
                                 + sampling_offsets * wh * 0.5
        else:
            raise ValueError(
                f'Last dim of reference_points must be'
                f' 2K, but get {reference_points.shape[-1]} instead.')

        output = self.ms_deformable_attn_core(
            value, value_spatial_shapes, value_level_start_index,
            sampling_locations, attention_weights)

        output = self.output_proj(output)
        return output
Forward Function of MultiScaleDeformAttention.

        Args:
            query (Tensor): Query of Transformer with shape
                (num_query, bs, embed_dims).
            key (Tensor): The key tensor with shape (num_key, bs, embed_dims).
            value (Tensor): The value tensor with shape
                (num_key, bs, embed_dims).
            residual (Tensor): The tensor used for addition, with the
                same shape as `x`. Default None. If None, `x` will be used.
            reference_points (Tensor):  The normalized reference points with
                shape (bs, num_query, num_levels, K*2), all elements is range
                in [0, 1], top-left (0,0), bottom-right (1, 1), including
                padding area.
            attn_mask (Tensor): ByteTensor for `query`, with
                shape [bs, num_key].
            value_spatial_shapes (Tensor): Spatial shape of features in
                different level. With shape  (num_levels, 2),
                last dimension represent (h, w).
            value_level_start_index (Tensor): The start index of each level.
                A tensor has shape (num_levels) and can be represented
                as [0, h_0*w_0, h_0*w_0+h_1*w_1, ...].

        Returns:
            Tensor: forwarded results with shape [num_query, bs, embed_dims].
        
ppdet/modeling/transformers/petr_transformer.py