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
18 #include "pybind11/pybind11.h"
19 #include "pybind11/stl_bind.h"
20
21 struct ConcreteFunction; // Forward declaration.
22
23 // TODO(jlchu): Migrate Python characteristics to C++
24
25 namespace tensorflow {
26
27 namespace py = pybind11;
28
29 struct PyConcreteFunction {
PyConcreteFunctiontensorflow::PyConcreteFunction30 PyConcreteFunction() {}
31 py::object _build_call_outputs(py::object result,
32 py::object structured_outputs,
33 bool _ndarrays_list, bool _ndarray_singleton);
34 };
35
_build_call_outputs(py::object result,py::object structured_outputs,bool _ndarrays_list,bool _ndarray_singleton)36 py::object PyConcreteFunction::_build_call_outputs(
37 py::object result, py::object structured_outputs, bool _ndarrays_list,
38 bool _ndarray_singleton) {
39 static const py::module* nest =
40 new py::module(py::module::import("tensorflow.python.util.nest"));
41 // TODO(jlchu): Look into lazy loading of np_arrays module
42 static const py::module* np_arrays = new py::module(
43 py::module::import("tensorflow.python.ops.numpy_ops.np_arrays"));
44
45 if (structured_outputs.is_none()) {
46 return result;
47 }
48
49 // TODO(jlchu): Verify invariant -result = None only if
50 // structured_outputs = None?
51 py::list list_result = (py::list)result;
52
53 if (!list_result.empty()) {
54 if (_ndarrays_list) {
55 py::list ndarr_result(list_result.size());
56 for (int i = 0; i < ndarr_result.size(); ++i) {
57 ndarr_result[i] = np_arrays->attr("tensor_to_ndarray")(list_result[i]);
58 }
59 return ndarr_result;
60 } else if (_ndarray_singleton) {
61 return np_arrays->attr("tensor_to_ndarray")(list_result[0]);
62 }
63 }
64
65 // Replace outputs with results, skipping over any 'None' values.
66 py::list outputs_list = nest->attr("flatten")(structured_outputs, true);
67 int j = 0;
68 for (int i = 0; i < outputs_list.size(); ++i) {
69 if (!outputs_list[i].is_none()) {
70 outputs_list[i] = list_result[j];
71 ++j;
72 }
73 }
74 return nest->attr("pack_sequence_as")(structured_outputs, outputs_list, true);
75 }
76
PYBIND11_MODULE(_concrete_function,m)77 PYBIND11_MODULE(_concrete_function, m) {
78 py::class_<PyConcreteFunction>(m, "ConcreteFunction")
79 .def(py::init<>())
80 .def("_build_call_outputs", &PyConcreteFunction::_build_call_outputs);
81 }
82
83 } // namespace tensorflow
84