| 146 | } |
| 147 | |
| 148 | xla::StatusOr<std::shared_ptr<XrtContext>> XrtClientTest::MakeContext() { |
| 149 | ChannelCreationFunction channel_func = |
| 150 | ConvertToChannelCreationFunction(NewHostPortGrpcChannel); |
| 151 | TF_ASSIGN_OR_RETURN(std::shared_ptr<GrpcChannelCache> channel_cache, |
| 152 | GetGrpcChannelCache(cluster_def_, channel_func)); |
| 153 | |
| 154 | auto client = std::make_shared<XrtTfClient>(cluster_def_, channel_cache); |
| 155 | TF_ASSIGN_OR_RETURN( |
| 156 | std::shared_ptr<XrtTfContext> tf_context, |
| 157 | XrtTfContext::Create(XrtTfContext::Options(), client, /*job=*/"localhost", |
| 158 | /*task=*/0)); |
| 159 | |
| 160 | TF_ASSIGN_OR_RETURN(auto context, XrtContext::Create(tf_context, "XLA_CPU")); |
| 161 | |
| 162 | // There should be exactly one XLA_CPU device. |
| 163 | TF_RET_CHECK(context->device_count() == 1); |
| 164 | return context; |
| 165 | } |
| 166 | |
| 167 | // Tests that we can use the XRT client to perform some simple operations. |
| 168 | TEST_F(XrtClientTest, XrtClientWorks) { |
| 169 | TF_ASSERT_OK_AND_ASSIGN(std::shared_ptr<XrtContext> context, MakeContext()); |
| 170 | |
| 171 | ASSERT_TRUE(context->tf_context() != nullptr); |
| 172 | |
| 173 | EXPECT_EQ(context->tf_device_ids().size(), 1); |
| 174 | |
| 175 | ASSERT_EQ(context->device_mesh_coordinates().size(), 1); |
| 176 | ASSERT_EQ(context->device_mesh_coordinates()[0].value_size(), 1); |
| 177 | EXPECT_EQ(context->device_mesh_coordinates()[0].value(0), 0); |
| 178 | |
| 179 | // Tests sending a literal to and from the device. |
| 180 | xla::Shape shape = xla::ShapeUtil::MakeShape(xla::F32, {3, 4, 5}); |
| 181 | TF_ASSERT_OK_AND_ASSIGN(xla::Literal a, |
| 182 | xla::LiteralUtil::CreateRandomLiteral<xla::F32>( |
| 183 | shape, |
| 184 | /*mean=*/7.0, /*stddev=*/13.5)); |
| 185 | TF_ASSERT_OK_AND_ASSIGN(auto buffer, XrtBuffer::FromLiteral(context, 0, a)); |
| 186 | TF_ASSERT_OK_AND_ASSIGN(xla::Literal b, buffer->ToLiteral()); |
| 187 | EXPECT_TRUE(xla::LiteralTestUtil::Equal(a, b)); |
| 188 | |
| 189 | // Run a simple computation, fetch its output, and check it is what we expect. |
| 190 | auto build_computation = [&]() { |
| 191 | xla::XlaBuilder builder("test_computation"); |
| 192 | xla::XlaOp p = xla::Parameter(&builder, 0, shape, "param"); |
| 193 | xla::Add(p, p); |
| 194 | return builder.Build(); |
| 195 | }; |
| 196 | TF_ASSERT_OK_AND_ASSIGN(xla::XlaComputation computation, build_computation()); |
| 197 | |
| 198 | TF_ASSERT_OK_AND_ASSIGN(xla::DeviceAssignment assignment, |
| 199 | xla::ComputationPlacer().AssignDevices(1, 1)); |
| 200 | TF_ASSERT_OK_AND_ASSIGN(auto executable, |
| 201 | XrtExecutable::Compile(context, computation.proto(), |
| 202 | {shape}, shape, assignment)); |
| 203 | EXPECT_EQ(executable->device_assignment(), assignment); |
| 204 | TF_ASSERT_OK_AND_ASSIGN(auto c_buffer, executable->Execute({buffer})); |
| 205 |
nothing calls this directly
no test coverage detected