1 /* Copyright 2018 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 // Basic server binary that exposes a xla::Service through a GRPC interface
17 // on a configurable port.
18 #include "absl/strings/str_format.h"
19 #include "grpcpp/security/server_credentials.h"
20 #include "grpcpp/server.h"
21 #include "grpcpp/server_builder.h"
22 #include "tensorflow/compiler/xla/rpc/grpc_service.h"
23 #include "tensorflow/compiler/xla/service/platform_util.h"
24 #include "tensorflow/core/platform/init_main.h"
25 #include "tensorflow/core/platform/logging.h"
26 #include "tensorflow/core/util/command_line_flags.h"
27
28 namespace xla {
29 namespace {
30
RealMain(int argc,char ** argv)31 int RealMain(int argc, char** argv) {
32 int32_t port = 1685;
33 bool any_address = false;
34 std::string platform_str;
35 std::vector<tensorflow::Flag> flag_list = {
36 tensorflow::Flag("platform", &platform_str,
37 "The XLA platform this service should be bound to"),
38 tensorflow::Flag("port", &port, "The TCP port to listen on"),
39 tensorflow::Flag(
40 "any", &any_address,
41 "Whether to listen to any host address or simply localhost"),
42 };
43 std::string usage = tensorflow::Flags::Usage(argv[0], flag_list);
44 bool parsed_values_ok = tensorflow::Flags::Parse(&argc, argv, flag_list);
45 if (!parsed_values_ok) {
46 LOG(ERROR) << usage;
47 return 2;
48 }
49 tensorflow::port::InitMain(argv[0], &argc, &argv);
50
51 se::Platform* platform = nullptr;
52 if (!platform_str.empty()) {
53 platform = PlatformUtil::GetPlatform(platform_str).ValueOrDie();
54 }
55 std::unique_ptr<xla::GRPCService> service =
56 xla::GRPCService::NewService(platform).value();
57
58 ::grpc::ServerBuilder builder;
59 std::string server_address(
60 absl::StrFormat("%s:%d", any_address ? "[::]" : "localhost", port));
61
62 builder.SetMaxReceiveMessageSize(INT_MAX);
63 builder.AddListeningPort(server_address, ::grpc::InsecureServerCredentials());
64 builder.RegisterService(service.get());
65 std::unique_ptr<::grpc::Server> server(builder.BuildAndStart());
66
67 LOG(INFO) << "Server listening on " << server_address;
68 server->Wait();
69 return 0;
70 }
71
72 } // namespace
73 } // namespace xla
74
main(int argc,char ** argv)75 int main(int argc, char** argv) { return xla::RealMain(argc, argv); }
76