[RFC] Target and Generic Function System on C++
Use this issue to discuss design choices we can make for target and generic function system. Most of the current discussion are implemented in https://github.com/dmlc/tvm/pull/892 thanks to @alex-weaver
Since target system is quite useful, we want to take some deliberation on this and use this issue for discussing potential API.
- Generic function itself behaves like PackedFunc, except it dispatches on current target scope.
- We need to support a target scoping stack via TLS
We can have RAII style version of scoping. Like
tvm::TargetContext ctx(target);
Which set target at construction time, and call Exit at destructor
Instead of using setter getter, the target can be supported via scoping backed by a stack in TLS, which looks like Target::EnterTargetScope(target); Target::ExitTargetScope().
We can have RAII style version of scoping. Like
tvm::TargetContext ctx(target);
Which set target at construction time, and call Exit at destructor.
In general, #892 is mostly ready, except for the target scoping system. We can paste in example code in this issue and discuss what can be improved. These examples can eventually go into the tutorials
Should the python lib share the same underlying context? ie. should tvm.target.current_target Target.__enter__ and Target.__exit__ call into C++ to get, push and pop the context respectively? Or should the python retain a separate stack?
EDIT: got confused between Target and BuildConfig. Extra paragraph removed.
Ideally we would love them to be the same, so the interpolation happens.
I take another pass of the current c++ code. One limitation of current generic func system, which I don't like is that it relies on func_name as the key to query for both register and invoke. I hope we have a cleaner solution, which makes GenericFunc object as an interop object between python and c++. This can likely be done by making GenericFunc as a Node.
The only thing to keep in mind is that we should avoid registering it to the Node constructor registry since it does not support serialization. Otherwise, it works like a traditional object, we can add additional member functions like
@tvm.register
class GenericFunc(Node):
def __call__(*args):
if self._call_functor is None:
self._call_functor = _api_internal._GenericFuncCallFunctor(self)
self._call_functor(*args)
Ok, just to make sure I understand, this will just affect the api used inside the python target.generic_func ? So the api used to register functions on both the python and c++ side will remain the same, but internally target.generic_func will keep a reference to the GenericFunc node instead of keeping a reference to the func_name of the default function?
yes, in python side we can just get a generic func object that contains register and is callable. Using string as key is always fragile since there can be duplication in names
OK, but they will still have a string key to go into the dmlc::registry? This allows python to use schedules defined in C++, and provides a mechanism for nnvm to look up schedules EDIT: I mean the GenericFunc internal registry, since it doesn't use dmlc::Registry
yes, we can still have a string key based registry for generic func
#892