1 /* Copyright 2015 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 "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
22 namespace py = pybind11;
23
PYBIND11_MODULE(_pywrap_utils,m)24 PYBIND11_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 "IsSequence",
52 [](const py::handle& o) {
53 bool result = tensorflow::swig::IsSequence(o.ptr());
54 return result;
55 },
56 R"pbdoc(
57 Returns true if its input is a collections.Sequence (except strings).
58
59 Args:
60 seq: an input sequence.
61
62 Returns:
63 True if the sequence is a not a string and is a collections.Sequence or a
64 dict.
65 )pbdoc");
66 m.def(
67 "IsSequenceOrComposite",
68 [](const py::handle& o) {
69 bool result = tensorflow::swig::IsSequenceOrComposite(o.ptr());
70 if (PyErr_Occurred()) {
71 throw py::error_already_set();
72 }
73 return result;
74 },
75 R"pbdoc(
76 Returns true if its input is a sequence or a `CompositeTensor`.
77
78 Args:
79 seq: an input sequence.
80
81 Returns:
82 True if the sequence is a not a string and is a collections.Sequence or a
83 dict or a CompositeTensor or a TypeSpec (except string and TensorSpec).
84 )pbdoc");
85 m.def(
86 "IsCompositeTensor",
87 [](const py::handle& o) {
88 bool result = tensorflow::swig::IsCompositeTensor(o.ptr());
89 if (PyErr_Occurred()) {
90 throw py::error_already_set();
91 }
92 return result;
93 },
94 R"pbdoc(
95 Returns true if its input is a `CompositeTensor`.
96
97 Args:
98 seq: an input sequence.
99
100 Returns:
101 True if the sequence is a CompositeTensor.
102 )pbdoc");
103 m.def(
104 "IsTypeSpec",
105 [](const py::handle& o) {
106 bool result = tensorflow::swig::IsTypeSpec(o.ptr());
107 if (PyErr_Occurred()) {
108 throw py::error_already_set();
109 }
110 return result;
111 },
112 R"pbdoc(
113 Returns true if its input is a `TypeSpec`, but is not a `TensorSpec`.
114
115 Args:
116 seq: an input sequence.
117
118 Returns:
119 True if the sequence is a `TypeSpec`, but is not a `TensorSpec`.
120 )pbdoc");
121 m.def(
122 "IsNamedtuple",
123 [](const py::handle& o, bool strict) {
124 return tensorflow::PyoOrThrow(
125 tensorflow::swig::IsNamedtuple(o.ptr(), strict));
126 },
127 R"pbdoc(
128 Check if an object is a NamedTuple.
129 )pbdoc");
130 m.def(
131 "IsMapping",
132 [](const py::handle& o) {
133 bool result = tensorflow::swig::IsMapping(o.ptr());
134 if (PyErr_Occurred()) {
135 throw py::error_already_set();
136 }
137 return result;
138 },
139 R"pbdoc(
140 Returns True if `instance` is a `collections.Mapping`.
141
142 Args:
143 instance: An instance of a Python object.
144
145 Returns:
146 True if `instance` is a `collections.Mapping`.
147 )pbdoc");
148 m.def(
149 "IsMutableMapping",
150 [](const py::handle& o) {
151 bool result = tensorflow::swig::IsMutableMapping(o.ptr());
152 if (PyErr_Occurred()) {
153 throw py::error_already_set();
154 }
155 return result;
156 },
157 R"pbdoc(
158 Returns True if `instance` is a `collections.MutableMapping`.
159
160 Args:
161 instance: An instance of a Python object.
162
163 Returns:
164 True if `instance` is a `collections.MutableMapping`.
165 )pbdoc");
166 m.def(
167 "IsMappingView",
168 [](const py::handle& o) {
169 bool result = tensorflow::swig::IsMappingView(o.ptr());
170 if (PyErr_Occurred()) {
171 throw py::error_already_set();
172 }
173 return result;
174 },
175 R"pbdoc(
176 Returns True if considered a mapping view for the purposes of Flatten()`.
177
178 Args:
179 instance: An instance of a Python object.
180
181 Returns:
182 True if considered a mapping view for the purposes of Flatten().
183 )pbdoc");
184 m.def(
185 "IsAttrs",
186 [](const py::handle& o) {
187 bool result = tensorflow::swig::IsAttrs(o.ptr());
188 if (PyErr_Occurred()) {
189 throw py::error_already_set();
190 }
191 return result;
192 },
193 R"pbdoc(
194 Returns True if `instance` is an instance of an `attr.s` decorated class.
195
196 Args:
197 instance: An instance of a Python object.
198
199 Returns:
200 True if `instance` is an instance of an `attr.s` decorated class.
201 )pbdoc");
202 m.def(
203 "SameNamedtuples",
204 [](const py::handle& o1, const py::handle& o2) {
205 return tensorflow::PyoOrThrow(
206 tensorflow::swig::SameNamedtuples(o1.ptr(), o2.ptr()));
207 },
208 R"pbdoc(
209 Returns True if the two namedtuples have the same name and fields.
210 )pbdoc");
211 m.def(
212 "AssertSameStructure",
213 [](const py::handle& o1, const py::handle& o2, bool check_types,
214 bool expand_composites) {
215 bool result = tensorflow::swig::AssertSameStructure(
216 o1.ptr(), o2.ptr(), check_types, expand_composites);
217 if (PyErr_Occurred()) {
218 throw py::error_already_set();
219 }
220 return result;
221 },
222 R"pbdoc(
223 Returns True if the two structures are nested in the same way.
224 )pbdoc");
225 m.def(
226 "Flatten",
227 [](const py::handle& o, bool expand_composites) {
228 return tensorflow::PyoOrThrow(
229 tensorflow::swig::Flatten(o.ptr(), expand_composites));
230 },
231 R"pbdoc(
232 Returns a flat list from a given nested structure.
233
234 If `nest` is not a sequence, tuple, or dict, then returns a single-element
235 list: `[nest]`.
236
237 In the case of dict instances, the sequence consists of the values, sorted by
238 key to ensure deterministic behavior. This is true also for `OrderedDict`
239 instances: their sequence order is ignored, the sorting order of keys is
240 used instead. The same convention is followed in `pack_sequence_as`. This
241 correctly repacks dicts and `OrderedDict`s after they have been flattened,
242 and also allows flattening an `OrderedDict` and then repacking it back using
243 a corresponding plain dict, or vice-versa.
244 Dictionaries with non-sortable keys cannot be flattened.
245
246 Users must not modify any collections used in `nest` while this function is
247 running.
248
249 Args:
250 nest: an arbitrarily nested structure or a scalar object. Note, numpy
251 arrays are considered scalars.
252 expand_composites: If true, then composite tensors such as `tf.sparse.SparseTensor`
253 and `tf.RaggedTensor` are expanded into their component tensors.
254
255 Returns:
256 A Python list, the flattened version of the input.
257
258 Raises:
259 TypeError: The nest is or contains a dict with non-sortable keys.
260 )pbdoc");
261 m.def(
262 "IsSequenceForData",
263 [](const py::handle& o) {
264 bool result = tensorflow::swig::IsSequenceForData(o.ptr());
265 if (PyErr_Occurred()) {
266 throw py::error_already_set();
267 }
268 return result;
269 },
270 R"pbdoc(
271 Returns a true if `seq` is a Sequence or dict (except strings/lists).
272
273 NOTE(mrry): This differs from `tensorflow.python.util.nest.is_sequence()`,
274 which *does* treat a Python list as a sequence. For ergonomic
275 reasons, `tf.data` users would prefer to treat lists as
276 implicit `tf.Tensor` objects, and dicts as (nested) sequences.
277
278 Args:
279 seq: an input sequence.
280
281 Returns:
282 True if the sequence is a not a string or list and is a
283 collections.Sequence.
284 )pbdoc");
285 m.def(
286 "FlattenForData",
287 [](const py::handle& o) {
288 return tensorflow::PyoOrThrow(
289 tensorflow::swig::FlattenForData(o.ptr()));
290 },
291 R"pbdoc(
292 Returns a flat sequence from a given nested structure.
293
294 If `nest` is not a sequence, this returns a single-element list: `[nest]`.
295
296 Args:
297 nest: an arbitrarily nested structure or a scalar object.
298 Note, numpy arrays are considered scalars.
299
300 Returns:
301 A Python list, the flattened version of the input.
302 )pbdoc");
303 m.def(
304 "AssertSameStructureForData",
305 [](const py::handle& o1, const py::handle& o2, bool check_types) {
306 bool result = tensorflow::swig::AssertSameStructureForData(
307 o1.ptr(), o2.ptr(), check_types);
308 if (PyErr_Occurred()) {
309 throw py::error_already_set();
310 }
311 return result;
312 },
313 R"pbdoc(
314 Returns True if the two structures are nested in the same way in particular tf.data.
315 )pbdoc");
316 m.def(
317 "IsResourceVariable",
318 [](const py::handle& o) {
319 bool result = tensorflow::swig::IsResourceVariable(o.ptr());
320 if (PyErr_Occurred()) {
321 throw py::error_already_set();
322 }
323 return result;
324 },
325 R"pbdoc(
326 Returns 1 if `o` is a ResourceVariable.
327
328 Args:
329 instance: An instance of a Python object.
330
331 Returns:
332 True if `instance` is a `ResourceVariable`.
333 )pbdoc");
334 m.def(
335 "IsVariable",
336 [](const py::handle& o) {
337 bool result = tensorflow::swig::IsVariable(o.ptr());
338 if (PyErr_Occurred()) {
339 throw py::error_already_set();
340 }
341 return result;
342 },
343 R"pbdoc(
344 Returns 1 if `o` is a Variable.
345
346 Args:
347 instance: An instance of a Python object.
348
349 Returns:
350 True if `instance` is a `Variable`.
351 )pbdoc");
352 m.def(
353 "IsBF16SupportedByOneDNNOnThisCPU",
354 []() {
355 bool result = tensorflow::port::TestCPUFeature(
356 tensorflow::port::CPUFeature::AVX512F);
357 if (PyErr_Occurred()) {
358 throw py::error_already_set();
359 }
360 return result;
361 },
362 R"pbdoc(
363 Returns 1 if CPU has avx512f feature.
364
365 Args:
366 None
367
368 Returns:
369 True if CPU has avx512f feature.
370 )pbdoc");
371 }
372