o
    h                     @   s   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 deZdd Zdd ZG dd deZG dd deZG dd deZG dd deZdS )    N)DictList   )	variables)unimplemented)istype   )VariableTrackerConstantVariablec                       s(   e Zd Z fddZedd Z  ZS )DistributedVariablec                    s*   t  jdi | t std d S d S )Nz+torch.distributed package is not available! )super__init__r   is_availabler   )selfkwargs	__class__r   Y/var/www/html/ai/venv/lib/python3.10/site-packages/torch/_dynamo/variables/distributed.pyr      s   zDistributedVariable.__init__c                   C   s
   t j S N)torchdistributedr   r   r   r   r   r      s   
z DistributedVariable.is_available)__name__
__module____qualname__r   staticmethodr   __classcell__r   r   r   r   r      s    r   c                 C   s,   t  sdS ddlm} t| o| |ju S )NFr   )DTensor)r   r   torch.distributed._tensorr   inspect
isfunction
from_local)valuer   r   r   r   is_from_local   s   r$   c                 C   s6   t  sdS ddlm}m} ||g}t| o| |v S )NFr   )_get_group_tagget_process_group_ranks)r   r   "torch.distributed.distributed_c10dr%   r&   r    r!   )r#   r%   r&   constant_processgroup_functionsr   r   r   is_constant_pg_functions    s   r)   c                       sB   e Zd Z fddZedd Z							
d fddZ  ZS )PlacementClassVariablec                       t  jdi | || _d S Nr   r   r   r#   r   r#   r   r   r   r   r   2      
zPlacementClassVariable.__init__c                 C   s.   t  sdS ddlm} t| tu ot| |S NFr   )	Placement)r   r   )torch.distributed._tensor.placement_typesr1   type
issubclassr#   r1   r   r   r   is_placement_type6   s   z(PlacementClassVariable.is_placement_typeargsList[VariableTracker]r   Dict[str, VariableTracker]returnr	   c                    s   t | || }t| jdd tjfv r9| jr9t| j}t	|fi |}t| jdd r9|
||d||S t |||S )N__new__r   )r	   	propagatevaluesr    getattr_staticr#   objectr;   sourcePlacementVariableadd_optionscall_methodr   call_function)r   txr7   r   optionsnew_objvarr   r   r   rD   @   s   z$PlacementClassVariable.call_functionr7   r8   r   r9   r:   r	   )r   r   r   r   r   r6   rD   r   r   r   r   r   r*   1   s    
	r*   c                       sJ   e Zd Z fddZedd Zdd Z					
		d fddZ  ZS )rA   c                    r+   r,   r-   r.   r   r   r   r   S   r/   zPlacementVariable.__init__c                 C   "   t  sdS ddlm} t| |S r0   )r   r   r2   r1   r   r5   r   r   r   is_placementW      
zPlacementVariable.is_placementc                 C      | j S r   r#   r   r   r   r   as_python_constanta      z$PlacementVariable.as_python_constantr7   r8   r   r9   r:   r	   c           
         s   ddl m} t| || }ddg}||v rgzt| j}t|dd d u s*J dt||}	W n t	y<   d }	Y nw |	t
ju rI|di |S dd |D }d	d
 | D }|	| jg|R i | | S t ||||S )Nr   r
   r   __setattr____getattr__zno custom getattr allowed!c                 S   s   g | ]}|  qS r   rP   ).0xr   r   r   
<listcomp>}   s    z1PlacementVariable.call_method.<locals>.<listcomp>c                 S   s   i | ]	\}}||  qS r   rT   )rU   kvr   r   r   
<dictcomp>~   s    z1PlacementVariable.call_method.<locals>.<dictcomp>r   ) r   r	   r<   r=   r3   r#   r    r>   AttributeErrorr?   r   itemsr   rC   )
r   rE   namer7   r   r   rF   allowed_methods
value_typemethodr   r   r   rC   d   s(   

zPlacementVariable.call_methodrI   )	r   r   r   r   r   rK   rP   rC   r   r   r   r   r   rA   R   s    
	rA   c                       sF   e Zd Z fddZedd Zdd Zdedef fd	d
Z	  Z
S )DeviceMeshVariablec                    r+   r,   r-   r.   r   r   r   r      r/   zDeviceMeshVariable.__init__c                 C   rJ   )NFr   )
DeviceMesh)r   r   %torch.distributed._tensor.device_meshrc   r   )r#   rc   r   r   r   is_device_mesh   rL   z!DeviceMeshVariable.is_device_meshc                 C   rM   r   rN   rO   r   r   r   rP      rQ   z%DeviceMeshVariable.as_python_constantr^   r:   c                    s"   |dkr
t | jjS t ||S )Nndim)r   r#   rf   r   var_getattrr   rE   r^   r   r   r   rg      s   zDeviceMeshVariable.var_getattr)r   r   r   r   r   re   rP   strr	   rg   r   r   r   r   r   rb      s    
	rb   c                       sb   e Zd ZdZ fddZdd Zdd Z				
			d fddZ fddZe	dd Z
  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                    r+   r,   r-   r.   r   r   r   r      r/   zProcessGroupVariable.__init__c                 C   rM   r   rN   rO   r   r   r   rP      rQ   z'ProcessGroupVariable.as_python_constantc                 C   s
   t | jS r   )r3   r#   rO   r   r   r   python_type   s   
z ProcessGroupVariable.python_typer7   r8   r   r9   r:   r	   c                    sB   |dkrt | j S |dkrt | j S t ||||S )Nranksize)r   r   r#   rl   rm   r   rC   )r   rE   r^   r7   r   r   r   r   rC      s
   z ProcessGroupVariable.call_methodc                    s2    dv rt  fddS t  S )N)rl   rm   c                     s     | |S r   )rC   )r7   r   r^   r   rE   r   r   <lambda>   s    z2ProcessGroupVariable.var_getattr.<locals>.<lambda>)r   LambdaVariablerB   r   rg   rh   r   rn   r   rg      s   z ProcessGroupVariable.var_getattrc                 C   rJ   )NFr   )ProcessGroup)r   r   torch._C._distributed_c10drq   r   )r#   rq   r   r   r   is_process_group   s   
z%ProcessGroupVariable.is_process_grouprI   )r   r   r   __doc__r   rP   rk   rC   rg   r   rs   r   r   r   r   r   rj      s    rj   )r    typingr   r   r   r[   r   excr   utilsr   baser	   constantr   r   r$   r)   r*   rA   rb   rj   r   r   r   r   <module>   s    !3