leandron commented on a change in pull request #7304: URL: https://github.com/apache/tvm/pull/7304#discussion_r572141976
########## File path: python/tvm/driver/tvmc/byoc.py ########## @@ -0,0 +1,191 @@ +# 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. +""" +Provides support to Bring Your Own Codegen (BYOC) on TVMC. +""" +import logging + +from abc import ABC +from abc import abstractmethod + +import tvm + +from tvm import relay + +from tvm.relay.op.contrib import get_pattern_table + +from .common import TVMCException + + +# pylint: disable=invalid-name +logger = logging.getLogger("TVMC") + +# Global dictionary to map existing custom codegens +# with the names used to use them +REGISTERED_CODEGEN = {} + + +def register_codegen(kind): + """ + Utility function to register a BYOC class for TVMC. + + Classes decorated with `tvm.driver.tvmc.target.register_codegen` will + be added to the codegens dictionary. + + Example + ------- + + @register_codegen(kind="samplebyoc") + class MyCustomTarget(TVMCCodegen): + ... + """ + + def codegen_decorator(cls): + cls.kind = kind + + assert kind not in REGISTERED_CODEGEN, "there is already a codegen '%s': %s" % ( + kind, + REGISTERED_CODEGEN[kind], + ) + assert issubclass(cls, TVMCCodegen), "%s is expected to be a subclass of TVMCCodegen" % cls + + REGISTERED_CODEGEN[kind] = cls + return cls + + return codegen_decorator + + +def get_codegen_kinds(): + """Return a list of all registered codegens. + + Returns + -------- + list of str + all registered codegens + """ + return REGISTERED_CODEGEN.keys() + + +def get_codegen_by_kind(kind): + """Return a custom codegen by kind. + + Returns + -------- + TVMCCodegen + The requested codegen or None in case it is + not valid + """ + try: + return REGISTERED_CODEGEN[kind] + except KeyError: + raise TVMCException("Target %s is not defined." % kind) + + +class TVMCCodegen(ABC): Review comment: So, at the moment we still would need to have an extra argument to know which config dictionary entry we need for the `PassContext`. I'm also playing with the idea of having this better documented w.r.t. enhancing the `--help` option per codegen. I suggest we keep at least this minimal layer encapsulated in a classes, but happy to discuss in case there is a major flaw in that. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
