code-atlas-provenance / data /manifests /github_api /nvidia_cutlass.dcf215a.json
arpandeepk's picture
Initialize Code-ATLAS provenance registry
4f040da verified
Raw
History Blame Contribute Delete
18.6 kB
{
"sha": "dcf215af68a2d08d305076c152a06f201728cd53",
"node_id": "C_kwDOBrVDM9oAKGRjZjIxNWFmNjhhMmQwOGQzMDUwNzZjMTUyYTA2ZjIwMTcyOGNkNTM",
"commit": {
"author": {
"name": "drazi",
"email": "ftse@nvidia.com",
"date": "2026-08-06T14:31:22Z"
},
"committer": {
"name": "GitHub",
"email": "noreply@github.com",
"date": "2026-08-06T14:31:22Z"
},
"message": "Merge pull request #3289 from kainzhong/more_tvm_ffi_doc\n\n[CuTeDSL] Add docs of how to call cute.jit functions in C++ via TVM-FFI",
"tree": {
"sha": "7aa3ab329b868295c83bf13da7326f6654aea34e",
"url": "https://api.github.com/repos/NVIDIA/cutlass/git/trees/7aa3ab329b868295c83bf13da7326f6654aea34e"
},
"url": "https://api.github.com/repos/NVIDIA/cutlass/git/commits/dcf215af68a2d08d305076c152a06f201728cd53",
"comment_count": 0,
"verification": {
"verified": true,
"reason": "valid",
"signature": "-----BEGIN PGP SIGNATURE-----\n\nwsFcBAABCAAQBQJqdJq6CRC1aQ7uu5UhlAAAn1sQAJYUMAZdX2hYWtSfji512tB5\ndsehxTsV3o8oMCxj03j+lSboJWdKxjmGPrRlrfKWHX5pTWxKupDZ6jGtGQF46Mre\ndbx9ZzcpCgLFktxoQTWnVQ85Y9+jcofkpCc7Q72lRQ7CHHhbBteZ+pPh3qJfxNjI\nqgbHREhcIG12kj0sP3h9qMF81deer/aG8B4v09J1pqh6cz0mqfzxVgKceOgbxXxI\n5EXz8i4jCe5WekNt0h7ZKVUUJqytf1n6YfaJ6uWduEojZb1lCwfd61QJrm2C1HjC\nRSgw8YJsYEPc+DJCed1cjQ0F/XUwiRciydrdogwa0MvhwubCCGdIFG13fwO56S6P\nfQOu2dsHzngwBldxWtNKQFNfJOdFbOHoXoLeTWoTyuPsUP7X8hatS9+hNP4lAlv2\nEHBXLBtoyv6CzKZkraPbd8IjRm+F9soGsbs/AwYeZnGhItodW6qeAEHrC/JxiPeQ\n34tPvqOwuf2ZXgomj3ofe76aN0ueitTdoocSIxe+/lJ7SDXkK+8VCLdace/IaPjV\nKATgVAYpdoLYSBhq7HaLhg7Ntx3+DG+eaPS2PZ1PNx/Sl9YMkZTkWbWQjx7xixt3\nONx20TfZ2CAAKbeO/nPWOjE44i6eEF3xdH0HRHt11lzO2PPyV7sYpH9B+Pl5zAo9\nkui6el+wszolOulBCcmU\n=LXZF\n-----END PGP SIGNATURE-----\n",
"payload": "tree 7aa3ab329b868295c83bf13da7326f6654aea34e\nparent b50d8fd7196e1c5801c1bd8edf1d64e4bf9add6c\nparent 9245be15dbaedf4109e04cfedf232ab690e1607e\nauthor drazi <ftse@nvidia.com> 1786026682 +0800\ncommitter GitHub <noreply@github.com> 1786026682 +0800\n\nMerge pull request #3289 from kainzhong/more_tvm_ffi_doc\n\n[CuTeDSL] Add docs of how to call cute.jit functions in C++ via TVM-FFI",
"verified_at": "2026-08-06T14:33:02Z"
}
},
"url": "https://api.github.com/repos/NVIDIA/cutlass/commits/dcf215af68a2d08d305076c152a06f201728cd53",
"html_url": "https://github.com/NVIDIA/cutlass/commit/dcf215af68a2d08d305076c152a06f201728cd53",
"comments_url": "https://api.github.com/repos/NVIDIA/cutlass/commits/dcf215af68a2d08d305076c152a06f201728cd53/comments",
"author": {
"login": "fengxie",
"id": 524446,
"node_id": "MDQ6VXNlcjUyNDQ0Ng==",
"avatar_url": "https://avatars.githubusercontent.com/u/524446?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/fengxie",
"html_url": "https://github.com/fengxie",
"followers_url": "https://api.github.com/users/fengxie/followers",
"following_url": "https://api.github.com/users/fengxie/following{/other_user}",
"gists_url": "https://api.github.com/users/fengxie/gists{/gist_id}",
"starred_url": "https://api.github.com/users/fengxie/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/fengxie/subscriptions",
"organizations_url": "https://api.github.com/users/fengxie/orgs",
"repos_url": "https://api.github.com/users/fengxie/repos",
"events_url": "https://api.github.com/users/fengxie/events{/privacy}",
"received_events_url": "https://api.github.com/users/fengxie/received_events",
"type": "User",
"user_view_type": "public",
"site_admin": false
},
"committer": {
"login": "web-flow",
"id": 19864447,
"node_id": "MDQ6VXNlcjE5ODY0NDQ3",
"avatar_url": "https://avatars.githubusercontent.com/u/19864447?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/web-flow",
"html_url": "https://github.com/web-flow",
"followers_url": "https://api.github.com/users/web-flow/followers",
"following_url": "https://api.github.com/users/web-flow/following{/other_user}",
"gists_url": "https://api.github.com/users/web-flow/gists{/gist_id}",
"starred_url": "https://api.github.com/users/web-flow/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/web-flow/subscriptions",
"organizations_url": "https://api.github.com/users/web-flow/orgs",
"repos_url": "https://api.github.com/users/web-flow/repos",
"events_url": "https://api.github.com/users/web-flow/events{/privacy}",
"received_events_url": "https://api.github.com/users/web-flow/received_events",
"type": "User",
"user_view_type": "public",
"site_admin": false
},
"parents": [
{
"sha": "b50d8fd7196e1c5801c1bd8edf1d64e4bf9add6c",
"url": "https://api.github.com/repos/NVIDIA/cutlass/commits/b50d8fd7196e1c5801c1bd8edf1d64e4bf9add6c",
"html_url": "https://github.com/NVIDIA/cutlass/commit/b50d8fd7196e1c5801c1bd8edf1d64e4bf9add6c"
},
{
"sha": "9245be15dbaedf4109e04cfedf232ab690e1607e",
"url": "https://api.github.com/repos/NVIDIA/cutlass/commits/9245be15dbaedf4109e04cfedf232ab690e1607e",
"html_url": "https://github.com/NVIDIA/cutlass/commit/9245be15dbaedf4109e04cfedf232ab690e1607e"
}
],
"stats": {
"total": 194,
"additions": 194,
"deletions": 0
},
"files": [
{
"sha": "fa5edc27f4d1a124720ece12fa1cbe7e0ae1d27a",
"filename": "media/docs/pythonDSL/guides/tvm_ffi_compilation.rst",
"status": "modified",
"additions": 194,
"deletions": 0,
"changes": 194,
"blob_url": "https://github.com/NVIDIA/cutlass/blob/dcf215af68a2d08d305076c152a06f201728cd53/media%2Fdocs%2FpythonDSL%2Fguides%2Ftvm_ffi_compilation.rst",
"raw_url": "https://github.com/NVIDIA/cutlass/raw/dcf215af68a2d08d305076c152a06f201728cd53/media%2Fdocs%2FpythonDSL%2Fguides%2Ftvm_ffi_compilation.rst",
"contents_url": "https://api.github.com/repos/NVIDIA/cutlass/contents/media%2Fdocs%2FpythonDSL%2Fguides%2Ftvm_ffi_compilation.rst?ref=dcf215af68a2d08d305076c152a06f201728cd53",
"patch": "@@ -539,6 +539,167 @@ before launching the kernel based on that tensor's device index.\n For advanced scenarios that pass raw pointers instead of tensors, you should call\n ``cudaSetDevice`` explicitly through the CUDA Python API.\n \n+Call a compiled function from C++ via the TVM FFI registry\n+----------------------------------------------------------\n+\n+The object returned by ``cute.compile(..., options=\"--enable-tvm-ffi\")`` is itself a\n+``tvm_ffi.Function``: a native callable that follows the TVM FFI calling convention.\n+You can publish it in TVM FFI's process-global function registry under a string name with\n+``tvm_ffi.register_global_func``. Once registered, the same compiled kernel can be\n+looked up by name and invoked from any TVM FFI-supported language (e.g. C++) running\n+in the same process. This allows you to bypass the Python interpreter entirely and avoid\n+the CPU overhead by staying in the C++ environment only.\n+In such a case, Python is only used for expressiveness as a DSL to describe the kernel and register it in the shared registry,\n+and C++ is used for the actual execution with efficiency.\n+\n+The following is a minimal example of how to call a compiled CuTeDSL function in C++.\n+\n+This is the C++ code we will compile into a PyTorch extension. We name it ``extension.cpp`` here:\n+\n+.. code-block:: cpp\n+\n+ #include <ATen/DLConvertor.h> // at::toDLPackNonOwning\n+ #include <torch/extension.h> // pybind11 + at::Tensor\n+ #include <tvm/ffi/container/tensor.h> // tvm::ffi::TensorView\n+ #include <tvm/ffi/function.h> // tvm::ffi::Function\n+\n+ #include <string>\n+\n+ void apply_tvm_function(const std::string& name, at::Tensor &x, at::Tensor &y, at::Tensor &z) {\n+ tvm::ffi::Function fn = tvm::ffi::Function::GetGlobalRequired(name);\n+ DLTensor dl_x = {};\n+ DLTensor dl_y = {};\n+ DLTensor dl_z = {};\n+ at::toDLPackNonOwning(x, &dl_x);\n+ at::toDLPackNonOwning(y, &dl_y);\n+ at::toDLPackNonOwning(z, &dl_z);\n+ fn(&dl_x, &dl_y, &dl_z);\n+ }\n+\n+ PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {\n+ m.def(\"apply_tvm_function\", &apply_tvm_function,\n+ \"Look up a tvm-ffi global function by name and call it with three tensors.\");\n+ }\n+\n+\n+Then we need to compile and load this extension into PyTorch:\n+\n+.. code-block:: python\n+\n+ import subprocess\n+ import sys\n+\n+ from torch.utils.cpp_extension import load\n+\n+ def _tvm_ffi_config(flag: str) -> str:\n+ \"\"\"Ask the installed apache-tvm-ffi where its headers and lib live.\"\"\"\n+ out = subprocess.check_output([sys.executable, \"-m\", \"tvm_ffi.config\", flag])\n+ return out.decode().strip()\n+\n+\n+ def build_extension():\n+ include_dir = _tvm_ffi_config(\"--includedir\")\n+ dlpack_include_dir = _tvm_ffi_config(\"--dlpack-includedir\")\n+ lib_dir = _tvm_ffi_config(\"--libdir\")\n+ return load(\n+ name=\"tvm_ffi_demo_ext\",\n+ sources=[\"extension.cpp\"],\n+ extra_include_paths=[include_dir, dlpack_include_dir],\n+ extra_cflags=[\"-std=c++17\"],\n+ # -ltvm_ffi to link, and -rpath so the .so is found at runtime. This is\n+ # the same libtvm_ffi.so that `import tvm_ffi` loads -> shared registry.\n+ extra_ldflags=[f\"-L{lib_dir}\", \"-ltvm_ffi\", f\"-Wl,-rpath,{lib_dir}\"],\n+ verbose=True,\n+ )\n+\n+With all the boilerplate code ready, now let's write a CuTeDSL kernel and use the C++ extension to call it.\n+In practice you might want to call the CuTeDSL function from C++ directly without going back to Python. We call from Python here just for demonstration purposes.\n+\n+.. code-block:: python\n+\n+ import cutlass\n+ import torch\n+ import tvm_ffi\n+ import cutlass.cute as cute\n+\n+ @cute.jit\n+ def add(x: cute.Tensor, y: cute.Tensor, z: cute.Tensor):\n+ add_kernel(x, y, z).launch(grid=[1, 1, 1], block=[16, 1, 1])\n+\n+ @cute.kernel\n+ def add_kernel(x: cute.Tensor, y: cute.Tensor, z: cute.Tensor):\n+ tidx, _, _ = cute.arch.thread_idx()\n+ if tidx < 16:\n+ z[tidx] = x[tidx] + y[tidx]\n+\n+ def main() -> None:\n+ ext = build_extension()\n+\n+ fake_x = cute.runtime.make_fake_compact_tensor(cutlass.BFloat16, (4, 4), stride_order=(1, 0), memspace=cute.AddressSpace.gmem, assumed_align=4)\n+ fake_y = cute.runtime.make_fake_compact_tensor(cutlass.BFloat16, (4, 4), stride_order=(1, 0), memspace=cute.AddressSpace.gmem, assumed_align=4)\n+ fake_z = cute.runtime.make_fake_compact_tensor(cutlass.BFloat16, (4, 4), stride_order=(1, 0), memspace=cute.AddressSpace.gmem, assumed_align=4)\n+ compiled = cute.compile(add, fake_x, fake_y, fake_z, options=\"--enable-tvm-ffi\",)\n+ tvm_ffi.register_global_func(\"CuTeDSL_add\", compiled, override=True)\n+\n+ x = torch.randn((4, 4), dtype=torch.bfloat16, device=\"cuda\")\n+ y = torch.randn((4, 4), dtype=torch.bfloat16, device=\"cuda\")\n+ z = torch.randn((4, 4), dtype=torch.bfloat16, device=\"cuda\")\n+\n+ # Launch the C++ function. This is only for demonstration because it's the easiest way to run our C++ function.\n+ # In practice you might be working with some C++ heavy framework and you should call the C++ function from C++ directly without going through Python.\n+ ext.apply_tvm_function(\"CuTeDSL_add\", x, y, z)\n+ assert torch.allclose(x + y, z, atol=1e-8, rtol=1e-8)\n+ print(\"Successfully called CuTeDSL function from C++!\")\n+\n+ if __name__ == \"__main__\":\n+ main()\n+\n+Calling convention of TVM-FFI in C++\n+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n+To call a compiled CuTeDSL function from C++, we need to utilize the Ahead-of-Time (AOT) compilation to obtain the compiled function as a TVM FFI function object first,\n+and then register it in the TVM FFI global registry with a string name. This requires you to use ``cute.compile`` to compile the ``@cute.jit`` function with the ``--enable-tvm-ffi`` option, then ``cute.compile`` will return a TVM FFI function object.\n+Next, you need to register this function object as a `Global Function <https://tvm.apache.org/ffi/guides/export_func_cls.html#global-functions>`_ with this API: ``tvm_ffi.register_global_func(func_name, f=None, override=False)``,\n+where ``func_name`` is the string name to identify the function in the global registry, ``f`` is the TVM FFI function object returned by ``cute.compile``, and ``override=True`` allows overwriting an existing function with the same name in the registry.\n+In this way, you can make this compiled ``@cute.jit`` TVM-FFI function accessible from other languages, including C++.\n+\n+Note this is not the only option to obtain a TVM-FFI function in C++. You can also export the compiled module to an object file and load it in C++ with TVM-FFI APIs.\n+See `Exporting Compiled Module <https://docs.nvidia.com/cutlass/latest/media/docs/pythonDSL/cute_dsl_general/compile_with_tvm_ffi.html#exporting-compiled-module>`_ for more details.\n+\n+In C++, you can load the compiled function from the global registry with ``tvm::ffi::Function::GetGlobal`` (or ``tvm::ffi::Function::GetGlobalRequired``, which will throw if the function is not found).\n+The returned object will be of type ``tvm::ffi::Function``.\n+\n+The signature of the TVM FFI function loaded in C++ will use a unified ABI for all functions like this:\n+\n+.. code-block:: cpp\n+\n+ void CallPacked(const AnyView* args, int32_t num_args, Any* result) const\n+\n+where ``const AnyView* args`` is the type-erased array of arguments, whose actual types are determined at runtime,\n+``int32_t num_args`` is the number of arguments, and ``Any* result`` is a pointer to the return value (if any).\n+\n+- The arguments are called \"AnyView\", meaning they are \"non-owning\" views of the underlying data and therefore the lifetime is determined by the actual data owner.\n+- The return value is called \"Any\", meaning it owns the data and is responsible for its lifetime.\n+- Both ``Any`` and ``AnyView`` are type-erased containers that can hold objects from different types. The actual type is decided by their ``type_index`` attribute at runtime, which is a `TVMFFITypeIndex <https://tvm.apache.org/ffi/reference/cpp/generated/enum_c__api_8h_1a1925bb5d568a3f5c92a6c28934c9bcc2.html#_CPPv4N15TVMFFITypeIndex11kTVMFFINoneE>`_ enum that represents a TVM-FFI type.\n+\n+However, you do not need to explicitly call TVM-FFI functions with this low-level packed format signature, because TVM-FFI has overridden the ``operator()`` method, which creates arguments of ``CallPacked`` for you\n+to allow you to call the function with the same signature as how you defined it.\n+So in our elementwise addition example, the C++ signature of the TVM FFI function will be something like (note our kernel does not return anything, so the return value will be a ``tvm::ffi::Any`` with ``type_index`` of ``kTVMFFINone``):\n+\n+.. code-block:: cpp\n+\n+ tvm::ffi::Any add(tvm::ffi::AnyView x, tvm::ffi::AnyView y, tvm::ffi::AnyView z)\n+\n+When we want to call the TVM FFI function in C++, we need to construct our inputs in a form that can be converted to ``AnyView`` and recognized by TVM-FFI. In this case, the conversion path we would take is\n+``DLTensor`` -> ``tvm::ffi::TensorView`` -> ``tvm::ffi::AnyView``. The latter two conversions can be implicit (supported by TVM-FFI already), so we just need to convert our tensor type to ``DLTensor``.\n+For PyTorch tensors, they would be ``at::Tensor`` in C++ and we can use ``at::toDLPackNonOwning`` to get a ``DLTensor`` view. For custom tensor types, you might need to implement the conversion yourself.\n+\n+For other basic types, you can directly pass them and let TVM-FFI handle the conversion implicitly. You are unlikely to need to convert them manually, since they are general types that are widely recognized.\n+\n+See `layout <https://tvm.apache.org/ffi/concepts/any.html#layout>`_ for more detail on how TVM-FFI's ``Any`` type works.\n+\n+See `tensor-classes <https://tvm.apache.org/ffi/concepts/tensor.html#tensor-classes>`_ for more detail on how DLPack tensors and TVM-FFI tensors convert between each other.\n+\n+\n Exporting Compiled Module\n -------------------------\n \n@@ -586,6 +747,39 @@ The exported object file exposes the function symbol ``__tvm_ffi_add_one`` that\n compatible with TVM FFI and can be used in various frameworks and programming languages.\n You can either build a shared library and load it back, or link the object file directly\n into your application and invoke the function via the ``InvokeExternC`` mechanism in TVM FFI.\n+\n+How it works is very similar to the C++ example above. The only difference is that instead of looking up the function from the TVM-FFI global registry,\n+the TVM-FFI function symbol is now exposed via a shared library:\n+\n+.. code-block:: cpp\n+\n+ extern \"C\" int __tvm_ffi_add_one(void*, const TVMFFIAny*, int32_t, TVMFFIAny*);\n+\n+ // If the tvm-ffi function symbol is already known at compile time and it's dynamically linked (or statically linked if you build a static library),\n+ // then you can directly call the function via the exposed symbol via extern C.\n+ void apply_tvm_function_via_extern_C(at::Tensor &a, at::Tensor &b){\n+ DLTensor dl_a = {};\n+ DLTensor dl_b = {};\n+ at::toDLPackNonOwning(a, &dl_a);\n+ at::toDLPackNonOwning(b, &dl_b);\n+ tvm::ffi::Function::InvokeExternC(nullptr, __tvm_ffi_add_one, &dl_a, &dl_b);\n+ }\n+\n+ // If the tvm-ffi function symbol is not known until runtime, you can resolve it from its shared library at\n+ // runtime by giving the library path and function name (dynamic loading).\n+ void apply_tvm_function_via_dynamic_resolution(const std::string& lib_path,\n+ const std::string& func_name,\n+ at::Tensor &a, at::Tensor &b){\n+ tvm::ffi::Module mod = tvm::ffi::Module::LoadFromFile(lib_path);\n+ tvm::ffi::Function fn = mod->GetFunction(func_name).value();\n+ DLTensor dl_a = {};\n+ DLTensor dl_b = {};\n+ at::toDLPackNonOwning(a, &dl_a);\n+ at::toDLPackNonOwning(b, &dl_b);\n+ fn(&dl_a, &dl_b);\n+ }\n+\n+\n For more information, see the `quick start guide <https://tvm.apache.org/ffi/get_started/quickstart>`_\n in the official documentation.\n "
}
]
}