o
    Zh                  
   @   s   d dl mZmZmZ d dlmZmZ deeef dedeeef fddZ	ded	ed
eee
 eee
 egef defddZdS )    )AnyCallableOptional)ContextTreeSpecuser_kwargsspecreturnc                 C   s   |j tu sJ |jdksJ |jd }|j tu sJ t| t|jkr0tdt|  d|j i }|jD ]}| | ||< q5|S )aX  Reorder user-provided kwargs to match the order in `spec`. `spec` is
    expected to be the in_spec of an exported program, i.e. the spec that
    results from flattening `(args, kwargs)`.

    We need this to provide consistent input ordering, such so that users can
    pass in foo(a=a, b=b) OR foo(b=b, a=a) and receive the same result.
          z>Ran into a kwarg keyword mismatch: Got the following keywords z but expected )	typetupleZnum_childrenchildren_specsdictsetcontext
ValueErrorlist)r   r   Zkwargs_specZreordered_kwargskw r   G/var/www/auris/lib/python3.10/site-packages/torch/export/_tree_utils.pyreorder_kwargs   s    	

r   spec1spec2equivalence_fnc                 C   s`   || j | j|j |jsdS t| jt|jkrdS t| j|jD ]\}}t|||s- dS q dS )a,  Customizable equivalence check for two TreeSpecs.

    Arguments:
        spec1: The first TreeSpec to compare
        spec2: The second TreeSpec to compare
        equivalence_fn: A function to determine the equivalence of two
            TreeSpecs by examining their types and contexts. It will be called like:

                equivalence_fn(spec1.type, spec1.context, spec2.type, spec2.context)

            This function will be applied recursively to all children.

    Returns:
        True if the two TreeSpecs are equivalent, False otherwise.
    FT)r   r   lenr   zipis_equivalent)r   r   r   Zchild_spec1Zchild_spec2r   r   r   r   !   s   r   N)typingr   r   r   Ztorch.utils._pytreer   r   r   strr   r   boolr   r   r   r   r   <module>   s    &