Skip to content

Conversation

@yaoyaoding
Copy link
Member

@yaoyaoding yaoyaoding commented Oct 3, 2025

This PR makes the generated kernel library (lib.so) satisfy the ABI of tvm ffi (https://github.com/apache/tvm-ffi). Also use tvm-ffi package to load and launch the kernel. Previously, we use ctypes to run a function which is slower than tvm-ffi.

  • Use a distinct class void_p to replace void* so that we can allow passing Tensor/Integer directly.
  • Update the codegen to include tvm/ffi and add registration code.
  • Update the runtime part to use tvm_ffi to load the compiled shared library.
  • Define some TypeTraits for pointer types like int*, half*, etc to accept the corresponding tensors.

@copy-pr-bot
Copy link

copy-pr-bot bot commented Oct 3, 2025

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

Signed-off-by: Yaoyao Ding <dingyaoyao.cs@gmail.com>
Signed-off-by: Yaoyao Ding <dingyaoyao.cs@gmail.com>
Signed-off-by: Yaoyao Ding <dingyaoyao.cs@gmail.com>
Signed-off-by: Yaoyao Ding <dingyaoyao.cs@gmail.com>
Signed-off-by: Yaoyao Ding <dingyaoyao.cs@gmail.com>
Signed-off-by: Yaoyao Ding <dingyaoyao.cs@gmail.com>
@yaoyaoding
Copy link
Member Author

/ok to test 0134c50

Signed-off-by: Yaoyao Ding <dingyaoyao.cs@gmail.com>
@yaoyaoding
Copy link
Member Author

/ok to test a617e0e

@yaoyaoding yaoyaoding requested a review from Copilot October 3, 2025 21:25
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR migrates the generated kernel library to use TVM-FFI spec instead of ctypes for better performance. The changes replace the existing runtime loading mechanism with TVM-FFI and introduce type safety for pointer operations.

  • Replaced ctypes-based function loading with TVM-FFI for improved performance
  • Introduced void_p class for type-safe pointer handling
  • Added TVM-FFI type traits for various pointer types with automatic tensor conversion

Reviewed Changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/conftest.py Disabled cache clearing for parallel test execution
python/tilus/testing/requires.py Moved pytest import to local scope
python/tilus/runtime/compiled_program.py Replaced hidet CompiledModule with TVM-FFI module loading
python/tilus/lang/instantiated_script.py Updated function signatures to use TVM-FFI Function type
python/tilus/extensions/hidet/libinfo.py Added library info helper for include paths
python/tilus/extensions/hidet/include/hidet/void_p.h Defined void_p class for type-safe pointer operations
python/tilus/extensions/hidet/include/hidet/tvm/ffi/extra_type_traits.h Added TVM-FFI type traits for pointer types
python/tilus/extensions/hidet/backend/codegen.py Updated code generation to include TVM-FFI headers and registration
python/tilus/extensions/hidet/backend/build.py Added TVM-FFI include paths to compilation
python/tilus/extensions/hidet/init.py Exposed libinfo module
pyproject.toml Added apache-tvm-ffi dependency and package data
.vscode/settings.json Added pytest configuration

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.


TVM_FFI_INLINE static UInt* ConvertFallbackValue(DLTensor* src) {
if ((src->dtype.code != kDLUInt || src->dtype.bits != sizeof(UInt) * 8)
&& (src->dtype.code != kDLBool || src->dtype.bits != 8)
Copy link

Copilot AI Oct 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition for kDLBool is checking for 8 bits regardless of the UInt type size. This creates inconsistent behavior where bool tensors can be converted to any unsigned integer pointer type regardless of size. Consider adding a size check: && (src->dtype.code != kDLBool || src->dtype.bits != 8 || sizeof(UInt) != 1)

Suggested change
&& (src->dtype.code != kDLBool || src->dtype.bits != 8)
&& (src->dtype.code != kDLBool || src->dtype.bits != 8 || sizeof(UInt) != 1)

Copilot uses AI. Check for mistakes.
void_p(T *ptr) : internal_ptr(static_cast<void *>(ptr)) {}

// 3. Implicit template conversion operator (Allows implicit conversion back to ANY T*) This allows: int* ip = p;
// (where p holds an int*)
Copy link

Copilot AI Oct 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implicit conversion operator lacks documentation about potential type safety risks. Consider adding a comment explaining that this performs unchecked casting and the caller is responsible for ensuring type compatibility.

Suggested change
// (where p holds an int*)
// (where p holds an int*)
// WARNING: This operator performs an unchecked cast from the stored void* to the requested pointer type.
// The caller is responsible for ensuring that the type T matches the original type of the stored pointer.
// Using an incorrect type may result in undefined behavior.

Copilot uses AI. Check for mistakes.
@yaoyaoding yaoyaoding merged commit 8efbb10 into main Oct 3, 2025
9 checks passed
@yaoyaoding yaoyaoding mentioned this pull request Oct 4, 2025
17 tasks
@yaoyaoding yaoyaoding deleted the tvm-ffi branch October 4, 2025 01:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants