Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
aidge_core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Cyril Moineau
aidge_core
Commits
53f2e8b8
Commit
53f2e8b8
authored
1 year ago
by
Cyril Moineau
Browse files
Options
Downloads
Patches
Plain Diff
[Tensor] Add get & set method at Tensor level.
parent
4f3d4fbc
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
include/aidge/backend/TensorImpl.hpp
+3
-0
3 additions, 0 deletions
include/aidge/backend/TensorImpl.hpp
include/aidge/data/Tensor.hpp
+32
-17
32 additions, 17 deletions
include/aidge/data/Tensor.hpp
python_binding/data/pybind_Tensor.cpp
+26
-11
26 additions, 11 deletions
python_binding/data/pybind_Tensor.cpp
with
61 additions
and
28 deletions
include/aidge/backend/TensorImpl.hpp
+
3
−
0
View file @
53f2e8b8
...
...
@@ -27,6 +27,9 @@ public:
{
printf
(
"Cannot set raw pointer for backend %s
\n
"
,
mBackend
);
};
virtual
void
*
getRaw
(
std
::
size_t
/*idx*/
)
=
0
;
virtual
std
::
size_t
scalarSize
()
const
=
0
;
// Size of one scalar (in bytes)
constexpr
const
char
*
backend
()
const
{
return
mBackend
;
}
virtual
~
TensorImpl
()
=
default
;
...
...
This diff is collapsed.
Click to expand it.
include/aidge/data/Tensor.hpp
+
32
−
17
View file @
53f2e8b8
...
...
@@ -446,18 +446,33 @@ class Tensor : public Data,
*/
bool
empty
()
const
{
return
mDims
.
empty
();
}
template
<
typename
expectedType
,
std
::
array
<
std
::
size_t
,
1
>
::
size_type
DIM
>
constexpr
expectedType
&
get
(
std
::
array
<
std
::
size_t
,
DIM
>
idx
)
{
assert
(
DIM
==
mDims
.
size
());
assert
(
mImpl
);
std
::
size_t
unfoldedIdx
=
0
;
for
(
std
::
size_t
i
=
0
;
i
<
DIM
-
std
::
size_t
(
1
);
++
i
)
{
unfoldedIdx
=
(
unfoldedIdx
+
idx
[
i
])
*
mDims
[
i
+
1
];
}
unfoldedIdx
+=
idx
[
DIM
-
1
];
return
static_cast
<
expectedType
*>
(
mImpl
->
rawPtr
())[
unfoldedIdx
];
template
<
typename
expectedType
>
expectedType
&
get
(
std
::
size_t
idx
){
// TODO : add assert expected Type compatible with datatype
// TODO : add assert idx < Size
return
*
reinterpret_cast
<
expectedType
*>
(
mImpl
->
getRaw
(
idx
));
}
template
<
typename
expectedType
>
expectedType
&
get
(
std
::
vector
<
std
::
size_t
>
coordIdx
){
return
get
<
expectedType
>
(
getIdx
(
coordIdx
));
}
template
<
typename
expectedType
>
void
set
(
std
::
size_t
idx
,
expectedType
value
){
// TODO : add assert expected Type compatible with datatype
// TODO : add assert idx < Size
void
*
dataPtr
=
mImpl
->
getRaw
(
idx
);
std
::
memcpy
(
dataPtr
,
&
value
,
sizeof
(
expectedType
));
}
template
<
typename
expectedType
>
void
set
(
std
::
vector
<
std
::
size_t
>
coordIdx
,
expectedType
value
){
set
<
expectedType
>
(
getIdx
(
coordIdx
),
value
);
}
std
::
string
toString
()
{
if
(
dims
().
empty
())
{
return
"{}"
;
}
std
::
string
res
;
...
...
@@ -565,10 +580,10 @@ class Tensor : public Data,
* @param flatIdx 1D index of the value considering a flatten tensor.
* @return std::vector<DimSize_t>
*/
std
::
vector
<
DimS
ize_t
>
getCoord
(
DimS
ize_t
flatIdx
){
std
::
vector
<
DimS
ize_t
>
coordIdx
=
{};
DimS
ize_t
idx
=
flatIdx
;
for
(
DimS
ize_t
d
:
mDims
){
std
::
vector
<
std
::
s
ize_t
>
getCoord
(
std
::
s
ize_t
flatIdx
){
std
::
vector
<
std
::
s
ize_t
>
coordIdx
=
{};
std
::
s
ize_t
idx
=
flatIdx
;
for
(
std
::
s
ize_t
d
:
mDims
){
coordIdx
.
push_back
(
idx
%
d
);
idx
/=
d
;
}
...
...
@@ -581,9 +596,9 @@ class Tensor : public Data,
* @param coordIdx Coordinate to an element in the tensor
* @return DimSize_t
*/
DimS
ize_t
getIdx
(
std
::
vector
<
DimS
ize_t
>
coordIdx
){
DimS
ize_t
flatIdx
=
0
;
DimS
ize_t
stride
=
1
;
std
::
s
ize_t
getIdx
(
std
::
vector
<
std
::
s
ize_t
>
coordIdx
){
std
::
s
ize_t
flatIdx
=
0
;
std
::
s
ize_t
stride
=
1
;
assert
(
coordIdx
.
size
()
==
mDims
.
size
()
&&
"Coordinates does not match number of dimensions"
);
for
(
std
::
size_t
i
=
0
;
i
<
mDims
.
size
();
++
i
){
assert
(
coordIdx
[
i
]
<
mDims
[
i
]
&&
"Coordinates dimensions does not fit the dimensions of the tensor"
);
...
...
This diff is collapsed.
Click to expand it.
python_binding/data/pybind_Tensor.cpp
+
26
−
11
View file @
53f2e8b8
...
...
@@ -48,7 +48,10 @@ void addCtor(py::class_<Tensor,
}
return
newTensor
;
}));
}))
.
def
(
"__setitem__"
,
(
void
(
Tensor
::*
)(
std
::
size_t
,
T
))
&
Tensor
::
set
)
.
def
(
"__setitem__"
,
(
void
(
Tensor
::*
)(
std
::
vector
<
std
::
size_t
>
,
T
))
&
Tensor
::
set
)
;
}
...
...
@@ -84,15 +87,27 @@ void init_Tensor(py::module& m){
return
b
.
size
();
})
.
def
(
"__getitem__"
,
[](
Tensor
&
b
,
size_t
idx
)
->
py
::
object
{
// TODO : Should return error if backend not compatible with get
if
(
idx
>=
b
.
size
())
throw
py
::
index_error
();
switch
(
b
.
dataType
()){
case
DataType
::
Float64
:
return
py
::
cast
(
static_cas
t
<
double
*
>
(
b
.
getImpl
()
->
rawPtr
())[
idx
]
);
return
py
::
cast
(
b
.
ge
t
<
double
>
(
idx
)
);
case
DataType
::
Float32
:
return
py
::
cast
(
static_cast
<
float
*>
(
b
.
getImpl
()
->
rawPtr
())[
idx
]
);
return
py
::
cast
(
b
.
get
<
float
>
(
idx
)
);
case
DataType
::
Int32
:
return
py
::
cast
(
static_cast
<
int
*>
(
b
.
getImpl
()
->
rawPtr
())[
idx
]);
return
py
::
cast
(
b
.
get
<
int
>
(
idx
));
default:
return
py
::
none
();
}
})
.
def
(
"__getitem__"
,
[](
Tensor
&
b
,
std
::
vector
<
size_t
>
coordIdx
)
->
py
::
object
{
if
(
b
.
getIdx
(
coordIdx
)
>=
b
.
size
())
throw
py
::
index_error
();
switch
(
b
.
dataType
()){
case
DataType
::
Float64
:
return
py
::
cast
(
b
.
get
<
double
>
(
coordIdx
));
case
DataType
::
Float32
:
return
py
::
cast
(
b
.
get
<
float
>
(
coordIdx
));
case
DataType
::
Int32
:
return
py
::
cast
(
b
.
get
<
int
>
(
coordIdx
));
default:
return
py
::
none
();
}
...
...
@@ -128,12 +143,12 @@ void init_Tensor(py::module& m){
}
return
py
::
buffer_info
(
tensorImpl
->
rawPtr
(),
/* Pointer to buffer */
tensorImpl
->
scalarSize
(),
/* Size of one scalar */
dataFormatDescriptor
,
/* Python struct-style format descriptor */
b
.
nbDims
(),
/* Number of dimensions */
dims
,
/* Buffer dimensions */
strides
/* Strides (in bytes) for each index */
tensorImpl
->
rawPtr
(),
/* Pointer to buffer */
tensorImpl
->
scalarSize
(),
/* Size of one scalar */
dataFormatDescriptor
,
/* Python struct-style format descriptor */
b
.
nbDims
(),
/* Number of dimensions */
dims
,
/* Buffer dimensions */
strides
/* Strides (in bytes) for each index */
);
});
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment