/
order.py
340 lines (267 loc) · 11.5 KB
/
order.py
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
338
339
340
import warnings
import cupy
from cupy import _core
from cupy._core import _routines_statistics as _statistics
from cupy._core import _fusion_thread_local
from cupy._logic import content
def amin(a, axis=None, out=None, keepdims=False):
"""Returns the minimum of an array or the minimum along an axis.
.. note::
When at least one element is NaN, the corresponding min value will be
NaN.
Args:
a (cupy.ndarray): Array to take the minimum.
axis (int): Along which axis to take the minimum. The flattened array
is used by default.
out (cupy.ndarray): Output array.
keepdims (bool): If ``True``, the axis is remained as an axis of
size one.
Returns:
cupy.ndarray: The minimum of ``a``, along the axis if specified.
.. note::
When cuTENSOR accelerator is used, the output value might be collapsed
for reduction axes that have one or more NaN elements.
.. seealso:: :func:`numpy.amin`
"""
if _fusion_thread_local.is_fusing():
if keepdims:
raise NotImplementedError(
'cupy.amin does not support `keepdims` in fusion yet.')
return _fusion_thread_local.call_reduction(
_statistics.amin, a, axis=axis, out=out)
# TODO(okuta): check type
return a.min(axis=axis, out=out, keepdims=keepdims)
def amax(a, axis=None, out=None, keepdims=False):
"""Returns the maximum of an array or the maximum along an axis.
.. note::
When at least one element is NaN, the corresponding min value will be
NaN.
Args:
a (cupy.ndarray): Array to take the maximum.
axis (int): Along which axis to take the maximum. The flattened array
is used by default.
out (cupy.ndarray): Output array.
keepdims (bool): If ``True``, the axis is remained as an axis of
size one.
Returns:
cupy.ndarray: The maximum of ``a``, along the axis if specified.
.. note::
When cuTENSOR accelerator is used, the output value might be collapsed
for reduction axes that have one or more NaN elements.
.. seealso:: :func:`numpy.amax`
"""
if _fusion_thread_local.is_fusing():
if keepdims:
raise NotImplementedError(
'cupy.amax does not support `keepdims` in fusion yet.')
return _fusion_thread_local.call_reduction(
_statistics.amax, a, axis=axis, out=out)
# TODO(okuta): check type
return a.max(axis=axis, out=out, keepdims=keepdims)
def nanmin(a, axis=None, out=None, keepdims=False):
"""Returns the minimum of an array along an axis ignoring NaN.
When there is a slice whose elements are all NaN, a :class:`RuntimeWarning`
is raised and NaN is returned.
Args:
a (cupy.ndarray): Array to take the minimum.
axis (int): Along which axis to take the minimum. The flattened array
is used by default.
out (cupy.ndarray): Output array.
keepdims (bool): If ``True``, the axis is remained as an axis of
size one.
Returns:
cupy.ndarray: The minimum of ``a``, along the axis if specified.
.. warning::
This function may synchronize the device.
.. seealso:: :func:`numpy.nanmin`
"""
# TODO(niboshi): Avoid synchronization.
res = _core.nanmin(a, axis=axis, out=out, keepdims=keepdims)
if content.isnan(res).any(): # synchronize!
warnings.warn('All-NaN slice encountered', RuntimeWarning)
return res
def nanmax(a, axis=None, out=None, keepdims=False):
"""Returns the maximum of an array along an axis ignoring NaN.
When there is a slice whose elements are all NaN, a :class:`RuntimeWarning`
is raised and NaN is returned.
Args:
a (cupy.ndarray): Array to take the maximum.
axis (int): Along which axis to take the maximum. The flattened array
is used by default.
out (cupy.ndarray): Output array.
keepdims (bool): If ``True``, the axis is remained as an axis of
size one.
Returns:
cupy.ndarray: The maximum of ``a``, along the axis if specified.
.. warning::
This function may synchronize the device.
.. seealso:: :func:`numpy.nanmax`
"""
# TODO(niboshi): Avoid synchronization.
res = _core.nanmax(a, axis=axis, out=out, keepdims=keepdims)
if content.isnan(res).any(): # synchronize!
warnings.warn('All-NaN slice encountered', RuntimeWarning)
return res
def ptp(a, axis=None, out=None, keepdims=False):
"""Returns the range of values (maximum - minimum) along an axis.
.. note::
The name of the function comes from the acronym for 'peak to peak'.
When at least one element is NaN, the corresponding ptp value will be
NaN.
Args:
a (cupy.ndarray): Array over which to take the range.
axis (int): Axis along which to take the minimum. The flattened
array is used by default.
out (cupy.ndarray): Output array.
keepdims (bool): If ``True``, the axis is retained as an axis of
size one.
Returns:
cupy.ndarray: The minimum of ``a``, along the axis if specified.
.. note::
When cuTENSOR accelerator is used, the output value might be collapsed
for reduction axes that have one or more NaN elements.
.. seealso:: :func:`numpy.amin`
"""
return a.ptp(axis=axis, out=out, keepdims=keepdims)
def _quantile_unchecked(a, q, axis=None, out=None, interpolation='linear',
keepdims=False):
if q.ndim == 0:
q = q[None]
zerod = True
else:
zerod = False
if q.ndim > 1:
raise ValueError('Expected q to have a dimension of 1.\n'
'Actual: {0} != 1'.format(q.ndim))
if keepdims:
if axis is None:
keepdim = (1,) * a.ndim
else:
keepdim = list(a.shape)
for ax in axis:
keepdim[ax % a.ndim] = 1
keepdim = tuple(keepdim)
# Copy a since we need it sorted but without modifying the original array
if isinstance(axis, int):
axis = axis,
if axis is None:
ap = a.flatten()
nkeep = 0
else:
# Reduce axes from a and put them last
axis = tuple(ax % a.ndim for ax in axis)
keep = set(range(a.ndim)) - set(axis)
nkeep = len(keep)
for i, s in enumerate(sorted(keep)):
a = a.swapaxes(i, s)
ap = a.reshape(a.shape[:nkeep] + (-1,)).copy()
axis = -1
ap.sort(axis=axis)
Nx = ap.shape[axis]
indices = q * (Nx - 1.)
if interpolation == 'lower':
indices = cupy.floor(indices).astype(cupy.int32)
elif interpolation == 'higher':
indices = cupy.ceil(indices).astype(cupy.int32)
elif interpolation == 'midpoint':
indices = 0.5 * (cupy.floor(indices) + cupy.ceil(indices))
elif interpolation == 'nearest':
# TODO(hvy): Implement nearest using around
raise ValueError('\'nearest\' interpolation is not yet supported. '
'Please use any other interpolation method.')
elif interpolation == 'linear':
pass
else:
raise ValueError('Unexpected interpolation method.\n'
'Actual: \'{0}\' not in (\'linear\', \'lower\', '
'\'higher\', \'midpoint\')'.format(interpolation))
if indices.dtype == cupy.int32:
ret = cupy.rollaxis(ap, axis)
ret = ret.take(indices, axis=0, out=out)
else:
if out is None:
ret = cupy.empty(ap.shape[:-1] + q.shape, dtype=cupy.float64)
else:
ret = cupy.rollaxis(out, 0, out.ndim)
cupy.ElementwiseKernel(
'S idx, raw T a, raw int32 offset, raw int32 size', 'U ret',
'''
ptrdiff_t idx_below = floor(idx);
U weight_above = idx - idx_below;
ptrdiff_t max_idx = size - 1;
ptrdiff_t offset_bottom = _ind.get()[0] * offset + idx_below;
ptrdiff_t offset_top = min(offset_bottom + 1, max_idx);
U diff = a[offset_top] - a[offset_bottom];
if (weight_above < 0.5) {
ret = a[offset_bottom] + diff * weight_above;
} else {
ret = a[offset_top] - diff * (1 - weight_above);
}
''',
'cupy_percentile_weightnening'
)(indices, ap, ap.shape[-1] if ap.ndim > 1 else 0, ap.size, ret)
ret = cupy.rollaxis(ret, -1) # Roll q dimension back to first axis
if zerod:
ret = ret.squeeze(0)
if keepdims:
if q.size > 1:
keepdim = (-1,) + keepdim
ret = ret.reshape(keepdim)
return _core._internal_ascontiguousarray(ret)
def _quantile_is_valid(q):
if cupy.count_nonzero(q < 0.0) or cupy.count_nonzero(q > 1.0):
return False
return True
def percentile(a, q, axis=None, out=None, interpolation='linear',
keepdims=False):
"""Computes the q-th percentile of the data along the specified axis.
Args:
a (cupy.ndarray): Array for which to compute percentiles.
q (float, tuple of floats or cupy.ndarray): Percentiles to compute
in the range between 0 and 100 inclusive.
axis (int or tuple of ints): Along which axis or axes to compute the
percentiles. The flattened array is used by default.
out (cupy.ndarray): Output array.
interpolation (str): Interpolation method when a quantile lies between
two data points. ``linear`` interpolation is used by default.
Supported interpolations are``lower``, ``higher``, ``midpoint``,
``nearest`` and ``linear``.
keepdims (bool): If ``True``, the axis is remained as an axis of
size one.
Returns:
cupy.ndarray: The percentiles of ``a``, along the axis if specified.
.. seealso:: :func:`numpy.percentile`
"""
if not isinstance(q, cupy.ndarray):
q = cupy.asarray(q, dtype='d')
q = cupy.true_divide(q, 100)
if not _quantile_is_valid(q): # synchronize
raise ValueError('Percentiles must be in the range [0, 100]')
return _quantile_unchecked(a, q, axis=axis, out=out,
interpolation=interpolation, keepdims=keepdims)
def quantile(a, q, axis=None, out=None, interpolation='linear',
keepdims=False):
"""Computes the q-th quantile of the data along the specified axis.
Args:
a (cupy.ndarray): Array for which to compute quantiles.
q (float, tuple of floats or cupy.ndarray): Quantiles to compute
in the range between 0 and 1 inclusive.
axis (int or tuple of ints): Along which axis or axes to compute the
quantiles. The flattened array is used by default.
out (cupy.ndarray): Output array.
interpolation (str): Interpolation method when a quantile lies between
two data points. ``linear`` interpolation is used by default.
Supported interpolations are``lower``, ``higher``, ``midpoint``,
``nearest`` and ``linear``.
keepdims (bool): If ``True``, the axis is remained as an axis of
size one.
Returns:
cupy.ndarray: The quantiles of ``a``, along the axis if specified.
.. seealso:: :func:`numpy.quantile`
"""
if not isinstance(q, cupy.ndarray):
q = cupy.asarray(q, dtype='d')
if not _quantile_is_valid(q): # synchronize
raise ValueError('Quantiles must be in the range [0, 1]')
return _quantile_unchecked(a, q, axis=axis, out=out,
interpolation=interpolation, keepdims=keepdims)