### Install Example Wheel with uv pip Source: https://github.com/apache/tvm-ffi/blob/main/examples/python_packaging/README.md Use `uv pip` to build and install the example wheel. Ensure you are in the correct directory. ```bash cd examples/python_packaging uv pip install --reinstall --verbose . ``` -------------------------------- ### Run the Example Script Source: https://github.com/apache/tvm-ffi/blob/main/examples/python_packaging/README.md Execute the `run_example.py` script after installing the `my_ffi_extension` package to test various functionalities. ```bash python run_example.py ``` -------------------------------- ### Run Library Loading Example Source: https://github.com/apache/tvm-ffi/blob/main/rust/README.md Execute an example demonstrating library loading with Cargo, enabling the 'example' feature. ```bash cargo run --example load_library --features example ``` -------------------------------- ### Build and install TVM-FFI OrcJIT from source Source: https://github.com/apache/tvm-ffi/blob/main/addons/tvm_ffi_orcjit/README.md Clone the repository, install the base tvm-ffi package, and then build and install the orcjit addon. ```bash git clone --recursive https://github.com/apache/tvm-ffi.git cd tvm-ffi # Install tvm-ffi first pip install -e . # Build and install the orcjit addon cd addons/tvm_ffi_orcjit pip install -e . ``` -------------------------------- ### Run Quick-Start Example Source: https://github.com/apache/tvm-ffi/blob/main/addons/tvm_ffi_orcjit/tests/README.md Execute the quick-start example script, specifying the target language as C. ```bash python examples/quick-start/run.py --lang c ``` -------------------------------- ### End-to-End Example Initialization Source: https://github.com/apache/tvm-ffi/blob/main/addons/tvm_ffi_orcjit/ORCJIT_PRIMER.md Initializes the ORCJIT library for use in an end-to-end example. ```python import tvm_ffi_orcjit as oj ``` -------------------------------- ### Run All Examples (Bash) Source: https://github.com/apache/tvm-ffi/blob/main/examples/stable_c_abi/README.md Execute all provided examples using the run_all.sh script. This is the simplest way to run all demos. ```bash bash run_all.sh ``` -------------------------------- ### Install and Install Git Hooks for Pre-commit Source: https://github.com/apache/tvm-ffi/blob/main/docs/dev/ci_cd.md Install the pre-commit tool and register the git hooks to automatically run linters and formatters before each commit. ```bash uv tool install pre-commit pre-commit install ``` -------------------------------- ### Run All Examples (Batch) Source: https://github.com/apache/tvm-ffi/blob/main/examples/stable_c_abi/README.md Execute all provided examples using the run_all.bat script. This is the simplest way to run all demos on Windows. ```batch run_all.bat ``` -------------------------------- ### Install tvm_ffi_python_helpers.h Source: https://github.com/apache/tvm-ffi/blob/main/CMakeLists.txt Installs the tvm_ffi_python_helpers.h header file to the include directory. ```cmake install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/python/tvm_ffi/cython/tvm_ffi_python_helpers.h DESTINATION include/ ) ``` -------------------------------- ### Run All CPU Examples Source: https://github.com/apache/tvm-ffi/blob/main/examples/quickstart/README.md Execute all CPU-related examples using a shell script. This is the quickest way to test the CPU path. ```bash bash run_all_cpu.sh ``` ```batch run_all_cpu.bat ``` -------------------------------- ### Compile and Run C++ Example Source: https://github.com/apache/tvm-ffi/blob/main/docs/get_started/quickstart.md Provides the necessary compilation and execution commands for the C++ TVM-FFI example. Ensure tvm-ffi-config is in your PATH. ```bash g++ -fvisibility=hidden -O3 \ load/load_cpp.cc \ $(tvm-ffi-config --cxxflags) \ $(tvm-ffi-config --ldflags) \ $(tvm-ffi-config --libs) \ -Wl,-rpath,$(tvm-ffi-config --libdir) \ -o build/load_cpp build/load_cpp ``` -------------------------------- ### Install JAX-TVM-FFI Source: https://github.com/apache/tvm-ffi/blob/main/docs/get_started/quickstart.md Install the necessary package to integrate TVM-FFI with JAX. This is a prerequisite for using JAX-TVM-FFI. ```bash pip install jax-tvm-ffi ``` -------------------------------- ### Install Rust Toolchain Source: https://github.com/apache/tvm-ffi/blob/main/docs/dev/doc_build.md Installs the Rust toolchain using rustup, a prerequisite for generating Rust documentation. ```bash curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh ``` -------------------------------- ### Run Example: Dynamic CUBIN Loading Source: https://github.com/apache/tvm-ffi/blob/main/examples/cubin_launcher/README.md Builds and runs an example that loads CUBIN data from a file at runtime using the CUDA Driver API. This offers more flexibility for swapping CUBIN files. ```bash cd examples/cubin_launcher/dynamic_cubin mkdir build && cd build cmake .. make cd .. python main.py ``` -------------------------------- ### Load C++ Example with CMake Source: https://github.com/apache/tvm-ffi/blob/main/examples/quickstart/README.md Compile and run the C++ library loading example using CMake. The executable is generated in the build directory. ```bash cmake . -B build -DEXAMPLE_NAME="load_cpp" -DCMAKE_BUILD_TYPE=RelWithDebInfo cmake --build build --config RelWithDebInfo build/load_cpp ``` -------------------------------- ### Install TVM FFI source and configuration files Source: https://github.com/apache/tvm-ffi/blob/main/CMakeLists.txt Installs source files from src/ffi/, utility CMake files, the main CMakeLists.txt, and the tvm_ffi-config.cmake file. ```cmake install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src/ffi/ DESTINATION src/ffi/) ``` ```cmake install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Utils/ DESTINATION share/cmake/tvm_ffi/Utils) ``` ```cmake install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt DESTINATION .) ``` ```cmake install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/cmake/tvm_ffi-config.cmake DESTINATION share/cmake/tvm_ffi ) ``` -------------------------------- ### Run Example: Embedded CUBIN with Header Inclusion Source: https://github.com/apache/tvm-ffi/blob/main/examples/cubin_launcher/README.md Builds and runs an example that converts CUBIN to a C header file using bin2c for portability. This method works with any compiler. ```bash cd examples/cubin_launcher/embedded_cubin/include_bin2c mkdir build && cd build cmake .. make cd .. python main.py ``` -------------------------------- ### Install tvm_ffi_cython target Source: https://github.com/apache/tvm-ffi/blob/main/CMakeLists.txt Installs the tvm_ffi_cython target to the root destination. ```cmake install(TARGETS tvm_ffi_cython DESTINATION .) ``` -------------------------------- ### Load Distributed C Example with CMake Source: https://github.com/apache/tvm-ffi/blob/main/examples/stable_c_abi/README.md Compile and run a C example that loads a distributed library using CMake. This demonstrates dynamic library loading. ```bash cmake . -B build -DEXAMPLE_NAME="load" -DCMAKE_BUILD_TYPE=RelWithDebInfo cmake --build build --config RelWithDebInfo build/load ``` -------------------------------- ### Run TVM FFI Example Source: https://github.com/apache/tvm-ffi/blob/main/docs/guides/rust_lang_guide.md Execute the example provided in the `rust/tvm-ffi/examples/load_library.rs` file. This requires navigating to the rust directory and using cargo run. ```bash cd rust cargo run --example load_library --features example ``` -------------------------------- ### Run All CUDA Examples Source: https://github.com/apache/tvm-ffi/blob/main/examples/quickstart/README.md Execute all CUDA-related examples using a shell script. This is used for end-to-end testing of the CUDA path. ```bash bash run_all_cuda.sh ``` -------------------------------- ### Install Project and Run Python Tests Source: https://github.com/apache/tvm-ffi/blob/main/docs/dev/ci_cd.md Install the project in editable mode with test dependencies and then execute the full Python test suite using pytest. ```bash # Install the project in editable mode with test dependencies uv pip install --reinstall --verbose --group test -e . # Run the full test suite uv run pytest -vvs tests/python ``` -------------------------------- ### Install TVM-FFI OrcJIT from PyPI Source: https://github.com/apache/tvm-ffi/blob/main/addons/tvm_ffi_orcjit/README.md Install the TVM-FFI OrcJIT package and its dependencies using pip. ```bash pip install apache-tvm-ffi apache-tvm_ffi_orcjit ``` -------------------------------- ### Install 3rdparty directories Source: https://github.com/apache/tvm-ffi/blob/main/CMakeLists.txt Installs the include directories for dlpack and libbacktrace, excluding .git and .tmp files from libbacktrace. ```cmake install( DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/libbacktrace/ DESTINATION 3rdparty/libbacktrace/ PATTERN ".git" EXCLUDE PATTERN ".git*" EXCLUDE PATTERN "*.tmp" EXCLUDE ) ``` ```cmake install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/dlpack/include/ DESTINATION 3rdparty/dlpack/include/ ) ``` -------------------------------- ### Compile CPU Example with CMake Source: https://github.com/apache/tvm-ffi/blob/main/examples/quickstart/README.md Compile the C++ 'add_one_cpu' example using CMake. This generates a shared library for CPU execution. ```bash cmake . -B build -DEXAMPLE_NAME="compile_cpu" -DCMAKE_BUILD_TYPE=RelWithDebInfo cmake --build build --config RelWithDebInfo ``` -------------------------------- ### Run Example: Embedded CUBIN with C++23 #embed Source: https://github.com/apache/tvm-ffi/blob/main/examples/cubin_launcher/README.md Builds and runs an example using C++23 #embed for direct binary data inclusion. This is a clean approach for modern toolchains. ```bash cd examples/cubin_launcher/embedded_cubin/cpp_embed mkdir build && cd build cmake .. make cd .. python main.py ``` -------------------------------- ### Install Doxygen for C++ Docs Source: https://github.com/apache/tvm-ffi/blob/main/docs/dev/doc_build.md Installs Doxygen, a prerequisite for generating C++ reference documentation. Use the appropriate command for your operating system. ```bash brew install doxygen # macOS ``` ```bash sudo apt install doxygen # Linux ``` -------------------------------- ### Install TVM FFI OrcJIT Library Source: https://github.com/apache/tvm-ffi/blob/main/addons/tvm_ffi_orcjit/CMakeLists.txt Installs the tvm_ffi_orcjit target as a library, archive, and runtime component in the lib directory. ```cmake install( TARGETS tvm_ffi_orcjit LIBRARY DESTINATION lib ARCHIVE DESTINATION lib RUNTIME DESTINATION lib ) ``` -------------------------------- ### Install TVM FFI headers and shared library Source: https://github.com/apache/tvm-ffi/blob/main/CMakeLists.txt Installs the TVM FFI header files and the tvm_ffi_shared library to their respective destinations. ```cmake install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/tvm/ffi/ DESTINATION include/tvm/ffi/) ``` ```cmake install(TARGETS tvm_ffi_shared DESTINATION lib) ``` -------------------------------- ### Install and Run Python Tests Source: https://github.com/apache/tvm-ffi/blob/main/AGENTS.md Installs Python test dependencies and runs pytest for the TVM FFI project. ```bash uv pip install --force-reinstall --verbose --group test -e . uv run pytest -vvs tests/python ``` -------------------------------- ### Install Torch C DLPack Extension Source: https://github.com/apache/tvm-ffi/blob/main/addons/torch_c_dlpack_ext/README.md Install the package using pip. This allows users to avoid JIT compilation overhead. ```bash pip install torch-c-dlpack-ext ``` -------------------------------- ### Configure Loading C++ Shared Library Example Source: https://github.com/apache/tvm-ffi/blob/main/examples/quickstart/CMakeLists.txt Creates an executable that loads a C++ shared library. Links against TVM FFI headers and shared libraries. ```cmake # Example 3. Load C++ shared library add_executable(load_cpp load/load_cpp.cc) target_link_libraries(load_cpp PRIVATE tvm_ffi::header tvm_ffi::shared) set_flat_output_dirs(load_cpp) ``` -------------------------------- ### Compile CUDA Example with CMake Source: https://github.com/apache/tvm-ffi/blob/main/examples/quickstart/README.md Compile the CUDA example using CMake. Requires a CUDA toolchain to be available on Linux. ```bash cmake . -B build -DEXAMPLE_NAME="compile_cuda" -DCMAKE_BUILD_TYPE=RelWithDebInfo cmake --build build --config RelWithDebInfo ``` -------------------------------- ### Build Loader with CMake Source: https://github.com/apache/tvm-ffi/blob/main/docs/get_started/stable_c_abi.md Configures and builds the project using CMake, specifying the example name and build type. ```bash cmake . -B build -DEXAMPLE_NAME="load" -DCMAKE_BUILD_TYPE=RelWithDebInfo cmake --build build --config RelWithDebInfo build/load ``` -------------------------------- ### Build Python Wheels Locally Source: https://github.com/apache/tvm-ffi/blob/main/docs/dev/ci_cd.md Install cibuildwheel and use it to build Python wheels for the project, outputting them to the 'dist' directory. ```bash uv tool install cibuildwheel cibuildwheel --output-dir dist ``` -------------------------------- ### Build `add_one_cpu` Kernel with CMake Source: https://github.com/apache/tvm-ffi/blob/main/docs/get_started/stable_c_abi.md Builds the `add_one_cpu` kernel using CMake, specifying the example name and build configuration. ```bash cmake . -B build -DEXAMPLE_NAME="kernel" -DCMAKE_BUILD_TYPE=RelWithDebInfo cmake --build build --config RelWithDebInfo ``` -------------------------------- ### Configure CPU Compilation Example Source: https://github.com/apache/tvm-ffi/blob/main/examples/quickstart/CMakeLists.txt Builds a shared library for a C++ 'add_one' function targeting the CPU. Links against TVM FFI headers and shared libraries. ```cmake # Example 1. C++ `add_one` add_library(add_one_cpu SHARED compile/add_one_cpu.cc) target_link_libraries(add_one_cpu PRIVATE tvm_ffi::header tvm_ffi::shared) set_target_properties(add_one_cpu PROPERTIES PREFIX "" SUFFIX ".so") set_flat_output_dirs(add_one_cpu) ``` -------------------------------- ### Compile C Example with CMake Source: https://github.com/apache/tvm-ffi/blob/main/examples/stable_c_abi/README.md Compile a C example named 'kernel' using CMake. This command generates build artifacts, including a shared library. ```bash cmake . -B build -DEXAMPLE_NAME="kernel" -DCMAKE_BUILD_TYPE=RelWithDebInfo cmake --build build --config RelWithDebInfo ``` -------------------------------- ### Run Example: Embedded CUBIN with Object Linking Source: https://github.com/apache/tvm-ffi/blob/main/examples/cubin_launcher/README.md Builds and runs an example that embeds CUBIN data directly into a shared library at build time using CMake. This is the most robust method for CMake projects. ```bash cd examples/cubin_launcher/embedded_cubin/embed_with_tvm_ffi mkdir build && cd build cmake .. make cd .. python main.py ``` -------------------------------- ### Install LLVM and dependencies via conda-forge Source: https://github.com/apache/tvm-ffi/blob/main/addons/tvm_ffi_orcjit/README.md Set up LLVM development libraries and static zlib/zstd using conda-forge. This is the recommended way to satisfy build prerequisites. ```bash conda create -p /opt/llvm -c conda-forge \ llvmdev=22.1.0 clangdev=22.1.0 compiler-rt=22.1.0 zlib zstd-static -y export LLVM_PREFIX=/opt/llvm ``` ```cmd conda create -p C:\opt\llvm -c conda-forge llvmdev=22.1.0 zlib zstd-static -y set LLVM_PREFIX=C:\opt\llvm ``` -------------------------------- ### CMake Project Setup Source: https://github.com/apache/tvm-ffi/blob/main/addons/tvm_ffi_orcjit/examples/quick-start/CMakeLists.txt Basic CMake configuration for a C++ project, setting the C++ standard and position-independent code. ```cmake cmake_minimum_required(VERSION 3.18) project(tvm_ffi_orcjit_example) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_POSITION_INDEPENDENT_CODE ON) ``` -------------------------------- ### Install tvm_ffi_testing Target Conditionally Source: https://github.com/apache/tvm-ffi/blob/main/CMakeLists.txt Installs the 'tvm_ffi_testing' target to the 'lib' directory if it is defined. This is an optional installation. ```cmake if (TARGET tvm_ffi_testing) install( TARGETS tvm_ffi_testing DESTINATION lib OPTIONAL ) endif () ``` -------------------------------- ### Configure CUDA Compilation Example Source: https://github.com/apache/tvm-ffi/blob/main/examples/quickstart/CMakeLists.txt Builds a shared library for a CUDA 'add_one' function. Requires CUDA to be enabled and links against TVM FFI headers and shared libraries. ```cmake # Example 2. CUDA `add_one` enable_language(CUDA) add_library(add_one_cuda SHARED compile/add_one_cuda.cu) target_link_libraries(add_one_cuda PRIVATE tvm_ffi::header tvm_ffi::shared) set_target_properties(add_one_cuda PROPERTIES PREFIX "" SUFFIX ".so") set_flat_output_dirs(add_one_cuda) ``` -------------------------------- ### Install tvm_ffi_static Target Conditionally Source: https://github.com/apache/tvm-ffi/blob/main/CMakeLists.txt Installs the 'tvm_ffi_static' target to the 'lib' directory if 'TVM_FFI_BUILD_PYTHON_MODULE' is not defined. This is an optional installation, typically skipped when building wheels. ```cmake if (NOT TVM_FFI_BUILD_PYTHON_MODULE) install( TARGETS tvm_ffi_static DESTINATION lib OPTIONAL ) endif () ``` -------------------------------- ### Install dSYM Files on Apple Platforms Source: https://github.com/apache/tvm-ffi/blob/main/CMakeLists.txt Installs '.dSYM' files from the build's 'lib' directory to the installation 'lib' directory when on an Apple platform. This is used for debugging symbols. ```cmake if (APPLE) install( DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib/ DESTINATION lib FILES_MATCHING PATTERN "*.dSYM" ) endif () ``` -------------------------------- ### Install TVM-FFI Python Package Source: https://github.com/apache/tvm-ffi/blob/main/docs/dev/source_build.md Install the TVM-FFI Python package using uv pip. The --force-reinstall flag ensures a rebuild, and -e installs in editable mode for immediate reflection of Python-only changes. ```bash uv pip install --force-reinstall --verbose -e . ``` -------------------------------- ### Multi-GPU Kernel Launch Example Source: https://github.com/apache/tvm-ffi/blob/main/docs/guides/cubin_launcher.md Demonstrates launching the same CUBIN kernel on multiple GPUs using TVM FFI. The kernel automatically uses the device context from input tensors. ```cpp void MultiGPUExample(tvm::ffi::TensorView x_gpu0, tvm::ffi::TensorView x_gpu1) { static auto kernel = TVM_FFI_EMBED_CUBIN_GET_KERNEL(my_kernels, "process"); // Launch on GPU 0 (device determined by x_gpu0.device()) LaunchOnDevice(kernel, x_gpu0); // Launch on GPU 1 (device determined by x_gpu1.device()) LaunchOnDevice(kernel, x_gpu1); } ``` -------------------------------- ### Basic TVM-FFI OrcJIT Execution Session Source: https://github.com/apache/tvm-ffi/blob/main/addons/tvm_ffi_orcjit/README.md Demonstrates creating an execution session, a dynamic library, loading an object file, and calling a function. ```python from tvm_ffi_orcjit import ExecutionSession # Create an execution session session = ExecutionSession() # Create a dynamic library lib = session.create_library() # Load an object file lib.add("example.o") # Get and call a function add_func = lib.get_function("add") result = add_func(1, 2) print(f"Result: {result}") # Output: Result: 3 ``` -------------------------------- ### Install TVM-FFI Source: https://github.com/apache/tvm-ffi/blob/main/README.md Install TVM-FFI using pip. A compatibility package is available for older PyTorch versions. ```bash pip install apache-tvm-ffi pip install torch-c-dlpack-ext # compatibility package for torch <= 2.9 ``` -------------------------------- ### Install TVM-FFI Target Artifacts in CMake Source: https://github.com/apache/tvm-ffi/blob/main/docs/packaging/cpp_tooling.md Installs platform-specific artifacts for a target, such as .dSYM bundles on Apple platforms. ```cmake tvm_ffi_install( [DESTINATION ] ) ``` -------------------------------- ### Example of Any and AnyView Usage in C++ Source: https://github.com/apache/tvm-ffi/blob/main/docs/guides/cpp_lang_guide.md Demonstrates creating `Any` objects from various types, using `AnyView` for lightweight access, and casting or safely retrieving values using `cast`, `as`, and `try_cast`. `EXPECT_EQ` is used for testing purposes. ```cpp #include void ExampleAny() { namespace ffi = tvm::ffi; // Create an Any from various types // EXPECT_EQ is used here for demonstration purposes (testing framework) ffi::Any int_value = 42; ffi::Any float_value = 3.14; ffi::Any string_value = "hello world"; // AnyView provides a lightweight view without ownership ffi::AnyView view = int_value; // we can cast Any/AnyView to a specific type int extracted = view.cast(); EXPECT_EQ(extracted, 42); // If we are not sure about the type // we can use as to get an optional value std::optional maybe_int = view.as(); if (maybe_int.has_value()) { EXPECT_EQ(maybe_int.value(), 42); } // Try cast is another version that will try to run the type // conversion even if the type does not exactly match std::optional maybe_int_try = view.try_cast(); if (maybe_int_try.has_value()) { EXPECT_EQ(maybe_int_try.value(), 42); } } ``` -------------------------------- ### One-Off Build with All Docs Source: https://github.com/apache/tvm-ffi/blob/main/docs/dev/doc_build.md Performs a one-off build of all documentation, including C++ and Rust references. ```bash BUILD_CPP_DOCS=1 BUILD_RUST_DOCS=1 uv run --group docs sphinx-build \ -M html docs docs/_build ``` -------------------------------- ### Interactive Documentation Build Source: https://github.com/apache/tvm-ffi/blob/main/docs/dev/doc_build.md Rebuilds and serves the documentation locally with live reload. Open http://127.0.0.1:8000 in your browser after the initial build. ```bash uv run --group docs sphinx-autobuild docs docs/_build/html \ --ignore docs/reference/cpp/generated ``` -------------------------------- ### Verify TVM-FFI Python Installation Source: https://github.com/apache/tvm-ffi/blob/main/docs/dev/source_build.md Verify the installation of the TVM-FFI Python package by checking its version and running the configuration tool. ```bash uv run python -c "import tvm_ffi; print(tvm_ffi.__version__)" uv run tvm-ffi-config -h ``` -------------------------------- ### One-Off Build with C++ Docs Source: https://github.com/apache/tvm-ffi/blob/main/docs/dev/doc_build.md Enables C++ documentation generation for a one-off build. ```bash BUILD_CPP_DOCS=1 uv run --group docs sphinx-build -M html docs docs/_build ``` -------------------------------- ### One-Off Documentation Build Source: https://github.com/apache/tvm-ffi/blob/main/docs/dev/doc_build.md Generates the HTML documentation once without running a server. ```bash uv run --group docs sphinx-build -M html docs docs/_build ``` -------------------------------- ### Set LLVM Install Prefix Source: https://github.com/apache/tvm-ffi/blob/main/addons/tvm_ffi_orcjit/README.md When encountering LLVM version mismatches, set the LLVM_PREFIX environment variable to the LLVM installation directory. ```bash export LLVM_PREFIX=/path/to/llvm ``` -------------------------------- ### Interactive Build with All Docs Source: https://github.com/apache/tvm-ffi/blob/main/docs/dev/doc_build.md Builds all documentation, including C++ and Rust references, with interactive auto-rebuild on header and source changes. ```bash BUILD_CPP_DOCS=1 BUILD_RUST_DOCS=1 uv run --group docs sphinx-autobuild \ docs docs/_build/html \ --ignore docs/reference/cpp/generated \ --ignore docs/reference/rust/generated \ --watch include --watch rust ``` -------------------------------- ### Install tvm-ffi Python Package Source: https://github.com/apache/tvm-ffi/blob/main/docs/guides/rust_lang_guide.md Install the tvm-ffi Python package to enable Rust support. This is a prerequisite for using TVM FFI from Rust. ```bash pip install -v -e . ``` -------------------------------- ### Main Function for FFI Execution Source: https://github.com/apache/tvm-ffi/blob/main/docs/get_started/stable_c_abi.md Sets up DLDevice, DLTensor, and executes a function via FFI, printing results. ```c int main() { int ret_code = 0; float x_data[5] = {1.0, 2.0, 3.0, 4.0, 5.0}; float y_data[5] = {0.0, 0.0, 0.0, 0.0, 0.0}; int64_t shape[1] = {5}; int64_t strides[1] = {1}; DLDataType f32 = {.code = kTVMFFIFloat, .bits = 32, .lanes = 1}; DLDevice cpu = {.device_type = kDLCPU, .device_id = 0}; DLTensor x = {// .data = x_data, .device = cpu, .ndim = 1, .dtype = f32, .shape = shape, .strides = strides, .byte_offset = 0}; DLTensor y = {// .data = y_data, .device = cpu, .ndim = 1, .dtype = f32, .shape = shape, .strides = strides, .byte_offset = 0}; if ((ret_code = Initialize())) goto _RAII; if ((ret_code = Run(&x, &y))) goto _RAII; printf("[ "); for (int i = 0; i < 5; ++i) printf("%f ", y_data[i]); printf("]\n"); _RAII: Finalize(ret_code); return ret_code; } ``` -------------------------------- ### Build `add_one_cpu` Kernel with Raw Command Source: https://github.com/apache/tvm-ffi/blob/main/docs/get_started/stable_c_abi.md Builds the `add_one_cpu` kernel as a shared library using raw GCC commands and TVM-FFI configuration flags. ```bash gcc -shared -O3 -std=c11 src/add_one_cpu.c \ -fPIC -fvisibility=hidden \ $(tvm-ffi-config --cflags) \ $(tvm-ffi-config --ldflags) \ $(tvm-ffi-config --libs) \ -o $BUILD_DIR/add_one_cpu.so ``` -------------------------------- ### One-Off Build with Rust Docs Source: https://github.com/apache/tvm-ffi/blob/main/docs/dev/doc_build.md Enables Rust documentation generation for a one-off build. ```bash BUILD_RUST_DOCS=1 uv run --group docs sphinx-build -M html docs docs/_build ``` -------------------------------- ### Install TVM-FFI Python Package with Debug Symbols Source: https://github.com/apache/tvm-ffi/blob/main/docs/dev/source_build.md Install the TVM-FFI Python package while enabling debug symbols by passing a CMake define. This is useful for debugging C++ code. ```bash uv pip install --force-reinstall --verbose -e \ --config-settings cmake.define.TVM_FFI_ATTACH_DEBUG_SYMBOLS=ON ``` -------------------------------- ### Python ORC JIT Session Setup (Python) Source: https://github.com/apache/tvm-ffi/blob/main/addons/tvm_ffi_orcjit/examples/quick-start/README.md This Python code initializes an ORC JIT execution session, creates a JITDylib, loads a compiled object file into it, retrieves a function symbol, and calls the function. ```python from tvm_ffi_orcjit import ExecutionSession session = ExecutionSession() # Create ORC JIT session lib = session.create_library() # Create a JITDylib lib.add("add_c.o") # Load object file into JIT add = lib.get_function("add") # Look up symbol print(add(10, 20)) # Call like a normal function → 30 ``` -------------------------------- ### Building Pure C Object File Source: https://github.com/apache/tvm-ffi/blob/main/addons/tvm_ffi_orcjit/examples/quick-start/CMakeLists.txt Builds a pure C source file into an object file using the `add_example_object` function. This is enabled on all platforms. ```cmake # Pure C object — built on all platforms enable_language(C) add_example_object(add_c add_c.c) ``` -------------------------------- ### Create and Access Map Elements Source: https://github.com/apache/tvm-ffi/blob/main/docs/guides/cpp_lang_guide.md Shows how to create a Map with string keys and integer values, and access elements using at() and checking for existence with count(). EXPECT_EQ is used for testing. ```cpp #include void ExampleMap() { namespace ffi = tvm::ffi; ffi::Map map0 = {{"Alice", 100}, {"Bob", 95}}; // EXPECT_EQ is used here for demonstration purposes (testing framework) EXPECT_EQ(map0.size(), 2); EXPECT_EQ(map0.at("Alice"), 100); EXPECT_EQ(map0.count("Alice"), 1); } ``` -------------------------------- ### DAG reverse direction check example Source: https://github.com/apache/tvm-ffi/blob/main/docs/concepts/structural_eq_hash.md Demonstrates the importance of the reverse check in DAG comparison to prevent false positives. This example shows how a bijective mapping correctly identifies inequality when the right-hand side reuses an object already paired. ```text lhs: (a, b) rhs: (a, a) where a ≅ b (same content) .0: a₁ ↔ a₂ → equal, record a₁ ↔ a₂ .1: b₁ ↔ a₂ → b₁ is new, but a₂ already paired with a₁ → NOT EQUAL ``` -------------------------------- ### Configure, Build, and Run C++ Tests Source: https://github.com/apache/tvm-ffi/blob/main/docs/dev/ci_cd.md Configure the build with tests enabled, build the test target, and then run the tests using ctest. Ensure CMAKE_BUILD_TYPE is set appropriately. ```bash # Configure with tests enabled cmake . -B build_test -DTVM_FFI_BUILD_TESTS=ON -DCMAKE_BUILD_TYPE=Debug # Build the test target cmake --build build_test --clean-first --config Debug --target tvm_ffi_tests # Run tests ctest -V -C Debug --test-dir build_test --output-on-failure ``` -------------------------------- ### Get and Use Global FFI Function Source: https://github.com/apache/tvm-ffi/blob/main/docs/guides/python_lang_guide.md Retrieves a globally registered C++ function 'testing.echo' and asserts its behavior. ```python import tvm_ffi # testing.echo is defined and registered in C++ # [](ffi::Any x) { return x; } fecho = tvm_ffi.get_global_func("testing.echo") assert fecho(1) == 1 ```