Skip to content

Add triton_metadata_redirect module with with_metadata_path decorator - #1172

Merged
valarLip merged 2 commits into
ROCm:mainfrom
jwu10003:jian.wu.triton_metadata_redirect
Oct 15, 2025
Merged

valarLip merged 2 commits into
ROCm:mainfrom
jwu10003:jian.wu.triton_metadata_redirect

Conversation

@jwu10003

@jwu10003 jwu10003 commented Oct 13, 2025 •

Copy link
Copy Markdown
Contributor

Core Features:

  • with_custom_metadata_path() decorator for custom kernel metadata storage
  • Thread-safe global registry for kernel-to-directory mappings
  • Automatic patching of CompiledKernel.init method
  • Full backward compatibility with existing @triton.jit usage

Usage

Dump Custom .json/.hsaco Files to a Custom Directory

First you need to dump the metadata files to a custom directory(copy ~/.triton/cache/{hash}/{kernel_name}.json and ~/.triton/cache/{hash}/{kernel_name}*.hsaco to a custom directory).

For JIT Compilation (Using Decorator)

from aiter.utility.triton.triton_metadata_redirect import with_custom_metadata_path
import triton
import triton.language as tl

@with_custom_metadata_path("/custom/path")
@triton.jit
def my_kernel(x_ptr, y_ptr, output_ptr, n_elements, BLOCK_SIZE: tl.constexpr):
    # Your kernel implementation
    pid = tl.program_id(0)
    # ... kernel logic

For AOT Compilation (Using Context Manager)

# kernel.py
import triton
import triton.language as tl

@triton.jit
def kr_example(x_ptr, y_ptr, output_ptr, n_elements, BLOCK_SIZE: tl.constexpr):
  return


# aot_compile.py
import triton.language as tl
from triton.backends.compiler import GPUTarget
from triton.tools.compile import compile_kernel, CompileArgs

from aiter.utility.triton.triton_metadata_redirect import AOTMetadataContext

target = GPUTarget("hip", 'gfx942', 64)

################################################################################
## compile aot kernel0:f32
compile_args = CompileArgs(
    path=f"/path/to/kernel.py",
    kernel_name="kr_example",
    signature=f"*fp32:16,*fp32:16,*fp32:16,i32,64",
    grid="bs,nheads,max_seqlen_q",
    num_warps=4,
    num_stages=2,
    out_name="f32_kernel",
)

with AOTMetadataContext("kr_example", "/path/to/aot0"):
    triton_kernel0, output_files0 = compile_kernel(compile_args)

################################################################################
## compile aot kernel1:f16
compile_args = CompileArgs(
    path=f"/path/to/kernel.py",
    kernel_name="kr_example",
    signature=f"*fp16:16,*fp16:16,*fp16:16,i32,32",
    grid="bs,nheads,max_seqlen_q",
    num_warps=2,
    num_stages=2,
    out_name="f16_kernel",
)

with AOTMetadataContext("kr_example", "/path/to/aot1"):
    triton_kernel1, output_files1 = compile_kernel(compile_args)

For Separate Compile And Run

import triton.language as tl
from triton.backends.compiler import GPUTarget
from triton.tools.compile import compile_kernel, CompileArgs

from aiter.utility.triton.triton_metadata_redirect import AOTMetadataContext

@triton.jit
def kernel(x_ptr, SIZE: tl.constexpr):
    ...

# compile
target = GPUTarget("hip", 'gfx942', 64)
src = triton.compiler.ASTSource(
    fn=kernel
    signature = {
        "x_ptr": "*fp32",
    },
    constexprs={"SIZE": 64}
)
with AOTMetadataContext("kernel", "/custom/path"):
    kernel = triton.compile(src, target=target)

# run
x = torch.zeros((1, 1, 2), dtype=torch.float32, device='cuda')
kernel[(1, 1, 1)](x)

Motivation

Users hope to be able to use the kernel metadata files (.json and.hsaco) compiled by the new version of Triton to achieve performance optimizations of the new version without upgrading the old version of Triton.

Technical Details

The module patches the CompiledKernel.__init__ method to intercept metadata file path assignments during Triton kernel compilation. When a kernel is registered (either via decorator or context manager), the module redirects the output paths for:

  • {kernel_name}.json - Kernel metadata file

  • {kernel_name}.hsaco - Compiled kernel binary

NOTE

Critical: For the same kernel name with different template parameters, you must store the generated .json and .hsaco files in separate directories. The module does not differentiate between kernel variants based on template parameters - it only uses the kernel name.

This module implements a simple kernel name to directory mapping without considering compilation parameter variations. In order to handle multiple instantiations, we need to include information about the compilation parameters in the path. However, due to the instability of the hash value (which depends on the environment such as TRITON_PATH, ENV, etc.), we haven't found a good way to deal with it for now.

  • Hash Instability: Triton's hash values depend on TRITON_PATH and environment variables, making them unreliable for matching

  • Functional Equivalence: Even kernels with identical functionality may have different hashes due to external factors

  • Simplicity: The name-based approach provides a straightforward mapping mechanism

Test Plan

  • test for JIT Compilation

  • test for AOT Compilation

  • test for Separate Compile And Run

Test Result

pass python op_tests/triton_tests/triton_metadata_redirect/test_metadata_redirect.py

Submission Checklist

Core Features:
- `with_custom_metadata_path()` decorator for custom kernel metadata storage
- Thread-safe global registry for kernel-to-directory mappings
- Automatic patching of CompiledKernel.__init__ method
- Full backward compatibility with existing @triton.jit usage

Example usage:
```python
from aiter.utility.triton.triton_metadata_redirect import with_custom_metadata_path

@with_custom_metadata_path("/custom/metadata/path")
@triton.jit
def my_kernel(...):
    ...
```
@jwu10003
jwu10003 force-pushed the jian.wu.triton_metadata_redirect branch from 55a1251 to 8fb645c Compare October 13, 2025 03:08
@jwu10003

Copy link
Copy Markdown
Contributor Author

meet Setup-Triton fail:
image

It seems to be a CI environment issue, so re - trigger the CI.

@Dewei-Wang-sh Dewei-Wang-sh 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.

lgtm

@valarLip valarLip left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@valarLip
valarLip merged commit 1944dd2 into ROCm:main Oct 15, 2025
azaidy pushed a commit that referenced this pull request Oct 17, 2025
…#1172)

* Add triton_metadata_redirect module with with_metadata_path decorator

Core Features:
- `with_custom_metadata_path()` decorator for custom kernel metadata storage
- Thread-safe global registry for kernel-to-directory mappings
- Automatic patching of CompiledKernel.__init__ method
- Full backward compatibility with existing @triton.jit usage

Example usage:
```python
from aiter.utility.triton.triton_metadata_redirect import with_custom_metadata_path

@with_custom_metadata_path("/custom/metadata/path")
@triton.jit
def my_kernel(...):
    ...
```

* update code format
eliotwang pushed a commit to eliotwang/aiter that referenced this pull request Oct 21, 2025
…ROCm#1172)

* Add triton_metadata_redirect module with with_metadata_path decorator

Core Features:
- `with_custom_metadata_path()` decorator for custom kernel metadata storage
- Thread-safe global registry for kernel-to-directory mappings
- Automatic patching of CompiledKernel.__init__ method
- Full backward compatibility with existing @triton.jit usage

Example usage:
```python
from aiter.utility.triton.triton_metadata_redirect import with_custom_metadata_path

@with_custom_metadata_path("/custom/metadata/path")
@triton.jit
def my_kernel(...):
    ...
```

* update code format
sjfeng1999 pushed a commit that referenced this pull request Oct 28, 2025
…#1172)

* Add triton_metadata_redirect module with with_metadata_path decorator

Core Features:
- `with_custom_metadata_path()` decorator for custom kernel metadata storage
- Thread-safe global registry for kernel-to-directory mappings
- Automatic patching of CompiledKernel.__init__ method
- Full backward compatibility with existing @triton.jit usage

Example usage:
```python
from aiter.utility.triton.triton_metadata_redirect import with_custom_metadata_path

@with_custom_metadata_path("/custom/metadata/path")
@triton.jit
def my_kernel(...):
    ...
```

* update code format
sjfeng1999 pushed a commit that referenced this pull request Oct 31, 2025
…#1172)

* Add triton_metadata_redirect module with with_metadata_path decorator

Core Features:
- `with_custom_metadata_path()` decorator for custom kernel metadata storage
- Thread-safe global registry for kernel-to-directory mappings
- Automatic patching of CompiledKernel.__init__ method
- Full backward compatibility with existing @triton.jit usage

Example usage:
```python
from aiter.utility.triton.triton_metadata_redirect import with_custom_metadata_path

@with_custom_metadata_path("/custom/metadata/path")
@triton.jit
def my_kernel(...):
    ...
```

* update code format
sjfeng1999 pushed a commit that referenced this pull request Oct 31, 2025
…#1172)

* Add triton_metadata_redirect module with with_metadata_path decorator

Core Features:
- `with_custom_metadata_path()` decorator for custom kernel metadata storage
- Thread-safe global registry for kernel-to-directory mappings
- Automatic patching of CompiledKernel.__init__ method
- Full backward compatibility with existing @triton.jit usage

Example usage:
```python
from aiter.utility.triton.triton_metadata_redirect import with_custom_metadata_path

@with_custom_metadata_path("/custom/metadata/path")
@triton.jit
def my_kernel(...):
    ...
```

* update code format
valarLip pushed a commit that referenced this pull request Mar 18, 2026
…#1172)

* Add triton_metadata_redirect module with with_metadata_path decorator

Core Features:
- `with_custom_metadata_path()` decorator for custom kernel metadata storage
- Thread-safe global registry for kernel-to-directory mappings
- Automatic patching of CompiledKernel.__init__ method
- Full backward compatibility with existing @triton.jit usage

Example usage:
```python
from aiter.utility.triton.triton_metadata_redirect import with_custom_metadata_path

@with_custom_metadata_path("/custom/metadata/path")
@triton.jit
def my_kernel(...):
    ...
```

* update code format
@brunomazzottiamd brunomazzottiamd mentioned this pull request Mar 24, 2026
9 of 11 tasks
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