Ë
    †\;j×L  ã                   óÂ   — d dl Z d dlZd dlZd dlmZ d dlmZmZmZ  e«       r e«       s
d dlm	Z
 d„ ZndZ
d„ Zg d¢Zd a G d„ d	«      Z	dd
„Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zy)é    N)Úcore)Ú	CUDAPlaceÚis_compiled_with_cudaÚis_compiled_with_rocm)Ú	CUDAGraphc                   ó   — y)NT© r	   ó    úbG:\00. PROJECTS\API\Inventory\templateJSON\kerjaOCR\Lib\site-packages\paddle/device/cuda/graphs.pyÚis_cuda_graph_supportedr      s   € Ør
   c                   ó   — y)NFr	   r	   r
   r   r   r   #   s   € Ør
   )ÚglobalÚthread_localÚrelaxedc                   ó4   — e Zd Zdd„Zd„ Zd„ Zd„ Zd„ Zd	d„Zy)
r   Nc                 óî   — t         €J d«       ‚d | _        |€4t        t        j                  j                  dd«      «      }t        |«      }|| _        |t        v sJ ‚t        j                  |«      | _
        y )NzFCUDA Graph is only supported on PaddlePaddle compiled with NVIDIA GPU.ÚFLAGS_selected_gpusr   )ÚCoreCUDAGraphÚ_graphÚintÚosÚenvironÚgetr   Ú_placeÚ	ALL_MODESÚindexÚ_mode)ÚselfÚplaceÚmodeÚ	device_ids       r   Ú__init__zCUDAGraph.__init__,   sq   € äÐ%ð	TàSó	TØ%ð ˆŒØˆ=ÜœBŸJ™JŸN™NÐ+@À!ÓDÓEˆIÜ˜iÓ(ˆEØˆŒØ”yÑ Ð Ð Ü—_‘_ TÓ*ˆ�
r
   c                 óX   — t        j                  | j                  | j                  «       y ©N)r   Úbegin_capturer   r   ©r   s    r   Úcapture_beginzCUDAGraph.capture_begin9   s   € Ü×#Ñ# D§K¡K°·±Õ<r
   c                 ó6   — t        j                  «       | _        y r$   )r   Úend_capturer   r&   s    r   Úcapture_endzCUDAGraph.capture_end<   s   € Ü#×/Ñ/Ó1ˆ�r
   c                 ó8   — | j                   j                  «        y r$   )r   Úreplayr&   s    r   r,   zCUDAGraph.replay?   s   € Ø�‰×ÑÕr
   c                 ó8   — | j                   j                  «        y r$   )r   Úresetr&   s    r   r.   zCUDAGraph.resetB   s   € Ø�‰×ÑÕr
   c                 ó
  — t        |t        t        f«      s|j                  }t	        j
                  |d¬«       t        j                  j                  |«      sJ d|› d�«       ‚|€d}| j                  j                  ||«       y )NT)ÚnameÚexist_okzThe dirname z should be a directoryiÿ  )
Ú
isinstanceÚstrÚbytesr0   r   ÚmakedirsÚpathÚisdirr   Úprint_to_dot_files)r   ÚdirnameÚflagss      r   r8   zCUDAGraph.print_to_dot_filesE   sv   € Ü˜'¤C¬ <Ô0Ø—l‘lˆGÜ
�‰˜¨4Õ0Ü�w‰w�}‰}Øô
ð 	:à˜'˜Ð"8Ð9ó	:ð 
ð ˆ=ØˆEØ�‰×&Ñ& w°Õ6r
   )Nr   r$   )	Ú__name__Ú
__module__Ú__qualname__r"   r'   r*   r,   r.   r8   r	   r
   r   r   r   +   s    „ ó+ò=ò2òòô	7r
   r   c                 ód  ‡ — |t         v sJ ‚t        j                  «       srddlm} t        t        «      }t        dz  a|dk(  rd}n&|dk(  rt        j                  «       }nt        d|«      ‚  ||dz   t        |«      z   dz   |z   «      ˆ fd„«      S dd	l
m} dd
lm}  |‰ «      }t        ‰ |«      r|j                  }	n|}	||	_        |dk(  r	d|	_        |S |dk(  rt        j                  «       |	_        |S t        ||«      r|j                  j"                  |	_        |S |j"                  |	_        |S )Nr   )Ú_cuda_graph_guardé   ÚdefaultÚnewzLmemory_pool should be one of default or new under static graph mode, but gotÚ;c                  ó   •—  ‰| i |¤ŽS r$   r	   )ÚargsÚkwargsÚfunctions     €r   Ú<lambda>z!wrap_cuda_graph.<locals>.<lambda>e   s   ø€ ¡(¨DÐ";°FÒ";r
   )Ú	to_static)ÚLayer)r   ÚpaddleÚin_dynamic_modeÚpaddle.base.frameworkr?   r3   Úcuda_graph_idr   Úgen_new_memory_pool_idÚ
ValueErrorÚ
paddle.jitrI   Ú	paddle.nnrJ   r2   ÚforwardÚ_cuda_graph_capture_modeÚ_cuda_graph_pool_id)
rG   r    Úmemory_poolr?   Úgraph_idÚmemory_pool_idrI   rJ   Únew_functionÚ	mock_funcs
   `         r   Úwrap_cuda_graphr[   Q   sQ  ø€ Ø”9ÑÐÐÜ×!Ñ!Ô#å;ô ”}Ó%ˆÜ˜ÑˆØ˜)Ò#Ø‰NØ˜EÒ!Ü*×AÑAÓC‰NäØ^Øóð ð
Ñ Ø�3‰Jœ˜^Ó,Ñ,¨sÑ2°XÑ=ó
ã
;ó=ð 	=õ %Ýá˜XÓ&€LÜ�(˜EÔ"Ø ×(Ñ(‰	à ˆ	Ø)-€IÔ&Ø�iÒØ()ˆ	Ô%ð Ðð 
˜Ò	Ü(5×(LÑ(LÓ(Nˆ	Ô%ð Ðô �k 5Ô)à×#Ñ#×7Ñ7ð Ô)ð
 Ðð -8×,KÑ,KˆIÔ)ØÐr
   c                 ó~  — | j                  |j                  «       | j                  |j                  «       | j	                  |j
                  «       | j                  |j                  «       | j                  |j                  «       | j                  |j                  «       | j                  |j                  «       y)zÕ
    copy var desc from src to dst

    :param dst: framework.VarDesc(cpp), dst var desc, cpp VarDesc instance
    :param src: framework.VarDesc(cpp), src var desc, cpp VarDesc instance
    :return: no return
    N)Ú	set_shapeÚshapeÚ	set_dtypeÚdtypeÚset_lod_levelÚ	lod_levelÚset_typeÚtypeÚset_persistableÚpersistableÚset_is_parameterÚis_parameterÚset_stop_gradientÚstop_gradient)ÚdstÚsrcs     r   Úcopy_var_descrm   ~   s€   € ð ‡M�M�#—)‘)ÔØ‡M�M�#—)‘)ÔØ×Ñ�c—m‘mÔ$Ø‡L�L�—‘ÔØ×Ñ˜Ÿ™Ô(Ø×Ñ˜×)Ñ)Ô*Ø×Ñ˜#×+Ñ+Õ,r
   c                 ó¸   — g }t        | j                  «      D ]-  \  }}||k  rŒ|j                  D ]  }|j                  |«       Œ Œ/ t	        t        |«      «      S )a1  
    find all inputs of ops after an idx, used to determine the logical output of a cuda graph section

    :param block: framework.Block, the original block
    :param begin_idx: int, from which idx (not include) to find the later ins
    :return: a list of inputs names for all ops behind begin_idx
    )Ú	enumerateÚopsÚinput_arg_namesÚappendÚlistÚset)ÚblockÚ	begin_idxÚinsÚidxÚopÚin_names         r   Úall_inputs_of_later_opr{   �   sW   € ð €CÜ˜UŸY™YÖ'‰ˆˆRØ�)ÒØØ×)Ô)ˆGØ�J‰J�wÕñ *ð (ô
 ”�C“‹>Ðr
   c                 óˆ  — t         j                  j                  «       }|j                  «       }|j                  «       }g }g }t        j
                  j                  «       }t        ||d   «      }	| D �]¸  }
|
j                  D ]È  }|j                  |«      }|j                  j                  |j                  j                  d«      «      }t        ||«       |j                  |«      dk(  r&|j                  |«      dk(  r|j                  |«       ŒŽ|	j                  |«      dk(  sŒ£|j                  |«      dkD  sŒ¸|j!                  |«       ŒÊ |
j"                  D ]y  }|j                  |«      }|j                  j                  |j                  j                  d«      «      }t        ||«       |j                  |«      dk(  sŒi|j                  |«       Œ{ |j                  j%                  «       }|j'                  |
j                  «       |j)                  ||
j+                  |«      «       �Œ» |j-                  «        |||gfS )aÞ  
    1. Construct a new program for corresponding section
    2. Find all the logical inputs and outputs of a program section

    :param section: list, one cuda graph section, list of ops
    :param origin_program: framework.Program, origin program
    :param section_idx: list, the section ops' idx corresponding to the cuda graph section, a list of idx
    :return: a new program for the cuda graph section
             the logical ins and outs of the cuda graph section
    éÿÿÿÿÚasciir   )rK   ÚstaticÚProgramÚglobal_blockr   Úop_proto_and_checker_makerÚkOpRoleAttrNamer{   rq   ÚvarÚdescr0   Úencoderm   Úcountrr   ÚremoveÚoutput_arg_namesÚ	append_opÚ	copy_fromÚ	_set_attrÚattrÚ_sync_with_cpp)ÚsectionÚorigin_programÚsection_idxÚprogramru   Úorigin_blockrw   ÚoutsÚop_role_attr_nameÚ	later_insry   rz   r„   Únew_var_descÚout_nameÚnew_op_descs                   r   Ú#construct_program_and_find_ins_outsrš       sØ  € ô �m‰m×#Ñ#Ó%€GØ× Ñ Ó"€EØ!×.Ñ.Ó0€LØ
€CØ€DÜ×7Ñ7×GÑGÓIÐÜ& |°[À±_ÓE€IäˆØ×)Ô)ˆGØ×"Ñ" 7Ó+ˆCØ Ÿ:™:Ÿ>™>¨#¯(©(¯/©/¸'Ó*BÓCˆLÜ˜,¨Ô,Ø�z‰z˜'Ó" aÒ'¨C¯I©I°gÓ,>À!Ò,Cð —
‘
˜7Õ#Ø—‘ Ó)¨QÓ.°4·:±:¸gÓ3FÈÓ3Jà—‘˜GÕ$ð *ð ×+Ô+ˆHØ×"Ñ" 8Ó,ˆCØ Ÿ:™:Ÿ>™>¨#¯(©(¯/©/¸'Ó*BÓCˆLÜ˜,¨Ô,à�z‰z˜(Ó# qÓ(ð —‘˜HÕ%ð ,ð —j‘j×*Ñ*Ó,ˆØ×Ñ˜bŸg™gÔ&Ø×ÑÐ/°·±Ð9JÓ1KÖLð5 ð8 ×ÑÔà�S˜$�KÐÐr
   c                 óT  — | j                  «       }g }g }d}g }g }g }g }d}	t        j                  j                  «       }
t	        t        j                  j
                  j                  «      }t	        t        j                  j
                  j                  «      }||z  }t        |j                  «      D �]   \  }}|j                  dk(  s|j                  dk(  r|j                  �J d«       ‚|j                  d«      r|j                  d«      rd}|j                  ��t        |j                  t        «      sJ d«       ‚|j                  j!                  d	«      }t#        |«      d
k(  sJ d«       ‚t	        |d   «      }||	k(  �rUt#        |«      dkD  �rt#        |«      t#        |«      k(  sJ d«       ‚|D ]º  }t	        |j                  |
«      «      |k(  xs t	        |j                  |
«      |k(  «      }|j                  dk(  xs |j                  dk(  }|s|sŒdg }g }t#        |«      t#        |«      k(  sJ d«       ‚t#        |«      dkD  r"|j%                  |«       |j%                  |«       g }g } n t'        t#        |«      «      D ]*  }|j%                  ||   «       |j%                  ||   «       Œ, g }g }|j%                  |«       |j%                  |«       �Œ#g }g }|}	t#        |«      t#        |«      k(  sJ d«       ‚t#        |«      dkD  r"|j%                  |«       |j%                  |«       |g}|g}�Œ|j%                  |«       |j%                  |«       �Œ£ t#        |«      t#        |«      k(  sJ d«       ‚t#        |«      dkD  r"|j%                  |«       |j%                  |«       |||fS )a  
    get all sections that should run under cuda graph and the corresponding idx

    :param program: framework.Program, the original program
    :return: A list of cuda graph sections and the corresponding ops' idx in the block.
             The program is under is test or not.
    Fr}   Úconditional_blockÚwhilez9Cuda graph not support conditional block op and while op.Úis_testTzcuda_graph_attr should be a strrC   é   zccuda graph attr should have three fields: cuda graph mode, cuda graph memory pool id, cuda graph idé   r   z@len of internal section should be equal with len of internal idxz-num of section's op is not equal with the idxz0num of section's op is not equal with num of idx)r�   r   r‚   rƒ   r   ÚOpRoleÚLossÚBackwardro   rp   rd   Ú_cuda_graph_attrÚhas_attrr�   r2   r3   ÚsplitÚlenrr   Úrange)r’   ru   Úcuda_graph_sectionsÚsections_idxrž   Úinternal_sectionÚinternal_idxÚcurrent_sectionÚcurrent_idxÚcurrent_cuda_graph_idr•   Úloss_op_roleÚbackward_op_roleÚloss_grad_op_rolerx   ry   Úcuda_graph_attrsÚlocal_cuda_graph_idÚinternal_opÚloss_relatedÚsub_block_relatedÚis                         r   Úget_cuda_graph_sectionsr¹   Ô   s  € ð × Ñ Ó"€EØÐØ€LØ€Gð ÐØ€Là€OØ€KØÐÜ×7Ñ7×GÑGÓIÐÜ”t×6Ñ6×=Ñ=×BÑBÓC€LÜœ4×:Ñ:×AÑA×JÑJÓKÐØ$Ð'7Ñ7Ðä˜UŸY™Y×'‰ˆˆRØ�7‰7Ð)Ò)¨R¯W©W¸Ò-?à×#Ñ#Ð+ðKàJóKØ+à�;‰;�yÔ! b§g¡g¨iÔ&8ØˆGà×ÑÑ*ÜØ×#Ñ#¤Sôð 1à0ó1ð ð  "×2Ñ2×8Ñ8¸Ó=ÐÜÐ'Ó(¨AÒ-ð ðLóÐ-ô #&Ð&6°qÑ&9Ó":ÐØ"Ð&;Ó;ÜÐ'Ó(¨1Ó,ÜÐ/Ó0´CØ$ó5ò ð ZàYóZð ó (8˜ä × 0Ñ 0Ð1BÓ CÓDØ+ñ,ò(ô !Ø(×-Ñ-Ð.?Ó@Ø0ñ1óð %ð ŸG™GÐ':Ñ:ÒP¸b¿g¹gÈÑ>Pð *ñ (Ò+<ð 02Ð,Ø+-˜Lä#& Ó#7¼3Ø +ó<ò $ð OàNóOð $ô  # ?Ó3°aÒ7à 3× :Ñ :¸?Ô KØ ,× 3Ñ 3°KÔ @Ø.0˜OØ*,˜KÙ!ðC (8ôF #¤3Ð'7Ó#8Ö9˜Ø'×.Ñ.Ð/?ÀÑ/BÔCØ#×*Ñ*¨<¸©?Õ;ð :ð $&Ð Ø!�Ø×&Ñ& rÔ*Ø×"Ñ" 3Ö'ð $&Ð Ø!�à'ð &ô ˜?Ó+¬sØó0ò ð FàEóFð ô �Ó'¨!Ò+à'×.Ñ.¨Ô?Ø ×'Ñ'¨Ô4Ø#% $�Ø"˜e’ð ×#Ñ# BÔ'Ø×Ñ Ö$ðk (ôp ˆÓ¤3Øó$ò ð :à9ó:ð ô ˆ?Ó˜aÒà×"Ñ" ?Ô3Ø×Ñ˜KÔ(à ¨gÐ5Ð5r
   c                 ó\  — | d   }| d   }|d   }	|j                  «       }
t        |«      D ]  }|
j                  |d¬«       Œ d}d}|D ]>  }|j                  €Œ|j                  j	                  d«      }|d   }t        |d   «      } n |�|€J d«       ‚|
j                  dt        |«      z   t        j                  j                  j                  d	d	¬
«      }|
j                  dt        |«      z   t        j                  j                  j                  d	d	¬
«      }t        j                  j                  || «      }|
j!                  |	dd|i|||dœ|j                  «       dt#        |j                  «       j$                  «      ||||d|j                  «       |j                  «       dœ
¬«       y)a÷  
    Use section_program and ins_and_outs to initialize a run_program_op,
    and replace the section_idx marks ops in the origin program.

    :param ins_and_outs: list, the logical ins and outs of the section program
    :param section_program: framework.Program, the partial program need to run under cuda graph
    :param section_idx: list, the idx need to be removed from origin program
    :param origin_program: framework.Program, the origin program
    :param cuda_graph_section: list, the ops in current sections, used to get the mode, memory pool id and is_test
    :param order: int, the order of current section, used to create unique cuda graph var
    :param is_test: bool, the program is running under is_test or not
    :return: no return
    r   r@   F)ÚsyncNrC   z>mode and memory pool id should be specified in cuda graph attrÚcuda_graph_T)r0   rd   rf   rj   Úprogram_out_scope_Úrun_programÚX)ÚOutÚOutScoper   )
r�   Ústart_op_indexÚend_op_indexrž   Ú
program_idÚcuda_graph_capture_modeÚcuda_graph_pool_idÚuse_interpretorcoreÚforward_global_blockÚbackward_global_block)rd   ÚinputsÚoutputsÚattrs)r�   ÚreversedÚ
_remove_opr¤   r¦   r   Ú
create_varr3   r   ÚVarDescÚVarTypeÚRAWÚSTEP_SCOPESrK   ÚutilsÚ_hash_with_idÚ
_insert_opr§   rp   )Úins_and_outsÚsection_programr‘   r�   Úcuda_graph_sectionÚorderrž   rw   r”   Ú
insert_idxr“   rx   r    rX   ry   rÌ   Úcuda_graph_varÚout_scope_varrÄ   s                      r   Úreplace_cuda_graph_sectionrÞ   P  sÓ  € ð, �q‰/€CØ˜‰?€DØ˜Q‘€JØ!×.Ñ.Ó0€Lä˜Ö$ˆà×Ñ ¨%ÐÕ0ð %ð €DØ€Nã ˆà×ÑÑ*Ø×'Ñ'×-Ñ-¨cÓ2ˆEØ˜‘8ˆDÜ   q¡›]ˆNÙð !ð 	Ð˜^Ð7ðHàGóHØ7ð "×,Ñ,ØœS ›ZÑ'Ü�\‰\×!Ñ!×%Ñ%ØØð	 -ó €Nð !×+Ñ+Ø!¤C¨£JÑ.Ü�\‰\×!Ñ!×-Ñ-ØØð	 ,ó €Mô —‘×+Ñ+¨O¸\ÓJ€Jð ×ÑØØØ�SˆzàØ%Ø'ñ
ð ,×8Ñ8Ó:ØÜ × <Ñ <Ó >× BÑ BÓCØØ$Ø'+Ø"0à#(Ø$3×$@Ñ$@Ó$BØ%4×%AÑ%AÓ%Cñ
ð õ r
   c           
      ó   — t        | j                  «      dkD  rt        j                  d«       t	        | «      \  }}}t        |«      t        |«      k(  sJ d«       ‚g }g }t        t        |«      «      D ]:  }t        ||   | ||   «      \  }}|j                  |«       |j                  |«       Œ< t        |«      t        |«      k(  sJ d«       ‚t        t        t        |«      «      «      D ]   }t        ||   ||   ||   | ||   ||¬«       Œ" |S )zí
    replace the ops marked with cuda_graph_attr to run_program_op to use cuda graph

    :param program: framework.Program, the program to be transformed
    :return: the cuda graph section program, user should hold these programs!
    r@   z‚Sub block(s) has been detected in the program. Cuda graph not support op with sub block, and it will only handle the global block.z@num of cuda graph sections is not equal with num of idx sectionszJthe num of cuda graph sections should be equal with the num of new program)rÚ   rž   )
r§   ÚblocksÚwarningsÚwarnr¹   r¨   rš   rr   rÍ   rÞ   )	r’   r©   rª   rž   r×   Úsection_programsr¸   rØ   Úins_outss	            r   Úcuda_graph_transformrå   ©  sM  € ô ˆ7�>‰>Ó˜QÒä�‰ðbô	
ô 2IØó2Ñ.Ð˜ wô Ð"Ó#¤sØó(ò ð JàIóJð ð €LØÐÜ”3Ð*Ó+Ö,ˆä$GØ Ñ" G¨\¸!©_ó%
Ñ!ˆ˜ð 	×Ñ˜HÔ%Ø×Ñ Õ0ð -ô ÐÓ ¤CØó%ò ð TàSóTð ô ”eœCÐ 3Ó4Ó5Ö6ˆä"Ø˜‰OØ˜QÑØ˜‰OØØ Ñ"ØØö	
ð 7ð Ðr
   )r   rA   )r   rá   rK   Úpaddle.baser   Úpaddle.base.corer   r   r   r   r   r   r   rN   r[   rm   r{   rš   r¹   rÞ   rå   r	   r
   r   Ú<module>rè      s   ðó 
Û ã Ý ÷ñ ñ ÔÑ#8Ô#:Ý;óð €Mòò 2€	Ø€÷#7ñ #7óL*òZ-ò"ò"1 òhy6òxVór8r
   