-
Notifications
You must be signed in to change notification settings - Fork 4k
[TIR] Introduce Pass InjectPTXLDG32 #13973
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
14 commits
Select commit
Hold shift + click to select a range
0850d2b
add ptx load global 32bit
andy-yang-1 38c64df
Update inject_ptx_ldg32.cc
andy-yang-1 690db54
test use clang-format
andy-yang-1 78141e3
remove printIndent
andy-yang-1 bb7a70a
change inject_ptx_ldg32 to ptx_ldg32
andy-yang-1 36f9ad9
[TIR] Introduce Pass InjectPTXLDG32
andy-yang-1 89d2a7d
Merge branch 'main' of https://github.com/andy-yang-1/tvm
andy-yang-1 3b04bf3
[TIR] Introduce Pass InjectPTXLDG32
andy-yang-1 2259063
Merge branch 'main' of https://github.com/andy-yang-1/tvm
andy-yang-1 a458707
[TIR] Introduce Pass InjectPTXLDG32
andy-yang-1 d86324a
Update test_inject_ptx_ldg32.py
andy-yang-1 baacf71
Merge branch 'main' of https://github.com/andy-yang-1/tvm
andy-yang-1 b72d775
Update builtin.h
andy-yang-1 f47b518
Merge branch 'apache:main' into main
andy-yang-1 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| #include <tvm/arith/analyzer.h> | ||
| #include <tvm/arith/iter_affine_map.h> | ||
| #include <tvm/runtime/registry.h> | ||
| #include <tvm/tir/analysis.h> | ||
| #include <tvm/tir/op.h> | ||
| #include <tvm/tir/stmt.h> | ||
| #include <tvm/tir/stmt_functor.h> | ||
| #include <tvm/tir/transform.h> | ||
|
|
||
| #include "../../arith/const_fold.h" | ||
| #include "../../arith/pattern_match.h" | ||
|
|
||
| namespace tvm { | ||
| namespace tir { | ||
|
|
||
| class PTXRewriter : public StmtMutator { | ||
| public: | ||
| Stmt VisitStmt_(const AllocateNode* allocate) final { | ||
| if (!has_buffer_1) { | ||
| has_buffer_1 = true; | ||
| // addr[0] -> global_addr / addr[1] -> local_addr | ||
| addr_buffer = decl_buffer({IntImm(DataType::Int(32), 2)}, DataType::Int(32), "addr", "local"); | ||
| predicate_buffer = | ||
| decl_buffer({IntImm(DataType::Int(32), 1)}, DataType::Bool(1), "predicate", "local"); | ||
| } | ||
| Stmt result = StmtMutator::VisitStmt_(allocate); | ||
| if (!has_buffer_2) { | ||
| has_buffer_2 = true; | ||
| result = | ||
| Allocate(addr_buffer->data, addr_buffer->dtype, addr_buffer->shape, Bool(true), result); | ||
| result = Allocate(predicate_buffer->data, predicate_buffer->dtype, predicate_buffer->shape, | ||
| Bool(true), result); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| Stmt VisitStmt_(const BufferStoreNode* store) final { | ||
| Stmt result = StmtMutator::VisitStmt_(store); | ||
| Buffer load_buffer = store->buffer; | ||
| PrimExpr load_value = store->value; | ||
| // const BufferLoadNode* gload = load_value.as<BufferLoadNode>(); // take | ||
| // the place of instance of | ||
| const CallNode* call = load_value.as<CallNode>(); | ||
| if (call != nullptr) { | ||
| const OpNode* op = call->op.as<OpNode>(); | ||
| if (op != nullptr && op->name == "tir.if_then_else") { | ||
| const PrimExpr& predicate = call->args[0]; | ||
| const PrimExpr& lhs = call->args[1]; | ||
| const PrimExpr& rhs = call->args[2]; | ||
| PrimExpr global_addr, local_addr; | ||
| const BufferLoadNode* load = lhs.as<BufferLoadNode>(); | ||
| PrimExpr imm_value = rhs; | ||
| if (load == nullptr) { | ||
| load = rhs.as<BufferLoadNode>(); | ||
| imm_value = lhs; | ||
| if (load == nullptr) { | ||
| return result; | ||
| } | ||
| } | ||
| global_addr = load->indices[0]; | ||
| const RampNode* ramp = global_addr.as<RampNode>(); | ||
| if (ramp != nullptr) { | ||
| return result; | ||
| } | ||
| local_addr = store->indices[0]; | ||
| BufferStore addr_store(addr_buffer, global_addr, {IntImm(DataType::Int(32), 0)}); | ||
| BufferStore local_addr_store(addr_buffer, local_addr, {IntImm(DataType::Int(32), 1)}); | ||
| BufferStore predicate_store(predicate_buffer, predicate, {IntImm(DataType::Int(32), 0)}); | ||
| PrimExpr new_lhs, new_rhs, new_predicate, new_indice; | ||
| new_lhs = | ||
| BufferLoad(load->buffer, {BufferLoad(addr_buffer, {IntImm(DataType::Int(32), 0)})}); | ||
| new_rhs = IntImm(DataType::Int(32), 0); | ||
| new_predicate = BufferLoad(predicate_buffer, {IntImm(DataType::Int(32), 0)}); | ||
| new_indice = BufferLoad(addr_buffer, {IntImm(DataType::Int(32), 1)}); | ||
| BufferStore value_store(store->buffer, imm_value, {new_indice}); | ||
| Evaluate ptx_load(Call(store->buffer->dtype, tvm::tir::builtin::ptx_ldg32(), | ||
| {store->buffer->data, new_predicate, new_lhs, new_indice})); | ||
| Array<Stmt> tmp_seq = {addr_store, local_addr_store, predicate_store, value_store, | ||
| ptx_load}; | ||
| SeqStmt seq_stmt = SeqStmt(tmp_seq); | ||
| return seq_stmt; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| bool has_buffer_1 = false, has_buffer_2 = false; | ||
| Buffer addr_buffer, predicate_buffer; | ||
| }; | ||
|
|
||
| namespace transform { | ||
|
|
||
| Pass InjectPTXLDG32(bool enable_inject_ptx_intrin) { | ||
| auto pass_func = [enable_inject_ptx_intrin](PrimFunc f, IRModule m, PassContext ctx) { | ||
| if (enable_inject_ptx_intrin) { | ||
| auto* n = f.CopyOnWrite(); | ||
| n->body = PTXRewriter()(n->body); | ||
| // inject ptx | ||
| } | ||
| return f; | ||
| }; | ||
| return CreatePrimFuncPass(pass_func, 0, "tir.InjectPTXLDG32", {}); | ||
| } | ||
|
|
||
| // The pass can now be invoked via the pass infrastructure, but we also add a | ||
| // Python binding for it | ||
| TVM_REGISTER_GLOBAL("tir.transform.InjectPTXLDG32").set_body_typed(InjectPTXLDG32); | ||
|
|
||
|
Member
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. nit: you may use clang-format to somehow organize the file slightly better |
||
| } // namespace transform | ||
| } // namespace tir | ||
| } // namespace tvm | ||
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,67 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| import tvm | ||
| from tvm.script import tir as T | ||
| import numpy as np | ||
| import tvm.testing | ||
|
|
||
|
|
||
| @T.prim_func | ||
| def vector_add(A: T.Buffer((16), "float32"), B: T.Buffer((32), "float32")) -> None: | ||
| T.func_attr({"global_symbol": "default_function", "tir.noalias": True}) | ||
| bx = T.env_thread("blockIdx.x") | ||
| tx = T.env_thread("threadIdx.x") | ||
| T.launch_thread(bx, 1) | ||
| T.launch_thread(tx, 32) | ||
| A_local = T.Buffer((32), "float32", scope="local") | ||
|
|
||
| with T.block(): | ||
| T.reads(A[0:16]) | ||
| T.writes(A_local[0:32]) | ||
| A_local[tx] = T.if_then_else(tx % 2 == 0, A[tx / 2], T.float32(0), dtype="float32") | ||
| B[tx] = A_local[tx] + 1.0 | ||
|
|
||
|
|
||
| @tvm.testing.requires_cuda | ||
| def test_inject_ptx_intrin(): | ||
| f = vector_add | ||
| arch = tvm.contrib.nvcc.get_target_compute_version() | ||
| major, _ = tvm.contrib.nvcc.parse_compute_version(arch) | ||
| if major < 8: | ||
| # Require at least SM80 | ||
| return | ||
| with tvm.transform.PassContext(config={"tir.ptx_ldg32": True}): | ||
| mod = tvm.build(f, target="cuda") | ||
| A_np = np.random.rand(16).astype("float32") | ||
| B_np = np.zeros((32)).astype("float32") | ||
| dev = tvm.cuda(0) | ||
| A_nd = tvm.nd.array(A_np, device=dev) | ||
| B_nd = tvm.nd.array(B_np, device=dev) | ||
| mod(A_nd, B_nd) | ||
|
|
||
| C_np = np.zeros((32)).astype("float32") | ||
|
|
||
| for i in range(32): | ||
| if i % 2 == 0: | ||
| C_np[i] = A_np[i // 2] | ||
| C_np[i] += 1.0 | ||
|
|
||
| tvm.testing.assert_allclose(B_nd.numpy(), C_np) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| test_inject_ptx_intrin() |
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.
perhaps it would be clearer to write this way:
https://github.com/apache/tvm/pull/13966/files#diff-28ce493acf6a737cd561f3996bd897c8c14edc056f9125503344453dcf390d49R668-R690