From cf27635b26151e77065a23836136f2f098ed02ec Mon Sep 17 00:00:00 2001 From: Zijiang Yang Date: Tue, 18 Apr 2023 16:40:33 +0800 Subject: [PATCH 1/4] [Bugfix][VTA] Fix FSIM compile error on macOS. VTA FSIM could not be built on macOS, for it leverages malloc.h and memalign, yet both have been deprecated and are not provided by macOS. This issue was captured in #13173. This commit stops including malloc.h in VTA Runtime as stdlib.h has provided functions we need. This commit uses posix_memalign instead of memalign. It is a portable standard function. --- vta/runtime/runtime.cc | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/vta/runtime/runtime.cc b/vta/runtime/runtime.cc index c3d37a13133b..6ffcfd90f33a 100644 --- a/vta/runtime/runtime.cc +++ b/vta/runtime/runtime.cc @@ -27,7 +27,6 @@ #include "runtime.h" #include -#include #include #include #include @@ -76,7 +75,12 @@ class AlignmentAllocator : public std::allocator { inline const_pointer address(const_reference r) const { return &r; } - inline pointer allocate(size_type n) { return (pointer)memalign(N, n * sizeof(value_type)); } + inline pointer allocate(size_type n) { + pointer mem = nullptr; + const int err = posix_memalign((void **) &mem, N, n * sizeof(value_type)); + ICHECK_EQ(err, 0) << "InternalError: failed to allocate aligned memory. "; + return mem; + } inline void deallocate(pointer p, size_type) { free(p); } @@ -528,7 +532,9 @@ class UopQueue : public BaseQueue { total_size += ksize; } - char* lbuf = (char*)memalign(ALLOC_ALIGNMENT, total_size); + char* lbuf = nullptr; + const int err = posix_memalign((void **) &lbuf, ALLOC_ALIGNMENT, total_size); + ICHECK_EQ(err, 0) << "InternalError: failed to allocate aligned memory for load buffer. "; uint32_t offset = 0; for (uint32_t i = 0; i < cache_.size(); ++i) { uint32_t ksize = cache_[i]->size() * kElemBytes; From 2c3f1eeebe04004c4d137cb4a1601d9b65e9148c Mon Sep 17 00:00:00 2001 From: Zijiang Yang Date: Sat, 4 Nov 2023 18:47:08 +0800 Subject: [PATCH 2/4] Fix format. --- vta/runtime/runtime.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vta/runtime/runtime.cc b/vta/runtime/runtime.cc index 6ffcfd90f33a..66a428cc5590 100644 --- a/vta/runtime/runtime.cc +++ b/vta/runtime/runtime.cc @@ -77,7 +77,7 @@ class AlignmentAllocator : public std::allocator { inline pointer allocate(size_type n) { pointer mem = nullptr; - const int err = posix_memalign((void **) &mem, N, n * sizeof(value_type)); + const int err = posix_memalign((void**)&mem, N, n * sizeof(value_type)); ICHECK_EQ(err, 0) << "InternalError: failed to allocate aligned memory. "; return mem; } @@ -533,7 +533,7 @@ class UopQueue : public BaseQueue { } char* lbuf = nullptr; - const int err = posix_memalign((void **) &lbuf, ALLOC_ALIGNMENT, total_size); + const int err = posix_memalign((void**)&lbuf, ALLOC_ALIGNMENT, total_size); ICHECK_EQ(err, 0) << "InternalError: failed to allocate aligned memory for load buffer. "; uint32_t offset = 0; for (uint32_t i = 0; i < cache_.size(); ++i) { From e68793403ea947d0fa6a29ef75c808e18bf7a6b1 Mon Sep 17 00:00:00 2001 From: Zijiang Yang Date: Wed, 18 Sep 2024 16:11:33 +0800 Subject: [PATCH 3/4] =?UTF-8?q?[Bugfix][ONNX]=20Skip=20constant=20If=20nod?= =?UTF-8?q?e=20generated=20by=20PyTorch=20This=20commit=20adds=20a=20check?= =?UTF-8?q?=20for=20If=20nodes=20for=20ONNX=20frontend=20of=20Relay=20to?= =?UTF-8?q?=20skip=20the=20broadcast=20if=20the=20predicate=20is=20constan?= =?UTF-8?q?t.=20Sometimes=20PyTorch=20to=20ONNX=20inserts=20silly=20if=20n?= =?UTF-8?q?odes=20that=20produce=20dynamic=20ranks,=20and=20ONNX=20fronten?= =?UTF-8?q?d=20of=20TVM=20would=20broadcast=20the=20lower=20dimensions=20b?= =?UTF-8?q?etween=20branches,=20which=20is=20irrational=20for=20some=20cas?= =?UTF-8?q?es,=20e.g.=205=C3=975=C3=973=C3=974=20to=205=C3=975=C3=973?= =?UTF-8?q?=C3=974=C3=971.=20The=20predicate=20of=20silly=20if=20might=20b?= =?UTF-8?q?e=20constant=20and=20reasonable=20to=20skip=20to=20avoid=20the?= =?UTF-8?q?=20broadcast=20problem.=20This=20issue=20was=20captured=20in=20?= =?UTF-8?q?#16898.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/tvm/relay/frontend/onnx.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/python/tvm/relay/frontend/onnx.py b/python/tvm/relay/frontend/onnx.py index ee7a5d6b329a..7a7dd30f5e95 100644 --- a/python/tvm/relay/frontend/onnx.py +++ b/python/tvm/relay/frontend/onnx.py @@ -4565,6 +4565,23 @@ def _impl_v1(cls, inputs, attr, params): "Attempting to unify ranks but this may produce incorrect results." ) warnings.warn(warning_msg) + if type(inputs[0]) is tvm.relay.expr.Constant: + cond_value = inputs[0].data.asnumpy()[0] + # breakpoint() + node_name = attr["tvm_custom"]["name"] + warn_msg_begin = f"Predicate of If node {node_name} is always " + if cond_value == np.bool_(True): + warnings.warn( + warn_msg_begin + + "true so only then branch would be executed. Removing else branch. " + ) + else_expr = then_expr + elif cond_value == np.bool_(False): + warnings.warn( + warn_msg_begin + + "false so only else branch would be executed. Removing then branch. " + ) + then_expr = else_expr if len(then_shape) < len(else_shape): then_expr = _op.broadcast_to_like(then_expr, else_expr) else: @@ -6529,6 +6546,7 @@ def _impl_v11(cls, inputs, attr, params): # compatible operators that do NOT require any conversion. _identity_list = [] + # _convert_map defines maps of name to converter functor(callable) # for 1 to 1 mapping, use Renamer if nothing but name is different # use AttrCvt if attributes need to be converted From 125683cbc36be5536ece4dd89576298e40dfcc53 Mon Sep 17 00:00:00 2001 From: Zijiang Yang Date: Wed, 18 Sep 2024 16:48:46 +0800 Subject: [PATCH 4/4] Fix format. --- python/tvm/relay/frontend/onnx.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/python/tvm/relay/frontend/onnx.py b/python/tvm/relay/frontend/onnx.py index 7a7dd30f5e95..8da8a5b11262 100644 --- a/python/tvm/relay/frontend/onnx.py +++ b/python/tvm/relay/frontend/onnx.py @@ -4565,18 +4565,18 @@ def _impl_v1(cls, inputs, attr, params): "Attempting to unify ranks but this may produce incorrect results." ) warnings.warn(warning_msg) - if type(inputs[0]) is tvm.relay.expr.Constant: - cond_value = inputs[0].data.asnumpy()[0] - # breakpoint() + # Skip constant If node to avoid irrational broadcast + if isinstance(inputs[0], tvm.relay.expr.Constant): + predicate = inputs[0].data.asnumpy()[0] node_name = attr["tvm_custom"]["name"] warn_msg_begin = f"Predicate of If node {node_name} is always " - if cond_value == np.bool_(True): + if predicate == np.bool_(True): warnings.warn( warn_msg_begin + "true so only then branch would be executed. Removing else branch. " ) else_expr = then_expr - elif cond_value == np.bool_(False): + elif predicate == np.bool_(False): warnings.warn( warn_msg_begin + "false so only else branch would be executed. Removing then branch. "