o
    Xi                     @  sd   d Z ddlmZ dgZddlZddlZddlZee	Z
dd	d
ZG dd dejjZdddZdS )zEAdd default attributes to nodes that are missing optional attributes.    )annotationsAddDefaultAttributesPassNattr_defonnx.defs.OpSchema.Attributereturnboolc                 C  s   t | jo| jjtjjkS )z;Check if an attribute definition has a valid default value.)r   default_valuetypeonnxAttributeProto	UNDEFINED)r    r   \/home/ubuntu/.local/lib/python3.10/site-packages/onnx_ir/passes/common/default_attributes.py_has_valid_default   s   r   c                   @  s   e Zd ZdZd	ddZdS )
r   a^  Add default values for optional attributes that are not present in nodes.

    This pass iterates through all nodes in the model and for each node:
    1. Gets the ONNX schema for the operator
    2. For each optional attribute with a default value in the schema
    3. If the attribute is not present in the node, adds it with the default value
    modelir.Modelr   ir.passes.PassResultc                 C  s|   d}t j|jD ]}t||jjrd}q	|j D ]}t j|D ]}t||jjr-d}q"q|r6t	d t j
j||dS )z5Main entry point for the add default attributes pass.FTz,AddDefaultAttributes pass modified the model)modified)ir	traversalRecursiveGraphIteratorgraph_add_default_attributes_to_nodeopset_imports	functionsvaluesloggerinfopasses
PassResult)selfr   r   nodefunctionr   r   r   call$   s   
zAddDefaultAttributesPass.callN)r   r   r   r   )__name__
__module____qualname____doc__r#   r   r   r   r   r      s    r!   ir.Noder   dict[str, int]c           	      C  s   | j dur	| j }n| j|v r|| j }n
td| j|  dS ztjj| j|| jd}W n tjjy=   t	d|  Y dS w d}|j
 D ])\}}|jsQ|| j
v rRqEt|sWqE|j}tj|}|| j
|< t	d||  d}qE|S )zBAdd default attributes to a single node. Returns True if modified.Nz9OpSet version for domain '%s' not found. Skipping node %sF)domainz<Schema not found for %s, skipping default attribute additionz'Added default attribute '%s' to node %sT)versionr*   r   warningr
   defs
get_schemaop_typeSchemaErrordebug
attributesitemsrequiredr   r   r   serdedeserialize_attribute)	r!   r   opset_version	op_schemar   	attr_namer   default_attr_protodefault_attrr   r   r   r   9   s>   


r   )r   r   r   r   )r!   r(   r   r)   r   r   )r'   
__future__r   __all__loggingr
   onnx_irr   	getLoggerr$   r   r   r   InPlacePassr   r   r   r   r   r   <module>   s   

