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
Iterations
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
Eclipse Projects
aidge
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
1 merge request
!9
Fuse bn
Pipeline
#31780
failed
1 year ago
Stage: static_analysis
Stage: build
Stage: test
Stage: coverage
Changes
3
Pipelines
1
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:
...
@@ -27,6 +27,9 @@ public:
{
{
printf
(
"Cannot set raw pointer for backend %s
\n
"
,
mBackend
);
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)
virtual
std
::
size_t
scalarSize
()
const
=
0
;
// Size of one scalar (in bytes)
constexpr
const
char
*
backend
()
const
{
return
mBackend
;
}
constexpr
const
char
*
backend
()
const
{
return
mBackend
;
}
virtual
~
TensorImpl
()
=
default
;
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,
...
@@ -446,18 +446,33 @@ class Tensor : public Data,
*/
*/
bool
empty
()
const
{
return
mDims
.
empty
();
}
bool
empty
()
const
{
return
mDims
.
empty
();
}
template
<
typename
expectedType
,
std
::
array
<
std
::
size_t
,
1
>
::
size_type
DIM
>
template
<
typename
expectedType
>
constexpr
expectedType
&
get
(
std
::
array
<
std
::
size_t
,
DIM
>
idx
)
{
expectedType
&
get
(
std
::
size_t
idx
){
assert
(
DIM
==
mDims
.
size
());
// TODO : add assert expected Type compatible with datatype
assert
(
mImpl
);
// TODO : add assert idx < Size
std
::
size_t
unfoldedIdx
=
0
;
return
*
reinterpret_cast
<
expectedType
*>
(
mImpl
->
getRaw
(
idx
));
for
(
std
::
size_t
i
=
0
;
i
<
DIM
-
std
::
size_t
(
1
);
++
i
)
{
}
unfoldedIdx
=
(
unfoldedIdx
+
idx
[
i
])
*
mDims
[
i
+
1
];
}
template
<
typename
expectedType
>
unfoldedIdx
+=
idx
[
DIM
-
1
];
expectedType
&
get
(
std
::
vector
<
std
::
size_t
>
coordIdx
){
return
static_cast
<
expectedType
*>
(
mImpl
->
rawPtr
())[
unfoldedIdx
];
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
()
{
std
::
string
toString
()
{
if
(
dims
().
empty
())
{
return
"{}"
;
}
if
(
dims
().
empty
())
{
return
"{}"
;
}
std
::
string
res
;
std
::
string
res
;
...
@@ -565,10 +580,10 @@ class Tensor : public Data,
...
@@ -565,10 +580,10 @@ class Tensor : public Data,
* @param flatIdx 1D index of the value considering a flatten tensor.
* @param flatIdx 1D index of the value considering a flatten tensor.
* @return std::vector<DimSize_t>
* @return std::vector<DimSize_t>
*/
*/
std
::
vector
<
DimS
ize_t
>
getCoord
(
DimS
ize_t
flatIdx
){
std
::
vector
<
std
::
s
ize_t
>
getCoord
(
std
::
s
ize_t
flatIdx
){
std
::
vector
<
DimS
ize_t
>
coordIdx
=
{};
std
::
vector
<
std
::
s
ize_t
>
coordIdx
=
{};
DimS
ize_t
idx
=
flatIdx
;
std
::
s
ize_t
idx
=
flatIdx
;
for
(
DimS
ize_t
d
:
mDims
){
for
(
std
::
s
ize_t
d
:
mDims
){
coordIdx
.
push_back
(
idx
%
d
);
coordIdx
.
push_back
(
idx
%
d
);
idx
/=
d
;
idx
/=
d
;
}
}
...
@@ -581,9 +596,9 @@ class Tensor : public Data,
...
@@ -581,9 +596,9 @@ class Tensor : public Data,
* @param coordIdx Coordinate to an element in the tensor
* @param coordIdx Coordinate to an element in the tensor
* @return DimSize_t
* @return DimSize_t
*/
*/
DimS
ize_t
getIdx
(
std
::
vector
<
DimS
ize_t
>
coordIdx
){
std
::
s
ize_t
getIdx
(
std
::
vector
<
std
::
s
ize_t
>
coordIdx
){
DimS
ize_t
flatIdx
=
0
;
std
::
s
ize_t
flatIdx
=
0
;
DimS
ize_t
stride
=
1
;
std
::
s
ize_t
stride
=
1
;
assert
(
coordIdx
.
size
()
==
mDims
.
size
()
&&
"Coordinates does not match number of dimensions"
);
assert
(
coordIdx
.
size
()
==
mDims
.
size
()
&&
"Coordinates does not match number of dimensions"
);
for
(
std
::
size_t
i
=
0
;
i
<
mDims
.
size
();
++
i
){
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"
);
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,
...
@@ -48,7 +48,10 @@ void addCtor(py::class_<Tensor,
}
}
return
newTensor
;
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){
...
@@ -84,15 +87,27 @@ void init_Tensor(py::module& m){
return
b
.
size
();
return
b
.
size
();
})
})
.
def
(
"__getitem__"
,
[](
Tensor
&
b
,
size_t
idx
)
->
py
::
object
{
.
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
();
if
(
idx
>=
b
.
size
())
throw
py
::
index_error
();
switch
(
b
.
dataType
()){
switch
(
b
.
dataType
()){
case
DataType
::
Float64
:
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
:
case
DataType
::
Float32
:
return
py
::
cast
(
static_cast
<
float
*>
(
b
.
getImpl
()
->
rawPtr
())[
idx
]
);
return
py
::
cast
(
b
.
get
<
float
>
(
idx
)
);
case
DataType
::
Int32
:
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:
default:
return
py
::
none
();
return
py
::
none
();
}
}
...
@@ -128,12 +143,12 @@ void init_Tensor(py::module& m){
...
@@ -128,12 +143,12 @@ void init_Tensor(py::module& m){
}
}
return
py
::
buffer_info
(
return
py
::
buffer_info
(
tensorImpl
->
rawPtr
(),
/* Pointer to buffer */
tensorImpl
->
rawPtr
(),
/* Pointer to buffer */
tensorImpl
->
scalarSize
(),
/* Size of one scalar */
tensorImpl
->
scalarSize
(),
/* Size of one scalar */
dataFormatDescriptor
,
/* Python struct-style format descriptor */
dataFormatDescriptor
,
/* Python struct-style format descriptor */
b
.
nbDims
(),
/* Number of dimensions */
b
.
nbDims
(),
/* Number of dimensions */
dims
,
/* Buffer dimensions */
dims
,
/* Buffer dimensions */
strides
/* Strides (in bytes) for each index */
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