Skip to content

Commit

Permalink
[TIR][USMP] adding the pass to convert to pool offsets (apache#9418)
Browse files Browse the repository at this point in the history
* [TIR][USMP] adding the pass to convert to pool offsets

This commit adds a transform pass that consumes
the planned pool allocations using memory planning algorithm
that convertes them to pool offsets.

* adds two test cases for a linear structure with two pools
* adds test case with a single pool for residual structures

Change-Id: I9d31e854461b5c21df72d1452120d286b96791c0

* [TIR][USMP] adding the pass to convert to pool offsets

* Adding a toggle to produce TIR that is TVMScript printable for unit
testing
* Fixing the unit tests
* Ensure deterministic pool variable ordering.

Change-Id: I317675df03327b0ebbf4ca074255384e63f07cd6

* [TIR][USMP] adding the pass to convert to pool offsets

Fixing the references after changes in the memory planning
algorithm.

Change-Id: Id7c22356fd5de43d10a2b4fc70e978af2c6d599d

* [TIR][USMP] adding the pass to convert to pool offsets

* fixing the lint

Change-Id: I7ff920b92d14a9919c930a4b35a2169c77a57dd1

* [TIR][USMP] adding the pass to convert to pool offsets

* removing unnecessary defitinitions
* remove global var map
* adding explaination for let bindings to pointer type

Change-Id: I31bd1a9f3057ee7f06252263565b0f75c51e6d13

* [TIR][USMP] adding the pass to convert to pool offsets

* rebase changes
* making imports absolute
* fixing typos and removing unnecesary lines

Change-Id: I4c94b9955b001513fecb39ca94f81b1ad99c7bfc

* [TIR][USMP] adding the pass to convert to pool offsets

* fixing typos

Change-Id: I42c557fd394aefdf8c2e825c4e88770eb0732f9b
  • Loading branch information
manupak authored and ylc committed Jan 13, 2022
1 parent ed4a344 commit fdd3360
Show file tree
Hide file tree
Showing 12 changed files with 1,061 additions and 5 deletions.
48 changes: 48 additions & 0 deletions include/tvm/tir/usmp/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,44 @@ class PoolAllocation : public ObjectRef {
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(PoolAllocation, ObjectRef, PoolAllocationNode);
};

/*!
* \brief This object contains information post-allocation for PoolInfo objects
*/
struct AllocatedPoolInfoNode : public Object {
/*! \brief The assigned PoolInfo object */
PoolInfo pool_info;
/*! \brief The allocated size into this pool */
Integer allocated_size;
/*! \brief An optional associated pool Var*/
Optional<Var> pool_var;

void VisitAttrs(tvm::AttrVisitor* v) {
v->Visit("pool_info", &pool_info);
v->Visit("allocated_size", &allocated_size);
v->Visit("pool_var", &pool_var);
}

bool SEqualReduce(const AllocatedPoolInfoNode* other, SEqualReducer equal) const {
return equal(pool_info, other->pool_info) && equal(allocated_size, other->allocated_size) &&
equal(pool_var, other->pool_var);
}

void SHashReduce(SHashReducer hash_reduce) const {
hash_reduce(pool_info);
hash_reduce(allocated_size);
hash_reduce(pool_var);
}

static constexpr const char* _type_key = "tir.usmp.AllocatedPoolInfo";
TVM_DECLARE_FINAL_OBJECT_INFO(AllocatedPoolInfoNode, Object);
};

class AllocatedPoolInfo : public ObjectRef {
public:
TVM_DLL AllocatedPoolInfo(PoolInfo pool_info, Integer allocated_size, Var pool_var = Var());
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(AllocatedPoolInfo, ObjectRef, AllocatedPoolInfoNode);
};

/*!
* \brief Convert the IR-bound BufferInfo map to an array of BufferInfo
*
Expand All @@ -248,6 +286,16 @@ Integer CalculateExtentsSize(const AllocateNode* op);

} // namespace usmp
} // namespace tir

namespace attr {
/*!
* \brief This is a BaseFunc attribute to indicate which input var represent
* a PoolInfo Object in the form of a Map<Var, PoolInfo>.
*/
static constexpr const char* kPoolArgs = "pool_args";

} // namespace attr

} // namespace tvm

#endif // TVM_TIR_USMP_UTILS_H_
2 changes: 1 addition & 1 deletion python/tvm/script/tir/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"""TVMScript for TIR"""

# Type system
from .ty import int8, int16, int32, int64, float16, float32, float64
from .ty import uint8, int8, int16, int32, int64, float16, float32, float64
from .ty import boolean, handle, Ptr, Tuple, Buffer

from .prim_func import prim_func
1 change: 1 addition & 0 deletions python/tvm/script/tir/ty.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def __getitem__(self, args):
pass # pylint: disable=unnecessary-pass


uint8 = ConcreteType("uint8")
int8 = ConcreteType("int8")
int16 = ConcreteType("int16")
int32 = ConcreteType("int32")
Expand Down
1 change: 1 addition & 0 deletions python/tvm/tir/usmp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@
"""Namespace for Unified Static Memory Planner"""

from . import analysis
from . import transform
from .utils import BufferInfo
20 changes: 20 additions & 0 deletions python/tvm/tir/usmp/transform/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 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=unused-import, redefined-builtin
"""Namespace for Unified Static Memory Planner"""

from .transform import convert_pool_allocations_to_offsets
21 changes: 21 additions & 0 deletions python/tvm/tir/usmp/transform/_ffi_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 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.
"""FFI APIs for tvm.tir.usmp.analysis"""
import tvm._ffi


tvm._ffi._init_api("tir.usmp.transform", __name__)
46 changes: 46 additions & 0 deletions python/tvm/tir/usmp/transform/transform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 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.
"""USMP Transform Python API for passes"""
# pylint: disable=invalid-name

from typing import Dict

import tvm
from tvm.tir import Stmt
from tvm.tir.usmp.utils import PoolAllocation
from . import _ffi_api


def convert_pool_allocations_to_offsets(
pool_allocations: Dict[Stmt, PoolAllocation], emit_tvmscript_printable: bool = False
) -> tvm.transform.Pass:
"""Convert pool allocations to Load nodes with offsets from pools.
Parameters
----------
pool_allocations : Dict[Stmt, PoolAllocation]
Allocate or AllocateConst node to pool allocation mapping
emit_tvmscript_printable : bool
A toggle to emit TVMScript printable IRModule for unit tests
removing all attributes that should be attached for integration
Returns
-------
ret: tvm.transform.Pass
The registered pass that converts the allocations to offsets.
"""
return _ffi_api.ConvertPoolAllocationsToOffsets(pool_allocations, emit_tvmscript_printable)
7 changes: 4 additions & 3 deletions src/printer/text_printer.h
Original file line number Diff line number Diff line change
Expand Up @@ -449,10 +449,11 @@ class TextPrinter {

Doc PrintFinal(const ObjectRef& node) {
Doc doc;
if (node->IsInstance<IRModuleNode>()) {
if (node.defined() && node->IsInstance<IRModuleNode>()) {
doc << PrintMod(Downcast<IRModule>(node));
} else if (node->IsInstance<tir::PrimFuncNode>() || node->IsInstance<PrimExprNode>() ||
node->IsInstance<tir::StmtNode>()) {
} else if (node.defined() &&
(node->IsInstance<tir::PrimFuncNode>() || node->IsInstance<PrimExprNode>() ||
node->IsInstance<tir::StmtNode>())) {
doc << tir_text_printer_.Print(node);
} else {
doc << relay_text_printer_.PrintFinal(node);
Expand Down
9 changes: 8 additions & 1 deletion src/tir/ir/stmt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ namespace tir {
LetStmt::LetStmt(Var var, PrimExpr value, Stmt body, Span span) {
ICHECK(value.defined());
ICHECK(body.defined());
ICHECK_EQ(value.dtype(), var.dtype());
auto vdtype = value.dtype();
// It is still valid to bind a pointer type
// var to a value that is of type handle.
if (var->type_annotation.as<PointerTypeNode>()) {
ICHECK(vdtype.is_handle());
} else {
ICHECK_EQ(value.dtype(), var.dtype());
}

ObjectPtr<LetStmtNode> node = make_object<LetStmtNode>();
node->var = std::move(var);
Expand Down
Loading

0 comments on commit fdd3360

Please sign in to comment.