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
95ba2718
Commit
95ba2718
authored
1 year ago
by
Maxence Naud
Browse files
Options
Downloads
Patches
Plain Diff
[Add] Slice Operator
parent
a8e86249
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
include/aidge/operator/Slice.hpp
+185
-0
185 additions, 0 deletions
include/aidge/operator/Slice.hpp
with
185 additions
and
0 deletions
include/aidge/operator/Slice.hpp
0 → 100644
+
185
−
0
View file @
95ba2718
/********************************************************************************
* Copyright (c) 2023 CEA-List
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
********************************************************************************/
#ifndef AIDGE_CORE_OPERATOR_SLICE_H_
#define AIDGE_CORE_OPERATOR_SLICE_H_
#include
<memory>
#include
<vector>
#include
"aidge/backend/OperatorImpl.hpp"
#include
"aidge/data/Data.hpp"
#include
"aidge/data/Tensor.hpp"
#include
"aidge/graph/Node.hpp"
#include
"aidge/operator/Operator.hpp"
#include
"aidge/utils/Registrar.hpp"
#include
"aidge/utils/StaticAttributes.hpp"
#include
"aidge/utils/Types.h"
namespace
Aidge
{
enum
class
SliceAttr
{
Beginning
,
SliceDims
};
template
<
DimIdx_t
DIM
>
class
Slice_Op
:
public
Operator
,
public
Registrable
<
Slice_Op
<
DIM
>
,
std
::
string
,
std
::
unique_ptr
<
OperatorImpl
>
(
const
Slice_Op
<
DIM
>
&
)
>
,
public
StaticAttributes
<
SliceAttr
,
std
::
size_t
,
std
::
array
<
DimSize_t
,
DIM
>>
{
public:
// FIXME: change accessibility
std
::
shared_ptr
<
Tensor
>
mInput
=
std
::
make_shared
<
Tensor
>
();
const
std
::
shared_ptr
<
Tensor
>
mOutput
=
std
::
make_shared
<
Tensor
>
();
public:
static
constexpr
const
char
*
Type
=
"Slice"
;
Slice_Op
()
=
delete
;
using
Attributes_
=
StaticAttributes
<
SliceAttr
,
std
::
size_t
,
std
::
array
<
DimSize_t
,
DIM
>>
;
template
<
SliceAttr
e
>
using
attr
=
typename
Attributes_
::
template
attr
<
e
>;
Slice_Op
(
std
::
size_t
beginningPos
,
std
::
array
<
DimSize_t
,
DIM
>
sliceDims
)
:
Operator
(
Type
),
Attributes_
(
attr
<
SliceAttr
::
Beginning
>
(
beginningPos
),
attr
<
SliceAttr
::
SliceDims
>
(
sliceDims
))
{
setDatatype
(
DataType
::
Float32
);
}
/**
* @brief Copy-constructor. Copy the operator attributes and its output tensor(s), but not its
* input tensors (the new operator has no input associated).
* @param op Operator to copy.
*/
Slice_Op
(
const
Slice_Op
&
op
)
:
Operator
(
Type
),
Attributes_
(
op
),
mOutput
(
std
::
make_shared
<
Tensor
>
(
*
op
.
mOutput
))
{
// cpy-ctor
setDatatype
(
op
.
mOutput
->
dataType
());
mImpl
=
op
.
mImpl
?
Registrar
<
Slice_Op
<
DIM
>>::
create
(
mOutput
->
getImpl
()
->
backend
())(
*
this
)
:
nullptr
;
}
public
:
/**
* @brief Clone the operator using its copy-constructor.
* @see Operator::Slice_Op
*/
std
::
shared_ptr
<
Operator
>
clone
()
const
override
{
return
std
::
make_shared
<
Slice_Op
>
(
*
this
);
}
void
associateInput
(
const
IOIndex_t
inputIdx
,
std
::
shared_ptr
<
Data
>
data
)
override
final
{
assert
(
inputIdx
==
0
&&
"operator supports only 1 input"
);
(
void
)
inputIdx
;
// avoid unused warning
assert
(
strcmp
(
data
->
type
(),
Tensor
::
Type
)
==
0
&&
"input data must be of Tensor type"
);
mInput
=
std
::
dynamic_pointer_cast
<
Tensor
>
(
data
);
}
void
computeOutputDims
()
override
final
{
if
(
!
mInput
->
empty
())
{
// Check input dimensions is compatible with slice dimensions
if
(
mInput
->
nbDims
()
!=
DIM
)
{
printf
(
"Error: input and slice dimensions are not the same size.
\n
"
);
exit
(
-
1
);
}
std
::
array
<
DimSize_t
,
DIM
>
outputDims
;
// Check that the sliced Tensor is actually part of the input Tensor
// For a 5*5 tensor ('x') and a 3*3 slice kernel ('o'):
// xxxxx xxxxx
// xxxxx xxxxx
// xxooo --> ok xxxoo --> out of bound
// xxooo xxxoo
// xxooo xxxoo
std
::
vector
<
std
::
size_t
>
beginningCoords
=
mInput
->
getCoord
(
this
->
template
getAttr
<
SliceAttr
::
Beginning
>());
for
(
std
::
size_t
i
=
0
;
i
<
DIM
;
++
i
)
{
if
(
beginningCoords
[
i
]
+
this
->
template
getAttr
<
SliceAttr
::
SliceDims
>()[
i
]
>=
mInput
->
dims
()[
i
])
{
printf
(
"ROI of Slice operator out of bounds"
);
exit
(
-
1
);
}
else
{
outputDims
[
i
]
=
this
->
template
getAttr
<
SliceAttr
::
SliceDims
>()[
i
];
}
}
mOutput
->
resize
(
outputDims
);
}
}
bool
outputDimsForwarded
()
const
override
final
{
return
!
(
mOutput
->
empty
());
}
inline
Tensor
&
input
(
const
IOIndex_t
/*inputIdx*/
)
const
override
final
{
return
*
(
mInput
.
get
());
}
inline
Tensor
&
output
(
const
IOIndex_t
/*outputIdx*/
)
const
override
final
{
return
*
(
mOutput
.
get
());
}
inline
std
::
shared_ptr
<
Tensor
>
getInput
(
const
IOIndex_t
inputIdx
)
const
override
final
{
assert
((
inputIdx
==
0
)
&&
"Slice Operator has only 1 input"
);
(
void
)
inputIdx
;
// avoid unused warning
return
mInput
;
}
inline
std
::
shared_ptr
<
Tensor
>
getOutput
(
const
IOIndex_t
outputIdx
)
const
override
final
{
assert
((
outputIdx
==
0
)
&&
"Slice Operator has only 1 output"
);
(
void
)
outputIdx
;
// avoid unused warning
return
mOutput
;
}
std
::
shared_ptr
<
Data
>
getRawInput
(
const
IOIndex_t
inputIdx
)
const
override
final
{
assert
(
inputIdx
==
0
&&
"operator supports only 1 input"
);
(
void
)
inputIdx
;
// avoid unused warning
return
std
::
static_pointer_cast
<
Data
>
(
mInput
);
}
std
::
shared_ptr
<
Data
>
getRawOutput
(
const
IOIndex_t
outputIdx
)
const
override
final
{
assert
(
outputIdx
==
0
&&
"operator supports only 1 output"
);
(
void
)
outputIdx
;
// avoid unused warning
return
mOutput
;
}
void
setBackend
(
const
std
::
string
&
name
)
{
mImpl
=
Registrar
<
Slice_Op
>::
create
(
name
)(
*
this
);
mOutput
->
setBackend
(
name
);
// FIXME: temporary workaround
mInput
->
setBackend
(
name
);
}
void
setDatatype
(
const
DataType
&
datatype
)
{
mOutput
->
setDatatype
(
datatype
);
// FIXME: temporary workaround
mInput
->
setDatatype
(
datatype
);
}
inline
IOIndex_t
nbInputs
()
const
noexcept
override
final
{
return
1
;
}
inline
IOIndex_t
nbDataInputs
()
const
noexcept
override
final
{
return
1
;
}
inline
IOIndex_t
nbOutputs
()
const
noexcept
override
final
{
return
1
;
}
};
template
<
DimIdx_t
DIM
>
inline
std
::
shared_ptr
<
Node
>
Slice
(
std
::
size_t
beginningPos
,
std
::
array
<
DimSize_t
,
DIM
>
sliceDims
,
const
std
::
string
&
name
=
""
)
{
// FIXME: properly handle default w&b initialization in every cases
return
std
::
make_shared
<
Node
>
(
std
::
make_shared
<
Slice_Op
<
DIM
>>
(
beginningPos
,
sliceDims
),
name
);
}
template
<
DimIdx_t
DIM
>
inline
std
::
shared_ptr
<
Node
>
Slice
(
std
::
size_t
beginningPos
,
DimSize_t
const
(
&
sliceDims
)[
DIM
],
const
std
::
string
&
name
=
""
)
{
return
Slice
(
beginningPos
,
to_array
(
sliceDims
),
name
);
}
}
// namespace Aidge
namespace
{
template
<
>
const
char
*
const
EnumStrings
<
Aidge
::
SliceAttr
>::
data
[]
=
{
"Beginning"
,
"SliceDims"
};
}
#endif
/* AIDGE_CORE_OPERATOR_RELU_H_ */
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