-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[TIR][Runtime] Allow use of external non-compact/non-aligned buffers #14771
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TIR][Runtime] Allow use of external non-compact/non-aligned buffers #14771
Conversation
|
Thanks for contributing to TVM! Please refer to the contributing guidelines https://tvm.apache.org/docs/contribute/ for useful information and tips. Please request code reviews from Reviewers by @-ing them in a comment.
Generated by tvm-bot |
617c5bc to
8c9cd3b
Compare
|
It looks like this commit breaks most of the Rust tests, as the alignment assumed by the LLVM kernel was never validated when running through Rust. Unfortunately, I can't find a good way to produce aligned allocations in Rust, as |
This utility was useful during debugging that resulted in the changes made in apache#14771, but is not otherwise related to that PR.
This utility was useful during debugging that resulted in the changes made in #14771, but is not otherwise related to that PR.
0a43269 to
db500b8
Compare
4ba735d to
f79bf72
Compare
f8c1b78 to
d9ea602
Compare
By default, the LLVM codegen is allowed to assume that externally-allocated buffers are 64-byte aligned (from the value of `kAllocAlignment`). The buffers allocated in the Rust unit tests do not provide this alignment, and need to specify that no additional alignment is provided. This is a subset of changes made in apache#14771, broken out into an independent PR for ease of testing/review.
Prior to this commit, constants were aligned to 16-byte regions within the allocation pool. As a result, the pointer passed to packed functions would usually not meet the default alignment requirement of `kAllocAlignment = 64` bytes. Because LLVM is allowed to assume that this alignment is met, this could result in runtime errors. This commit updates the default alignment to `kAllocAlignment`, which can be overridden on a per-buffer basis. This is a subset of changes made in apache#14771, broken out into an independent PR for ease of testing/review.
The default TVM allocators provide allocations with 64-byte alignment. This alignment is provided as a guarantee to LLVM optimizations, and failure to provide aligned allocations may result in incorrect results. This commit updates the MicroTVM examples to provide 64-byte aligned memory allocations. This is a subset of changes made in apache#14771, broken out into an independent PR for ease of testing/review.
The default TVM allocators provide allocations with 64-byte alignment. This alignment is provided as a guarantee to LLVM optimizations, and failure to provide aligned allocations may result in incorrect results. This commit updates the CRT examples to provide 64-byte aligned memory allocations. This is a subset of changes made in apache#14771, broken out into an independent PR for ease of testing/review.
87dda27 to
8b5c779
Compare
|
Rebased onto main to resolve conflict with #15132. The individual commits are now cleaned up and ordered such that, if CI failures continue to occur, they can be segmented out into independent PRs. |
The `TVMAotExecutor_GetInputName` function requires a `const char**`, but is provided with a `char**`. While C's automatic pointer conversion does allow this as an automatic conversion, following const-correctness avoids a compile-time warning. This warning was first caused following apache#14529, which itself was to follow const-correctness within the implementation of `TVMAotExecutor_GetInputName`.
By default, the LLVM codegen is allowed to assume that externally-allocated buffers are 64-byte aligned (from the value of `kAllocAlignment`). The buffers allocated in the Rust unit tests do not provide this alignment, and need to specify that no additional alignment is provided. This is a subset of changes made in apache#14771, broken out into an independent PR for ease of testing/review.
Prior to this commit, constants were aligned to 16-byte regions within the allocation pool. As a result, the pointer passed to packed functions would usually not meet the default alignment requirement of `kAllocAlignment = 64` bytes. Because LLVM is allowed to assume that this alignment is met, this could result in runtime errors. This commit updates the default alignment to `kAllocAlignment`, which can be overridden on a per-buffer basis. This is a subset of changes made in apache#14771, broken out into an independent PR for ease of testing/review.
The default TVM allocators provide allocations with 64-byte alignment. This alignment is provided as a guarantee to LLVM optimizations, and failure to provide aligned allocations may result in incorrect results. This commit updates the MicroTVM examples to provide 64-byte aligned memory allocations. This is a subset of changes made in apache#14771, broken out into an independent PR for ease of testing/review.
The default TVM allocators provide allocations with 64-byte alignment. This alignment is provided as a guarantee to LLVM optimizations, and failure to provide aligned allocations may result in incorrect results. This commit updates the CRT examples to provide 64-byte aligned memory allocations. This is a subset of changes made in apache#14771, broken out into an independent PR for ease of testing/review.
Prior to this commit, any use of `tvm.nd.from_dlpack` to create a strided `NDArray`, or a `NDArray` whose alignment was less than `tvm::runtime::kAllocAlignment` would raise an error. As a result, views into larger arrays, which are unlikely to be aligned and compact, could only be shared when copied into an aligned and compact buffer. This commit moves the compact/aligned check from the `NDArray` class into the generated TIR code as part of DLTensor unpacking. These checks were initially introduced in apache#11391, to avoid segfaults caused by use of non-aligned buffers in code intended for aligned buffers. The new checks will provide the same safeguard as the alignment is checked prior to use, but allows the alignment requirement to be relaxed on a per-buffer basis. This approach also removes a potential bug resulting from compile-time configuration of `tvm::runtime::kAllocAlignment`, first introduced in apache#13307. Since TVM supports cross-compiling, the installation of TVM used to compile a kernel may assume a larger value of `kAllocAlignment` than is provided by the runtime installation of TVM. By validating the alignment within the generated kernel, rather than as part of the runtime, this potential inconsistency would be caught. This check is also restricted to targets whose `void*` opaque pointer can be interpreted as a pointer to the data array. (e.g. No such check applies on Vulkan, as the `void*` is a pointer to a struct that contains additional bookkeeping.)
8b5c779 to
b265574
Compare
|
Rebased this onto main, as I attempt to clear out some of my (very) old PRs. |
|
Closing this PR, as it is unlikely that I will have a chance to get back to it. If anybody else would like to pick this up, the last roadblock was that the Rust allocator does not provide the higher alignment requirement that is later assumed that memory allocation, and doesn't have a clear way to provide the larger-than-element-type alignment. |
Prior to this commit, any use of
tvm.nd.from_dlpackto create a stridedNDArray, or aNDArraywhose alignment was less thantvm::runtime::kAllocAlignmentwould raise an error. As a result, views into larger arrays, which are unlikely to be aligned and compact, could only be shared when copied into an aligned and compact buffer.This commit moves the compact/aligned check from the
NDArrayclass into the generated TIR code as part of DLTensor unpacking. These checks were initially introduced in #11391, to avoid segfaults caused by use of non-aligned buffers in code intended for aligned buffers. The new checks will provide the same safeguard as the alignment is checked prior to use, but allows the alignment requirement to be relaxed on a per-buffer basis.This approach also removes a potential bug resulting from compile-time configuration of
tvm::runtime::kAllocAlignment, first introduced in #13307. Since TVM supports cross-compiling, the installation of TVM used to compile a kernel may assume a larger value ofkAllocAlignmentthan is provided by the runtime installation of TVM. By validating the alignment within the generated kernel, rather than as part of the runtime, this potential inconsistency would be caught.