
    PhA                         d dl Z d dlmZmZ d dlZddlmZ ddlmZ ddl	m
Z
 ddlmZ dd	lmZ  G d
 de      Zd Zd Z G d de      Z G d de      Z G d de      Z G d de      Zy)    N)DictList   )	variables)unimplemented)istype   )VariableTrackerConstantVariablec                   .     e Zd Z fdZed        Z xZS )DistributedVariablec                 d    t        |   di | t        j                         st	        d       y y )Nz+torch.distributed package is not available! )super__init__r   is_availabler   )selfkwargs	__class__s     nC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/_dynamo/variables/distributed.pyr   zDistributedVariable.__init__   s-    "6""//1GH 2    c                  >    t         j                  j                         S N)torchdistributedr   r   r   r   r   z DistributedVariable.is_available   s       --//r   )__name__
__module____qualname__r   staticmethodr   __classcell__r   s   @r   r   r      s    I
 0 0r   r   c                     t         j                         syddlm} t	        j
                  |       xr | |j                  u S )NFr   )DTensor)r   r   torch.distributed._tensorr$   inspect
isfunction
from_local)valuer$   s     r   is_from_localr*      s5    ++-1e$D'2D2D)DDr   c                 z    t         j                         syddlm}m} ||g}t        j                  |       xr | |v S )NFr   )_get_group_tagget_process_group_ranks)r   r   "torch.distributed.distributed_c10dr,   r-   r&   r'   )r)   r,   r-   constant_processgroup_functionss       r   is_constant_pg_functionsr0       sD    ++- 	 '#
 e$Q2Q)QQr   c                   F     e Zd Z fdZed        Z	 	 	 	 	 	 d fdZ xZS )PlacementClassVariablec                 2    t        |   di | || _        y Nr   r   r   r)   r   r)   r   r   s      r   r   zPlacementClassVariable.__init__2       "6"
r   c                 v    t         j                         syddlm} t	        |       t        u xr t        | |      S NFr   )	Placement)r   r   )torch.distributed._tensor.placement_typesr:   type
issubclassr)   r:   s     r   is_placement_typez(PlacementClassVariable.is_placement_type6   s1     #//1GE{d"Cz%'CCr   c                 d   t        j                  | j                  dd       t        j                  fv rm| j
                  rat        j	                  | j                        }t        |      }t        j                  | j                  dd       r|j                  |d||       |S t        | %  |||      S )N__new__r   )
r&   getattr_staticr)   objectrA   sourcePlacementVariablecall_methodr   call_function)r   txargsr   new_objvarr   s         r   rG   z$PlacementClassVariable.call_function@   s     ""4::y$?FNNCTT nnTZZ0G#G,C%%djj*dCJf=
w$Rv66r   rI   zList[VariableTracker]r   zDict[str, VariableTracker]returnr
   )r   r   r   r   r    r?   rG   r!   r"   s   @r   r2   r2   1   s@     D D7/79U7	7 7r   r2   c                   L     e Zd Z fdZed        Zd Z	 	 	 	 	 	 d fdZ xZS )rE   c                 2    t        |   di | || _        y r4   r5   r6   s      r   r   zPlacementVariable.__init__S   r7   r   c                 P    t         j                         syddlm} t	        | |      S r9   )r   r   r;   r:   
isinstancer>   s     r   is_placementzPlacementVariable.is_placementW   s#     #//1G%++r   c                     | j                   S r   r)   r   s    r   as_python_constantz$PlacementVariable.as_python_constanta       zzr   c                 $   ddl m} ddg}||v r	 t        | j                        }t	        j
                  |dd       J d       t	        j
                  ||      }|t        j                  u r |j                  d       S |D 	cg c]  }	|	j                          }}	|j                         D 
ci c]  \  }
}|
|j                          }}
} || j                  g|i | | S t        | 5  ||||      S # t        $ r d }Y w xY wc c}	w c c}}
w )Nr	   r   r   __setattr____getattr__zno custom getattr allowed!) r   r<   r)   r&   rB   AttributeErrorrC   r   createrV   itemsr   rF   )r   rH   namerI   r   r   allowed_methods
value_typemethodxkvr   s               r   rF   zPlacementVariable.call_methodd   s    	'%}5 ?"!$**-
**:}dKS0/0S //
DA (.'..t44489DqA((*DD9<BLLNKNDAqa--//NFK4::///Kw"2tT6:: " 
 :Ks   A	C6 D.D6DDrL   )	r   r   r   r   r    rR   rV   rF   r!   r"   s   @r   rE   rE   R   sI     , ,; &	;
 -; 
; ;r   rE   c                   H     e Zd Z fdZed        Zd Zdedef fdZ	 xZ
S )DeviceMeshVariablec                 2    t        |   di | || _        y r4   r5   r6   s      r   r   zDeviceMeshVariable.__init__   r7   r   c                 P    t         j                         syddlm} t	        | |      S )NFr   )
DeviceMesh)r   r   torch.distributed.device_meshrj   r   )r)   rj   s     r   is_device_meshz!DeviceMeshVariable.is_device_mesh   s#     #//1<eZ((r   c                     | j                   S r   rT   rU   s    r   rV   z%DeviceMeshVariable.as_python_constant   rW   r   r_   rM   c                     |dk(  r)t        j                  | j                  j                        S t        |   ||      S )Nndim)r   r]   r)   ro   r   var_getattrr   rH   r_   r   s      r   rp   zDeviceMeshVariable.var_getattr   s5    6>#**4::??;;w"2t,,r   )r   r   r   r   r    rl   rV   strr
   rp   r!   r"   s   @r   rg   rg      s9     ) )-C -O - -r   rg   c                   `     e Zd ZdZ fdZd Zd Z	 	 	 	 	 	 d fdZ fdZe	d        Z
 xZS )	ProcessGroupVariablea  
    We don't want a ProcessGroup object to end up in our output graph.

    But it's common for dynamo to intercept a PG that is then used to get info like
    rank() or world_size(), as well as passed to utility functions in distributed_c10d
    which desugar it into plain types like a ranklist and tag.

    For convenience and proper guarding, we construct a variable type.

    TODO: make it possible to use ProcessGroupVariable as input to simple functions
          like _expand_group without dynamo complaining about making a proxy for it.
          It is not a tensor-like type, and we don't want a proxy- but dynamo assumes
          torch library functions are dealing with tensor-like types and would have proxies
          for their args.
    TODO: should we make this inherit VT instead of UDOV? Do we want any of the default behaviors
          or just graph-break whenever one of our special cases is not hit?
    c                 2    t        |   di | || _        y r4   r5   r6   s      r   r   zProcessGroupVariable.__init__   r7   r   c                     | j                   S r   rT   rU   s    r   rV   z'ProcessGroupVariable.as_python_constant   rW   r   c                 ,    t        | j                        S r   )r<   r)   rU   s    r   python_typez ProcessGroupVariable.python_type   s    DJJr   c                    |dk(  r7t         j                  j                  | j                  j	                               S |dk(  r7t         j                  j                  | j                  j                               S t        |   ||||      S )Nranksize)r   r   r]   r)   rz   r{   r   rF   )r   rH   r_   rI   r   r   s        r   rF   z ProcessGroupVariable.call_method   sm     6>--44TZZ__5FGG6>--44TZZ__5FGGw"2tT6::r   c                 f     dv rt        j                   fd      S t                 S )N)rz   r{   c                  ,    j                  | |      S r   )rF   )rI   r   r_   r   rH   s     r   <lambda>z2ProcessGroupVariable.var_getattr.<locals>.<lambda>   s    (8(8T4(Pr   )r   LambdaVariabler   rp   rq   s   ```r   rp   z ProcessGroupVariable.var_getattr   s6    ##++P  w"2t,,r   c                 `    t         j                         syddlm} ddlm} t        | ||f      S )NFr   )ProcessGroup)FakeProcessGroup)r   r   torch._C._distributed_c10dr   +torch.testing._internal.distributed.fake_pgr   r   )r)   r   r   s      r   is_process_groupz%ProcessGroupVariable.is_process_group   s,     #//1;Pel,<=>>r   rL   )r   r   r   __doc__r   rV   rx   rF   rp   r    r   r!   r"   s   @r   rt   rt      sS    $ ; &	;
 -; 
;- ? ?r   rt   )r&   typingr   r   r   r[   r   excr   utilsr   baser
   constantr   r   r*   r0   r2   rE   rg   rt   r   r   r   <module>r      sk          ! &	0/ 	0ER"70 7B/;+ /;d-, -0;?. ;?r   