o
     j                     @   s   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d Zdd Zd	d
 Zdd Zdd Zdd ZG dd dZG dd dZG dd dZG dd dZdS )    N   )core	framework)
cpu_placescuda_places
xpu_placesc                 C   s   t  }||  |S N)r   ZPlaceZ	set_place)placep r   U/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/base/compiler.py
_place_obj   s   
r   c                 C   s4   | r| nt  }| jD ]
}|jdv r dS qdS )N)sendrecvTF)r   default_main_programglobal_blockopstype)main_programmainopr   r   r   _is_pserver_mode$   s   
r   c                 C   s>   |   D ]}| r| d ur|  dr dS qdS )NZ_gradTF)nodesZis_opr   r   endswith)graphnoder   r   r   _has_backward_op,   s   r   c                 C   sV   g }t |  jD ]\}}|jdkr|| q	|d d d D ]	}|  | qd S )Nfeed)	enumerater   r   r   append
_remove_op)programZpop_idxir   indexr   r   r   _prune_feed_ops7   s   

r%   c                 C   sP   | j D ]"}tj}tjjj}| |jv r%t| |	  t|kr% dS qdS )NTF)
r   r   Zop_proto_and_checker_makerZOpRoleZOptimizeZkOpRoleVarAttrNameZ
attr_namesintZ	all_attrsZkOpRoleAttrName)blockr   Zop_makeroptimizer   r   r   _has_optimize_opA   s   

r)   c                 C   s@   |   }|j|d }|d u rdS t|ddpt|dd}| S )NTZ_is_distributedFis_distributed)r   varsgetgetattr)r"   var_namer'   varr*   r   r   r   _should_broadcast_or_not_existsL   s   r0   c                   @   sL   e Zd ZdZdddZdd Zdd Zdd	d
Zdd Zdd Z	dd Z
dS )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                 C   s   t |tjr|| _d | _nt |tjr"t| t|j| _|| _nt	dt
| d | _d | _d | _d| _d| _d | _d | _|| _d | _d S )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)selfZprogram_or_graphZbuild_strategyr   r   r   __init__   s*   
zCompiledProgram.__init__c                 C   s:   | j rJ dtt|tt|tgsJ d| _ || _| S )zAdd inference optimize

        Args:
            config: instance of `NativeConfig` or `AnalysisConfig` to create predictor
        Returns:
            self
        z6Already compiled with inference, cannot be recompiled.T)r=   anyr2   InferNativeConfigInferAnalysisConfig_infer_config)rB   configr   r   r   _with_inference_optimize   s   	z(CompiledProgram._with_inference_optimizec                 C   s   t d)NzFSubclass of CompiledProgram should implement _with_distributed method.)NotImplementedErrorrB   r   r   r   _with_distributed   s   z!CompiledProgram._with_distributedc              
   C   s   | j r|rtjd | j jd u rtd| j j | _n|d us%J dg | _t|t	t
fs8J dt|| jd u rAt | _t| j| j_| jd u rQt | _|| j_| jjdkr| jjtjkrkt|d | j_n| jjtjkrwd| j_nt|d | j_d	t v rt d	 r| jjdkrtd
 d| j_| jr| jjdkr| jjr| jj}| jjt|ksJ d|| j_ | jr| jj!| j_"| jj#| j_$| jj%| j_&| jj'rd| j_(| jd ur| jj)r| jjtjksJ d| jjt| dksJ d| jj*tj+j,ks	J dd| j_-g | _.| j/0 D ];}|1 rO|2 d urO|2 3 rO|2  tj4j5j6krO|7 }| jd urOt8| j|rO| j.9|7  qt	t:t;|}t	t<| j.| _.| j.=  t> rnt?dt@|| j.d| jA| 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      Z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.)Br>   sysstderrwriter;   
ValueErrorZlocal_scopesZ_local_scopesr2   listtupleformatr   r@   BuildStrategyr   r5   Zis_distributionrA   ExecutionStrategyZ_use_deviceZnum_threads
DeviceTypeCUDAlenXPUr   globalswarningswarnZnum_trainersZ_trainers_endpointsZtrainers_endpointsZ_nccl_comm_numZnccl_comm_numZ_use_hierarchical_allreduceZuse_hierarchical_allreduceZ$_hierarchical_allreduce_inter_nranksZ#hierarchical_allreduce_inter_nranksZsync_batch_normZenable_sequential_executionZ_enable_dgcZreduce_strategyZReduceStrategyZ	AllReduceZfuse_all_reduce_opsZ_persistable_varsr4   r   Zis_varr/   persistableZVarDescZVarTypeZRAWnamer0   r    mapr   setsortZis_cuda_graph_capturingRuntimeErrorParallelExecutorr9   )rB   places
use_devicescopeZtpsr   ra   r   r   r   _compile_data_parallel   s   







z&CompiledProgram._compile_data_parallelc                 C   s   t | jS r   )r   Zcreate_paddle_predictorrG   rK   r   r   r   _compile_inference@  s   z"CompiledProgram._compile_inferencec                 C   s   | j r|r| j|krtd|r| j|std| S d| _ || _|| _| jr/|  | _| S | jg| _t	| jt
jr?tj}nt	| jt
jrJtj}ntj}| j|| j| jd| _| 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)rh   ri   rg   )r<   r9   rS   r:   Z_equalsr=   rk   r;   r?   r2   r   	CUDAPlacerY   rZ   XPUPlacer\   ZCPUrj   )rB   ri   r	   rh   r   r   r   _compileC  s,   


zCompiledProgram._compilec                 C   sn   |d u}|r|D ]}|  |  ksJ dqnt|tjr"t }nt|tjr,t }nt }|s5J d|S )Nz7Place type not match. You may set wrong type of places.zNo places for execution.)_typer2   r   rl   r   rm   r   r   )rB   r	   Z
place_listZhas_set_placer
   r   r   r   _get_placesi  s   zCompiledProgram._get_placesr   )__name__
__module____qualname____doc__rC   rI   rL   rj   rk   rn   rp   r   r   r   r   r1   W   s    
1
 &r1   c                   @   s\   e Zd ZdZg Zdd Ze	dddZedd Zed	d
 Z	edd Z
edd ZdS )IpuDynamicPatcherz1
    Patcher for IPU dynamic2static support.
    c                 C   s   d S r   r   rK   r   r   r   rC     s   zIpuDynamicPatcher.__init__Nc              
      s  ddl ddlm  ddlm} ddlm} j}j}j	}j
 | fdd}jr8|jd	d
 jD ]}	|	j }
|	  }|
| q;|r_dd |dd D ndd |D dd |D jr| _j}|j D ]^\}}| D ]U\}}|jj|jd|j|j|jdd}||}|| |jj|j j!j"ddd W d   n1 sw   Y  #|j }|  }|| ||j| |< qq||fdd}| }|_S )zD
        Convert the ConcreteProgram to IPUConcreteProgram.
        r   NrO   )backward)switch_to_static_graph)device_guardc                     s<   j }  | j j }j }|| | S r   )r   Zgradients_with_optimizer
_optimizerstaticZExecutorCPUPlaceZdefault_startup_programrun)r"   exestartup_program)rv   concrete_programipu_strategypaddler   r   append_backward_desc  s   

zHIpuDynamicPatcher.convert_concrete_program.<locals>.append_backward_descZfloat16)dtypec                 S      g | ]	}|d ur|j qS r   ra   .0elemr   r   r   
<listcomp>      z>IpuDynamicPatcher.convert_concrete_program.<locals>.<listcomp>r   c                 S   r   r   r   r   r   r   r   r     r   c                 S   s   g | ]}|j qS r   r   r   r   r   r   r     s    T)ra   r`   r   r   shapeZbelong_to_optimizerg        )value)initializerc                     sf   j r%jj } dh| _jjj j| dd}jjj  j|d t	 jd
}|S )NZcumsumF)Zuse_fp16_guard)to_fp16_var_names)r   ri   )enable_fp16rz   ampZCustomOpListsZunsupported_listZcast_model_to_fp16r   Zcast_parameters_to_fp16r{   IpuCompiledProgramcompile)Zamp_listr   r"   )r   	feed_list
fetch_listr   r   ri   r   r   func_compile  s*   z@IpuDynamicPatcher.convert_concrete_program.<locals>.func_compile)$r   baserv   Zbase.dygraph.baserw   Zbase.frameworkrx   inputsoutputsr~   rz   global_scoper   to
parametersr/   ra   Z
get_tensorr   Z_share_data_withis_trainingr   ry   Z_accumulatorsitemshelperZcreate_global_variabler   r   r   Z_get_device_for_paramZset_variable_initializernnr   ConstantZfind_var)r   r   class_instancerw   rx   r   r   r~   r   Zparam_or_bufferZparam_or_buffer_tensorZ
src_tensor	optimizerkv
param_nameZvar_tmpr/   ZdeviceZparam_or_lr_tensorZoptim_tensorr   r   r   )rv   r   r   r   r   r   ri   r   convert_concrete_program  sx   




z*IpuDynamicPatcher.convert_concrete_programc                    sb   ddl m ddlm ddlmm m} |j} fdd}||_t	j
|d|g dS )	zMonkey 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                    s   t | stdt|j t|}|| _|| jvsjr`|| jv r'd | jr2js2d | 	|\}}t
||j}|||jd uf| j|< t| j}|kr`d| | 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.zCurrent 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.)r2   rS   r   rq   hashZ_recent_keyZ_cachesneed_compiler_   Z_build_onceru   r   r   r[   rV   )rB   itemZitem_idr   _Zcurrent_tracing_countr   r   r   r   r   r   r   patch_getter  sF   





z;IpuDynamicPatcher.patch_program_cache.<locals>.patch_getter__getitem__N)Zpaddle.jit.dy2staticr   Z$paddle.jit.dy2static.partial_programr   Z'paddle.jit.dy2static.program_translatorr   r   r   r   ru   patcher_cacher    )r   r   Z
old_getterr   r   r   r   patch_program_cache  s   
)z%IpuDynamicPatcher.patch_program_cachec                    s>   ddl m} |jd fdd	}||_tj|dg d S )Nr   LRSchedulerc                    s   | |   d| ji d S )Nlr)set_optionsZlast_lr)rB   epochr   Zold_stepr   r   
patch_step:  s   
z8IpuDynamicPatcher.patch_lr_scheduler.<locals>.patch_stepstepr   )paddle.optimizer.lrr   r   ru   r   r    )r   r   r   r   r   r   patch_lr_scheduler2  s
   z$IpuDynamicPatcher.patch_lr_schedulerc                 C   s   t |  t |  d S r   )ru   r   r   )r   r   r   r   register_patchA  s   
z IpuDynamicPatcher.register_patchc                  C   s"   t jD ]\} }}t| || qd S r   )ru   r   setattr)modulekeyattrr   r   r   release_patchF  s   zIpuDynamicPatcher.release_patchr   )rq   rr   rs   rt   r   rC   staticmethodr   r   r   r   r   r   r   r   r   ru   {  s    j
A

ru   c                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Z				d)ddZ					d*ddZ
d+ddZ	d,ddZdd Zdd Zdd Zdd  Zed!d" Zed#d$ Zed%d& Zed'd( ZdS )-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                 C   sn   t  r"t  | _dddddd}| j| d| _g | _d| _ntdddl	m
} | r5|   d S d S )	Nr   r   )Zon_chipZuse_replicated_tensor_sharding)Zlocation_optimizerZ+accumulation_and_replication_reduction_typeZ4mean_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_ipur   _ipu_strategyr   has_custom_opscustom_op_namesr   re   r   r   r   )rB   Zdefault_optionsr   r   r   r   rC   `  s&   
zIpuStrategy.__init__c                 C   s   t |  dS )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)ru   r   rK   r   r   r   r   x  s   zIpuStrategy.register_patchc                 C   s   t   dS )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)ru   r   rK   r   r   r   r     s   zIpuStrategy.release_patchc                 C   s:   ddl m} | r|| _| |}| j| dS 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   ry   parse_optimizerr   r   re   )rB   r   r   optimizer_attrsr   r   r   set_optimizer  s   
zIpuStrategy.set_optimizerc                    s4    fdd}|g}ddi}|D ]}| |  q|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                     s>   ddl m}  t jtrd jiS t j| rd  iS d S )Nr   r   r   )r   r   r2   Z_learning_ratefloatr   r   r   r   get_lr  s   
z+IpuStrategy.parse_optimizer.<locals>.get_lrZ
is_dynamicT)update)rB   r   r   Zattr_fnr   fnr   r   r   r     s   zIpuStrategy.parse_optimizerr   TFc                 C   s0   |dkr
|r
t d||||d}| | dS )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)re   r   )rB   r   r   r   r   optionsr   r   r   set_graph_config  s   %zIpuStrategy.set_graph_configc                 C   s6   |  d}|s|rtd||||d}| | dS )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)
        r   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_optionre   r   )rB   r   r   r   r   r   r   r   r   r   set_pipelining_config  s   
'z!IpuStrategy.set_pipelining_configc                 C   s   d|i}|  | dS )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   )rB   r   r   r   r   r   set_precision_configI  s   z IpuStrategy.set_precision_configN
custom.opsc                 C   sH   |du r|}||||d}|  d|i | j| | js"d| _dS dS )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   )rB   r   r   r   r   r   r   r   r   add_custom_ope  s   
zIpuStrategy.add_custom_opc                 C   s,   | j | dh}| | rd| _dS dS )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   )rB   r   Zrecompile_white_listr   r   r   r     s
   
zIpuStrategy.set_optionsc                 C   s   | 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   )rB   optionr   r   r   r        zIpuStrategy.get_optionc                 C      | j | dS )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rB   patternr   r   r   r     r   zIpuStrategy.enable_patternc                 C   r   )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_patternr   r   r   r   r     r   zIpuStrategy.disable_patternc                 C   
   |  dS )zJ
        Get the number of IPU devices from IpuStrategy instance.
        r   r   rK   r   r   r   r        
zIpuStrategy.num_ipusc                 C   r   )zU
        Get the boolean of training or inference from IpuStrategy instance.
        r   r   rK   r   r   r   r     r   zIpuStrategy.is_trainingc                 C   r   )zX
        Get the boolean of enable pipelining or not from IpuStrategy instance.
        r   r   rK   r   r   r   r     r   zIpuStrategy.enable_pipeliningc                 C   r   )zS
        Get the boolean of float16 mode or not from IpuStrategy instance.
        r   r   rK   r   r   r   r     r   zIpuStrategy.enable_fp16)r   Tr   F)Fr   Fr   )F)Nr   r   )rq   rr   rs   rt   rC   r   r   r   r   r   r   r   r   r   r   r   r   propertyr   r   r   r   r   r   r   r   r   L  s@     )
3

4
,


r   c                   @   s"   e Zd ZdZdddZdd ZdS )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                 C   s   t  std|d u rt }t|tjstdt| || _	d| _
|d ur,|| _n
dd l}|j | _|d ur>|| _nt | _|jrLt|j| _nd| _t j | _d S )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   rS   r   r   r2   r6   r8   r   r5   r<   r9   r   rz   r   r   r   r   rc   r   _custom_op_namesZ
IpuBackendZget_instance_backend)rB   r"   ri   r   r   r   r   r   rC   B  s2   zIpuCompiledProgram.__init__c                 C   sR  | j | j | j | jj | j }g }t|jD ]\}}|j	
d |jdks/|jdkr4|| q|ddd D ]}|| q<dD ]}||rR|| qF| jj	  t| jj	| _| jjryddg}	|	D ]}
t|
}|| j qkg d	}	|	D ]}
t|
}|
d
kr|d| || j qtd}| jr|d| j || j g d}	|	D ]}
t|
}|d| |d| || j qtd}t }|d| || j tj|}t| jdr| jjj }| jj|_| j }|j!| |j_"| }|D ]}|#|}|j	$d q
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   Zoptimizer_extract_passZoptimizer_state_align_pass)Zforward_graph_extract_passinfer_shape_passZavg_shard_passZdelete_scale_op_passr   r   Zpopart_canonicalization_passZ
custom_ops)Zipu_inplace_passZipu_graph_builder_passZipu_runtime_replacer_passr   Zgraph_to_program_passr"   lr_schedulerorg_program)'r   Z	set_scoper9   Zset_ipu_strategyr   r5   r   r   r   r7   Zset_is_targetr   r    r!   Zhas_varZ_remove_varflushr   r3   r4   r   Zget_passapplyrc   r   ZProgramDescZset_not_ownedr   r6   Z_construct_from_deschasattrr   Z	_var_namer+   Zlr_varr/   Zset_need_check_feedr   r   )rB   r   r   r   Zneed_to_remove_op_indexr#   r   r$   r/   ZpassesZ	pass_nameZa_passZconvert_passr7   r"   Zlr_var_nameZprogram_global_blockZ	feed_nameZfeed_varr   r   r   r   h  st   %












zIpuCompiledProgram.compile)NNN)rq   rr   rs   rt   rC   r   r   r   r   r   r     s    
*&r   )rP   r^   rM   r   r   r   r   r   __all__rf   rX   rW   ZNativeConfigrE   ZAnalysisConfigrF   rY   r   r   r   r%   r)   r0   r1   ru   r   r   r   r   r   r   <module>   s4   
  & R   N