Skip to content
Snippets Groups Projects
Commit d49c9890 authored by Grégoire Kubler's avatar Grégoire Kubler
Browse files

Merge branch 'feat/operator_globalAveragePooling' of...

Merge branch 'feat/operator_globalAveragePooling' of gitlab.eclipse.org:hrouis/aidge_backend_cpu into fix/ci_fork_exec
parents f578b7ed 4d100339
No related branches found
No related tags found
1 merge request!47Draft: TEST_CI_FORK_DO_NOT_MERGE
......@@ -18,6 +18,7 @@ option(PYBIND "python binding" ON)
option(WERROR "Warning as error" OFF)
option(TEST "Enable tests" ON)
option(COVERAGE "Enable coverage" OFF)
option(ENABLE_ASAN "Enable ASan (adress sanitizer) for runtime analysis of memory use (over/underflow, memory leak, ...)" OFF)
##############################################
# Import utils CMakeLists
......@@ -34,7 +35,6 @@ find_package(aidge_core REQUIRED)
##############################################
# Create target and set properties
file(GLOB_RECURSE src_files "src/*.cpp")
file(GLOB_RECURSE inc_files "include/*.hpp")
......@@ -43,9 +43,23 @@ target_link_libraries(${module_name}
PUBLIC
_aidge_core # _ is added because we link the target not the project
)
#Set target properties
set_property(TARGET ${module_name} PROPERTY POSITION_INDEPENDENT_CODE ON)
if( ${ENABLE_ASAN} )
message("Building ${module_name} with ASAN.")
set(SANITIZE_FLAGS -fsanitize=address -fno-omit-frame-pointer)
target_link_libraries(${module_name}
PUBLIC
-fsanitize=address
)
target_compile_options(${module_name}
PRIVATE
${SANITIZE_FLAGS}
)
endif()
target_include_directories(${module_name}
PUBLIC
$<INSTALL_INTERFACE:include>
......
......@@ -23,6 +23,7 @@
#include "aidge/backend/cpu/operator/ErfImpl.hpp"
#include "aidge/backend/cpu/operator/FCImpl.hpp"
#include "aidge/backend/cpu/operator/GatherImpl.hpp"
#include "aidge/backend/cpu/operator/GlobalAveragePoolingImpl.hpp"
#include "aidge/backend/cpu/operator/LeakyReLUImpl.hpp"
#include "aidge/backend/cpu/operator/MatMulImpl.hpp"
#include "aidge/backend/cpu/operator/MemorizeImpl.hpp"
......
......@@ -12,10 +12,10 @@
#ifndef AIDGE_CPU_OPERATOR_FCIMPL_FORWARD_KERNEL_H_
#define AIDGE_CPU_OPERATOR_FCIMPL_FORWARD_KERNEL_H_
#include "aidge/utils/Registrar.hpp"
#include <algorithm>
#include "aidge/backend/cpu/operator/FCImpl.hpp"
#include "aidge/utils/Registrar.hpp"
namespace Aidge {
// template <class I, class W, class B, class O>
......
/********************************************************************************
* 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_CPU_OPERATOR_GLOBALAVERAGEPOOLINGIMPL_H_
#define AIDGE_CPU_OPERATOR_GLOBALAVERAGEPOOLINGIMPL_H_
#include <memory>
#include <vector>
#include "aidge/backend/OperatorImpl.hpp"
#include "aidge/operator/GlobalAveragePooling.hpp"
#include "aidge/utils/Registrar.hpp"
#include "aidge/utils/Types.h"
namespace Aidge
{
// class GlobalAveragePooling_Op;
class GlobalAveragePoolingImplForward_cpu
: public Registrable<GlobalAveragePoolingImplForward_cpu, std::tuple<DataType, DataType>, void(const std::vector<DimSize_t> &, const void *, void *)>
{
};
class GlobalAveragePoolingImplBackward_cpu
: public Registrable<GlobalAveragePoolingImplBackward_cpu, std::tuple<DataType, DataType>, void(const std::vector<DimSize_t> &, const void *, void *)>
{
};
// Then we declare the Impl class for the operator
class GlobalAveragePoolingImpl_cpu : public OperatorImpl
{
public:
GlobalAveragePoolingImpl_cpu(const GlobalAveragePooling_Op &op) : OperatorImpl(op) {}
static std::unique_ptr<GlobalAveragePoolingImpl_cpu> create(const GlobalAveragePooling_Op &op)
{
return std::make_unique<GlobalAveragePoolingImpl_cpu>(op);
}
void forward() override;
};
// Finally we create the Registrar for the operator implementation in which we specify the backend cpu
namespace
{
static Registrar<GlobalAveragePooling_Op> registrarGlobalAveragePoolingImpl_cpu("cpu", Aidge::GlobalAveragePoolingImpl_cpu::create);
}
} // namespace Aidge
#endif /* _AIDGE_CPU_OPERATOR_GLOBALAVERAGEPOOLINGIMPL_H_ */
\ No newline at end of file
/********************************************************************************
* 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_CPU_OPERATOR_GLOBALAVERAGEPOOLINGIMPL_FORWARD_KERNEL_H_
#define AIDGE_CPU_OPERATOR_GLOBALAVERAGEPOOLINGIMPL_FORWARD_KERNEL_H_
#include <functional>
#include <numeric>
#include <vector>
#include "aidge/data/Data.hpp"
#include "aidge/utils/Registrar.hpp"
#include "aidge/utils/Types.h"
#include <cmath>
#include <cstddef>
#include "aidge/backend/cpu/operator/GlobalAveragePoolingImpl.hpp"
namespace Aidge {
template <class I, class O>
void GlobalAveragePoolingImpl_cpu_forward_kernel(
const std::vector<DimSize_t> &dims, const void *input_, void *output_) {
// error checking
AIDGE_ASSERT(dims.size() >= 3,"GlobalAveragePool needs at least a 3 dimensions "
"input, number of input dim : {}",
dims.size());
// computation
const I *input = static_cast<const I *>(input_);
O *output = static_cast<O *>(output_);
DimSize_t nb_elems = std::accumulate(dims.begin(), dims.end(), std::size_t(1),
std::multiplies<std::size_t>());
const DimSize_t in_batch_nb_elems{nb_elems / dims[0]};
const DimSize_t in_channel_nb_elems{in_batch_nb_elems / dims[1]};
const DimSize_t out_batch_nb_elems{dims[1]};
// parse channel by channel and fill each output with the average of the
// values in the channel
for (DimSize_t batch = 0; batch < dims[0]; ++batch) {
for (DimSize_t channel = 0; channel < dims[1]; ++channel) {
const I *filter_start = std::next(
input, (batch * in_batch_nb_elems) + (channel * in_channel_nb_elems));
I sum = 0;
for (size_t i = 0; i < in_channel_nb_elems; ++i) {
sum += filter_start[i];
}
output[batch * out_batch_nb_elems + channel] =
sum / static_cast<I>(in_channel_nb_elems);
}
}
}
// Then we add the Registrar declaration for different input/output types
namespace {
static Registrar<GlobalAveragePoolingImplForward_cpu>
registrarGlobalAveragePoolingImplForward_cpu_Float32(
{DataType::Float32, DataType::Float32},
Aidge::GlobalAveragePoolingImpl_cpu_forward_kernel<float, float>);
static Registrar<GlobalAveragePoolingImplForward_cpu>
registrarGlobalAveragePoolingImplForward_cpu_Int32(
{DataType::Int32, DataType::Int32},
Aidge::GlobalAveragePoolingImpl_cpu_forward_kernel<int, int>);
static Registrar<GlobalAveragePoolingImplForward_cpu>
registrarGlobalAveragePoolingImplForward_cpu_Float64(
{DataType::Float64, DataType::Float64},
Aidge::GlobalAveragePoolingImpl_cpu_forward_kernel<double, double>);
} // namespace
} // namespace Aidge
#endif /* AIDGE_CPU_OPERATOR_GLOBALAVERAGEPOOLINGIMPL_FORWARD_KERNEL_H_ */
/********************************************************************************
* Copyright (c) 2024 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 <cassert>
#include <chrono> // std::chrono::milliseconds
#include <numeric> // std::accumulate
#include <thread> // std::this_thread::sleep_for
#include <vector>
#include "aidge/operator/GlobalAveragePooling.hpp"
#include "aidge/utils/Types.h"
#include "aidge/backend/cpu/operator/GlobalAveragePoolingImpl.hpp"
#include "aidge/backend/cpu/operator/GlobalAveragePoolingImpl_forward_kernels.hpp"
void Aidge::GlobalAveragePoolingImpl_cpu::forward()
{
// Check if input is provided
assert(std::static_pointer_cast<Tensor>(mOp.getRawInput(0)) && "missing input");
// Create the forward kernal with the wanted types
auto kernelFunc = Registrar<GlobalAveragePoolingImplForward_cpu>::create({std::static_pointer_cast<Tensor>(mOp.getRawInput(0))->dataType(),
std::static_pointer_cast<Tensor>(mOp.getRawOutput(0))->dataType()});
// Call kernel
kernelFunc(std::static_pointer_cast<Tensor>(mOp.getRawInput(0))->dims(),
std::static_pointer_cast<Tensor>(mOp.getRawInput(0))->getImpl()->rawPtr(),
std::static_pointer_cast<Tensor>(mOp.getRawOutput(0))->getImpl()->rawPtr());
}
\ No newline at end of file
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment