Skip to content

Revamp metadata writing and add kernel id#471

Merged
danieldk merged 10 commits into
mainfrom
kernel-id-metadata
Apr 20, 2026
Merged

Revamp metadata writing and add kernel id#471
danieldk merged 10 commits into
mainfrom
kernel-id-metadata

Conversation

@danieldk
Copy link
Copy Markdown
Member

Changes in kernel-builder:

  • Strongly type the kernel identifier within kernel-builder.
  • Write the kernel identifier to the kernel metadata.
  • Make noarch kernels include the backend in the identifier as well.
  • Fix tvm-ffi build-time metadata update to include the arches.
  • Rename build-time metadata update script to be more general.
  • Run the build-time metadata update script for non-CUDA/ROCm backends. This is not strictly necessary now, but allows us to add other metadata bits in the future.
  • Add a build hook that validates the metadata after build using the Rust-side data structures. This ensures that build-time changes are valid and schema-conforming.

Changes in kernels:

  • Support the kernel id metadata.
  • Use the kernel id (when available) as the module identifier in sys.modules, avoiding the path hash.

* Strongly type the kernel identifier within kernel-builder.
* Write the kernel identifier to the kernel metadata.
* Make noarch kernels include the backend in the identifier as well.
* Fix tvm-ffi build-time metadata update to include the arches.
* Rename build-time metadata update script to be more general.
* Run the build-time metadata update script for non-CUDA/ROCm backends.
  This is not strictly necessary now, but allows us to add other
  metadata bits in the future.
* Add a build hook that validates the metadata after build using
  the Rust-side data structures. This ensures that build-time changes
  are valid and schema-conforming.
This change adds support for kernel id metadata and uses the kernel id
(when present) as the identifier in the Python module table in place of
the path hash.
@danieldk danieldk marked this pull request as ready for review April 17, 2026 11:06
@HuggingFaceDocBuilderDev
Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Copy Markdown
Member

@sayakpaul sayakpaul left a comment

Choose a reason for hiding this comment

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

Thanks!

I left some questions. Does it affect #465?

Also, we should probably add some tests as well?

Comment thread docs/source/kernel-requirements.md
Comment thread docs/source/kernel-requirements.md Outdated
Comment thread docs/source/kernel-requirements.md Outdated
Comment thread kernel-builder/src/pyproject/templates/torch/noarch/_ops.py

_OPS_NAME = _find_ops_name()

ops = getattr(torch.ops, _OPS_NAME)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Help me unpack this a little. _find_ops_name() seems to have placeholders that are not fully resolved at this point. Same for add_op_namespace_prefix()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

These are filled by kernel-builder:

env.get_template("torch/noarch/_ops.py")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oh cool. Perhaps a small explainer comment could helpful for future reference and it's not immediately clear where this will be used. No strong opinions.

Comment thread kernel-builder/src/pyproject/templates/torch/add_build_metadata.py
Comment thread kernels/src/kernels/metadata.py
Comment thread kernels/src/kernels/metadata.py
Comment thread kernels/src/kernels/metadata.py
kernels-data is going to be used by `kernels`, so we need to make sure
that we can also read kernels that do not have an id yet.
Copy link
Copy Markdown
Member

@sayakpaul sayakpaul left a comment

Choose a reason for hiding this comment

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

Looks great! I think it'd be good to have the tests for kernels to include some tests for the changes introduced?

path_hash = "{:x}".format(ctypes.c_size_t(hash(file_path)).value)
module_name = f"{module_name}_{path_hash}"
spec = importlib.util.spec_from_file_location(module_name, file_path)
if metadata.id is None:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should a warning be thrown in line with the previous warning?

f"Kernel loaded from `{variant_path}` does not have metadata,"

Or is it a fair assumption that LoadedKernel can't always guarantee metadata.id?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The warning will already by emitted by:

@danieldk danieldk merged commit 1fb18d4 into main Apr 20, 2026
64 of 66 checks passed
@danieldk danieldk deleted the kernel-id-metadata branch April 20, 2026 07:51
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.

3 participants