Skip to content

[REFACTOR][RUNTIME] Transition metadata into ffi#18784

Merged
tqchen merged 1 commit into
apache:mainfrom
tqchen:any
Feb 16, 2026
Merged

[REFACTOR][RUNTIME] Transition metadata into ffi#18784
tqchen merged 1 commit into
apache:mainfrom
tqchen:any

Conversation

@tqchen

@tqchen tqchen commented Feb 15, 2026

Copy link
Copy Markdown
Member

This PR migrates the metadata storage to use ffi data structures.
This allows us to modernize the storage and conversions of these
data structures and benefit from the core ffi infra.

We also updates the json storage convention to store int/double
as normal int double instead of converting to string as now
the ffi infra can reliably handle those cases.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @tqchen, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request undertakes a significant refactoring effort to modernize how metadata is stored and managed within TVM's runtime and codegen components. By migrating to FFI-native data structures like ffi::Array and ffi::Map for attributes and function information, the system benefits from improved consistency, streamlined data handling, and better integration with the core FFI infrastructure. This change impacts various backend codegen and runtime modules, ensuring a more robust and future-proof metadata management system.

Highlights

  • FFI Container Integration: The pull request transitions various internal metadata structures, including node attributes and function information, to utilize TVM's Foreign Function Interface (FFI) containers such as ffi::String, ffi::Array, and ffi::Map. This change modernizes data storage and conversion mechanisms.
  • Metadata Refactoring: The FunctionInfo struct has been refactored into an Object and ObjectRef (FunctionInfoObj and FunctionInfo) to align with TVM's object system, and its serialization logic has been updated accordingly. The meta_data.h header was also renamed to metadata.h.
  • Serialization Updates: New Serializer template specializations were added for ffi::String, ffi::Array<T>, and ffi::Map<K, V> to enable seamless serialization and deserialization of these FFI types.
  • Attribute Handling Modernization: Numerous codegen and runtime files were updated to replace legacy std::vector<std::string> and std::vector<std::any> usages for node attributes with the more efficient and FFI-compatible ffi::Array<ffi::String>.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • include/tvm/support/serializer.h
    • Added Serializer specializations for ffi::String, ffi::Array<T>, and ffi::Map<K, V>.
    • Included new FFI container headers.
  • src/contrib/msc/core/codegen/codegen_json.cc
    • Updated AddNodeAttr to use ffi::Array<ffi::String> for node attributes.
  • src/relax/backend/contrib/clml/codegen.cc
    • Replaced std::vector<std::string> and std::vector<std::any> with ffi::Array<ffi::String> for setting node attributes.
  • src/relax/backend/contrib/codegen_json/codegen_json.h
    • Modified OpAttrExtractor to use ffi::Array<ffi::String> for SetNodeAttr and ffi::Any handling.
    • Updated SetShape and SetDType to directly use internal members.
  • src/relax/backend/contrib/cublas/codegen.cc
    • Changed dq_scale attribute setting to use ffi::Array<ffi::String>.
  • src/relax/backend/contrib/cudnn/codegen.cc
    • Updated attribute setting for various parameters to use ffi::Array<ffi::String>.
  • src/relax/backend/contrib/nnapi/codegen.cc
    • Replaced std::vector<std::any> with direct ffi::Array<ffi::String> for several attributes.
  • src/relax/backend/contrib/tensorrt/codegen.cc
    • Updated compiler configuration attributes to use ffi::Array<ffi::String>.
  • src/relax/backend/vm/codegen_vm.cc
    • Added TVM_FFI_UNREACHABLE() after a fatal log message.
  • src/runtime/contrib/arm_compute_lib/acl_runtime.cc
    • Changed GetAttr calls to retrieve ffi::Array<ffi::String> for various attributes.
  • src/runtime/contrib/bnns/bnns_json_runtime.cc
    • Updated GetAttr calls to retrieve ffi::Array<ffi::String> for various attributes.
  • src/runtime/contrib/clml/clml_runtime.cc
    • Modified GetAttr calls to retrieve ffi::Array<ffi::String> for numerous attributes.
  • src/runtime/contrib/cublas/cublas_json_runtime.cc
    • Updated GetAttr call for dq_scale to retrieve ffi::Array<ffi::String>.
  • src/runtime/contrib/cudnn/cudnn_json_runtime.cc
    • Changed vstr2vint and direct GetAttr calls to retrieve ffi::Array<ffi::String> for attributes.
  • src/runtime/contrib/dnnl/dnnl_json_runtime.cc
    • Modified GetNodeAttr to retrieve ffi::Array<ffi::String>.
  • src/runtime/contrib/json/json_node.h
    • Changed JSONGraphAttrs from std::unordered_map<std::string, std::any> to ffi::Map<ffi::String, ffi::Any>.
    • Removed std::any related helper functions.
    • Updated JSONGraphNode's ToJSON and FromJSON methods for new FFI types.
    • Added SetAttr overloads for std::string and ffi::Array<ffi::String>.
    • Added SetShape and SetDType methods.
  • src/runtime/contrib/msc/tensorrt_runtime.cc
    • Updated GetAttr calls to retrieve ffi::Array<ffi::String> for global options.
  • src/runtime/contrib/nnapi/nnapi_ops.cc
    • Modified GetAttr calls to retrieve ffi::Array<ffi::String> for various attributes.
  • src/runtime/contrib/tensorrt/tensorrt_ops.cc
    • Updated GetAttr calls to retrieve ffi::Array<ffi::String> for numerous operator attributes.
  • src/runtime/contrib/tensorrt/tensorrt_runtime.cc
    • Changed GetAttr calls to retrieve ffi::Array<ffi::String> for runtime configuration.
  • src/runtime/cuda/cuda_module.cc
    • Renamed meta_data.h to metadata.h.
    • Changed fmap_ type to ffi::Map<ffi::String, FunctionInfo>.
    • Updated CUDAModuleCreate and CUDAModuleLoadFile signatures.
    • Modified GetFunction to use fmap_.Get and FunctionInfo members via ->.
    • Updated CUDAModuleLoadFromBytes to use ffi::Map and ICHECK for stream read.
  • src/runtime/cuda/cuda_module.h
    • Renamed meta_data.h to metadata.h.
    • Updated CUDAModuleCreate signature.
  • src/runtime/file_utils.cc
    • Removed std::unordered_map include.
    • Refactored FunctionInfo to FunctionInfoObj and FunctionInfo (ObjectRef).
    • Updated SaveToJSON and LoadFromJSON to use ffi::String and ffi::Array.
    • Modified SaveMetaDataToFile and LoadMetaDataFromFile to work with ffi::Map<ffi::String, FunctionInfo>.
    • Added TVM_FFI_STATIC_INIT_BLOCK for FunctionInfoObj reflection.
  • src/runtime/file_utils.h
    • Renamed meta_data.h to metadata.h.
    • Updated SaveMetaDataToFile and LoadMetaDataFromFile signatures.
    • Added using Stream::Read; using Stream::Write; to SimpleBinaryFileStream.
  • src/runtime/hexagon/hexagon_module.cc
    • Updated HexagonModuleNode constructor and HexagonModuleCreate signature to use ffi::Map<ffi::String, FunctionInfo>.
  • src/runtime/hexagon/hexagon_module.h
    • Renamed meta_data.h to metadata.h.
    • Updated HexagonModuleCreate and HexagonModuleNode constructor signatures.
    • Changed fmap_ type to ffi::Map<ffi::String, FunctionInfo>.
  • src/runtime/meta_data.h
    • File renamed to src/runtime/metadata.h.
  • src/runtime/metadata.h
    • Renamed from meta_data.h.
    • Removed std::unordered_map include.
    • Refactored FunctionInfo into FunctionInfoObj (Object) and FunctionInfo (ObjectRef).
    • Updated SaveToJSON and LoadFromJSON methods.
    • Updated Serializer specialization for runtime::FunctionInfo to handle new FFI types.
  • src/runtime/metal/metal_module.h
    • Renamed meta_data.h to metadata.h.
    • Updated MetalModuleCreate signature.
  • src/runtime/metal/metal_module.mm
    • Renamed meta_data.h to metadata.h.
    • Updated MetalModuleNode constructor and fmap_ type.
    • Modified MetalWrappedFunc::Init and MetalModuleNode::GetFunction to use ffi::Array<ffi::String> and ffi::Map::Get.
    • Updated MetalModuleCreate and MetalModuleLoadFromBytes to use ffi::Map.
  • src/runtime/opencl/opencl_common.h
    • Renamed meta_data.h to metadata.h.
    • Updated OpenCLModuleNodeBase constructor and fmap_ type.
    • Updated OpenCLModuleNode constructor.
  • src/runtime/opencl/opencl_module.cc
    • Removed std::unordered_map include.
    • Updated OpenCLWrappedFunc::Init to use ffi::Array<ffi::String>.
    • Modified OpenCLModuleNodeBase::GetFunction to use fmap_.Get and FunctionInfo members via ->.
    • Updated OpenCLModuleCreate and OpenCLModuleLoadFile signatures.
    • Updated OpenCLModuleLoadFromBytes to use ffi::Map and ICHECK for stream read.
  • src/runtime/opencl/opencl_module.h
    • Renamed meta_data.h to metadata.h.
    • Updated OpenCLModuleCreate signatures.
  • src/runtime/opencl/opencl_module_spirv.cc
    • Removed std::unordered_map include.
    • Updated OpenCLSPIRVModuleNode constructor and OpenCLModuleCreate signature to use ffi::Map<ffi::String, FunctionInfo>.
  • src/runtime/pack_args.h
    • Included metadata.h.
    • Updated PackFuncVoidAddr, PackFuncNonBufferArg, PackFuncPackedArgAligned, and NumBufferArgs signatures to use ffi::Array and ArgExtraTags.
  • src/runtime/rocm/rocm_module.cc
    • Renamed meta_data.h to metadata.h.
    • Updated ROCMModuleNode constructor and fmap_ type.
    • Modified ROCMWrappedFunc::Init and ROCMModuleNode::GetFunction to use ffi::Array<ffi::String> and ffi::Map::Get.
    • Updated ROCMModuleCreate and ROCMModuleLoadFile signatures.
    • Updated ROCMModuleLoadFromBytes to use ffi::Map and ICHECK for stream read.
  • src/runtime/rocm/rocm_module.h
    • Renamed meta_data.h to metadata.h.
    • Updated ROCMModuleCreate signature.
  • src/runtime/thread_storage_scope.h
    • Renamed meta_data.h to metadata.h.
    • Updated LaunchParamConfig::Init to use ffi::Array<ffi::String>.
  • src/runtime/vulkan/vulkan_module.cc
    • Updated VulkanModuleCreate and VulkanModuleLoadFile signatures to use ffi::Map<ffi::String, FunctionInfo>.
    • Updated VulkanModuleLoadFromBytes to use ffi::Map and ICHECK for stream read.
  • src/runtime/vulkan/vulkan_module.h
    • Renamed meta_data.h to metadata.h.
    • Updated VulkanModuleCreate signature.
  • src/runtime/vulkan/vulkan_wrapped_func.cc
    • Modified VulkanWrappedFunc::Init and VulkanModuleNode::GetFunction to use ffi::Array<ffi::String> and ffi::Map::Get.
    • Updated VulkanModuleNode::GetPipeline to use fmap_.Get and access FunctionInfo members via ->.
  • src/runtime/vulkan/vulkan_wrapped_func.h
    • Renamed meta_data.h to metadata.h.
    • Updated VulkanWrappedFunc::Init and VulkanModuleNode constructor signatures.
    • Changed fmap_ type to ffi::Map<ffi::String, FunctionInfo>.
  • src/target/build_common.h
    • Included tvm/ffi/container/map.h.
    • Updated ExtractFuncInfo to return ffi::Map<ffi::String, runtime::FunctionInfo> and construct runtime::FunctionInfo using the new ObjectRef constructor.
  • src/target/opt/build_cuda_off.cc
    • Updated CUDAModuleCreate signature to use ffi::Map<ffi::String, FunctionInfo>.
  • src/target/opt/build_hexagon_off.cc
    • Updated HexagonModuleCreate signature to use ffi::Map<ffi::String, FunctionInfo>.
  • src/target/opt/build_metal_off.cc
    • Updated MetalModuleCreate signature to use ffi::Map<ffi::String, FunctionInfo>.
  • src/target/opt/build_opencl_off.cc
    • Updated OpenCLModuleCreate signatures to use ffi::Map<ffi::String, FunctionInfo>.
  • src/target/opt/build_rocm_off.cc
    • Updated ROCMModuleCreate signature to use ffi::Map<ffi::String, FunctionInfo>.
  • src/target/source/codegen_source_base.h
    • Renamed meta_data.h to metadata.h.
    • Updated DeviceSourceModuleCreate signature.
  • src/target/source/codegen_webgpu.cc
    • Removed std::unordered_map include.
    • Included runtime/file_utils.h and runtime/metadata.h.
    • Modified CodeGenWebGPU::AddFunction to construct runtime::FunctionInfo using the new ObjectRef constructor and use ffi::Array for arg_types and launch_param_tags.
    • Updated WebGPUSourceModuleNode constructor and InspectSource to use ffi::Map and access FunctionInfo members via ->.
    • Updated BuildWebGPU to use ffi::Map.
  • src/target/source/source_module.cc
    • Removed std::unordered_map include.
    • Updated DeviceSourceModuleNode constructor and fmap_ type.
    • Updated DeviceSourceModuleCreate signature.
  • web/emcc/webgpu_runtime.cc
    • Included runtime/file_utils.h and runtime/metadata.h.
    • Updated WebGPUModuleNode constructor and fmap_ type.
    • Modified GetFunction and InspectSource to use ffi::Map and access FunctionInfo members via ->.
    • Updated WebGPUModuleLoadFromBytes to use ffi::Map and ICHECK for stream read.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@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 PR is a large refactoring to migrate metadata storage to use FFI data structures like ffi::Map, ffi::Array, and ffi::Any. This modernizes the storage and conversion of these data structures. The changes are widespread across the codebase, touching runtime modules for various backends (CUDA, ROCm, OpenCL, Metal, Vulkan, etc.) and JSON serialization logic. The core change is the refactoring of FunctionInfo from a plain struct to a TVM ObjectRef, and using ffi::Map instead of std::unordered_map for function info maps. Most of the changes are correct and improve code clarity and consistency. I've found one critical bug that will break compilation and one opportunity for optimization.

Comment thread src/runtime/contrib/dnnl/dnnl_json_runtime.cc Outdated
Comment thread include/tvm/support/serializer.h
@tqchen

tqchen commented Feb 15, 2026

Copy link
Copy Markdown
Member Author

/gemini review

@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 is a large-scale refactoring that migrates metadata storage to use FFI data structures, such as ffi::String, ffi::Array, and ffi::Map. This is a significant and positive change that modernizes data handling and serialization throughout the codebase. The core of the refactoring involves changing FunctionInfo to an FFI-compatible object and updating its usage across various runtime and backend components. While the refactoring is generally well-executed, I've identified several compilation errors in contrib runtimes (nnapi, arm_compute_lib, clml, tensorrt) where ffi::Array<ffi::String> is now used in places that still expect std::vector<std::string>. These issues need to be addressed to ensure the code compiles successfully.

Comment thread src/relax/backend/contrib/nnapi/codegen.cc Outdated
Comment thread src/relax/backend/contrib/nnapi/codegen.cc Outdated
Comment thread src/relax/backend/contrib/nnapi/codegen.cc Outdated
Comment thread src/relax/backend/contrib/nnapi/codegen.cc Outdated
Comment thread src/relax/backend/contrib/nnapi/codegen.cc Outdated
Comment thread src/runtime/contrib/clml/clml_runtime.cc Outdated
Comment thread src/runtime/contrib/tensorrt/tensorrt_ops.cc Outdated
Comment thread src/runtime/contrib/tensorrt/tensorrt_ops.cc Outdated
Comment thread src/runtime/contrib/tensorrt/tensorrt_ops.cc Outdated
Comment thread src/runtime/contrib/tensorrt/tensorrt_ops.cc Outdated
@tqchen

tqchen commented Feb 15, 2026

Copy link
Copy Markdown
Member Author

/gemini review

@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 is a large-scale refactoring that transitions metadata storage to use FFI data structures like ffi::Map, ffi::Array, and ffi::Any. This is a significant and positive change that modernizes the codebase, improving interoperability and consistency. The changes are applied systematically across many files. I've identified a bug in the array type detection logic in codegen_json.h that could lead to a crash, and a minor inefficiency in clml_runtime.cc. With these issues addressed, this PR will be a solid improvement.

Comment thread src/relax/backend/contrib/codegen_json/codegen_json.h Outdated
Comment thread src/runtime/contrib/clml/clml_runtime.cc Outdated
@tqchen tqchen force-pushed the any branch 4 times, most recently from 762392f to 2868edd Compare February 16, 2026 01:59
This PR migrates the metadata storage to use ffi data structures.
This allows us to modernize the storage and conversions of these
data structures and benefit from the core ffi infra.

We also updates the json storage convention to store int/double
as normal int double instead of converting to string as now
the ffi infra can reliably handle those cases.
@tqchen tqchen merged commit c1b6e50 into apache:main Feb 16, 2026
9 checks passed
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.

2 participants