FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
uTensor/src/uTensor/core/tensor.cpp at develop · uTensor/uTensor · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
uTensor
/
uTensor
Public
Notifications
You must be signed in to change notification settings
Fork
250
Star
1.9k
Code
Issues
51
Pull requests
5
Discussions
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
uTensor
/
src
/
uTensor
/
core
/
tensor.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
178 lines (165 loc) · 5.7 KB
Breadcrumbs
uTensor
/
src
/
uTensor
/
core
/
tensor.cpp
Copy path
File metadata and controls
178 lines (165 loc) · 5.7 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
#
include
"
uTensor/core/tensor.hpp
"
#
include
"
uTensor/core/context.hpp
"
#
include
"
uTensor/core/uTensor_util.hpp
"
namespace
uTensor
{
//
Tensor::Tensor(const Tensor& that) {} // Cannot copy Tensors, must pass by
//
reference
TensorInterface* Tensor::
operator
->() {
return
reinterpret_cast
<TensorInterface*>(_ptr);
}
const
TensorInterface* Tensor::
operator
->()
const
{
return
reinterpret_cast
<
const
TensorInterface*>(_ptr);
}
TensorInterface* Tensor::
operator
*() {
return
reinterpret_cast
<TensorInterface*>(_ptr);
}
Tensor::~Tensor
() {
free
(); }
void
Tensor::free
() {
void
*
ptr_t
= _ptr;
//
unbind invalidates this handle so store a copy
if
(_ptr) {
AllocatorInterface* alloc =
Context::get_default_context
()->
get_metadata_allocator
();
if
(alloc->
is_bound
(_ptr,
this
)) {
alloc->
unbind
(_ptr,
this
);
}
delete
reinterpret_cast
<TensorInterface*>(
ptr_t
);
alloc->
deallocate
(
ptr_t
);
}
_ptr =
nullptr
;
}
Tensor::Tensor
() : Handle() {}
Tensor::Tensor
(TensorInterface* ptr) : Handle((
void
*)ptr) {
//
Context::get_default_context()->get_metadata_allocator()->bind(_ptr, this);
if
(ptr !=
nullptr
) {
bind
(*
this
, *
Context::get_default_context
()->
get_metadata_allocator
());
}
}
Tensor& Tensor::
operator
=(TensorInterface* ptr) {
_ptr = (
void
*)ptr;
bind
(*
this
, *
Context::get_default_context
()->
get_metadata_allocator
());
//
Context::get_metadata_allocator()->bind(_ptr, this);
return
*
this
;
}
Tensor::Tensor
(Tensor&& that) {
_ptr = that.
_ptr
;
AllocatorInterface* alloc =
Context::get_default_context
()->
get_metadata_allocator
();
if
(alloc->
is_bound
(_ptr, &that)) {
alloc->
unbind
(_ptr, &that);
alloc->
bind
(_ptr,
this
);
}
that.
_ptr
=
nullptr
;
}
Tensor& Tensor::
operator
=(Tensor&& that) {
if
(
this
!= &that) {
_ptr = that.
_ptr
;
AllocatorInterface* alloc =
Context::get_default_context
()->
get_metadata_allocator
();
if
(alloc->
is_bound
(_ptr, &that)) {
alloc->
unbind
(_ptr, &that);
alloc->
bind
(_ptr,
this
);
}
that.
_ptr
=
nullptr
;
}
return
*
this
;
}
//
Add some bits to make the interface nicer to the user
//
Force everything to be on the utensor allocator
void
* Tensor::
operator
new
(
size_t
sz) {
//
Have to delegate this size from
//
tensors somehow + sizeof(Tensor)
void
* p =
Context::get_default_context
()->
get_metadata_allocator
()->
allocate
(sz);
return
p;
}
void
Tensor::
operator
delete
(
void
* p) {
Context::get_default_context
()->
get_metadata_allocator
()->
deallocate
(p);
}
//
Interface
const
IntegralValue
Tensor::operator
()(
uint16_t
i,
uint16_t
j,
uint16_t
k,
uint16_t
l)
const
{
return
reinterpret_cast
<TensorInterface*>(_ptr)->
operator
()(i, j, k, l);
}
IntegralValue
Tensor::operator
()(
uint16_t
i,
uint16_t
j,
uint16_t
k,
uint16_t
l) {
return
reinterpret_cast
<TensorInterface*>(_ptr)->
operator
()(i, j, k, l);
}
const
IntegralValue
Tensor::operator
()(
uint32_t
linear_index)
const
{
return
reinterpret_cast
<TensorInterface*>(_ptr)->
operator
()(linear_index);
}
IntegralValue
Tensor::operator
()(
uint32_t
linear_index) {
return
reinterpret_cast
<TensorInterface*>(_ptr)->
operator
()(linear_index);
}
TensorShape&
Tensor::get_shape
() {
return
reinterpret_cast
<TensorInterface*>(_ptr)->
get_shape
();
}
const
TensorShape&
Tensor::get_shape
()
const
{
return
reinterpret_cast
<TensorInterface*>(_ptr)->
get_shape
();
}
TensorInterface* TensorReference::
operator
*() {
return
reinterpret_cast
<TensorInterface*>(_ref->
operator
*());
}
//
Add a couple of bits for GDB debugging since GDB doesnt support operator()
IntegralValue
Tensor::gdb_read
(
uint16_t
i) {
return
reinterpret_cast
<TensorInterface*>(_ptr)->
operator
()(i);
}
IntegralValue
Tensor::gdb_read
(
uint16_t
i,
uint16_t
j) {
return
reinterpret_cast
<TensorInterface*>(_ptr)->
operator
()(i, j);
}
IntegralValue
Tensor::gdb_read
(
uint16_t
i,
uint16_t
j,
uint16_t
k) {
return
reinterpret_cast
<TensorInterface*>(_ptr)->
operator
()(i, j, k);
}
IntegralValue
Tensor::gdb_read
(
uint16_t
i,
uint16_t
j,
uint16_t
k,
uint16_t
l) {
return
reinterpret_cast
<TensorInterface*>(_ptr)->
operator
()(i, j, k, l);
}
void
print
(
const
Tensor& t) {
const
TensorShape& t_shape = t->
get_shape
();
if
(t_shape.
num_dims
() >
2
) {
uTensor_printf
(
"
printing > 2D tensors not supported
\n
"
);
return
;
}
uTensor_printf
(
"
[
\n
"
);
for
(
int
j =
0
; j < t_shape[
1
]; j++) {
uTensor_printf
(
"
[
"
);
for
(
int
i =
0
; i < t_shape[
0
]; i++) {
switch
(t->
get_type
()) {
case
u8
:
uTensor_printf
(
"
%hhu
"
,
static_cast
<
uint8_t
>(
t
(j, i)));
break
;
case
i8
:
uTensor_printf
(
"
%hhd
"
,
static_cast
<
int8_t
>(
t
(j, i)));
break
;
case
u16
:
uTensor_printf
(
"
%hu
"
,
static_cast
<
uint16_t
>(
t
(j, i)));
break
;
case
i16
:
uTensor_printf
(
"
%hd
"
,
static_cast
<
int16_t
>(
t
(j, i)));
break
;
case
u32
:
uTensor_printf
(
"
%u
"
,
static_cast
<
uint32_t
>(
t
(j, i)));
break
;
case
i32
:
uTensor_printf
(
"
%d
"
,
static_cast
<
int32_t
>(
t
(j, i)));
break
;
case
flt:
uTensor_printf
(
"
%f
"
,
static_cast
<
float
>(
t
(j, i)));
break
;
default
:
uTensor_printf
(
"
Unknown data type
"
);
return
;
}
if
(i != (t_shape[
0
] -
1
)) {
uTensor_printf
(
"
,
"
);
}
else
{
uTensor_printf
(
"
"
);
}
}
uTensor_printf
(
"
]
\n
"
);
}
uTensor_printf
(
"
]
\n
"
);
}
SimpleNamedTensor::SimpleNamedTensor
(
const
uTensor::string& name,
Tensor& tensor)
: name(&name), _tensor(&tensor) {}
SimpleNamedTensor::SimpleNamedTensor
() : name(
nullptr
), _tensor(
nullptr
) {}
Tensor&
SimpleNamedTensor::tensor
() {
return
*_tensor; }
}
//
namespace uTensor
Back
|
FazBrowse Home
|
New Git URL