
    AVh'                        d 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	 ddlm
Z
 dd	lmZ dd
lmZ d Z ej                  d      dej                  fd       Z ej                  d      dej                  fd       Z ej                  d      dej                  fd       Zd Zd Z ej                  d      dej                  fd       Zy)z1Gradients for operators defined in random_ops.py.    N)constant_op)dtypes)ops)	array_ops)clip_ops)gen_array_ops)gen_random_ops)math_opsc                     t        j                  t        j                  |gt        j                        t        j
                  |       gd      }t        j                  | |      S )N)dtyper   axis)r   concatonesr   int32shapereshape)xnum_dimensions	new_shapes      Q/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/ops/random_grad.pyadd_leading_unit_dimensionsr      sP    ~~~&fll;q!"$) 
		1i	((    RandomGammaopc                    | j                   d   }| j                   d   }| j                  d   }t        j                  |g      5  t	        j
                  |      d   }t        ||      }t        j                  ||      }dt        j                  ||z  t        j                  |            fcddd       S # 1 sw Y   yxY w)a  Returns the gradient of a Gamma sample w.r.t. alpha.

  The gradient is computed using implicit differentiation
  (Figurnov et al., 2018).

  Args:
    op: A `RandomGamma` operation. We assume that the inputs to the operation
      are `shape` and `alpha` tensors, and the output is the `sample` tensor.
    grad: The incoming gradient `dloss / dsample` of the same shape as
      `op.outputs[0]`.

  Returns:
    A `Tensor` with derivatives `dloss / dalpha`.

  References:
    Implicit Reparameterization Gradients:
      [Figurnov et al., 2018]
      (http://papers.nips.cc/paper/7326-implicit-reparameterization-gradients)
      ([pdf]
      (http://papers.nips.cc/paper/7326-implicit-reparameterization-gradients.pdf))
  r      Nr   )inputsoutputsr   control_dependenciesr   r   r   r	   random_gamma_gradr
   
reduce_sumrange)r   gradr   alphasamplenum_sample_dimensionsalpha_broadcastable	partial_as           r   _RandomGammaGradr*   $   s    . ))A,%
))A,%::a=&
' 
G &OOE2155$&001DfMI (%%yx~~.CDF G
G 
G 
Gs   A)B77C StatelessRandomGammaV2c           	          | j                   d   }| j                   d   }| j                  d   }t        j                  |g      5  ddt	        ||||      fcddd       S # 1 sw Y   yxY w)a  Returns the gradient of a Gamma sample w.r.t. alpha.

  The gradient is computed using implicit differentiation
  (Figurnov et al., 2018).

  Args:
    op: A `StatelessRandomGamma` operation. We assume that the inputs to the
      operation are `shape`, `seed` and `alpha` tensors, and the output is the
      `sample` tensor.
    grad: The incoming gradient `dloss / dsample` of the same shape as
      `op.outputs[0]`.

  Returns:
    A `Tensor` with derivatives `dloss / dalpha`.

  References:
    Implicit Reparameterization Gradients:
      [Figurnov et al., 2018]
      (http://papers.nips.cc/paper/7326-implicit-reparameterization-gradients)
      ([pdf]
      (http://papers.nips.cc/paper/7326-implicit-reparameterization-gradients.pdf))
  r      Nr   r   r   r    _StatelessGammaGradAlphar   r$   r   r%   r&   s        r   _StatelessRandomGammaV2Gradr1   L   sj    0 ))A,%
))A,%::a=&
' N$0vtLMN N Ns   AA(StatelessRandomGammaV3c                     | j                   d   }| j                   d   }| j                  d   }t        j                  |g      5  ddddt	        ||||      fcddd       S # 1 sw Y   yxY w)a  Returns the gradient of a Gamma sample w.r.t. alpha.

  The gradient is computed using implicit differentiation
  (Figurnov et al., 2018).

  Args:
    op: A `StatelessRandomGamma` operation. We assume that the inputs to the
      operation are `shape`, `key`, `counter`, `alg`, and `alpha` tensors, and
      the output is the `sample` tensor.
    grad: The incoming gradient `dloss / dsample` of the same shape as
      `op.outputs[0]`.

  Returns:
    A `Tensor` with derivatives `dloss / dalpha`.

  References:
    Implicit Reparameterization Gradients:
      [Figurnov et al., 2018]
      (http://papers.nips.cc/paper/7326-implicit-reparameterization-gradients)
      ([pdf]
      (http://papers.nips.cc/paper/7326-implicit-reparameterization-gradients.pdf))
  r      Nr.   r0   s        r   _StatelessRandomGammaV3Gradr5   l   sq    0 ))A,%
))A,%::a=&
' B$d$UE64@BB B Bs   A!!A*c                     t        j                  |       d   t        j                  |      z
  }t        ||      }t	        j
                  ||      }t        j                  ||z  t        j                  |            S )z/Returns gradients of a gamma sampler wrt alpha.r   r   )	r   r   rankr   r	   r!   r
   r"   r#   )r   r%   r&   r$   r'   r(   r)   s          r   r/   r/      sw     $//%03innU6KK 4E4IK../BFK) 
		
YX^^,AB
D Dr   c                    t        j                  dt        j                  d      z  | j                  d      }| |z  }t        j                  |      }t        j                  ||k  dt        j                  |      z   t        j                  |dkD  dt        j                  |      z
  t        j                  |                  }d|z  S )zNormal distribution function.      ?g       @half_sqrt_2)r   nameg      ?g        )r   constantnpsqrtr   r
   absr   whereerferfc)r   r:   wzys        r   _NdtrrF      s    $$	BGGBKqww]<++o!ll1o!oo+o8<<?oo
b&"x}}Q''q)9;<!
 
q.r   %StatelessParameterizedTruncatedNormalc                 X   | j                   d   }| j                   d   }| j                   d   }| j                   d   }| j                   d   }| j                  d   }t        j                  |g      5  ||z
  |z  }||z
  |z  }	||z
  |z  }
t	        |
      t	        |      z
  t	        |	      t	        |      z
  z  }t        j                  |j                  j                        j                  }t        j                  |j                  j                        j                  }t        j                  ||d|z
        }t        j                  d|
dz  |	dz  z
  z  t        j                  |      z         }t        j                  d|
dz  |dz  z
  z  t        j                   |       z         }t#        j$                  |
      }|
}t#        j&                  |      }t#        j&                  |      }t#        j&                  |      }t#        j&                  |      }t#        j(                  ||      }t#        j(                  ||      }t#        j(                  ||      }t        j*                  t#        j,                  |      t#        j,                  |      z
        }t        j.                  ||z  |      }t        j.                  ||z  |      }t        j.                  ||z  |      }t        j.                  ||z  |      }t1        j2                  ||      \  }}t1        j2                  ||      \  }}t1        j2                  ||      \  }}t1        j2                  ||      \  }} t#        j4                  t        j.                  ||d	
      |      }t#        j4                  t        j.                  ||d	
      |      }t#        j4                  t        j.                  ||d	
      |      }t#        j4                  t        j.                  || d	
      |      }dd||||fcddd       S # 1 sw Y   yxY w)a?  Returns the gradient of a TruncatedNormal sample w.r.t. parameters.

  The gradient is computed using implicit differentiation
  (Figurnov et al., 2018).

  Args:
    op: A `StatelessParameterizedTruncatedNormal` operation. We assume that the
      inputs to the operation are `shape`, `seed`, `mean`, `stddev`, `minval`,
      and `maxval` tensors, and the output is the `sample` tensor.
    grad: The incoming gradient `dloss / dsample` of the same shape as
      `op.outputs[0]`.

  Returns:
    A list of `Tensor` with derivates with respect to each parameter.

  References:
    Implicit Reparameterization Gradients:
      [Figurnov et al., 2018]
      (http://papers.nips.cc/paper/7326-implicit-reparameterization-gradients)
      ([pdf]
      (http://papers.nips.cc/paper/7326-implicit-reparameterization-gradients.pdf))
  r   r-      r4      r   r9   r   T)r   keepdimsN)r   r   r   r    rF   r=   finfor   as_numpy_dtypetinyepsr   clip_by_valuer
   exploglog1pr   	ones_liker   broadcast_dynamic_shaper#   sizer"   r   broadcast_gradient_argsr   )!r   r$   r   meanstddevminvalmaxvalr&   
minval_std
maxval_std
sample_std
cdf_samplerN   rO   dmaxvaldminvaldmeandstddev
mean_shapestddev_shapeminval_shapemaxval_shapebroadcast_shape
extra_dims	grad_meangrad_stddevgrad_minvalgrad_maxval_rmeanrstddevrminvalrmaxvals!                                    r   *_StatelessParameterizedTruncatedNormalGradrs      s   0 ))A,%	1$99Q<&99Q<&99Q<&::a=&
' CJ4-6)J4-6)J4-6)J
#eJ&77jE*--/J 88DJJ--.33D
((4::,,
-
1
1C''
D!c'BJll3*/J!O"CD#<<
34 5Gll3*/J!O"CD#>>:+67 8G
+EG
 &J??6*L??6*L??6*L77L"O77o'O77o'Ou	 ??AJ ##D5LzBI%%dWn:FK%%dWn:FK%%dWn:FK44%HAu66'JAw66'JAw66'JAw !!IEDA:OI ##KgEK ##KgEK ##KgEK
 $	;[IGCJ CJ CJs   1N%P  P))__doc__numpyr=   tensorflow.python.frameworkr   r   r   tensorflow.python.opsr   r   r   r	   r
   r   RegisterGradient	Operationr*   r1   r5   r/   rF   rs    r   r   <module>r{      s    8  3 . + + * / 0 *) m$$G $G %$GN ./NCMM N 0N> ./BCMM B 0B@D  =>aJ3== aJ ?aJr   