Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GNA] Additional PWL segments are added to avoid saturation #5399

Merged
merged 2 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 50 additions & 3 deletions inference-engine/src/gna_plugin/backend/make_pwl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,58 @@
#include <iostream>
#include <cmath>

#include <runtime/pwl.h>
#include <gna_slope_scale.h>
#include "runtime/pwl.h"
#include "gna_slope_scale.h"
#include "dnn_types.h"
#include "backend/gna_types.h"
#include "round_float_define.hpp"


// This function performes emulatation of HW saturation of PWL segments in SW
// by inserting additional segments when overflow would happen
static void insert_extra_pwl_segments(std::vector<gna_pwl_segment_t>& gna_pwl,
const int16_t y_min,
const int16_t y_max) {
std::map<size_t, gna_pwl_segment_t> extra_segments;
gna_pwl_segment_t extra_segment;
size_t gna_pwl_size = gna_pwl.size();

if (gna_pwl_size == 0)
return;

// We're adding a segment at the beginning if the first one doesn't cover min value
if ((gna_pwl[0].xBase & XBASEMASK) != INT32_MIN) {
extra_segment.xBase = INT32_MIN & XBASEMASK;
extra_segment.yBase = gna_pwl[0].yBase;
extra_segment.slope = 0;
extra_segments[0] = extra_segment;
}

// We're checking here if saturation could potentially happen at the trailing segments
if (gna_pwl[gna_pwl_size - 1].slope != 0) {
int16_t slope = gna_pwl[gna_pwl_size - 1].slope;
int32_t xBase = gna_pwl[gna_pwl_size - 1].xBase & XBASEMASK;
int16_t yBase = gna_pwl[gna_pwl_size - 1].yBase;
float scale = pow(2, ((gna_pwl[gna_pwl_size - 1].xBase & ~XBASEMASK) + 1) * 8);
float y_value = ((static_cast<float>(INT32_MAX) - xBase) * slope) / scale + yBase;

if (y_value > static_cast<float>(INT16_MAX) || y_value < static_cast<float>(INT16_MIN)) {
float x_value = ((static_cast<float>(y_max) - yBase) * scale) / slope + xBase;
extra_segment.xBase = FLOAT_TO_INT32(x_value) & XBASEMASK;
extra_segment.yBase = slope > 0 ? y_max : y_min;
extra_segment.slope = 0;
extra_segments[gna_pwl_size] = extra_segment;
}
}

if (!extra_segments.empty())
gnalog() << "Additional segment(s) added to protect against saturation\n";

for (auto i = extra_segments.rbegin(); i != extra_segments.rend(); i++) {
gna_pwl.insert(gna_pwl.begin() + i->first, i->second);
}
}

void make_gna_pwl(const DnnActivation fun,
const std::vector<pwl_t>& pwl,
const double l_bound,
Expand Down Expand Up @@ -583,6 +629,7 @@ void make_gna_pwl(const DnnActivation fun,
}
default:
gnalog() << "Unexpected function activation!\n";
std::cerr << "Unexpected function activation!\n";
THROW_GNA_EXCEPTION << "Unexpected function activation!" << fun;
}
insert_extra_pwl_segments(gna_pwl, y_min, y_max);
sirzabek marked this conversation as resolved.
Show resolved Hide resolved
}
4 changes: 1 addition & 3 deletions inference-engine/src/gna_plugin/backend/make_pwl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
#pragma once

#include <vector>
#include <runtime/pwl.h>
#include "backend/gna_types.h"

#include "runtime/pwl.h"

void make_gna_pwl(const DnnActivation fun,
const std::vector<pwl_t>& pwl,
Expand Down
4 changes: 1 addition & 3 deletions inference-engine/src/gna_plugin/runtime/pwl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
#include <limits>
#include <cstdint>
#include <algorithm>
#include "backend/gna_types.h"

#ifdef _NO_MKL_
#include <cmath>
#include <backend/make_pwl.hpp>
#include "backend/make_pwl.hpp"

#define SCOPY(num, in, inci, out, inco) for (int i_ = 0; i_ < *(num); i_++) *(out + i_ * *(inco)) = *(in + i_ * *(inci));
#define SSCAL(num, scale, inout, inco) for (int i_ = 0; i_ < *(num); i_++) *(inout + i_ * *(inco)) = *(scale) * *(inout + i_ * *(inco));
Expand All @@ -27,7 +26,6 @@

#include "pwl.h"
#include "gna_plugin_log.hpp"
#include "backend/dnn_types.h"
#include "gna_slope_scale.h"
#include "round_float_define.hpp"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ TEST_F(PWLAproximationTest, forReLUonRecursiveAlgoWithSegmentThresholdIsSuccess)
.propagate_forward()
.called_with()
.pwl_quantization_activation(DnnActivationType::kActRelu)
.pwl_quantization_segments_threshold(2);
.pwl_quantization_segments_threshold(4);
}

TEST_F(PWLAproximationTest, forLeakyReLUonRecursiveAlgoWithSegmentThresholdIsSuccess) {
Expand All @@ -157,7 +157,7 @@ TEST_F(PWLAproximationTest, forLeakyReLUonRecursiveAlgoWithSegmentThresholdIsSuc
.propagate_forward()
.called_with()
.pwl_quantization_activation(DnnActivationType::kActLeakyRelu)
.pwl_quantization_segments_threshold(2);
.pwl_quantization_segments_threshold(4);
}

TEST_F(PWLAproximationTest, DISABLED_forIdentityOnRecursiveAlgoWithSegmentThresholdIsSuccess) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,14 @@ class PWLMatcher : public ::testing::MatcherInterface<const gna_nnet_type_t*> {
}

switch (slopeChangedTimes) {
case 2 : return kActRelu; // also relu has y=0 segment while identity doenst have
case 3 : return kActIdentity;
case 3 :
if (comp.op.pwl.num_segments == 4) {
// ReLU has y=0 segment while identity doesn't have
// 2 segments are added: one at the begining and one at the end, due to saturation errata
return kActRelu;
} else {
return kActIdentity;
}
default:
// currently cannot determine between sigmoid or tanh etc
if (slopeChangedTimes > 3) {
Expand Down