1 /* Copyright 2020 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 "Python.h"
17 #include "pybind11/chrono.h"
18 #include "pybind11/complex.h"
19 #include "pybind11/detail/common.h"
20 #include "pybind11/functional.h"
21 #include "pybind11/pybind11.h"
22 #include "pybind11/pytypes.h"
23 #include "pybind11/stl.h"
24 #include "tensorflow/core/data/service/server_lib.h"
25 #include "tensorflow/core/platform/errors.h"
26 #include "tensorflow/core/protobuf/service_config.pb.h"
27 #include "tensorflow/python/lib/core/pybind11_lib.h"
28 #include "tensorflow/python/lib/core/pybind11_status.h"
29 
30 namespace py = pybind11;
31 
PYBIND11_MODULE(_pywrap_server_lib,m)32 PYBIND11_MODULE(_pywrap_server_lib, m) {
33   py::class_<tensorflow::data::DispatchGrpcDataServer>(m,
34                                                        "DispatchGrpcDataServer")
35       .def("start", &tensorflow::data::DispatchGrpcDataServer::Start)
36       .def("stop", &tensorflow::data::DispatchGrpcDataServer::Stop)
37       .def("join", &tensorflow::data::DispatchGrpcDataServer::Join,
38            py::call_guard<py::gil_scoped_release>())
39       .def("bound_port", &tensorflow::data::DispatchGrpcDataServer::BoundPort)
40       .def("num_workers",
41            [](tensorflow::data::DispatchGrpcDataServer* server) -> int {
42              int num_workers;
43              tensorflow::Status status = server->NumWorkers(&num_workers);
44              tensorflow::MaybeRaiseFromStatus(status);
45              return num_workers;
46            });
47 
48   py::class_<tensorflow::data::WorkerGrpcDataServer>(m, "WorkerGrpcDataServer")
49       .def("start", &tensorflow::data::WorkerGrpcDataServer::Start)
50       .def("stop", &tensorflow::data::WorkerGrpcDataServer::Stop)
51       .def("join", &tensorflow::data::WorkerGrpcDataServer::Join,
52            py::call_guard<py::gil_scoped_release>())
53       .def("bound_port", &tensorflow::data::WorkerGrpcDataServer::BoundPort)
54       .def("num_tasks",
55            [](tensorflow::data::WorkerGrpcDataServer* server) -> int {
56              int num_tasks;
57              tensorflow::Status status = server->NumTasks(&num_tasks);
58              tensorflow::MaybeRaiseFromStatus(status);
59              return num_tasks;
60            });
61 
62   m.def(
63       "TF_DATA_NewDispatchServer",
64       [](std::string serialized_dispatcher_config)
65           -> std::unique_ptr<tensorflow::data::DispatchGrpcDataServer> {
66         tensorflow::data::experimental::DispatcherConfig config;
67         if (!config.ParseFromString(serialized_dispatcher_config)) {
68           tensorflow::MaybeRaiseFromStatus(tensorflow::errors::InvalidArgument(
69               "Failed to deserialize dispatcher config."));
70         }
71         std::unique_ptr<tensorflow::data::DispatchGrpcDataServer> server;
72         tensorflow::Status status =
73             tensorflow::data::NewDispatchServer(config, server);
74         tensorflow::MaybeRaiseFromStatus(status);
75         return server;
76       },
77       py::return_value_policy::reference);
78 
79   m.def(
80       "TF_DATA_NewWorkerServer",
81       [](std::string serialized_worker_config)
82           -> std::unique_ptr<tensorflow::data::WorkerGrpcDataServer> {
83         tensorflow::data::experimental::WorkerConfig config;
84         if (!config.ParseFromString(serialized_worker_config)) {
85           tensorflow::MaybeRaiseFromStatus(tensorflow::errors::InvalidArgument(
86               "Failed to deserialize worker config."));
87         }
88         std::unique_ptr<tensorflow::data::WorkerGrpcDataServer> server;
89         tensorflow::Status status =
90             tensorflow::data::NewWorkerServer(config, server);
91         tensorflow::MaybeRaiseFromStatus(status);
92         return server;
93       },
94       py::return_value_policy::reference);
95 };
96