-
Notifications
You must be signed in to change notification settings - Fork 2.9k
[triton-ext][FrontEnd] Custom DSL Plugin Ops #9626
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
35a1a64
[triton-ext] Reapply custom-ops changes for Triton Extensions
CRobeck a199240
turn custom_ops.py into a pytest
plotfi ad3a1cb
update test
CRobeck b2afcc3
Add import for os module in custom_ops.py
CRobeck 2d4d995
more clean up
CRobeck 729b5ad
Make custom ops callable with variable arguments
plotfi fc3f367
lint
CRobeck d4cfb54
clang-format toggle to prevent excessive changes due to pre-commit
plotfi 9aa0f2e
lint
plotfi a1d955d
adding add pass arguments for things like num_warps, num_stages etc
plotfi f7f8af5
lint
plotfi 21a2aae
make 0 param passes look and feel normal for adding to pipeline
plotfi e743b35
refactor plugin arg intrfaces to have a simple point of truth
plotfi cbd9e33
drop _count
plotfi be14d33
fix dialect count bug
plotfi 9dad391
drop builtin since it comes from triton core
plotfi e3d7a86
clean up custom ops interface
plotfi 11f4cc4
clean up custom ops interface 2
plotfi 96a4381
move opt arg list to stringified list
plotfi 9c0eeef
drop _count
plotfi 4b76d1a
cleanup addCustomOp
plotfi 8927563
oops, fix out of bounds error for no args passed
plotfi 11126d7
clean up formatting
CRobeck b3bfde6
drop test bloat
plotfi 6224224
undo NFC changes for API interface macros
plotfi 157d710
Fix failure in python/test/unit/plugins/test_plugin.py when custom op…
plotfi 243af4b
further simplification
plotfi 6cb3788
drop getBuilder()
plotfi 99610dd
clang-format off removed + lint
plotfi 49acef8
improve whitespace
plotfi d671287
Merge branch 'main' into custom_dialect_ops
CRobeck 059fa21
Merge branch 'main' into custom_dialect_ops
CRobeck e5c6ef6
Remove unused functions from custom_ops.py
CRobeck 1864dc6
address review comments
CRobeck db91a8d
address review comments
CRobeck 7d03b4c
Merge branch 'main' into custom_dialect_ops
CRobeck b0665a4
Merge branch 'main' into custom_dialect_ops
plotfi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import torch | ||
|
|
||
| import triton | ||
| import triton.language as tl | ||
| from triton._C.libtriton import ir | ||
| from triton.language.core import builtin | ||
| from typing import TypeVar, Type | ||
| import builtins | ||
| import os | ||
| import pathlib | ||
| from triton.compiler.code_generator import flatten_values_to_ir | ||
|
|
||
| T = TypeVar('T') | ||
| TensorTy = TypeVar('TensorTy') | ||
|
|
||
| triton.language.__all__.append("custom_op") | ||
| tensor: Type[TensorTy] = tl.tensor | ||
| builder: ir.builder | ||
|
|
||
| TRITON_BUILTIN = "__triton_builtin__" | ||
|
|
||
|
|
||
| def _unwrap_if_constexpr(o): | ||
| if isinstance(o, list): | ||
| return [_unwrap_if_constexpr(x) for x in o] | ||
| if isinstance(o, builtins.tuple): | ||
| return builtins.tuple(_unwrap_if_constexpr(x) for x in o) | ||
| if isinstance(o, tuple): | ||
| return tuple(_unwrap_if_constexpr(x) for x in o) | ||
| return o.value if isinstance(o, tl.constexpr) else o | ||
|
|
||
|
|
||
| DEVICE = triton.runtime.driver.active.get_active_torch_device() | ||
|
|
||
|
|
||
| @builtin | ||
| def custom_op(x, sanitize_overflow: tl.constexpr = True, _semantic=None): | ||
| x = _unwrap_if_constexpr(x) | ||
| builder = _semantic.builder | ||
| arg_handles = [] | ||
| arg_handles.extend(flatten_values_to_ir([x])) | ||
| return tl.tensor(builder.create_custom_op(arg_handles), x.type) | ||
|
|
||
|
|
||
| @triton.jit | ||
| def add_kernel( | ||
| x_ptr, | ||
| output_ptr, | ||
| n_elements, | ||
| BLOCK_SIZE: tl.constexpr, | ||
| ): | ||
| pid = tl.program_id(axis=0) | ||
| block_start = pid * BLOCK_SIZE | ||
| offsets = block_start + tl.arange(0, BLOCK_SIZE) | ||
| mask = offsets < n_elements | ||
| x = tl.load(x_ptr + offsets, mask=mask) | ||
| output = custom_op(x) | ||
| tl.store(output_ptr + offsets, output, mask=mask) | ||
|
|
||
|
|
||
| def test_custom_ops(tmp_path: pathlib.Path): | ||
| if os.environ.get('LLVM_BUILD_SHARED_LIBS', '0') == '0': | ||
| return | ||
| size = 8 | ||
| x = torch.zeros(size, device=DEVICE, dtype=torch.float32) | ||
| output_triton = torch.empty_like(x) | ||
| n_elements = output_triton.numel() | ||
| grid = lambda meta: (triton.cdiv(n_elements, meta['BLOCK_SIZE']), ) | ||
| h = add_kernel[grid](x, output_triton, n_elements, BLOCK_SIZE=32) | ||
|
|
||
| src = h.asm["source"] | ||
| assert "arith.addf" in src | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.