Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIR] Support Return in TIR #7084

Merged
merged 21 commits into from
Jan 16, 2021
Merged

[TIR] Support Return in TIR #7084

merged 21 commits into from
Jan 16, 2021

Conversation

ZihengJiang
Copy link
Contributor

This PR supports building TIR function directly and allows TIR returns value directly.
Current approach is by adding an intrinsic for return. Example:

def add():
    a = tir.Var("a", "float32") 
    b = tir.Var("b", "float32") 
    c = a + b
    c = tir.call_intrin("float32", "tir.myreturn", c) 
    c = tir.Evaluate(c)
    func = tir.PrimFunc([a, b], c)
    mod = tvm.IRModule({'add': func})
    func = tvm.build(mod['add'])
    out = func(1.0, 2.0)
    print(out)

In the future, with the tvm script, we should be able to write some like:

@tvm.script
def add(a, b):
    tir.myreturn(a + b)

There are two things need to be discussed:

  • Because return is a keyword in Python, currently I use myreturn, we should have another name for it.
  • Whether we should support multiple return in a single function.

@tqchen @junrushao1994 @areusch

include/tvm/tir/builtin.h Outdated Show resolved Hide resolved
python/tvm/driver/build_module.py Outdated Show resolved Hide resolved
} else if (dtype.is_void()) {
return {kTVMNullptr, val};
} else {
LOG(FATAL) << "data type " << dtype << " not supported yet";
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we may need to return at least DLTensor for AOT

src/tir/transforms/make_packed_api.cc Outdated Show resolved Hide resolved
src/tir/transforms/make_packed_api.cc Outdated Show resolved Hide resolved
tests/python/unittest/test_tir_build.py Outdated Show resolved Hide resolved
python/tvm/driver/build_module.py Outdated Show resolved Hide resolved
python/tvm/driver/build_module.py Outdated Show resolved Hide resolved
@ZihengJiang ZihengJiang changed the title [WIP][TIR] Support Return in TIR [TIR] Support Return in TIR Dec 14, 2020
src/tir/op/builtin.cc Outdated Show resolved Hide resolved
@tqchen
Copy link
Member

tqchen commented Dec 16, 2020

One additional comment, overall lgtm. @junrushao1994 @tkonolige @areusch please take another look and https://tvm.apache.org/docs/contribute/code_review.html#approve-and-request-changes-explicitly

@tqchen tqchen added status: need review status: need update need update based on feedbacks labels Dec 16, 2020
@tqchen tqchen self-assigned this Dec 16, 2020
src/tir/transforms/make_packed_api.cc Outdated Show resolved Hide resolved
src/tir/transforms/make_packed_api.cc Outdated Show resolved Hide resolved
} else if (dtype.is_void()) {
return {kTVMNullptr, val};
} else {
LOG(FATAL) << "data type " << dtype << " not supported yet";
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add which data datatypes are supported to this error message

@tqchen
Copy link
Member

tqchen commented Dec 17, 2020

Thanks @tkonolige @jroesch @ZihengJiang on discussing the issue of ICHECK and comments. While I in general agree that for cases that are user facing, having clear error message would be helpful.

I would be hesitant to enforce error message for every internal invariance check. This decision is more of an engineering tradeoff. For example are many ways to improve code robustness and readability, code comment is one of them. However, it does not necessarily mean we need to comment every line of code, even though it is possible to do so and doing so might improve readability slightly.

Developers may want to spend their attentions in other matters and we also need to balance these concerns, so as long as the code structure is clear, we could focus the code review on more important things -- e.g. documenting the interface clearly, making sure the overall logics are correct.

Similarly, while it is always possible to add error message to every internal invariance checks, I don't think we are required to do so for every care. We can find many similar examples(like the ones below) in other large code base like TensorFlow and PyTorch, doing so doesn't make pytorch or tensorflow not product ready.

Of course this does not mean that we should not add error messages to the checks that could cause user facing errors. We should actually shift our attention to address these checks quickly, while giving flexibility to the developers in other cases that have minimum gain.

In summary, context of the code matters, and I believe code review should focus on understanding the overall logics, trying to think about readability in a broader context(other than error message itself), while encouraging things to move on smoothly once things met a good quality.

In this case we have discussed and I believe the ICHECK on pointer null invariant checking is not necessary. While it is indeed helpful to have better error message in other two cases.

@tkonolige
Copy link
Contributor

I'm a little confused on how we use return in tir? How is the example you give different if we just remove the return?

a = tir.Var("a", "float32") 
b = tir.Var("b", "float32") 
c = a + b
# c = tir.call_intrin("float32", "tir.myreturn", c)
c = tir.Evaluate(c) /# doesn't this return a + b?

@ZihengJiang
Copy link
Contributor Author

@tkonolige
This will generate a function that only evaluate but not return:

def func(a, b):
    c = a + b
    # return c

Think and have a try by yourself would make you understand other's PR.

@tkonolige
Copy link
Contributor

I'm just trying to understand things better. There's not really any documentation on TIR.

I had thought that the last statement in every TIR expression was implicitly returned. Am I correct in saying that before this PR calling a TIR function would always return nullptr/None? And that now you can actually return a value?

@ZihengJiang
Copy link
Contributor Author

@tkonolige
Happy to address your comment and hope it will make you understand TIR. Yes, usually a zero value would be returned before this PR and now a value can be explicitly returned.

Copy link
Contributor

@tkonolige tkonolige left a comment

Choose a reason for hiding this comment

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

Thanks for the clarification.

Given that return is a useful feature, could you add tir.myreturn (or whatever name you think best) as a function to python/tvm/tir/stmt.py. That way we don't have to call the intrinsic manually. Could you also add documentation with that function. An example would be nice.

(Trying to use the submit review feature correctly here.)

@tqchen
Copy link
Member

tqchen commented Dec 17, 2020

Thanks @tkonolige , I agree, and perhaps https://github.com/apache/tvm/blob/main/python/tvm/tir/op.py is a better place given other intrinsics are in there

@tqchen
Copy link
Member

tqchen commented Jan 6, 2021

@ZihengJiang would be great if we can act on the thread and bring it in :)

Copy link
Member

@tqchen tqchen left a comment

Choose a reason for hiding this comment

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

Thanks @ZihengJiang please check the comment on control flow termination

src/tir/transforms/make_packed_api.cc Outdated Show resolved Hide resolved
src/target/llvm/codegen_llvm.cc Show resolved Hide resolved
src/target/llvm/codegen_llvm.cc Outdated Show resolved Hide resolved
@tqchen tqchen merged commit 052ad3d into apache:main Jan 16, 2021
@tqchen
Copy link
Member

tqchen commented Jan 16, 2021

@tqchen tqchen added status: accepted and removed status: need review status: need update need update based on feedbacks labels Jan 16, 2021
@ZihengJiang ZihengJiang deleted the tir branch January 16, 2021 17:57
masahi pushed a commit to masahi/tvm that referenced this pull request Jan 18, 2021
TusharKanekiDey pushed a commit to TusharKanekiDey/tvm that referenced this pull request Jan 20, 2021
trevor-m pushed a commit to neo-ai/tvm that referenced this pull request Jan 21, 2021
electriclilies pushed a commit to electriclilies/tvm that referenced this pull request Feb 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants