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>