Skip to content

Conversation

@yaoyaoding
Copy link
Contributor

@yaoyaoding yaoyaoding commented Sep 6, 2025

This PR update the interface of ffi.load_inline to match torch.utils.cpp_extensions.load_inline:

  1. Rename cpp_source to cpp_sources, cuda_source to cuda_sources.
  2. Unify the cpp_functions and cuda_functions into functions.
  3. Add build_directory to allow the user to specify the build directory directly.

Example:

    mod: Module = tvm_ffi.cpp.load_inline(
        name="hello",
        cpp_sources=r"""
            void add_one_cpu(DLTensor* x, DLTensor* y) {
              // implementation of a library function
              TVM_FFI_ICHECK(x->ndim == 1) << "x must be a 1D tensor";
              DLDataType f32_dtype{kDLFloat, 32, 1};
              TVM_FFI_ICHECK(x->dtype == f32_dtype) << "x must be a float tensor";
              TVM_FFI_ICHECK(y->ndim == 1) << "y must be a 1D tensor";
              TVM_FFI_ICHECK(y->dtype == f32_dtype) << "y must be a float tensor";
              TVM_FFI_ICHECK(x->shape[0] == y->shape[0]) << "x and y must have the same shape";
              for (int i = 0; i < x->shape[0]; ++i) {
                static_cast<float*>(y->data)[i] = static_cast<float*>(x->data)[i] + 1;
              }
            }
        """,
        functions=["add_one_cpu"],
    )

    x = numpy.array([1, 2, 3, 4, 5], dtype=numpy.float32)
    y = numpy.empty_like(x)
    mod.add_one_cpu(x, y)
    numpy.testing.assert_equal(x + 1, y)

Copy link
Member

@tqchen tqchen left a comment

Choose a reason for hiding this comment

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

lgtm modulo lint

@tqchen tqchen merged commit d3a5811 into apache:main Sep 6, 2025
10 checks passed
tqchen pushed a commit to tqchen/tvm that referenced this pull request Sep 13, 2025
…e#18274)

This PR update the interface of ffi.load_inline to match torch.utils.cpp_extensions.load_inline:

- Rename cpp_source to cpp_sources, cuda_source to cuda_sources.
- Unify the cpp_functions and cuda_functions into functions.
- Add build_directory to allow the user to specify the build directory directly.
tqchen pushed a commit to tqchen/tvm that referenced this pull request Sep 13, 2025
…e#18274)

This PR update the interface of ffi.load_inline to match torch.utils.cpp_extensions.load_inline:

- Rename cpp_source to cpp_sources, cuda_source to cuda_sources.
- Unify the cpp_functions and cuda_functions into functions.
- Add build_directory to allow the user to specify the build directory directly.
tqchen pushed a commit to tqchen/tvm that referenced this pull request Sep 13, 2025
…e#18274)

This PR update the interface of ffi.load_inline to match torch.utils.cpp_extensions.load_inline:

- Rename cpp_source to cpp_sources, cuda_source to cuda_sources.
- Unify the cpp_functions and cuda_functions into functions.
- Add build_directory to allow the user to specify the build directory directly.
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