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
657c7302
Commit
657c7302
authored
1 year ago
by
Olivier BICHLER
Browse files
Options
Downloads
Patches
Plain Diff
Added Tensor::ref() methods
parent
5f918671
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
include/aidge/data/Tensor.hpp
+37
-0
37 additions, 0 deletions
include/aidge/data/Tensor.hpp
src/data/Tensor.cpp
+26
-0
26 additions, 0 deletions
src/data/Tensor.cpp
with
63 additions
and
0 deletions
include/aidge/data/Tensor.hpp
+
37
−
0
View file @
657c7302
...
...
@@ -781,6 +781,43 @@ class Tensor : public Data,
return
refCastFrom
(
fallback
,
targetReqs
.
dataType
(),
device
.
first
,
device
.
second
);
}
/**
* Return a reference to a Tensor on desired data type and backend/device:
* - itself, if already with the right characteristics;
* - the provided Tensor, overwritten with the right characteristics.
* NOTE: no data is copy-casted. If it was so in a previous refCastFrom() on
* the same fallback, it remains valid, otherwise, data is invalid.
* @param fallback A shared_ptr to Tensor ready to be overwritten if necessary.
* The shared_ptr does not need to be initialized. No new memory allocation
* will occur if fallback has already been allocated with the right
* type/size/device.
* @param dt The desired data type.
* @param backend The desired backend.
* @param device The desired device.
* @return Reference to either itself or to fallback.
*/
Tensor
&
ref
(
std
::
shared_ptr
<
Tensor
>&
fallback
,
const
Aidge
::
DataType
&
dt
,
const
std
::
string
&
backend
,
DeviceIdx_t
device
=
0
);
const
Tensor
&
ref
(
std
::
shared_ptr
<
Tensor
>&
fallback
,
const
Aidge
::
DataType
&
dt
,
const
std
::
string
&
backend
,
DeviceIdx_t
device
=
0
)
const
;
/**
* Return a reference to a Tensor with same characteristics
* (data type, backend/device) as targetReqs Tensor:
* - itself, if already with the right characteristics;
* - the provided Tensor, overwritten with the right characteristics.
* NOTE: no data is copy-casted. If it was so in a previous refCastFrom() on
* the same fallback, it remains valid, otherwise, data is invalid.
* @param fallback A shared_ptr to Tensor ready to be overwritten if necessary.
* The shared_ptr does not need to be initialized. No new memory allocation
* will occur if fallback has already been allocated with the right
* type/size/device.
* @param targetReqs Tensor with the desired target characteristics.
* @return Reference to either itself or to fallback.
*/
Tensor
&
ref
(
std
::
shared_ptr
<
Tensor
>&
fallback
,
const
Tensor
&
targetReqs
)
{
const
auto
&
device
=
targetReqs
.
getImpl
()
->
device
();
return
ref
(
fallback
,
targetReqs
.
dataType
(),
device
.
first
,
device
.
second
);
}
private
:
///\bug not protected against overflow
void
computeSize
()
{
...
...
This diff is collapsed.
Click to expand it.
src/data/Tensor.cpp
+
26
−
0
View file @
657c7302
...
...
@@ -216,3 +216,29 @@ const Aidge::Tensor& Aidge::Tensor::refFrom(std::shared_ptr<Tensor>& fallback, c
return
*
fallback
;
}
}
Aidge
::
Tensor
&
Aidge
::
Tensor
::
ref
(
std
::
shared_ptr
<
Tensor
>&
fallback
,
const
Aidge
::
DataType
&
dt
,
const
std
::
string
&
backend
,
DeviceIdx_t
device
)
{
// Scott Meyers' solution to avoid code duplication
return
const_cast
<
Tensor
&>
(
static_cast
<
const
Tensor
&>
(
*
this
).
ref
(
fallback
,
dt
,
backend
,
device
));
}
const
Aidge
::
Tensor
&
Aidge
::
Tensor
::
ref
(
std
::
shared_ptr
<
Tensor
>&
fallback
,
const
Aidge
::
DataType
&
dt
,
const
std
::
string
&
backend
,
DeviceIdx_t
device
)
const
{
AIDGE_ASSERT
(
getImpl
(),
"no backend was set for tensor, cannot ref() it"
);
if
(
dt
==
dataType
()
&&
std
::
make_pair
(
backend
,
device
)
==
getImpl
()
->
device
())
{
return
*
this
;
}
else
{
// Change fallback type, backend & device, without any data copy
if
(
!
fallback
)
{
fallback
=
std
::
make_shared
<
Tensor
>
(
dt
);
}
else
{
fallback
->
setDataType
(
dt
,
false
);
// don't keep previous data (no copy)
}
fallback
->
setBackend
(
backend
,
device
,
false
);
// don't keep previous data (no copy)
fallback
->
resize
(
dims
());
return
*
fallback
;
}
}
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