Skip to content
Snippets Groups Projects

Add Scaling operator

Merged Cyril Moineau requested to merge Scaling into master
1 unresolved thread
1 file
+ 59
0
Compare changes
  • Side-by-side
  • Inline
@@ -16,7 +16,60 @@
@@ -16,7 +16,60 @@
#include "aidge/backend/cpu/operator/ScalingImpl.hpp"
#include "aidge/backend/cpu/operator/ScalingImpl.hpp"
 
//TODO : improve propagate, n2d2 :
    • Author Maintainer

      This todo is not very clear ... furthermore what is the comment code ou added bellow ?

      We have to make the kernels as the n2d2 version in the future. you can see what's missing from n2d2 example. Actually only Clip is missing now.

      Edited by Inna Kucher
Please register or sign in to reply
 
/*
 
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 {
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(O value, std::size_t quantizedNbBits, bool isOutputUnsigned) {
 
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>
template <class I, class O>
void ScalingImpl_cpu_forward_kernel(const Scaling_Op::Attrs& attrs,
void ScalingImpl_cpu_forward_kernel(const Scaling_Op::Attrs& attrs,
std::size_t inputLenght,
std::size_t inputLenght,
@@ -26,9 +79,15 @@ void ScalingImpl_cpu_forward_kernel(const Scaling_Op::Attrs& attrs,
@@ -26,9 +79,15 @@ void ScalingImpl_cpu_forward_kernel(const Scaling_Op::Attrs& attrs,
const I* input = static_cast<const I*>(input_);
const I* input = static_cast<const I*>(input_);
O* output = static_cast<O*>(output_);
O* output = static_cast<O*>(output_);
const I& scalingFactor = static_cast<const I&>(std::get<0>(attrs));
const I& scalingFactor = static_cast<const I&>(std::get<0>(attrs));
 
std::size_t quantizedNbBits = static_cast<std::size_t>(std::get<1>(attrs));
 
bool isOutputUnsigned = static_cast<bool>(std::get<2>(attrs));
for (std::size_t i = 0; i < inputLenght; ++i) {
for (std::size_t i = 0; i < inputLenght; ++i) {
output[i] = input[i] * scalingFactor;
output[i] = input[i] * scalingFactor;
 
 
if(quantizedNbBits > 0) {
 
output[i] = saturate(std::round(output[i]), quantizedNbBits, isOutputUnsigned);
 
}
}
}
}
}
Loading