Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
34 changes: 34 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,39 @@ 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"), b: T.Buffer(16, "float32")):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we reduce this to a minimal PrimFunc as well? That way, it makes it clear to the reader that the test is validating the behavior of the attribute, and that the if/else/floormod aren't required for the test.

@T.prim_func
def func(A: T.Buffer(1)):
    T.func_attr("estimated_flops": 32)
    for i in T.serial(0, 16):
        A[0] = A[0] + 1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good Eric. Thanks for the suggestion. Fixed in the next commit.

T.func_attr({"estimated_flops": 32})
for i in range(16):
if i % 2 == 0:
a[i] = b[i]
else:
if i % 3 == 0:
a[i] = b[i - 1] + b[i - 2]


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()