o
    I&i                     @   sr  U d dl Z d dlmZmZmZmZmZmZmZ d dl	Z	d dl
mZ d dlmZmZmZmZmZmZmZmZ i Zeeee f ed< e	jjdd Zde	jjfd	d
Z		d!ddddddee dee dee dee dee dee deddfddZdede	jj defddZ!dede	jj dee	j"j# fddZ$dede	jj defddZ%dede	jj dee	j& fdd Z'dS )"    N)AnyDictIterableListOptionalTupleType)ExportedProgram)_register_pytree_nodeContextDumpableContextFlattenFuncFromDumpableContextFnToDumpableContextFntree_flattenUnflattenFunc(SERIALIZED_DATACLASS_TO_PYTHON_DATACLASSc                 O   s$   t |\}}t| j| j| jd| S )N)range_constraintsequality_constraints)r   "_check_input_constraints_for_graphgraphr   r   )selfargskwargsZ	flat_args_ r   >C:\wamp64\www\opt\env\Lib\site-packages\torch/_export/utils.py!_check_input_constraints_pre_hook   s   r   r   c                    s"   ddl m   fdd}|S )Nr   )'_AddRuntimeAssertionsForConstraintsPassc                     sz   t ji t j }jD ]}|jdkrq|j|j}|j	|_	q|j
d  |}|d us4J |j}||   d S )Nplaceholderr   )torchfxZGraphModuleGraphnodesopr   r   namemetaoutputZgraph_module)r   Z_assertion_graphpZnew_pZ_assertion_graph_resr   r   r   r   r   r   inner)   s    


z1_check_input_constraints_for_graph.<locals>.inner)Z@torch._export.passes.add_runtime_assertions_for_constraints_passr   )r   r   r   r*   r   r)   r   r   "   s   r   F)serialized_type_nameto_dumpable_contextfrom_dumpable_contextreturn_none_fieldscls
flatten_fnunflatten_fnr+   r,   r-   r.   returnc                   s  t  sJ d   j d j  t< dtdttt tf f fdd}dt	t dtdtfd	d
}dtdt
ffdd}	dt
dtfdd}
|d urR|n|}|d urZ|n|}|d u |d u A rltd  d|d urr|n|	}|d urz|n|
}t |||||d d S )Nz7Only dataclasses can be registered with this function: .objr2   c                    sj   g }g }g }t | D ]"}|jt| |j}}|d usr(|| || q|| q| ||ffS N)dataclassesfieldsr%   getattrappend)r4   Z	flattened
flat_names
none_namesfr%   val)r/   r.   r   r   default_flatten_fnN   s   
z=register_dataclass_as_pytree_node.<locals>.default_flatten_fnvaluescontextc                 S   s0   |\}}}|di t t|| dd |D S )Nc                 S   s   i | ]}|d qS r5   r   ).0kr   r   r   
<dictcomp>]   s    zSregister_dataclass_as_pytree_node.<locals>.default_unflatten_fn.<locals>.<dictcomp>r   )dictzip)r?   r@   typr:   r;   r   r   r   default_unflatten_fn[   s   
&z?register_dataclass_as_pytree_node.<locals>.default_unflatten_fnc                    s    | d | d fS )N      r   )r@   )serialized_typer   r   default_to_dumpable_context_   s   zFregister_dataclass_as_pytree_node.<locals>.default_to_dumpable_contextdumpable_contextc                 S   s   t | d  | d | d fS )Nr   rH   rI   )r   )rL   r   r   r   default_from_dumpable_contextb   s   
zHregister_dataclass_as_pytree_node.<locals>.default_from_dumpable_contextz7Both to_dumpable_context and from_dumpable_context for z must be None or registered.)r+   r,   r-   )r6   is_dataclass
__module____qualname__r   r   r   r   r   r   r   
ValueErrorr
   )r/   r0   r1   r+   r,   r-   r.   r>   rG   rK   rM   r   )r/   r.   rJ   r   !register_dataclass_as_pytree_node=   sB   
$

rR   programnodec                 C      |j | jjv S )zM
    Checks if the given node is a parameter within the exported program
    )r%   graph_signatureinputs_to_parametersrS   rT   r   r   r   is_param      rY   c                 C   &   t | |r| jj|j }| j| S dS )z
    Returns the parameter associated with the given node in the exported program.
    Returns None if the node is not a parameter within the exported program
    N)rY   rV   rW   r%   
state_dict)rS   rT   Zparameter_namer   r   r   	get_param      
	
r]   c                 C   rU   )zJ
    Checks if the given node is a buffer within the exported program
    )r%   rV   inputs_to_buffersrX   r   r   r   	is_buffer   rZ   r`   c                 C   r[   )z
    Returns the buffer associated with the given node in the exported program.
    Returns None if the node is not a buffer within the exported program
    N)r`   rV   r_   r%   r\   )rS   rT   Zbuffer_namer   r   r   
get_buffer   r^   ra   )NN)(r6   typingr   r   r   r   r   r   r   r    Ztorch._exportr	   Ztorch.utils._pytreer
   r   r   r   r   r   r   r   r   str__annotations__Z_dynamodisabler   r!   r"   r   boolrR   NoderY   nn	Parameterr]   r`   ZTensorra   r   r   r   r   <module>   sf   
 $(
	
	
J

