Home | History | Annotate | Download | only in cpu
      1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #include <memory>
     17 #include <string>
     18 
     19 #include "absl/strings/str_format.h"
     20 #include "tensorflow/compiler/xla/array4d.h"
     21 #include "tensorflow/compiler/xla/client/client.h"
     22 #include "tensorflow/compiler/xla/client/client_library.h"
     23 #include "tensorflow/compiler/xla/client/global_data.h"
     24 #include "tensorflow/compiler/xla/client/local_client.h"
     25 #include "tensorflow/compiler/xla/client/xla_builder.h"
     26 #include "tensorflow/compiler/xla/client/xla_computation.h"
     27 #include "tensorflow/compiler/xla/literal.h"
     28 #include "tensorflow/compiler/xla/statusor.h"
     29 #include "tensorflow/compiler/xla/types.h"
     30 #include "tensorflow/compiler/xla/xla_data.pb.h"
     31 #include "tensorflow/core/platform/init_main.h"
     32 #include "tensorflow/core/platform/logging.h"
     33 
     34 int main(int argc, char** argv) {
     35   tensorflow::port::InitMain(argv[0], &argc, &argv);
     36 
     37   xla::LocalClient* client(xla::ClientLibrary::LocalClientOrDie());
     38 
     39   // Transfer parameters.
     40   xla::Literal param0_literal =
     41       xla::LiteralUtil::CreateR1<float>({1.1f, 2.2f, 3.3f, 5.5f});
     42   std::unique_ptr<xla::GlobalData> param0_data =
     43       client->TransferToServer(param0_literal).ConsumeValueOrDie();
     44 
     45   xla::Literal param1_literal = xla::LiteralUtil::CreateR2<float>(
     46       {{3.1f, 4.2f, 7.3f, 9.5f}, {1.1f, 2.2f, 3.3f, 4.4f}});
     47   std::unique_ptr<xla::GlobalData> param1_data =
     48       client->TransferToServer(param1_literal).ConsumeValueOrDie();
     49 
     50   // Build computation.
     51   xla::XlaBuilder builder("");
     52   auto p0 = Parameter(&builder, 0, param0_literal.shape(), "param0");
     53   auto p1 = Parameter(&builder, 1, param1_literal.shape(), "param1");
     54   Add(p1, p0, {0});
     55 
     56   xla::StatusOr<xla::XlaComputation> computation_status = builder.Build();
     57   xla::XlaComputation computation = computation_status.ConsumeValueOrDie();
     58 
     59   // Execute and transfer result of computation.
     60   xla::ExecutionProfile profile;
     61   xla::StatusOr<xla::Literal> result = client->ExecuteAndTransfer(
     62       computation,
     63       /*arguments=*/{param0_data.get(), param1_data.get()},
     64       /*execution_options=*/nullptr,
     65       /*execution_profile=*/&profile);
     66   xla::Literal actual = result.ConsumeValueOrDie();
     67 
     68   LOG(INFO) << absl::StrFormat("computation took %dns",
     69                                profile.compute_time_ns());
     70   LOG(INFO) << actual.ToString();
     71 
     72   return 0;
     73 }
     74