MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / IgammaGradA

Function IgammaGradA

tensorflow/compiler/xla/client/lib/math.cc:954–999  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

952}
953
954XlaOp IgammaGradA(XlaOp a, XlaOp x) {
955 auto& b = *a.builder();
956 auto doit = [&b](XlaOp a, XlaOp x, PrimitiveType type) -> XlaOp {
957 XlaOp is_nan = Or(IsNan(a), IsNan(x));
958 XlaOp x_is_zero = Eq(x, ScalarLike(x, 0));
959 XlaOp domain_error = Or(Lt(x, ScalarLike(x, 0)), Le(a, ScalarLike(a, 0)));
960 XlaOp use_igammac = And(Gt(x, ScalarLike(x, 1)), Gt(x, a));
961 XlaOp ax = a * Log(x) - x - Lgamma(a);
962 XlaOp underflow = Lt(ax, -Log(MaxFiniteValue(&b, type)));
963 ax = Exp(ax);
964 XlaOp enabled = Not(Or(Or(Or(x_is_zero, domain_error), underflow), is_nan));
965 const double nan = std::numeric_limits<double>::quiet_NaN();
966 XlaOp output = Select(use_igammac,
967 -IgammacContinuedFraction<DERIVATIVE>(
968 ax, x, a, And(enabled, use_igammac), type),
969 IgammaSeries<DERIVATIVE>(
970 ax, x, a, And(enabled, Not(use_igammac)), type));
971 output = Select(underflow, ZerosLike(output), output);
972 output = Select(x_is_zero, ZerosLike(output), output);
973 output = Select(Or(domain_error, is_nan), FullLike(a, nan), output);
974 return output;
975 };
976 return b.ReportErrorOrReturn([&]() -> StatusOr<XlaOp> {
977 TF_ASSIGN_OR_RETURN(auto a_shape, b.GetShape(a));
978 TF_ASSIGN_OR_RETURN(auto x_shape, b.GetShape(x));
979 if (a_shape != x_shape) {
980 return InvalidArgument(
981 "Arguments to IgammaGradA must have equal shapes and types; got %s "
982 "and %s",
983 a_shape.ToString(), x_shape.ToString());
984 }
985 TF_RETURN_IF_ERROR(EnsureOperandIsRealFp("IgammaGradA", a));
986 bool needs_upcast =
987 a_shape.element_type() == F16 || a_shape.element_type() == BF16;
988
989 if (needs_upcast) {
990 a = ConvertElementType(a, F32);
991 x = ConvertElementType(x, F32);
992 }
993 XlaOp result = doit(a, x, a_shape.element_type());
994 if (needs_upcast) {
995 result = ConvertElementType(result, a_shape.element_type());
996 }
997 return result;
998 });
999}
1000
1001// Gradient of Gamma sample from Gamma(a, 1) with respect to `a`.
1002XlaOp RandomGammaGrad(XlaOp a, XlaOp x) {

Callers

nothing calls this directly

Calls 15

ScalarLikeFunction · 0.85
MaxFiniteValueFunction · 0.85
NotFunction · 0.85
FullLikeFunction · 0.85
InvalidArgumentFunction · 0.85
EnsureOperandIsRealFpFunction · 0.85
ReportErrorOrReturnMethod · 0.80
IsNanFunction · 0.70
LgammaFunction · 0.70
ZerosLikeFunction · 0.70
OrFunction · 0.50
EqFunction · 0.50

Tested by

no test coverage detected