How to map a API function in backend in C++ into frontend in Python?

I am reading the TVM source, I am trying to figure out the convert process, for example, if I write a code like this: “a = tvm.placeholder(xxx)”, I know it will call API function(Tensor placeholder(Array shape, Type dtype, std::string name)) in libtvm.so, but how does it happen? could someone give me some hint about the process?

PS:
I used to call C function in shared lib in Python code using ctypes, but is it possible for C++? For some C++ functions, it will return class type, how does python code map it?

All these information are available as part of the Design and Developper Guide.
TVM uses PackedFunc to make calls to backends.

1 Like

Thanks, it really help a lot. But I still have a question, take ‘tvm.placeholder’ function for example, I notice that in frontend python code, it returns a Tensor object in python, but in the backend c++ implementation, it is a Tensor class object in c++, how does PackedFunc convert an object in c++ to an object in python?

C++ ctypes are also valid with an “extern” keyword before function declaration.
There are many examples when you google it, such as this link.

I think your question is just about ctypes and it’s well described in the link.

As nscotto explains it, TVM internally uses the trick and they called it as PackedFunc.
The name of C-native method called via TVM’s python interface typically starts with underbar, such as module._GetSource, module._LoadFromFile, etc.
And the name is registered by macro under a namespace, “module” in this case.
Find TVM_REGISTER_GLOBAL on C++ sources if you want to check it.
This registered method can be called like tvm.module._GetSource() in python due to help of codes in python/tvm/_ffi/function.py

1 Like