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

[RUNTIME] Add TypedPackedFunc #1626

Merged
merged 2 commits into from
Aug 21, 2018
Merged

[RUNTIME] Add TypedPackedFunc #1626

merged 2 commits into from
Aug 21, 2018

Conversation

tqchen
Copy link
Member

@tqchen tqchen commented Aug 21, 2018

TypedPackedFunc enables compile-time type checking. TypedPackedFunc works with the runtime system:

  • It can be passed as an argument of PackedFunc,
  • It can be assigned to TVMRetValue.
  • It can be directly converted to a type-erased PackedFunc.

This allows us to write more typed code in c++ while taking benefit of PackedFunc that can be used interchangeably with frontend code.

Example

void TestTypedPackedFunc() {
  using Int1Func = TypedPackedFunc<int(int)>;
  using Int2Func = TypedPackedFunc<int(int, int)>;
  using BindFunc = TypedPackedFunc<Int1Func(Int2Func, int value)>;
  BindFunc ftyped;
  ftyped = [](Int2Func f1, int value) -> Int1Func {
    auto binded = [f1, value](int x) {
      return f1(value, x);
    };
    Int1Func x(binded);
    return x;
  };
  auto add = [](int x, int y) { return x + y; };
  CHECK_EQ(ftyped(Int2Func(add), 1)(2), 3);
  PackedFunc f = ftyped(Int2Func(add), 1);
  CHECK_EQ(f(3).operator int(), 4);
  // call the type erased version.
  Int1Func f1 = ftyped.packed()(Int2Func(add), 1);
  CHECK_EQ(f1(3), 4);
}

@tqchen
Copy link
Member Author

tqchen commented Aug 21, 2018

@jroesch @yzhliu please review

Copy link
Member

@jroesch jroesch left a comment

Choose a reason for hiding this comment

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

Just some small nits about English, the code looks good.

*
* TypedPackedFunc enables compile time type checking.
* TypedPackedFunc works with the runtime system:
* - It can be passed as an argument of PackedFunc,
Copy link
Member

Choose a reason for hiding this comment

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

trailing comma should be a period?

* - It can be assigned to TVMRetValue.
* - It can be directly converted to a type-erased PackedFunc.
*
* Please use it when possible in the c++ code to enable more
Copy link
Member

Choose a reason for hiding this comment

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

Maybe something like "Developers should prefer TypedPackedFunc over PackedFunc in C++ code as it enables compile time checking."

@tqchen tqchen merged commit 16d3c1f into apache:master Aug 21, 2018
@yzhliu
Copy link
Member

yzhliu commented Aug 21, 2018

Are we going to use it in frontend language bindings?

@tqchen
Copy link
Member Author

tqchen commented Aug 21, 2018

@yzhliu This is mainly for c++ backend code, in the frontend binding, what user see is mainly PackedFunc.

  • TypedPackedFunc is only a wrapper around PackedFunc, so if you return TypedPackedFunc, you get a PackedFunc back
  • You can take a PackedFunc and convert it to TypedPackedfUNC

This being said, we can also try to support type signature for strongly typed language like java, if we have ways to dynamically build up the function(I do not know if that is possible).

@tqchen
Copy link
Member Author

tqchen commented Aug 21, 2018

To follow up on what this might be helpful, currently, nnvm uses std::function for most of the operator attributes, which makes it hard to interchange between frontend(python) and backend. We want to upgrade the operator attribute system so that next version we can directly use PackedFunc, but we still want to have the std::function signature so that the c++ side of pass gives you strong type and compiler will hit an error when you mistakenly call it with wrong signature

@yzhliu
Copy link
Member

yzhliu commented Aug 21, 2018

got it. thanks for explaining.

FrozenGene pushed a commit to FrozenGene/tvm that referenced this pull request Dec 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants