1 | /* Copyright 2019 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 "pybind11/pybind11.h" |
17 | #include "tensorflow/core/framework/op.h" |
18 | #include "tensorflow/core/framework/op_def.pb.h" |
19 | #include "tensorflow/core/framework/op_def_util.h" |
20 | #include "tensorflow/python/lib/core/pybind11_status.h" |
21 | |
22 | namespace py = pybind11; |
23 | |
24 | PYBIND11_MODULE(_op_def_registry, m) { |
25 | m.def("get", [](const std::string& name) { |
26 | const tensorflow::OpDef* op_def = nullptr; |
27 | auto status = tensorflow::OpRegistry::Global()->LookUpOpDef(name, &op_def); |
28 | if (!status.ok()) return py::reinterpret_borrow<py::object>(py::none()); |
29 | |
30 | tensorflow::OpDef stripped_op_def = *op_def; |
31 | tensorflow::RemoveNonDeprecationDescriptionsFromOpDef(&stripped_op_def); |
32 | |
33 | tensorflow::MaybeRaiseFromStatus(status); |
34 | std::string serialized_op_def; |
35 | if (!stripped_op_def.SerializeToString(&serialized_op_def)) { |
36 | throw std::runtime_error("Failed to serialize OpDef to string"); |
37 | } |
38 | |
39 | // Explicitly convert to py::bytes because std::string is implicitly |
40 | // convertable to py::str by default. |
41 | return py::reinterpret_borrow<py::object>(py::bytes(serialized_op_def)); |
42 | }); |
43 | } |
44 |