Skip to content
Snippets Groups Projects
Commit 64740c41 authored by Olivier BICHLER's avatar Olivier BICHLER
Browse files

Adaptation with core

parent 99af25ca
No related branches found
No related tags found
2 merge requests!1740.6.1,!169Simplify and optimize operators
Pipeline #72149 passed
...@@ -54,7 +54,6 @@ ...@@ -54,7 +54,6 @@
#include "aidge/backend/cpu/operator/ResizeImpl.hpp" #include "aidge/backend/cpu/operator/ResizeImpl.hpp"
#include "aidge/backend/cpu/operator/ReLUImpl.hpp" #include "aidge/backend/cpu/operator/ReLUImpl.hpp"
#include "aidge/backend/cpu/operator/RoundImpl.hpp" #include "aidge/backend/cpu/operator/RoundImpl.hpp"
#include "aidge/backend/cpu/operator/ScalingImpl.hpp"
#include "aidge/backend/cpu/operator/SigmoidImpl.hpp" #include "aidge/backend/cpu/operator/SigmoidImpl.hpp"
#include "aidge/backend/cpu/operator/SqrtImpl.hpp" #include "aidge/backend/cpu/operator/SqrtImpl.hpp"
#include "aidge/backend/cpu/operator/SliceImpl.hpp" #include "aidge/backend/cpu/operator/SliceImpl.hpp"
......
/********************************************************************************
* 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_ScalingIMPL_H__
#define __AIDGE_CPU_OPERATOR_ScalingIMPL_H__
#include "aidge/backend/cpu/operator/OperatorImpl.hpp"
#include "aidge/operator/Scaling.hpp"
#include "aidge/utils/Registrar.hpp"
#include "aidge/utils/Types.h"
#include "aidge/backend/cpu/data/GetCPUPtr.h"
#include <memory>
#include <vector>
#include <array>
namespace Aidge {
// Operator implementation entry point for the backend
using ScalingImpl_cpu = OperatorImpl_cpu<Scaling_Op,
void(const float,
const std::size_t,
const bool,
std::size_t,
const void*,
void*)>;
// Implementation entry point registration to Operator
REGISTRAR(Scaling_Op, "cpu", Aidge::ScalingImpl_cpu::create);
} // namespace Aidge
#endif /* __AIDGE_CPU_OPERATOR_ScalingIMPL_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_SCALINGIMPL_KERNELS_H_
#define AIDGE_CPU_OPERATOR_SCALINGIMPL_KERNELS_H_
#include <cmath>
#include <cstddef>
#include "aidge/utils/Registrar.hpp"
#include "aidge/backend/cpu/operator/ScalingImpl.hpp"
//TODO : improve propagate, n2d2 :
/*
template<typename T>
void N2D2::floatingPointScaling_propagate(const Tensor<T>& input, Tensor<T>& output,
std::size_t batchSize, std::size_t nbChannels,
std::size_t height, std::size_t width,
bool isClipped,
const std::vector<Float_T>& clippingFactorPerChannel,
const std::vector<Float_T>& scalingFactorPerChannel,
std::size_t quantizedNbBits, bool isOutputUnsigned)
{
std::size_t index = 0;
for (std::size_t batch = 0; batch < batchSize; batch++) {
for(std::size_t ch = 0; ch < nbChannels; ch++) {
for(std::size_t y = 0; y < height; y++) {
for(std::size_t x = 0; x < width; x++) {
T res = isClipped ? Clip(input(index), clippingFactorPerChannel[ch])
: input(index);
res = Scale(res, scalingFactorPerChannel[ch]);
if(quantizedNbBits > 0) {
res = saturate(std::round(res), quantizedNbBits, isOutputUnsigned);
}
output(index) = (T) res;
index++;
}
}
}
}
}
*/
namespace Aidge {
template <class O>
const O& clamp(const O& x, const O& min, const O& max)
{
return (x < min) ? min : (x > max) ? max : x;
}
template<class O>
O saturate(const O value, const std::size_t quantizedNbBits, const bool isOutputUnsigned) {
// TODO: no assertions in kernel
assert(quantizedNbBits > 0);
const O min = isOutputUnsigned ? 0 :
-(1ll << (quantizedNbBits - 1ll));
const O max = isOutputUnsigned ? (1ll << quantizedNbBits) - 1ll :
(1ll << (quantizedNbBits - 1ll)) - 1ll;
return clamp(value, min, max);
}
template <class I, class O>
void ScalingImpl_cpu_forward_kernel(const float scalingFactor,
const std::size_t quantizedNbBits,
const bool isOutputUnsigned,
std::size_t inputLength,
const void* input_,
void* output_) {
const I* input = static_cast<const I*>(input_);
O* output = static_cast<O*>(output_);
for (std::size_t i = 0; i < inputLength; ++i) {
output[i] = static_cast<O>(input[i] * static_cast<I>(scalingFactor));
if(quantizedNbBits > 0) {
output[i] = saturate(std::round(output[i]), quantizedNbBits, isOutputUnsigned);
}
}
}
// Kernels registration to implementation entry point
REGISTRAR(ScalingImpl_cpu,
{DataType::Float32},
{ProdConso::inPlaceModel, Aidge::ScalingImpl_cpu_forward_kernel<float, float>, nullptr});
REGISTRAR(ScalingImpl_cpu,
{DataType::Float64},
{ProdConso::inPlaceModel, Aidge::ScalingImpl_cpu_forward_kernel<double, double>, nullptr});
REGISTRAR(ScalingImpl_cpu,
{DataType::Int32},
{ProdConso::inPlaceModel, Aidge::ScalingImpl_cpu_forward_kernel<int32_t, int32_t>, nullptr});
} // namespace Aidge
#endif /* AIDGE_CPU_OPERATOR_SCALINGIMPL_KERNELS_H_ */
\ No newline at end of file
...@@ -28,13 +28,11 @@ template <> void ExpandImpl_cpu::forward() { ...@@ -28,13 +28,11 @@ template <> void ExpandImpl_cpu::forward() {
const Expand_Op &op_ = static_cast<const Expand_Op &>(mOp); const Expand_Op &op_ = static_cast<const Expand_Op &>(mOp);
// Check if input are provided // Check if input are provided
AIDGE_ASSERT(op_.getInput(0), AIDGE_ASSERT(op_.getInput(0),
"{}: missing input 0: {}", "{}: missing input 0",
Expand_Op::Type, Expand_Op::Type);
Expand_Op::getInputsName()[0]);
AIDGE_ASSERT(op_.getInput(1), AIDGE_ASSERT(op_.getInput(1),
"{}: missing input 1: {}", "{}: missing input 1",
Expand_Op::Type, Expand_Op::Type);
Expand_Op::getInputsName()[1]);
// Find the correct kernel type // Find the correct kernel type
const auto impl = const auto impl =
......
/********************************************************************************
* 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 <cassert>
#include <numeric> // std::accumulate
#include <functional> // std::multiplies
#include <vector>
#include "aidge/operator/Scaling.hpp"
#include "aidge/backend/cpu/operator/ScalingImpl.hpp"
#include "aidge/backend/cpu/operator/ScalingImpl_kernels.hpp"
#include "aidge/utils/Types.h"
#include "aidge/backend/cpu/data/GetCPUPtr.h"
template <>
void Aidge::ScalingImpl_cpu::forward() {
const auto& op_ = dynamic_cast<const Scaling_Op&>(mOp);
AIDGE_ASSERT(op_.getInput(0), "missing input #0 in Scaling Operator.");
// Find the correct kernel type
const auto impl = Registrar<ScalingImpl_cpu>::create(getBestMatch(getRequiredSpec()));
// Call kernel
impl.forward(op_.scalingFactor(),
op_.quantizedNbBits(),
op_.isOutputUnsigned(),
op_.getInput(0)->size(),
getCPUPtr(mOp.getRawInput(0)),
getCPUPtr(mOp.getRawOutput(0)));
}
template <>
void Aidge::ScalingImpl_cpu::backward() {
AIDGE_THROW_OR_ABORT(std::runtime_error, "Backward not yet implemented for Scaling_Op on backend cpu");
}
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