import tensorflow as tf


        # Convolution on patches.
        w_ = tf.reshape(w, [ksize[0] * ksize[1] * ksize[2], -1])
        q = tf.matmul(p_, w_)

        # Center locations.
        blk_indices_crop = blk_indices[:, 0, 0, :]

        #  Project back to an image.
        y = tf.scatter_nd(blk_indices_crop, q, out_shape)
        return y

    with tf.control_dependencies([assert_shape, assert_strides]):
        return tf.cond(
            tf.equal(tf.size(blk_indices_), 0), lambda: tf.zeros(out_shape, dtype=x.dtype),
            _conv_nonzero)


def mask_conv2d(x, w, mask, strides, padding):
    """Masked 2D convolution. Used to check 2D sparse convolution.

    :param x:         [Tensor]    Convolution feature map, 4D, dtype float32.
    :param w:         [Tensor]    Convolution kernel, 4D, dtype float32.
    :param mask:      [Tensor]    Binary mask, 3D or 4D, [N, H, W] or [N, H, W, 1], dtype float32.
