import tensorflow as tf


    # Currently we do not support strides > 1 in this matrix multiplication mode. Could be supported
    # in the future.
    assert_strides = tf.assert_equal(
        tf.cast(tf.stack([strides[1], strides[2]]), tf.int64),
        tf.constant([1, 1], dtype=tf.int64),
        message='Strides > 1 not supported.')

    # Convolution when number of indices is larger than zero.
    def _conv_nonzero():
        # Gather patches.
        p = tf.gather_nd(x_, blk_indices_)
        p_ = tf.reshape(p, [-1, ksize[0] * ksize[1] * ksize[2]])

        # 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.
