def _try_tf_conv(lhs, rhs, window_strides, padding, lhs_dilation, rhs_dilation, dimension_numbers, feature_group_count, batch_group_count, preferred_element_type: Optional[DType], out_shape) -> TfVal:

    def error(msg):
        suffix = 'See source code for the precise conditions under which convolutions can be converted without XLA.'
        return _xla_disabled_error('conv_general_dilated', f'{msg} - {suffix}')
    if (lhs.dtype not in [tf.float16, tf.float32, tf.float64]):
        raise error(f'tf.nn.convolution is not supported for dtype {lhs.dtype}')
    if (feature_group_count != 1):
        raise error('tf.nn.convolution does not support grouped convolutions')
    if (batch_group_count != 1):
        raise error('Unimplemented support for batch_group_count != 1')
    nb_spatial_dimensions = (len(lhs.shape) - 2)
    if ((nb_spatial_dimensions < 1) or (nb_spatial_dimensions > 3)):
        raise error('TensorFlow can only handle convolutions with 1, 2, or 3 spatial dimensions')
    if (list(window_strides) != ([1] * nb_spatial_dimensions)):
        raise error(f'Unimplemented support for window_strides != {tuple(([1] * nb_spatial_dimensions))}')
    if ((preferred_element_type is not None) and (preferred_element_type != lhs.dtype)):
        raise error('Unimplemented support for preferred_element_type')

    def convert_padding() -> str:
        if (list(lhs_dilation) != ([1] * nb_spatial_dimensions)):
            raise error('Padding conversion is not supported for transposed convolution.')
        (lhs_perm, rhs_perm, _) = dimension_numbers
        effective_rhs_shape = [(((k - 1) * r) + 1) for (k, r) in zip(np.take(rhs.shape, rhs_perm)[2:], rhs_dilation)]
        lhs_shape = np.take(lhs.shape, lhs_perm)[2:]
        for pad_str in ['VALID', 'SAME']:
            gen_padding = lax.padtype_to_pads(lhs_shape, effective_rhs_shape, window_strides, pad_str)
            if (list(gen_padding) == list(padding)):
                return pad_str
        raise error('Input padding not supported in TensorFlow.')

    def convert_dim_nums() -> str:
        (lhs_spec, rhs_spec, out_spec) = dimension_numbers
        supported_rhs_shape = ([(nb_spatial_dimensions + 1), nb_spatial_dimensions] + list(range(nb_spatial_dimensions)))
        if (list(rhs_spec) != supported_rhs_shape):
            raise error('Input filter (RHS) shape format not supported in TensorFlow.')
        if (lhs_spec != out_spec):
            raise error('TensorFlow requires the same data format for LHS and output.')
        spatial_dim_alphabet = 'DHW'[(- nb_spatial_dimensions):]
        if (list(lhs_spec) == ([0, (len(lhs_spec) - 1)] + list(range(1, (len(lhs_spec) - 1))))):
            return (('N' + spatial_dim_alphabet) + 'C')
        raise error('Data format is unsupported by TensorFlow.')

    def convert_dilation_and_compute_result(tf_padding: str, tf_dim_nums: str) -> TfVal:
        no_dilation = ([1] * nb_spatial_dimensions)
        if (not ((list(lhs_dilation) == no_dilation) or (list(rhs_dilation) == no_dilation))):
            raise error('Both LHS and RHS dilations are set.')
        if (list(lhs_dilation) == no_dilation):
            return tf.nn.convolution(lhs, rhs, strides=window_strides, padding=tf_padding, data_format=tf_dim_nums, dilations=rhs_dilation)
        return tf.nn.conv_transpose(lhs, rhs, out_shape, window_strides, padding=tf_padding, data_format=tf_dim_nums, dilations=lhs_dilation)
    tf_padding = convert_padding()
    tf_dim_nums = convert_dim_nums()
    return convert_dilation_and_compute_result(tf_padding, tf_dim_nums)
def _try_tf_conv(lhs, rhs, window_strides, padding, lhs_dilation, rhs_dilation, dimension_numbers, feature_group_count, batch_group_count, preferred_element_type: Optional[DType], out_shape) -> TfVal:

    def error(msg):
        suffix = 'See source code for the precise conditions under which convolutions can be converted without XLA.'
        return _xla_disabled_error('conv_general_dilated', f'{msg} - {suffix}')
    if (lhs.dtype not in [tf.float16, tf.float32, tf.float64]):
        raise error(f'tf.nn.convolution is not supported for dtype {lhs.dtype}')
    if (feature_group_count != 1):
        raise error('tf.nn.convolution does not support grouped convolutions')
    if (batch_group_count != 1):
        raise error('Unimplemented support for batch_group_count != 1')
    nb_spatial_dimensions = (len(lhs.shape) - 2)
    if ((nb_spatial_dimensions < 1) or (nb_spatial_dimensions > 3)):
        raise error('TensorFlow can only handle convolutions with 1, 2, or 3 spatial dimensions')
    if (list(window_strides) != ([1] * nb_spatial_dimensions)):
        raise error(f'Unimplemented support for window_strides != {tuple(([1] * nb_spatial_dimensions))}')
    if ((preferred_element_type is not None) and (preferred_element_type != lhs.dtype)):
        raise error('Unimplemented support for preferred_element_type')

    def convert_padding() -> str:
        if (list(lhs_dilation) != ([1] * nb_spatial_dimensions)):
            raise error('Padding conversion is not supported for transposed convolution.')
        (lhs_perm, rhs_perm, _) = dimension_numbers
        effective_rhs_shape = [(((k - 1) * r) + 1) for (k, r) in zip(np.take(rhs.shape, rhs_perm)[2:], rhs_dilation)]
        lhs_shape = np.take(lhs.shape, lhs_perm)[2:]
        for pad_str in ['VALID', 'SAME']:
            gen_padding = lax.padtype_to_pads(lhs_shape, effective_rhs_shape, window_strides, pad_str)
            if (list(gen_padding) == list(padding)):
                return pad_str
        raise error('Input padding not supported in TensorFlow.')

    def convert_dim_nums() -> str:
        (lhs_spec, rhs_spec, out_spec) = dimension_numbers
        supported_rhs_shape = ([(nb_spatial_dimensions + 1), nb_spatial_dimensions] + list(range(nb_spatial_dimensions)))
        if (list(rhs_spec) != supported_rhs_shape):
            raise error('Input filter (RHS) shape format not supported in TensorFlow.')
        if (lhs_spec != out_spec):
            raise error('TensorFlow requires the same data format for LHS and output.')
        spatial_dim_alphabet = 'DHW'[(- nb_spatial_dimensions):]
        if (list(lhs_spec) == ([0, (len(lhs_spec) - 1)] + list(range(1, (len(lhs_spec) - 1))))):
            return (('N' + spatial_dim_alphabet) + 'C')
        raise error('Data format is unsupported by TensorFlow.')

    def convert_dilation_and_compute_result(tf_padding: str, tf_dim_nums: str) -> TfVal:
        no_dilation = ([1] * nb_spatial_dimensions)
        if (not ((list(lhs_dilation) == no_dilation) or (list(rhs_dilation) == no_dilation))):
            raise error('Both LHS and RHS dilations are set.')
        if (list(lhs_dilation) == no_dilation):
            return tf.nn.convolution(lhs, rhs, strides=window_strides, padding=tf_padding, data_format=tf_dim_nums, dilations=rhs_dilation)
        return tf.nn.conv_transpose(lhs, rhs, out_shape, window_strides, padding=tf_padding, data_format=tf_dim_nums, dilations=lhs_dilation)
    tf_padding = convert_padding()
    tf_dim_nums = convert_dim_nums()
    return convert_dilation_and_compute_result(tf_padding, tf_dim_nums)