You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by "cgerum (via GitHub)" <gi...@apache.org> on 2023/03/06 17:31:47 UTC

[GitHub] [tvm] cgerum commented on a diff in pull request #14165: [TVMC][UMA] Support using UMA with TVMC

cgerum commented on code in PR #14165:
URL: https://github.com/apache/tvm/pull/14165#discussion_r1122917478


##########
python/tvm/driver/tvmc/extensions.py:
##########
@@ -0,0 +1,128 @@
+# 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.
+"""
+Allows to extend TVMC with external code.
+"""
+import sys
+import importlib
+import inspect
+import pkgutil
+import warnings
+import copy
+from abc import abstractmethod
+
+
+_EXTENSIONS = []
+
+
+class TVMExtension(object):

Review Comment:
   Just a minor nit, but is it TVMExtension or TVMCExtension



##########
python/tvm/driver/tvmc/extensions.py:
##########
@@ -0,0 +1,128 @@
+# 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.
+"""
+Allows to extend TVMC with external code.
+"""
+import sys
+import importlib
+import inspect
+import pkgutil
+import warnings
+import copy
+from abc import abstractmethod
+
+
+_EXTENSIONS = []
+
+
+class TVMExtension(object):
+    @abstractmethod
+    def uma_backends(self):
+        return []
+
+
+def get_extensions():
+    """Returns all loaded extensions."""
+
+    for ext in _EXTENSIONS:
+        yield ext
+
+
+def load_extensions(paths):
+    """
+    Loads extensions from the given locations.
+
+    Extensions must implement the `TVMExtension` interface and be stored in a directory called
+    `tvm_extension`.
+    """
+
+    path_backup = copy.copy(sys.path)
+    sys.path.extend(paths)
+
+    top_modules = []
+    try:
+        mod = importlib.import_module("tvm_extension")
+        top_modules.append(mod)
+    except ImportError:
+        pass
+
+    sys.path.clear()
+    sys.path.extend(path_backup)
+
+    extension_classes = _scan_all(top_modules)
+    for ext_cls in extension_classes:
+        _EXTENSIONS.append(ext_cls())
+
+
+def _scan_all(top_level):
+    scanned_extensions = []
+    for mdl in top_level:
+        for importer, modname, _ in pkgutil.walk_packages(
+            path=mdl.__path__, prefix=mdl.__name__ + ".", onerror=lambda x: None
+        ):
+            try:
+                module_name = modname.rsplit(".", 1)[-1]
+                # If module's name starts with "_", do not load the module.
+                # But if the module's name starts with a "__", then load the
+                # module.
+                if module_name.startswith("_") and not module_name.startswith("__"):
+                    continue

Review Comment:
   Not scanning in '_' allows to split interface and implementation:
   
   `my_backend.py`:
   
   ```
   class MyBackendExtension(TVMExtension):
       def __init__(self):
           from ._impl import MyBacken
           self.backend = MyBackend()
   
       def uma_backends(self):
           return [self.backend]
   ```
   
   
   `_my_backend_impl.py`:
   
   ```
   class MyBackend(UMABackend):
      ....
   ```
   
   The exception for "__" is there to allow declaring extensions in `__init__.py` and `__main__.py` but I am not sure wether this is actually worth it. Or we should just scan all modules for now. 



##########
python/tvm/relay/backend/contrib/uma/api/lower.py:
##########
@@ -71,9 +71,7 @@ def _get_tensors(te_cached_func):
         )
 
         compiler_attr = relay_prim_func.attrs["Compiler"]
-        target = tvm.target.Target.current()
-        if target.kind.name != compiler_attr:
-            target = tvm.target.Target(compiler_attr)
+        target = tvm.target.Target(compiler_attr)

Review Comment:
   I think `tvm.target.Target.current()` should generally work. The main reason for using ``target.Target.current()` is to allow the declaration of target specific options, e.G.: 
   
   ```
   target = tvm.target.Target("vanilla_backend --use-tuner=true --target-local-ram=10240")
   ```
   
   I would have assumed that `target.current` is always set in the compilation flow, but if it actually can be None, we can probabbly use: 
   
   ```
   if (target is None) or (target.kind.name != compiler_attr):
   ```
   



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org