
    ϑi*+                         S SK r S SKJr  S SKJr  S SKJr  S SKJr  S SK	J
r
  / r " S S5      r " S	 S
\ R                  S9r " S S\ R                  S9rg)    N)base)ExecutorRoleMakerBase)SGD)OptimizerWithMixedPrecisionc                   $    \ rS rSrSrSrSrSrSrg)Mode   zQ
There are various mode for fleet, each of them is designed for different model.
          N)	__name__
__module____qualname____firstlineno____doc__
TRANSPILERPSLIB
COLLECTIVE__static_attributes__r       f/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/incubate/distributed/fleet/base.pyr
   r
      s     JEJr   r
   c                      \ rS rSrSrS rS rS rS rS r	SS jr
S	 rS
 rSS jrS rS rS rSS jrS rS r\R*                  S 5       r\R*                  SS j5       r\R*                  S 5       r\R*                  S 5       r\R*                  SS j5       r\R*                     SS j5       r\R*                  SS j5       rSrg)Fleet$   z
Fleet is the base class, transpiler and pslib are implementation of Fleet.

Args:
    mode(Mode): the implementation of Fleet's mode.

Returns:
    None
c                 H    SU l         Xl        S U l        S U l        S U l        g )NF)_is_initialized_mode
_optimizer_role_maker	_executor)selfmodes     r   __init__Fleet.__init__/   s%    $
r   c                 6    U R                   R                  5       $ )z
Check whether the node is the first instance of worker.

Returns:
    bool: True if this is the first node of worker,
          False if not.
)r"   is_first_workerr$   s    r   r)   Fleet.is_first_worker6   s     //11r   c                 6    U R                   R                  5       $ )z6
Get current worker index.

Returns:
    int: node id
)r"   worker_indexr*   s    r   r-   Fleet.worker_index@        ,,..r   c                 6    U R                   R                  5       $ )zD
Get current total worker number.

Returns:
    int: worker numbers
)r"   
worker_numr*   s    r   r1   Fleet.worker_numI   s     **,,r   c                 6    U R                   R                  5       $ )z
Check whether the node is an instance of worker.

Returns:
    bool: True if this is a node of worker,
          False if not.
)r"   	is_workerr*   s    r   r4   Fleet.is_workerR        ))++r   c                     U(       a)  SR                  U R                  R                  5       5      $ U R                  R                  5       $ zy
Get current server endpoints, such as ["127.0.0.1:1001", "127.0.0.1:1002"].

Returns:
    list/string: server endpoints
,)joinr"   get_trainer_endpointsr$   	to_strings     r   worker_endpointsFleet.worker_endpoints\   :     88D,,BBDEE##99;;r   c                 H    [        U R                  R                  5       5      $ )zC
Get current total worker number.

Returns:
    int: server number
)lenr"   get_pserver_endpointsr*   s    r   
server_numFleet.server_numi   s     4##99;<<r   c                 6    U R                   R                  5       $ )z6
Get current server index.

Returns:
    int: node id
)r"   server_indexr*   s    r   rG   Fleet.server_indexr   r/   r   c                     U(       a)  SR                  U R                  R                  5       5      $ U R                  R                  5       $ r8   )r:   r"   rC   r<   s     r   server_endpointsFleet.server_endpoints{   r@   r   c                 6    U R                   R                  5       $ )z
Check whether the node is an instance of server.

Returns:
    bool: True if this is a node of server,
          False if not
)r"   	is_serverr*   s    r   rM   Fleet.is_server   r6   r   c                 6    U R                   R                  5       $ )z
Check whether the node is an instance of server.

Returns:
    bool: True if this is a node of server,
          False if not.
)r"   is_xpur*   s    r   rP   Fleet.is_xpu   s     &&((r   c                 b   [        U[        5      (       d  [        S5      eU R                  5       nU R	                  5       n[        U5      U-  n[        U5      U-  nU/U-  n[        U5       H  nXg==   S-  ss'   M     / /U-  nSn	[        U5       H  nXXU   -    X'   XU   -  n	M     X   $ )a  
split files before distributed training,
example 1: files is [a, b, c ,d, e]  and trainer_num = 2, then trainer
           0 gets [a, b, c] and trainer 1 gets [d, e].
example 2: files is [a, b], and trainer_num = 3, then trainer 0 gets
           [a], trainer 1 gets [b],  trainer 2 gets []

Args:
    files(list): file list need to be read.

Returns:
    list: files belongs to this worker.
z/files should be a list of file need to be read.r   r   )
isinstancelist	TypeErrorr-   r1   rB   range)
r$   files
trainer_idtrainers	remainder	blocksizeblocksitrainer_filesbegins
             r   split_filesFleet.split_files   s     %&&MNN&&(
??$J)	J(*	x'y!AINI " xxA$UAY->?MAYE ! ((r   Nc                     [        [        R                  " 5       5      U l        U(       a=  [	        U[
        5      (       d(  SSKJn  U(       a  [	        X5      (       d  [        S5      eXl        U R                  R                  5         SU l
        g)z
should be called only once in user's python scripts,
init() will initialize RoleMaker which is used for identifying
    current node's role, e.g. worker, server, etc.

Args:
    role_maker(RoleMakerBase): subclass of RoleMakerBase.

Returns:
    None
r   r   z/role_maker must be an instance of RoleMakerBaseTN)r   r   CPUPlacer#   rS   r   ,paddle.incubate.distributed.fleet.role_makerrU   r"   generate_roler   )r$   
role_makerRoleMakerBaseIncubates      r   init
Fleet.init   sk     "$--/2j]CC *Z"O"OE  &&&(#r   c                 :    U R                   R                  X5        g)z
all reduce between workers, only support array of one dim.

Args:
    input(list|numpy.array): array of one dim
    output(list|numpy.array): array of one dim
N)r"   all_reduce_worker)r$   inputoutputs      r   rk   Fleet.all_reduce_worker   s     	**59r   c                 8    U R                   R                  5         g)z
barrier between workers
N)r"   barrier_workerr*   s    r   rp   Fleet.barrier_worker   s     	'')r   c                     g Nr   r*   s    r   init_workerFleet.init_worker       r   c                     g rs   r   )r$   	model_dirkwargss      r   init_serverFleet.init_server   rv   r   c                     g rs   r   r*   s    r   
run_serverFleet.run_server   rv   r   c                     g rs   r   r*   s    r   stop_workerFleet.stop_worker   rv   r   c                     g rs   r   r$   	optimizerstrategys      r   distributed_optimizerFleet.distributed_optimizer   rv   r   c                     g rs   r   )r$   executordirnamefeeded_var_namestarget_varsmain_programexport_for_deploymentlegacy_formats           r   save_inference_modelFleet.save_inference_model   s     	r   c                     g rs   r   )r$   r   r   r   s       r   save_persistablesFleet.save_persistables  rv   r   )r#   r   r    r!   r"   )Frs   )NTF)r   r   r   r   r   r&   r)   r-   r1   r4   r>   rD   rG   rJ   rM   rP   r`   rh   rk   rp   abcabstractmethodrt   rz   r}   r   r   r   r   r   r   r   r   r   r   $   s   2/-,<=/<,)!)F$8:* 	  	  	  	  	  	 "
 
 	 r   r   )	metaclassc                       \ rS rSrSrS	S jr\R                      S
S j5       r\R                  S 5       r	\R                      S
S j5       r
Srg)DistributedOptimizeri  a  
DistributedOptimizer is a wrapper for paddle.base.optimizer
A user should pass a paddle.base.optimizer to DistributedOptimizer
minimize() function is implemented.
DistributedOptimizer is the starting point for a user who wants to
run distributed training. The optimized information will be stored in
Fleet() instance who holds the global information about current distributed
training.

Args:
    optimizer(Optimizer): subclass of Optimizer.
    strategy(any): the user define config for Optimizer.

Returns:
    None

Nc                     [        U[        R                  5      (       d   [        U[        5      (       d  [	        S5      eXl        X l        g )Nz*optimizer must be an instance of Optimizer)rS   r   	__bases__r   rU   r!   	_strategyr   s      r   r&   DistributedOptimizer.__init__$  s=    )S]]33J2=
 =
 HII#!r   c                     g)ai  
First part of `minimize`, do auto-diff to append backward ops for
the current program.

Args:
    loss (Variable): loss variable to run optimizations.
    startup_program (Program): startup_program for initializing parameters
        in `parameter_list`.
    parameter_list (list): list of Variables to update.
    no_grad_set (set|None): set of Variables should be ignored.
    callbacks (list|None): list of callables to run when appending backward
        operator for one parameter.

Return:
    list: list of (param, grad) pair, grad is the output of backward.

Examples:
    See examples in `apply_gradients`.
Nr   )r$   lossstartup_programparameter_listno_grad_set	callbackss         r   backwardDistributedOptimizer.backward-  s    8 	r   c                     g)ak  
Second part of `minimize`, appending optimization operators for
given `params_grads` pairs.

Args:
    params_grads (list): list of (param, grad) pair to do optimization.

Returns:
    list: A list of operators appended to the current program.

Examples:
    .. code-block:: python

        >>> # doctest: +SKIP('The network is not defined.')
        >>> loss = network()
        >>> optimizer = base.optimizer.SGD(learning_rate=0.1)
        >>> params_grads = optimizer.backward(loss)
        >>> # you may append operations for params_grads here
        >>> # ...
        >>> optimizer.apply_gradients(params_grads)
Nr   )r$   params_gradss     r   apply_gradients$DistributedOptimizer.apply_gradientsK  s    . 	r   c                     g)a  
Add operations to minimize `loss` by updating `parameter_list`.

This method combines interface `backward()` and
`apply_gradients()` into one.

Args:
    losses (Variable|Variable List): loss variable to run optimizations.
    scopes (Scope| Scope List): scope instance.
    startup_programs (Program|Program List): startup_program for initializing parameters
        in `parameter_list`.
    parameter_list (list): list of Variables to update.
    no_grad_set (set|None): set of Variables should be ignored.

Returns:
    tuple: (optimize_ops, params_grads) which are, list of operators appended;
    and list of (param, grad) Variables pair for optimization.
Nr   )r$   lossesscopesstartup_programsr   r   s         r   minimizeDistributedOptimizer.minimized  s    6 	r   )r!   r   rs   )NNNN)r   r   r   r   r   r&   r   r   r   r   r   r   r   r   r   r   r     sv    $" 	  : 	 0 	  r   r   )r   paddler   paddle.base.executorr   (paddle.distributed.fleet.base.role_makerr   paddle.optimizerr   paddle.static.amp.decoratorr   __all__r
   ABCMetar   r   r   r   r   <module>r      sM      ) B   C
 jckk jZnS[[ nr   