
    [ThL&                        S SK r S SKJr  S SKJr  S SKJr  S SKrS SKJ	s  J
r  S SKJ	s  J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  S	/r\ R.                  " \5      r\R4                   " S
 S5      5       r " S S\R8                  5      r " S S5      rS rS r S r!\RD                  S\\   S\#SS4S j5       r$S r% " S S	5      r&g)    N)defaultdict)Lock)Optional)Tensor)RRef   )functional_optim_mapDistributedOptimizerc                   &    \ rS rSrS\SS4S jrSrg)_ScriptLocalOptimizerInterface"   autograd_ctx_idreturnNc                     g N )selfr   s     Y/var/www/auris/envauris/lib/python3.13/site-packages/torch/distributed/optim/optimizer.pystep#_ScriptLocalOptimizerInterface.step$   s        r   )__name__
__module____qualname____firstlineno__intr   __static_attributes__r   r   r   r   r   "   s    C D r   r   c                   b   ^  \ rS rSr\" 5       rU 4S jr\R                  S\	4S j5       r
SrU =r$ )_ScriptLocalOptimizer(   c                    > [         TU ]  5         U Vs/ s H  oUR                  5       PM     snU l        U" U R                  /UQ70 UD6U l        g s  snf r   )super__init__local_value_local_paramsoptim)r   	optim_clslocal_params_rrefargskwargsrref	__class__s         r   r#   _ScriptLocalOptimizer.__init__.   sP    =NO=NT..0=NOt11CDCFC
 Ps   Ar   c                     [         R                  " U5      nU R                   Vs/ s H  nX2;   a  X#   OS PM     nnU R                  R	                  U5        g s  snf r   )dist_autogradget_gradientsr%   r&   r   )r   r   all_local_gradspgradss        r   r   _ScriptLocalOptimizer.step3   s^    '55oF '')
' #$"6OD@' 	 )

 	

)
s   Ar%   r&   )r   r   r   r   r   compile_lockr#   jitexportr   r   r   __classcell__)r,   s   @r   r   r   (   s1     6LD
 	ZZC  r   r   c                   .    \ rS rSr\" 5       rS rS rSrg)_LocalOptimizerA   c                     U Vs/ s H  oUR                  5       PM     snU l        U" U R                  /UQ70 UD6U l        g s  snf r   )r$   r%   r&   )r   r'   r(   r)   r*   r+   s         r   r#   _LocalOptimizer.__init__K   sD    =NO=NT..0=NOt11CDCFC
 Ps   Ac                     [         R                  " U5      n[        R                     UR	                  5        H  u  p4XCl        M     U R                  R                  5         S S S 5        g ! , (       d  f       g = fr   )r/   r0   r;   global_lockitemsgradr&   r   )r   r   r1   paramrB   s        r   r   _LocalOptimizer.stepO   sO    '55oF((.446!
  7JJOO )((s   :A**
A8r5   N)	r   r   r   r   r   r@   r#   r   r   r   r   r   r;   r;   A   s     &KDr   r;   c                 H    [         R                  " [        X/UQ70 UD65      $ r   )rpcr   r;   )r'   r(   r)   r*   s       r   _new_local_optimizerrG   X   s     88OIR4R6RSSr   c                 F    U R                  5       nUR                  U5        g r   r$   r   local_optim_rrefr   local_optims      r   _local_optimizer_steprM   \   s    "..0K_%r   c                     [        X/UQ70 UD6n[         R                     [        R                  " U5      n[        R
                  " U[        5      sS S S 5        $ ! , (       d  f       g = fr   )r   r6   r7   scriptrF   r   r   )r'   r(   r)   r*   r&   script_optims         r   _new_script_local_optimizerrQ   b   sH    !)PPPE		+	+zz%(xx&DE 
,	+	+s   1A
A)rK   r   r   c                 F    U R                  5       nUR                  U5        g r   rI   rJ   s      r   _script_local_optimizer_steprS   j   s      #..0K_%r   c                     S n/ nU  H#  n UR                  UR                  5       5        M%     Ub  UeU$ ! [         a  nUR                  U5        Un S nAMQ  S nAff = fr   )appendwait	Exception)rpc_futs	exceptionresultsfutes        r   _wait_for_allr]   r   sb    IG	NN388:&  N  	NN1I	s   5
AAAc                   $    \ rS rSrSrS rS rSrg)r
      a	  
DistributedOptimizer takes remote references to parameters scattered
across workers and applies the given optimizer locally for each parameter.

This class uses :meth:`~torch.distributed.autograd.get_gradients` in order
to retrieve the gradients for specific parameters.

Concurrent calls to
:meth:`~torch.distributed.optim.DistributedOptimizer.step`,
either from the same or different clients, will
be serialized on each worker -- as each worker's optimizer can only work
on one set of gradients at a time. However, there is no guarantee that
the full forward-backward-optimizer sequence will execute for one client
at a time. This means that the gradients being applied may not correspond
to the latest forward pass executed on a given worker. Also, there is no
guaranteed ordering across workers.

`DistributedOptimizer` creates the local optimizer with TorchScript enabled
by default, so that optimizer updates are not blocked by the Python Global
Interpreter Lock (GIL) in the case of multithreaded training (e.g. Distributed
Model Parallel). This feature is currently enabled for most optimizers. You
can also follow `the recipe`__ in PyTorch tutorials to enable TorchScript support
for your own custom optimizers.

Args:
    optimizer_class (optim.Optimizer): the class of optimizer to
        instantiate on each worker.
    params_rref (list[RRef]): list of RRefs to local or remote parameters
        to optimize.
    args: arguments to pass to the optimizer constructor on each worker.
    kwargs: arguments to pass to the optimizer constructor on each worker.

Example::
    >>> # xdoctest: +SKIP("distributed")
    >>> import torch.distributed.autograd as dist_autograd
    >>> import torch.distributed.rpc as rpc
    >>> from torch import optim
    >>> from torch.distributed.optim import DistributedOptimizer
    >>>
    >>> with dist_autograd.context() as context_id:
    >>>   # Forward pass.
    >>>   rref1 = rpc.remote("worker1", torch.add, args=(torch.ones(2), 3))
    >>>   rref2 = rpc.remote("worker1", torch.add, args=(torch.ones(2), 1))
    >>>   loss = rref1.to_here() + rref2.to_here()
    >>>
    >>>   # Backward pass.
    >>>   dist_autograd.backward(context_id, [loss.sum()])
    >>>
    >>>   # Optimizer.
    >>>   dist_optim = DistributedOptimizer(
    >>>      optim.SGD,
    >>>      [rref1, rref2],
    >>>      lr=0.05,
    >>>   )
    >>>   dist_optim.step(context_id)

__ https://github.com/pytorch/tutorials/pull/1465
c                 `   [         R                  R                  S5        [        [        5      nU H$  nXVR                  5          R                  U5        M&     U[        ;   a6  [        R                  R                  (       a  [        R                  " U5      nOUnXq:g  U l        U R                  (       a  [        nO[        R                  SU5        [         n/ n	UR#                  5        H1  u  p[$        R&                  " U
UX{4U-   US9nU	R                  U5        M3     [)        U	5      U l        g )Nz,torch.distributed.optim.DistributedOptimizera$  Creating the optimizer %s without TorchScript support, this might result in slow computation time in multithreading environment(i.e. Distributed Model Parallel training on CPU) due to the Python's Global Interpreter Lock (GIL). Please file an issue if you need this optimizer in TorchScript. )r)   r*   )torch_C_log_api_usage_oncer   listownerrU   r	   r7   _state_enabledgetis_functional_optimrQ   loggerwarningrG   rA   rF   	rpc_asyncr]   remote_optimizers)r   optimizer_classparams_rrefr)   r*   per_worker_params_rrefrC   
optim_ctoroptimizer_new_funcremote_optim_futsworkerparam_rrefsremote_optim_rref_futs                r   r#   DistributedOptimizer.__init__   s   $$%ST!,T!2 E";;=188? ! 22szz7J7J-11/BJ(J#-#@ ##!<NN-
   "6#9#?#?#AF$'MM" .5	%! $$%:; $B "//@!Ar   c           	         [         R                  " U5        U R                  (       a  [        O[        nU R
                   Vs/ s H(  n[        R                  " UR                  5       UX14S9PM*     nn[        U5        gs  snf )a  
Performs a single optimization step.

This will call :meth:`torch.optim.Optimizer.step` on each worker
containing parameters to be optimized, and will block until all workers
return. The provided ``context_id`` will be used to retrieve the
corresponding :class:`~torch.distributed.autograd.context` that
contains the gradients that should be applied to the parameters.

Args:
    context_id: the autograd context id for which we should run the
        optimizer step.
)r)   N)
r/   _is_valid_contextri   rS   rM   rm   rF   rl   re   r]   )r   
context_idoptimizer_step_func	optimizerrX   s        r   r   DistributedOptimizer.step   s     	''
3 '' )& 	 "33
 4	 MM!#,
 4 	 
 	h
s   /A?)ri   rm   N)r   r   r   r   __doc__r#   r   r   r   r   r   r
   r
      s    9v#BJ r   )'loggingcollectionsr   	threadingr   typingr   ra   torch.distributed.autograddistributedautogradr/   torch.distributed.rpcrF   	torch.jitr7   torch.nnnnr   r   utilsr	   __all__	getLoggerr   rj   	interfacer   Moduler   r;   rG   rM   rQ   rO   r   rS   r]   r
   r   r   r   <module>r      s     #    2 2 # #    & ' "
"			8	$   
BII 2 .T&F &9:&MP&	& &   r   