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.
* add BlockFrame * upd * add T::axis::Spatial/Reduce * include dom in for-frame * finish T.axis.remap
- Loading branch information
Showing
6 changed files
with
283 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* 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 "./block_frame.h" | ||
|
||
#include "./for_frame.h" | ||
|
||
namespace tvm { | ||
namespace script { | ||
namespace builder { | ||
namespace tir { | ||
|
||
BlockFrame::BlockFrame(String name) { | ||
ObjectPtr<BlockFrameNode> n = make_object<BlockFrameNode>(); | ||
n->name = name; | ||
n->iter_vars.clear(); | ||
n->reads = NullOpt; | ||
n->writes = NullOpt; | ||
n->init = NullOpt; | ||
n->alloc_buffers.clear(); | ||
n->match_buffers.clear(); | ||
n->annotations.clear(); | ||
n->iter_values.clear(); | ||
n->predicate = NullOpt; | ||
data_ = n; | ||
} | ||
|
||
namespace axis { | ||
|
||
// TODO(@junrushao1994): figure out the Block syntax without BlockRealize | ||
|
||
tvm::tir::IterVar PushBlockVar(tvm::tir::IterVar iter_var, PrimExpr binding) { | ||
if (const BlockFrameNode* opt_frame = Builder::Current()->frames.back().as<BlockFrameNode>()) { | ||
BlockFrame frame = GetRef<BlockFrame>(opt_frame); | ||
frame->iter_vars.push_back(iter_var); | ||
frame->iter_values.push_back(binding); | ||
} else { | ||
LOG(FATAL) << "TypeError: The last frame is not BlockFrame"; | ||
} | ||
return iter_var; | ||
} | ||
|
||
tvm::tir::IterVar Spatial(Range dom, PrimExpr binding, DataType dtype) { | ||
using namespace tvm::tir; | ||
ICHECK(dom.defined()) << "Spatial axis must have a domain"; | ||
int bits = std::max({dom->min.dtype().bits(), dom->extent.dtype().bits(), dtype.bits()}); | ||
return PushBlockVar(IterVar(/*dom=*/dom, // | ||
/*var=*/Var("_", dtype.with_bits(bits)), // | ||
/*iter_type=*/IterVarType::kDataPar, // | ||
/*thread_tag=*/""), | ||
binding); | ||
} | ||
|
||
tvm::tir::IterVar Reduce(Range dom, PrimExpr binding, DataType dtype) { | ||
using namespace tvm::tir; | ||
ICHECK(dom.defined()) << "Spatial axis must have a domain"; | ||
int bits = std::max({dom->min.dtype().bits(), dom->extent.dtype().bits(), dtype.bits()}); | ||
return PushBlockVar(IterVar(/*dom=*/dom, // | ||
/*var=*/Var("_", dtype.with_bits(bits)), // | ||
/*iter_type=*/IterVarType::kCommReduce, // | ||
/*thread_tag=*/""), | ||
binding); | ||
} | ||
|
||
Array<tvm::tir::IterVar> Remap(String kinds, Array<PrimExpr> bindings, DataType dtype) { | ||
using namespace tvm::tir; | ||
Array<tvm::tir::IterVar> results; | ||
ICHECK_EQ(kinds.size(), bindings.size()); | ||
int n = bindings.size(); | ||
results.reserve(n); | ||
for (int i = 0; i < n; ++i) { | ||
char c = kinds.c_str()[i]; | ||
PrimExpr e = bindings[i]; | ||
const VarNode* v = e.as<VarNode>(); | ||
ICHECK(v) << "TypeError: Only Var is supported in T.axis.remap"; | ||
Range dom{nullptr}; | ||
for (const auto& frame : Builder::Current()->frames) { | ||
if (const auto* for_frame = frame.as<ForFrameNode>()) { | ||
ICHECK_EQ(for_frame->doms.size(), for_frame->vars.size()); | ||
int n = for_frame->doms.size(); | ||
for (int i = 0; i < n; ++i) { | ||
if (for_frame->vars[i].get() == v) { | ||
dom = for_frame->doms[i]; | ||
break; | ||
} | ||
} | ||
if (dom.defined()) { | ||
break; | ||
} | ||
} | ||
} | ||
ICHECK(dom.defined()) << "TypeError: Variable is not in the loop: " << GetRef<Var>(v); | ||
DataType dtype = v->dtype; | ||
if (c == 'S') { | ||
results.push_back(PushBlockVar(IterVar(/*dom=*/dom, | ||
/*var=*/Var("_", dtype), | ||
/*iter_type=*/IterVarType::kDataPar, | ||
/*thread_tag=*/""), | ||
e)); | ||
} else if (c == 'R') { | ||
results.push_back(PushBlockVar(IterVar(/*dom=*/dom, | ||
/*var=*/Var("_", dtype), | ||
/*iter_type=*/IterVarType::kCommReduce, | ||
/*thread_tag=*/""), | ||
e)); | ||
} else { | ||
LOG(FATAL) << "Unknown axis kind: " << c; | ||
} | ||
} | ||
return results; | ||
} | ||
|
||
} // namespace axis | ||
|
||
TVM_REGISTER_NODE_TYPE(BlockFrameNode); | ||
|
||
} // namespace tir | ||
} // namespace builder | ||
} // namespace script | ||
} // 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* 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. | ||
*/ | ||
#ifndef TVM_SCRIPT_BUILDER_TIR_BLOCK_FRAME_H_ | ||
#define TVM_SCRIPT_BUILDER_TIR_BLOCK_FRAME_H_ | ||
|
||
#include "./tir.h" | ||
|
||
namespace tvm { | ||
namespace script { | ||
namespace builder { | ||
namespace tir { | ||
|
||
class BlockFrameNode : public TIRFrameNode { | ||
public: | ||
String name; | ||
Array<tvm::tir::IterVar> iter_vars; | ||
Optional<Array<tvm::tir::BufferRegion>> reads; | ||
Optional<Array<tvm::tir::BufferRegion>> writes; | ||
Optional<tvm::tir::Stmt> init; | ||
Array<tvm::tir::Buffer> alloc_buffers; | ||
Array<tvm::tir::MatchBufferRegion> match_buffers; | ||
Map<String, ObjectRef> annotations; | ||
|
||
Array<PrimExpr> iter_values; | ||
Optional<PrimExpr> predicate; | ||
|
||
void VisitAttrs(tvm::AttrVisitor* v) { | ||
v->Visit("name", &name); | ||
v->Visit("iter_vars", &iter_vars); | ||
v->Visit("reads", &reads); | ||
v->Visit("writes", &writes); | ||
v->Visit("init", &init); | ||
v->Visit("alloc_buffers", &alloc_buffers); | ||
v->Visit("match_buffers", &match_buffers); | ||
v->Visit("annotations", &annotations); | ||
v->Visit("iter_values", &iter_values); | ||
v->Visit("predicate", &predicate); | ||
} | ||
|
||
static constexpr const char* _type_key = "script.builder.tir.BlockFrame"; | ||
TVM_DECLARE_FINAL_OBJECT_INFO(BlockFrameNode, TIRFrameNode); | ||
}; | ||
|
||
class BlockFrame : public TIRFrame { | ||
public: | ||
explicit BlockFrame(String name); | ||
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(BlockFrame, TIRFrame, BlockFrameNode); | ||
}; | ||
|
||
namespace axis { | ||
tvm::tir::IterVar Spatial(Range dom, PrimExpr binding, DataType dtype); | ||
tvm::tir::IterVar Reduce(Range dom, PrimExpr binding, DataType dtype); | ||
Array<tvm::tir::IterVar> Remap(String kinds, Array<PrimExpr> bindings, DataType dtype); | ||
} // namespace axis | ||
} // namespace tir | ||
} // namespace builder | ||
} // namespace script | ||
} // namespace tvm | ||
|
||
#endif // TVM_SCRIPT_BUILDER_TIR_BLOCK_FRAME_H_ |
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.