Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion xla/hlo/builder/lib/math.cc
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,19 @@ XlaOp IsNegZero(XlaOp operand) {

XlaOp Square(XlaOp operand) { return operand * operand; }

XlaOp Reciprocal(XlaOp operand) { return ScalarLike(operand, 1.0) / operand; }
XlaOp Reciprocal(XlaOp operand) {
XlaBuilder* b = operand.builder();
return b->ReportErrorOrReturn([&]() -> absl::StatusOr<XlaOp> {
TF_ASSIGN_OR_RETURN(auto shape, b->GetShape(operand));
if (primitive_util::IsComplexType(shape.element_type())) {
XlaOp is_finite = And(IsFinite(Real(operand)), IsFinite(Imag(operand)));
XlaOp is_inf = And(Not(is_finite), Eq(operand, operand));
return Select(is_inf, ZerosLike(operand),
ScalarLike(operand, 1.0) / operand);
}
return ScalarLike(operand, 1.0) / operand;
});
}

// Computes an approximation of the error function complement (1 - erf(x)).
//
Expand Down
14 changes: 14 additions & 0 deletions xla/hlo/builder/lib/math_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,20 @@ TEST_F(MathTest, ReciprocalTenValues) {
ComputeAndCompareR1<float>(&builder, expected, {}, kErrorSpec);
}

TEST_F(MathTest, ReciprocalComplexInfinity) {
XlaBuilder builder(TestName());
auto x = ConstantR1<std::complex<float>>(
&builder, {{std::numeric_limits<float>::infinity(), 0.0},
{0.0, std::numeric_limits<float>::infinity()},
{std::numeric_limits<float>::infinity(),
std::numeric_limits<float>::infinity()}});
Reciprocal(x);

std::vector<std::complex<float>> expected = {
{0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0}};
ComputeAndCompareR1<std::complex<float>>(&builder, expected, {}, kErrorSpec);
}

TEST_F(MathTest, SqrtZeroes) {
XlaBuilder builder(TestName());
auto x = ConstantR1<float>(&builder, {0.0, -0.0});
Expand Down
Loading