You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by ju...@apache.org on 2024/01/21 07:44:15 UTC
(tvm) branch unity updated: [Unity] Improve buffer allocation for handling duplicated buffer names. (#16437)
This is an automated email from the ASF dual-hosted git repository.
junrushao pushed a commit to branch unity
in repository https://gitbox.apache.org/repos/asf/tvm.git
The following commit(s) were added to refs/heads/unity by this push:
new 8f2e820434 [Unity] Improve buffer allocation for handling duplicated buffer names. (#16437)
8f2e820434 is described below
commit 8f2e82043492423de96bb2a73193424dfa7418f1
Author: Lei Wang <34...@users.noreply.github.com>
AuthorDate: Sun Jan 21 15:44:06 2024 +0800
[Unity] Improve buffer allocation for handling duplicated buffer names. (#16437)
Refer to this issue
https://github.com/apache/tvm/issues/16433 , the relax Pass FuseTIR creates intermediate name by
```c++
n->name = param->name_hint + "_intermediate";
```
which may lead to some bugs when we encounter two fused blocks with the same param name, for example what I encountered in resnet-18, the conv+add+multiply+add, the fused two add operators' block may point to the same block.
So we should provide a method to get an unique name for each in duplicate buffers.
```cpp
auto unify_name_hints = [this, &buffer, ¶m]() {
String base_name = buffer->name;
String unique_name = base_name + "_intermediate";
size_t unique_id = 0;
std::unordered_set<std::string> names;
for (auto& _buffer : func_info_.alloc_buffers) {
names.insert(_buffer->name);
}
while (names.find(unique_name) != names.end()) {
unique_name = unique_name + "_" + std::to_string(++unique_id);
}
return unique_name;
};
```
---
src/relax/transform/fuse_tir.cc | 17 +++++-
tests/python/relax/test_transform_fuse_tir.py | 86 +++++++++++++++++++++++++++
2 files changed, 102 insertions(+), 1 deletion(-)
diff --git a/src/relax/transform/fuse_tir.cc b/src/relax/transform/fuse_tir.cc
index e5e2883a29..fc3d79ae62 100644
--- a/src/relax/transform/fuse_tir.cc
+++ b/src/relax/transform/fuse_tir.cc
@@ -682,10 +682,25 @@ class FusedTIRConstructor : public ExprVisitor {
const tir::Var& param = output_params[i];
const tir::Buffer& buffer = func->buffer_map.at(param);
+ auto unify_name_hints = [this, &buffer, ¶m]() {
+ String base_name = buffer->name;
+ String unique_name = base_name + "_intermediate";
+ size_t unique_id = 0;
+ std::unordered_set<std::string> names;
+
+ for (auto& _buffer : func_info_.alloc_buffers) {
+ names.insert(_buffer->name);
+ }
+
+ while (names.find(unique_name) != names.end()) {
+ unique_name = unique_name + "_" + std::to_string(++unique_id);
+ }
+ return unique_name;
+ };
// Update buffer with new symbolic shape according to the sinfo
auto n = make_object<tir::BufferNode>(*buffer.get());
n->shape = output_shapes[i];
- n->name = param->name_hint + "_intermediate";
+ n->name = unify_name_hints();
tir::Buffer new_buffer(n);
func_info_.alloc_buffers.push_back(new_buffer);
alloc_buffers.push_back(new_buffer);
diff --git a/tests/python/relax/test_transform_fuse_tir.py b/tests/python/relax/test_transform_fuse_tir.py
index c8f98e8724..143670c701 100644
--- a/tests/python/relax/test_transform_fuse_tir.py
+++ b/tests/python/relax/test_transform_fuse_tir.py
@@ -1351,6 +1351,92 @@ def test_tuple_input_unused_field():
_check(Module, Expected)
+def test_unique_duplicated_buffer_allocation():
+ @I.ir_module
+ class Module:
+ @T.prim_func(private=True)
+ def add(
+ A: T.Buffer((T.int64(4096), T.int64(4096)), "float16"),
+ Out: T.Buffer((T.int64(4096), T.int64(4096)), "float16"),
+ ):
+ for i, j in T.grid(T.int64(4096), T.int64(4096)):
+ with T.block("add"):
+ vi, vj = T.axis.remap("SS", [i, j])
+ Out[vi, vj] = A[vi, vj] + T.float16(1.0)
+
+ @T.prim_func(private=True)
+ def add1(
+ A: T.Buffer((T.int64(4096), T.int64(4096)), "float16"),
+ Out: T.Buffer((T.int64(4096), T.int64(4096)), "float16"),
+ ):
+ for i, j in T.grid(T.int64(4096), T.int64(4096)):
+ with T.block("add"):
+ vi, vj = T.axis.remap("SS", [i, j])
+ Out[vi, vj] = A[vi, vj] + T.float16(2.0)
+
+ @R.function
+ def main(
+ input_embeds: R.Tensor((4096, 4096), dtype="float16"),
+ ) -> R.Tensor((4096, 4096), dtype="float16"):
+ cls = Module
+ with R.dataflow():
+ gv: R.Tensor((4096, 4096), dtype="float16") = cls.fused_func(input_embeds)
+ R.output(gv)
+ return gv
+
+ @R.function(private=True)
+ def fused_func(
+ input_embeds: R.Tensor((4096, 4096), dtype="float16"),
+ ) -> R.Tensor((4096, 4096), dtype="float16"):
+ R.func_attr({"Primitive": 1})
+ cls = Module
+ with R.dataflow():
+ lv = R.call_tir(
+ cls.add, (input_embeds,), out_sinfo=R.Tensor((4096, 4096), dtype="float16")
+ )
+ gv = R.call_tir(cls.add1, (lv,), out_sinfo=R.Tensor((4096, 4096), dtype="float16"))
+ R.output(gv)
+ return gv
+
+ @I.ir_module
+ class Expected:
+ @T.prim_func(private=True)
+ def fused_func(
+ input_embeds: T.Buffer((T.int64(4096), T.int64(4096)), "float16"),
+ Out_intermediate_1: T.Buffer((T.int64(4096), T.int64(4096)), "float16"),
+ ):
+ T.func_attr({"tir.noalias": T.bool(True)})
+ Out_intermediate = T.alloc_buffer((T.int64(4096), T.int64(4096)), "float16")
+ for i, j in T.grid(T.int64(4096), T.int64(4096)):
+ with T.block("add"):
+ vi, vj = T.axis.remap("SS", [i, j])
+ T.reads(input_embeds[vi, vj])
+ T.writes(Out_intermediate[vi, vj])
+ Out_intermediate[vi, vj] = input_embeds[vi, vj] + T.float16(1)
+ for i, j in T.grid(T.int64(4096), T.int64(4096)):
+ with T.block("add_1"):
+ vi, vj = T.axis.remap("SS", [i, j])
+ T.reads(Out_intermediate[vi, vj])
+ T.writes(Out_intermediate_1[vi, vj])
+ Out_intermediate_1[vi, vj] = Out_intermediate[vi, vj] + T.float16(2)
+
+ @R.function
+ def main(
+ input_embeds: R.Tensor((4096, 4096), dtype="float16")
+ ) -> R.Tensor((4096, 4096), dtype="float16"):
+ cls = Expected
+ with R.dataflow():
+ gv = R.call_tir(
+ cls.fused_func,
+ (input_embeds,),
+ out_sinfo=R.Tensor((4096, 4096), dtype="float16"),
+ )
+ R.output(gv)
+ return gv
+
+ _check(Module, Expected)
+
+
def test_extern_func():
bb = relax.BlockBuilder()
bb.add_func(relax.extern("extern_func"), "extern_func")