Compile, build and load a C/C++/CUDA module from source files. This function compiles the given source files into a shared library and loads it as a tvm ffi module. The compiler is selected automatically based on file extension. Note that this function does not automatically export fun
( # noqa: PLR0913
name: str,
*,
sources: Sequence[str] | str | None = None,
cpp_files: Sequence[str] | str | None = None,
cuda_files: Sequence[str] | str | None = None,
extra_cflags: Sequence[str] | None = None,
extra_cuda_cflags: Sequence[str] | None = None,
extra_ldflags: Sequence[str] | None = None,
extra_include_paths: Sequence[str] | None = None,
build_directory: str | None = None,
keep_module_alive: bool = True,
backend: str | None = None,
)
| 1321 | |
| 1322 | |
| 1323 | def load( # noqa: PLR0913 |
| 1324 | name: str, |
| 1325 | *, |
| 1326 | sources: Sequence[str] | str | None = None, |
| 1327 | cpp_files: Sequence[str] | str | None = None, |
| 1328 | cuda_files: Sequence[str] | str | None = None, |
| 1329 | extra_cflags: Sequence[str] | None = None, |
| 1330 | extra_cuda_cflags: Sequence[str] | None = None, |
| 1331 | extra_ldflags: Sequence[str] | None = None, |
| 1332 | extra_include_paths: Sequence[str] | None = None, |
| 1333 | build_directory: str | None = None, |
| 1334 | keep_module_alive: bool = True, |
| 1335 | backend: str | None = None, |
| 1336 | ) -> Module: |
| 1337 | """Compile, build and load a C/C++/CUDA module from source files. |
| 1338 | |
| 1339 | This function compiles the given source files into a shared library and loads it as a tvm ffi |
| 1340 | module. The compiler is selected automatically based on file extension. |
| 1341 | |
| 1342 | Note that this function does not automatically export functions to the tvm ffi module. You need to |
| 1343 | manually use the TVM FFI export macros (e.g., :c:macro:`TVM_FFI_DLL_EXPORT_TYPED_FUNC`) in your source files to export |
| 1344 | functions. This gives you more control over which functions are exported and how they are exported. |
| 1345 | |
| 1346 | Extra compiler and linker flags can be provided via the ``extra_cflags``, ``extra_cuda_cflags``, and ``extra_ldflags`` |
| 1347 | parameters. The default flags are generally sufficient for most use cases, but you may need to provide additional |
| 1348 | flags for your specific use case. |
| 1349 | |
| 1350 | The include dir of tvm ffi and dlpack are used by default for the compiler to find the headers. Thus, you can |
| 1351 | include any header from tvm ffi in your source files. You can also provide additional include paths via the |
| 1352 | ``extra_include_paths`` parameter and include custom headers in your source code. |
| 1353 | |
| 1354 | The compiled shared library is cached in a cache directory to avoid recompilation. The `build_directory` parameter |
| 1355 | is provided to specify the build directory. If not specified, a default tvm ffi cache directory will be used. |
| 1356 | The default cache directory can be specified via the `TVM_FFI_CACHE_DIR` environment variable. If not specified, |
| 1357 | the default cache directory is ``~/.cache/tvm-ffi``. |
| 1358 | |
| 1359 | Parameters |
| 1360 | ---------- |
| 1361 | name |
| 1362 | The name of the tvm ffi module. |
| 1363 | sources |
| 1364 | Source files to compile. The compiler is auto-detected from the file extension: |
| 1365 | ``.c`` → C, ``.cc``/``.cpp``/``.cxx`` → C++, ``.cu`` → CUDA/HIP, |
| 1366 | ``.o``/``.obj`` → linker passthrough. It can be a list of file paths or a single file path. |
| 1367 | cpp_files |
| 1368 | Alias for ``sources``, kept for backward compatibility. |
| 1369 | cuda_files |
| 1370 | Alias for ``sources``, kept for backward compatibility. |
| 1371 | extra_cflags |
| 1372 | The extra compiler flags for C++ compilation. |
| 1373 | The default flags are: |
| 1374 | |
| 1375 | - On Linux/macOS: ['-std=c++17', '-fPIC', '-O2'] |
| 1376 | - On Windows: ['/std:c++17', '/MD', '/O2'] |
| 1377 | |
| 1378 | extra_cuda_cflags |
| 1379 | The extra compiler flags for CUDA compilation. |
| 1380 | The default flags are: |
nothing calls this directly
no test coverage detected