Ë
    �\;jé²  ã                   óP  — d dl Z d dlZddlmZmZ ddlmZmZmZ g Zej                  j                  Z
ej                  j                  Zej                  Zej                  Zej                   Zd„ Zd„ Zd„ Zd„ Zd	„ Zd
„ Z G d„ d«      Z G d„ d«      Z G d„ d«      Z G d„ d«      Zy)é    Né   )ÚcoreÚ	framework)Ú
cpu_placesÚcuda_placesÚ
xpu_placesc                 óP   — t        j                  «       }|j                  | «       |S ©N)r   ÚPlaceÚ	set_place)ÚplaceÚps     ú]G:\00. PROJECTS\API\Inventory\templateJSON\kerjaOCR\Lib\site-packages\paddle/base/compiler.pyÚ
_place_objr      s   € Ü�
‰
‹€AØ‡K�K�ÔØ€Hó    c                 ó’   — | r| nt        j                  «       }|j                  «       j                  D ]  }|j                  dv sŒ y y)N)ÚsendÚrecvTF)r   Údefault_main_programÚglobal_blockÚopsÚtype)Úmain_programÚmainÚops      r   Ú_is_pserver_moder   $   sB   € Ù'‰<¬Y×-KÑ-KÓ-M€DØ×ÑÓ!×%Ô%ˆØ�7‰7Ð&Ò&Ùð &ð r   c                 óÐ   — | j                  «       D ]S  }|j                  «       sŒ|j                  «       €Œ%|j                  «       j                  «       j	                  d«      sŒS y y)NÚ_gradTF)ÚnodesÚis_opr   r   Úendswith)ÚgraphÚnodes     r   Ú_has_backward_opr$   ,   sJ   € Ø—‘–ˆà�J‰J�LØ—‘“	Ñ%Ø—‘“	—‘Ó ×)Ñ)¨'Õ2áð ð r   c                 óø   — g }t        | j                  «       j                  «      D ]&  \  }}|j                  dk(  sŒ|j	                  |«       Œ( |d d d…   D ]!  }| j                  «       j                  |«       Œ# y )NÚfeedéÿÿÿÿ)Ú	enumerater   r   r   ÚappendÚ
_remove_op)ÚprogramÚpop_idxÚir   Úindexs        r   Ú_prune_feed_opsr/   7   sl   € à€GÜ˜7×/Ñ/Ó1×5Ñ5Ö6‰ˆˆ2Ø�7‰7�fÓØ�N‰N˜1Õð 7ð ™˜2˜”ˆØ×ÑÓ×)Ñ)¨%Õ0ñ r   c                 ó8  — | j                   D ]‹  }t        j                  }t        j                  j                  j                  }|j                  «       |j                  v sŒTt        |j                  «       |j                  «          «      t        |«      k(  sŒ‹ y y)NTF)
r   r   Úop_proto_and_checker_makerÚOpRoleÚOptimizeÚkOpRoleVarAttrNameÚ
attr_namesÚintÚ	all_attrsÚkOpRoleAttrName)Úblockr   Úop_makerÚoptimizes       r   Ú_has_optimize_opr<   A   s{   € Ø�iŒiˆÜ×2Ñ2ˆÜ×2Ñ2×9Ñ9×BÑBˆØ×&Ñ&Ó(¨B¯M©MÒ9¼cØ�L‰L‹N˜8×3Ñ3Ó5Ñ6ó?
ä�‹]ó?ñ ð ð r   c                 óž   — | j                  «       }|j                  j                  |d «      }|€yt        |dd«      xs t        |dd«      }| S )NTÚ_is_distributedFÚis_distributed)r   ÚvarsÚgetÚgetattr)r+   Úvar_namer9   Úvarr?   s        r   Ú_should_broadcast_or_not_existsrE   L   s[   € Ø× Ñ Ó"€EØ
�*‰*�.‰.˜ 4Ó
(€CØ
€{ØÜ˜SÐ"3°UÓ;ò ¼wØÐ˜uó@€Nð ÐÐr   c                   ó>   — e Zd ZdZd
d„Zd„ Zd„ Zd
d„Zd„ Zd„ Z	d	„ Z
y)ÚCompiledPrograma„  
    :api_attr: Static Graph

    The CompiledProgram is used to transform a program or graph for
    various optimizations according to the configuration of build_strategy,
    for example, the operators' fusion in the computation graph, memory
    optimization during the execution of the computation graph, etc.
    For more information about build_strategy, please refer to
    :code:`paddle.static.BuildStrategy`.

    Args:
        program_or_graph (Graph|Program): This argument is the Program or Graph
            being executed.
        build_strategy(BuildStrategy): This argument is used to compile the
            program or graph with the specified options, such as operators' fusion
            in the computational graph and memory optimization during the execution
            of the computational graph. For more information about build_strategy,
            please refer to :code:`paddle.static.BuildStrategy`. The default is None.

    Returns:
        CompiledProgram

    Example:
        .. code-block:: python

            >>> import numpy
            >>> import paddle
            >>> import paddle.static as static

            >>> paddle.enable_static()

            >>> place = paddle.CPUPlace()
            >>> exe = static.Executor(place)

            >>> data = static.data(name='X', shape=[None, 1], dtype='float32')
            >>> hidden = static.nn.fc(x=data, size=10)
            >>> loss = paddle.mean(hidden)
            >>> paddle.optimizer.SGD(learning_rate=0.01).minimize(loss)

            >>> exe.run(static.default_startup_program())
            >>> compiled_prog = static.CompiledProgram(
            ...     static.default_main_program())

            >>> x = numpy.random.random(size=(10, 1)).astype('float32')
            >>> loss_data, = exe.run(compiled_prog,
            ...                     feed={"X": x},
            ...                     fetch_list=[loss.name])
    Nc                 ó¤  — t        |t        j                  «      r|| _        d | _        nht        |t
        j                  «      r7t        |«       t        j                  |j                  «      | _        || _        nt        dt        |«      z  «      ‚d | _        d | _        d | _        d| _        d| _        d | _        d | _        || _        d | _        y )Nz[The type of program_to_graph parameter is wrong, expected Graph or Program, but received %sF)Ú
isinstancer   ÚGraphÚ_graphÚ_programr   ÚProgramr/   ÚdescÚ	TypeErrorr   Ú_scopeÚ_placeÚ	_executorÚ	_compiledÚ_is_inferenceÚ_share_vars_fromÚ_placesÚ_build_strategyÚ_exec_strategy)ÚselfÚprogram_or_graphÚbuild_strategys      r   Ú__init__zCompiledProgram.__init__‰   sº   € ÜÐ&¬¯
©
Ô3Ø*ˆDŒKà ˆD�MÜÐ(¬)×*;Ñ*;Ô<ÜÐ,Ô-ÜŸ*™*Ð%5×%:Ñ%:Ó;ˆDŒKØ,ˆD�MäØmÜÐ'Ó(ñ)óð ð
 ˆŒØˆŒØˆŒØˆŒØ"ˆÔØ $ˆÔØˆŒØ-ˆÔØ"ˆÕr   c                 óž   — | j                   rJ d«       ‚t        t        |t        «      t        |t        «      g«      sJ ‚d| _         || _        | S )z§Add inference optimize

        Args:
            config: instance of `NativeConfig` or `AnalysisConfig` to create predictor
        Returns:
            self
        z6Already compiled with inference, cannot be recompiled.T)rT   ÚanyrI   ÚInferNativeConfigÚInferAnalysisConfigÚ_infer_config)rY   Úconfigs     r   Ú_with_inference_optimizez(CompiledProgram._with_inference_optimize¢   se   € ð ×"Ò"ð	DàCó	DØ"ô ä˜6Ô#4Ó5Ü˜6Ô#6Ó7ðô
ð 	
ð 
ð "ˆÔØ#ˆÔØˆr   c                 ó   — t        d«      ‚)NzFSubclass of CompiledProgram should implement _with_distributed method.)ÚNotImplementedError©rY   s    r   Ú_with_distributedz!CompiledProgram._with_distributed¸   s   € Ü!ØTó
ð 	
r   c           
      ó   — | j                   rl|rt        j                  j                  d«       | j                   j                  €t        d«      ‚| j                   j                  j                  «       | _        n|€J d«       ‚g | _        t        |t        t        f«      sJ dj                  t        |«      «      «       ‚| j                  €t        «       | _        t        | j                   «      | j                  _        | j$                  €t'        «       | _        || j$                  _        | j$                  j*                  dk(  r›| j$                  j(                  t,        j.                  k(  rt1        |«      dz  | j$                  _        nV| j$                  j(                  t,        j2                  k(  rd| j$                  _        nt1        |«      dz  | j$                  _        d	t5        j6                  «       v rVt5        j6                  «       d	   r?| j$                  j*                  dk7  r&t9        j:                  d
«       d| j$                  _        | j                   r| j                  j<                  dkD  rf| j                   j>                  rP| j                   j>                  }| j                  j<                  t1        |«      k(  sJ d«       ‚|| j                  _         | j                   ro| j                   jB                  | j                  _"        | j                   jF                  | j                  _$        | j                   jJ                  | j                  _&        | j                  jN                  rd| j                  _(        | j                   �¹| j                   jR                  r£| j$                  j(                  t,        j.                  k(  sJ d«       ‚| j                  j<                  t1        |«      z  dkD  sJ d«       ‚| j                  jT                  t        jV                  jX                  k(  sJ d«       ‚d| j                  _-        g | _.        | j^                  ja                  «       D ]ä  }|jc                  «       sŒ|je                  «       €Œ%|je                  «       jg                  «       sŒD|je                  «       j                  «       t4        jh                  jj                  jl                  k7  sŒˆ|jo                  «       }| j                   €Œ¥tq        | j                   |«      sŒ¼| j\                  js                  |jo                  «       «       Œæ t        tu        tv        |«      «      }t        ty        | j\                  «      «      | _.        | j\                  j{                  «        t5        j|                  «       rt        d«      ‚t5        j€                  || j\                  d| j‚                  | j                  | j$                  | j                  | j^                  «      S )Nz*share_vars_from is set, scope is ignored.
zSThe shared Program is not compiled and executed, so there is no variables to share.Ú zOCurrently, The places type can only be list or tuple, but the input type is {}.r   é   r   é   ÚFLAGS_use_cinnzwAt present, when CINN is turned on, each process can only contain one thread, so reset the number of threads to 1 here.z5The trainer numbers is not equal to endpoint numbers.Tz%DGC only used under CUDA environment.z.DGC is not avaliable for single card training.zADGC                 only can be used for AllReduce BuildStrategy.FzBCUDA Graph is not allowed to capture when running the first batch.)BrU   ÚsysÚstderrÚwriterR   Ú
ValueErrorÚlocal_scopesÚ_local_scopesrI   ÚlistÚtupleÚformatr   rW   ÚBuildStrategyr   rL   Úis_distributionrX   ÚExecutionStrategyÚ_use_deviceÚnum_threadsÚ
DeviceTypeÚCUDAÚlenÚXPUr   ÚglobalsÚwarningsÚwarnÚnum_trainersÚ_trainers_endpointsÚtrainers_endpointsÚ_nccl_comm_numÚnccl_comm_numÚ_use_hierarchical_allreduceÚuse_hierarchical_allreduceÚ$_hierarchical_allreduce_inter_nranksÚ#hierarchical_allreduce_inter_nranksÚsync_batch_normÚenable_sequential_executionÚ_enable_dgcÚreduce_strategyÚReduceStrategyÚ	AllReduceÚfuse_all_reduce_opsÚ_persistable_varsrK   r   Úis_varrD   ÚpersistableÚVarDescÚVarTypeÚRAWÚnamerE   r)   Úmapr   ÚsetÚsortÚis_cuda_graph_capturingÚRuntimeErrorÚParallelExecutorrP   )rY   ÚplacesÚ
use_deviceÚscopeÚtpsr#   r˜   s          r   Ú_compile_data_parallelz&CompiledProgram._compile_data_parallel½   sÔ  € Ø× Ò ÙÜ—
‘
× Ñ Ð!NÔOØ×$Ñ$×.Ñ.Ð6Ü ð*óð ð "&×!6Ñ!6×!@Ñ!@×!MÑ!MÓ!OˆDÕàÐ$Ð( bÓ(Ð$Ø!#ˆDÔäØ”Tœ5�Mô
ð 	
à\×cÑcÜ�‹Ló
ó	
ð 
ð ×ÑÐ'Ü#0£?ˆDÔ Ü/?ÀÇÁÓ/Nˆ×ÑÔ,à×ÑÐ&Ü"3Ó"5ˆDÔØ*4ˆ×ÑÔ'à×Ñ×*Ñ*¨aÒ/Ø×"Ñ"×.Ñ.´*·/±/ÒAô 36°f³+À±/�×#Ñ#Õ/Ø×$Ñ$×0Ñ0´J·N±NÒBà23�×#Ñ#Õ/ä25°f³+À±/�×#Ñ#Ô/ð ¤§¡£Ñ.Ü—‘“Ð/Ò0Ø×#Ñ#×/Ñ/°1Ò4ä�M‰MðUôð /0ˆD×ÑÔ+ð �MŠMØ×$Ñ$×1Ñ1°AÒ5Ø—‘×1Ò1à—-‘-×3Ñ3ˆCà×'Ñ'×4Ñ4¼Øó9ò ð GàFóGð ð 7:ˆD× Ñ Ô3à�=Š=Ø15·±×1MÑ1MˆD× Ñ Ô.à—‘×9Ñ9ð × Ñ Ô;ð —‘×BÑBð × Ñ ÔDð ×Ñ×/Ò/Ø?CˆD× Ñ Ô<à�=‰=Ð$¨¯©×)BÒ)Bà×#Ñ#×/Ñ/´:·?±?ÒBð7à6ó7ØBð ×$Ñ$×1Ñ1´C¸³KÑ?À!ÒCð@à?ó@ØCð ×$Ñ$×4Ñ4Ü ×/Ñ/×9Ñ9ò:ð?ð?ó?ð:ð 8=ˆD× Ñ Ô4à!#ˆÔØ—K‘K×%Ñ%Ö'ˆDà—‘•Ø—H‘H“JÑ*Ø—H‘H“J×*Ñ*Õ,Ø—H‘H“J—O‘OÓ%¬¯©×)=Ñ)=×)AÑ)AÓAà—y‘y“{�à—M‘MÑ-Ü7¸¿¹ÀtÕLà×*Ñ*×1Ñ1°$·)±)³+Õ>ð (ô ”cœ* fÓ-Ó.ˆô
 "&¤c¨$×*@Ñ*@Ó&AÓ!BˆÔØ×Ñ×#Ñ#Ô%ä×'Ñ'Ô)ÜØTóð ô ×$Ñ$ØØ×"Ñ"ØØ�K‰KØ×ÑØ×ÑØ× Ñ Ø�K‰Kó	
ð 		
r   c                 ó@   — t        j                  | j                  «      S r
   )r   Úcreate_paddle_predictorra   rf   s    r   Ú_compile_inferencez"CompiledProgram._compile_inference@  s   € Ü×+Ñ+¨D×,>Ñ,>Ó?Ð?r   c                 óŒ  — | j                   rF|r| j                  |k7  rt        d«      ‚|r&| j                  j	                  |«      st        d«      ‚| S d| _         || _        || _        | j
                  r| j                  «       | _        | S | j                  g| _        t        | j                  t        j                  «      rt        j                  }nEt        | j                  t        j                  «      rt        j                  }nt        j                   }| j#                  || j                  | j                  ¬«      | _        | S )a  Compile the program based on the configs.

        Args:
            scope: The variables (resources) that are associated with
               this compiled program.
            place: The location that the compiled program will be run on.

        Returns:
            self
        z,Cannot compile program with different scope.z,Cannot compile program with different place.T)r    r¡   rŸ   )rS   rP   rp   rQ   Ú_equalsrT   r¦   rR   rV   rI   r   Ú	CUDAPlacer{   r|   ÚXPUPlacer~   ÚCPUr£   )rY   r¡   r   r    s       r   Ú_compilezCompiledProgram._compileC  sú   € ð �>Š>Ù˜Ÿ™¨Ò-Ü Ð!OÓPÐPÙ˜TŸ[™[×0Ñ0°Ô7Ü Ð!OÓPÐPØˆKØˆŒàˆŒØˆŒà×ÒØ!×4Ñ4Ó6ˆDŒNð ˆð !ŸK™K˜=ˆDŒLä˜$Ÿ+™+¤t§~¡~Ô6Ü'Ÿ_™_‘
Ü˜DŸK™K¬¯©Ô7Ü'Ÿ^™^‘
ä'Ÿ^™^�
Ø!×8Ñ8Ø%¨T¯[©[ÀÇÁð 9ó ˆDŒNð ˆr   c                 ó,  — |d u}|r0|D ]*  }|j                  «       |j                  «       k(  rŒ%J d«       ‚ nTt        |t        j                  «      rt	        «       }n/t        |t        j
                  «      rt        «       }n
t        «       }|sJ d«       ‚|S )Nz7Place type not match. You may set wrong type of places.zNo places for execution.)Ú_typerI   r   r©   r   rª   r   r   )rY   r   Ú
place_listÚhas_set_placer   s        r   Ú_get_placeszCompiledProgram._get_placesi  s†   € Ø"¨$Ð.ˆÙÛ�à—G‘G“I §¡£Ó.ðMàLóMØ.ñ  ô
 ˜%¤§¡Ô0Ü(›]‘
Ü˜E¤4§=¡=Ô1Ü'›\‘
ä'›\�
ÙÐ5Ð5Ó5ˆzØÐr   r
   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r\   rc   rg   r£   r¦   r¬   r±   © r   r   rG   rG   W   s/   „ ñ/ób#ò2ò,
ó
A
òF@ò$óLr   rG   c                   ón   — e Zd ZdZg Zd„ Ze	 d	d„«       Zed„ «       Zed„ «       Z	ed„ «       Z
ed„ «       Zy)
ÚIpuDynamicPatcherz1
    Patcher for IPU dynamic2static support.
    c                  ó   — y r
   r¶   rf   s    r   r\   zIpuDynamicPatcher.__init__‚  s   € Ør   Nc           
      óú  ‡ ‡‡‡‡‡‡— ddl ŠddlmŠ ddlm} ddlm} ‰j                  }‰j                  }‰j                  }‰j                  j                  «       Š|ˆˆˆ ˆfd„«       }‰ j                  r|j                  d¬	«       ‰j                  D ]Z  }	‰j                  |	j                   «      j#                  «       }
|	j%                  «       j#                  «       }|
j'                  |«       Œ\ |r!|d
d D �cg c]  }|€Œ|j                   ‘Œ c}Šn|D �cg c]  }|€Œ|j                   ‘Œ c}Š|D �cg c]  }|j                   ‘Œ c}Š‰ j(                  �rl |«       ‰_        ‰ j,                  }|j.                  j1                  «       D �]6  \  }}|j1                  «       D �]  \  }}|j2                  j5                  |j                   d|j6                  |j8                  |j:                  d¬«      }|j=                  |«      } ||«      5  |j2                  j?                  |‰j@                  jB                  jE                  d¬«      ¬«       ddd«       ‰jG                  |j                   «      j#                  «       }|j%                  «       j#                  «       }|j'                  |«       ||j.                  |   |<   �Œ �Œ9 |ˆˆˆˆ ˆˆfd„«       } |«       }|‰_        ‰S c c}w c c}w c c}w # 1 sw Y   Œ§xY w)zD
        Convert the ConcreteProgram to IPUConcreteProgram.
        r   Nrk   )Úbackward)Úswitch_to_static_graph)Údevice_guardc                  ó   •— ‰j                   } ‰j                  | ‰j                  «       ‰j                  j	                  ‰j                  «       «      }‰j                  j                  «       }|j                  |«       | S r
   )r   Úgradients_with_optimizerÚ
_optimizerÚstaticÚExecutorÚCPUPlaceÚdefault_startup_programÚrun)r+   ÚexeÚstartup_programr»   Úconcrete_programÚipu_strategyÚpaddles      €€€€r   Úappend_backward_desczHIpuDynamicPatcher.convert_concrete_program.<locals>.append_backward_desc˜  sf   ø€ à&×3Ñ3ˆGð ×-Ñ-¨g°|×7NÑ7NÔOð —-‘-×(Ñ(¨¯©Ó):Ó;ˆCØ$Ÿm™m×CÑCÓEˆOØ�G‰G�OÔ$àˆNr   Úfloat16)Údtyper   T)r˜   r”   rÍ   r   ÚshapeÚbelong_to_optimizerg        )Úvalue)Úinitializerc                  ó¬  •— ‰j                   rž‰j                  j                  j                  «       } dh| _        ‰j                  j                  j                  ‰j                  | d¬«      }‰j                  j                  j                  ‰j                  «       ‰j                  |¬«       t        ‰j                  ‰‰¬«      j                  ‰‰«      }|S )NÚcumsumF)Úuse_fp16_guard)Úto_fp16_var_names)rÉ   r¡   )Úenable_fp16rÁ   ÚampÚCustomOpListsÚunsupported_listÚcast_model_to_fp16r   Úcast_parameters_to_fp16rÃ   ÚIpuCompiledProgramÚcompile)	Úamp_listrÕ   r+   rÈ   Ú	feed_listÚ
fetch_listrÉ   rÊ   r¡   s	      €€€€€€r   Úfunc_compilez@IpuDynamicPatcher.convert_concrete_program.<locals>.func_compileÕ  sÆ   ø€ à×'Ò'Ø!Ÿ=™=×,Ñ,×:Ñ:Ó<�Ø-5¨J�Ô)Ø$*§M¡M×$5Ñ$5×$HÑ$HØ$×1Ñ1ØØ#(ð %Ió %Ð!ð
 —‘×!Ñ!×9Ñ9Ø—O‘OÓ%Ø$×1Ñ1Ø&7ð :ô ô )Ø ×-Ñ-Ø)Øô÷ ‰g�i Ó,ð	 ð
 ˆNr   )$rÊ   Úbaser»   Úbase.dygraph.baser¼   Úbase.frameworkr½   ÚinputsÚoutputsrÇ   rÁ   Úglobal_scoperÖ   ÚtoÚ
parametersrD   r˜   Ú
get_tensorrÐ   Ú_share_data_withÚis_trainingr   rÀ   Ú_accumulatorsÚitemsÚhelperÚcreate_global_variablerÍ   r   rÎ   Ú_get_device_for_paramÚset_variable_initializerÚnnrÑ   ÚConstantÚfind_var)rÉ   rÈ   Úclass_instancer¼   r½   rå   ræ   rÇ   rË   Úparam_or_bufferÚparam_or_buffer_tensorÚ
src_tensorÚelemÚ	optimizerÚkÚvÚ
param_nameÚvar_tmprD   ÚdeviceÚparam_or_lr_tensorÚoptim_tensorrá   r   r»   rß   rà   rÊ   r¡   s   ``                      @@@@@r   Úconvert_concrete_programz*IpuDynamicPatcher.convert_concrete_program…  s·  þ€ ó 	å#Ý>Ý1à!×(Ñ(ˆØ"×*Ñ*ˆØ*×:Ñ:ˆà—‘×*Ñ*Ó,ˆà	ö	ó 
 ð	ð ×#Ò#Ø×Ñ IÐÔ.ð  0×:Ô:ˆOØ%*§Y¡YØ×$Ñ$ó&ç‰j‹lð #ð )×.Ñ.Ó0×;Ñ;Ó=ˆJØ"×3Ñ3°JÕ?ð  ;ñ Ø/5°a°b©zÓN©z t¸TÑ=M˜Ÿ›¨zÑN‰Iá/5ÓJ©v t¸Ñ9I˜Ÿ›¨vÑJˆIÙ,3Ó4©G D�d—i“i¨GÑ4ˆ
à×#Ó#Ù,@Ó,BÐÔ)à$×/Ñ/ˆIØ!×/Ñ/×5Ñ5×7‘��1Ø+,¯7©7¯9Ñ'�J Ø#×*Ñ*×AÑAØ$Ÿ\™\Ø$(Ø%Ÿm™mØ$Ÿ\™\Ø%Ÿm™mØ,0ð Bó �Cð '×<Ñ<¸ZÓH�FÙ% fÕ-Ø!×(Ñ(×AÑAØØ(.¯	©	×(=Ñ(=×(FÑ(FØ&)ð )Gó )ð Bô ÷ .ð */¯©ØŸ™ó*ç ‘j“lð 'ð $'§9¡9£;×#9Ñ#9Ó#;�LØ&×7Ñ7¸ÔEØ=@�I×+Ñ+¨AÑ.¨zÓ:ò- ,5ð 8ð2 
 ÷	ð 	ó 
 ð	ñ, $“~ˆØ(4ÐÔ%ØÐùòw OùâJùÚ4÷" .Ð-ús+   Ã9K"ÄK"ÄK'ÄK'Ä4K,È	AK1Ë1K:c                 ó¶   ‡ ‡‡‡‡— ddl mŠ ddlmŠ ddlmŠmŠm} |j                  }ˆˆˆ ˆˆfd„}||_        t        j                  j                  |d|g«       y)zÃMonkey patch ProgramCache discriptor to support dynamic2static in IPU.

        Args:
            ipu_strategy: The ipu_strategy used in dynamic graph.

        Returns:
            None
        r   )Úlogging_utils)Úpartial_program_from)ÚMAX_TRACED_PROGRAM_COUNTÚCacheKeyÚProgramCachec                 óŒ  •— t        |‰«      s!t        dt        |«      j                  z  «      ‚t	        |«      }|| _        || j                  vs‰j                  rÜ|| j                  v r‰	j                  d«       | j                  r‰j                  s‰	j                  d«       | j                  |«      \  }}t        j                  ‰||j                  «      }| ‰
||j                  d u«      f| j                  |<   t        | j                  «      }|‰kD  r!‰	j                  dj                  |‰«      «       | j                  |   S )Nz.type(item) should be CacheKey, but received %sz3ipu_strategy chances detected. Please sync weights.zadynamic2static on IPU doesn't support mutiple caches. Please make suredynamic inputs is not used.zàCurrent traced program number: {} > `max_tracing_count`:{}. Too much cached programs will bring expensive overhead. The reason may be: (1) passing tensors with different shapes, (2) passing python objects instead of tensors.)rI   rp   r   r²   ÚhashÚ_recent_keyÚ_cachesÚneed_compiler�   Ú_build_oncer¸   r  rö   r}   ru   )rY   ÚitemÚitem_idrÈ   Ú_Úcurrent_tracing_countr  r  rÉ   r  r  s         €€€€€r   Úpatch_getterz;IpuDynamicPatcher.patch_program_cache.<locals>.patch_getter  sT  ø€ Ü˜d HÔ-Ü ØDÜ˜4“j×)Ñ)ñ*óð ô ˜4“jˆGØ&ˆDÔØ˜dŸl™lÑ*¨l×.GÒ.GØ˜dŸl™lÑ*Ø!×&Ñ&ØMôð —<’<¨×(AÒ(AØ!×&Ñ&ð6ôð '+×&6Ñ&6°tÓ&<Ñ#Ð  !Ü#4×#MÑ#MØ Ð"2°D×4GÑ4Gó$Ð ð
 %Ù(Ø(¨$×*=Ñ*=ÀTÐ*Ióð)�—‘˜WÑ%ô ),¨D¯L©LÓ(9Ð%Ø(Ð+CÒCØ!×&Ñ&ðG÷ HNñ  HNØ1Ð3KóHôð —<‘< Ñ(Ð(r   Ú__getitem__N)Úpaddle.jit.dy2staticr  Ú$paddle.jit.dy2static.partial_programr  Ú'paddle.jit.dy2static.program_translatorr  r  r	  r  r¸   Úpatcher_cacher)   )rÉ   r	  Ú
old_getterr  r  r  r  r  s   `   @@@@r   Úpatch_program_cachez%IpuDynamicPatcher.patch_program_cacheð  sU   ü€ õ 	7ÝM÷	
ñ 	
ð "×-Ñ-ˆ
÷'	)ð '	)ðR $0ˆÔ Ü×'Ñ'×.Ñ.Ø˜=¨*Ð5õ	
r   c                 óŒ   ‡ ‡— ddl m} |j                  Šdˆ ˆfd„	}||_        t        j                  j                  |d‰g«       y )Nr   ©ÚLRSchedulerc                 óR   •—  ‰| |«       ‰j                  d| j                  i«       y )NÚlr)Úset_optionsÚlast_lr)rY   ÚepochrÉ   Úold_steps     €€r   Ú
patch_stepz8IpuDynamicPatcher.patch_lr_scheduler.<locals>.patch_step:  s$   ø€ Ù�T˜5Ô!Ø×$Ñ$ d¨D¯L©LÐ%9Õ:r   Ústepr
   )Úpaddle.optimizer.lrr  r&  r¸   r  r)   )rÉ   r  r%  r$  s   `  @r   Úpatch_lr_schedulerz$IpuDynamicPatcher.patch_lr_scheduler2  s=   ù€ å3ð ×#Ñ#ˆö	;ð &ˆÔÜ×'Ñ'×.Ñ.°¸VÀXÐ/NÕOr   c                 óX   — t         j                  | «       t         j                  | «       y r
   )r¸   r  r(  )rÉ   s    r   Úregister_patchz IpuDynamicPatcher.register_patchA  s   € ä×-Ñ-¨lÔ;Ü×,Ñ,¨\Õ:r   c                  óP   — t         j                  D ]  \  } }}t        | ||«       Œ y r
   )r¸   r  Úsetattr)ÚmoduleÚkeyÚattrs      r   Úrelease_patchzIpuDynamicPatcher.release_patchF  s&   € ä!2×!@Ô!@ÑˆF�C˜Ü�F˜C Õ&ñ "Ar   r
   )r²   r³   r´   rµ   r  r\   Ústaticmethodr  r  r(  r*  r0  r¶   r   r   r¸   r¸   {  s€   „ ñð €Mòð à7;òh ó ðh ðT ñ?
ó ð?
ðB ñPó ðPð ñ;ó ð;ð ñ'ó ñ'r   r¸   c                   ó¸   — e Zd ZdZd„ Zd„ Zd„ Zd„ Zd„ Z	 	 	 	 dd„Z		 	 	 	 dd„Z
dd	„Z	 dd„Zd„ Zd„ Zd„ Zd„ Zed„ «       Zed„ «       Zed„ «       Zed„ «       Zy
)ÚIpuStrategyaŸ  
    Help users precisely control the graph building in :code:`paddle.static.IpuCompiledProgram` .

    Returns:
        The IpuStrategy instance.

    Examples:
        .. code-block:: python

            >>> # doctest: +REQUIRES(env:IPU)

            >>> import paddle
            >>> import paddle.static as static

            >>> paddle.enable_static()

            >>> ipu_strategy = static.IpuStrategy()
    c                 ó$  — t        j                  «       rSt        j                  «       | _        dddœdddœ}| j                  j	                  |«       d| _        g | _        d| _        nt        d«      ‚ddl	m
}  |«       r| j                  «        y y )	Nr   r   )Úon_chipÚuse_replicated_tensor_sharding)Úlocation_optimizerÚ+accumulation_and_replication_reduction_typeÚ4mean_accumulation_and_replication_reduction_strategyFTz\Can not use IpuStrategy in non IPU compiled environment, please re-compile with WITH_IPU=ON.©Úin_dynamic_mode)r   Úis_compiled_with_ipur3  Ú_ipu_strategyr!  Úhas_custom_opsÚcustom_op_namesr  r�   rÊ   r;  r*  )rY   Údefault_optionsr;  s      r   r\   zIpuStrategy.__init__`  s—   € Ü×$Ñ$Ô&Ü!%×!1Ñ!1Ó!3ˆDÔð  !Ø67ñ'ð @AØHIñˆOð ×Ñ×*Ñ*¨?Ô;Ø"'ˆDÔØ#%ˆDÔ Ø $ˆDÕäØnóð õ 	+áÔØ×ÑÕ!ð r   c                 ó.   — t         j                  | «       y)aë  
        Register patchs function to support dynamic to static on IPU. This operation would break the dy2static functionality on CPU.
        Use `release_patch` to release the patch.

        Examples:
            .. code-block:: python

                >>> # doctest: +REQUIRES(env:IPU)

                >>> import paddle
                >>> import paddle.static as static

                >>> ipu_strategy = static.IpuStrategy()

                >>> ipu_strategy.register_patch()
        N)r¸   r*  rf   s    r   r*  zIpuStrategy.register_patchx  s   € ô" 	×(Ñ(¨Õ.r   c                 ó,   — t         j                  «        y)aa  
        Release the registered IPU functions.

        Examples:
            .. code-block:: python

                >>> # doctest: +REQUIRES(env:IPU)

                >>> import paddle
                >>> import paddle.static as static

                >>> ipu_strategy = static.IpuStrategy()

                >>> ipu_strategy.release_patch()
        N)r¸   r0  rf   s    r   r0  zIpuStrategy.release_patch‹  s   € ô  	×'Ñ'Õ)r   c                 óš   — ddl m}  |«       r4|| _        | j                  |«      }| j                  j                  |«       yt        d«      ‚)aê  
        Set optimizer to ipu_strategy in dynamic mode.

          Args:
              optimizer (Optimizer): Optimizer to be used in training.

          Returns:
              None.

          Examples:
                .. code-block:: python

                    >>> # doctest: +REQUIRES(env:IPU)
                    >>> import paddle
                    >>> import paddle.static as static

                    >>> linear = paddle.nn.Linear(10, 10)
                    >>> optimizer = paddle.optimizer.SGD(learning_rate=0.01,
                    ...                                 parameters=linear.parameters())
                    >>> ipu_strategy = static.IpuStrategy()
                    >>> ipu_strategy.set_optimizer(optimizer)
        r   r:  z,Only needs to set optimizer in dynamic mode.N)rÊ   r;  rÀ   Úparse_optimizerr=  r!  r�   )rY   rû   r;  Úoptimizer_attrss       r   Úset_optimizerzIpuStrategy.set_optimizer�  sD   € õ. 	+áÔØ'ˆDŒOØ"×2Ñ2°9Ó=ˆOØ×Ñ×*Ñ*¨?Õ;äÐMÓNÐNr   c                 óZ   ‡— ˆfd„}|g}ddi}|D ]  }|j                   |«       «       Œ |S )a  
        Parse optimizer attributes for IPU dynamic to static support. Currently only support parse lr.

          Args:
              optimizer (Optimizer): Optimizer to be parsed.

          Returns:
              Dict.

          Examples:
                .. code-block:: python

                    >>> # doctest: +REQUIRES(env:IPU)

                    >>> import paddle
                    >>> import paddle.static as static

                    >>> linear = paddle.nn.Linear(10, 10)
                    >>> optimizer = paddle.optimizer.SGD(learning_rate=0.01,
                    ...                                 parameters=linear.parameters())
                    >>> ipu_strategy = static.IpuStrategy()
                    >>> attrs = ipu_strategy.parse_optimizer(optimizer)
        c                  ó²   •— ddl m}  t        ‰j                  t        «      rd‰j                  iS t        ‰j                  | «      rd‰j                  «       iS y )Nr   r  r   )r'  r  rI   Ú_learning_rateÚfloat)r  rû   s    €r   Úget_lrz+IpuStrategy.parse_optimizer.<locals>.get_lrÖ  sQ   ø€ Ý7ä˜)×2Ñ2´EÔ:Ø˜i×6Ñ6Ð7Ð7Ü˜I×4Ñ4°kÔBØ˜i×6Ñ6Ó8Ð9Ð9ð Cr   Ú
is_dynamicT)Úupdate)rY   rû   rK  Úattr_fnrE  Úfns    `    r   rD  zIpuStrategy.parse_optimizer½  s;   ø€ ô2	:ð �(ˆØ'¨Ð.ˆÛˆBØ×"Ñ"¡2£4Õ(ð àÐr   c                 óX   — |dk(  r|rt        d«      ‚||||dœ}| j                  |«       y)a]  
        Set graph configuration to the IpuStrategy instance.

        Args:
            num_ipus (int, optional): Number of IPU devices. Default 1, which means only use 1 IPU.
            is_training (bool, optional): True is training graph, False is inference graph. Default True, which means is training mode.
            batch_size (int, optional): The batch-size in the graph. Used to make the graph batch-size fixed,
                if the batch-size in the graph is dynamic. Default 1, which means the batch-size would be set 1, if the batch-size is dynamice.
            enable_manual_shard (bool, optional): Enable graph sharding or not. Only if num_ipus > 1, enable_manual_shard is able to be set True.
                Default False, which means disabled.

        Returns:
            None.

        Examples:
            .. code-block:: python

                >>> # doctest: +REQUIRES(env:IPU)

                >>> import paddle
                >>> import paddle.static as static

                >>> paddle.enable_static()

                >>> ipu_strategy = static.IpuStrategy()
                >>> ipu_strategy.set_graph_config(num_ipus=1,
                ...                             is_training=True,
                ...                             micro_batch_size=1,
                ...                             enable_manual_shard=False)
        r   zAOnly if num_ipus > 1, enable_manual_shard is able to be set True.)Únum_ipusrì   Úmicro_batch_sizeÚenable_manual_shardN)r�   r!  )rY   rQ  rì   rR  rS  Úoptionss         r   Úset_graph_configzIpuStrategy.set_graph_configä  sD   € ðJ �qŠ=Ñ0ÜØSóð ð !Ø&Ø 0Ø#6ñ	
ˆð 	×Ñ˜Õ!r   c                 ót   — | j                  d«      }|s|rt        d«      ‚||||dœ}| j                  |«       y)a¸  
        Set pipelining configuration to the IpuStrategy instance. Used to optimize the throughput performance.

        Args:
            enable_pipelining (bool, optional): Enable data pipelining between subgraphs. Only if enable_manual_shard=True, enable_pipelining is able to be set True.
                Default False, which means disabled.
            batches_per_step (int, optional): Set the batches per run in data pipelining mode. Only if enable_pipelining=True, batches_per_step is able to be set > 1.
                Default 1, which means no data pipelining.
            enable_gradient_accumulation (bool, optional): Enable to accumulate gradients before updating the weights in training mode. Only if enable_pipelining=True,
                enable_gradient_accumulation is able to be set True. Default False, which means no gradient accumulation.
            accumulation_factor (int, optional): Specify the number of micro-batches to accumulate
                before applying the varUpdate. Default 1, which means disable the accumulation.

        Returns:
            None.

        Examples:
            .. code-block:: python

                >>> # doctest: +REQUIRES(env:IPU)

                >>> import paddle
                >>> import paddle.static as static

                >>> paddle.enable_static()

                >>> ipu_strategy = static.IpuStrategy()
                >>> ipu_strategy.set_pipelining_config(enable_pipelining=False,
                ...                                     batches_per_step=1,
                ...                                     enable_gradient_accumulation=False,
                ...                                     accumulation_factor=1)
        rS  zKOnly if enable_manual_shard=True, enable_pipelining is able to be set True.)Úenable_pipeliningÚbatches_per_stepÚenable_gradient_accumulationÚaccumulation_factorN)Ú
get_optionr�   r!  )rY   rW  rX  rY  rZ  rS  rT  s          r   Úset_pipelining_configz!IpuStrategy.set_pipelining_config  sP   € ðN #Ÿo™oÐ.CÓDÐÙ"Ñ'8ÜØ]óð ð "3Ø 0Ø,HØ#6ñ	
ˆð 	×Ñ˜Õ!r   c                 ó.   — d|i}| j                  |«       y)a¤  
        Set half computation configuration to the IpuStrategy instance. Used to optimize the performance.

        Args:
            enable_fp16 (bool, optional): Enable FLOAT16 mode and transform FLOAT32 to FLOAT16. Default False, which means disable FLOAT16 mode.

        Returns:
            None.

        Examples:
            .. code-block:: python

                >>> # doctest: +REQUIRES(env:IPU)

                >>> import paddle
                >>> import paddle.static as static

                >>> paddle.enable_static()

                >>> ipu_strategy = static.IpuStrategy()
                >>> ipu_strategy.set_precision_config(enable_fp16=False)
        rÖ   N)r!  )rY   rÖ   rT  s      r   Úset_precision_configz IpuStrategy.set_precision_configI  s    € ð0 ˜;ð
ˆð 	×Ñ˜Õ!r   Nc                 óž   — |€|}||||dœ}| j                  d|i«       | j                  j                  |«       | j                  sd| _        yy)aä  
        Add a mapping to use popart custom ops running on the IPU.

        Args:
            paddle_op(str): the name of custom op in paddle.

            popart_op(str): the name of custom op in popart.

            domain(str): domain name of custom op in popart.

            version(int): version of custom op in popart.

        Returns:
            None.

        Examples:
            .. code-block:: python

                >>> # doctest: +REQUIRES(env:IPU)

                >>> import paddle
                >>> import paddle.static as static

                >>> paddle.enable_static()

                >>> ipu_strategy = static.IpuStrategy()
                >>> ipu_strategy.add_custom_op('paddle_relu', 'popart_relu')
        N)Ú	paddle_opÚ	popart_opÚdomainÚversionÚ	custom_opT)r!  r?  r)   r>  )rY   r`  ra  rb  rc  rd  s         r   Úadd_custom_opzIpuStrategy.add_custom_ope  sa   € ð> ÐØ!ˆIà"Ø"ØØñ	
ˆ	ð 	×Ñ˜+ yÐ1Ô2Ø×Ñ×#Ñ# IÔ.Ø×"Ò"Ø"&ˆDÕð #r   c                 óv   — | j                   j                  |«       dh}|j                  «       |z
  rd| _        yy)a#  
        Set options from dict.

        Args:
            options(dict): dict of options.

        Returns:
            None.

        Examples:
            .. code-block:: python

                >>> # doctest: +REQUIRES(env:IPU)

                >>> import paddle
                >>> import paddle.static as static

                >>> paddle.enable_static()

                >>> ipu_strategy = static.IpuStrategy()
                >>> options = {'num_ipus':1, 'enable_fp16': True}
                >>> ipu_strategy.set_options(options)
        r   TN)r=  r!  Úkeysr  )rY   rT  Úrecompile_white_lists      r   r!  zIpuStrategy.set_options‘  s<   € ð0 	×Ñ×&Ñ& wÔ/à $˜vÐØ�<‰<‹>Ð0Ò0Ø $ˆDÕð 1r   c                 ó>   — | j                   j                  |«      d   S )aè  
        Get option.

        Args:
            option(str): name of option.

        Returns:
            option value.

        Examples:
            .. code-block:: python

                >>> # doctest: +REQUIRES(env:IPU)

                >>> import paddle
                >>> import paddle.static as static

                >>> paddle.enable_static()

                >>> ipu_strategy = static.IpuStrategy()
                >>> num_ipus = ipu_strategy.get_option('num_ipus')
        rÐ   )r=  r[  )rY   Úoptions     r   r[  zIpuStrategy.get_option¯  s    € ð. ×!Ñ!×,Ñ,¨VÓ4°WÑ=Ð=r   c                 ó:   — | j                   j                  |«       y)a  
        Enable PopART pattern to optimize the graph.

        Args:
            pattern(string): the name of the pattern.

        Returns:
            None.

        Examples:
            .. code-block:: python

                >>> # doctest: +REQUIRES(env:IPU)

                >>> import paddle
                >>> import paddle.static as static

                >>> paddle.enable_static()

                >>> ipu_strategy = static.IpuStrategy()
                >>> ipu_strategy.enable_pattern("ViewSimplifyPattern")
        N)r=  Úenable_pattern©rY   Úpatterns     r   rl  zIpuStrategy.enable_patternÈ  s   € ð. 	×Ñ×)Ñ)¨'Õ2r   c                 ó:   — | j                   j                  |«       y)aþ  
        Disable PopART pattern.

        Args:
            pattern(string): the name of the pattern.

        Returns:
            None.

        Examples:
            .. code-block:: python

                >>> # doctest: +REQUIRES(env:IPU)

                >>> import paddle
                >>> import paddle.static as static

                >>> paddle.enable_static()

                >>> ipu_strategy = static.IpuStrategy()
                >>> ipu_strategy.disable_pattern("ViewSimplifyPattern")
        N)r=  Údisable_patternrm  s     r   rp  zIpuStrategy.disable_patterná  s   € ð. 	×Ñ×*Ñ*¨7Õ3r   c                 ó$   — | j                  d«      S )zJ
        Get the number of IPU devices from IpuStrategy instance.
        rQ  ©r[  rf   s    r   rQ  zIpuStrategy.num_ipusú  s   € ð
 �‰˜zÓ*Ð*r   c                 ó$   — | j                  d«      S )zU
        Get the boolean of training or inference from IpuStrategy instance.
        rì   rr  rf   s    r   rì   zIpuStrategy.is_training  ó   € ð
 �‰˜}Ó-Ð-r   c                 ó$   — | j                  d«      S )zX
        Get the boolean of enable pipelining or not from IpuStrategy instance.
        rW  rr  rf   s    r   rW  zIpuStrategy.enable_pipelining  s   € ð
 �‰Ð2Ó3Ð3r   c                 ó$   — | j                  d«      S )zS
        Get the boolean of float16 mode or not from IpuStrategy instance.
        rÖ   rr  rf   s    r   rÖ   zIpuStrategy.enable_fp16  rt  r   )r   Tr   F)Fr   Fr   )F)Nz
custom.opsr   )r²   r³   r´   rµ   r\   r*  r0  rF  rD  rU  r\  r^  re  r!  r[  rl  rp  ÚpropertyrQ  rì   rW  rÖ   r¶   r   r   r3  r3  L  sÁ   „ ñò&"ò0/ò&*ò$Oò@%ðR ØØØ!ó/"ðf  ØØ%*Øó2"óh"ð: GHó*'òX%ò<>ò23ò24ð2 ñ+ó ð+ð ñ.ó ð.ð ñ4ó ð4ð ñ.ó ñ.r   r3  c                   ó   — e Zd ZdZdd„Zd„ Zy)rÜ   al  
    The IpuCompiledProgram is used to transform a program to a ipu-target program,
    such as forward graph extraction, computing graph transformation, useless scale Ops clean, etc.

    Args:
        program(Program, optional): This parameter represents the :code:`Program`
            to be executed. Default is None, which means the program will be set to
            the default program :code:`paddle.static.default_main_program()` .
        scope(Scope, optional): The scope used to run this program, you can switch
            it to different scope. Default is None, which means use the global
            scope :code:`paddle.static.global_scope()` .
        ipu_strategy(IpuStrategy, optional): This argument is used to build the program with the
            specified options, such as half computation, training or inference session, the number of IPUs, etc.
            Default is None, which means build the program based on the default `ipu_strategy`.

    Returns:
        IpuCompiledProgram

    Example:
        .. code-block:: python

            >>> # doctest: +REQUIRES(env:IPU)

            >>> import paddle
            >>> import paddle.static as static

            >>> paddle.enable_static()

            >>> a = static.data(name='data', shape=[None, 1], dtype='int32')
            >>> b = a + 1
            >>> main_prog = static.default_main_program()

            >>> ipu_strategy = static.IpuStrategy()
            >>> ipu_strategy.set_graph_config(num_ipus=1, is_training=True, micro_batch_size=1)
            >>> ipu_strategy.set_pipelining_config(enable_pipelining=False, batches_per_step=1, enable_gradient_accumulation=False, accumulation_factor=1)
            >>> ipu_strategy.set_precision_config(enable_fp16=False)

            >>> ipu_compiled_program = static.IpuCompiledProgram(
            ...     main_prog,
            ...     ipu_strategy=ipu_strategy)
    Nc                 ó  — t        j                  «       st        d«      ‚|€t        j                  «       }t        |t        j                  «      st        dt        |«      z  «      ‚|| _	        d| _
        |�|| _        n#dd l}|j                  j                  «       | _        |�|| _        nt!        «       | _        |j"                  rt%        |j&                  «      | _        nd| _        t         j*                  j-                  «       | _        y )NzECan not use this function since PaddlePaddle is not compiled with IPUz:The type of program is wrong, expected Program, but got %sFr   r¶   )r   r<  rp   r   r   rI   rM   rO   r   rL   rS   rP   rÊ   rÁ   rç   r=  r3  r>  rš   r?  Ú_custom_op_namesÚ
IpuBackendÚget_instanceÚ_backend)rY   r+   r¡   rÉ   rÊ   s        r   r\   zIpuCompiledProgram.__init__B  så   € Ü×(Ñ(Ô*ÜØWóð ð ˆ?Ü×4Ñ4Ó6ˆGä˜'¤9×#4Ñ#4Ô5ÜØLÜ�w“-ñ óð ð
  ˆŒØˆŒàÐØˆD�Kó à Ÿ-™-×4Ñ4Ó6ˆDŒKàÐ#Ø!-ˆDÕä!,£ˆDÔà×&Ò&Ü$'¨×(DÑ(DÓ$EˆDÕ!à$&ˆDÔ!äŸ™×4Ñ4Ó6ˆ�r   c                 ó|  — | j                   j                  | j                  «       | j                   j                  | j                  j                  «       | j
                  j                  «       }g }t        |j                  «      D ]P  \  }}|j                  j                  d«       |j                  dk(  s|j                  dk(  sŒ@|j                  |«       ŒR |ddd…   D ]  }|j                  |«       Œ dD ]%  }|j                  |«      sŒ|j                  |«       Œ' | j
                  j                  j!                  «        t#        j$                  | j
                  j                  «      | _        | j                  j(                  r;ddg}	|	D ]2  }
t#        j*                  |
«      }|j-                  | j&                  «       Œ4 g d	¢}	|	D ]I  }
t#        j*                  |
«      }|
d
k(  r|j/                  d|«       |j-                  | j&                  «       ŒK t#        j*                  d«      }| j0                  r|j/                  d| j0                  «       |j-                  | j&                  «       g d¢}	|	D ]V  }
t#        j*                  |
«      }|j/                  d|«       |j/                  d|«       |j-                  | j&                  «       ŒX t#        j*                  d«      }t#        j2                  «       }|j5                  d|«       |j-                  | j&                  «       t6        j8                  j;                  |«      }t=        | j
                  d«      rs| j
                  j>                  j@                  }| j
                  j>                  |_        | j
                  j                  «       }|jB                  |   |j>                  _"        |j                  «       }|D ].  }|jG                  |«      }|j                  jI                  d«       Œ0 t=        |d«      s| j
                  |_%        d| j                  _&        |S )aK  
        This interface is used to compile the input Program to a program
        to run the model on the ipu.

        Args:
            feed_list(list): This parameter represents the input Tensors of the model.

            fetch_list(list): This parameter represents the Tensors that need to be returned
                after the model.

        Returns:
            Program

        Example:
            .. code-block:: python

                >>> # doctest: +REQUIRES(env:IPU)

                >>> import paddle
                >>> import paddle.static as static

                >>> paddle.enable_static()

                >>> a = static.data(name='data', shape=[None, 1], dtype='int32')
                >>> b = a + 1
                >>> main_prog = static.default_main_program()

                >>> ipu_strategy = static.IpuStrategy()
                >>> ipu_strategy.set_graph_config(num_ipus=1, is_training=True, micro_batch_size=1)
                >>> ipu_strategy.set_pipelining_config(enable_pipelining=False, batches_per_step=1, enable_gradient_accumulation=False, accumulation_factor=1)
                >>> ipu_strategy.set_precision_config(enable_fp16=False)

                >>> program = static.IpuCompiledProgram(
                ...     main_prog,
                ...     ipu_strategy=ipu_strategy).compile([a.name], [b.name])
        Fr&   ÚfetchNr'   )r&   r  Úoptimizer_extract_passÚoptimizer_state_align_pass)Úforward_graph_extract_passÚinfer_shape_passÚavg_shard_passÚdelete_scale_op_passrƒ  rß   Úpopart_canonicalization_passÚ
custom_ops)Úipu_inplace_passÚipu_graph_builder_passÚipu_runtime_replacer_passrà   Úgraph_to_program_passr+   Úlr_schedulerÚorg_program)'r}  Ú	set_scoperP   Úset_ipu_strategyr=  rL   r   r(   r   rN   Úset_is_targetr   r)   r*   Úhas_varÚ_remove_varÚflushr   rJ   rK   rì   Úget_passÚapplyrš   rz  ÚProgramDescÚset_not_ownedr   rM   Ú_construct_from_descÚhasattrrŒ  Ú	_var_namer@   Úlr_varrD   Úset_need_check_feedr�  r  )rY   rß   rà   r   Úneed_to_remove_op_indexr-   r   r.   rD   ÚpassesÚ	pass_nameÚa_passÚconvert_passrN   r+   Úlr_var_nameÚprogram_global_blockÚ	feed_nameÚfeed_vars                      r   rÝ   zIpuCompiledProgram.compileh  sZ  € ðJ 	�‰×Ñ §¡Ô,Ø�‰×&Ñ& t×'9Ñ'9×'GÑ'GÔHð —}‘}×1Ñ1Ó3ˆØ"$ÐÜ˜|×/Ñ/Ö0‰EˆAˆrØ�G‰G×!Ñ! %Ô(Ø�w‰w˜&Ò  B§G¡G¨wÓ$6Ø'×.Ñ.¨qÕ1ð 1ð
 -©T¨r¨TÔ2ˆEØ×#Ñ# EÕ*ð 3ó %ˆCØ×#Ñ# CÕ(Ø×(Ñ(¨Õ-ð %ð 	�‰×Ñ× Ñ Ô"Ü—j‘j §¡×!3Ñ!3Ó4ˆŒà×Ñ×)Ò)à(Ø,ðˆFó $�	ÜŸ™ yÓ1�Ø—‘˜TŸ[™[Õ)ð $ò
ˆó  ˆIÜ—]‘] 9Ó-ˆFØÐ.Ò.Ø—
‘
˜;¨	Ô2Ø�L‰L˜Ÿ™Õ%ð	  ô —‘Ð=Ó>ˆØ× Ò Ø�J‰J�| T×%:Ñ%:Ô;Ø�‰�T—[‘[Ô!ò
ˆó
  ˆIÜ—]‘] 9Ó-ˆFØ�J‰J�{ IÔ.Ø�J‰J�| ZÔ0Ø�L‰L˜Ÿ™Õ%ð	  ô —}‘}Ð%<Ó=ˆÜ×ÑÓ!ˆØ×"Ñ" 9¨dÔ3Ø×Ñ˜4Ÿ;™;Ô'Ü×#Ñ#×8Ñ8¸Ó>ˆä�4—=‘= .Ô1àŸ-™-×4Ñ4×>Ñ>ˆKà#'§=¡=×#=Ñ#=ˆGÔ ð  Ÿ=™=×5Ñ5Ó7ˆLØ*6×*;Ñ*;¸KÑ*HˆG× Ñ Ô'ð  '×3Ñ3Ó5ÐÛ"ˆIØ+×/Ñ/°	Ó:ˆHØ�M‰M×-Ñ-¨eÕ4ð #ô �w Ô.Ø"&§-¡-ˆGÔà*/ˆ×ÑÔ'àˆr   )NNN)r²   r³   r´   rµ   r\   rÝ   r¶   r   r   rÜ   rÜ     s   „ ñ(óT$7óL}r   rÜ   )rm   r€   ri   r   r   r   r   r   Ú__all__rž   rx   rv   ÚNativeConfigr_   ÚAnalysisConfigr`   r{   r   r   r$   r/   r<   rE   rG   r¸   r3  rÜ   r¶   r   r   Ú<module>r©     s°   ðó Û ç ß :Ñ :à
€à×)Ñ)×;Ñ;Ð Ø×%Ñ%×3Ñ3€Ø×%Ñ%Ð Ø×)Ñ)Ð Ø�_‰_€
òòòò1òò÷añ a÷H	N'ñ N'÷bH.ñ H.÷VNò Nr   