shtinsa commented on code in PR #11341:
URL: https://github.com/apache/tvm/pull/11341#discussion_r902473958


##########
python/tvm/topi/x86/concat.py:
##########
@@ -0,0 +1,109 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"concatenate related operators"
+from typing import Optional
+import tvm
+from tvm import te
+import numpy as np
+from ..utils import get_const_int, const_vector
+
+
+def concatenate(data: tvm.te.Tensor, axis: Optional[int] = 0):
+    """Join a sequence of arrays along an existing axis. Optimized for CPU 
exeution.
+
+    Parameters
+    ----------
+    data : tuple of tvm.te.Tensor
+        The arrays to concatenate
+
+    axis : int, optional
+        The axis along which the arrays will be joined. Default is 0.
+
+    Returns
+    -------
+    ret : tvm.te.Tensor
+    """
+
+    def gen_ir_1d(data_bufs, in_outers_tensor, in_cumsum_tensor, out_buf):
+        """Custom conactenation execution."""
+        i_b = tvm.tir.ir_builder.create()
+        data_bufs1 = [i_b.buffer_ptr(data_buf) for data_buf in data_bufs]
+        out_buf = i_b.buffer_ptr(out_buf)
+        outers = i_b.buffer_ptr(in_outers_tensor)
+        cumsum = i_b.buffer_ptr(in_cumsum_tensor)
+        for i in range(len(data)):
+            with i_b.for_range(0, outers[i], name="j") as j:
+                out_buf[cumsum[i] + j] = data_bufs1[i][j]
+        return i_b.get()
+
+    def gen_ir(data_bufs, in_outers_tensor, in_cumsum_tensor, out_buf, inner, 
outer):
+        """Common case of conactenation execution."""
+        i_b = tvm.tir.ir_builder.create()
+        data_bufs1 = [i_b.buffer_ptr(data_buf) for data_buf in data_bufs]
+        out_buf = i_b.buffer_ptr(out_buf)
+        outers = i_b.buffer_ptr(in_outers_tensor)
+        cumsum = i_b.buffer_ptr(in_cumsum_tensor)
+        if inner > 1:
+            with i_b.for_range(0, inner, name="inn", kind="parallel") as inn:
+                pos = inn * outer
+                for i in range(len(data)):
+                    offset = inn * outers[i]
+                    with i_b.for_range(0, outers[i], name="j") as j:
+                        out_buf[pos + cumsum[i] + j] = data_bufs1[i][offset + 
j]
+        else:
+            for i in range(len(data)):
+                with i_b.for_range(0, outers[i], name="j", kind="parallel") as 
j:
+                    out_buf[cumsum[i] + j] = data_bufs1[i][j]
+        return i_b.get()
+
+    if axis < 0:
+        axis += len(data[0].shape)
+    concat_axis_sizes = [int(t.shape[axis]) for t in data]
+    join_size = int(np.sum(concat_axis_sizes))
+    in_outers = [int(np.prod(i.shape[axis:])) for i in data]
+    in_outers_cumsum = [0, *np.cumsum(in_outers, dtype="int64")[0:-1]]
+    dtype = data[0].dtype
+    out_shape = data[0].shape[:axis] + [join_size] + data[0].shape[axis + 1 :]
+    in_outers_tensor = const_vector(in_outers)
+    in_cumsum_tensor = const_vector(in_outers_cumsum, name="cumsum")

Review Comment:
   Hello @DzAvril I analyzed compiled so files and disasm code, and code block 
for one concatenation looks like this:
    ```
      v384 = v311 & 0xFFFFFFFFFFFFFFF0LL;
         _RSI = v304 + 4 * (v276 + v305);
         _RDX = 0LL;
         do
         {
           __asm
           {
             vmovups ymm0, ymmword ptr [rax+rdx*4-20h]
             vmovups ymm1, ymmword ptr [rax+rdx*4]
             vmovups ymmword ptr [rsi+rdx*4-20h], ymm0
             vmovups ymmword ptr [rsi+rdx*4], ymm1
           }
           _RDX += 16LL;
         }
         while ( v384 != _RDX );
         _RSI = v311 & 0xFFFFFFFFFFFFFFF0LL;
         if ( v311 != v384 )
           goto LABEL_209;
   LABEL_211:
         if ( v310 <= 0 )
           goto LABEL_219;
         v285 = v306[25];
         if ( (unsigned __int64)v310 < 0x10 )
         {
           _RSI = 0LL;
   LABEL_217:
           _RDX = v302 + 4 * v285;
           do
           {
             _RCX = v356;
             __asm
             {
               vmovss  xmm0, dword ptr [rcx+rsi*4]
               vmovss  dword ptr [rdx+rsi*4], xmm0
             }
             ++_RSI;
           }
           while ( v310 != _RSI );
           goto LABEL_219;
         }
   ```
   So formally I would add some unrolling to copy loop and remove tiles 
evaluation for data-blocks proportional to SIMD line. But it is a very small 
improvement which should be implemented on codegen side. Anyway I'm going to 
check the performance of your's proposals.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to