huajsj commented on a change in pull request #7892:
URL: https://github.com/apache/tvm/pull/7892#discussion_r626209703



##########
File path: tests/python/relay/test_analysis_pipeline.py
##########
@@ -0,0 +1,144 @@
+# 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.
+
+import numpy as np
+import tvm
+import tvm.testing
+from tvm import relay
+from tvm.relay import transform
+from tvm.contrib import graph_executor, pipeline_executor
+
+
+def run_module(mod, dev, target, dname, data):
+    with tvm.transform.PassContext(opt_level=3):
+        lib = relay.build(mod, target)
+
+    m = graph_executor.GraphModule(lib["default"](dev))
+    m.set_input(dname, data)
+    m.run()
+    n = m.get_num_outputs()
+    output = m.get_output(0).asnumpy()
+    return output
+
+
+def run_modules(mods, dev, target, dname, data):
+    for mod in mods:
+        data = run_module(mod, dev, target, dname, data)
+
+    return data
+
+
+def get_mannual_mod():
+    mods = []
+    dshape = (3, 3)
+    data = relay.var("data", relay.TensorType(dshape, "float32"))
+    mvalue1 = np.full((1), 5).astype("float32")
+    mvalue2 = np.full((1), 2).astype("float32")
+    mvalue3 = np.full((1), 3).astype("float32")
+    mvalue4 = np.full((1), 4).astype("float32")
+    mv1 = relay.Constant(tvm.nd.array(mvalue1))
+    mv2 = relay.Constant(tvm.nd.array(mvalue2))
+    mv3 = relay.Constant(tvm.nd.array(mvalue3))
+    mv4 = relay.Constant(tvm.nd.array(mvalue4))
+    net1 = relay.multiply(data, mv1)
+
+    net2 = relay.add(data, mv2)
+    net2 = relay.add(net2, mv3)
+
+    net3 = relay.multiply(data, mv4)
+
+    net4 = relay.subtract(data, mv1)
+
+    mods.append(tvm.IRModule.from_expr(relay.Function([data], net1)))
+    mods.append(tvm.IRModule.from_expr(relay.Function([data], net2)))
+    mods.append(tvm.IRModule.from_expr(relay.Function([data], net3)))
+    mods.append(tvm.IRModule.from_expr(relay.Function([data], net4)))
+
+    return mods, dshape
+
+
+"""
+#split compute graph into 4 pipeline
+"""
+mods, dshape = get_mannual_mod()
+"""
+#Prepare batch data for pipeline feeding
+"""
+datas = []
+for i in range(len(mods) + 1):
+    datas.append(np.full(dshape, 3 + i).astype("float32"))
+
+"""
+#Run with graph executor for verification purpose
+"""
+outs = []
+for data in datas:
+    outs.append(run_modules(mods, tvm.cpu(), "llvm", "data", data))
+
+"""
+#Parameter use for pipeline executor creation
+"""
+mod_config = {}
+for i in range(len(mods)):
+    mconfig = {"target_host": None, "mod_name": "default", "build": None, 
"params": None}
+    # if cuda enabled, first 2 module us cuda as target
+    if i < 2 and tvm.testing.device_enabled("cuda"):
+        mconfig["target"] = "cuda"
+        mconfig["dev"] = tvm.gpu()
+    else:
+        mconfig["target"] = "llvm"
+        mconfig["dev"] = tvm.cpu()
+
+    mod_config[mods[i]] = mconfig
+
+"""
+#Build module and append module and device type into variable that
+#use for pipeline creation.
+#first and second pipeline use cuda when cuda enable, second and 
+#last pipeline use cpu
+"""
+with relay.build_config(opt_level=3):
+    pipeline_mod = tvm.relay.build(mods, config=mod_config)
+"""
+#Create pipeline executor
+"""
+pipeline_module = pipeline_executor.create(pipeline_mod)
+
+"""
+#Use pipeline executor to pipeline the said pipeline which use different 
backend
+"""
+for data in datas:
+    pipeline_module.set_input("data", data)
+    pipeline_module.run()

Review comment:
       for a pipeline executing, we expecting all backend/hw involved in this 
pipeline process can parallel running at same time, to test such capability we 
need to set multiple data to make that happen. single data would only can 
simulate serialize processing.
   
   about input data/name, only first module need a input, these partitioned 
modules after first module must use the previous modules output as input, and 
such data transfer handle by schedule logic, 
"pipeline_function.cc:pipeline_pipeline_run" implement related logic.
   
   about differentiate,  we not use name for input setting, we depend on 
previous module output number and current module input index to do input set 
for these partition modules except of first module that need a manually input 
data set




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

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to