You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2022/07/14 14:18:33 UTC

[GitHub] [tvm] yelite commented on a diff in pull request #11911: TVM Vertical Integration with PyTorch

yelite commented on code in PR #11911:
URL: https://github.com/apache/tvm/pull/11911#discussion_r921207358


##########
python/tvm/contrib/torch/as_torch.py:
##########
@@ -0,0 +1,156 @@
+# pylint: disable=inconsistent-return-statements
+#!/usr/bin/env python
+
+# 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.
+# pylint: disable=missing-module-docstring
+# pylint: disable=missing-class-docstring
+# pylint: disable=missing-function-docstring
+"""
+as_torch: a decorator, which is used to wrap the TVMscript code to `torch.nn.module`.
+"""
+import tempfile
+from typing import Callable, List, Union
+
+import torch
+import torch.utils.dlpack
+
+import tvm
+from tvm.meta_schedule import default_config
+from tvm.meta_schedule.database.database import TuningRecord
+from tvm.meta_schedule.extracted_task import ExtractedTask
+from tvm.meta_schedule.tune import TuneConfig, tune_extracted_tasks
+from tvm.target.target import Target
+from tvm.tir.schedule.schedule import Schedule
+
+
+# python wrapper for OperatorModule
+class OperatorModuleWrapper(torch.nn.Module):
+    def __init__(
+        self,
+        module: Union[
+            tvm.ir.module.IRModule,
+            tvm.tir.function.PrimFunc,
+        ],
+    ):
+        super().__init__()
+        self.rt_module = None  # runtime module
+        self.ir_module = module  # IR modules
+
+    def tune_tir_auto(self, mod: Union[tvm.ir.module.IRModule, tvm.tir.function.PrimFunc]):
+        with tempfile.TemporaryDirectory() as work_dir:
+            sch: Schedule = self.tune_tir_inner(
+                mod=mod,
+                target=Target("llvm --num-cores=16"),
+                work_dir=work_dir,
+            )
+            return sch.mod
+
+    def tune_tir_inner(
+        self,
+        mod: Union[tvm.ir.module.IRModule, tvm.tir.function.PrimFunc],
+        target: Union[str, Target],
+        work_dir: str,
+    ):
+        """Tune a TIR IRModule with a given target.
+
+        Parameters
+        ----------
+        mod : Union[IRModule, PrimFunc]
+            The module to tune.
+        target : Union[str, Target]
+            The target to tune for.
+        work_dir : Optional[str]
+            The working directory to save intermediate results.
+
+        Returns
+        -------
+        sch : Optional[Schedule]
+            The tuned schedule.
+        """
+        mod = default_config.mod(mod)
+        target = default_config.target(target)
+
+        extracted_task = ExtractedTask(
+            task_name="main",
+            mod=mod,
+            dispatched=[mod],
+            target=target,
+            weight=1,
+        )
+        config = TuneConfig(
+            # Default setting
+            strategy="replay_trace",
+            num_trials_per_iter=32,
+            max_trials_per_task=32,
+            max_trials_global=32,
+        )
+        database = tune_extracted_tasks(
+            extracted_tasks=[extracted_task], config=config, work_dir=work_dir
+        )
+        bests: List[TuningRecord] = database.get_top_k(database.commit_workload(mod), top_k=1)
+        if not bests:
+            return None
+        assert len(bests) == 1
+        sch = Schedule(mod)
+        bests[0].trace.apply_to_schedule(sch, remove_postproc=False)
+
+        return sch
+
+    def build(self, target=None):
+        tuned_module = self.tune_tir_auto(self.ir_module)

Review Comment:
   Can you decouple the tuning from building runtime module? It's okay to lazily build the module as `forward` is called but I don't feel it's reasonable to do the same thing with tuning due to the high turnaround time (more than 1 minute). 



##########
python/tvm/contrib/torch/as_torch.py:
##########
@@ -0,0 +1,156 @@
+# pylint: disable=inconsistent-return-statements
+#!/usr/bin/env python
+
+# 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.
+# pylint: disable=missing-module-docstring
+# pylint: disable=missing-class-docstring
+# pylint: disable=missing-function-docstring
+"""
+as_torch: a decorator, which is used to wrap the TVMscript code to `torch.nn.module`.
+"""
+import tempfile
+from typing import Callable, List, Union
+
+import torch
+import torch.utils.dlpack
+
+import tvm
+from tvm.meta_schedule import default_config
+from tvm.meta_schedule.database.database import TuningRecord
+from tvm.meta_schedule.extracted_task import ExtractedTask
+from tvm.meta_schedule.tune import TuneConfig, tune_extracted_tasks
+from tvm.target.target import Target
+from tvm.tir.schedule.schedule import Schedule
+
+
+# python wrapper for OperatorModule
+class OperatorModuleWrapper(torch.nn.Module):
+    def __init__(
+        self,
+        module: Union[
+            tvm.ir.module.IRModule,
+            tvm.tir.function.PrimFunc,
+        ],
+    ):
+        super().__init__()
+        self.rt_module = None  # runtime module
+        self.ir_module = module  # IR modules
+
+    def tune_tir_auto(self, mod: Union[tvm.ir.module.IRModule, tvm.tir.function.PrimFunc]):
+        with tempfile.TemporaryDirectory() as work_dir:
+            sch: Schedule = self.tune_tir_inner(
+                mod=mod,
+                target=Target("llvm --num-cores=16"),

Review Comment:
   Should this be passed through function parameter?



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