You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2022/02/22 10:11:59 UTC

[GitHub] [tvm] NicolaLancellotti opened a new pull request #10344: [microNPU] Integrate rolling buffers in Arm(R) Ethos(TM)-U

NicolaLancellotti opened a new pull request #10344:
URL: https://github.com/apache/tvm/pull/10344


   This pr adds support for rolling buffers in Arm(R) Ethos(TM)-U.


-- 
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: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] manupa-arm commented on a change in pull request #10344: [microNPU] Integrate rolling buffers in Arm(R) Ethos(TM)-U

Posted by GitBox <gi...@apache.org>.
manupa-arm commented on a change in pull request #10344:
URL: https://github.com/apache/tvm/pull/10344#discussion_r814741231



##########
File path: python/tvm/relay/backend/contrib/ethosu/codegen.py
##########
@@ -31,6 +31,12 @@
 from tvm.relay.backend.contrib.ethosu.op import op_attrs
 from tvm.relay.backend.contrib.ethosu import op
 
+# We are currently using copy_constants scheduler In the long run,
+# this should be a single intelligent and a composite scheduler
+# that can perform scheduling based on user inputs such as
+# scratch memory size.
+CASCADER = copy_constants()

Review comment:
       nit : let us use something like SCHEDULER -- because cascading is a way of scheduling and copy_constants is not a cascader scheduler.




-- 
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: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] manupa-arm commented on a change in pull request #10344: [microNPU] Integrate rolling buffers in Arm(R) Ethos(TM)-U

Posted by GitBox <gi...@apache.org>.
manupa-arm commented on a change in pull request #10344:
URL: https://github.com/apache/tvm/pull/10344#discussion_r829857975



##########
File path: python/tvm/relay/backend/contrib/ethosu/tir/dma.py
##########
@@ -197,18 +197,72 @@ def get_read_params(stmt):
 
     base_address = [get_base_address(index) for index in inner.value.indices]
     data_type = inner.buffer.data.type_annotation.element_type.dtype
+
+    def check_rolling_buffer():

Review comment:
       Lets make this a function of dma.py and make it accept stmt which is a read loop nest.

##########
File path: python/tvm/relay/backend/contrib/ethosu/tir/dma.py
##########
@@ -197,18 +197,72 @@ def get_read_params(stmt):
 
     base_address = [get_base_address(index) for index in inner.value.indices]
     data_type = inner.buffer.data.type_annotation.element_type.dtype
+
+    def check_rolling_buffer():
+        rolling_buffer = True
+        floor_mod = None
+
+        def _get_rolling_var(stmt):
+            nonlocal rolling_buffer, floor_mod
+
+            if isinstance(stmt, tvm.tir.FloorMod):
+                if floor_mod is not None:
+                    rolling_buffer = False
+                elif (
+                    isinstance(stmt.b, tvm.tir.expr.IntImm)
+                    and isinstance(stmt.a, tvm.tir.expr.Add)
+                    and isinstance(stmt.a.a, tvm.tir.expr.Var)
+                    and isinstance(stmt.a.b, tvm.tir.expr.IntImm)
+                ):
+                    floor_mod = stmt
+                else:
+                    rolling_buffer = False
+            elif isinstance(stmt, tvm.tir.FloorDiv):
+                rolling_buffer = False
+
+        tvm.tir.stmt_functor.post_order_visit(inner.value, _get_rolling_var)
+
+        if rolling_buffer and floor_mod is not None:
+            rolling_var = floor_mod.a.a
+            tile_length = floor_mod.b - floor_mod.a.b
+            return rolling_var, tile_length

Review comment:
       I think it is clear if we return a NamedTuple that contains tile_height_0, tile_height_1, tile_width_0, tile_address_0, tile_address_1, tile_address_2.
   
   If you agree, then we might need to change the function to be something like CreateTiles(...)
   
   Then, I think we should tests for CreateTiles function with a set of test cases that include different Stmt types.
   

##########
File path: python/tvm/relay/backend/contrib/ethosu/tir/dma.py
##########
@@ -197,18 +197,72 @@ def get_read_params(stmt):
 
     base_address = [get_base_address(index) for index in inner.value.indices]
     data_type = inner.buffer.data.type_annotation.element_type.dtype
+
+    def check_rolling_buffer():
+        rolling_buffer = True
+        floor_mod = None
+
+        def _get_rolling_var(stmt):
+            nonlocal rolling_buffer, floor_mod
+
+            if isinstance(stmt, tvm.tir.FloorMod):
+                if floor_mod is not None:
+                    rolling_buffer = False
+                elif (
+                    isinstance(stmt.b, tvm.tir.expr.IntImm)
+                    and isinstance(stmt.a, tvm.tir.expr.Add)
+                    and isinstance(stmt.a.a, tvm.tir.expr.Var)
+                    and isinstance(stmt.a.b, tvm.tir.expr.IntImm)

Review comment:
       Out of curiosity, would it be possible to use this ? 
   
   https://github.com/apache/tvm/blob/b01e3fc4d21bba898a5ea17d526013c52ea720eb/python/tvm/ir/base.py#L160-L209




-- 
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: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] NicolaLancellotti commented on a change in pull request #10344: [microNPU] Integrate rolling buffers in Arm(R) Ethos(TM)-U

Posted by GitBox <gi...@apache.org>.
NicolaLancellotti commented on a change in pull request #10344:
URL: https://github.com/apache/tvm/pull/10344#discussion_r835125952



##########
File path: python/tvm/relay/backend/contrib/ethosu/tir/dma.py
##########
@@ -197,18 +197,72 @@ def get_read_params(stmt):
 
     base_address = [get_base_address(index) for index in inner.value.indices]
     data_type = inner.buffer.data.type_annotation.element_type.dtype
+
+    def check_rolling_buffer():
+        rolling_buffer = True
+        floor_mod = None
+
+        def _get_rolling_var(stmt):
+            nonlocal rolling_buffer, floor_mod
+
+            if isinstance(stmt, tvm.tir.FloorMod):
+                if floor_mod is not None:
+                    rolling_buffer = False
+                elif (
+                    isinstance(stmt.b, tvm.tir.expr.IntImm)
+                    and isinstance(stmt.a, tvm.tir.expr.Add)
+                    and isinstance(stmt.a.a, tvm.tir.expr.Var)
+                    and isinstance(stmt.a.b, tvm.tir.expr.IntImm)
+                ):
+                    floor_mod = stmt
+                else:
+                    rolling_buffer = False
+            elif isinstance(stmt, tvm.tir.FloorDiv):
+                rolling_buffer = False
+
+        tvm.tir.stmt_functor.post_order_visit(inner.value, _get_rolling_var)
+
+        if rolling_buffer and floor_mod is not None:
+            rolling_var = floor_mod.a.a
+            tile_length = floor_mod.b - floor_mod.a.b
+            return rolling_var, tile_length

Review comment:
       Done




-- 
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: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] NicolaLancellotti commented on a change in pull request #10344: [microNPU] Integrate rolling buffers in Arm(R) Ethos(TM)-U

Posted by GitBox <gi...@apache.org>.
NicolaLancellotti commented on a change in pull request #10344:
URL: https://github.com/apache/tvm/pull/10344#discussion_r835129150



##########
File path: python/tvm/relay/backend/contrib/ethosu/tir/dma.py
##########
@@ -197,18 +197,72 @@ def get_read_params(stmt):
 
     base_address = [get_base_address(index) for index in inner.value.indices]
     data_type = inner.buffer.data.type_annotation.element_type.dtype
+
+    def check_rolling_buffer():
+        rolling_buffer = True
+        floor_mod = None
+
+        def _get_rolling_var(stmt):
+            nonlocal rolling_buffer, floor_mod
+
+            if isinstance(stmt, tvm.tir.FloorMod):
+                if floor_mod is not None:
+                    rolling_buffer = False
+                elif (
+                    isinstance(stmt.b, tvm.tir.expr.IntImm)
+                    and isinstance(stmt.a, tvm.tir.expr.Add)
+                    and isinstance(stmt.a.a, tvm.tir.expr.Var)
+                    and isinstance(stmt.a.b, tvm.tir.expr.IntImm)

Review comment:
       I tried but it seems that the constants must always be the same in the lhs and rhs to ensure that the two expressions are structurally the same. Unfortunately in this case the constants are not always the same, so I could not use this approach.




-- 
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: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] NicolaLancellotti commented on a change in pull request #10344: [microNPU] Integrate rolling buffers in Arm(R) Ethos(TM)-U

Posted by GitBox <gi...@apache.org>.
NicolaLancellotti commented on a change in pull request #10344:
URL: https://github.com/apache/tvm/pull/10344#discussion_r816961600



##########
File path: python/tvm/relay/backend/contrib/ethosu/tir/dma.py
##########
@@ -287,31 +321,69 @@ def get_ifm_params(pointer, producers):
         The serializable padding.
 
     """
-    pad = producers[pointer]
+    pad = producers_consumers.get_producer(pointer, stmt)
     serial_padding, input_pointer, _ = get_pad_params(pad)
-    upscale = producers[input_pointer]
+    upscale = producers_consumers.get_producer(input_pointer, pad)
     input_pointer, _ = get_upscale_params(upscale)
-    convert_to_nhwc = producers[input_pointer]
+    convert_to_nhwc = producers_consumers.get_producer(input_pointer, upscale)
     in_channels, input_pointer, _ = get_convert_to_nhwc_params(convert_to_nhwc)
-    read = producers[input_pointer]
+    read = producers_consumers.get_producer(input_pointer, convert_to_nhwc)
     serial_ifm, _, _ = get_read_params(read)
     serial_ifm.channels = in_channels
+
+    floor_mod_stmt = None
+    for_stmt = None
+
+    def _get_buffer_var(stmt):
+        nonlocal for_stmt
+        nonlocal floor_mod_stmt
+        if isinstance(stmt, tvm.tir.For):
+            for_stmt = stmt
+        if isinstance(stmt, tvm.tir.FloorMod):
+            floor_mod_stmt = stmt
+
+    tvm.tir.stmt_functor.post_order_visit(stmt, _get_buffer_var)
+
+    if floor_mod_stmt is not None:
+        layout = get_op_attrs(read)[0]["layout"]
+        channels = serial_ifm.channels
+        if for_stmt.body.loop_var == floor_mod_stmt.a.a.a:

Review comment:
       With rolling buffers the `floor_mod_stmt` should be always like that:
   `floormod(((HEIGHT_OR_WIDTH_INDEX + KERNEL_INDEX) + BUFFER_OFFSET), BUFFER_SIZE)`
   I cannot think of any other case.




-- 
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: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] NicolaLancellotti commented on pull request #10344: [microNPU] Integrate rolling buffers in Arm(R) Ethos(TM)-U

Posted by GitBox <gi...@apache.org>.
NicolaLancellotti commented on pull request #10344:
URL: https://github.com/apache/tvm/pull/10344#issuecomment-1047634288


   @mbaret @manupa-arm @ekalda @lhutton1 @jacobbohlin 


-- 
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: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] NicolaLancellotti commented on a change in pull request #10344: [microNPU] Integrate rolling buffers in Arm(R) Ethos(TM)-U

Posted by GitBox <gi...@apache.org>.
NicolaLancellotti commented on a change in pull request #10344:
URL: https://github.com/apache/tvm/pull/10344#discussion_r829413144



##########
File path: python/tvm/relay/backend/contrib/ethosu/tir_to_cs_translator.py
##########
@@ -398,11 +398,11 @@ def assign_addresses(buffer_info, npu_ops, scratch_region_map):
 
     def replace_npu_fm_with_address(npu_fm):
         assert isinstance(npu_fm.tiles.addresses[0], tvm.tir.Load)
-        # We currently does not support tiles
-        # Change this when tiles are needed
-        # (i.e. when using rolling buffers)
-        assert npu_fm.tiles.addresses[1:] == [0, 0, 0]
-        npu_fm.tiles.addresses[1:] = [0, 0, 0]
+        for i in range(1, 4):
+            address = npu_fm.tiles.addresses[i]
+            if isinstance(address, tvm.tir.expr.Load):
+                address = address.index
+            npu_fm.tiles.addresses[i] = int(address)

Review comment:
       You are right, that logic (the integer conversion) worked before the USMP was enabled, I forgot to remove it during a rebase.
   Now we have the following logic:
   ```
   npu_fm.tiles.addresses[0] = address + int(index)
   npu_fm.tiles.addresses[1] = address if isinstance(npu_fm.tiles.addresses[1], tvm.tir.BufferLoad) else 0
   npu_fm.tiles.addresses[2] = address if isinstance(npu_fm.tiles.addresses[2], tvm.tir.BufferLoad) else 0
   npu_fm.tiles.addresses[3] = 0
   ```
   
   Where the index of tile1 and tile2, when used, are always 0.




-- 
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: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] ekalda commented on a change in pull request #10344: [microNPU] Integrate rolling buffers in Arm(R) Ethos(TM)-U

Posted by GitBox <gi...@apache.org>.
ekalda commented on a change in pull request #10344:
URL: https://github.com/apache/tvm/pull/10344#discussion_r817652817



##########
File path: python/tvm/relay/backend/contrib/ethosu/tir_to_cs_translator.py
##########
@@ -398,11 +398,11 @@ def assign_addresses(buffer_info, npu_ops, scratch_region_map):
 
     def replace_npu_fm_with_address(npu_fm):
         assert isinstance(npu_fm.tiles.addresses[0], tvm.tir.Load)
-        # We currently does not support tiles
-        # Change this when tiles are needed
-        # (i.e. when using rolling buffers)
-        assert npu_fm.tiles.addresses[1:] == [0, 0, 0]
-        npu_fm.tiles.addresses[1:] = [0, 0, 0]
+        for i in range(1, 4):
+            address = npu_fm.tiles.addresses[i]
+            if isinstance(address, tvm.tir.expr.Load):
+                address = address.index
+            npu_fm.tiles.addresses[i] = int(address)

Review comment:
       Ok cool... I'm sill a bit confused what is going on in that change, first it converts `IntImm` into `int` in that block using the addresses already is `npu_fm.tiles.addresses`, but then in the end of that function it overwrites the middle two addresses to with `address`. What's the reason for that overwriting in the end of the function? Maybe a comment would help there (can be done in a follow up though). 

##########
File path: python/tvm/relay/backend/contrib/ethosu/tir/transform.py
##########
@@ -21,19 +21,16 @@
 from .utils import get_base_address, get_op_attrs
 
 
-def get_copy_params(stmt, producers, consumers):
+def get_copy_params(stmt, producers_consumers):

Review comment:
       Ah yes, that's right...

##########
File path: python/tvm/relay/backend/contrib/ethosu/tir/dma.py
##########
@@ -287,31 +321,69 @@ def get_ifm_params(pointer, producers):
         The serializable padding.
 
     """
-    pad = producers[pointer]
+    pad = producers_consumers.get_producer(pointer, stmt)
     serial_padding, input_pointer, _ = get_pad_params(pad)
-    upscale = producers[input_pointer]
+    upscale = producers_consumers.get_producer(input_pointer, pad)
     input_pointer, _ = get_upscale_params(upscale)
-    convert_to_nhwc = producers[input_pointer]
+    convert_to_nhwc = producers_consumers.get_producer(input_pointer, upscale)
     in_channels, input_pointer, _ = get_convert_to_nhwc_params(convert_to_nhwc)
-    read = producers[input_pointer]
+    read = producers_consumers.get_producer(input_pointer, convert_to_nhwc)
     serial_ifm, _, _ = get_read_params(read)
     serial_ifm.channels = in_channels
+
+    floor_mod_stmt = None
+    for_stmt = None
+
+    def _get_buffer_var(stmt):
+        nonlocal for_stmt
+        nonlocal floor_mod_stmt
+        if isinstance(stmt, tvm.tir.For):
+            for_stmt = stmt
+        if isinstance(stmt, tvm.tir.FloorMod):
+            floor_mod_stmt = stmt
+
+    tvm.tir.stmt_functor.post_order_visit(stmt, _get_buffer_var)
+
+    if floor_mod_stmt is not None:
+        layout = get_op_attrs(read)[0]["layout"]
+        channels = serial_ifm.channels
+        if for_stmt.body.loop_var == floor_mod_stmt.a.a.a:

Review comment:
       Ok cool, that makes sense! :) 




-- 
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: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] manupa-arm commented on a change in pull request #10344: [microNPU] Integrate rolling buffers in Arm(R) Ethos(TM)-U

Posted by GitBox <gi...@apache.org>.
manupa-arm commented on a change in pull request #10344:
URL: https://github.com/apache/tvm/pull/10344#discussion_r829857975



##########
File path: python/tvm/relay/backend/contrib/ethosu/tir/dma.py
##########
@@ -197,18 +197,72 @@ def get_read_params(stmt):
 
     base_address = [get_base_address(index) for index in inner.value.indices]
     data_type = inner.buffer.data.type_annotation.element_type.dtype
+
+    def check_rolling_buffer():

Review comment:
       Lets make this a function of dma.py and make it accept stmt which is a read loop nest.

##########
File path: python/tvm/relay/backend/contrib/ethosu/tir/dma.py
##########
@@ -197,18 +197,72 @@ def get_read_params(stmt):
 
     base_address = [get_base_address(index) for index in inner.value.indices]
     data_type = inner.buffer.data.type_annotation.element_type.dtype
+
+    def check_rolling_buffer():
+        rolling_buffer = True
+        floor_mod = None
+
+        def _get_rolling_var(stmt):
+            nonlocal rolling_buffer, floor_mod
+
+            if isinstance(stmt, tvm.tir.FloorMod):
+                if floor_mod is not None:
+                    rolling_buffer = False
+                elif (
+                    isinstance(stmt.b, tvm.tir.expr.IntImm)
+                    and isinstance(stmt.a, tvm.tir.expr.Add)
+                    and isinstance(stmt.a.a, tvm.tir.expr.Var)
+                    and isinstance(stmt.a.b, tvm.tir.expr.IntImm)
+                ):
+                    floor_mod = stmt
+                else:
+                    rolling_buffer = False
+            elif isinstance(stmt, tvm.tir.FloorDiv):
+                rolling_buffer = False
+
+        tvm.tir.stmt_functor.post_order_visit(inner.value, _get_rolling_var)
+
+        if rolling_buffer and floor_mod is not None:
+            rolling_var = floor_mod.a.a
+            tile_length = floor_mod.b - floor_mod.a.b
+            return rolling_var, tile_length

Review comment:
       I think it is clear if we return a NamedTuple that contains tile_height_0, tile_height_1, tile_width_0, tile_address_0, tile_address_1, tile_address_2.
   
   If you agree, then we might need to change the function to be something like CreateTiles(...)
   
   Then, I think we should tests for CreateTiles function with a set of test cases that include different Stmt types.
   

##########
File path: python/tvm/relay/backend/contrib/ethosu/tir/dma.py
##########
@@ -197,18 +197,72 @@ def get_read_params(stmt):
 
     base_address = [get_base_address(index) for index in inner.value.indices]
     data_type = inner.buffer.data.type_annotation.element_type.dtype
+
+    def check_rolling_buffer():
+        rolling_buffer = True
+        floor_mod = None
+
+        def _get_rolling_var(stmt):
+            nonlocal rolling_buffer, floor_mod
+
+            if isinstance(stmt, tvm.tir.FloorMod):
+                if floor_mod is not None:
+                    rolling_buffer = False
+                elif (
+                    isinstance(stmt.b, tvm.tir.expr.IntImm)
+                    and isinstance(stmt.a, tvm.tir.expr.Add)
+                    and isinstance(stmt.a.a, tvm.tir.expr.Var)
+                    and isinstance(stmt.a.b, tvm.tir.expr.IntImm)

Review comment:
       Out of curiosity, would it be possible to use this ? 
   
   https://github.com/apache/tvm/blob/b01e3fc4d21bba898a5ea17d526013c52ea720eb/python/tvm/ir/base.py#L160-L209




-- 
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: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] NicolaLancellotti commented on pull request #10344: [microNPU] Integrate rolling buffers in Arm(R) Ethos(TM)-U

Posted by GitBox <gi...@apache.org>.
NicolaLancellotti commented on pull request #10344:
URL: https://github.com/apache/tvm/pull/10344#issuecomment-1054361755


   The CI problems have been fixed. Other reviews are appreciated.


-- 
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: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] NicolaLancellotti commented on a change in pull request #10344: [microNPU] Integrate rolling buffers in Arm(R) Ethos(TM)-U

Posted by GitBox <gi...@apache.org>.
NicolaLancellotti commented on a change in pull request #10344:
URL: https://github.com/apache/tvm/pull/10344#discussion_r816961090



##########
File path: python/tvm/relay/backend/contrib/ethosu/tir/passes.py
##########
@@ -64,13 +65,16 @@ def ReplaceOperators():
         "ethosu_identity": get_identity_params,
         "ethosu_unary_elementwise": get_unary_elementwise_params,
     }
-    pointer_to_producer = {}
-    pointer_to_consumer = {}
+    producers_consumers = ProducersConsumers()
     replace_output_pointer = {}
     pointer_to_extents = {}
 
     ReplaceInfo = namedtuple("ReplaceInfo", ["pointer", "reallocate"])
 
+    def _pointer_to_extend(stmt):

Review comment:
       Done.

##########
File path: python/tvm/relay/backend/contrib/ethosu/tir/producers_consumers.py
##########
@@ -0,0 +1,77 @@
+# 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.
+# pylint: disable=invalid-name, unused-argument
+"""The ProducersConsumers class"""
+from typing import Optional
+from collections.abc import KeysView
+import tvm
+
+
+class ProducersConsumers:
+    """It associates pointers with the loop nest that produces
+    their values and with the loop nest that consumes their values."""
+
+    def __init__(self) -> None:
+        self.indices: dict[tvm.tir.AttrStmt, int] = {}
+        self.producers: list[(tvm.tir.AttrStmt, tvm.tir.expr.Var)] = []
+        self.consumers: list[(tvm.tir.AttrStmt, list[tvm.tir.expr.Var])] = []
+
+    def add_producer(self, var: tvm.tir.expr.Var, attr: tvm.tir.AttrStmt) -> None:
+        """Add the attribute statement attr as producer of the variable var."""
+        self.indices[attr] = len(self.producers)
+        self.producers.append((attr, var))
+
+    def get_producer(
+        self, var: tvm.tir.expr.Var, attr: tvm.tir.AttrStmt
+    ) -> Optional[tvm.tir.AttrStmt]:
+        """Get the last attribute statement which produces the variable var when
+        the current attribute statement is attr."""
+        if var not in self.allocate_variables:
+            return None
+
+        index = self.indices[attr]
+        for i in list(reversed(range(index + 1))):
+            if self.producers[i][1] == var:
+                return self.producers[i][0]
+        return None
+
+    def get_last_producer(self, var: tvm.tir.expr.Var) -> Optional[tvm.tir.AttrStmt]:
+        """Get the last attribute statement which produces the variable var."""
+        return self.get_producer(var, self.producers[-1][0])
+
+    def add_allocate_variables(self, allocate_variables: KeysView) -> None:
+        """Add the allocated variables."""
+        self.allocate_variables = allocate_variables

Review comment:
       Done.

##########
File path: python/tvm/relay/backend/contrib/ethosu/tir/transform.py
##########
@@ -21,19 +21,16 @@
 from .utils import get_base_address, get_op_attrs
 
 
-def get_copy_params(stmt, producers, consumers):
+def get_copy_params(stmt, producers_consumers):

Review comment:
       No, we cannot. All functions in `ReplaceOperators`'s `op_map` must have the same signature.




-- 
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: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] ekalda commented on a change in pull request #10344: [microNPU] Integrate rolling buffers in Arm(R) Ethos(TM)-U

Posted by GitBox <gi...@apache.org>.
ekalda commented on a change in pull request #10344:
URL: https://github.com/apache/tvm/pull/10344#discussion_r816799153



##########
File path: python/tvm/relay/backend/contrib/ethosu/tir_to_cs_translator.py
##########
@@ -398,11 +398,11 @@ def assign_addresses(buffer_info, npu_ops, scratch_region_map):
 
     def replace_npu_fm_with_address(npu_fm):
         assert isinstance(npu_fm.tiles.addresses[0], tvm.tir.Load)
-        # We currently does not support tiles
-        # Change this when tiles are needed
-        # (i.e. when using rolling buffers)
-        assert npu_fm.tiles.addresses[1:] == [0, 0, 0]
-        npu_fm.tiles.addresses[1:] = [0, 0, 0]
+        for i in range(1, 4):
+            address = npu_fm.tiles.addresses[i]
+            if isinstance(address, tvm.tir.expr.Load):
+                address = address.index
+            npu_fm.tiles.addresses[i] = int(address)

Review comment:
       Do we need to take the size of the data type into account here?

##########
File path: python/tvm/relay/backend/contrib/ethosu/tir/producers_consumers.py
##########
@@ -0,0 +1,77 @@
+# 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.
+# pylint: disable=invalid-name, unused-argument
+"""The ProducersConsumers class"""
+from typing import Optional
+from collections.abc import KeysView
+import tvm
+
+
+class ProducersConsumers:
+    """It associates pointers with the loop nest that produces
+    their values and with the loop nest that consumes their values."""
+
+    def __init__(self) -> None:
+        self.indices: dict[tvm.tir.AttrStmt, int] = {}
+        self.producers: list[(tvm.tir.AttrStmt, tvm.tir.expr.Var)] = []
+        self.consumers: list[(tvm.tir.AttrStmt, list[tvm.tir.expr.Var])] = []
+
+    def add_producer(self, var: tvm.tir.expr.Var, attr: tvm.tir.AttrStmt) -> None:
+        """Add the attribute statement attr as producer of the variable var."""
+        self.indices[attr] = len(self.producers)
+        self.producers.append((attr, var))
+
+    def get_producer(
+        self, var: tvm.tir.expr.Var, attr: tvm.tir.AttrStmt
+    ) -> Optional[tvm.tir.AttrStmt]:
+        """Get the last attribute statement which produces the variable var when
+        the current attribute statement is attr."""
+        if var not in self.allocate_variables:
+            return None
+
+        index = self.indices[attr]
+        for i in list(reversed(range(index + 1))):
+            if self.producers[i][1] == var:
+                return self.producers[i][0]
+        return None
+
+    def get_last_producer(self, var: tvm.tir.expr.Var) -> Optional[tvm.tir.AttrStmt]:
+        """Get the last attribute statement which produces the variable var."""
+        return self.get_producer(var, self.producers[-1][0])
+
+    def add_allocate_variables(self, allocate_variables: KeysView) -> None:
+        """Add the allocated variables."""
+        self.allocate_variables = allocate_variables

Review comment:
       Maybe `self.allocate_variables` should be declared in the `__init__` as well?

##########
File path: python/tvm/relay/backend/contrib/ethosu/tir/transform.py
##########
@@ -21,19 +21,16 @@
 from .utils import get_base_address, get_op_attrs
 
 
-def get_copy_params(stmt, producers, consumers):
+def get_copy_params(stmt, producers_consumers):

Review comment:
       By the looks of it, the producers and consumers are not used in this function, so maybe we can remove it?

##########
File path: python/tvm/relay/backend/contrib/ethosu/tir/dma.py
##########
@@ -287,31 +321,69 @@ def get_ifm_params(pointer, producers):
         The serializable padding.
 
     """
-    pad = producers[pointer]
+    pad = producers_consumers.get_producer(pointer, stmt)
     serial_padding, input_pointer, _ = get_pad_params(pad)
-    upscale = producers[input_pointer]
+    upscale = producers_consumers.get_producer(input_pointer, pad)
     input_pointer, _ = get_upscale_params(upscale)
-    convert_to_nhwc = producers[input_pointer]
+    convert_to_nhwc = producers_consumers.get_producer(input_pointer, upscale)
     in_channels, input_pointer, _ = get_convert_to_nhwc_params(convert_to_nhwc)
-    read = producers[input_pointer]
+    read = producers_consumers.get_producer(input_pointer, convert_to_nhwc)
     serial_ifm, _, _ = get_read_params(read)
     serial_ifm.channels = in_channels
+
+    floor_mod_stmt = None
+    for_stmt = None
+
+    def _get_buffer_var(stmt):
+        nonlocal for_stmt
+        nonlocal floor_mod_stmt
+        if isinstance(stmt, tvm.tir.For):
+            for_stmt = stmt
+        if isinstance(stmt, tvm.tir.FloorMod):
+            floor_mod_stmt = stmt
+
+    tvm.tir.stmt_functor.post_order_visit(stmt, _get_buffer_var)
+
+    if floor_mod_stmt is not None:
+        layout = get_op_attrs(read)[0]["layout"]
+        channels = serial_ifm.channels
+        if for_stmt.body.loop_var == floor_mod_stmt.a.a.a:

Review comment:
       Asking for enlightenment - does the `floor_mod_stmt` always have that type of nesting, in a sense that `a.a.a` always exists? 

##########
File path: python/tvm/relay/backend/contrib/ethosu/tir/passes.py
##########
@@ -64,13 +65,16 @@ def ReplaceOperators():
         "ethosu_identity": get_identity_params,
         "ethosu_unary_elementwise": get_unary_elementwise_params,
     }
-    pointer_to_producer = {}
-    pointer_to_consumer = {}
+    producers_consumers = ProducersConsumers()
     replace_output_pointer = {}
     pointer_to_extents = {}
 
     ReplaceInfo = namedtuple("ReplaceInfo", ["pointer", "reallocate"])
 
+    def _pointer_to_extend(stmt):

Review comment:
       nit: To match with the spirit of other similar functions, should it start with a verb, e.g. `_find_pointer_to_extent`




-- 
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: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] NicolaLancellotti commented on a change in pull request #10344: [microNPU] Integrate rolling buffers in Arm(R) Ethos(TM)-U

Posted by GitBox <gi...@apache.org>.
NicolaLancellotti commented on a change in pull request #10344:
URL: https://github.com/apache/tvm/pull/10344#discussion_r814917156



##########
File path: python/tvm/relay/backend/contrib/ethosu/codegen.py
##########
@@ -31,6 +31,12 @@
 from tvm.relay.backend.contrib.ethosu.op import op_attrs
 from tvm.relay.backend.contrib.ethosu import op
 
+# We are currently using copy_constants scheduler In the long run,
+# this should be a single intelligent and a composite scheduler
+# that can perform scheduling based on user inputs such as
+# scratch memory size.
+CASCADER = copy_constants()

Review comment:
       Done




-- 
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: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] NicolaLancellotti commented on a change in pull request #10344: [microNPU] Integrate rolling buffers in Arm(R) Ethos(TM)-U

Posted by GitBox <gi...@apache.org>.
NicolaLancellotti commented on a change in pull request #10344:
URL: https://github.com/apache/tvm/pull/10344#discussion_r829413144



##########
File path: python/tvm/relay/backend/contrib/ethosu/tir_to_cs_translator.py
##########
@@ -398,11 +398,11 @@ def assign_addresses(buffer_info, npu_ops, scratch_region_map):
 
     def replace_npu_fm_with_address(npu_fm):
         assert isinstance(npu_fm.tiles.addresses[0], tvm.tir.Load)
-        # We currently does not support tiles
-        # Change this when tiles are needed
-        # (i.e. when using rolling buffers)
-        assert npu_fm.tiles.addresses[1:] == [0, 0, 0]
-        npu_fm.tiles.addresses[1:] = [0, 0, 0]
+        for i in range(1, 4):
+            address = npu_fm.tiles.addresses[i]
+            if isinstance(address, tvm.tir.expr.Load):
+                address = address.index
+            npu_fm.tiles.addresses[i] = int(address)

Review comment:
       You are right, that logic (the integer conversion) worked before the USMP was enabled, I forgot to remove it during a rebase.
   Now we have the following logic:
   ```
   npu_fm.tiles.addresses[0] = address + int(index)
   npu_fm.tiles.addresses[1] = address if isinstance(npu_fm.tiles.addresses[1], tvm.tir.BufferLoad) else 0
   npu_fm.tiles.addresses[2] = address if isinstance(npu_fm.tiles.addresses[2], tvm.tir.BufferLoad) else 0
   npu_fm.tiles.addresses[3] = 0
   ```
   
   Where the index of tile1 and tile2, when used, are always 0.




-- 
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: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] NicolaLancellotti commented on a change in pull request #10344: [microNPU] Integrate rolling buffers in Arm(R) Ethos(TM)-U

Posted by GitBox <gi...@apache.org>.
NicolaLancellotti commented on a change in pull request #10344:
URL: https://github.com/apache/tvm/pull/10344#discussion_r816961419



##########
File path: python/tvm/relay/backend/contrib/ethosu/tir_to_cs_translator.py
##########
@@ -398,11 +398,11 @@ def assign_addresses(buffer_info, npu_ops, scratch_region_map):
 
     def replace_npu_fm_with_address(npu_fm):
         assert isinstance(npu_fm.tiles.addresses[0], tvm.tir.Load)
-        # We currently does not support tiles
-        # Change this when tiles are needed
-        # (i.e. when using rolling buffers)
-        assert npu_fm.tiles.addresses[1:] == [0, 0, 0]
-        npu_fm.tiles.addresses[1:] = [0, 0, 0]
+        for i in range(1, 4):
+            address = npu_fm.tiles.addresses[i]
+            if isinstance(address, tvm.tir.expr.Load):
+                address = address.index
+            npu_fm.tiles.addresses[i] = int(address)

Review comment:
       The addresses are already correct, we have only to convert `IntImm` to `int` and consider the case where the address is a `Load`.




-- 
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: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] NicolaLancellotti commented on a change in pull request #10344: [microNPU] Integrate rolling buffers in Arm(R) Ethos(TM)-U

Posted by GitBox <gi...@apache.org>.
NicolaLancellotti commented on a change in pull request #10344:
URL: https://github.com/apache/tvm/pull/10344#discussion_r835125719



##########
File path: python/tvm/relay/backend/contrib/ethosu/tir/dma.py
##########
@@ -197,18 +197,72 @@ def get_read_params(stmt):
 
     base_address = [get_base_address(index) for index in inner.value.indices]
     data_type = inner.buffer.data.type_annotation.element_type.dtype
+
+    def check_rolling_buffer():

Review comment:
       Done




-- 
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: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] NicolaLancellotti commented on pull request #10344: [microNPU] Integrate rolling buffers in Arm(R) Ethos(TM)-U

Posted by GitBox <gi...@apache.org>.
NicolaLancellotti commented on pull request #10344:
URL: https://github.com/apache/tvm/pull/10344#issuecomment-1078867786


   I rebased the first commit and I addressed the comments in the second one.


-- 
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: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org