1/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15// Note: This library is only used by python_api_info_test. It
16// is not meant to be used in other circumstances.
17
18#include "pybind11/pybind11.h"
19#include "pybind11/pytypes.h"
20#include "pybind11/stl.h"
21#include "tensorflow/python/framework/python_api_info.h"
22
23namespace py = pybind11;
24
25namespace tensorflow {
26namespace {
27
28void InitializeFromRegisteredOp(PythonAPIInfo* api_info,
29 const std::string& op_name) {
30 auto result = api_info->InitializeFromRegisteredOp(op_name);
31 if (!result.ok()) {
32 PyErr_SetString(PyExc_ValueError, result.ToString().c_str());
33 throw py::error_already_set();
34 }
35}
36
37void InitializeFromParamSpecs(
38 PythonAPIInfo* api_info,
39 const std::map<std::string, std::string>& input_specs,
40 const std::map<std::string, std::string>& attr_specs,
41 const std::vector<string>& param_names, py::handle defaults_tuple) {
42 auto result = api_info->InitializeFromParamSpecs(
43 input_specs, attr_specs, param_names, defaults_tuple.ptr());
44 if (!result.ok()) {
45 PyErr_SetString(PyExc_ValueError, result.ToString().c_str());
46 throw py::error_already_set();
47 }
48}
49
50std::string DebugInfo(PythonAPIInfo* api_info) { return api_info->DebugInfo(); }
51
52} // namespace
53} // namespace tensorflow
54
55using PythonAPIInfo = tensorflow::PythonAPIInfo;
56using InferredAttributes = tensorflow::PythonAPIInfo::InferredAttributes;
57
58PYBIND11_MODULE(_pywrap_python_api_info, m) {
59 py::class_<PythonAPIInfo>(m, "PythonAPIInfo")
60 .def(py::init<const std::string&>())
61 .def("InitializeFromRegisteredOp",
62 &tensorflow::InitializeFromRegisteredOp)
63 .def("InitializeFromParamSpecs", &tensorflow::InitializeFromParamSpecs)
64 .def("DebugInfo", &tensorflow::DebugInfo)
65 .def("InferredTypeAttrs",
66 [](PythonAPIInfo* self) { return self->inferred_type_attrs(); })
67 .def("InferredTypeListAttrs",
68 [](PythonAPIInfo* self) { return self->inferred_type_list_attrs(); })
69 .def("InferredLengthAttrs",
70 [](PythonAPIInfo* self) { return self->inferred_length_attrs(); });
71 py::class_<InferredAttributes>(m, "InferredAttributes")
72 .def_readonly("types", &InferredAttributes::types)
73 .def_readonly("type_lists", &InferredAttributes::type_lists)
74 .def_readonly("lengths", &InferredAttributes::lengths);
75}
76