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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]