Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[Op][Ref] Use functions ptr to decrease bin size in Interpolate ref (#29859)

Katarzyna Mitrus809a7ac65f5e
diff --git a/src/core/reference/include/openvino/reference/interpolate.hpp b/src/core/reference/include/openvino/reference/interpolate.hpp
index e188a39f020..dae5fc47975 100644
--- a/src/core/reference/include/openvino/reference/interpolate.hpp
+++ b/src/core/reference/include/openvino/reference/interpolate.hpp
@@ -29,7 +29,9 @@ using InterpolateMode = op::v4::Interpolate::InterpolateMode;
/// \param mode the calculation mode
///
/// \return The function to calculate the nearest pixel.
-std::function<int64_t(float, bool)> get_func(Nearest_mode mode);
+using NearestFuncPtr = int64_t (*)(float, bool);
+NearestFuncPtr get_func(Nearest_mode mode);
+
/// \brief Calculation of nearest pixel.
class GetNearestPixel final {
public:
@@ -54,9 +56,7 @@ public:
}
private:
- using Func = std::function<int64_t(float, bool)>;
-
- Func m_func;
+ NearestFuncPtr m_func;
};
/// \brief Gets the function to calculate the source coordinate.
@@ -64,7 +64,8 @@ private:
/// \param mode the calculation mode
///
/// \return The function to calculate the source coordinate.
-std::function<float(float, float, float, float)> get_func(Transform_mode mode);
+using TransformFuncPtr = float (*)(float, float, float, float);
+TransformFuncPtr get_func(Transform_mode mode);
/// \brief Calculation of the source coordinate using the resized coordinate
class GetOriginalCoordinate final {
@@ -95,9 +96,7 @@ public:
}
private:
- using Func = std::function<float(float, float, float, float)>;
-
- Func m_func;
+ TransformFuncPtr m_func;
};
/// \brief Helper class to implent non-template parts of the interpolation calculation.
diff --git a/src/core/reference/src/op/interpolate.cpp b/src/core/reference/src/op/interpolate.cpp
index 11d6d1b7387..ff2ae777dda 100644
--- a/src/core/reference/src/op/interpolate.cpp
+++ b/src/core/reference/src/op/interpolate.cpp
@@ -9,64 +9,80 @@
namespace ov {
namespace reference {
-std::function<int64_t(float, bool)> get_func(Nearest_mode mode) {
+static int64_t round_prefer_ceil(float x_original, bool) {
+ return static_cast<int64_t>(std::round(x_original));
+}
+
+static int64_t floor_func(float x_original, bool) {
+ return static_cast<int64_t>(std::floor(x_original));
+}
+
+static int64_t ceil_func(float x_original, bool) {
+ return static_cast<int64_t>(std::ceil(x_original));
+}
+
+static int64_t simple_func(float x_original, bool is_downsample) {
+ if (is_downsample) {
+ return static_cast<int64_t>(std::ceil(x_original));
+ } else {
+ return static_cast<int64_t>(x_original);
+ }
+}
+
+static int64_t default_nearest(float x_original, bool) {
+ if (x_original == static_cast<int64_t>(x_original) + 0.5f) {
+ return static_cast<int64_t>(std::floor(x_original));
+ }
+ return static_cast<int64_t>(std::round(x_original));
+}
+
+NearestFuncPtr get_func(Nearest_mode mode) {
switch (mode) {
case Nearest_mode::ROUND_PREFER_CEIL:
- return [](float x_original, bool) {
- return static_cast<int64_t>(std::round(x_original));
- };
+ return round_prefer_ceil;
case Nearest_mode::FLOOR:
- return [](float x_original, bool) {
- return static_cast<int64_t>(std::floor(x_original));
- };
+ return floor_func;
case Nearest_mode::CEIL:
- return [](float x_original, bool) {
- return static_cast<int64_t>(std::ceil(x_original));
- };
+ return ceil_func;
case Nearest_mode::SIMPLE:
- return [](float x_original, bool is_downsample) {
- if (is_downsample) {
- return static_cast<int64_t>(std::ceil(x_original));
- } else {
- return static_cast<int64_t>(x_original);
- }
- };
+ return simple_func;
default:
- return [](float x_original, bool) {
- if (x_original == static_cast<int64_t>(x_original) + 0.5f) {
- return static_cast<int64_t>(std::floor(x_original));
- }
- return static_cast<int64_t>(std::round(x_original));
- };
+ return default_nearest;
}
}
-std::function<float(float, float, float, float)> get_func(Transform_mode mode) {
+static float pytorch_half_pixel(float x_resized, float x_scale, float length_resized, float) {
+ return length_resized > 1 ? (x_resized + 0.5f) / x_scale - 0.5f : 0.0f;
+}
+
+static float asymmetric(float x_resized, float x_scale, float, float) {
+ return x_resized / x_scale;
+}
+
+static float tf_half_pixel_for_nn(float x_resized, float x_scale, float, float) {
+ return (x_resized + 0.5f) / x_scale;
+}
+
+static float align_corners(float x_resized, float, float length_resized, float length_original) {
+ return length_resized == 1 ? 0 : x_resized * (length_original - 1) / (length_resized - 1);
+}
+
+static float default_source_coord(float x_resized, float x_scale, float, float) {
+ return ((x_resized + 0.5f) / x_scale) - 0.5f;
+}
+
+TransformFuncPtr get_func(Transform_mode mode) {
switch (mode) {
case Transform_mode::PYTORCH_HALF_PIXEL:
- return [](float x_resized, float x_scale, float length_resized, float) {
- return length_resized > 1 ? (x_resized + 0.5f) / x_scale - 0.5f : 0.0f;
- };
- break;
+ return pytorch_half_pixel;
case Transform_mode::ASYMMETRIC:
- return [](float x_resized, float x_scale, float, float) {
- return x_resized / x_scale;
- };
- break;
+ return asymmetric;
case Transform_mode::TF_HALF_PIXEL_FOR_NN:
- return [](float x_resized, float x_scale, float, float) {
- return (x_resized + 0.5f) / x_scale;
- };
- break;
+ return tf_half_pixel_for_nn;
case Transform_mode::ALIGN_CORNERS:
- return [](float x_resized, float, float length_resized, float length_original) {
- return length_resized == 1 ? 0 : x_resized * (length_original - 1) / (length_resized - 1);
- };
- break;
+ return align_corners;
default:
- return [](float x_resized, float x_scale, float, float) {
- return ((x_resized + 0.5f) / x_scale) - 0.5f;
- };
+ return default_source_coord;
}
}