Skip to content

[RUNTIME][FFI][RELAX] Add vm.builtin.shape_to_tensor builtin#19849

Open
cbalint13 wants to merge 1 commit into
apache:mainfrom
cbalint13:tvm-vm-runtime
Open

[RUNTIME][FFI][RELAX] Add vm.builtin.shape_to_tensor builtin#19849
cbalint13 wants to merge 1 commit into
apache:mainfrom
cbalint13:tvm-vm-runtime

Conversation

@cbalint13

@cbalint13 cbalint13 commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

Implements vm.builtin.shape_to_tensor builtin to runtime.
Adds hook for relax to builtin lowering, siding with the existing vm.builtin.tensor_to_shape builtin.

Replaces python relax.run.shape_to_tensor variant of the function.


Issue

Attempted to do inference with a c++ only deployment (no public API yet) and discovered the following issue:

#include <ffi/extra/module.h>
#include <runtime/vm/executable.h>
#include <runtime/vm/vm.h>

int main() {
  auto mod_dso = tvm::ffi::Module::LoadFromFile("./lib.tar.so");
  auto vme = mod_dso->GetFunction("vm_load_executable", false);

  auto mod = (*vme)().cast<tvm::ffi::Module>();
  tvm::ffi::Optional<tvm::ffi::Function> vm_ini = mod->GetFunction("vm_initialization");
  (*vm_ini)(static_cast<int>(kDLCPU),
            static_cast<int>(0),
            static_cast<int>(tvm::runtime::AllocatorType::kPooled),
            static_cast<int>(kDLCPU),
            0,
            static_cast<int>(tvm::runtime::AllocatorType::kPooled));
  return 0;
}

Exported DSO contains a relax.run.shape_to_tensor call that works in python env but not in pure runtime:

$ c++ -I/usr/include/tvm dso-run-tvm.cpp -o dso-run-tvm -ltvm_ffi -ltvm_runtime

$ ./dso-run-tvm
{...}
DBG ModuleObj::GetFunction() [vm_load_executable][0]
HERE [vm_load_executable][ffi.Function]
DBG ModuleObj::GetFunction() [invoke_closure][0]
HERE [invoke_closure][ffi.Function]
{...}
DBG ModuleObj::GetFunction() [vm.builtin.shape_of][1]
DBG ModuleObj::GetFunction() [relax.run.shape_to_tensor][1] <- HERE

tvm.error.InternalError: Check failed: (func.has_value()) is false: Error: 
Cannot find ffi::Function relax.run.shape_to_tensor in either Relax VM kernel library,
or in TVM runtime ffi::Function registry, or in global Relax functions of the VM executable

$ strings lib.tar.so | grep relax.run
relax.run.shape_to_tensor

$

Solution

The relax.run.shape_to_tensor (python only) is replaced with more appropriate vm.builtin.shape_to_tensor.


Results

[x] The exported DSO is instantiable in a pure C++ env having only tvm_runtime.so + tvm_ffi.so .

$ strings lib.tar.so | grep shape_to_tens
vm.builtin.shape_to_tensor

[x] The inference speed went up by almost 2x factor (for a RNN, small input size), even in a python program (full env):

  • Before

    [*] Analyzing with chunk size 512 and batch size 1 (Threshold: 0.2)...
    Inference:  48%|██████████▋           | 9074/18737 [00:08<00:08, 1084.33chunk/s]
    
  • After

    [*] Analyzing with chunk size 512 and batch size 1 (Threshold: 0.2)...
    Inference: 100%|█████████████████████| 18737/18737 [00:10<00:00, 1803.61chunk/s]
    

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request replaces the Python-based runtime function relax.run.shape_to_tensor with a C++ implementation vm.builtin.shape_to_tensor in the Virtual Machine builtins. It updates the operator registration, the constant folding pass, and the VM builtin lowering pass to use this new builtin, and adds a corresponding test. Feedback points out a potential runtime type mismatch in fold_constant.cc where arr (an Array<int64_t>) is passed directly to vm.builtin.shape_to_tensor which expects ffi::Shape. It is recommended to explicitly construct a ffi::Shape from arr's elements.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread src/relax/transform/fold_constant.cc Outdated
@cbalint13 cbalint13 marked this pull request as draft June 20, 2026 11:13
@cbalint13 cbalint13 force-pushed the tvm-vm-runtime branch 2 times, most recently from 4baa2fc to 8bf91ff Compare June 20, 2026 11:42
@cbalint13 cbalint13 marked this pull request as ready for review June 20, 2026 12:56
@cbalint13

Copy link
Copy Markdown
Contributor Author

This is ready for review.
Cc @tqchen , @tlopex

@cbalint13 cbalint13 marked this pull request as draft June 20, 2026 13:52
@cbalint13 cbalint13 marked this pull request as ready for review June 20, 2026 15:23
@cbalint13 cbalint13 force-pushed the tvm-vm-runtime branch 2 times, most recently from 2e22d1e to 566cf7b Compare June 20, 2026 15:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant