o
    YÛ·i¾  ã                   @  s¢   d Z ddlmZ ddlmZmZ erddlmZ ddlm	Z	 ddl
mZ ddlmZ g d	¢Z	
dd dd„Z	
dd!dd„Z	
dd"dd„ZedƒG dd„ deƒƒZdS )#z(Structural helper objects and functions.é    )Úannotations)ÚTYPE_CHECKINGÚAnyé   )Ú
AccessPath)Ú_ffi_api)ÚObject)Úregister_object)ÚStructuralKeyÚget_first_structural_mismatchÚstructural_equalÚstructural_hashFÚlhsr   ÚrhsÚmap_free_varsÚboolÚskip_tensor_contentÚreturnc                 C  ó   t  | |||¡S )a¯  Check structural equality between two values.

    Structural equality compares the *shape/content structure* of two values
    instead of Python object identity. For container-like values, this means
    recursive comparison of elements/fields. For object types that provide
    structural equal hooks, those hooks are used.

    Parameters
    ----------
    lhs
        Left-hand side value.
    rhs
        Right-hand side value.

    map_free_vars
        Whether free variables (variables without a definition site) can be
        mapped to each other during comparison.

    skip_tensor_content
        Whether to skip tensor data content when comparing tensors.

    Returns
    -------
    result
        Whether the two values are structurally equal.

    Examples
    --------
    .. code-block:: python

        import tvm_ffi

        assert tvm_ffi.structural_equal([1, 2, 3], [1, 2, 3])
        assert not tvm_ffi.structural_equal([1, 2, 3], [1, 2, 4])

    See Also
    --------
    :py:func:`tvm_ffi.structural_hash`
        Hash function compatible with structural equality.
    :py:func:`tvm_ffi.get_first_structural_mismatch`
        Mismatch diagnostics with access paths.

    )r   ÚStructuralEqual©r   r   r   r   © r   úH/home/ubuntu/vllm_env/lib/python3.10/site-packages/tvm_ffi/structural.pyr   '   s   .r   ÚvalueÚintc                 C  s   t  | ||¡d@ S )a—  Compute structural hash of a value.

    This hash is designed to be consistent with :py:func:`structural_equal`
    under the same options. If two values are structurally equal, they should
    have the same structural hash.

    Parameters
    ----------
    value
        Input value to hash.

    map_free_vars
        Whether free variables mapped to each other during hashing.

    skip_tensor_content
        Whether tensor data content is ignored when hashing tensors.

    Returns
    -------
    hash_value
        Structural hash value.

    Examples
    --------
    .. code-block:: python

        import tvm_ffi

        h0 = tvm_ffi.structural_hash([1, 2, 3])
        h1 = tvm_ffi.structural_hash([1, 2, 3])
        assert h0 == h1

    Notes
    -----
    Structural hash is intended for hash-table bucketing and fast pre-checks.
    Always use structural equality to confirm semantic equivalence.

    ì   ÿÿÿÿ )r   ÚStructuralHash)r   r   r   r   r   r   r   X   s   +r   ú$tuple[AccessPath, AccessPath] | Nonec                 C  r   )a„  Like structural_equal(), but returns the AccessPath pair of the first detected mismatch.

    Parameters
    ----------
    lhs
        The left operand.

    rhs
        The right operand.

    map_free_vars
        Whether free variables (i.e. variables without a definition site) should be mapped
        as equal to each other.

    skip_tensor_content
        Whether to skip the data content of tensor.

    Returns
    -------
    mismatch: tuple[AccessPath, AccessPath] | None
        `None` if `lhs` and `rhs` are structurally equal.
        Otherwise, a tuple of two AccessPath objects that point to the first detected mismatch.

    )r   ÚGetFirstStructuralMismatchr   r   r   r   r   †   s   r   zffi.StructuralKeyc                   @  sN   e Zd ZU dZded< ded< erddd	„Zddd„Zddd„Zddd„Z	dS )r
   aÌ  Hash-cached structural key wrapper.

    This wrapper can be used to hint that a dict uses structural equality and hash for the key.

    Examples
    --------
    Use ``StructuralKey`` with Python dictionaries when you want key lookup by
    structural semantics:

    .. code-block:: python

        import tvm_ffi

        k0 = tvm_ffi.StructuralKey([1, 2, 3])
        k1 = tvm_ffi.StructuralKey([1, 2, 3])
        k2 = tvm_ffi.StructuralKey([1, 2, 4])

        d = {k0: "value-a", k2: "value-b"}
        assert d[k1] == "value-a"  # k1 matches k0 structurally
        assert d[k2] == "value-b"

    It can also be used directly with :py:class:`tvm_ffi.Map`:

    .. code-block:: python

        m = tvm_ffi.Map({k0: 1, k1: 2})
        assert len(m) == 1
        assert m[k0] == 2

    See Also
    --------
    :py:func:`tvm_ffi.structural_equal`
        Structural equality comparison.
    :py:func:`tvm_ffi.structural_hash`
        Structural hash computation.

    r   Úkeyr   Úhash_i64r   r   c                C  s   d S )Nr   ©Úselfr   r   r   Ú__ffi_shallow_copy__Ñ   s    z"StructuralKey.__ffi_shallow_copy__ÚNonec                 C  s   |   tj|¡ dS )z¦Create a structural key from ``key``.

        Parameters
        ----------
        key
            The underlying value used for structural hash/equality.

        N)Ú__init_handle_by_constructor__r   r
   )r"   r   r   r   r   Ú__init__Õ   s   	zStructuralKey.__init__c                 C  s
   | j d@ S )zReturn cached structural hash.r   )r    r!   r   r   r   Ú__hash__à   s   
zStructuralKey.__hash__Úotherr   c                 C  s   t |tƒo
t | |¡S )zCompare by structural equality.)Ú
isinstancer
   r   ÚStructuralKeyEqual)r"   r(   r   r   r   Ú__eq__æ   s   zStructuralKey.__eq__N)r   r   )r   r   r   r$   )r   r   )r(   r   r   r   )
Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r   r#   r&   r'   r+   r   r   r   r   r
   ¤   s   
 (


r
   N)FF)
r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   )
r   r   r   r   r   r   r   r   r   r   )r/   Ú
__future__r   Útypingr   r   Úaccess_pathr   Ú r   Úcorer   Úregistryr	   Ú__all__r   r   r   r
   r   r   r   r   Ú<module>   s"   	ÿ2ÿ/ÿ