1/* Copyright 2015 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
16#include "pybind11/pybind11.h"
17#include "pybind11/pytypes.h"
18#include "tensorflow/core/platform/cpu_info.h"
19#include "tensorflow/python/lib/core/pybind11_lib.h"
20#include "tensorflow/python/util/util.h"
21
22namespace py = pybind11;
23
24PYBIND11_MODULE(_pywrap_utils, m) {
25 m.doc() = R"pbdoc(
26 _pywrap_utils
27 -----
28 )pbdoc";
29 m.def("RegisterType",
30 [](const py::handle& type_name, const py::handle& type) {
31 return tensorflow::PyoOrThrow(
32 tensorflow::swig::RegisterType(type_name.ptr(), type.ptr()));
33 });
34 m.def("RegisterPyObject", [](const py::handle& name, const py::handle& type) {
35 return tensorflow::PyoOrThrow(
36 tensorflow::swig::RegisterPyObject(name.ptr(), type.ptr()));
37 });
38 m.def(
39 "IsTensor",
40 [](const py::handle& o) {
41 bool result = tensorflow::swig::IsTensor(o.ptr());
42 if (PyErr_Occurred()) {
43 throw py::error_already_set();
44 }
45 return result;
46 },
47 R"pbdoc(
48 Check if an object is a Tensor.
49 )pbdoc");
50 m.def(
51 "IsNested",
52 [](const py::handle& o) {
53 bool result = tensorflow::swig::IsNested(o.ptr());
54 return result;
55 },
56 R"pbdoc(
57 Refer to `tf.nest.is_nested`.
58 )pbdoc");
59 m.def(
60 "IsNestedOrComposite",
61 [](const py::handle& o) {
62 bool result = tensorflow::swig::IsNestedOrComposite(o.ptr());
63 if (PyErr_Occurred()) {
64 throw py::error_already_set();
65 }
66 return result;
67 },
68 R"pbdoc(
69 Returns true if its input is a sequence or a `CompositeTensor`.
70
71 Args:
72 seq: an input sequence.
73
74 Returns:
75 True if the sequence is a not a string and is a collections.Sequence or a
76 dict or a CompositeTensor or a TypeSpec (except string and TensorSpec).
77 )pbdoc");
78 m.def(
79 "IsCompositeTensor",
80 [](const py::handle& o) {
81 bool result = tensorflow::swig::IsCompositeTensor(o.ptr());
82 if (PyErr_Occurred()) {
83 throw py::error_already_set();
84 }
85 return result;
86 },
87 R"pbdoc(
88 Returns true if its input is a `CompositeTensor`.
89
90 Args:
91 seq: an input sequence.
92
93 Returns:
94 True if the sequence is a CompositeTensor.
95 )pbdoc");
96 m.def(
97 "IsTypeSpec",
98 [](const py::handle& o) {
99 bool result = tensorflow::swig::IsTypeSpec(o.ptr());
100 if (PyErr_Occurred()) {
101 throw py::error_already_set();
102 }
103 return result;
104 },
105 R"pbdoc(
106 Returns true if its input is a `TypeSpec`, but is not a `TensorSpec`.
107
108 Args:
109 seq: an input sequence.
110
111 Returns:
112 True if the sequence is a `TypeSpec`, but is not a `TensorSpec`.
113 )pbdoc");
114 m.def(
115 "IsNamedtuple",
116 [](const py::handle& o, bool strict) {
117 return tensorflow::PyoOrThrow(
118 tensorflow::swig::IsNamedtuple(o.ptr(), strict));
119 },
120 R"pbdoc(
121 Check if an object is a NamedTuple.
122 )pbdoc");
123 m.def(
124 "IsMapping",
125 [](const py::handle& o) {
126 bool result = tensorflow::swig::IsMapping(o.ptr());
127 if (PyErr_Occurred()) {
128 throw py::error_already_set();
129 }
130 return result;
131 },
132 R"pbdoc(
133 Returns True if `instance` is a `collections.Mapping`.
134
135 Args:
136 instance: An instance of a Python object.
137
138 Returns:
139 True if `instance` is a `collections.Mapping`.
140 )pbdoc");
141 m.def(
142 "IsMutableMapping",
143 [](const py::handle& o) {
144 bool result = tensorflow::swig::IsMutableMapping(o.ptr());
145 if (PyErr_Occurred()) {
146 throw py::error_already_set();
147 }
148 return result;
149 },
150 R"pbdoc(
151 Returns True if `instance` is a `collections.MutableMapping`.
152
153 Args:
154 instance: An instance of a Python object.
155
156 Returns:
157 True if `instance` is a `collections.MutableMapping`.
158 )pbdoc");
159 m.def(
160 "IsMappingView",
161 [](const py::handle& o) {
162 bool result = tensorflow::swig::IsMappingView(o.ptr());
163 if (PyErr_Occurred()) {
164 throw py::error_already_set();
165 }
166 return result;
167 },
168 R"pbdoc(
169 Returns True if considered a mapping view for the purposes of Flatten()`.
170
171 Args:
172 instance: An instance of a Python object.
173
174 Returns:
175 True if considered a mapping view for the purposes of Flatten().
176 )pbdoc");
177 m.def(
178 "IsAttrs",
179 [](const py::handle& o) {
180 bool result = tensorflow::swig::IsAttrs(o.ptr());
181 if (PyErr_Occurred()) {
182 throw py::error_already_set();
183 }
184 return result;
185 },
186 R"pbdoc(
187 Returns True if `instance` is an instance of an `attr.s` decorated class.
188
189 Args:
190 instance: An instance of a Python object.
191
192 Returns:
193 True if `instance` is an instance of an `attr.s` decorated class.
194 )pbdoc");
195 m.def(
196 "SameNamedtuples",
197 [](const py::handle& o1, const py::handle& o2) {
198 return tensorflow::PyoOrThrow(
199 tensorflow::swig::SameNamedtuples(o1.ptr(), o2.ptr()));
200 },
201 R"pbdoc(
202 Returns True if the two namedtuples have the same name and fields.
203 )pbdoc");
204 m.def(
205 "AssertSameStructure",
206 [](const py::handle& o1, const py::handle& o2, bool check_types,
207 bool expand_composites) {
208 bool result = tensorflow::swig::AssertSameStructure(
209 o1.ptr(), o2.ptr(), check_types, expand_composites);
210 if (PyErr_Occurred()) {
211 throw py::error_already_set();
212 }
213 return result;
214 },
215 R"pbdoc(
216 Returns True if the two structures are nested in the same way.
217 )pbdoc");
218 m.def(
219 "Flatten",
220 [](const py::handle& o, bool expand_composites) {
221 return tensorflow::PyoOrThrow(
222 tensorflow::swig::Flatten(o.ptr(), expand_composites));
223 },
224 R"pbdoc(
225 Refer to `tf.nest.flatten`.
226 )pbdoc");
227 m.def(
228 "IsNestedForData",
229 [](const py::handle& o) {
230 bool result = tensorflow::swig::IsNestedForData(o.ptr());
231 if (PyErr_Occurred()) {
232 throw py::error_already_set();
233 }
234 return result;
235 },
236 R"pbdoc(
237 Returns a true if `seq` is a nested structure for tf.data.
238
239 NOTE(mrry): This differs from `tensorflow.python.util.nest.is_nested()`,
240 which *does* treat a Python list as a sequence. For ergonomic
241 reasons, `tf.data` users would prefer to treat lists as
242 implicit `tf.Tensor` objects, and dicts as (nested) sequences.
243
244 Args:
245 seq: an input sequence.
246
247 Returns:
248 True if the sequence is a not a string or list and is a
249 collections.Sequence.
250 )pbdoc");
251 m.def(
252 "FlattenForData",
253 [](const py::handle& o) {
254 return tensorflow::PyoOrThrow(
255 tensorflow::swig::FlattenForData(o.ptr()));
256 },
257 R"pbdoc(
258 Returns a flat sequence from a given nested structure.
259
260 If `nest` is not a sequence, this returns a single-element list: `[nest]`.
261
262 Args:
263 nest: an arbitrarily nested structure or a scalar object.
264 Note, numpy arrays are considered scalars.
265
266 Returns:
267 A Python list, the flattened version of the input.
268 )pbdoc");
269 m.def(
270 "AssertSameStructureForData",
271 [](const py::handle& o1, const py::handle& o2, bool check_types) {
272 bool result = tensorflow::swig::AssertSameStructureForData(
273 o1.ptr(), o2.ptr(), check_types);
274 if (PyErr_Occurred()) {
275 throw py::error_already_set();
276 }
277 return result;
278 },
279 R"pbdoc(
280 Returns True if the two structures are nested in the same way in particular tf.data.
281 )pbdoc");
282 m.def(
283 "IsResourceVariable",
284 [](const py::handle& o) {
285 bool result = tensorflow::swig::IsResourceVariable(o.ptr());
286 if (PyErr_Occurred()) {
287 throw py::error_already_set();
288 }
289 return result;
290 },
291 R"pbdoc(
292 Returns 1 if `o` is a ResourceVariable.
293
294 Args:
295 instance: An instance of a Python object.
296
297 Returns:
298 True if `instance` is a `ResourceVariable`.
299 )pbdoc");
300 m.def(
301 "IsVariable",
302 [](const py::handle& o) {
303 bool result = tensorflow::swig::IsVariable(o.ptr());
304 if (PyErr_Occurred()) {
305 throw py::error_already_set();
306 }
307 return result;
308 },
309 R"pbdoc(
310 Returns 1 if `o` is a Variable.
311
312 Args:
313 instance: An instance of a Python object.
314
315 Returns:
316 True if `instance` is a `Variable`.
317 )pbdoc");
318 m.def(
319 "IsBF16SupportedByOneDNNOnThisCPU",
320 []() {
321 bool result = tensorflow::port::TestCPUFeature(
322 tensorflow::port::CPUFeature::AVX512F);
323 if (PyErr_Occurred()) {
324 throw py::error_already_set();
325 }
326 return result;
327 },
328 R"pbdoc(
329 Returns 1 if CPU has avx512f feature.
330
331 Args:
332 None
333
334 Returns:
335 True if CPU has avx512f feature.
336 )pbdoc");
337}
338