/
from_data.py
161 lines (109 loc) · 4.89 KB
/
from_data.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
import numpy
from cupy import core
from cupy.core import fusion
def array(obj, dtype=None, copy=True, order='K', subok=False, ndmin=0):
"""Creates an array on the current device.
This function currently does not support the ``subok`` option.
Args:
obj: :class:`cupy.ndarray` object or any other object that can be
passed to :func:`numpy.array`.
dtype: Data type specifier.
copy (bool): If ``False``, this function returns ``obj`` if possible.
Otherwise this function always returns a new array.
order ({'C', 'F', 'A', 'K'}): Row-major (C-style) or column-major
(Fortran-style) order.
When ``order`` is ``'A'``, it uses ``'F'`` if ``a`` is column-major
and uses ``'C'`` otherwise.
And when ``order`` is ``'K'``, it keeps strides as closely as
possible.
If ``obj`` is :class:`numpy.ndarray`, the function returns ``'C'``
or ``'F'`` order array.
subok (bool): If ``True``, then sub-classes will be passed-through,
otherwise the returned array will be forced to be a base-class
array (default).
ndmin (int): Minimum number of dimensions. Ones are inserted to the
head of the shape if needed.
Returns:
cupy.ndarray: An array on the current device.
.. note::
This method currently does not support ``subok`` argument.
.. seealso:: :func:`numpy.array`
"""
return core.array(obj, dtype, copy, order, subok, ndmin)
def asarray(a, dtype=None, order=None):
"""Converts an object to array.
This is equivalent to ``array(a, dtype, copy=False)``.
This function currently does not support the ``order`` option.
Args:
a: The source object.
dtype: Data type specifier. It is inferred from the input by default.
order ({'C', 'F'}):
Whether to use row-major (C-style) or column-major (Fortran-style)
memory representation. Defaults to ``'C'``. ``order`` is ignored
for objects that are not :class:`cupy.ndarray`, but have the
``__cuda_array_interface__`` attribute.
Returns:
cupy.ndarray: An array on the current device. If ``a`` is already on
the device, no copy is performed.
.. seealso:: :func:`numpy.asarray`
"""
return core.array(a, dtype, False, order)
def asanyarray(a, dtype=None, order=None):
"""Converts an object to array.
This is currently equivalent to :func:`cupy.asarray`, since there is no
subclass of :class:`cupy.ndarray` in CuPy. Note that the original
:func:`numpy.asanyarray` returns the input array as is if it is an instance
of a subtype of :class:`numpy.ndarray`.
.. seealso:: :func:`cupy.asarray`, :func:`numpy.asanyarray`
"""
return core.array(a, dtype, False, order)
def ascontiguousarray(a, dtype=None):
"""Returns a C-contiguous array.
Args:
a (cupy.ndarray): Source array.
dtype: Data type specifier.
Returns:
cupy.ndarray: If no copy is required, it returns ``a``. Otherwise, it
returns a copy of ``a``.
.. seealso:: :func:`numpy.ascontiguousarray`
"""
return core.ascontiguousarray(a, dtype)
# TODO(okuta): Implement asmatrix
def copy(a, order='K'):
"""Creates a copy of a given array on the current device.
This function allocates the new array on the current device. If the given
array is allocated on the different device, then this function tries to
copy the contents over the devices.
Args:
a (cupy.ndarray): The source array.
order ({'C', 'F', 'A', 'K'}): Row-major (C-style) or column-major
(Fortran-style) order.
When ``order`` is ``'A'``, it uses ``'F'`` if ``a`` is column-major
and uses ``'C'`` otherwise.
And when ``order`` is ``'K'``, it keeps strides as closely as
possible.
Returns:
cupy.ndarray: The copy of ``a`` on the current device.
.. seealso:: :func:`numpy.copy`, :meth:`cupy.ndarray.copy`
"""
if fusion._is_fusing():
if order != 'K':
raise NotImplementedError(
'cupy.copy does not support `order` in fusion yet.')
return fusion._call_ufunc(core.elementwise_copy, a)
# If the current device is different from the device of ``a``, then this
# function allocates a new array on the current device, and copies the
# contents over the devices.
return a.copy(order=order)
# TODO(okuta): Implement frombuffer
def fromfile(*args, **kwargs):
"""Reads an array from a file.
.. note::
Uses NumPy's ``fromfile`` and coerces the result to a CuPy array.
.. seealso:: :func:`numpy.fromfile`
"""
return asarray(numpy.fromfile(*args, **kwargs))
# TODO(okuta): Implement fromfunction
# TODO(okuta): Implement fromiter
# TODO(okuta): Implement fromstring
# TODO(okuta): Implement loadtxt