|
| 1 | +# License .to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +# pylint: disable=no-else-return, unidiomatic-typecheck, undefined-variable, invalid-name |
| 18 | +""" |
| 19 | +The debug Relay Virtual Machine. |
| 20 | +
|
| 21 | +Provides extra APIs for debugging vm execution. |
| 22 | +""" |
| 23 | +import tvm |
| 24 | +from . import vm, _vm |
| 25 | + |
| 26 | +def _update_target(target): |
| 27 | + target = target if target else tvm.target.current_target() |
| 28 | + if target is None: |
| 29 | + raise ValueError("Target is not set in env or passed as argument.") |
| 30 | + |
| 31 | + tgts = {} |
| 32 | + if isinstance(target, (str, tvm.target.Target)): |
| 33 | + dev_type = tvm.expr.IntImm("int32", tvm.nd.context(str(target)).device_type) |
| 34 | + tgts[dev_type] = tvm.target.create(target) |
| 35 | + elif isinstance(target, dict): |
| 36 | + for dev, tgt in target.items(): |
| 37 | + dev_type = tvm.expr.IntImm("int32", tvm.nd.context(dev).device_type) |
| 38 | + tgts[dev_type] = tvm.target.create(tgt) |
| 39 | + else: |
| 40 | + raise TypeError("target is expected to be str, tvm.target.Target, " + |
| 41 | + "or dict of str to str/tvm.target.Target, but received " + |
| 42 | + "{}".format(type(target))) |
| 43 | + return tgts |
| 44 | + |
| 45 | +class VMCompilerDebug(object): |
| 46 | + """Build Relay module to run on VM runtime.""" |
| 47 | + def __init__(self): |
| 48 | + self.mod = _vm._VMCompilerDebug() |
| 49 | + self._compile = self.mod["compile"] |
| 50 | + self._get_vm = self.mod["get_vm"] |
| 51 | + |
| 52 | + def compile(self, mod, target=None, target_host=None): |
| 53 | + """ |
| 54 | + Parameters |
| 55 | + ---------- |
| 56 | + mod : relay.Module |
| 57 | + The Relay module to build. |
| 58 | +
|
| 59 | + target : str, :any:`tvm.target.Target`, or dict of str(i.e. |
| 60 | + device/context name) to str/tvm.target.Target, optional |
| 61 | + For heterogeneous compilation, it is a dictionary indicating context |
| 62 | + to target mapping. For homogeneous compilation, it is a build target. |
| 63 | +
|
| 64 | + target_host : str or :any:`tvm.target.Target`, optional |
| 65 | + Host compilation target, if target is device. |
| 66 | + When TVM compiles device specific program such as CUDA, |
| 67 | + we also need host(CPU) side code to interact with the driver |
| 68 | + to setup the dimensions and parameters correctly. |
| 69 | + target_host is used to specify the host side codegen target. |
| 70 | + By default, llvm is used if it is enabled, |
| 71 | + otherwise a stackvm intepreter is used. |
| 72 | +
|
| 73 | + Returns |
| 74 | + ------- |
| 75 | + vm : VirtualMachineDebug |
| 76 | + The debug VM runtime. |
| 77 | + """ |
| 78 | + target = _update_target(target) |
| 79 | + self._compile(mod, target, target_host) |
| 80 | + return VirtualMachineDebug(self._get_vm()) |
| 81 | + |
| 82 | +class VirtualMachineDebug(vm.VirtualMachine): |
| 83 | + """Relay debug VM runtime.""" |
| 84 | + def __init__(self, mod): |
| 85 | + super().__init__(mod) |
| 86 | + self._get_stat = self.mod["get_stat"] |
| 87 | + |
| 88 | + def get_stat(self): |
| 89 | + return self._get_stat() |
0 commit comments