o
    XiQ                     @  s   d Z ddlmZ ddlZddlmZmZmZ ddlm	Z	m
Z
 ddlmZmZ ddlmZ e
dZed	Zed
ZedZedd.ddZd/ddZd0d d!Zd1d#d$Zd2d&d'Zd3d(d)Zd4d,d-ZdS )5a  Wrappers for IR classes to enable journaling.

This module provides wrapper functions that enable journaling for ONNX IR classes.
The wrappers are applied when a Journal context is active, and they record operations
to the journal for debugging and analysis purposes.
    )annotationsN)AnyCallableTypeVar)Concatenate	ParamSpec)_core_graph_containers)_journaling_P_T_SelfT_ValueTdetails_funcjournal_journaling.Journaloriginal_init'Callable[Concatenate[_SelfT, _P], None]r   Callable[[_SelfT], str | None]returnc                  s    t d fd	d
}|S )zGeneric wrapper factory for __init__ methods.

    Args:
        journal: The journal to record to.
        original_init: The original __init__ method.
        details_func: A function that takes self and returns details string.
    selfr   args_P.argskwargs	_P.kwargsr   Nonec                   s.   | g|R i | j | d | d d S )Ninitdetailsrecordr   r   r   r   r   r    P/home/ubuntu/.local/lib/python3.10/site-packages/onnx_ir/journaling/_wrappers.pywrapper.   s   z_init_wrapper.<locals>.wrapperN)r   r   r   r   r   r   r   r   	functoolswraps)r   r   r   r&   r$   r#   r%   _init_wrapper    s   r*   original_setter!Callable[[_SelfT, _ValueT], None]property_namestr	operationc                   s"   t d
 fdd}|S )a*  Generic wrapper factory for property setters.

    Args:
        journal: The journal to record to.
        original_setter: The original setter method.
        property_name: The private attribute name (e.g., "_name").
        operation: The operation name for the journal (e.g., "set_name").
    r   r   valuer   r   r   c                   s2   t | } j| |d|d | | d S )N -> r   getattrr!   )r   r0   	old_valuer   r/   r+   r-   r$   r%   r&   E   s   
z _setter_wrapper.<locals>.wrapperN)r   r   r0   r   r   r   r'   )r   r+   r-   r/   r&   r$   r5   r%   _setter_wrapper6   s   r6   original_method%Callable[Concatenate[_SelfT, _P], _T]-Callable[Concatenate[_SelfT, _P], str | None]c                  s"   t d fd	d
}|S )a  Generic wrapper factory for methods.

    Args:
        journal: The journal to record to.
        original_method: The original method.
        operation: The operation name for the journal.
        details_func: A function that takes (self, *args, **kwargs) and returns details.
    r   r   r   r   r   r   r   r   c                   s8   j |  | g|R i |d | g|R i |S Nr   r    r"   r   r   r/   r7   r$   r%   r&   ^   s   "z _method_wrapper.<locals>.wrapperNr   r   r   r   r   r   r   r   r'   )r   r7   r/   r   r&   r$   r;   r%   _method_wrapperN   s   r=   target_attrc                  s$   t d fd	d
}|S )a  Generic wrapper factory for container methods that record on a parent object.

    Args:
        journal: The journal to record to.
        original_method: The original method.
        operation: The operation name for the journal.
        target_attr: The attribute name to get the target object from self (e.g., "_graph").
        details_func: A function that takes (self, *args, **kwargs) and returns details.
    r   r   r   r   r   r   r   r   c                   sB   t | }j| | g|R i |d | g|R i |S r:   r2   )r   r   r   targetr   r   r/   r7   r>   r$   r%   r&   x   s   
"z*_container_method_wrapper.<locals>.wrapperNr<   r'   )r   r7   r/   r>   r   r&   r$   r@   r%   _container_method_wrapperf   s   rA   dict[str, Any]c                  C  s  i dt jjdt jjdt jjjdt jjjdt jjjdt jjjdt jj	jdt jj
d	t jjd
t jjdt jjdt jjjdt jjdt jjjdt jjjdt jjjdt jjji dt jjdt jjdt jjdt jjdt jjdt jjdt jjdt jjdt jjdt jjdt jjdt jjdt jjjdt jjjd t jj	jd!t jjd"tj jtj jtj j!tj j"tj jtj j#tj j$tj%j$tj%j&tj'j$d#	} | S )$zObtain original methods for later restoration.

    Returns:
        A dictionary mapping method names to their original implementations.
    TensorBase.__init__Node.__init__Node.name.fsetNode.domain.fsetNode.version.fsetNode.op_type.fsetNode.overload.fsetNode.resize_inputsNode.prependNode.appendNode.resize_outputsNode.graph.fsetValue.__init__Value.name.fsetValue.type.fsetValue.shape.fsetValue.const_value.fsetValue.replace_all_uses_withValue.merge_shapesGraph.__init__Graph.register_initializerGraph.appendGraph.extendGraph.removeGraph.insert_afterGraph.insert_before
Graph.sortModel.__init__Function.__init__Function.name.fsetFunction.domain.fsetFunction.overload.fsetAttr.__init___GraphIO.append)	_GraphIO.extend_GraphIO.insert_GraphIO.pop_GraphIO.remove_GraphIO.clear_GraphIO.__setitem__GraphInitializers.__setitem__GraphInitializers.__delitem__Attributes.__setitem__)(r   
TensorBase__init__Nodenamefsetdomainversionop_typeoverloadresize_inputsprependappendresize_outputsgraphValuetypeshapeconst_valuereplace_all_uses_withmerge_shapesGraphregister_initializerextendremoveinsert_afterinsert_beforesortModelFunctionAttrr	   _GraphIOinsertpopclear__setitem__GraphInitializers__delitem__
Attributesoriginal_methodsr$   r$   r%   get_original_methods   s   




	





!#
$
%
&(*+8r   c                 C  s  t  }t| |d dd dtj_t| |d tj_ttjjjt	| |d ddtj_ttjj
jt	| |d	 d
dtj_
ttjjjt	| |d ddtj_ttjjjt	| |d ddtj_ttjjjt	| |d ddtj_t| |d ddd dtj_t| |d ddd dtj_t| |d ddd dtj_t| |d dd d dtj_ttjjjt| |d! d"d#d dtj_t| |d$ tj_ttjjjt	| |d% ddtj_ttjjjt	| |d& d'd(tj_ttjjjt	| |d) d*d+tj_ttjjjt	| |d, d-d.tj_t| |d/ d0dud2ddtj_t| |d3 d4d5d dtj_t| |d6 d7d dtj_t| |d8 d9d:d dtj_t| |d; dd<d dtj_t| |d= d>d?d dtj_t| |d@ dAdudBddtj_t| |dC dDdEd dtj_t| |dF dGdHd dtj_t| |dI dJdKd dtj_ t| |dL tj!_t| |dM tj"_ttj"jjt	| |dN ddtj"_ttj"j
jt	| |dO d
dtj"_
ttj"jjt	| |dP ddtj"_t| |dQ tj#_t$| |dR dSdTdUd dVt%j&_t$| |dW dXdTdYd dVt%j&_t$| |dZ d[dTd\d dVt%j&_'t$| |d] d^dTdvd`ddVt%j&_(t$| |da dbdTdcd dVt%j&_t$| |dd dedTdfd dVt%j&_)t$| |dg dhdTdid dVt%j&_*t$| |dj dkdTdld dVt%j+_*t$| |dm dndTdod dVt%j+_,t$| |dp dqdrdsd dVt%j-_*|S )wzWrap IR classes with journaling-enabled versions.

    This function replaces methods on IR classes with wrapped versions that
    record operations to the active journal.
    rC   c                 S     d S Nr$   r   r$   r$   r%   <lambda>       z!wrap_ir_classes.<locals>.<lambda>r   rD   rE   _nameset_namerF   _domain
set_domainrG   _versionset_versionrH   _op_typeset_op_typerI   	_overloadset_overloadrJ   rw   c                 S     t | j d| S Nr1   )len_inputsr   new_sizer$   r$   r%   r          rK   rx   c                 S     t |S r   reprr   nodesr$   r$   r%   r          rL   ry   c                 S  r   r   r   r   r$   r$   r%   r     r   rM   rz   c                 S  r   r   )r   _outputsr   r$   r$   r%   r     r   rN   	set_graphc                 S  s   t |tjr
|jS |S r   )
isinstancer   r   rq   r   r0   r$   r$   r%   r     s   rO   rP   rQ   _typeset_typerR   _shape	set_shaperS   _const_valueset_const_valuerT   r   Fc                 S     d|d| S )Nzreplacement=z, replace_graph_outputs=r$   )r   replacementreplace_graph_outputsr$   r$   r%   r   0  s   rU   r   c                 S  s   d| j d|S )Nz	original=z, other=)r   r   otherr$   r$   r%   r   8  s    rV   c                 S  s
   t | jS r   )r.   rq   r   r$   r$   r%   r   =     
 rW   r   c                 S  r   r   r   r   r$   r$   r%   r   C  r   rX   c                 S  r   r   r   )r   noder$   r$   r%   r   I  r   rY   r   c                 S  r   r   r   r   r$   r$   r%   r   O  r   rZ   r   c                 S  r   )Nznodes=z, safe=r$   )r   r   safer$   r$   r%   r   U      r[   r   c                 S     d|d|S Nznode=z, new_nodes=r$   r   r   	new_nodesr$   r$   r%   r   [  r   r\   r   c                 S  r   r   r$   r   r$   r$   r%   r   a  r   r]   r   c                 S  r   r   r$   r   r$   r$   r%   r   g  r   r^   r_   r`   ra   rb   rc   rd   	append_io_graphc                 S     d| j j d|S N[z] 	__class____name__r   itemr$   r$   r%   r     r   )r>   r   re   	extend_ioc                 S  r   r   r   r   r$   r$   r%   r     r   rf   	insert_ioc                 S  s   d| j j d|S r   r   r   ir   r$   r$   r%   r     r   rg   pop_ioc                 S  s   d| j j d| S )Nr   ] index=r   )r   r   r$   r$   r%   r     r   rh   	remove_ioc                 S  r   r   r   r   r$   r$   r%   r     r   ri   clear_ioc                 S  s   d| j j dS )Nr   ]r   r   r$   r$   r%   r     r   rj   set_ioc                 S  s   d| j j d| d|S )Nr   r   z, item=r   r   r$   r$   r%   r     s   rk   set_initializerc                 S  r   Nkey=z, value=r$   r   keyr0   r$   r$   r%   r     r   rl   delete_initializerc                 S  s
   d|S )Nr   r$   )r   r   r$   r$   r%   r     r   rm   set_attribute_ownerc                 S  r   r   r$   r   r$   r$   r%   r     r   N)F)r   ).r   r*   r   rn   ro   rp   propertyrq   fgetr6   rs   rt   ru   rv   r=   rw   rx   ry   rz   r{   r|   r}   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   rA   r	   r   r   r   r   r   r   r   r   )r   r   r$   r$   r%   wrap_ir_classes   s  




	


















	
r   r   r   c                 C  s  | d t j_| d t j_tt jjj| d t j_tt jjj| d t j_tt jjj| d t j_tt jj	j| d t j_	tt jj
j| d t j_
| d t j_| d	 t j_| d
 t j_| d t j_tt jjj| d t j_| d t j_tt jjj| d t j_tt jjj| d t j_tt jjj| d t j_tt jjj| d t j_| d t j_| d t j_| d t j_| d t j_| d t j_| d t j_| d t j_| d t j_| d t j_| d t j_| d t j_| d t j_tt jjj| d t j_tt jjj| d t j_tt jj
j| d  t j_
| d! t j_| d" t j!_| d# t j!_| d$ t j!_"| d% t j!_#| d& t j!_| d' t j!_$| d( t j!_%| d) t j&_%| d* t j&_'| d+ t j(_%d,S )-z|Restore IR classes to their original implementations.

    This function undoes the wrapping done by wrap_ir_classes().
    rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   re   rf   rg   rh   ri   rj   rk   rl   rm   N))r   rn   ro   rp   r   rq   r   rs   rt   ru   rv   rw   rx   ry   rz   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$   r%   restore_ir_classes  s   r   )r   r   r   r   r   r   r   r   )
r   r   r+   r,   r-   r.   r/   r.   r   r,   )
r   r   r7   r8   r/   r.   r   r9   r   r8   )r   r   r7   r8   r/   r.   r>   r.   r   r9   r   r8   )r   rB   )r   r   r   rB   )r   rB   r   r   )__doc__
__future__r   r(   typingr   r   r   typing_extensionsr   r   onnx_irr   r	   onnx_ir.journalingr
   r   r   r   r   r   r*   r6   r=   rA   r   r   r   r$   r$   r$   r%   <module>   s*   



 
A  