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 2020/04/10 23:54:22 UTC

[GitHub] [incubator-tvm] zhiics commented on a change in pull request #5144: [Relay][VM] Memory planner (part 1)

zhiics commented on a change in pull request #5144: [Relay][VM] Memory planner (part 1)
URL: https://github.com/apache/incubator-tvm/pull/5144#discussion_r406985977
 
 

 ##########
 File path: python/tvm/relay/transform/memory_plan.py
 ##########
 @@ -0,0 +1,189 @@
+# 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=no-else-return,invalid-name,len-as-condition,too-many-nested-blocks
+"""
+A pass for manifesting explicit memory allocations.
+"""
+import attr
+import numpy as np
+from typing import Optional, Dict
+
+from ..expr_functor import ExprMutator
+from ..scope_builder import ScopeBuilder
+from .. import op, ty, expr
+from ... import DataType, register_func, IRModule
+from .. import analysis
+from . import FoldConstant, InferType, function_pass
+from ..backend import compile_engine
+
+def is_primitive(call):
+    return hasattr(call, 'op') and hasattr(call.op, 'attrs') and \
+           hasattr(call.op.attrs, 'Primitive') and int(call.op.attrs.Primitive) == 1
+
+@attr.s(auto_attribs=True)
+class Region:
+    var: expr.Var
+    size: expr.Expr
+    alignment: Optional[expr.Expr]
+    dtype: Optional[str]
+    offsets: Dict[expr.Var, expr.Expr] = {}
+
+    def grow(self, old_storage: expr.Var, size: expr.Expr, alignment: expr.Expr, dtype: str) -> None:
+        if self.dtype:
+            assert self.dtype == dtype, "must have matching dtypes in a region"
+        else:
+            self.dtype = dtype
+
+        if self.alignment:
+            assert analysis.alpha_equal(self.alignment, alignment), "must have matching alignments in a region"
+        else:
+            self.alignment = alignment
+
+        # Record the offset at which we allocate the storage.
+        self.offsets[old_storage] = self.size
+
+        self.size = self.size + size
+
+    def to_expr(self) -> expr.Expr:
+        return op.memory.alloc_storage(self.size, self.alignment, self.dtype)
+
+def iterative_let(let, each_binding, kont):
+    bindings = []
+    while isinstance(let, expr.Let):
+        lhs = let.var
+        rhs = let.value
+        bindings.append(each_binding(lhs, rhs))
+        let = let.body
+
+    return kont(bindings, let)
+
+def mk_let(bindings, body):
+    for var, value in reversed(bindings):
+        body = expr.Let(var, value, body)
+    return body
+
+class StorageCoalesce(ExprMutator):
+    def __init__(self):
+        super().__init__()
+        self.regions = []
+
+    def enter_scope(self):
+        zero = expr.const(0, dtype="int64")
+        region_var = expr.var(f"region{len(self.regions)}")
+        region = Region(region_var, zero, None, None)
+        self.regions.append(region)
+
+    def exit_scope(self, body: expr.Expr) -> expr.Expr:
+        region = self.regions.pop()
+        storage_expr = region.to_expr()
+        assert storage_expr, "can not be None"
+        return expr.Let(region.var, storage_expr, body)
+
+    def current_region(self) -> Region:
+        return self.regions[-1]
+
+    def visit_function(self, function):
+        if function.attrs and int(function.attrs.Primitive) == 1:
+            return super().visit_function(function)
 
 Review comment:
   QQ, do we need to step into primitive function? I thought we should probably just return it, right?

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services