COMPMID-935 - Implementing Convolution with Winograd on OpenCL (part 2)

Implemented Winograd Filter Transform 3x3 on OpenCL

Change-Id: I8f2b2dd938c5c000ef7ce392a37fb7b8b4202a4e
Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/122708
Reviewed-by: Georgios Pinitas <georgios.pinitas@arm.com>
Tested-by: Jenkins <bsgcomp@arm.com>
diff --git a/tests/validation/reference/Winograd.cpp b/tests/validation/reference/Winograd.cpp
index 371bb63..3ed55fb 100644
--- a/tests/validation/reference/Winograd.cpp
+++ b/tests/validation/reference/Winograd.cpp
@@ -26,6 +26,8 @@
 #include "tests/validation/Helpers.h"
 #include "tests/validation/reference/Utils.h"
 
+#include "arm_compute/core/Types.h"
+
 namespace arm_compute
 {
 namespace test
@@ -108,6 +110,87 @@
         }
     }
 }
+
+template <typename T>
+void winograd_filter_transform3x3(const SimpleTensor<T> &in, SimpleTensor<T> &out)
+{
+    // Simple tensor for the 3x3 input tile
+    SimpleTensor<T> input_tile{ TensorShape(3u, 3u), in.data_type(), 1 };
+
+    // Simple tensor for the transformation matrix
+    SimpleTensor<T> trans_matrix{ TensorShape(3u, 4u), in.data_type(), 1 };
+
+    // Simple tensor for the transformation matrix transpose
+    SimpleTensor<T> trans_matrix_transposed{ TensorShape(4u, 3u), in.data_type(), 1 };
+
+    // Simple tensor for the 4x3 temporary tile
+    SimpleTensor<T> tmp_tile{ TensorShape(3u, 4u), in.data_type(), 1 };
+
+    // Simple tensor for the 4x4 output tile
+    SimpleTensor<T> output_tile{ TensorShape(4u, 4u), in.data_type(), 1 };
+
+    // Initialize transformation matrix
+    // 1   | 0   | 0
+    // 0.5 | 0.5 | 0.5
+    // 0.5 |-0.5 | 0.5
+    // 0   | 0   | 1
+    trans_matrix[0 + 0 * 3] = 1.0f;
+    trans_matrix[1 + 0 * 3] = 0.0f;
+    trans_matrix[2 + 0 * 3] = 0.0f;
+    trans_matrix[0 + 1 * 3] = 0.5f;
+    trans_matrix[1 + 1 * 3] = 0.5f;
+    trans_matrix[2 + 1 * 3] = 0.5f;
+    trans_matrix[0 + 2 * 3] = 0.5f;
+    trans_matrix[1 + 2 * 3] = -0.5f;
+    trans_matrix[2 + 2 * 3] = 0.5f;
+    trans_matrix[0 + 3 * 3] = 0.0f;
+    trans_matrix[1 + 3 * 3] = 0.0f;
+    trans_matrix[2 + 3 * 3] = 1.0f;
+
+    // Transpose the transformation matrix
+    transpose_matrix(trans_matrix, trans_matrix_transposed);
+
+    const int num_channels = in.shape()[2];
+    const int num_filters  = in.shape()[3];
+    const int num_batches  = in.shape().total_size() / (9 * num_channels * num_filters);
+
+    for(int n = 0; n < num_batches; ++n)
+    {
+        for(int w = 0; w < num_filters; ++w)
+        {
+            for(int z = 0; z < num_channels; ++z)
+            {
+                // Load the 3x3 tile from the input tensor
+                get_tile(in, input_tile, Coordinates(0, 0, z, w, n));
+
+                // First transformation
+                matrix_multiply(trans_matrix, input_tile, tmp_tile);
+
+                // Second transformation
+                matrix_multiply(tmp_tile, trans_matrix_transposed, output_tile);
+
+                // Store the 4x4 output tile across the 16 channels
+                const int output_offset                              = w + z * num_filters;
+                out[output_offset + 0 * num_filters * num_channels]  = output_tile[0 + 0 * 4];
+                out[output_offset + 1 * num_filters * num_channels]  = output_tile[1 + 0 * 4];
+                out[output_offset + 2 * num_filters * num_channels]  = output_tile[2 + 0 * 4];
+                out[output_offset + 3 * num_filters * num_channels]  = output_tile[3 + 0 * 4];
+                out[output_offset + 4 * num_filters * num_channels]  = output_tile[0 + 1 * 4];
+                out[output_offset + 5 * num_filters * num_channels]  = output_tile[1 + 1 * 4];
+                out[output_offset + 6 * num_filters * num_channels]  = output_tile[2 + 1 * 4];
+                out[output_offset + 7 * num_filters * num_channels]  = output_tile[3 + 1 * 4];
+                out[output_offset + 8 * num_filters * num_channels]  = output_tile[0 + 2 * 4];
+                out[output_offset + 9 * num_filters * num_channels]  = output_tile[1 + 2 * 4];
+                out[output_offset + 10 * num_filters * num_channels] = output_tile[2 + 2 * 4];
+                out[output_offset + 11 * num_filters * num_channels] = output_tile[3 + 2 * 4];
+                out[output_offset + 12 * num_filters * num_channels] = output_tile[0 + 3 * 4];
+                out[output_offset + 13 * num_filters * num_channels] = output_tile[1 + 3 * 4];
+                out[output_offset + 14 * num_filters * num_channels] = output_tile[2 + 3 * 4];
+                out[output_offset + 15 * num_filters * num_channels] = output_tile[3 + 3 * 4];
+            }
+        }
+    }
+}
 } // namespace
 
 template <typename T>
@@ -130,7 +213,29 @@
     return dst;
 }
 
+template <typename T>
+SimpleTensor<T> winograd_filter_transform(const SimpleTensor<T> &in, const TensorShape &output_shape)
+{
+    ARM_COMPUTE_ERROR_ON_MSG(in.data_layout() != DataLayout::NCHW, "Only supported NCHW data format");
+
+    // Create reference
+    SimpleTensor<T> out{ output_shape, in.data_type(), 1 };
+
+    switch(in.shape()[0])
+    {
+        case 3:
+            winograd_filter_transform3x3(in, out);
+            break;
+        default:
+            ARM_COMPUTE_ERROR("Only supported 3x3 kernel");
+            break;
+    }
+
+    return out;
+}
+
 template SimpleTensor<float> winograd_input_transform(const SimpleTensor<float> &src, const TensorShape &dst_shape, const PadStrideInfo &conv_info, const Size2D &kernel_dims);
+template SimpleTensor<float> winograd_filter_transform(const SimpleTensor<float> &in, const TensorShape &output_shape);
 } // namespace reference
 } // namespace validation
 } // namespace test