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ly in TensorFlow.

It is sometimes useful to be able to build HLO programs directly from
TensorFlow. This file provides Tensorflow operators that mirror the semantics of
HLO operators as closely as possible.

Note: There is no promise of backward or forward compatibility for operators
defined in this module. This is primarily because the underlying HLO operators
do not promise backward or forward compatibility.
é    )Úabsolute_import)Údivision)Úprint_function)Úgen_xla_ops)Úconstant_op)Údtypes)Úops)Ú	array_ops)Úbitwise_ops)Úgen_math_ops)Úgen_random_ops)Úmath_ops)Ú
random_ops)Úspecial_math_opsc                s   d‡ fdd„	}|S )z:Wrapper that restricts `fn` to have the correct signature.Nc                s   ˆ | |dS )N)Úname© )Úxr   )Úfnr   úT/home/dcms/DCMS/lib/python3.7/site-packages/tensorflow/compiler/tf2xla/python/xla.pyÚunary_op_wrapperH   s    z#_unary_op.<locals>.unary_op_wrapper)Nr   )r   r   r   )r   r   Ú	_unary_opE   s    r   c                s   d‡ fdd„	}|S )zGWraps a binary Tensorflow operator and performs XLA-style broadcasting.Nc                s6   |pg }t  |tj¡}t | ||¡\} }ˆ | ||dS )zInner wrapper function.)r   )r   Úconvert_to_tensorr   Úint64r   Zxla_broadcast_helper)r   ÚyZbroadcast_dimsr   )r   r   r   Úbroadcasting_binary_op_wrappery   s    z?_broadcasting_binary_op.<locals>.broadcasting_binary_op_wrapper)NNr   )r   r   r   )r   r   Ú_broadcasting_binary_opv   s    
r   Nc             C   sf   |j | j kst‚| j }|tk}|rBt| }t | |¡} t ||¡}tj| ||d}|rbt ||¡}|S )zCPerforms an integer right logical shift irrespective of input type.)r   )ÚdtypeÚAssertionErrorÚ_SIGNED_TO_UNSIGNED_TABLEr   Úcastr
   Úright_shift)r   r   r   r   ÚsignedZunsigned_dtypeÚoutputr   r   r   Ú_shift_right_logical_helper—   s    r#   c             C   sf   |j | j kst‚| j }|tk}|rBt| }t | |¡} t ||¡}tj| ||d}|rbt ||¡}|S )zFPerforms an integer right arithmetic shift irrespective of input type.)r   )r   r   Ú_UNSIGNED_TO_SIGNED_TABLEr   r   r
   r    )r   r   r   r   ZunsignedZsigned_dtyper"   r   r   r   Ú_shift_right_arithmetic_helper¦   s    r%   c                s   d‡ fdd„	}|S )z:Wrapper that restricts `fn` to have the correct signature.Nc                s   ˆ | ||dS )N)r   r   )r   r   r   )r   r   r   Úbinary_op_wrapperÕ   s    z%_binary_op.<locals>.binary_op_wrapper)Nr   )r   r&   r   )r   r   Ú
_binary_opÒ   s    r'   c             C   s8   t  | ¡} tjt |¡t | ¡gdd}tj| ||dS )Nr   )Zaxis)r   )r   r   r	   Úconcatr   ÚconstantÚshapeZbroadcast_to)r   Údimsr   r*   r   r   r   Ú	broadcastá   s
    

r,   c             C   s   t t| ||d||dS )N)r   )ÚminÚmax)Úar   Úbr   r   r   r   Úclampé   s    r1   é   c
             C   s2   d}
|r|  ¡ }
tj| |||||||  ¡ |
|	d
S )a9  Wraps the XLA ConvGeneralDilated operator.

  ConvGeneralDilated is the most general form of XLA convolution and is
  documented at
  https://www.tensorflow.org/performance/xla/operation_semantics#conv_convolution

  Args:
    lhs: the input tensor
    rhs: the kernel tensor
    window_strides: the inter-window strides
    padding: the padding to apply at the start and end of each input dimensions
    lhs_dilation: dilation to apply between input elements
    rhs_dilation: dilation to apply between kernel elements
    dimension_numbers: a `ConvolutionDimensionNumbers` proto.
    feature_group_count: number of feature groups for grouped convolution.
    precision_config: a `xla.PrecisionConfig` proto.
    name: an optional name for the operator

  Returns:
    A tensor representing the output of the convolution.
  Ú )Úwindow_stridesÚpaddingÚlhs_dilationÚrhs_dilationÚfeature_group_countÚdimension_numbersÚprecision_configr   )ÚSerializeToStringr   Zxla_conv)ÚlhsÚrhsr4   r5   r6   r7   r9   r8   r:   r   Úprecision_config_protor   r   r   Úconvð   s    r?   c             C   s   t j| |d|dS )Nr2   )Zaxesr   )r   Z	tensordot)r<   r=   r   r   r   r   Údot"  s    r@   c             C   s(   d}|r|  ¡ }tj| ||  ¡ ||dS )Nr3   )r9   r:   r   )r;   r   Zxla_dot)r<   r=   r9   r:   r   r>   r   r   r   Údot_general&  s    rA   c             C   s   t  | |||¡S )N)r   Zxla_self_adjoint_eig)r/   ÚlowerÚmax_iterÚepsilonr   r   r   Úself_adjoint_eig2  s    rE   c             C   s    d}|r|  ¡ }t | |||¡S )Nr3   )r;   r   Zxla_svd)r/   rC   rD   r:   r>   r   r   r   Úsvd6  s    rF   c             C   s    t  | ¡} tj|| || j|dS )N)ZmeanÚstddevr   r   )r   r   r   Úrandom_normalr   )ÚmuÚsigmar+   r   r   r   r   rH   F  s    
rH   c             C   s    t  | ¡} tj|| || j|dS )N)r   r   )r   r   r   Úrandom_uniformr   )ÚminvalÚmaxvalr+   r   r   r   r   rK   L  s    
rK   c	       	      C   sd   |pdgt |ƒ }|p"dgt |ƒ }|p4dgt |ƒ }|pFdgt |ƒ }tj| ||||||||d	S )aN  Wraps the XLA ReduceWindow operator.

  ReduceWindow is documented at
  https://www.tensorflow.org/performance/xla/operation_semantics#reducewindow .

  Args:
    operand: the input tensor
    init: a scalar tensor representing the initial value for the reduction
    reducer: a reduction function that combines a pair of scalars.
    window_dimensions: shape of the window, as a list of integers
    window_strides: inter-window strides, as a list of integers. Optional; if
      omitted, defaults to strides of 1.
    padding: padding to apply to 'operand'. List of (low, high) pairs of
      integers that specify the padding to apply before and after each
      dimension. Optional; if omitted, defaults to no padding.
    name: the operator name, or None.

  Returns:
    A tensor that represents the output of the reduce_window operator.
  r2   )r   r   )	ÚinputZ
init_valueÚwindow_dimensionsr4   Úbase_dilationsÚwindow_dilationsr5   Zcomputationr   )Úlenr   Zxla_reduce_window)	ÚoperandÚinitZreducerrO   r4   rP   rQ   r5   r   r   r   r   Úreduce_windowV  s    rU   c             C   s(   |d k	rt  | |¡} t j| ||d} | S )N)r   )r	   Ú	transposeÚreshape)r   Z	new_sizesZ
dimensionsr   r   r   r   rW   †  s    rW   c             C   s   t  | |||¡S )N)r	   Úwhere)Ú	conditionr   r   r   r   r   r   Úselect  s    rZ   c             C   s"   dd„ t |||ƒD ƒ}| t|ƒ S )Nc             S   s   g | ]\}}}t |||ƒ‘qS r   )Ú_slice)Ú.0ÚstartÚlimitZstrider   r   r   ú
<listcomp>—  s   zslice.<locals>.<listcomp>)ÚzipÚtuple)r   Z
start_dimsZ
limit_dimsÚstridesÚspecr   r   r   Úslice•  s    rd   ZXlaShardingc             C   s   ~ |gS )Nr   )ÚopÚgradr   r   r   Ú_sharding_grad   s    rg   ZXlaSpmdFullToShardShapec             C   s(   t j||  d¡| jd j ¡ d}|gS )NÚmanual_shardingr   )rh   Z
full_shape)r   Úxla_spmd_shard_to_full_shapeÚget_attrÚinputsr*   Zas_list)re   rf   Zs2fr   r   r   Ú_spmd_full_to_shard_shape_gradª  s
    rl   ZXlaSpmdShardToFullShapec             C   s   t j||  d¡d}|gS )Nrh   )rh   )r   Úxla_spmd_full_to_shard_shaperj   )re   rf   Zf2sr   r   r   Ú_spmd_shard_to_full_shape_grad³  s    rn   Fc             C   s   t j| ||| ¡ ||dS )N)Úslice_sizesr9   Úindices_are_sortedr   )r   Z
xla_gatherr;   )rS   Zstart_indicesr9   ro   rp   r   r   r   r   ÚgatherÀ  s    rq   c          	   C   s   t j| |||| ¡ ||dS )N)Úupdate_computationr9   rp   r   )r   Zxla_scatterr;   )rS   Zscatter_indicesZupdatesrr   r9   rp   r   r   r   r   ÚscatterË  s    rs   )N)N)N)N)r2   NN)N)NN)N)N)N)NNNNN)NN)N)FN)FN) Ú__doc__Ú
__future__r   r   r   Ztensorflow.compiler.tf2xla.opsr   Ztensorflow.python.frameworkr   r   r   Ztensorflow.python.opsr	   r
   r   r   r   r   r   r.   Z_maxr-   Z_minrd   r[   r)   r   ÚabsZconjÚcosÚceilZdigammaÚerfÚerfcZerfinvZndtriÚexpÚexpm1ÚfloorÚimagÚ	is_finiteÚlgammaÚlogÚlog1pZlogical_notÚnegÚrealÚroundÚsinÚsignÚtanhZ
bessel_i0eZ
bessel_i1er   Zint8Zuint8Úint16Zuint16Úint32Zuint32r   Zuint64r   r$   r#   r%   ÚaddÚsubÚmulÚdivÚmodÚremÚmaximumZminimumÚatan2ÚcomplexÚlogical_andÚ
logical_orÚlogical_xorÚequalÚeqÚ	not_equalÚneZgreater_equalÚgeZgreaterÚgtZ
less_equalÚleZlessÚltÚpowZ
left_shiftZ
shift_leftZshift_right_logicalZshift_right_arithmeticZigammaZigamma_grad_aZrandom_gamma_gradZigammacr'   rV   ÚreverseÚrevZbitcastZbitcast_convert_typer,   r1   r(   Zconcatenater?   r   Zconvert_element_typer@   rA   rE   rF   Zxla_dynamic_sliceZdynamic_sliceZxla_dynamic_update_sliceZdynamic_update_sliceZ
xla_einsumZeinsumZxla_padÚpadrH   rK   Zxla_recvÚrecvZ
xla_reduceÚreducerU   Zxla_replica_idZ
replica_idrW   rZ   Zxla_select_and_scatterZselect_and_scatterZxla_sendÚsendZxla_shardingZshardingZRegisterGradientrg   rm   Zspmd_full_to_shard_shaperi   Zspmd_shard_to_full_shaperl   rn   Zxla_sortÚsortZxla_key_value_sortZkey_value_sortZ	xla_whileZ
while_loopZxla_dequantizeZ
dequantizerq   rs   r   r   r   r   Ú<module>   s   	




















































	




  
&




    
%

	
