[Solved] Schedule not registered for 'mytarget'

I added a new ‘mytarget’ to target list and add dense strategy registration in python/tvm/op/strategy/mytarget.py as below (__init__.py has been updated appropriately to include this newly added target file):

@dense_strategy.register("mytarget")
def dense_strategy_mytarget(attrs, inputs, out_type, target):
    strategy = _op.OpStrategy()
    strategy.add_implementation(wrap_compute_dense(topi.nn.dense),
                                wrap_topi_schedule(topi.mytarget.schedule_dense),
                                name="dense.mytarget", 
                                plevel=20)
    return strategy

However, when tvm tries to select_implementations() in python/tvm/relay/backend/compile_engine.py. It always goes to the generic version of dense_strategy defined in python/tvm/op/strategy/generic.py:

@override_native_generic_func("dense_strategy")
def dense_strategy(attrs, inputs, out_type, target):
    """dense generic strategy"""
    logger.warning("dense is not optimized for this platform.")
    strategy = _op.OpStrategy()
    strategy.add_implementation(wrap_compute_dense(topi.nn.dense),
                                wrap_topi_schedule(topi.generic.schedule_dense),
                                name="dense.generic")
    return strategy

Can anyone give me some hint why my registration doesnt work? Thanks.

It seems right. Maybe your target is error. Use this to see your current target.

print(tvm.target.Target.current(allow_none=False)

Thank you, @hht . It seems my target isn’t constructed appropriately. But why it doesn’t complain anything in the run before?

  File "relay_linearnet.py", line 32, in <module>
    print(tvm.target.Target.current(allow_none=False))

  File "/work/git_repo/tvm/python/tvm/target/target.py", line 103, in current
    return _ffi_api.GetCurrentTarget(allow_none)

  File "tvm/_ffi/_cython/./packed_func.pxi", line 308, in tvm._ffi._cy3.core.PackedFuncBase.__call__

  File "tvm/_ffi/_cython/./packed_func.pxi", line 243, in tvm._ffi._cy3.core.FuncCall

  File "tvm/_ffi/_cython/./packed_func.pxi", line 232, in tvm._ffi._cy3.core.FuncCall3

  File "tvm/_ffi/_cython/./base.pxi", line 159, in tvm._ffi._cy3.core.CALL

tvm._ffi.base.TVMError: Traceback (most recent call last):
  [bt] (6) /work/git_repo/tvm/build/libtvm.so(TVMFuncCall+0x95) [0x7f39251155b6]
  [bt] (5) /work/git_repo/tvm/build/libtvm.so(tvm::runtime::PackedFunc::CallPacked(tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*) const+0x30) [0x7f39246d655e]
  [bt] (4) /work/git_repo/tvm/build/libtvm.so(std::function<void (tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*)>::operator()(tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*) const+0x5a) [0x7f39246d6baa]
  [bt] (3) /work/git_repo/tvm/build/libtvm.so(+0x31a7fd4) [0x7f3924ac3fd4]
  [bt] (2) /work/git_repo/tvm/build/libtvm.so(+0x31a5f5d) [0x7f3924ac1f5d]
  [bt] (1) /work/git_repo/tvm/build/libtvm.so(tvm::Target::Current(bool)+0xec) [0x7f3924ac1eb2]
  [bt] (0) /work/git_repo/tvm/build/libtvm.so(dmlc::LogMessageFatal::~LogMessageFatal()+0x4a) [0x7f3924575370]
  File "/work/git_repo/tvm/src/target/target.cc", line 269
TVMError: Check failed: allow_not_defined: Target context required. Please set it by constructing a TargetContext

This is where the problem lies. You need to give the target context by relay.build with the target argument. But there is no requirement to do this in TVM for flexibility. In some tutorials, compute graph is not build by using op strategy but directly using the wrap function(cfunc&sfunc).

For example, one can build an nms graph by cfunc and sfunc without target context. And then give the target context in building stage.

out = topi.vision.nms.non_max_suppression(data, valid_count, iou_threshold=iou_threshold,
                                force_suppress=force_suppress, top_k=top_k)
s = topi.generic.schedule_nms(out)
print(tvm.lower(s,[data,valid_count,out]))
f = tvm.build(s, [data, valid_count, out],  "llvm")

I finally figured it out. It was because I didn’t specify “keys” when I create the new ‘mytarget’. Once I add ‘mytarget’ as the key, the dense_strategy registration works like a charm…

It is really appreciated there’ll be a tutorial/docs on how to add a new target, :slight_smile: