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