Skip to content

Commit 51428e2

Browse files
committed
Added forgotten file + unit test fix
Change-Id: Ice305f4fefd13fe95e97574e6d63ffeb664621df
1 parent c5c1561 commit 51428e2

2 files changed

Lines changed: 109 additions & 1 deletion

File tree

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
/*!
21+
* \file extract_constants.cc
22+
* \brief Collects constants from PrimFunc
23+
TODO:
24+
For more information, see the RFC:
25+
TODO
26+
https://discuss.tvm.apache.org/t/rfc-introducing-a-rolling-buffer-scheduling-primitive/9836
27+
*/
28+
#include <tvm/arith/analyzer.h>
29+
#include <tvm/runtime/registry.h>
30+
#include <tvm/tir/stmt_functor.h>
31+
#include <tvm/tir/transform.h>
32+
33+
#include "ir_utils.h"
34+
35+
namespace tvm {
36+
namespace tir {
37+
38+
// Replaces constant data to index into mod's "Constants" attrs array.
39+
// Only processes tir::PrimFunc and ignores everything else
40+
using ConstArrayType = Array<runtime::NDArray>;
41+
class Applicator : public tir::StmtExprVisitor {
42+
protected:
43+
// returns index of the a in constant_array_, if not found - appends
44+
size_t deDup(const runtime::NDArray& a) {
45+
tvm::SEqualReducer eql;
46+
auto it = std::find_if(
47+
constant_array_.begin(), constant_array_.end(), [&eql, a](const runtime::NDArray& v) {
48+
return NDArrayContainerTrait::SEqualReduce(a.as<runtime::NDArray::Container>(),
49+
v.as<runtime::NDArray::Container>(), eql);
50+
});
51+
if (it != constant_array_.end()) {
52+
return it - constant_array_.begin();
53+
}
54+
constant_array_.push_back(std::move(a));
55+
return constant_array_.size() - 1;
56+
}
57+
58+
public:
59+
ConstArrayType Apply(tir::Stmt body, const ConstArrayType& constant_array) {
60+
constant_array_ = constant_array;
61+
this->VisitStmt(body);
62+
return constant_array_;
63+
}
64+
65+
void VisitStmt_(const tir::AllocateConstNode* acn) override {
66+
tir::AllocateConstNode* node = const_cast<tir::AllocateConstNode*>(acn);
67+
// Check whether the data already defined within the module's attrs
68+
// and replace it with array index;
69+
ICHECK(node->data) << "data field should be defined";
70+
if (node->data) {
71+
node->irmod_storage_idx = Optional<Integer>(Integer(deDup(node->data.value())));
72+
}
73+
tir::StmtExprVisitor::VisitStmt_(acn);
74+
}
75+
76+
private:
77+
ConstArrayType constant_array_;
78+
};
79+
80+
namespace transform {
81+
82+
Pass ExtractPrimFuncConstants() {
83+
auto pass_func = [=](PrimFunc f, IRModule m, PassContext ctx) {
84+
auto* func = f.CopyOnWrite();
85+
if (!m->attrs.defined()) {
86+
m->attrs = DictAttrs(Map<String, ObjectRef>());
87+
}
88+
auto* attrs = m->attrs.CopyOnWrite();
89+
ConstArrayType constant_array_ =
90+
(attrs->dict.count(tvm::attr::kConstantsArray))
91+
? Downcast<ConstArrayType>(attrs->dict[tvm::attr::kConstantsArray])
92+
: ConstArrayType();
93+
94+
const ConstArrayType constant_list = Applicator().Apply(func->body, constant_array_);
95+
if (constant_list.size()) {
96+
attrs->dict.Set(tvm::attr::kConstantsArray, constant_list);
97+
}
98+
return f;
99+
};
100+
return CreatePrimFuncPass(pass_func, 0, "tir.ExtractPrimFuncConstants", {});
101+
}
102+
103+
TVM_REGISTER_GLOBAL("tir.transform.ExtractPrimFuncConstants")
104+
.set_body_typed(ExtractPrimFuncConstants);
105+
106+
} // namespace transform
107+
108+
} // namespace tir
109+
} // namespace tvm

tests/python/unittest/test_tvmscript_roundtrip.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2968,7 +2968,6 @@ def B(a: T.handle, c: T.handle) -> None:
29682968
def test_module_const():
29692969
func = Module4
29702970
rt_func = tvm.script.from_source(func.script(show_meta=True))
2971-
assert len(func.attrs["Constants"]) == 1
29722971
tvm.ir.assert_structural_equal(func, rt_func)
29732972

29742973

0 commit comments

Comments
 (0)