def forward(self,
                query,
                reference_points,
                input_flatten,
                input_spatial_shapes,
                input_level_start_index,
                input_padding_mask=None):
        """
        Args:
            query:                       (N, Length_{query}, C)
            reference_points:            (N, Length_{query}, n_levels, 2), range in [0, 1], top-left (0,0), bottom-right (1, 1), including padding area
                                            or (N, Length_{query}, n_levels, 4), add additional (w, h) to form reference boxes
            input_flatten:               (N, \sum_{l=0}^{L-1} H_l \cdot W_l, C)
            input_spatial_shapes:        (n_levels, 2), [(H_0, W_0), (H_1, W_1), ..., (H_{L-1}, W_{L-1})]
            input_level_start_index:     (n_levels, ), [0, H_0*W_0, H_0*W_0+H_1*W_1, H_0*W_0+H_1*W_1+H_2*W_2, ..., H_0*W_0+H_1*W_1+...+H_{L-1}*W_{L-1}]
            input_padding_mask:          (N, \sum_{l=0}^{L-1} H_l \cdot W_l), True for padding elements, False for non-padding elements

        Returns:
            output                     (N, Length_{query}, C)
        """

        def masked_fill(x, mask, value):
            y = paddle.full(x.shape, value, x.dtype)
            return paddle.where(mask, y, x)

        N, Len_q, _ = query.shape
        N, Len_in, _ = input_flatten.shape
        assert (input_spatial_shapes[:, 0] *
                input_spatial_shapes[:, 1]).sum() == Len_in

        value = self.value_proj(input_flatten)
        if input_padding_mask is not None:
            value = masked_fill(value, input_padding_mask[..., None], float(0))

        value = value.reshape([
            N, Len_in, self.n_heads,
            int(self.ratio * self.d_model) // self.n_heads
        ])
        sampling_offsets = self.sampling_offsets(query).reshape(
            [N, Len_q, self.n_heads, self.n_levels, self.n_points, 2])
        attention_weights = self.attention_weights(query).reshape(
            [N, Len_q, self.n_heads, self.n_levels * self.n_points])
        attention_weights = F.softmax(attention_weights, -1).\
            reshape([N, Len_q, self.n_heads, self.n_levels, self.n_points])

        if reference_points.shape[-1] == 2:
            offset_normalizer = paddle.stack(
                [input_spatial_shapes[..., 1], input_spatial_shapes[..., 0]],
                -1).astype('float32')
            sampling_locations = reference_points[:, :, None, :, None, :] \
                                 + sampling_offsets / offset_normalizer[None, None, None, :, None, :]
        elif reference_points.shape[-1] == 4:
            sampling_locations = reference_points[:, :, None, :, None, :2] \
                                 + sampling_offsets / self.n_points * reference_points[:, :, None, :, None, 2:] * 0.5
        else:
            raise ValueError(
                'Last dim of reference_points must be 2 or 4, but get {} instead.'
                .format(reference_points.shape[-1]))
        try:
            import ms_deform_attn
        except:
            print(
                "Import ms_deform_attn failed. Please download the following file and refer to "
                "the readme to install ms_deform_attn lib: "
                "https://paddleseg.bj.bcebos.com/dygraph/customized_ops/ms_deform_attn.zip"
            )
            exit()
        output = ms_deform_attn.ms_deform_attn(value, input_spatial_shapes,
                                               input_level_start_index,
                                               sampling_locations,
                                               attention_weights,
                                               self.im2col_step)
        output = self.output_proj(output)
        return output

        Args:
            query:                       (N, Length_{query}, C)
            reference_points:            (N, Length_{query}, n_levels, 2), range in [0, 1], top-left (0,0), bottom-right (1, 1), including padding area
                                            or (N, Length_{query}, n_levels, 4), add additional (w, h) to form reference boxes
            input_flatten:               (N, \sum_{l=0}^{L-1} H_l \cdot W_l, C)
            input_spatial_shapes:        (n_levels, 2), [(H_0, W_0), (H_1, W_1), ..., (H_{L-1}, W_{L-1})]
            input_level_start_index:     (n_levels, ), [0, H_0*W_0, H_0*W_0+H_1*W_1, H_0*W_0+H_1*W_1+H_2*W_2, ..., H_0*W_0+H_1*W_1+...+H_{L-1}*W_{L-1}]
            input_padding_mask:          (N, \sum_{l=0}^{L-1} H_l \cdot W_l), True for padding elements, False for non-padding elements

        Returns:
            output                     (N, Length_{query}, C)
        
paddleseg/models/layers/ms_deformable_attention.py