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
44740599
Commit
44740599
authored
1 year ago
by
Houssem ROUIS
Browse files
Options
Downloads
Patches
Plain Diff
add ReduceMean operator
parent
1409df0f
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
include/aidge/operator/ReduceMean.hpp
+194
-0
194 additions, 0 deletions
include/aidge/operator/ReduceMean.hpp
python_binding/operator/pybind_ReduceMean.cpp
+58
-0
58 additions, 0 deletions
python_binding/operator/pybind_ReduceMean.cpp
with
252 additions
and
0 deletions
include/aidge/operator/ReduceMean.hpp
0 → 100644
+
194
−
0
View file @
44740599
/********************************************************************************
* 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_REDUCEMEAN_H_
#define AIDGE_CORE_OPERATOR_REDUCEMEAN_H_
#include
<array>
#include
<cmath>
#include
<numeric>
#include
<vector>
#include
"aidge/data/Tensor.hpp"
#include
"aidge/graph/Node.hpp"
#include
"aidge/operator/Operator.hpp"
#include
"aidge/operator/Producer.hpp"
#include
"aidge/utils/StaticAttributes.hpp"
#include
"aidge/utils/Registrar.hpp"
#include
"aidge/utils/Types.h"
namespace
Aidge
{
enum
class
ReduceMeanAttr
{
Axes
,
KeepDims
};
template
<
DimIdx_t
DIM
>
class
ReduceMean_Op
:
public
Operator
,
public
Registrable
<
ReduceMean_Op
<
DIM
>
,
std
::
string
,
std
::
unique_ptr
<
OperatorImpl
>
(
const
ReduceMean_Op
<
DIM
>
&
)
>
,
public
StaticAttributes
<
ReduceMeanAttr
,
std
::
array
<
DimSize_t
,
DIM
>
,
DimSize_t
>
{
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
=
"ReduceMean"
;
ReduceMean_Op
()
=
delete
;
using
Attributes_
=
StaticAttributes
<
ReduceMeanAttr
,
std
::
array
<
DimSize_t
,
DIM
>
,
DimSize_t
>
;
template
<
ReduceMeanAttr
e
>
using
attr
=
typename
Attributes_
::
template
attr
<
e
>;
constexpr
ReduceMean_Op
(
const
std
::
array
<
DimSize_t
,
DIM
>
&
axes
,
DimSize_t
keep_dims
)
:
Operator
(
Type
),
Attributes_
(
attr
<
ReduceMeanAttr
::
Axes
>
(
axes
),
attr
<
ReduceMeanAttr
::
KeepDims
>
(
keep_dims
))
{
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.
*/
ReduceMean_Op
(
const
ReduceMean_Op
<
DIM
>&
op
)
:
Operator
(
Type
),
Attributes_
(
op
),
mOutput
(
std
::
make_shared
<
Tensor
>
(
*
op
.
mOutput
))
{
// cpy-ctor
setDatatype
(
op
.
mOutput
->
dataType
());
mImpl
=
op
.
mImpl
?
Registrar
<
ReduceMean_Op
<
DIM
>>::
create
(
mOutput
->
getImpl
()
->
backend
())(
*
this
)
:
nullptr
;
}
/**
* @brief Clone the operator using its copy-constructor.
* @see Operator::ReduceMean_Op
*/
std
::
shared_ptr
<
Operator
>
clone
()
const
override
{
return
std
::
make_shared
<
ReduceMean_Op
<
DIM
>>
(
*
this
);
}
void
associateInput
(
const
IOIndex_t
inputIdx
,
std
::
shared_ptr
<
Data
>
data
)
override
final
{
assert
(
inputIdx
==
0
&&
"ReduceMean operator supports only 1 input"
);
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
())
{
std
::
vector
<
DimSize_t
>
outDims
;
for
(
std
::
size_t
d
=
0
;
d
<
mInput
->
dims
().
size
();
++
d
)
{
bool
reducedDim
=
false
;
for
(
std
::
size_t
i
=
0
;
i
<
DIM
;
++
i
)
{
if
(
this
->
template
getAttr
<
ReduceMeanAttr
::
Axes
>()[
i
]
==
d
)
{
reducedDim
=
true
;
break
;
}
}
if
(
!
reducedDim
)
{
if
(
this
->
template
getAttr
<
ReduceMeanAttr
::
KeepDims
>())
outDims
.
push_back
(
1
);
}
else
outDims
.
push_back
(
mInput
->
dims
()[
d
]);
}
mOutput
->
resize
(
outDims
);
}
}
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
&&
"ReduceMean Operators supports 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
)
&&
"ReduceMean 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
&&
"ReduceMean Operators 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
&&
"ReduceMean Operator supports only 1 output"
);
(
void
)
outputIdx
;
// avoid unused warning
return
std
::
static_pointer_cast
<
Data
>
(
mOutput
);
}
void
setBackend
(
const
std
::
string
&
name
)
override
{
mImpl
=
Registrar
<
ReduceMean_Op
<
DIM
>>::
create
(
name
)(
*
this
);
mOutput
->
setBackend
(
name
);
// FIXME: temporary workaround
mInput
->
setBackend
(
name
);
}
void
setDatatype
(
const
DataType
&
datatype
)
override
{
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
;
}
static
const
std
::
vector
<
std
::
string
>
getInputsName
(){
return
{
"data_input"
};
}
static
const
std
::
vector
<
std
::
string
>
getOutputsName
(){
return
{
"data_output"
};
}
};
template
<
std
::
array
<
DimSize_t
,
1
>
::
size_type
DIM
>
inline
std
::
shared_ptr
<
Node
>
ReduceMean
(
const
std
::
array
<
DimSize_t
,
DIM
>
&
axes
,
DimSize_t
keep_dims
,
const
std
::
string
&
name
=
""
)
{
// FIXME: properly handle default w&b initialization in every cases
static_assert
(
DIM
<=
MaxDim
,
"Too many kernel dimensions required by ReduceMean, not supported"
);
return
std
::
make_shared
<
Node
>
(
std
::
make_shared
<
ReduceMean_Op
<
static_cast
<
DimIdx_t
>
(
DIM
)
>>
(
axes
,
keep_dims
),
name
);
}
// helper with C-style array instead of std::array for kernel_dims to allow automatic template DIM deduction
template
<
DimSize_t
DIM
>
inline
std
::
shared_ptr
<
Node
>
ReduceMean
(
DimSize_t
const
(
&
axes
)[
DIM
],
DimSize_t
keep_dims
=
1
,
const
std
::
string
&
name
=
""
)
{
static_assert
(
DIM
<=
MaxDim
,
"Too many kernel dimensions required by ReduceMean, not supported"
);
return
ReduceMean
(
to_array
(
axes
),
keep_dims
,
name
);
}
}
// namespace Aidge
namespace
{
template
<
>
const
char
*
const
EnumStrings
<
Aidge
::
ReduceMeanAttr
>::
data
[]
=
{
"Axes"
,
"KeepDims"
};
}
#endif
/* AIDGE_CORE_OPERATOR_REDUCEMEAN_H_ */
This diff is collapsed.
Click to expand it.
python_binding/operator/pybind_ReduceMean.cpp
0 → 100644
+
58
−
0
View file @
44740599
// /********************************************************************************
// * 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
// *
// ********************************************************************************/
// #include <pybind11/pybind11.h>
// #include <pybind11/stl.h>
// #include <iostream>
// #include <string>
// #include <vector>
// #include <array>
// #include "aidge/backend/OperatorImpl.hpp"
// #include "aidge/operator/ReduceMean.hpp"
// #include "aidge/operator/Operator.hpp"
// #include "aidge/utils/Types.h"
// namespace py = pybind11;
// namespace Aidge {
// template <DimIdx_t DIM> void declare_ReduceMeanOp(py::module &m) {
// py::class_<ReduceMean_Op<DIM>, std::shared_ptr<ReduceMean_Op<DIM>>, Operator, Attributes>(
// m, ("ReduceMeanOp" + std::to_string(DIM) + "D").c_str(),
// py::multiple_inheritance())
// .def(py::init<const std::array<DimSize_t, DIM> &, DimSize_t>(),
// py::arg("axes"),
// py::arg("keep_dims"))
// .def("get_inputs_name", &ReduceMean_Op<DIM>::getInputsName)
// .def("get_outputs_name", &ReduceMean_Op<DIM>::getOutputsName)
// ;
// m.def(("ReduceMean" + std::to_string(DIM) + "D").c_str(), [](const std::vector<DimSize_t>& axes,
// DimSize_t keepDims,
// const std::string& name) {
// AIDGE_ASSERT(axes.size() == DIM, "axes size [%ld] does not match DIM [%d]", axes.size(), DIM);
// return ReduceMean<DIM>(to_array<DIM>(axes.begin()), keepDims, name);
// }, py::arg("axes"),
// py::arg("keep_dims") = 1);
// }
// void init_ReduceMean(py::module &m) {
// declare_ReduceMeanOp<1>(m);
// declare_ReduceMeanOp<2>(m);
// declare_ReduceMeanOp<3>(m);
// // FIXME:
// // m.def("ReduceMean1D", static_cast<NodeAPI(*)(const char*, int, int, int const
// // (&)[1])>(&ReduceMean));
// }
// } // namespace Aidge
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