forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Metaschedule] Auto tensorization for CPU / GPU dot product (apache#1…
…1088) * [Metaschedule] Auto-tensorization for CPU / GPU dot product Co-authored-by: Siyuan Feng <Hzfengsy@sjtu.edu.cn> Co-authored-by: Bohan Hou <32121147+spectrometerHBH@users.noreply.github.com> Co-authored-by: Hongyi Jin <3231950289@qq.com> Co-authored-by: Ruihang Lai <lairuihangdongdong@qq.com> Co-authored-by: Wuwei Lin <wuwei@apache.org> * doc update * add vnni conv2d test * add dp4a test * adding tests for rewrite_tensorize * add rewrite_tensorize test * add missing pydoc * black * more doc * adding auto tensorize integration test * add dp4a test * fix target name * fix dtype in test * skip bert test * replace hard-coded llvm intrinsic id in test with look up * remove unnecessary include, add doc for the rest of params * update postproc.h * update doc * fix shape in te matmul workload * fix newline in cppdoc Co-authored-by: Siyuan Feng <Hzfengsy@sjtu.edu.cn> Co-authored-by: Bohan Hou <32121147+spectrometerHBH@users.noreply.github.com> Co-authored-by: Hongyi Jin <3231950289@qq.com> Co-authored-by: Ruihang Lai <lairuihangdongdong@qq.com> Co-authored-by: Wuwei Lin <wuwei@apache.org>
- Loading branch information
1 parent
fd7faf4
commit 96d2edd
Showing
15 changed files
with
1,457 additions
and
27 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,38 @@ | ||
# 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. | ||
"""A postprocessor that tensorize related components.""" | ||
|
||
from tvm._ffi.registry import register_object | ||
from .. import _ffi_api | ||
from .postproc import Postproc | ||
|
||
|
||
@register_object("meta_schedule.RewriteTensorize") | ||
class RewriteTensorize(Postproc): | ||
"""A postprocessor that applies tensorization to annotated blocks. | ||
Parameters | ||
---------- | ||
vectorize_init_loop : bool | ||
Whether or not vectorize the initialization loop produced by DecomposeReduction | ||
""" | ||
|
||
def __init__(self, vectorize_init_loop=False) -> None: | ||
self.__init_handle_by_constructor__( | ||
_ffi_api.PostprocRewriteTensorize, # type: ignore # pylint: disable=no-member | ||
vectorize_init_loop, | ||
) |
This file contains 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 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 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 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,105 @@ | ||
/* | ||
* 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/meta_schedule/postproc.h> | ||
|
||
#include <algorithm> | ||
|
||
#include "../utils.h" | ||
|
||
namespace tvm { | ||
namespace meta_schedule { | ||
|
||
using tir::BlockRV; | ||
using tir::LoopRV; | ||
|
||
void ApplyTensorization(const tir::Schedule& sch, const String& func_name, | ||
const tir::PrimFuncNode* func, bool vectorize_init_loop) { | ||
std::vector<std::pair<std::string, std::function<void(tir::BlockRV)>>> jobs; | ||
|
||
tir::PostOrderVisit(func->body, [=, &jobs](const ObjectRef& obj) { | ||
if (const auto* block = obj.as<tir::BlockNode>()) { | ||
tir::StmtSRef block_sref = sch->GetSRef(block); | ||
if (Optional<String> intrin_name = | ||
tir::GetAnn<String>(block_sref, tir::attr::meta_schedule_auto_tensorize)) { | ||
std::string block_name = block_sref->StmtAs<tir::BlockNode>()->name_hint; | ||
if (block_name.find("init") == std::string::npos) { | ||
jobs.emplace_back(block_name, [sch, intrin_name](tir::BlockRV block) { | ||
try { | ||
sch->Tensorize(block, intrin_name.value()); | ||
} catch (const std::exception& e) { | ||
LOG(WARNING) << "Tensorize failed with error " << e.what(); | ||
} | ||
}); | ||
} else if (vectorize_init_loop) { | ||
jobs.emplace_back(block_name, [sch](tir::BlockRV block) { | ||
Array<BlockRV> child_blocks = sch->GetChildBlocks(block); | ||
ICHECK(child_blocks.size() == 1); | ||
Array<LoopRV> init_loops = sch->GetLoops(child_blocks[0]); | ||
ICHECK(init_loops.size() == 1); | ||
sch->Vectorize(init_loops[0]); | ||
}); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
for (auto kv : jobs) { | ||
tir::BlockRV block = sch->GetBlock(kv.first, func_name); | ||
sch->Unannotate(block, tir::attr::meta_schedule_auto_tensorize); | ||
kv.second(block); | ||
} | ||
} | ||
|
||
class RewriteTensorizeNode : public PostprocNode { | ||
public: | ||
void InitializeWithTuneContext(const TuneContext& context) final {} | ||
|
||
bool Apply(const tir::Schedule& sch) final; | ||
|
||
void VisitAttrs(tvm::AttrVisitor* v) {} | ||
|
||
bool vectorize_init_loop = false; | ||
|
||
static constexpr const char* _type_key = "meta_schedule.RewriteTensorize"; | ||
TVM_DECLARE_FINAL_OBJECT_INFO(RewriteTensorizeNode, PostprocNode); | ||
}; | ||
|
||
bool RewriteTensorizeNode::Apply(const tir::Schedule& sch) { | ||
for (const auto& kv : sch->mod()->functions) { | ||
GlobalVar g_var = kv.first; | ||
BaseFunc base_func = kv.second; | ||
if (const tir::PrimFuncNode* prim_func = base_func.as<tir::PrimFuncNode>()) { | ||
ApplyTensorization(sch, g_var->name_hint, prim_func, vectorize_init_loop); | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
Postproc Postproc::RewriteTensorize(bool vectorize_init_loop) { | ||
ObjectPtr<RewriteTensorizeNode> n = make_object<RewriteTensorizeNode>(); | ||
n->vectorize_init_loop = vectorize_init_loop; | ||
return Postproc(n); | ||
} | ||
|
||
TVM_REGISTER_NODE_TYPE(RewriteTensorizeNode); | ||
TVM_REGISTER_GLOBAL("meta_schedule.PostprocRewriteTensorize") | ||
.set_body_typed(Postproc::RewriteTensorize); | ||
|
||
} // namespace meta_schedule | ||
} // namespace tvm |
This file contains 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 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.