U
    9%e                     @   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f | t std d S )Nz+torch.distributed package is not available!)super__init__r   is_availabler   )selfkwargs	__class__ b/var/www/html/Darija-Ai-API/env/lib/python3.8/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kS )NFr   )DTensor)r   r   Ztorch.distributed._tensorr   inspect
isfunctionZ
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| o4| |kS )NFr   )_get_group_tagget_process_group_ranks)r   r   Z"torch.distributed.distributed_c10dr#   r$   r   r    )r!   r#   r$   Zconstant_processgroup_functionsr   r   r   is_constant_pg_functions    s    r%   c                       s>   e Zd Z fddZedd Zdddd fd	d
Z  ZS )PlacementClassVariablec                    s   t  jf | || _d S r   r   r   r!   r   r!   r   r   r   r   r   2   s    zPlacementClassVariable.__init__c                 C   s.   t  sdS ddlm} t| tko,t| |S NFr   )	Placement)r   r   )torch.distributed._tensor.placement_typesr*   type
issubclassr!   r*   r   r   r   is_placement_type6   s    z(PlacementClassVariable.is_placement_typeList[VariableTracker]Dict[str, VariableTracker]r	   argsr   returnc                    s~   t | || }t| jdd tjfkrn| jrnt| j}t	|f|}t| jdd rn|
||d||S t |||S )N__new__r   )r	   	propagatevaluesr   getattr_staticr!   objectr5   sourcePlacementVariableadd_optionscall_methodr   call_function)r   txr3   r   optionsZnew_objvarr   r   r   r>   @   s    z$PlacementClassVariable.call_function)r   r   r   r   r   r/   r>   r   r   r   r   r   r&   1   s   

 r&   c                       sF   e Zd Z fddZedd Zdd Zddd	d
 fddZ  ZS )r;   c                    s   t  jf | || _d S r   r'   r(   r   r   r   r   S   s    zPlacementVariable.__init__c                 C   s"   t  sdS ddlm} t| |S r)   )r   r   r+   r*   r   r.   r   r   r   is_placementW   s    zPlacementVariable.is_placementc                 C   s   | j S r   r!   r   r   r   r   as_python_constanta   s    z$PlacementVariable.as_python_constantr0   r1   r	   r2   c           
         s   ddl m} t| || }ddg}||krz4t| j}t|dd d ksTt	dt||}	W n t
k
rz   d }	Y nX |	tjkr|d|S dd |D }d	d
 | D }|	| jf|| | S t ||||S )Nr   r
   r   __setattr____getattr__zno custom getattr allowed!c                 S   s   g | ]}|  qS r   rE   ).0xr   r   r   
<listcomp>}   s     z1PlacementVariable.call_method.<locals>.<listcomp>c                 S   s   i | ]\}}||  qS r   rH   )rI   kvr   r   r   
<dictcomp>~   s      z1PlacementVariable.call_method.<locals>.<dictcomp>)N) r   r	   r6   r7   r,   r!   r   r8   AssertionErrorAttributeErrorr9   r   itemsr   r=   )
r   r?   namer3   r   r   r@   allowed_methodsZ
value_typemethodr   r   r   r=   d   s(    



zPlacementVariable.call_method)	r   r   r   r   r   rB   rE   r=   r   r   r   r   r   r;   R   s   
	r;   c                       sD   e Zd Z fddZedd Zdd Zeed fdd	Z	  Z
S )
DeviceMeshVariablec                    s   t  jf | || _d S r   r'   r(   r   r   r   r      s    zDeviceMeshVariable.__init__c                 C   s"   t  sdS ddlm} t| |S )NFr   )
DeviceMesh)r   r   Z%torch.distributed._tensor.device_meshrW   r   )r!   rW   r   r   r   is_device_mesh   s    z!DeviceMeshVariable.is_device_meshc                 C   s   | j S r   rC   rD   r   r   r   rE      s    z%DeviceMeshVariable.as_python_constant)rS   r4   c                    s"   |dkrt | jjS t ||S )Nndim)r   r!   rY   r   var_getattrr   r?   rS   r   r   r   rZ      s    zDeviceMeshVariable.var_getattr)r   r   r   r   r   rX   rE   strr	   rZ   r   r   r   r   r   rV      s
   
	rV   c                       s^   e Zd ZdZ fddZdd Zdd Zdd	d
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                    s   t  jf | || _d S r   r'   r(   r   r   r   r      s    zProcessGroupVariable.__init__c                 C   s   | j S r   rC   rD   r   r   r   rE      s    z'ProcessGroupVariable.as_python_constantc                 C   s
   t | jS r   )r,   r!   rD   r   r   r   python_type   s    z ProcessGroupVariable.python_typer0   r1   r	   r2   c                    sB   |dkrt | j S |dkr0t | j S t ||||S )Nranksize)r   r   r!   r_   r`   r   r=   )r   r?   rS   r3   r   r   r   r   r=      s
    z ProcessGroupVariable.call_methodc                    s2    dkr$t  fddS t  S )N)r_   r`   c                     s     | |S r   )r=   )r3   r   rS   r   r?   r   r   <lambda>       z2ProcessGroupVariable.var_getattr.<locals>.<lambda>)r   ZLambdaVariabler<   r   rZ   r[   r   ra   r   rZ      s    z ProcessGroupVariable.var_getattrc                 C   s"   t  sdS ddlm} t| |S )NFr   )ProcessGroup)r   r   Ztorch._C._distributed_c10drd   r   )r!   rd   r   r   r   is_process_group   s    z%ProcessGroupVariable.is_process_group)r   r   r   __doc__r   rE   r^   r=   rZ   r   re   r   r   r   r   r   r]      s   r]   )r   typingr   r   r   rO   r   excr   utilsr   baser	   Zconstantr   r   r"   r%   r&   r;   rV   r]   r   r   r   r   <module>   s   !3