Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/tir/analysis/estimate_flops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,15 @@ double EstimateTIRFlops(const Stmt& stmt) {
double EstimateTIRFlops(const IRModule& mod) {
FlopEstimator counter;
TResult result;
VisitPrimFuncs(mod, [&result, &counter](const PrimFuncNode* f) {
result += counter.VisitStmt(f->body); //
double cached_result = 0;
VisitPrimFuncs(mod, [&result, &counter, &cached_result](const PrimFuncNode* f) {
if (auto cached = f->attrs.GetAttr<Integer>("estimated_flops")) {
cached_result += cached.value()->value;
} else {
result += counter.VisitStmt(f->body); //
}
});
return PostprocessResults(result);
return PostprocessResults(result) + cached_result;
}

TVM_REGISTER_GLOBAL("tir.analysis.EstimateTIRFlops").set_body_typed([](ObjectRef obj) -> double {
Expand Down
30 changes: 30 additions & 0 deletions tests/python/unittest/test_tir_analysis_estimate_tir_flops.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,35 @@ def test_flops_with_if():
assert flops == 16


@T.prim_func
def flops_with_forloop_as_expression(A: T.Buffer(1)):
for i in T.serial(0, 16):
for k in T.serial(0, i):
A[0] = A[0] + 1


@T.prim_func
def flops_override(A: T.Buffer(16, "float32")):
T.func_attr({"estimated_flops": 32})
for i in range(16):
A[0] = A[0] + 1


def test_estimate_flops_forloop_as_experssion():
flops = estimate_tir_flops(
IRModule({"main": flops_with_forloop_as_expression.with_attr("estimated_flops", 32)})
)
assert flops == 32

# test whether the user estimated flop would over ride
flops = estimate_tir_flops(IRModule({"main": flops_override}))
assert flops == 32


def test_exception():
with pytest.raises(tvm.TVMError):
flops = estimate_tir_flops(IRModule({"main": flops_with_forloop_as_expression}))


if __name__ == "__main__":
tvm.testing.main()