huajsj commented on a change in pull request #14: URL: https://github.com/apache/tvm-rfcs/pull/14#discussion_r683076998
########## File path: rfcs/0012-pipeline-executor.md ########## @@ -0,0 +1,367 @@ +<!--- 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. --> +- Feature Name: (fill me in with a unique identifier, `my_awesome_feature`) +- Start Date: (fill me in with today's date, YYYY-MM-DD) +- RFC PR: [apache/tvm-rfcs#0014](https://github.com/apache/tvm-rfcs/pull/0014) +- GitHub Issue: [apache/tvm#8596](https://github.com/apache/tvm/issues/8596) + +## 1. Summary + + +This proposal introduces Pipeline Executor: A runtime executor that by scheduling +splitted subgraph of relay graph in pipeline to implement task level parallism to +reduce compute latency. Review comment: about #2, compute latency, in blocking/serialize mode, let's say we get 2 data at same time and need to do compute, for every single data the processing time is 4ms, then first data latency is 4ms, second data latency is 4ms + 4ms = 8ms, in 4 stage pipe line mode, every stage spend 1ms, then first data latency still is 4ms but second data processing time get reduced into 1ms + 4ms = 5ms, hence we can say latency get reduced. about #1, yes this RFC only include executor, i will add the expected input part ########## File path: rfcs/0012-pipeline-executor.md ########## @@ -0,0 +1,367 @@ +<!--- 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. --> +- Feature Name: (fill me in with a unique identifier, `my_awesome_feature`) +- Start Date: (fill me in with today's date, YYYY-MM-DD) +- RFC PR: [apache/tvm-rfcs#0014](https://github.com/apache/tvm-rfcs/pull/0014) +- GitHub Issue: [apache/tvm#8596](https://github.com/apache/tvm/issues/8596) + +## 1. Summary + + +This proposal introduces Pipeline Executor: A runtime executor that by scheduling +splitted subgraph of relay graph in pipeline to implement task level parallism to +reduce compute latency. + +## 2. Motivation + + + +Currently more and more edge device inference deployment cases happens on SOC device, +SOC device have heterogenous chipset like GPU, FPGA, CPU, DSP…, to reach the best +performance there is a requirement to run the ML network parallel in these heterogenous +chipset, however currently graph executor solution only doing the serialize operator +execution without papalism logic and the existing data papalism solution only support +parallel on same chipset(device), then only way to do batch processing on heterogenous +device with tvm is that treat whole ML network as schedule unit and running them on +different heterogenous device but that would cause latency issue(low speed chipset +generate big latency for single data processing) . + +Therefore, we need an runtime executor that can provide papalism scheduling function +with a smaller schedule unit like subgraph (a group of operator with dependency relation) +to be more efficient to use SOC heterogenous hardware resource and get better performance. + + +### Benefits of Pipeline Executor + +There are three benefit for Pipeline Executor + +Pipeline Executor provides: +* Compute single network on Multiple backend in parallel to improve performance. Review comment: for example for a resnet18 compute, first we manually split the network into 4 subgraph, then use pipeline executor to build these four subgraph with target llvm+cpu(0), opencl-gpu(0), opencl -gpu(1), VTA(FPGA), and generate the dependency relation, when pipeline executor run the compute, the resnet18(now already get split into 4 subgraph) would get running in multiple backend in parallel(pipeline). ########## File path: rfcs/0012-pipeline-executor.md ########## @@ -0,0 +1,367 @@ +<!--- 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. --> +- Feature Name: (fill me in with a unique identifier, `my_awesome_feature`) +- Start Date: (fill me in with today's date, YYYY-MM-DD) +- RFC PR: [apache/tvm-rfcs#0014](https://github.com/apache/tvm-rfcs/pull/0014) +- GitHub Issue: [apache/tvm#8596](https://github.com/apache/tvm/issues/8596) + +## 1. Summary + + +This proposal introduces Pipeline Executor: A runtime executor that by scheduling +splitted subgraph of relay graph in pipeline to implement task level parallism to +reduce compute latency. + +## 2. Motivation + + + +Currently more and more edge device inference deployment cases happens on SOC device, +SOC device have heterogenous chipset like GPU, FPGA, CPU, DSP…, to reach the best +performance there is a requirement to run the ML network parallel in these heterogenous +chipset, however currently graph executor solution only doing the serialize operator +execution without papalism logic and the existing data papalism solution only support +parallel on same chipset(device), then only way to do batch processing on heterogenous +device with tvm is that treat whole ML network as schedule unit and running them on +different heterogenous device but that would cause latency issue(low speed chipset +generate big latency for single data processing) . + +Therefore, we need an runtime executor that can provide papalism scheduling function +with a smaller schedule unit like subgraph (a group of operator with dependency relation) +to be more efficient to use SOC heterogenous hardware resource and get better performance. + + +### Benefits of Pipeline Executor + +There are three benefit for Pipeline Executor + +Pipeline Executor provides: +* Compute single network on Multiple backend in parallel to improve performance. + +* With RPC help do ML network distribute computation cross multiple remote device + +* User can use Pipeline Executor to integrate pre-compute processing and pos-processing with + network compute together and compute in same executor. Review comment: let's take yolov3-tiny deploy as a example, before run yolov3-tiny tvm build library, user need to prepare the data that include #1 image format transfer(for example YUV to RGB) #2 , image reshape to match network size #3. data normalization as network required, all these I called it as pre-compute processing, after yolov3-tiny tvm build library run, user can get a [1, 3, 85, 13,13], [1, 3,85,26,26] array, user need to parse these 2 array to NMS sort to get bunding box, this I called it post-processing. Currently pre-processing and post-processing not handled/scheduled by TVM, Pipeline executor provide such scheduling solution to involve these 2 part with network compute together to provide complete solution for model deploy. ########## File path: rfcs/0012-pipeline-executor.md ########## @@ -0,0 +1,367 @@ +<!--- 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. --> +- Feature Name: (fill me in with a unique identifier, `my_awesome_feature`) +- Start Date: (fill me in with today's date, YYYY-MM-DD) +- RFC PR: [apache/tvm-rfcs#0014](https://github.com/apache/tvm-rfcs/pull/0014) +- GitHub Issue: [apache/tvm#8596](https://github.com/apache/tvm/issues/8596) + +## 1. Summary + + +This proposal introduces Pipeline Executor: A runtime executor that by scheduling +splitted subgraph of relay graph in pipeline to implement task level parallism to +reduce compute latency. + +## 2. Motivation + + + +Currently more and more edge device inference deployment cases happens on SOC device, +SOC device have heterogenous chipset like GPU, FPGA, CPU, DSP…, to reach the best +performance there is a requirement to run the ML network parallel in these heterogenous +chipset, however currently graph executor solution only doing the serialize operator +execution without papalism logic and the existing data papalism solution only support +parallel on same chipset(device), then only way to do batch processing on heterogenous +device with tvm is that treat whole ML network as schedule unit and running them on +different heterogenous device but that would cause latency issue(low speed chipset +generate big latency for single data processing) . + +Therefore, we need an runtime executor that can provide papalism scheduling function +with a smaller schedule unit like subgraph (a group of operator with dependency relation) +to be more efficient to use SOC heterogenous hardware resource and get better performance. + + +### Benefits of Pipeline Executor + +There are three benefit for Pipeline Executor + +Pipeline Executor provides: +* Compute single network on Multiple backend in parallel to improve performance. + +* With RPC help do ML network distribute computation cross multiple remote device + +* User can use Pipeline Executor to integrate pre-compute processing and pos-processing with + network compute together and compute in same executor. + +## 3. Guide-level explanation +Pipeline Executor is a runtime executor which implement pipeline execution logic for multiple +subgraph and relies on graph_executor for operator storage, execution. + +This section introduce the use case for Pipeline Executor. + +* 1. Manually constructing pipeline subgraph from a network compute graph. +* 2. Manually contstructin pipeline subgraph configuration for dependency and target device... +* 3. Use pipeline_executor to build pipeline module with the said subgraph and configuration. +* 4. Use pipeline_executor to load pipeline module to run network in pipeline parallism mode. + +### 3.1. Manually constructing pipeline subgraph from a network compute graph. +pipeline subgraph is subset of network compute graph, there are dependency relation +between different pipeline subgraph, each pipeline subgraph running on different backend +, the purpose of split network into pipeline subgraph is to do network compute on different +compute unit and pipeline them to reduce compute latency, following is example for network +compute graph split. + +```python +import tvm +from ...ir import IRModule +from ...relay import transform, build_module +def pipeline_graph(expr, indices): + """Split Graph Into A Group Of Subgraph + Parameters + ---------- + expr : tvm.relay.Expr + indices : Array[int] + Returns + ------- + ret : Array[tvm.relay.IRModule] + """ + + def run_opt_pass(expr, opt_pass): + """Exectue a relay pass""" + assert isinstance(opt_pass, tvm.transform.Pass) + mod = tvm.IRModule.from_expr(expr) + mod = tvm.relay.transform.InferType()(mod) + mod = opt_pass(mod) + entry = mod["main"] + return entry if isinstance(expr, tvm.relay.Function) else entry.body + + def _operator_idx_inc(expr, operator_current_idx): + """Increase operator index""" + if not isinstance(expr, tvm.relay.expr.Constant): + operator_current_idx = operator_current_idx + 1 + + return operator_current_idx + + def merge_constant_expr(constant_expr, expr): + # merge constant express with a express + # Parameters + # ---------- + # constant_expr: + # constant expression + # expr: + # expression to merge with constant expression + + # If body not let, then reached end of the express + if not isinstance(constant_expr.body, tvm.relay.expr.Let): + return tvm.relay.expr.Let(constant_expr.var, constant_expr.value, expr) + + return tvm.relay.expr.Let( + constant_expr.var, constant_expr.value, merge_constant_expr(constant_expr.body, expr) + ) + + def _recursion(anf, operator_indx, pipeline_mods, indices, constant_expr): + # Enumrate all operator of compute graph then split the compute graph + # into a group subgraph. + # Parameters + # ---------- + # anf: + # ANF format expression + # operator_indx: + # current operator indice + # pipeline_mods: + # the subgraph list get storage in this variable + # indices: + # Array of indices use to define the subgraph scope + # constant_expr: + # constant defined before current operator + + # Do the split work + if isinstance(anf, tvm.relay.Function): + return tvm.relay.Function( + anf.params, + _recursion(anf.body, operator_indx, pipeline_mods, indices, constant_expr), + anf.ret_type, + anf.type_params, + anf.attrs, + ) + if isinstance(anf, tvm.relay.expr.Let): + value = anf.value + operator_indx = _operator_idx_inc(value, operator_indx) + + # record constan expr to make sure all sugraph can find correct + # constant. + if isinstance(value, tvm.relay.expr.Constant): + if not constant_expr: + constant_expr = tvm.relay.expr.Let(anf.var, value, anf.var) + else: + constant_expr = tvm.relay.expr.Let(anf.var, value, constant_expr) + + if isinstance(value, tvm.relay.expr.Call): + if isinstance(value.op, tvm.ir.Op): + + # if have expr a(b(c(d(e)))) and indexes are [1,2,3] + # then would get separate modules for a(b),c,d(e). + # the split area is a(b)[0,1] c[2,2] d(e)[2,3] + if indices and operator_indx == indices[0]: + indices.pop(0) + ann = _recursion( + anf.body, operator_indx, pipeline_mods, indices, constant_expr + ) + + # when current subgraph use previous subgraph constant, + # such constant may become free varaible due to the constant + # not exist, merge the previous constant with current subgraph + # to avoid such issue. + if constant_expr: + ann = merge_constant_expr(constant_expr, ann) + + ann = run_opt_pass(ann, transform.ToGraphNormalForm()) + mod = tvm.IRModule.from_expr(ann) + pipeline_mods.insert(0, mod) + return tvm.relay.expr.Let(anf.var, value, anf.var) + return tvm.relay.expr.Let( + anf.var, + value, + _recursion(anf.body, operator_indx, pipeline_mods, indices, constant_expr), + ) + else: + return anf + + pipeline_mods = [] + + # operator count start from 0, then initial value get set into -1 + operator_indx = -1 + constant_expr = None + subgraph_indices = indices.copy() + anf = run_opt_pass(expr, transform.ToANormalForm()) + anf = run_opt_pass(anf, transform.InferType()) + ann = _recursion(anf, operator_indx, pipeline_mods, subgraph_indices, constant_expr) + ann = run_opt_pass(ann.body, transform.ToGraphNormalForm()) + mod = tvm.IRModule.from_expr(ann) + pipeline_mods.insert(0, mod) + return pipeline_mods + +#... +mod, params = relay.frontend.from_darknet(net, dtype=dtype, shape=dshape) +split = [11, 22] +mods = pipeline_graph(mod["main"], split) +``` + +### 3.2. Manually contstructin pipeline subgraph configuration for dependency and target device... +There are dependency between pipeline subgraph, for example we have 3 pipeline subgraph named +s1, s2, and s3, s2 input is s1 output and s2 output is s3 input, we need to construct a configuation +file to descript such dependency relation, such configuratin also need to involved "target" and +"device" information following is a example. + +```python +mconfig = {"target_host": None, "mod_name": "default", "build": None, "params": None} + mconfig1 = mconfig.copy() + mconfig1["target"] = "cuda" + mconfig1["dev"] = tvm.gpu[0] + # third output is final output, second output for mod3, first for mod2 + # input + mconfig1["pipeline"] = { + "mod_indx": 1, + "output": [ + {"output_indx": 1, "dependent": [{"mod_indx": 2, "input_name": "data_0"}]}, + {"output_indx": 2, "dependent": [{"mod_indx": 3, "input_name": "data_0"}]}, + {"output_indx": 3, "dependent": [{"mod_indx": 0, "input_name": "1"}]}, + ], + } + mod_config[mods[0]] = mconfig1 + + mconfig2 = mconfig.copy() + mconfig2["target"] = "llvm" + mconfig2["dev"] = tvm.cpu(0) + mconfig2["pipeline"] = { + "mod_indx": 2, + "output": [ + {"output_indx": 1, "dependent": [{"mod_indx": 3, "input_name": "data_1"}]}, + ], + } + mod_config[mods[1]] = mconfig2 + + mconfig3 = mconfig.copy() + mconfig3["target"] = "llvm" + mconfig3["dev"] = tvm.cpu(0) + + mconfig3["pipeline"] = { + "mod_indx": 3, + "output": [{"output_indx": 1, "dependent": [{"mod_indx": 0, "input_name": "2"}]}], + } + mod_config[mods[2]] = mconfig3 +``` + +### 3.3. Use pipeline_executor to build pipeline module with the said subgraph and configuration. + +Pipeline executor provide a build function to compile and save the compile output into disk, +following is a example + +```python + with relay.build_config(opt_level=3): + pipeline_mods, string_config = pipeline_executor.build_pipeline( + mod_config, "<path to storage the build output>" Review comment: build output are multiple files, to hide the complexity of export/import here add a path to save multiple output into a disk directory, then import only need the path as 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
