diff --git a/include/tvm/script/printer/ir_docsifier_functor.h b/include/tvm/script/printer/ir_docsifier_functor.h index 54810fd55a43..e63c00f68a95 100644 --- a/include/tvm/script/printer/ir_docsifier_functor.h +++ b/include/tvm/script/printer/ir_docsifier_functor.h @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -69,6 +70,10 @@ class IRDocsifierFunctor { if ((pf = LookupDispatchTable("", type_index)) != nullptr) { return (*pf)(obj, args...); } + if ((pf = LookupFallback()) != nullptr) { + return (*pf)(obj, args...); + } + LOG(WARNING) << "ObjectFunctor calls un-registered function on type: " << runtime::Object::TypeIndex2Key(type_index) << " (token: " << token << ")" << ". ObjectType: " << obj->GetTypeKey() << ". Object: " << obj; @@ -100,6 +105,14 @@ class IRDocsifierFunctor { return *this; } + TSelf& set_fallback(runtime::PackedFunc f) { + ICHECK(!dispatch_fallback_.has_value()) << "Fallback is already defined"; + dispatch_fallback_ = f; + return *this; + } + + void remove_fallback() { dispatch_fallback_ = std::nullopt; } + /*! * \brief Set the dispatch function * \param token The dispatch token. @@ -112,6 +125,13 @@ class IRDocsifierFunctor { runtime::TypedPackedFunc(f)); } + template ::value>> + TSelf& set_fallback(TCallable f) { + runtime::PackedFunc func = runtime::TypedPackedFunc(f); + return set_fallback(func); + } + /*! * \brief Remove dispatch function * \param token The dispatch token. @@ -151,6 +171,18 @@ class IRDocsifierFunctor { return nullptr; } } + + /*! + * \brief Look up the fallback to be used if no handler is registered + */ + const runtime::PackedFunc* LookupFallback() const { + if (dispatch_fallback_.has_value()) { + return &*dispatch_fallback_; + } else { + return nullptr; + } + } + /* * This type alias and the following free functions are created to reduce the binary bloat * from template and also hide implementation details from this header @@ -158,6 +190,7 @@ class IRDocsifierFunctor { using DispatchTable = std::unordered_map>; /*! \brief The dispatch table. */ DispatchTable dispatch_table_; + std::optional dispatch_fallback_; }; } // namespace printer diff --git a/src/script/printer/ir/relay.cc b/src/script/printer/ir/relay.cc deleted file mode 100644 index 574c07e32aa0..000000000000 --- a/src/script/printer/ir/relay.cc +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -#include -#include - -#include "../../../relay/backend/utils.h" -#include "./utils.h" - -namespace tvm { -namespace script { -namespace printer { - -TVM_STATIC_IR_FUNCTOR(IRDocsifier, vtable) - .set_dispatch("", [](relay::Executor ty, ObjectPath p, IRDocsifier d) -> Doc { - return d->AddMetadata(ty); - }); - -TVM_STATIC_IR_FUNCTOR(IRDocsifier, vtable) - .set_dispatch("", [](relay::Runtime ty, ObjectPath p, IRDocsifier d) -> Doc { - return d->AddMetadata(ty); - }); - -TVM_STATIC_IR_FUNCTOR(IRDocsifier, vtable) - .set_dispatch("", - [](relay::backend::FunctionInfo ty, ObjectPath p, - IRDocsifier d) -> Doc { - return d->AddMetadata(ty); - }); - -} // namespace printer -} // namespace script -} // namespace tvm diff --git a/src/script/printer/ir_docsifier.cc b/src/script/printer/ir_docsifier.cc index fd5003073afb..62084d17be03 100644 --- a/src/script/printer/ir_docsifier.cc +++ b/src/script/printer/ir_docsifier.cc @@ -160,6 +160,11 @@ IRDocsifier::FType& IRDocsifier::vtable() { TVM_REGISTER_NODE_TYPE(FrameNode); TVM_REGISTER_NODE_TYPE(IRDocsifierNode); +TVM_STATIC_IR_FUNCTOR(IRDocsifier, vtable) + .set_fallback([](ObjectRef obj, ObjectPath p, IRDocsifier d) -> Doc { + return d->AddMetadata(obj); + }); + } // namespace printer } // namespace script } // namespace tvm