FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
xtensor-python/include/xtensor-python/pytensor.hpp at master · BenJamesbabala/xtensor-python · GitHub
BenJamesbabala
xtensor-python
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
xtensor-python
/
include
/
xtensor-python
/
pytensor.hpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
304 lines (251 loc) · 9.63 KB
Breadcrumbs
xtensor-python
/
include
/
xtensor-python
/
pytensor.hpp
Copy path
File metadata and controls
304 lines (251 loc) · 9.63 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
**************************************************************************
* Copyright (c) 2016, Johan Mabille and Sylvain Corlay *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
***************************************************************************
*/
#
ifndef
PY_TENSOR_HPP
#
define
PY_TENSOR_HPP
#
include
<
cstddef
>
#
include
<
array
>
#
include
<
algorithm
>
#
include
"
xtensor/xutils.hpp
"
#
include
"
xtensor/xsemantic.hpp
"
#
include
"
xtensor/xiterator.hpp
"
#
include
"
pycontainer.hpp
"
#
include
"
pybuffer_adaptor.hpp
"
namespace
xt
{
template
<
class
T
, std::
size_t
N>
class
pytensor
;
}
namespace
pybind11
{
namespace
detail
{
template
<
class
T
, std::
size_t
N>
struct
handle_type_name
<xt::pytensor<T, N>>
{
static
PYBIND11_DESCR
name
()
{
return
_
(
"
numpy.ndarray[
"
) + make_caster<T>::
name
() +
_
(
"
]
"
);
}
};
template
<
class
T
, std::
size_t
N>
struct
pyobject_caster
<xt::pytensor<T, N>>
{
using
type = xt::pytensor<T, N>;
bool
load
(handle src,
bool
)
{
value =
type::ensure
(src);
return
static_cast
<
bool
>(value);
}
static
handle
cast
(
const
handle& src, return_value_policy, handle)
{
return
src.
inc_ref
();
}
PYBIND11_TYPE_CASTER
(type, handle_type_name<type>::name());
};
}
}
namespace
xt
{
template
<
class
T
, std::
size_t
N>
struct
xiterable_inner_types
<pytensor<T, N>>
: xcontainer_iterable_types<pytensor<T, N>>
{
};
template
<
class
T
, std::
size_t
N>
struct
xcontainer_inner_types
<pytensor<T, N>>
{
using
container_type = pybuffer_adaptor<T>;
using
shape_type = std::array<npy_intp, N>;
using
strides_type = shape_type;
using
backstrides_type = shape_type;
using
inner_shape_type = shape_type;
using
inner_strides_type = strides_type;
using
inner_backstrides_type = backstrides_type;
using
temporary_type = pytensor<T, N>;
};
template
<
class
T
, std::
size_t
N>
class
pytensor
:
public
pycontainer
<pytensor<T, N>>,
public xcontainer_semantic<pytensor<T, N>>
{
public:
using
self_type = pytensor<T, N>;
using
semantic_base = xcontainer_semantic<self_type>;
using
base_type = pycontainer<self_type>;
using
container_type =
typename
base_type::container_type;
using
value_type =
typename
base_type::value_type;
using
pointer =
typename
base_type::pointer;
using
size_type =
typename
base_type::size_type;
using
shape_type =
typename
base_type::shape_type;
using
strides_type =
typename
base_type::strides_type;
using
backstrides_type =
typename
base_type::backstrides_type;
using
inner_shape_type =
typename
base_type::inner_shape_type;
using
inner_strides_type =
typename
base_type::inner_strides_type;
using
inner_backstrides_type =
typename
base_type::inner_backstrides_type;
pytensor
() =
default
;
pytensor
(
nested_initializer_list_t
<T, N> t);
pytensor
(pybind11::handle h, pybind11::object::
borrowed_t
);
pytensor
(pybind11::handle h, pybind11::object::
stolen_t
);
pytensor
(
const
pybind11::object &o);
explicit
pytensor
(
const
shape_type& shape, layout l = layout::row_major);
pytensor
(
const
shape_type& shape,
const
strides_type& strides);
template
<
class
E
>
pytensor
(
const
xexpression<E>& e);
template
<
class
E
>
self_type&
operator
=(
const
xexpression<E>& e);
using
base_type::begin;
using
base_type::end;
static
self_type
ensure
(pybind11::handle h);
static
bool
check_
(pybind11::handle h);
private:
inner_shape_type m_shape;
inner_strides_type m_strides;
inner_backstrides_type m_backstrides;
container_type m_data;
void
init_tensor
(
const
shape_type& shape,
const
strides_type& strides);
void
init_from_python
();
void
compute_backstrides
();
const
inner_shape_type&
shape_impl
()
const
noexcept
;
const
inner_strides_type&
strides_impl
()
const
noexcept
;
const
inner_backstrides_type&
backstrides_impl
()
const
noexcept
;
container_type&
data_impl
()
noexcept
;
const
container_type&
data_impl
()
const
noexcept
;
friend
class
xcontainer
<pytensor<T, N>>;
};
/*
**************************
* pytensor implementation *
**************************
*/
template
<
class
T
, std::
size_t
N>
inline
pytensor<T, N>::pytensor(
nested_initializer_list_t
<T, N> t)
{
base_type::reshape
(xt::shape<shape_type>(t), layout::row_major);
nested_copy
(m_data.
begin
(), t);
}
template
<
class
T
, std::
size_t
N>
inline
pytensor<T, N>::pytensor(pybind11::handle h, pybind11::object::
borrowed_t
)
: base_type(h, pybind11::object::borrowed)
{
init_from_python
();
}
template
<
class
T
, std::
size_t
N>
inline
pytensor<T, N>::pytensor(pybind11::handle h, pybind11::object::
stolen_t
)
: base_type(h, pybind11::object::stolen)
{
init_from_python
();
}
template
<
class
T
, std::
size_t
N>
inline
pytensor<T, N>::pytensor(
const
pybind11::object& o)
: base_type(o)
{
init_from_python
();
}
template
<
class
T
, std::
size_t
N>
inline
pytensor<T, N>::pytensor(
const
shape_type& shape, layout l)
{
base_type::fill_default_strides
(shape, l, m_strides);
init_tensor
(shape, m_strides);
}
template
<
class
T
, std::
size_t
N>
inline
pytensor<T, N>::pytensor(
const
shape_type& shape,
const
strides_type& strides)
{
init_tensor
(shape, strides);
}
template
<
class
T
, std::
size_t
N>
template
<
class
E
>
inline
pytensor<T, N>::pytensor(
const
xexpression<E>& e)
{
semantic_base::assign
(e);
}
template
<
class
T
, std::
size_t
N>
template
<
class
E
>
inline
auto
pytensor<T, N>::
operator
=(
const
xexpression<E>& e) -> self_type&
{
return
semantic_base::
operator
=(e);
}
template
<
class
T
, std::
size_t
N>
inline
auto
pytensor<T, N>::ensure(pybind11::handle h) -> self_type
{
return
base_type::ensure
(h);
}
template
<
class
T
, std::
size_t
N>
inline
bool
pytensor<T, N>::check_(pybind11::handle h)
{
return
base_type::check_
(h);
}
template
<
class
T
, std::
size_t
N>
inline
void
pytensor<T, N>::init_tensor(
const
shape_type& shape,
const
strides_type& strides)
{
npy_intp python_strides[N];
std::transform
(strides.
begin
(), strides.
end
(), python_strides,
[](
auto
v) {
return
sizeof
(T) * v; });
int
flags =
NPY_ARRAY_ALIGNED
;
if
(!std::is_const<T>::value)
{
flags |=
NPY_ARRAY_WRITEABLE
;
}
int
type_num = detail::numpy_traits<T>::type_num;
auto
tmp = pybind11::reinterpret_steal<pybind11::object>(
PyArray_New
(&PyArray_Type, N,
const_cast
<npy_intp*>(shape.
data
()),
type_num, python_strides,
nullptr
,
sizeof
(T), flags,
nullptr
)
);
if
(!tmp)
throw
std::runtime_error
(
"
NumPy: unable to create ndarray
"
);
this
->
m_ptr
= tmp.
release
().
ptr
();
m_shape = shape;
m_strides = strides;
compute_backstrides
();
m_data =
container_type
(
reinterpret_cast
<pointer>(
PyArray_DATA
(
this
->
python_array
())),
static_cast
<size_type>(
PyArray_SIZE
(
this
->
python_array
())));
}
template
<
class
T
, std::
size_t
N>
inline
void
pytensor<T, N>::init_from_python()
{
if
(
PyArray_NDIM
(
this
->
python_array
()) != N)
throw
std::runtime_error
(
"
NumPy: ndarray has incorrect number of dimensions
"
);
std::copy
(
PyArray_DIMS
(
this
->
python_array
()),
PyArray_DIMS
(
this
->
python_array
()) + N, m_shape.
begin
());
std::transform
(
PyArray_STRIDES
(
this
->
python_array
()),
PyArray_STRIDES
(
this
->
python_array
()) + N, m_strides.
begin
(),
[](
auto
v) {
return
v /
sizeof
(T); });
compute_backstrides
();
m_data =
container_type
(
reinterpret_cast
<pointer>(
PyArray_DATA
(
this
->
python_array
())),
static_cast
<size_type>(
PyArray_SIZE
(
this
->
python_array
())));
}
template
<
class
T
, std::
size_t
N>
inline
void
pytensor<T, N>::compute_backstrides()
{
for
(size_type i =
0
; i < m_shape.
size
(); ++i)
{
m_backstrides[i] = m_strides[i] * (m_shape[i] -
1
);
}
}
template
<
class
T
, std::
size_t
N>
inline
auto
pytensor<T, N>::shape_impl()
const
noexcept
->
const
inner_shape_type&
{
return
m_shape;
}
template
<
class
T
, std::
size_t
N>
inline
auto
pytensor<T, N>::strides_impl()
const
noexcept
->
const
inner_strides_type&
{
return
m_strides;
}
template
<
class
T
, std::
size_t
N>
inline
auto
pytensor<T, N>::backstrides_impl()
const
noexcept
->
const
inner_backstrides_type&
{
return
m_backstrides;
}
template
<
class
T
, std::
size_t
N>
inline
auto
pytensor<T, N>::data_impl()
noexcept
-> container_type&
{
return
m_data;
}
template
<
class
T
, std::
size_t
N>
inline
auto
pytensor<T, N>::data_impl()
const
noexcept
->
const
container_type&
{
return
m_data;
}
}
#
endif
Back
|
FazBrowse Home
|
New Git URL