| 181 | public ::testing::WithParamInterface<CholeskyTestCase> {}; |
| 182 | |
| 183 | XLA_TEST_P(RandomCholeskyTest, Random) { |
| 184 | XlaBuilder builder(TestName()); |
| 185 | |
| 186 | auto test_params = GetParam(); |
| 187 | std::vector<int64> dimensions = {std::get<0>(test_params), |
| 188 | std::get<1>(test_params), |
| 189 | std::get<1>(test_params)}; |
| 190 | bool lower = std::get<2>(test_params); |
| 191 | Shape shape = ShapeUtil::MakeShape(F32, dimensions); |
| 192 | TF_ASSERT_OK_AND_ASSIGN( |
| 193 | auto literal, LiteralUtil::CreateRandomLiteral<F32>(shape, 0.0, 1.0)); |
| 194 | |
| 195 | auto input = Parameter(&builder, 0, shape, "input"); |
| 196 | // Form a random positive definite matrix. |
| 197 | auto matrix = |
| 198 | BatchDot(input, TransposeInMinorDims(input), PrecisionConfig::HIGHEST); |
| 199 | |
| 200 | auto cholesky = Triangle(Cholesky(matrix, lower), lower); |
| 201 | |
| 202 | // Verify that ||matrix - cholesky * cholesky_t||_2 ~= 0 |
| 203 | XlaOp verification; |
| 204 | if (lower) { |
| 205 | verification = BatchDot(cholesky, TransposeInMinorDims(cholesky), |
| 206 | PrecisionConfig::HIGHEST); |
| 207 | } else { |
| 208 | verification = BatchDot(TransposeInMinorDims(cholesky), cholesky, |
| 209 | PrecisionConfig::HIGHEST); |
| 210 | } |
| 211 | auto delta = matrix - verification; |
| 212 | Reduce(delta * delta, ConstantR0<float>(&builder, 0.0), |
| 213 | CreateScalarAddComputation(F32, &builder), {0, 1, 2}); |
| 214 | |
| 215 | TF_ASSERT_OK_AND_ASSIGN(auto input_data, client_->TransferToServer(literal)); |
| 216 | ComputeAndCompareR0<float>(&builder, 0.0, {input_data.get()}, |
| 217 | ErrorSpec(1e-4, 1e-4)); |
| 218 | } |
| 219 | |
| 220 | INSTANTIATE_TEST_SUITE_P(RandomCholeskyTestInstance, RandomCholeskyTest, |
| 221 | ::testing::Values(CholeskyTestCase{1, 1, true}, |
nothing calls this directly
no test coverage detected