o
    ;ήc                     @   s4   d dl Z ddlmZmZ de jfddZdd ZdS )	    N   )get_producer_consumer_mapsiterate_graph_per_graph_funcgraphc                    s  |d }|d }|d }g }t dd  jD ]&}||v r || ng }t|dkr<t fdd|D s7td	|| q|rG|rEtd
d S |rd}t }	t	dd  j
D }
 jD ]m}|	j| ||v r|j
d |
v }|rsdnd}t|| |d  }t|D ]E\}}t }|| | jd| 7  _|j
d }| d| }||j
d< t|jD ]\}}||kr||j|< q|	j| |d7 }qq\ jd d =  j|	j |  d S d S )Nupdated_graphsnode_to_consumersvalidate_updatesc                 S   s
   | j dkS )NDequantizeLinear)op_typenode r   T/tmp/pip-target-vg8gfxp4/lib/python/onnxruntime/tools/qdq_helpers/qdq_model_utils.py<lambda>   s   
 z=_duplicate_dq_nodes_with_multiple_consumers.<locals>.<lambda>   c                 3   s    | ]}| j v V  qd S )Nr   ).0consumerr   r   r   	<genexpr>   s    z>_duplicate_dq_nodes_with_multiple_consumers.<locals>.<genexpr>zXDequantizeLinear node output is consumed by a subgraph. This is not currently supported.z?Graph still has DequantizeLinear nodes with multiple consumers.r   c                 S   s   g | ]}|j qS r   )name)r   outputr   r   r   
<listcomp>'   s    z?_duplicate_dq_nodes_with_multiple_consumers.<locals>.<listcomp>z/qdq_utils_dup_)filterr   lenall
IndexErrorappend
ValueErroronnx
GraphProtosetr   list	enumerate	NodeProtoCopyFromr   inputextend)r   kwargsr   r   r   nodes_to_updater   	consumersdup_idx	new_graphgraph_outputsis_graph_output	start_idxidxr   	duplicateorig_output
new_output	input_idx
input_namer   r   r   +_duplicate_dq_nodes_with_multiple_consumers	   sZ   






r5   c                 C   sp   t | j\}}g }t| jt|d|d |r6g }t | j\}}t| jt|d|d tj|  tj| }dS dS )a-  
    Update a model if any DequantizeLinear nodes have multiple consumers.
    The QDQ node unit processing is overly complicated if this is the case, as the DQ node would be in multiple units,
    and the units may end up in different partitions at runtime.
    :param model: QDQ model to update
    F)r   r   r   TN)	r   r   r   r5   r   checkercheck_modelshape_inferenceinfer_shapes)modelnode_to_producersr   r   _r   r   r   $fix_dq_nodes_with_multiple_consumersM   s,   	r=   )r   onnx_model_utilsr   r   r   r5   r=   r   r   r   r   <module>   s   D