|
| 1 | +# Licensed 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 | +"""Pipeline executor that executes pipeline containing TVM PackedFunc.""" |
| 18 | +import json |
| 19 | +import tvm._ffi |
| 20 | +from tvm import relay |
| 21 | +from tvm.contrib import graph_executor |
| 22 | + |
| 23 | + |
| 24 | +def pipeline_executor_enabled(): |
| 25 | + """check if pipeline executor enabled. |
| 26 | + Return |
| 27 | + ------ |
| 28 | + enable: bool |
| 29 | + return pipeline executor get enabled or not |
| 30 | + """ |
| 31 | + pipeline_enabled = False |
| 32 | + try: |
| 33 | + pipelinecreate = tvm._ffi.get_global_func("tvm.pipeline_executor.create") |
| 34 | + assert pipelinecreate |
| 35 | + pipeline_enabled = True |
| 36 | + except ValueError: |
| 37 | + print("pipeline executor not enabled!") |
| 38 | + |
| 39 | + return pipeline_enabled |
| 40 | + |
| 41 | + |
| 42 | +def build_pipeline(mod_n_configs): |
| 43 | + """build module list that can use for pipeline execution. |
| 44 | +
|
| 45 | + Parameters |
| 46 | + ---------- |
| 47 | + mod_n_configs: Dict[IRModule, Dict[str, Any]] |
| 48 | + build configuration informaton, structure like following. |
| 49 | + {IRModule: {"target":target, |
| 50 | + "target_host":target_host, |
| 51 | + "params":params, |
| 52 | + "mod_name"mod_name, |
| 53 | + "build":build}} |
| 54 | +
|
| 55 | + Returns |
| 56 | + ------- |
| 57 | + ret: List[IRModule] |
| 58 | + list of IRModule |
| 59 | + string_config: Dict[int, Dict[str, any]] |
| 60 | + pipeline configuration |
| 61 | + """ |
| 62 | + mods = {} |
| 63 | + config_len = len(mod_n_configs) |
| 64 | + string_config = [{} for _ in range(config_len)] |
| 65 | + for _, (ir_mod, mod_config) in enumerate(mod_n_configs.items()): |
| 66 | + # init lib_name and json_name params with empty |
| 67 | + lib_name = "" |
| 68 | + json_name = "" |
| 69 | + params_name = "" |
| 70 | + # Get module configuration |
| 71 | + assert "pipeline" in mod_config and "mod_indx" in mod_config["pipeline"] |
| 72 | + # Get module index in pipeline configuration |
| 73 | + mconf = mod_config["pipeline"].copy() |
| 74 | + # Get mod device config |
| 75 | + dev = mod_config["dev"] |
| 76 | + mod_indx = mconf["mod_indx"] - 1 |
| 77 | + target = mod_config["target"] |
| 78 | + assert mod_indx < config_len |
| 79 | + build_func = relay.build |
| 80 | + # if there is a self defined build function then use it. |
| 81 | + if "build" in mod_config and mod_config["build"]: |
| 82 | + build_func = mod_config["build"] |
| 83 | + |
| 84 | + # build IRModule |
| 85 | + mod = build_func( |
| 86 | + ir_mod, |
| 87 | + target, |
| 88 | + params=mod_config["params"], |
| 89 | + target_host=mod_config["target_host"], |
| 90 | + mod_name=mod_config["mod_name"], |
| 91 | + ) |
| 92 | + |
| 93 | + mconf["lib_name"] = lib_name |
| 94 | + mconf["json_name"] = json_name |
| 95 | + mconf["params_name"] = params_name |
| 96 | + mconf["dev"] = "{},{}".format(dev.device_type, dev.device_id) |
| 97 | + # Create pipeline configuration |
| 98 | + string_config[mod_indx] = mconf |
| 99 | + # associate mod with device |
| 100 | + mods[mod] = {"dev": dev} |
| 101 | + |
| 102 | + # return IRModule list and pipeline configuration |
| 103 | + return mods, string_config |
| 104 | + |
| 105 | + |
| 106 | +def create(pipeline_mods, mod_config): |
| 107 | + """Create a pipeline runtime executor. |
| 108 | +
|
| 109 | + Parameters |
| 110 | + ---------- |
| 111 | + pipeline_mods : List[IRModule] |
| 112 | + list of IRModule |
| 113 | +
|
| 114 | + mod_config : Dict[int, Dict[str, Any]] |
| 115 | + modules and modules dependency configuration informaiton. |
| 116 | +
|
| 117 | + Returns |
| 118 | + ------- |
| 119 | + submodule : PipelineModule |
| 120 | + Runtime pipeline module. |
| 121 | + """ |
| 122 | + |
| 123 | + submodule = PipelineModule(pipeline_mods, mod_config) |
| 124 | + return submodule |
| 125 | + |
| 126 | + |
| 127 | +class PipelineModule(object): |
| 128 | + """Wrapper runtime module. This is a thin wrapper of the underlying TVM module. |
| 129 | + you can also directly call set_input, run, and get_output of underlying module functions. |
| 130 | +
|
| 131 | + Parameters |
| 132 | + ---------- |
| 133 | + graph_module : List[GraphModule] |
| 134 | + The internal tvm module that holds the actual graph functions. |
| 135 | +
|
| 136 | + pipeline_config : Dict[IRModule, Dict[str, Any]] |
| 137 | + modules and modules dependency configuration informaiton. |
| 138 | +
|
| 139 | + """ |
| 140 | + |
| 141 | + def graph_executor_create(self, pipeline_mods, mod_config): |
| 142 | + """Create a pipeline runtime executor. |
| 143 | +
|
| 144 | + Parameters |
| 145 | + ---------- |
| 146 | + pipeline_mods : List[IRModule] |
| 147 | + list of IRModule |
| 148 | +
|
| 149 | + mod_config : Dict[int, Dict[str, Any]] |
| 150 | + modules and modules dependency configuration informaiton. |
| 151 | +
|
| 152 | + Returns |
| 153 | + ------- |
| 154 | + mods : GreaphModule |
| 155 | + Runtime graph module. |
| 156 | + """ |
| 157 | + |
| 158 | + mods = [] |
| 159 | + for pipeline_mod in pipeline_mods: |
| 160 | + mod = graph_executor.GraphModule( |
| 161 | + pipeline_mod["default"](pipeline_mods[pipeline_mod]["dev"]) |
| 162 | + ) |
| 163 | + mods.append(mod.module) |
| 164 | + |
| 165 | + return mods, json.dumps(mod_config) |
| 166 | + |
| 167 | + def __init__(self, pipeline_mods, mod_config): |
| 168 | + self.pipeline_mods = pipeline_mods |
| 169 | + self.mod_config = mod_config |
| 170 | + mods, config = self.graph_executor_create(pipeline_mods, mod_config) |
| 171 | + |
| 172 | + pipelinecreate = tvm._ffi.get_global_func("tvm.pipeline_executor.create") |
| 173 | + assert pipelinecreate |
| 174 | + module = pipelinecreate(mods, config) |
| 175 | + |
| 176 | + self.module_ = module |
0 commit comments