
    fPi                     H    d Z ddlZddlmZmZ  G d dej                  ZdS )zDefine SpaceToDepth fusion.    N   )fusions
onnx_modelc                        e Zd ZdZdej        f fdZdej        de	e
eej                 f         de	e
ej        f         fdZdej        de	e
eej                 f         de	e
ej        f         fdZ xZS )	FusionSpaceToDepthzFusion for SpaceToDepth.modelc                 N    t                                          |dd           dS )zXInitialize.

        Args:
            model: An onnx_model.ONNXModel instance.
        SpaceToDepthReshapeN)super__init__)selfr   	__class__s     /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/onnxruntime/quantization/execution_providers/qnn/fusion_spacetodepth.pyr   zFusionSpaceToDepth.__init__   s'     		:::::    nodeinput_name_to_nodesoutput_name_to_nodec           	          |}fd} ||d          x}F ||d          x}8 ||d          x}* ||d          x}	 ||	d          x}
 ||
d          x}dS  fd} ||j         d                   x}` ||j        d                   x}H ||j        d                   x}0 ||	j        d                   x} ||j        d                   x}dS                      |d          }                     |d          }                     |
d          }t          |          d	k    sLt          |          d
k    s9t          |          d	k    s&t          |          dk    st          |          d	k    rdS |\  }}}}|d         }|||||z  |||z  |gk    sX|g dk    sP|||||z  ||z  z  |dz  gk    s:|g dk    s2||||dz  ||z  ||z  gk    s|g dk    s|||dz  |z  ||z  ||z  gk    rdS  j                            |||||	|
|g           t          j                             j	         
                                |j         d         g|j        d         g|          } j                            |           dS )at  Fuse for early version of YOLO.

        Pattern:

                |     [N, C, H, W]
             Reshape
                |     [N, C, H/blk, blk, W/blk, blk]
            Transpose
                |     [N, C, H/blk, W/blk, blk, blk]
             Reshape
                |     [N, C, H/blk * W/blk, blk * blk]
            Transpose
                |     [N, C, blk * blk, H/blk * W/blk]
             Reshape
                |     [N, C, blk * blk, H/blk, W/blk]
            Transpose
                |     [N, blk * blk, C, H/blk, W/blk]
             Reshape
                |     [N, blk * blk * C, H/blk, W/blk]

        This sequence can be fused into a single SpaceToDepth with blocksize `blk`. Note that unlike DepthToSpace
        supporting DCR or CRD mode, SpaceToDepth only supports DCR mode in its latest opset version (13), which matches
        the pattern here.
        c                     | j         d         vrdS | j         d                  }t          |          dk    s|d         j        |k    rdS |d         S )zGet target child of given node.r   N   )outputlenop_type)parent_nodetarget_op_typechildrenr   s      r   get_target_childz7FusionSpaceToDepth._fuse_yolo.<locals>.get_target_child8   s_    !!$,???t*;+=a+@AH8}}q  HQK$7>$I$ItA;r   	TransposeNr   Fc                 v    j                             |           }|sdS                     |          }|sdS |S )z Get shape for given tensor name.N)r   get_tensor_typetensor_shape_to_list)tensor_nametensor_typetensor_shaper   s      r   get_tensor_shapez7FusionSpaceToDepth._fuse_yolo.<locals>.get_tensor_shapeM   sK    *44[AAK t44[AAL tr   r   perm         r   )r   r      r(   r   r*   r+   )r   r   r   r+   )r   r+   r   r   r(   )nameinputsoutputs	blocksizeT)inputr   get_node_attributer   nodes_to_removeextendonnxhelper	make_nodefused_op_typecreate_unique_node_namenodes_to_addappend)r   r   r   r   reshape_node1r   transpose_node1reshape_node2transpose_node2reshape_node3transpose_node3reshape_node4r&   input_shapereshape_shape1reshape_shape2reshape_shape3reshape_shape4transpose_perm1transpose_perm2transpose_perm3batchchannelheightwidthr/   s2d_nodes   ` `                        r   
_fuse_yolozFusionSpaceToDepth._fuse_yolo   s   < 		 		 		 		 		 !1 0 L LL_U!1!1/9!M!MMV#3#3M;#O#OOX!1!1/9!M!MMV#3#3M;#O#OOX!1!1/9!M!MMV5
	  
	  
	  
	  
	  -,]-@-CDDD[M"2"2=3G3J"K"KKT"2"2=3G3J"K"KKT"2"2=3G3J"K"KKT"2"2=3G3J"K"KKT511/6JJ11/6JJ11/6JJ !!>""a''>""a''>""a''>""a''5 )4%w"1%	ugv/BIuXaOaclmmm"4"4"444%6Y3F5T]K]2^`ikl`l!mmm,,,..%)Q,)@SUZ^gUg!hhh///11%A)?9ATV[_hVh!iii5##
	
 
	
 
	
 ;((--//!'*+")!,- ) 
 
 	  ***tr   c                 4    |                      |||           dS )a\  Fuse a sequence of Reshape and Transpose nodes into a single SpaceToDepth node.

        Args:
            node: An onnx.NodeProto matching the specified search type (i.e., Reshape).
            input_name_to_nodes: A dict mapping tensor name to consumed nodes.
            output_name_to_node: A dict mapping tensor name to produced node.
        N)rO   )r   r   r   r   s       r   fusezFusionSpaceToDepth.fuse   s"     	13FGGGGGr   )__name__
__module____qualname____doc__r   	ONNXModelr   r4   	NodeProtodictstrlistrO   rQ   __classcell__)r   s   @r   r   r      s        "";j2 ; ; ; ; ; ;{n{ "#tDN';";<{ "#t~"56	{ { { {zHnH "#tDN';";<H "#t~"56	H H H H H H H Hr   r   )rU   r4    r   r   Fusionr    r   r   <module>r_      sx    " !  # # # # # # # #UH UH UH UH UH UH UH UH UH UHr   