• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/dispatcher_client.h"
25 #include "tensorflow/core/data/service/grpc_util.h"
26 #include "tensorflow/core/data/service/server_lib.h"
27 #include "tensorflow/core/platform/errors.h"
28 #include "tensorflow/core/platform/types.h"
29 #include "tensorflow/core/protobuf/service_config.pb.h"
30 #include "tensorflow/python/lib/core/pybind11_lib.h"
31 #include "tensorflow/python/lib/core/pybind11_status.h"
32 
33 namespace py = pybind11;
34 
PYBIND11_MODULE(_pywrap_server_lib,m)35 PYBIND11_MODULE(_pywrap_server_lib, m) {
36   py::class_<tensorflow::data::DispatchGrpcDataServer>(m,
37                                                        "DispatchGrpcDataServer")
38       .def("start", &tensorflow::data::DispatchGrpcDataServer::Start)
39       .def("stop", &tensorflow::data::DispatchGrpcDataServer::Stop)
40       .def("join", &tensorflow::data::DispatchGrpcDataServer::Join,
41            py::call_guard<py::gil_scoped_release>())
42       .def("bound_port", &tensorflow::data::DispatchGrpcDataServer::BoundPort)
43       .def("num_workers",
44            [](tensorflow::data::DispatchGrpcDataServer* server) -> int {
45              int num_workers;
46              tensorflow::Status status = server->NumWorkers(&num_workers);
47              tensorflow::MaybeRaiseFromStatus(status);
48              return num_workers;
49            });
50 
51   py::class_<tensorflow::data::WorkerGrpcDataServer>(m, "WorkerGrpcDataServer")
52       .def("start", &tensorflow::data::WorkerGrpcDataServer::Start)
53       .def("stop", &tensorflow::data::WorkerGrpcDataServer::Stop)
54       .def("join", &tensorflow::data::WorkerGrpcDataServer::Join,
55            py::call_guard<py::gil_scoped_release>())
56       .def("bound_port", &tensorflow::data::WorkerGrpcDataServer::BoundPort)
57       .def("num_tasks",
58            [](tensorflow::data::WorkerGrpcDataServer* server) -> int {
59              int num_tasks;
60              tensorflow::Status status = server->NumTasks(&num_tasks);
61              tensorflow::MaybeRaiseFromStatus(status);
62              return num_tasks;
63            });
64 
65   m.def(
66       "TF_DATA_NewDispatchServer",
67       [](std::string serialized_dispatcher_config)
68           -> std::unique_ptr<tensorflow::data::DispatchGrpcDataServer> {
69         tensorflow::data::experimental::DispatcherConfig config;
70         if (!config.ParseFromString(serialized_dispatcher_config)) {
71           tensorflow::MaybeRaiseFromStatus(tensorflow::errors::InvalidArgument(
72               "Failed to deserialize dispatcher config."));
73         }
74         std::unique_ptr<tensorflow::data::DispatchGrpcDataServer> server;
75         tensorflow::Status status =
76             tensorflow::data::NewDispatchServer(config, server);
77         tensorflow::MaybeRaiseFromStatus(status);
78         return server;
79       },
80       py::return_value_policy::reference);
81 
82   m.def(
83       "TF_DATA_NewWorkerServer",
84       [](std::string serialized_worker_config)
85           -> std::unique_ptr<tensorflow::data::WorkerGrpcDataServer> {
86         tensorflow::data::experimental::WorkerConfig config;
87         if (!config.ParseFromString(serialized_worker_config)) {
88           tensorflow::MaybeRaiseFromStatus(tensorflow::errors::InvalidArgument(
89               "Failed to deserialize worker config."));
90         }
91         std::unique_ptr<tensorflow::data::WorkerGrpcDataServer> server;
92         tensorflow::Status status =
93             tensorflow::data::NewWorkerServer(config, server);
94         tensorflow::MaybeRaiseFromStatus(status);
95         return server;
96       },
97       py::return_value_policy::reference);
98 
99   m.def(
100       "TF_DATA_GetElementSpec",
101       [](int64_t dataset_id, const std::string& address,
102          const std::string& protocol) -> py::bytes {
103         std::string element_spec;
104         tensorflow::data::DataServiceDispatcherClient client(address, protocol);
105         int64_t deadline_micros = tensorflow::kint64max;
106         tensorflow::Status status;
107         Py_BEGIN_ALLOW_THREADS;
108         status = tensorflow::data::grpc_util::Retry(
109             [&]() { return client.GetElementSpec(dataset_id, element_spec); },
110             /*description=*/
111             tensorflow::strings::StrCat(
112                 "get the element_spec with dispatcher at ", address),
113             deadline_micros);
114         Py_END_ALLOW_THREADS;
115         tensorflow::MaybeRaiseFromStatus(status);
116         return py::bytes(element_spec);
117       },
118       py::return_value_policy::reference);
119 };
120