COMPMID-2317: Implement CLROIAlignLayer

Change-Id: Iaa61b7a3528d3f82339d2ff8a2d77e77a1c68603
Signed-off-by: Michele Di Giorgio <michele.digiorgio@arm.com>
Reviewed-on: https://review.mlplatform.org/c/1821
Reviewed-by: Pablo Marquez <pablo.tello@arm.com>
Comments-Addressed: Arm Jenkins <bsgcomp@arm.com>
Tested-by: Arm Jenkins <bsgcomp@arm.com>
diff --git a/tests/validation/fixtures/ROIAlignLayerFixture.h b/tests/validation/fixtures/ROIAlignLayerFixture.h
index dfbb478..b9b85d3 100644
--- a/tests/validation/fixtures/ROIAlignLayerFixture.h
+++ b/tests/validation/fixtures/ROIAlignLayerFixture.h
@@ -26,7 +26,7 @@
 
 #include "arm_compute/core/TensorShape.h"
 #include "arm_compute/core/Types.h"
-#include "arm_compute/runtime/CL/functions/CLROIAlignLayer.h"
+#include "arm_compute/core/utils/misc/ShapeCalculator.h"
 #include "tests/AssetsLibrary.h"
 #include "tests/Globals.h"
 #include "tests/IAccessor.h"
@@ -42,14 +42,17 @@
 namespace validation
 {
 template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
-class ROIAlignLayerFixture : public framework::Fixture
+class ROIAlignLayerGenericFixture : public framework::Fixture
 {
 public:
+    using TRois = typename std::conditional<std::is_same<typename std::decay<T>::type, uint8_t>::value, uint16_t, T>::type;
+
     template <typename...>
-    void setup(TensorShape input_shape, const ROIPoolingLayerInfo pool_info, TensorShape rois_shape, DataType data_type, DataLayout data_layout)
+    void setup(TensorShape input_shape, const ROIPoolingLayerInfo pool_info, TensorShape rois_shape, DataType data_type, DataLayout data_layout, QuantizationInfo qinfo, QuantizationInfo output_qinfo)
     {
-        _target    = compute_target(input_shape, data_type, data_layout, pool_info, rois_shape);
-        _reference = compute_reference(input_shape, data_type, pool_info, rois_shape);
+        _rois_data_type = is_data_type_quantized_asymmetric(data_type) ? DataType::QASYMM16 : data_type;
+        _target         = compute_target(input_shape, data_type, data_layout, pool_info, rois_shape, qinfo, output_qinfo);
+        _reference      = compute_reference(input_shape, data_type, pool_info, rois_shape, qinfo, output_qinfo);
     }
 
 protected:
@@ -66,17 +69,17 @@
         const size_t num_rois       = rois_shape.y();
 
         std::mt19937 gen(library->seed());
-        T           *rois_ptr = static_cast<T *>(rois.data());
+        TRois       *rois_ptr = static_cast<TRois *>(rois.data());
 
         const float pool_width  = pool_info.pooled_width();
         const float pool_height = pool_info.pooled_height();
         const float roi_scale   = pool_info.spatial_scale();
 
         // Calculate distribution bounds
-        const auto scaled_width  = static_cast<T>((shape[get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH)] / roi_scale) / pool_width);
-        const auto scaled_height = static_cast<T>((shape[get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT)] / roi_scale) / pool_height);
-        const auto min_width     = static_cast<T>(pool_width / roi_scale);
-        const auto min_height    = static_cast<T>(pool_height / roi_scale);
+        const auto scaled_width  = static_cast<float>((shape[get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH)] / roi_scale) / pool_width);
+        const auto scaled_height = static_cast<float>((shape[get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT)] / roi_scale) / pool_height);
+        const auto min_width     = static_cast<float>(pool_width / roi_scale);
+        const auto min_height    = static_cast<float>(pool_height / roi_scale);
 
         // Create distributions
         std::uniform_int_distribution<int> dist_batch(0, shape[3] - 1);
@@ -93,11 +96,21 @@
             const auto x2        = x1 + dist_w(gen);
             const auto y2        = y1 + dist_h(gen);
 
-            rois_ptr[values_per_roi * pw]     = batch_idx;
-            rois_ptr[values_per_roi * pw + 1] = x1;
-            rois_ptr[values_per_roi * pw + 2] = y1;
-            rois_ptr[values_per_roi * pw + 3] = x2;
-            rois_ptr[values_per_roi * pw + 4] = y2;
+            rois_ptr[values_per_roi * pw] = batch_idx;
+            if(rois.data_type() == DataType::QASYMM16)
+            {
+                rois_ptr[values_per_roi * pw + 1] = quantize_qasymm16(static_cast<float>(x1), rois.quantization_info());
+                rois_ptr[values_per_roi * pw + 2] = quantize_qasymm16(static_cast<float>(y1), rois.quantization_info());
+                rois_ptr[values_per_roi * pw + 3] = quantize_qasymm16(static_cast<float>(x2), rois.quantization_info());
+                rois_ptr[values_per_roi * pw + 4] = quantize_qasymm16(static_cast<float>(y2), rois.quantization_info());
+            }
+            else
+            {
+                rois_ptr[values_per_roi * pw + 1] = static_cast<TRois>(x1);
+                rois_ptr[values_per_roi * pw + 2] = static_cast<TRois>(y1);
+                rois_ptr[values_per_roi * pw + 3] = static_cast<TRois>(x2);
+                rois_ptr[values_per_roi * pw + 4] = static_cast<TRois>(y2);
+            }
         }
     }
 
@@ -105,17 +118,23 @@
                               DataType                   data_type,
                               DataLayout                 data_layout,
                               const ROIPoolingLayerInfo &pool_info,
-                              const TensorShape          rois_shape)
+                              const TensorShape          rois_shape,
+                              const QuantizationInfo    &qinfo,
+                              const QuantizationInfo    &output_qinfo)
     {
         if(data_layout == DataLayout::NHWC)
         {
             permute(input_shape, PermutationVector(2U, 0U, 1U));
         }
 
+        const QuantizationInfo rois_qinfo = is_data_type_quantized(data_type) ? QuantizationInfo(0.125f, 0) : QuantizationInfo();
+
         // Create tensors
-        TensorType src         = create_tensor<TensorType>(input_shape, data_type, 1, QuantizationInfo(), data_layout);
-        TensorType rois_tensor = create_tensor<TensorType>(rois_shape, data_type);
-        TensorType dst;
+        TensorType src         = create_tensor<TensorType>(input_shape, data_type, 1, qinfo, data_layout);
+        TensorType rois_tensor = create_tensor<TensorType>(rois_shape, _rois_data_type, 1, rois_qinfo);
+
+        const TensorShape dst_shape = misc::shape_calculator::compute_roi_align_shape(*(src.info()), *(rois_tensor.info()), pool_info);
+        TensorType        dst       = create_tensor<TensorType>(dst_shape, data_type, 1, output_qinfo, data_layout);
 
         // Create and configure function
         FunctionType roi_align_layer;
@@ -147,23 +166,51 @@
     SimpleTensor<T> compute_reference(const TensorShape         &input_shape,
                                       DataType                   data_type,
                                       const ROIPoolingLayerInfo &pool_info,
-                                      const TensorShape          rois_shape)
+                                      const TensorShape          rois_shape,
+                                      const QuantizationInfo    &qinfo,
+                                      const QuantizationInfo    &output_qinfo)
     {
         // Create reference tensor
-        SimpleTensor<T> src{ input_shape, data_type };
-        SimpleTensor<T> rois_tensor{ rois_shape, data_type };
+        SimpleTensor<T>        src{ input_shape, data_type, 1, qinfo };
+        const QuantizationInfo rois_qinfo = is_data_type_quantized(data_type) ? QuantizationInfo(0.125f, 0) : QuantizationInfo();
+        SimpleTensor<TRois>    rois_tensor{ rois_shape, _rois_data_type, 1, rois_qinfo };
 
         // Fill reference tensor
         fill(src);
         generate_rois(rois_tensor, input_shape, pool_info, rois_shape);
 
-        return reference::roi_align_layer(src, rois_tensor, pool_info);
+        return reference::roi_align_layer(src, rois_tensor, pool_info, output_qinfo);
     }
 
     TensorType      _target{};
     SimpleTensor<T> _reference{};
+    DataType        _rois_data_type{};
 };
 
+template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
+class ROIAlignLayerFixture : public ROIAlignLayerGenericFixture<TensorType, AccessorType, FunctionType, T>
+{
+public:
+    template <typename...>
+    void setup(TensorShape input_shape, const ROIPoolingLayerInfo pool_info, TensorShape rois_shape, DataType data_type, DataLayout data_layout)
+    {
+        ROIAlignLayerGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, pool_info, rois_shape, data_type, data_layout,
+                                                                                      QuantizationInfo(), QuantizationInfo());
+    }
+};
+
+template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
+class ROIAlignLayerQuantizedFixture : public ROIAlignLayerGenericFixture<TensorType, AccessorType, FunctionType, T>
+{
+public:
+    template <typename...>
+    void setup(TensorShape input_shape, const ROIPoolingLayerInfo pool_info, TensorShape rois_shape, DataType data_type,
+               DataLayout data_layout, QuantizationInfo qinfo, QuantizationInfo output_qinfo)
+    {
+        ROIAlignLayerGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, pool_info, rois_shape,
+                                                                                      data_type, data_layout, qinfo, output_qinfo);
+    }
+};
 } // namespace validation
 } // namespace test
 } // namespace arm_compute