Depthwise channel pre-multiplication

Resolves: COMPMID-6337
Change-Id: Ie9097b3f56e8071426c621386a5988bd7f7e8ef2
Signed-off-by: Michael Tyler <michael.tyler@arm.com>
Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/9852
Tested-by: Arm Jenkins <bsgcomp@arm.com>
Reviewed-by: Viet-Hoa Do <viet-hoa.do@arm.com>
Benchmark: Arm Jenkins <bsgcomp@arm.com>
diff --git a/src/core/NEON/kernels/assembly/depthwise.hpp b/src/core/NEON/kernels/assembly/depthwise.hpp
index 8eb278c..dbd47cc 100644
--- a/src/core/NEON/kernels/assembly/depthwise.hpp
+++ b/src/core/NEON/kernels/assembly/depthwise.hpp
@@ -27,6 +27,7 @@
 #include "arm_gemm.hpp"
 #include "arm_gemm_local.hpp"
 #include "depthwise_common.hpp"
+#include "premultiply.hpp"
 
 namespace arm_conv
 {
@@ -38,8 +39,8 @@
     std::string     filter = "";
 
     DepthwiseConfig(DepthwiseMethod method)
-        : method(method){};
-    DepthwiseConfig(){};
+        : method(method) {};
+    DepthwiseConfig() {};
 };
 
 struct DepthwiseArgs
@@ -112,17 +113,64 @@
     }
 };
 
+template <typename TInput>
+struct Tile
+{
+    TInput *array;
+
+    unsigned int tile_rows     = 0;
+    unsigned int tile_cols     = 0;
+    unsigned int tile_channels = 0;
+
+    Tile(TInput *array, unsigned int tile_rows, unsigned int tile_cols, unsigned int tile_channels)
+        : array(array), tile_rows(tile_rows), tile_cols(tile_cols), tile_channels(tile_channels)
+    {
+    }
+
+    Tile()
+        : Tile(nullptr, 0, 0, 0)
+    {
+    }
+
+    void load_from(
+        const TInput      *input,
+        const unsigned int ld_row, const unsigned int ld_col,
+        const unsigned int n_rows, const unsigned int n_cols,
+        const int input_i, const int input_j,
+        const unsigned int channel_multiplier) const
+    {
+        const auto pad_top  = input_i < 0 ? -input_i : 0;
+        const auto pad_left = input_j < 0 ? -input_j : 0;
+
+        const auto padded_rows = std::min(n_rows - input_i, tile_rows) - pad_top;
+        const auto padded_cols = std::min(n_cols - input_j, tile_cols) - pad_left;
+
+        if(padded_rows < tile_rows || padded_cols < tile_cols)
+        {
+            memset(array, 0, tile_rows * tile_cols * tile_channels * sizeof(TInput));
+        }
+
+        do_premultiply<TInput>(
+            (TInput *)input + std::max(input_i, 0) * ld_row + std::max(input_j, 0) * ld_col,
+            ld_row, ld_col,
+            array + pad_top * tile_cols * tile_channels + pad_left * tile_channels,
+            tile_cols * tile_channels, tile_channels,
+            padded_rows, padded_cols, tile_channels / channel_multiplier,
+            channel_multiplier);
+    }
+};
+
 template <typename TInput, typename TWeight, typename TOutput>
 class DepthwiseCommon : public IDepthwiseCommon
 {
-    protected:
+protected:
     const DepthwiseArgs m_args; // Copy of arguments
     std::string         m_name{};
 
-    public:
+public:
     DepthwiseCommon(const DepthwiseArgs &args)
-        : m_args(args){};
-    DepthwiseCommon(DepthwiseCommon &)            = delete;
+        : m_args(args) {};
+    DepthwiseCommon(DepthwiseCommon &) = delete;
     DepthwiseCommon &operator=(DepthwiseCommon &) = delete;
 
     std::string name() const override
@@ -133,7 +181,7 @@
     void set_name(std::string name)
     {
         // Only allow the name to be set once
-        if (m_name.empty())
+        if(m_name.empty())
         {
             m_name = name;
         }
@@ -209,47 +257,47 @@
         // passed different input/output tensors. Dilation is handled at this
         // level; so we set the dilation in the arguments to zero.
         DepthwiseArgs args(this->m_args);
-        args.n_batches = batches;
-        args.input_rows = input_height;
-        args.input_cols = input_width;
+        args.n_batches      = batches;
+        args.input_rows     = input_height;
+        args.input_cols     = input_width;
         args.input_channels = channels;
-        args.output_rows = output_height;
-        args.output_cols = output_width;
-        args.padding = padding;
+        args.output_rows    = output_height;
+        args.output_cols    = output_width;
+        args.padding        = padding;
         args.dilation_rows = args.dilation_cols = 1;
 
-        auto ld_input_col_d = ld_input_col * m_args.dilation_cols;
-        auto ld_input_row_d = ld_input_row * m_args.dilation_rows;
+        auto ld_input_col_d  = ld_input_col * m_args.dilation_cols;
+        auto ld_input_row_d  = ld_input_row * m_args.dilation_rows;
         auto ld_output_col_d = ld_output_col * m_args.dilation_cols;
         auto ld_output_row_d = ld_output_row * m_args.dilation_rows;
 
-        for (size_t drow = 0; drow < m_args.dilation_rows; drow++)
+        for(size_t drow = 0; drow < m_args.dilation_rows; drow++)
         {
             size_t start_i;
             std::tie(args.output_rows, args.input_rows, start_i,
                      args.padding.top, args.padding.bottom) =
-                get_reduced_view_for_dilation(
-                        output_height, input_height, drow, m_args.dilation_rows,
-                        m_args.kernel_rows, m_args.stride_rows, padding.top);
+                         get_reduced_view_for_dilation(
+                             output_height, input_height, drow, m_args.dilation_rows,
+                             m_args.kernel_rows, m_args.stride_rows, padding.top);
 
-            auto input_row = static_cast<const TInput *>(input) + start_i * ld_input_row;
+            auto input_row  = static_cast<const TInput *>(input) + start_i * ld_input_row;
             auto output_row = static_cast<TOutput *>(output) + drow * ld_output_row;
 
-            if (args.output_rows)
+            if(args.output_rows)
             {
-                for (size_t dcol = 0; dcol < m_args.dilation_cols; dcol++)
+                for(size_t dcol = 0; dcol < m_args.dilation_cols; dcol++)
                 {
                     size_t start_j;
                     std::tie(args.output_cols, args.input_cols, start_j,
                              args.padding.left, args.padding.right) =
-                        get_reduced_view_for_dilation(
-                                output_width, input_width, dcol, m_args.dilation_cols,
-                                m_args.kernel_cols, m_args.stride_cols, padding.left);
+                                 get_reduced_view_for_dilation(
+                                     output_width, input_width, dcol, m_args.dilation_cols,
+                                     m_args.kernel_cols, m_args.stride_cols, padding.left);
 
-                    const TInput *input_col = input_row + start_j * ld_input_col;
-                    TOutput *output_col = output_row + dcol * ld_output_col;
+                    const TInput *input_col  = input_row + start_j * ld_input_col;
+                    TOutput      *output_col = output_row + dcol * ld_output_col;
 
-                    if (args.output_cols)
+                    if(args.output_cols)
                     {
                         this->execute_internal(
                             args, input_col, ld_input_col_d, ld_input_row_d, ld_input_batch, parameters,
@@ -261,7 +309,7 @@
         }
     }
 
-    protected:
+protected:
     virtual void execute_internal(
         const DepthwiseArgs &instance_args,
         const void          *input,
@@ -276,6 +324,11 @@
         void                *working_space,
         unsigned int         thread_id,
         unsigned int         n_threads) const = 0;
+
+    virtual bool uses_premultiply() const
+    {
+        return true;
+    }
 };
 
 template <typename TInput, typename TWeight = TInput, typename TOutput = TInput>
diff --git a/src/core/NEON/kernels/assembly/depthwise_common.hpp b/src/core/NEON/kernels/assembly/depthwise_common.hpp
index fea6326..a5db793 100644
--- a/src/core/NEON/kernels/assembly/depthwise_common.hpp
+++ b/src/core/NEON/kernels/assembly/depthwise_common.hpp
@@ -85,7 +85,7 @@
         size_t      ld_weight_row = 0) = 0;
 
     // Determine the amount of working space required
-    virtual size_t get_working_size(unsigned int n_threads, unsigned int n_input_channels) const = 0;
+    virtual size_t get_working_size(unsigned int n_threads) const = 0;
 
     // Execute the convolution over the specified area of memory.
     virtual void execute(
diff --git a/src/core/NEON/kernels/assembly/pool_common.hpp b/src/core/NEON/kernels/assembly/pool_common.hpp
index 599e18a..f1f70cf 100644
--- a/src/core/NEON/kernels/assembly/pool_common.hpp
+++ b/src/core/NEON/kernels/assembly/pool_common.hpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021-2022 Arm Limited.
+ * Copyright (c) 2021-2023 Arm Limited.
  *
  * SPDX-License-Identifier: MIT
  *
@@ -66,7 +66,6 @@
 
     // Determine the amount of working space required.
     virtual size_t get_working_size(unsigned int num_threads) const = 0;
-    virtual size_t get_working_size(unsigned int num_threads, unsigned int n_channels) const = 0;
 
     // Execute pooling over the specified area of memory.
     virtual void execute(
diff --git a/src/core/NEON/kernels/assembly/pooling.hpp b/src/core/NEON/kernels/assembly/pooling.hpp
index 1b47853..e8db35c 100644
--- a/src/core/NEON/kernels/assembly/pooling.hpp
+++ b/src/core/NEON/kernels/assembly/pooling.hpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021-2022 Arm Limited.
+ * Copyright (c) 2021-2023 Arm Limited.
  *
  * SPDX-License-Identifier: MIT
  *
@@ -122,11 +122,7 @@
     PoolingCommon(PoolingCommon &) = delete;
     PoolingCommon &operator=(PoolingCommon &) = delete;
 
-    size_t get_working_size(unsigned int, unsigned int) const override = 0;
-    size_t get_working_size(unsigned int n_threads) const override
-    {
-        return this->get_working_size(n_threads, m_args.n_channels);
-    }
+    size_t get_working_size(unsigned int) const override = 0;
 
     // Execute pooling over the specified area of memory.
     void execute(
diff --git a/src/core/NEON/kernels/assembly/premultiply.hpp b/src/core/NEON/kernels/assembly/premultiply.hpp
new file mode 100644
index 0000000..16f26de
--- /dev/null
+++ b/src/core/NEON/kernels/assembly/premultiply.hpp
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2023 Arm Limited.
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+void do_premultiply_float_6(const float       *in_ptr,
+                            const unsigned int ld_row,
+                            const unsigned int ld_col,
+                            float             *out_ptr,
+                            const unsigned int out_ld_row,
+                            const unsigned int out_ld_col,
+                            const unsigned int tile_rows,
+                            const unsigned int tile_cols,
+                            const unsigned     input_channels);
+
+template <typename T>
+void do_premultiply(const T           *in_ptr,
+                    const unsigned int ld_row,
+                    const unsigned int ld_col,
+                    T                 *out_ptr,
+                    const unsigned int out_ld_row,
+                    const unsigned int out_ld_col,
+                    const unsigned int tile_rows,
+                    const unsigned int tile_cols,
+                    const unsigned     input_channels,
+                    const unsigned int channel_multiplier)
+{
+    if(sizeof(T) == 4 && channel_multiplier == 6)
+    {
+        do_premultiply_float_6(
+            (const float *)in_ptr, ld_row, ld_col,
+            (float *)out_ptr, out_ld_row, out_ld_col,
+            tile_rows, tile_cols,
+            input_channels);
+    }
+    else
+    {
+        for(unsigned int i = 0; i < tile_rows; i++)
+        {
+            const T *ip2 = in_ptr + i * ld_row;
+            T       *op2 = out_ptr + i * out_ld_row;
+            for(unsigned int j = 0; j < tile_cols; j++)
+            {
+                const T *ip = ip2;
+                T       *op = op2;
+                for(unsigned int c = 0; c < input_channels; c++)
+                {
+                    T val = *ip;
+                    ip++;
+
+                    for(unsigned int r = 0; r < channel_multiplier; r++)
+                    {
+                        op[r] = val;
+                    }
+                    op += channel_multiplier;
+                }
+                ip2 += ld_col;
+                op2 += out_ld_col;
+            }
+        }
+    }
+}