forked from jax-ml/jax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cc
228 lines (201 loc) · 7.16 KB
/
utils.cc
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
/* Copyright 2023 The JAX Authors.
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 <Python.h>
#include "nanobind/nanobind.h"
#include "absl/cleanup/cleanup.h"
#include "absl/container/inlined_vector.h"
namespace nb = nanobind;
namespace {
// A variant of map(...) that:
// a) returns a list instead of an iterator, and
// b) checks that the input iterables are of equal length.
PyObject* SafeMap(PyObject* self, PyObject* const* args, Py_ssize_t nargs) {
if (nargs < 2) {
PyErr_SetString(PyExc_TypeError, "safe_map requires at least 2 arguments");
return nullptr;
}
PyObject* fn = args[0];
absl::InlinedVector<nb::object, 4> iterators;
iterators.reserve(nargs - 1);
for (Py_ssize_t i = 1; i < nargs; ++i) {
PyObject* it = PyObject_GetIter(args[i]);
if (!it) return nullptr;
iterators.push_back(nb::steal<nb::object>(it));
}
// Try to use a length hint to estimate how large a list to allocate.
Py_ssize_t length_hint = PyObject_LengthHint(args[1], 2);
if (PyErr_Occurred()) {
PyErr_Clear();
}
if (length_hint < 0) {
length_hint = 2;
}
nb::list list = nb::steal<nb::list>(PyList_New(length_hint));
int n = 0; // Current true size of the list
// The arguments we will pass to fn. We allocate space for one more argument
// than we need at the start of the argument list so we can use
// PY_VECTORCALL_ARGUMENTS_OFFSET which may speed up the callee.
absl::InlinedVector<PyObject*, 4> values(nargs, nullptr);
while (true) {
absl::Cleanup values_cleanup = [&values]() {
for (PyObject* v : values) {
Py_XDECREF(v);
v = nullptr;
}
};
values[1] = PyIter_Next(iterators[0].ptr());
if (PyErr_Occurred()) return nullptr;
if (values[1]) {
for (size_t i = 1; i < iterators.size(); ++i) {
values[i + 1] = PyIter_Next(iterators[i].ptr());
if (PyErr_Occurred()) return nullptr;
if (!values[i + 1]) {
PyErr_Format(PyExc_ValueError,
"safe_map() argument %u is shorter than argument 1",
i + 1);
return nullptr;
}
}
} else {
// No more elements should be left. Checks the other iterators are
// exhausted.
for (size_t i = 1; i < iterators.size(); ++i) {
values[i + 1] = PyIter_Next(iterators[i].ptr());
if (PyErr_Occurred()) return nullptr;
if (values[i + 1]) {
PyErr_Format(PyExc_ValueError,
"safe_map() argument %u is longer than argument 1",
i + 1);
return nullptr;
}
}
// If the length hint was too large, truncate the list to the true size.
if (n < length_hint) {
if (PyList_SetSlice(list.ptr(), n, length_hint, nullptr) < 0) {
return nullptr;
}
}
return list.release().ptr();
}
nb::object out = nb::steal<nb::object>(PyObject_Vectorcall(
fn, &values[1], (nargs - 1) | PY_VECTORCALL_ARGUMENTS_OFFSET,
/*kwnames=*/nullptr));
if (PyErr_Occurred()) {
return nullptr;
}
if (n < length_hint) {
PyList_SET_ITEM(list.ptr(), n, out.release().ptr());
} else {
if (PyList_Append(list.ptr(), out.ptr()) < 0) {
return nullptr;
}
}
++n;
}
}
PyMethodDef safe_map_def = {
"safe_map",
reinterpret_cast<PyCFunction>(SafeMap),
METH_FASTCALL,
};
// A variant of zip(...) that:
// a) returns a list instead of an iterator, and
// b) checks that the input iterables are of equal length.
// TODO(phawkins): consider replacing this function with
// list(zip(..., strict=True)) once TensorFlow 2.13 is released, which should
// resolve an incompatibility with strict=True and jax2tf.
PyObject* SafeZip(PyObject* self, PyObject* const* args, Py_ssize_t nargs) {
if (nargs < 1) {
PyErr_SetString(PyExc_TypeError, "safe_zip requires at least 1 argument");
return nullptr;
}
absl::InlinedVector<nb::object, 4> iterators;
iterators.reserve(nargs);
for (Py_ssize_t i = 0; i < nargs; ++i) {
PyObject* it = PyObject_GetIter(args[i]);
if (!it) return nullptr;
iterators.push_back(nb::steal<nb::object>(it));
}
// Try to use a length hint to estimate how large a list to allocate.
Py_ssize_t length_hint = PyObject_LengthHint(args[0], 2);
if (PyErr_Occurred()) {
PyErr_Clear();
}
if (length_hint < 0) {
length_hint = 2;
}
nb::list list = nb::steal<nb::list>(PyList_New(length_hint));
int n = 0; // Current true size of the list
while (true) {
nb::object tuple;
nb::object v = nb::steal<nb::object>(PyIter_Next(iterators[0].ptr()));
if (PyErr_Occurred()) return nullptr;
if (v.ptr()) {
tuple = nb::steal<nb::object>(PyTuple_New(nargs));
if (!tuple.ptr()) return nullptr;
PyTuple_SET_ITEM(tuple.ptr(), 0, v.release().ptr());
for (size_t i = 1; i < iterators.size(); ++i) {
v = nb::steal<nb::object>(PyIter_Next(iterators[i].ptr()));
if (PyErr_Occurred()) return nullptr;
if (!v.ptr()) {
PyErr_Format(PyExc_ValueError,
"safe_zip() argument %u is shorter than argument 1",
i + 1);
return nullptr;
}
PyTuple_SET_ITEM(tuple.ptr(), i, v.release().ptr());
}
} else {
// No more elements should be left. Checks the other iterators are
// exhausted.
for (size_t i = 1; i < iterators.size(); ++i) {
v = nb::steal<nb::object>(PyIter_Next(iterators[i].ptr()));
if (PyErr_Occurred()) return nullptr;
if (v.ptr()) {
PyErr_Format(PyExc_ValueError,
"safe_zip() argument %u is longer than argument 1",
i + 1);
return nullptr;
}
}
// If the length hint was too large, truncate the list to the true size.
if (n < length_hint) {
if (PyList_SetSlice(list.ptr(), n, length_hint, nullptr) < 0) {
return nullptr;
}
}
return list.release().ptr();
}
if (n < length_hint) {
PyList_SET_ITEM(list.ptr(), n, tuple.release().ptr());
} else {
if (PyList_Append(list.ptr(), tuple.ptr()) < 0) {
return nullptr;
}
tuple = nb::object();
}
++n;
}
}
PyMethodDef safe_zip_def = {
"safe_zip",
reinterpret_cast<PyCFunction>(SafeZip),
METH_FASTCALL,
};
} // namespace
NB_MODULE(utils, m) {
nb::object module_name = m.attr("__name__");
m.attr("safe_map") = nb::steal<nb::object>(
PyCFunction_NewEx(&safe_map_def, /*self=*/nullptr, module_name.ptr()));
m.attr("safe_zip") = nb::steal<nb::object>(
PyCFunction_NewEx(&safe_zip_def, /*self=*/nullptr, module_name.ptr()));
}