From 7e4b23953e885e58d655a7d9f35a1afcc38365e4 Mon Sep 17 00:00:00 2001 From: Gian Marco Iodice Date: Thu, 22 Feb 2018 16:17:20 +0000 Subject: 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 Tested-by: Jenkins --- tests/validation/reference/Winograd.cpp | 105 ++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) (limited to 'tests/validation/reference/Winograd.cpp') diff --git a/tests/validation/reference/Winograd.cpp b/tests/validation/reference/Winograd.cpp index 371bb6348e..3ed55fb9fc 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 @@ void winograd_input_transform3x3(const SimpleTensor &src, SimpleTensor &ds } } } + +template +void winograd_filter_transform3x3(const SimpleTensor &in, SimpleTensor &out) +{ + // Simple tensor for the 3x3 input tile + SimpleTensor input_tile{ TensorShape(3u, 3u), in.data_type(), 1 }; + + // Simple tensor for the transformation matrix + SimpleTensor trans_matrix{ TensorShape(3u, 4u), in.data_type(), 1 }; + + // Simple tensor for the transformation matrix transpose + SimpleTensor trans_matrix_transposed{ TensorShape(4u, 3u), in.data_type(), 1 }; + + // Simple tensor for the 4x3 temporary tile + SimpleTensor tmp_tile{ TensorShape(3u, 4u), in.data_type(), 1 }; + + // Simple tensor for the 4x4 output tile + SimpleTensor 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 @@ -130,7 +213,29 @@ SimpleTensor winograd_input_transform(const SimpleTensor &src, const Tenso return dst; } +template +SimpleTensor winograd_filter_transform(const SimpleTensor &in, const TensorShape &output_shape) +{ + ARM_COMPUTE_ERROR_ON_MSG(in.data_layout() != DataLayout::NCHW, "Only supported NCHW data format"); + + // Create reference + SimpleTensor 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 winograd_input_transform(const SimpleTensor &src, const TensorShape &dst_shape, const PadStrideInfo &conv_info, const Size2D &kernel_dims); +template SimpleTensor winograd_filter_transform(const SimpleTensor &in, const TensorShape &output_shape); } // namespace reference } // namespace validation } // namespace test -- cgit v1.2.1