forked from hw-native-sys/PTOAS
-
Notifications
You must be signed in to change notification settings - Fork 16
Add merge-axis TileLang element-wise templates #456
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
Open
liuzidi
wants to merge
1
commit into
feature-vpto-backend
Choose a base branch
from
tilelang-dsl-merge-axis
base: feature-vpto-backend
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| # Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| # This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| # CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| # Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| # THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| # INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| # See LICENSE in the root of the software repository for the full text of the License. | ||
|
|
||
| """Shared helpers for full-axis merge-axis TileOp implementations.""" | ||
|
|
||
| import tilelang_dsl as pto | ||
|
|
||
|
|
||
| def full_axis_constraint(**attrs): | ||
| """Match only full-axis tiles whose valid extents equal their allocation shape.""" | ||
| matched_any_tile = False | ||
| for attr_name, shape in attrs.items(): | ||
| if not attr_name.endswith("_shape") or attr_name.endswith("_valid_shape"): | ||
| continue | ||
| valid_shape = attrs.get(attr_name.replace("_shape", "_valid_shape")) | ||
| if valid_shape is None: | ||
| continue | ||
| matched_any_tile = True | ||
| if tuple(shape) != tuple(valid_shape): | ||
| return False | ||
| return matched_any_tile | ||
|
|
||
|
|
||
| @pto.inline_proc | ||
| def emit_unary_merge_axis(src: pto.Tile, dst: pto.Tile, compute): | ||
| """Lower a full-axis unary element-wise kernel through one linearized loop.""" | ||
| dtype = dst.element_type | ||
| valid_rows, valid_cols = dst.valid_shape | ||
| total_elems = valid_rows * valid_cols | ||
| lanes = pto.get_lanes(dtype) | ||
|
|
||
| with pto.strict_vecscope(dst, src, total_elems, 0, total_elems, lanes) as ( | ||
| out_tile, | ||
| in_tile, | ||
| area, | ||
| lb, | ||
| ub, | ||
| step, | ||
| ): | ||
| remained = area | ||
| for lane in range(lb, ub, step): | ||
| mask, remained = pto.make_mask(out_tile.element_type, remained) | ||
| result = compute(pto.vlds(in_tile, lane), mask) | ||
| pto.vsts(result, out_tile, lane, mask) | ||
|
|
||
|
|
||
| @pto.inline_proc | ||
| def emit_binary_merge_axis(src0: pto.Tile, src1: pto.Tile, dst: pto.Tile, compute): | ||
| """Lower a full-axis binary element-wise kernel through one linearized loop.""" | ||
| dtype = dst.element_type | ||
| valid_rows, valid_cols = dst.valid_shape | ||
| total_elems = valid_rows * valid_cols | ||
| lanes = pto.get_lanes(dtype) | ||
|
|
||
| with pto.strict_vecscope(dst, src0, src1, total_elems, 0, total_elems, lanes) as ( | ||
| out_tile, | ||
| lhs_tile, | ||
| rhs_tile, | ||
| area, | ||
| lb, | ||
| ub, | ||
| step, | ||
| ): | ||
| remained = area | ||
| for lane in range(lb, ub, step): | ||
| mask, remained = pto.make_mask(out_tile.element_type, remained) | ||
| result = compute(pto.vlds(lhs_tile, lane), pto.vlds(rhs_tile, lane), mask) | ||
| pto.vsts(result, out_tile, lane, mask) |
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 |
|---|---|---|
|
|
@@ -9,6 +9,37 @@ | |
| """TileLang DSL template for pto.tabs""" | ||
|
|
||
| import tilelang_dsl as pto | ||
| from merge_axis import emit_binary_merge_axis, emit_unary_merge_axis, full_axis_constraint | ||
|
|
||
|
|
||
| @pto.vkernel( | ||
| target="a5", | ||
| op="pto.tabs", | ||
| constraints=[full_axis_constraint], | ||
| priority=100, | ||
| advanced=True, | ||
| ) | ||
| def template_tabs_merge_axis(src: pto.Tile, dst: pto.Tile): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. binary op能否共用1个模板,看一下手册里有template的写法 |
||
| dtype = dst.element_type | ||
| valid_rows, valid_cols = dst.valid_shape | ||
| total_elems = valid_rows * valid_cols | ||
| lanes = pto.get_lanes(dtype) | ||
|
|
||
| with pto.strict_vecscope(dst, src, total_elems, 0, total_elems, lanes) as ( | ||
| out_tile, | ||
| in_tile, | ||
| area, | ||
| lb, | ||
| ub, | ||
| step, | ||
| ): | ||
| remained = area | ||
| for lane in range(lb, ub, step): | ||
| mask, remained = pto.make_mask(out_tile.element_type, remained) | ||
| vec = pto.vlds(in_tile, lane) | ||
| result = pto.vabs(vec, mask) | ||
| pto.vsts(result, out_tile, lane, mask) | ||
| return | ||
|
|
||
|
|
||
| @pto.vkernel( | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -13,6 +13,55 @@ | |
| import tilelang_dsl as pto | ||
|
|
||
|
|
||
| def _constraint_tadd_full_axis( | ||
| src0_shape=(), | ||
| src0_valid_shape=(), | ||
| src1_shape=(), | ||
| src1_valid_shape=(), | ||
| dst_shape=(), | ||
| dst_valid_shape=(), | ||
| ): | ||
| """Select the merged-axis fast path only for full-axis contiguous tiles.""" | ||
| return ( | ||
| src0_shape == src0_valid_shape | ||
| and src1_shape == src1_valid_shape | ||
| and dst_shape == dst_valid_shape | ||
| ) | ||
|
|
||
|
|
||
| @pto.vkernel( | ||
| target="a5", | ||
| op="pto.tadd", | ||
| constraints=[_constraint_tadd_full_axis], | ||
| priority=100, | ||
| advanced=True, | ||
| ) | ||
| def template_tadd_merge_axis(src0: pto.Tile, src1: pto.Tile, dst: pto.Tile): | ||
| """Merged-axis full-tile version aligned with TBinOps_1D_NoPostUpdate.""" | ||
| dtype = dst.element_type | ||
| valid_rows, valid_cols = dst.valid_shape | ||
| total_elems = valid_rows * valid_cols | ||
| lanes = pto.get_lanes(dtype) | ||
|
|
||
| with pto.strict_vecscope(dst, src0, src1, total_elems, 0, total_elems, lanes) as ( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PTOAS后端会自动推导vecscope,这里不需要显式写 |
||
| out_tile, | ||
| lhs_tile, | ||
| rhs_tile, | ||
| area, | ||
| lb, | ||
| ub, | ||
| step, | ||
| ): | ||
| remained = area | ||
| for lane in range(lb, ub, step): | ||
| mask, remained = pto.make_mask(out_tile.element_type, remained) | ||
| lhs = pto.vlds(lhs_tile, lane) | ||
| rhs = pto.vlds(rhs_tile, lane) | ||
| summed = pto.vadd(lhs, rhs, mask) | ||
| pto.vsts(summed, out_tile, lane, mask) | ||
| return | ||
|
|
||
|
|
||
| @pto.vkernel( | ||
| target="a5", | ||
| op="pto.tadd" | ||
|
|
||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
跟ptr无关的op不需要advanced模式