Skip to content

Allow passing a key/name to the @wp.kernel #1561

Closed

Description

@thomasbbrunner

Description

@wp.kernel derives a kernel's key from make_full_qualified_name(func), which in turn uses the function's __qualname__. There's no way to override it.

The @wp.func decorator already exposes a name argument for the same purpose.

It would be great if we would extend this pattern to the wp.kernel, enabling overriding the kernel's name through an argument: @wp.kernel(key="custom_name")

Context

This is relevant when generating kernels using kernel closures. In this case, different kernels have the same key and only differ by their hashes:

import warp as wp

def make_scaler(factor: float) -> wp.Kernel:
    @wp.kernel
    def scale(x: wp.array(dtype=wp.float32)):
        i = wp.tid()
        x[i] = x[i] * factor
    return scale

double = make_scaler(2.0)
triple = make_scaler(3.0)

print(double.key)  # make_scaler__locals__scale
print(triple.key)  # make_scaler__locals__scale  <-- identical!

In our case, we export these kernels with compile_aot_module with strip_hash=True. This results in a collision of the kernel names. We strip the hashes to enable more flexible access to the exported code.

Proposal

Expose a wp.kernel(key=...) or wp.kernel(name=...) (to be consistent with wp.func) argument, which overrides the kernel's name and skips the sanitation of the name with make_full_qualified_name.

A potential pitfall is if the user passes a name that is not a valid C name. We should make it clear in the docstring which requirements the name needs to satisfy. Alternatively, we could check the user's custom name and raise if it is not valid.

Happy to implement this, if there's interest!

Activity

  1. thomasbbrunner commented on Jun 19, 2026

    @thomasbbrunner
    ContributorAuthor

    Addressed in #1570

  2. added this to the 1.16.0 milestone on Jul 7, 2026
  3. self-assigned this
    on Jul 7, 2026
  4. c0d1f1ed commented on Jul 7, 2026

    @c0d1f1ed
    Contributor

    Thanks for filing this issue and providing a PR!

    I'll take it for internal review and try to get it merged for the 1.16 release. I think I'll make the following refinements:

    • If strip_hash=True causes distinct kernels to have the same exported native name, fail early with a clear message.
    • Clarify the difference between "key" (implementation detail), kernel name, and exported symbol name.
    • Revisit the wp.overload handling.

    EDIT: Let's get the PR merged. I can follow up with the refinements.

  5. modified the milestones: 1.16.0, 1.17.0 on Jul 23, 2026
  6. shi-eric commented on Aug 20, 2026

    @shi-eric
    Contributor

    @thomasbbrunner, thanks for filing this and for putting together #1570. We initially planned to merge that PR, but the kernel-naming change was ultimately folded into the related external-compilation MR instead, so #1570 was not merged separately.

    I am leaving this issue open so you can verify that the merged behavior covers your closure-generated AOT use case.

    Codex-generated summary

    ffce876 landed experimental support for custom kernel names.

    The merged implementation uses @wp.kernel(name=...), consistent with @wp.func, and validates that custom names are valid C++ identifiers. It also handles custom-named overload lookup, including rejecting ambiguous decorator-form lookups. The existing AOT guard remains in place for generic overloads that would collide when strip_hash=True.

    Public API behavior

    • @wp.kernel(name="custom_name") overrides the derived kernel name.
    • Custom names must match [A-Za-z_][A-Za-z0-9_]*.
    • With strip_hash=True, the custom name becomes the base of the exported native symbol.
    • The API is experimental and may change without deprecation.

    Test coverage

    • warp/tests/test_codegen.py adds test_kernel_name_preserves_warp_kernel_abi, test_generic_unique_module_hash_includes_kernel_name, and test_kernel_name_rejects_invalid_identifier.
    • warp/tests/test_generics.py adds test_custom_named_overload_stub and test_ambiguous_custom_named_overload_stub.

    Behavioral verification

    Two closure-generated kernels were given distinct custom names, launched successfully, and compiled together with strip_hash=True. The resulting AOT object contained distinct exported symbols for both kernels.

    Follow-up to consider

    • A committed regression test for two closure-generated named kernels exported with strip_hash=True would pin the exact scenario reported here. The behavior passes in an issue-shaped probe, so this is non-blocking.
  7. thomasbbrunner commented on Aug 27, 2026

    @thomasbbrunner
    ContributorAuthor

    Thanks for the update, @shi-eric. The changes seem to address this. I'll only try them out once the next release is published. We can close the issue and if this ends up not working, we can re-open it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

feature requestRequest for something to be added

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions