### Native Build Environment Setup Source: https://github.com/apache/tvm/blob/main/docs/README.md Set up your environment variables and install necessary Python packages for a native TVM documentation build. Ensure TVM is built and importable before proceeding. ```bash export TVM_HOME=/path-to-tvm export TVM_LIBRARY_PATH=$TVM_HOME/build pip install --target=$TVM_HOME/python $TVM_HOME/3rdparty/tvm-ffi export PYTHONPATH=$TVM_HOME/python:$PYTHONPATH ``` ```bash # Pillow on Ubuntu may require libjpeg-dev from apt ./docker/bash.sh ci_gpu -c \ 'python3 -m pip install --quiet tlcpack-sphinx-addon==0.2.1 && python3 -m pip freeze' > frozen-requirements.txt pip install -r frozen-requirements.txt ``` ```bash # TVM_TUTORIAL_EXEC_PATTERN=none skips the tutorial execution to the build # work on most environments (e.g. MacOS). export TVM_TUTORIAL_EXEC_PATTERN=none cd docs make html ``` ```bash cd _build/html && python3 -m http.server ``` -------------------------------- ### Discover Example NPU Patterns in TVM Source: https://github.com/apache/tvm/blob/main/python/tvm/relax/backend/contrib/example_npu/README.md Import the example NPU backend and use `get_patterns_with_prefix` to discover registered partitioning patterns. This helps in understanding which operations can be offloaded to the example NPU. ```python import tvm from tvm import relax from tvm.relax.backend.pattern_registry import get_patterns_with_prefix from tvm.relax.transform import FuseOpsByPattern, RunCodegen # Import to register patterns import tvm.relax.backend.contrib.example_npu # Get available patterns patterns = get_patterns_with_prefix("example_npu") print(f"Available patterns: {[p.name for p in patterns]}") ``` -------------------------------- ### Configure TVM with Example NPU Support Source: https://github.com/apache/tvm/blob/main/python/tvm/relax/backend/contrib/example_npu/README.md Enable example NPU codegen and runtime support during TVM's CMake configuration. This is required to build TVM with the necessary components for the example NPU backend. ```bash cmake -DUSE_EXAMPLE_NPU_CODEGEN=ON -DUSE_EXAMPLE_NPU_RUNTIME=ON .. ``` ```cmake set(USE_EXAMPLE_NPU_CODEGEN ON) set(USE_EXAMPLE_NPU_RUNTIME ON) ``` -------------------------------- ### Install System Dependencies (Ubuntu/Debian) Source: https://github.com/apache/tvm/blob/main/docs/install/from_source.rst Installs essential system libraries required for building TVM on Ubuntu/Debian-based systems using apt. ```bash sudo apt update sudo apt install zlib1g-dev libxml2-dev ``` -------------------------------- ### End-to-End BYOC Flow with Example NPU Source: https://github.com/apache/tvm/blob/main/python/tvm/relax/backend/contrib/example_npu/README.md Demonstrates the complete BYOC flow: partitioning operations using registered patterns, merging them into composite functions, and running the codegen for the example NPU backend. This example uses a simple Matmul followed by ReLU. ```python import tvm from tvm import relax from tvm.script import relax as R from tvm.relax.backend.pattern_registry import get_patterns_with_prefix from tvm.relax.transform import FuseOpsByPattern, MergeCompositeFunctions, RunCodegen import tvm.relax.backend.contrib.example_npu # registers patterns @tvm.script.ir_module class MatmulReLU: @R.function def main( x: R.Tensor((2, 4), "float32"), w: R.Tensor((4, 8), "float32"), ) -> R.Tensor((2, 8), "float32"): with R.dataflow(): y = relax.op.matmul(x, w) z = relax.op.nn.relu(y) R.output(z) return z mod = MatmulReLU patterns = get_patterns_with_prefix("example_npu") # Apply partitioning and codegen annotation mod = FuseOpsByPattern(patterns, bind_constants=False, annotate_codegen=True)(mod) mod = MergeCompositeFunctions()(mod) mod = RunCodegen()(mod) print(mod) ``` -------------------------------- ### Install usbmuxd using Homebrew Source: https://github.com/apache/tvm/blob/main/apps/ios_rpc/README.md Install the usbmuxd utility on your system using Homebrew. This is a prerequisite for using iproxy to manage device ports. ```shell brew install usbmuxd ``` -------------------------------- ### Install Essential TVM Metadata Files with CMake Source: https://github.com/apache/tvm/blob/main/CMakeLists.txt Installs essential metadata files such as README, LICENSE, and NOTICE to the root installation directory. These files are important for users to understand and comply with licensing and project information. ```cmake install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/README.md" "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE" "${CMAKE_CURRENT_SOURCE_DIR}/NOTICE" DESTINATION "." ) ``` -------------------------------- ### Install tvm-ffi package Source: https://github.com/apache/tvm/blob/main/docs/install/from_source.rst Installs the tvm-ffi package, which is required for TVM's Python bindings, after the main build is complete. Navigate to the tvm-ffi directory before installation. ```bash cd 3rdparty/tvm-ffi; pip install .; cd .. ``` -------------------------------- ### Run Example NPU Tests Source: https://github.com/apache/tvm/blob/main/python/tvm/relax/backend/contrib/example_npu/README.md Execute the test suite for the example NPU backend using pytest. Ensure the backend is built into TVM, otherwise tests will be skipped. ```bash pytest tests/python/contrib/test_example_npu.py -v ``` -------------------------------- ### Start the TVM RPC Server Source: https://github.com/apache/tvm/blob/main/apps/cpp_rpc/README.md Execute the tvm_rpc server binary to start the RPC service. Various command-line arguments can be used to configure its behavior. ```bash ./tvm_rpc server --host=0.0.0.0 --port=9000 --port-end=9090 --tracker=127.0.0.1:9190 --key=rasp ``` -------------------------------- ### Start WebSocket RPC Server (NodeJS) Source: https://github.com/apache/tvm/blob/main/web/README.md Starts the WebSocket RPC server in a NodeJS environment. This allows for remote execution and testing. ```bash npm run rpc ``` -------------------------------- ### Start JVM RPC Server with Proxy Source: https://github.com/apache/tvm/blob/main/jvm/README.md Starts an RPC server that communicates through a proxy, useful for managing connections and routing requests. ```java Server server = new Server(proxyHost, proxyPort, "key"); server.start(); ``` -------------------------------- ### Install TVM Headers Source: https://github.com/apache/tvm/blob/main/CMakeLists.txt Configures the installation of TVM header files. It conditionally installs all header files from the 'include/' directory or only those from 'include/tvm/runtime/' based on the 'INSTALL_DEV' variable. ```cmake if (INSTALL_DEV) install( DIRECTORY "include/" DESTINATION "include" FILES_MATCHING PATTERN "*.h" ) else(INSTALL_DEV) install( DIRECTORY "include/tvm/runtime/" DESTINATION "include/tvm/runtime" FILES_MATCHING PATTERN "*.h" ) endif(INSTALL_DEV) ``` -------------------------------- ### Full Docs Build with Tutorial Execution Source: https://github.com/apache/tvm/blob/main/docs/README.md Perform a full documentation build that includes the execution of all tutorials. This command ensures that tutorials are rendered correctly and all examples are validated. ```bash python tests/scripts/ci.py docs --full ``` -------------------------------- ### Start RPC Tracker Source: https://github.com/apache/tvm/blob/main/apps/ios_rpc/README.md Launch the RPC tracker on the host machine. The tracker manages RPC servers and allows clients to discover and connect to them. ```shell python3 -m tvm.exec.rpc_tracker --host 0.0.0.0 --port 9190 ``` -------------------------------- ### Install TVM Licenses with CMake Source: https://github.com/apache/tvm/blob/main/CMakeLists.txt Installs the license files for the TVM project. This is a mandatory step for distributing the software. ```cmake install( DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/licenses/" DESTINATION "licenses/" ) ``` -------------------------------- ### Install TVM via Environment Variable Source: https://github.com/apache/tvm/blob/main/docs/install/from_source.rst Installs the built TVM into your Python environment by setting the TVM_HOME environment variable and using pip to install the Python package and tvm-ffi. ```bash export TVM_HOME=/path-to-tvm pip install --target=$TVM_HOME/python $TVM_HOME/3rdparty/tvm-ffi export PYTHONPATH=$TVM_HOME/python:$PYTHONPATH ``` -------------------------------- ### Android RPC Test Output Example Source: https://github.com/apache/tvm/blob/main/apps/android_rpc/README.md Example output from running the Android RPC test script, showing performance metrics for CPU, OpenCL, and Vulkan targets, along with potential Vulkan driver incompatibility errors. ```bash Run CPU test ... 0.000962932 secs/op Run GPU(OpenCL Flavor) test ... 0.000155807 secs/op [23:29:34] /home/tvm/src/runtime/vulkan/vulkan_device_api.cc:674: Cannot initialize vulkan: [23:29:34] /home/tvm/src/runtime/vulkan/vulkan_device_api.cc:512: Check failed: __e == VK_SUCCESS Vulan Error, code=-9: VK_ERROR_INCOMPATIBLE_DRIVER Stack trace returned 10 entries: [bt] (0) /home/user/.local/lib/python3.6/site-packages/tvm-0.4.0-py3.6-linux-x86_64.egg/tvm/libtvm.so(dmlc::StackTrace[abi:cxx11]()+0x53) [0x7f477f5399f3] ......... You can still compile vulkan module but cannot run locally Run GPU(Vulkan Flavor) test ... 0.000225198 secs/op ``` -------------------------------- ### Configure GPG for Signing Source: https://github.com/apache/tvm/blob/main/docs/contribute/release_process.rst Example of how to configure GPG to use a specific private key for signing artifacts. ```bash $ cat ~/.gnupg/gpg.conf default-key F42xxxxxxxxxxxxxxx ``` -------------------------------- ### Install Package Configuration File Source: https://github.com/apache/tvm/blob/main/CMakeLists.txt Installs the generated CMake configuration file. This makes the TVM package discoverable by CMake's `find_package` command. ```cmake install( FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") ``` -------------------------------- ### Install Necessary Python Dependencies Source: https://github.com/apache/tvm/blob/main/docs/install/from_source.rst Installs essential Python packages like numpy and cython, which are required for building and using TVM. ```bash pip3 install numpy cython ``` -------------------------------- ### Install Python Dependencies for RPC Tracker Source: https://github.com/apache/tvm/blob/main/docs/install/from_source.rst Installs the tornado package, which is necessary for enabling the RPC Tracker functionality in TVM. ```bash pip3 install tornado ``` -------------------------------- ### Install Android RPC App Source: https://github.com/apache/tvm/blob/main/apps/android_rpc/README.md Install the signed APK to your Android device using ADB. Ensure the APK is signed using dev_tools/gen_keystore.sh and dev_tools/sign_apk.sh. ```bash $ANDROID_HOME/platform-tools/adb install app/build/outputs/apk/release/tvmrpc-release.apk ``` -------------------------------- ### Start WebSocket RPC Proxy Source: https://github.com/apache/tvm/blob/main/web/README.md Starts the WebSocket RPC proxy server. This is a prerequisite for remote execution and testing via WebSocket RPC. ```python python -m tvm.exec.rpc_proxy --example-rpc=1 ``` -------------------------------- ### Install TVM Web Package with CMake Source: https://github.com/apache/tvm/blob/main/CMakeLists.txt Installs the web-related assets for TVM. This includes files necessary for any web-based interfaces or components of TVM. ```cmake install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/web/" DESTINATION "web/") ``` -------------------------------- ### Install TVM Libraries Source: https://github.com/apache/tvm/blob/main/CMakeLists.txt Installs the 'tvm_compiler' and 'tvm_runtime' targets to the specified library directory. The destination path is determined by 'lib${LIB_SUFFIX}', which typically includes platform-specific suffixes. ```cmake install(TARGETS tvm_compiler DESTINATION lib${LIB_SUFFIX}) install(TARGETS tvm_runtime DESTINATION lib${LIB_SUFFIX}) ``` -------------------------------- ### Launch Prebuilt Docker Image Source: https://github.com/apache/tvm/blob/main/docker/README.md Launch a prebuilt TVM Docker image for quick exploration. After launching, you can start a Jupyter notebook inside the container. ```bash /path/to/tvm/docker/bash.sh tvmai/demo-cpu ``` ```bash jupyter notebook ``` -------------------------------- ### C++ Function Docstring Example (doxygen) Source: https://github.com/apache/tvm/blob/main/docs/contribute/document.rst Example of a C++ docstring using the doxygen format. Comments clarifying internal logic are also recommended. ```c++ /*! * \brief Description of my function * \param arg1 Description of arg1 * \param arg2 Description of arg2 * \returns describe return value */ int myfunction(int arg1, int arg2) { // When necessary, also add comment to clarify internal logics } ``` -------------------------------- ### Install Python Dependencies for Auto-tuning Source: https://github.com/apache/tvm/blob/main/docs/install/from_source.rst Installs Python packages required for TVM's auto-tuning module, including tornado, psutil, xgboost, and cloudpickle. ```bash pip3 install tornado psutil 'xgboost>=1.1.0' cloudpickle ``` -------------------------------- ### Start Interactive Docker Bash Session Source: https://github.com/apache/tvm/blob/main/docker/README.md Use this helper script to start an interactive bash session within a specified Docker image. It mounts the current directory, sets it as home, and uses the host network. ```bash /path/to/tvm/docker/bash.sh image_name ``` -------------------------------- ### Install PyTorch in TVM Tutorial for Colab Source: https://github.com/apache/tvm/blob/main/docs/README.md Use the `%%shell` IPython magic command to install dependencies like PyTorch within a TVM tutorial for Google Colab. This command provides real-time output. ```python ###################################################################### # To run this tutorial, we must install PyTorch: # # .. code-block:: bash # # %%shell # pip install torch # ``` -------------------------------- ### Elementwise Fusion Example Before FuseOps Source: https://github.com/apache/tvm/blob/main/docs/arch/fusion.rst Illustrates a sequence of elementwise and injective operations before fusion. ```python # Before FuseOps (simplified) @R.function def main(x: R.Tensor((10, 20), "float32")): with R.dataflow(): lv0 = R.call_tir(add, (x, const_1), out_sinfo=R.Tensor((10, 20), "float32")) lv1 = R.call_tir(exp, (lv0,), out_sinfo=R.Tensor((10, 20), "float32")) gv = R.call_tir(squeeze, (lv1,), out_sinfo=R.Tensor((10, 20), "float32")) R.output(gv) return gv ``` -------------------------------- ### Install Prebuilt Extra Runtime Libraries Source: https://github.com/apache/tvm/blob/main/CMakeLists.txt Installs prebuilt extra runtime libraries, such as CUDA runtimes, into the 'lib' directory. This is crucial for bundling separately built libraries like libtvm_runtime_cuda.so. ```cmake foreach(_extra_lib IN LISTS TVM_PACKAGE_EXTRA_LIBS) if(NOT EXISTS "${_extra_lib}") message(FATAL_ERROR "TVM_PACKAGE_EXTRA_LIBS entry does not exist: ${_extra_lib}") endif() install(FILES "${_extra_lib}" DESTINATION "lib") endforeach() ``` -------------------------------- ### Start Jupyter Notebook Source: https://github.com/apache/tvm/blob/main/docs/install/docker.rst Start a Jupyter notebook within the Docker container. On macOS, you may need to specify the IP address to avoid binding errors. ```bash jupyter notebook ``` ```bash jupyter notebook --ip=0.0.0.0 ``` -------------------------------- ### Pass Instrumentation Call Sequence Example Source: https://github.com/apache/tvm/blob/main/docs/arch/pass_infra.rst Illustrates the typical call sequence for a pass instrument within a PassContext, showing how `InstrumentBeforePass` and `InstrumentAfterPass` are used. ```c++ if (pass_ctx.InstrumentBeforePass(ir_module, pass_info)) { new_ir_module = run_pass(ir_module, pass_ctx); pass_ctx.InstrumentAfterPass(new_ir_module, pass_info); return new_ir_module; } ``` -------------------------------- ### Install Python Test Dependencies Source: https://github.com/apache/tvm/blob/main/docs/contribute/pull_request.rst Install necessary Python packages for running tests, including pytest and Cython. This command should be run before executing Python tests. ```bash pip install --user pytest Cython ``` -------------------------------- ### Python Runtime Execution Example Source: https://github.com/apache/tvm/blob/main/docs/arch/index.rst Demonstrates loading a compiled module, creating a tensor, and executing a function on a CUDA device. Ensure the 'compiled_artifact.so' exists and the TVM runtime is properly configured. ```python import tvm # Example runtime execution program in python, with type annotated mod: tvm.runtime.Module = tvm.runtime.load_module("compiled_artifact.so") arr: tvm.runtime.Tensor = tvm.runtime.tensor([1, 2, 3], device=tvm.cuda(0)) fun: tvm_ffi.Function = mod["addone"] fun(arr) print(arr.numpy()) ``` -------------------------------- ### Install TVM Runtime Source Files with CMake Source: https://github.com/apache/tvm/blob/main/CMakeLists.txt Installs the source files (.cc and .h) for the TVM runtime. This is typically used for development or when building TVM from source. ```cmake install( DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/src/runtime/" DESTINATION "src/runtime/" FILES_MATCHING PATTERN "*.cc" PATTERN "*.h" ) ``` -------------------------------- ### Check Example NPU Codegen and Runtime Availability Source: https://github.com/apache/tvm/blob/main/python/tvm/relax/backend/contrib/example_npu/README.md This Python snippet checks for the presence of the 'relax.ext.example_npu' codegen and 'runtime.ExampleNPUJSONRuntimeCreate' runtime functions. Tests are automatically skipped if either is not available, ensuring compatibility with different TVM build configurations. ```python import tvm has_codegen = tvm.get_global_func("relax.ext.example_npu", True) has_runtime = tvm.get_global_func("runtime.ExampleNPUJSONRuntimeCreate", True) has_example_npu = has_codegen and has_runtime ``` -------------------------------- ### Start Standalone JVM RPC Server Source: https://github.com/apache/tvm/blob/main/jvm/README.md Initiates a standalone RPC server on a specified port, ready to accept remote requests from various clients. ```java Server server = new Server(port); server.start(); ``` -------------------------------- ### Start RPC Proxy Server Source: https://github.com/apache/tvm/blob/main/apps/ios_rpc/README.md Initiate the RPC proxy server on the host machine. This server acts as a mediator for communication between the RPC client and the iOS device. ```shell python3 -m tvm.exec.rpc_proxy --host 0.0.0.0 --port 9090 ``` -------------------------------- ### Set TVM Runtime Include Directories Source: https://github.com/apache/tvm/blob/main/CMakeLists.txt Sets the include directories for the tvm_runtime target, making it installable. ```cmake target_include_directories(tvm_runtime PUBLIC "$") ``` -------------------------------- ### Initialize Virtual Machine Runtime Source: https://github.com/apache/tvm/blob/main/docs/arch/relax_vm.rst Shows how to create and initialize a VirtualMachine instance with a loaded executable and a target device. This sets up the VM's execution environment. ```python from tvm.relax import VirtualMachine vm = VirtualMachine(exec_module, tvm.cuda()) ``` -------------------------------- ### Install Shared Libraries for Python Package Source: https://github.com/apache/tvm/blob/main/CMakeLists.txt Installs compiled shared libraries into the 'lib' directory for Python package installation. This allows tvm_ffi.libinfo.load_lib_ctypes to find them. ```cmake install(TARGETS tvm_compiler DESTINATION "lib") install(TARGETS tvm_runtime DESTINATION "lib") ``` -------------------------------- ### Runtime Function Call Example Source: https://github.com/apache/tvm/blob/main/docs/arch/external_library_dispatch.rst Illustrates how high-level function calls are replaced with calls to externally compiled functions via the PackedFunc interface at runtime. ```python R.call_dps_packed(ExternFunc("fused_relax_matmul_cublas0"), (x, w, bias), ...) ``` -------------------------------- ### Fast Local Docs Build (No Tutorials) Source: https://github.com/apache/tvm/blob/main/docs/README.md Build the documentation locally without executing tutorials for faster iteration. This is achieved by setting the TVM_TUTORIAL_EXEC_PATTERN environment variable. ```bash cd docs && TVM_TUTORIAL_EXEC_PATTERN=none make html ``` -------------------------------- ### Build and Serve Docs with Docker Source: https://github.com/apache/tvm/blob/main/docs/README.md Use the ci.py script with Docker to build and serve the TVM documentation. This is the recommended method for a consistent build environment. ```bash python tests/scripts/ci.py docs ``` ```bash python tests/scripts/ci.py docs --help ``` ```bash python tests/scripts/ci.py serve-docs ``` -------------------------------- ### Python Function Docstring Example (numpydoc) Source: https://github.com/apache/tvm/blob/main/docs/contribute/document.rst Example of a Python docstring using the numpydoc format. Ensure blank lines between sections like Parameters, Returns, and Examples for correct building. ```python def myfunction(arg1, arg2, arg3=3): """Briefly describe my function. Parameters ---------- arg1 : Type1 Description of arg1 arg2 : Type2 Description of arg2 arg3 : Type3, optional Description of arg3 Returns ------- rv1 : RType1 Description of return type one Examples -------- .. code:: python # Example usage of myfunction x = myfunction(1, 2) """ return rv1 ``` -------------------------------- ### Initialize and Build TVM from Scratch Source: https://github.com/apache/tvm/blob/main/AGENTS.md Initializes git submodules, creates a build directory, configures CMake, and builds the TVM project. This is for a fresh checkout. ```bash git submodule update --init --recursive mkdir -p build cp cmake/config.cmake build/config.cmake cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo cmake --build build --parallel ``` -------------------------------- ### View Available Platforms Source: https://github.com/apache/tvm/blob/main/docs/contribute/pull_request.rst Display all available platforms for testing or view help specific to the CPU platform. This command is useful for understanding the testing capabilities of the CI script. ```bash python tests/scripts/ci.py --help ``` ```bash python tests/scripts/ci.py cpu --help ``` -------------------------------- ### CUDA and CUTLASS Installation Source: https://github.com/apache/tvm/blob/main/CMakeLists.txt Installs targets related to CUDA and CUTLASS, specifically fpA_intB_gemm and flash_attn, into the library directory. ```cmake if(USE_CUDA AND USE_CUTLASS) install(TARGETS fpA_intB_gemm EXPORT ${PROJECT_NAME}Targets DESTINATION lib${LIB_SUFFIX}) install(TARGETS flash_attn EXPORT ${PROJECT_NAME}Targets DESTINATION lib${LIB_SUFFIX}) endif() ``` -------------------------------- ### TIR to Executable Code Flow Source: https://github.com/apache/tvm/blob/main/docs/arch/codegen.rst Illustrates the steps involved in transforming TIR PrimFuncs into host and device modules, culminating in a unified host module. ```text TIR PrimFuncs (in IRModule) │ ▼ TIR pipeline ← lowering passes (flatten buffers, lower intrinsics, etc.) TIR PrimFuncs (lowered) │ ▼ split_host_device_mods() ← separate host and device functions Host IRModule + Device IRModule(s) │ │ ▼ ▼ codegen_build() codegen_build() ← target-specific code generation │ │ ▼ ▼ Host Module Device Module(s) │ │ ▼ import_module() │ Host Module ◄─────────────┘ ← device modules imported into host │ ▼ (returned to relax.build for linking with VM bytecode) ``` -------------------------------- ### Run Full CI Docs Pipeline Source: https://github.com/apache/tvm/blob/main/docs/README.md Execute the complete CI documentation pipeline, including tutorial execution and deployment steps. This script requires a GPU CI environment. ```bash tests/scripts/task_python_docs.sh ``` -------------------------------- ### VM Instrumentation Callback Source: https://github.com/apache/tvm/blob/main/docs/arch/relax_vm.rst Shows how to set up a custom instrumentation callback for the Virtual Machine to observe function calls. ```python def my_instrument(func, func_symbol, before_run, ret_value, *args): if before_run: print(f"About to call: {func_symbol}") return VMInstrumentReturnKind.NO_OP vm.set_instrument(my_instrument) vm["main"](inp) ``` -------------------------------- ### Save and Load VM Executable Source: https://github.com/apache/tvm/blob/main/docs/arch/relax_vm.rst Demonstrates how to serialize a VM executable to a library file and load it back. This is useful for deploying compiled Relax models. ```python # Save ex.export_library("model.so") # Load loaded = tvm.runtime.load_module("model.so") vm = relax.VirtualMachine(loaded, tvm.cuda()) ``` -------------------------------- ### Virtual Machine Execution Flow Source: https://github.com/apache/tvm/blob/main/docs/arch/relax_vm.rst Illustrates the frame stack and register file organization within the VM's execution context. ```text Frame Stack Register File (per frame) ┌─────────────┐ ┌────┬────┬────┬─────┬────┐ │ Frame 2 │ ───────► │ R0 │ R1 │ R2 │ ... │ Rn │ ├─────────────┤ └────┴────┴────┴─────┴────┘ │ Frame 1 │ ───────► [register file] ├─────────────┤ │ Frame 0 │ ───────► [register file] └─────────────┘ ``` -------------------------------- ### Install Third-Party Compiled Dependencies Source: https://github.com/apache/tvm/blob/main/CMakeLists.txt Installs third-party compiled dependencies into the 'lib' directory if they are targeted. This ensures that external libraries used by TVM are available. ```cmake if(TARGET fpA_intB_gemm) tvm_configure_target_library(fpA_intB_gemm) install(TARGETS fpA_intB_gemm DESTINATION "lib") endif() if(TARGET flash_attn) tvm_configure_target_library(flash_attn) install(TARGETS flash_attn DESTINATION "lib") endif() ``` -------------------------------- ### Build Documentation in Docker Source: https://github.com/apache/tvm/blob/main/docker/README.md Build the project documentation using make html within the Docker environment. The generated HTML files will be located in `docs/_build/html`. ```bash ./docker/ci_build.sh ci_gpu bash -c "cd docs && make html" ``` -------------------------------- ### Compile and Run a Relax Module Source: https://github.com/apache/tvm/blob/main/docs/arch/relax_vm.rst Demonstrates how to compile a Relax module and interact with the Virtual Machine in Python. ```python import tvm from tvm import relax import numpy as np # Compile ex = tvm.compile(MyModule, target="llvm") # Create VM vm = relax.VirtualMachine(ex, tvm.cpu()) # Direct invocation inp = tvm.runtime.tensor(np.random.rand(3, 4).astype("float32")) result = vm["main"](inp) # Stateful interface (useful for RPC) vm.set_input("main", inp) vm.invoke_stateful("main") output = vm.get_outputs("main") ``` -------------------------------- ### Execute Specific Tutorials with ci.py Source: https://github.com/apache/tvm/blob/main/docs/README.md Control which tutorials are executed during the documentation build by specifying a regular expression pattern. This is useful for faster local iteration or when certain tutorials fail on your environment. ```bash python tests/scripts/ci.py docs --tutorial-pattern=/get_started/tutorials ``` ```bash # The slash \ is used to get . in regular expression python tests/scripts/ci.py docs --tutorial-pattern=file_name\.py ``` -------------------------------- ### Install Exported Targets Source: https://github.com/apache/tvm/blob/main/CMakeLists.txt Installs exported targets for the project, specifying a namespace and destination directory. This is typically used for making targets available to other CMake projects. ```cmake install(EXPORT ${PROJECT_NAME}Targets NAMESPACE ${PROJECT_NAME}:: DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}) ``` -------------------------------- ### Load and Run Generated Shared Library in Java Source: https://github.com/apache/tvm/blob/main/jvm/README.md Shows how to load a generated shared library (e.g., `add_cpu.so`) and execute a function from it. Ensure the native library is accessible via `LD_LIBRARY_PATH`. Remember to release resources like `Tensor` and `Module` after use. ```java import org.apache.tvm.Module; import org.apache.tvm.Tensor; import org.apache.tvm.Device; import java.io.File; import java.util.Arrays; public class LoadAddFunc { public static void main(String[] args) { String loadingDir = args[0].cast(); Module fadd = Module.load(loadingDir + File.separator + "add_cpu.so"); Device dev = Device.cpu(); long[] shape = new long[]{2}; Tensor arr = Tensor.empty(shape, dev); arr.copyFrom(new float[]{3f, 4f}); Tensor res = Tensor.empty(shape, dev); fadd.entryFunc().pushArg(arr).pushArg(arr).pushArg(res).invoke(); System.out.println(Arrays.toString(res.asFloatArray())); arr.release(); res.release(); fadd.release(); } } ``` -------------------------------- ### Conditionally Install CUTLASS Headers with CMake Source: https://github.com/apache/tvm/blob/main/CMakeLists.txt Installs CUTLASS header files only if the CUTLASS library is detected in the specified directory. This allows for optional inclusion of CUTLASS support. ```cmake if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/cutlass/include") install( DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/cutlass/include/" DESTINATION "3rdparty/cutlass/include/" FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp" ) endif() ``` -------------------------------- ### Configure TVM Build with CMake Source: https://github.com/apache/tvm/blob/main/docs/install/from_source.rst Sets up the build environment for TVM by creating a build directory and copying the CMake configuration file. ```bash cd tvm rm -rf build && mkdir build && cd build # Specify the build configuration via CMake options cp ../cmake/config.cmake . ``` -------------------------------- ### Update TVM Website with Release Docs Source: https://github.com/apache/tvm/blob/main/docs/contribute/release_process.rst Clone the TVM site repository, checkout the 'asf-site' branch, create a new version directory, download release documentation from CI, and commit/push the changes. ```bash git clone https://github.com/apache/tvm-site.git pushd tvm-site git checkout asf-site pushd docs # make release docs directory mkdir v0.9.0 pushd v0.9.0 # download the release docs from CI # find this URL by inspecting the CI logs for the most recent build of the release branch curl -LO https://tvm-jenkins-artifacts-prod.s3.us-west-2.amazonaws.com/tvm/v0.9.0/1/docs/docs.tgz tar xf docs.tgz rm docs.tgz # add the docs and push git add . git commit -m "Add v0.9.0 docs" git push ``` -------------------------------- ### Install TensorFlow for Frontend Contributors (TFLite) Source: https://github.com/apache/tvm/blob/main/docs/install/from_source.rst Installs a specific version of TensorFlow required for running or contributing to TVM's frontend tests, particularly those involving TFLite. ```bash pip install tensorflow==2.19.0 ``` -------------------------------- ### Relax VM Compilation Flow Source: https://github.com/apache/tvm/blob/main/docs/arch/relax_vm.rst Illustrates the end-to-end flow from Relax IR to runtime execution, including compilation, linking, and VM execution. ```text IRModule (Relax + TIR) │ ▼ relax_pipeline (FuseOps, LegalizeOps, ...) IRModule (optimized) │ ▼ VMCodeGen ExecBuilder (bytecode) + IRModule (TIR only) │ │ │ ▼ tirx.build() │ runtime.Module (native kernels) │ │ ▼ VMLink ▼ VMExecutable ◄───────── linked together │ ▼ VirtualMachine(exec, device) Runtime execution ``` -------------------------------- ### Install TVM CMake Utilities with CMake Source: https://github.com/apache/tvm/blob/main/CMakeLists.txt Installs CMake utility files used within the TVM build system. These files are essential for configuring and managing the build process. ```cmake install( DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/cmake/utils/" DESTINATION "cmake/utils/" FILES_MATCHING PATTERN "*.cmake" ) ``` -------------------------------- ### Install TVM via Pip Local Project Source: https://github.com/apache/tvm/blob/main/docs/install/from_source.rst Installs TVM as a local editable project using pip after activating a conda environment. This method requires setting the TVM_LIBRARY_PATH. ```bash conda activate your-own-env conda install python # make sure python is installed export TVM_LIBRARY_PATH=/path-to-tvm/build pip install -e /path-to-tvm ``` -------------------------------- ### Run C++ Unit Tests Locally Source: https://github.com/apache/tvm/blob/main/docs/contribute/pull_request.rst Execute C++ unit tests from the TVM source root. Ensure gtest is installed and follow the C++ test installation instructions. ```bash # assume you are in tvm source root TVM_ROOT=`pwd` ./tests/scripts/task_cpp_unittest.sh ``` -------------------------------- ### Install TVM Runtime Headers with CMake Source: https://github.com/apache/tvm/blob/main/CMakeLists.txt Installs the minimal header files required by Python extensions from the TVM runtime directory. This ensures that Python bindings can correctly access the necessary header definitions. ```cmake install( DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/include/tvm/runtime/" DESTINATION "include/tvm/runtime/" FILES_MATCHING PATTERN "*.h" ) ``` -------------------------------- ### Tagging and Branching for Release Candidate Source: https://github.com/apache/tvm/blob/main/docs/contribute/release_process.rst This sequence of commands demonstrates how to tag the next development cycle, cut a release branch, and tag the first release candidate on the release branch. ```bash git clone https://github.com/apache/tvm.git cd tvm/ # 1. Tag the next dev cycle on main (drives main's 0.7.devN version), # on the last commit to be included in the release. git checkout git tag v0.7.dev0 git push origin refs/tags/v0.7.dev0 # 2. Cut the release branch off that same commit. git branch v0.6 git push --set-upstream origin v0.6 # 3. Tag the first release candidate on the release branch. Keep this tag on a # release-prep commit, NOT the v0.7.dev0-tagged branch point, so the two tags # never share a commit. git checkout v0.6 # ... make any release-prep commits (release notes, etc.) here ... git tag v0.6.0.rc0 git push origin refs/tags/v0.6.0.rc0 ``` -------------------------------- ### Build TVM on Windows using CMake Source: https://github.com/apache/tvm/blob/main/docs/install/from_source.rst Basic steps to build TVM on Windows using CMake. This involves creating a build directory, navigating into it, and running the initial CMake configuration. ```bash mkdir build cd build cmake .. cd .. ``` -------------------------------- ### Parametrized Test Function Source: https://github.com/apache/tvm/blob/main/docs/contribute/testing.rst Example of a test function parametrized with different targets and implementations. ```python @pytest.mark.parametrize('target,impl', [ ('llvm', cpu_implementation), ('cuda', gpu_implementation_small_batch), ('cuda', gpu_implementation_large_batch), ]) def test_function(target, dev, impl): # Test code goes here ``` -------------------------------- ### Build Android RPC App with Gradle Source: https://github.com/apache/tvm/blob/main/apps/android_rpc/README.md Set the ANDROID_HOME environment variable and use Gradle to clean, build the Android application, and resolve dependencies. The APK will be generated in app/build/outputs/apk. ```bash export ANDROID_HOME=[Path to your Android SDK, e.g., ~/Android/sdk] cd apps/android_rpc gradle clean build ``` -------------------------------- ### Elementwise Fusion Example After FuseOps Source: https://github.com/apache/tvm/blob/main/docs/arch/fusion.rst Shows the result of fusion, where multiple operations are grouped into a single function. ```python # After FuseOps @R.function(private=True) def fused_add_exp_squeeze(x, p0): R.func_attr({"Primitive": True}) with R.dataflow(): lv0 = R.call_tir(add, (x, p0), ...) lv1 = R.call_tir(exp, (lv0,), ...) gv = R.call_tir(squeeze, (lv1,), ...) R.output(gv) return gv @R.function def main(x: R.Tensor((10, 20), "float32")): with R.dataflow(): gv = fused_add_exp_squeeze(x, const_1) R.output(gv) return gv ``` -------------------------------- ### End-to-End Model Execution with Relax VM Source: https://github.com/apache/tvm/blob/main/docs/arch/index.rst Demonstrates loading a compiled artifact and executing a model using the Relax Virtual Machine on a CUDA device. Requires a pre-compiled module (e.g., resnet18.so) and input data. ```python import tvm from tvm import relax # Load the compiled artifact mod: tvm.runtime.Module = tvm.runtime.load_module("resnet18.so") # Create a VM instance on cuda(0) vm = relax.VirtualMachine(mod, tvm.cuda(0)) data: tvm.runtime.Tensor = get_input_data() # Run the model — vm["main"] returns a PackedFunc result = vm["main"](data).numpy() ``` -------------------------------- ### Enable NVSHMEM Runtime Source: https://github.com/apache/tvm/blob/main/CMakeLists.txt Configures and builds the NVSHMEM runtime library if CUDA and NVSHMEM are enabled. This requires NVSHMEM to be installed and found by CMake. ```cmake if(USE_CUDA AND USE_NVSHMEM) find_nvshmem(${USE_NVSHMEM}) if(NOT NVSHMEM_FOUND) message(FATAL_ERROR "Cannot find NVSHMEM, USE_NVSHMEM=" ${USE_NVSHMEM}) endif() set(CMAKE_CUDA_SEPARABLE_COMPILATION ON) set(CMAKE_POSITION_INDEPENDENT_CODE ON) tvm_file_glob(GLOB _nvshmem_srcs src/runtime/extra/contrib/nvshmem/*.cc src/runtime/extra/contrib/nvshmem/*.cu) add_library(tvm_nvshmem_objs OBJECT ${_nvshmem_srcs}) target_link_libraries(tvm_nvshmem_objs PRIVATE tvm_runtime_extra_defs) target_include_directories(tvm_nvshmem_objs PUBLIC ${NVSHMEM_INCLUDE_DIR}) find_library(NVSHMEM_HOST nvshmem_host ${NVSHMEM_LIB_DIR}) find_library(NVSHMEM_DEVICE nvshmem_device ${NVSHMEM_LIB_DIR}) target_link_libraries(tvm_runtime_extra PRIVATE tvm_nvshmem_objs ${NVSHMEM_HOST} ${NVSHMEM_DEVICE}) set_target_properties(tvm_runtime_extra PROPERTIES CUDA_SEPARABLE_COMPILATION ON) endif() ``` -------------------------------- ### Locate TVM Python Package Source: https://github.com/apache/tvm/blob/main/docs/install/from_source.rst Verifies that the TVM Python package is installed correctly and displays its location within the Python site-packages directory. ```python import tvm; print(tvm.__file__) ``` -------------------------------- ### Python PassContext with Pass Instrument Source: https://github.com/apache/tvm/blob/main/docs/arch/pass_infra.rst Example of using PassContext with a PassInstrument in Python. The instrument is used to track pass execution within the context. ```python with PassContext(instruments=[pi]) # pi = a PassInstrument implementation. pi.EnterPassContext() if pi.ShouldRun(Pass1): pi.RunBeforePass() Pass1() pi.RunAfterPass() if pi.ShouldRun(Pass2): pi.RunBeforePass() Pass2() pi.RunAfterPass() pi.ExitPassContext() ``` -------------------------------- ### Initialize Linker Libraries Source: https://github.com/apache/tvm/blob/main/CMakeLists.txt Initializes lists for various linker libraries used in the TVM build. ```cmake set(TVM_LINKER_LIBS "") set(TVM_RUNTIME_LINKER_LIBS "") set(TVM_RUNTIME_BACKEND_LIBS "") ```