Skip to content

Commit 8d41893

Browse files
committed
[Runtime] Pipeline Executor Initial patch.
This patch is one of serial patch for PR 7892 splitting.this is the initial part of the pipeline executor, this patch include the cmake change and python and C++ interface for pipeline executor.
1 parent 49756a5 commit 8d41893

File tree

6 files changed

+442
-0
lines changed

6 files changed

+442
-0
lines changed

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,12 @@ if(USE_PROFILER)
377377
list(APPEND RUNTIME_SRCS ${RUNTIME_VM_PROFILER_SRCS})
378378
endif(USE_PROFILER)
379379

380+
if(USE_PIPELINE_EXECUTOR)
381+
message(STATUS "Build with Pipeline Executor support...")
382+
file(GLOB RUNTIME_PIPELINE_SRCS src/runtime/pipeline/*.cc)
383+
list(APPEND RUNTIME_SRCS ${RUNTIME_PIPELINE_SRCS})
384+
endif(USE_PIPELINE_EXECUTOR)
385+
380386
# Module rules
381387
include(cmake/modules/VTA.cmake)
382388
include(cmake/modules/StandaloneCrt.cmake)

cmake/config.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ set(USE_GRAPH_EXECUTOR ON)
105105
# Whether enable tiny graph executor with CUDA Graph
106106
set(USE_GRAPH_EXECUTOR_CUDA_GRAPH OFF)
107107

108+
# Whether enable subgraph runtime.
109+
set(USE_PIPELINE_EXECUTOR ON)
110+
108111
# Whether to enable the profiler for the graph executor and vm
109112
set(USE_PROFILER ON)
110113

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
/*!
21+
* \file pipeline_executor.cc
22+
*/
23+
#include "pipeline_executor.h"
24+
25+
namespace tvm {
26+
namespace runtime {
27+
28+
void SubGraphRuntime::Init(const Array<tvm::runtime::Module>& modules,
29+
const std::string& pipeline_json) {
30+
return;
31+
}
32+
33+
PackedFunc SubGraphRuntime::GetFunction(const std::string& name,
34+
const ObjectPtr<Object>& sptr_to_self) {
35+
return PackedFunc();
36+
}
37+
38+
Module PipelineRuntimeCreate(const Array<tvm::runtime::Module>& m,
39+
const std::string& pipeline_json) {
40+
auto exec = make_object<SubGraphRuntime>();
41+
exec->Init(m, pipeline_json);
42+
return Module(exec);
43+
}
44+
45+
TVM_REGISTER_GLOBAL("tvm.pipeline_executor.create").set_body([](TVMArgs args, TVMRetValue* rv) {
46+
*rv = PipelineRuntimeCreate(args[0], args[1]);
47+
});
48+
} // namespace runtime
49+
} // namespace tvm
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
/*!
21+
* \brief pipeline executor
22+
* \file pipeline_executor.h
23+
*/
24+
#ifndef TVM_RUNTIME_PIPELINE_PIPELINE_EXECUTOR_H_
25+
#define TVM_RUNTIME_PIPELINE_PIPELINE_EXECUTOR_H_
26+
#include <tvm/runtime/registry.h>
27+
28+
#include <memory>
29+
#include <string>
30+
#include <unordered_map>
31+
#include <vector>
32+
33+
#include "../file_utils.h"
34+
using namespace std;
35+
namespace tvm {
36+
namespace runtime {
37+
38+
/*!
39+
* \brief pipeline runtime.
40+
*
41+
* This runtime can be acccesibly in various language via
42+
* TVM runtime PackedFunc API.
43+
*/
44+
class TVM_DLL SubGraphRuntime : public ModuleNode {
45+
public:
46+
/*!
47+
* \return The type key of the executor.
48+
*/
49+
const char* type_key() const final { return "SubGraphRuntime"; }
50+
/*!
51+
* \brief Initialize the graph executor with graph and context.
52+
* \param graph_json The execution graph.
53+
* \param module The module containing the compiled functions for the host
54+
* processor.
55+
* \param ctxs The context of the host and devices where graph nodes will be
56+
* executed on.
57+
* \param lookup_linked_param_func If given, a PackedFunc invoked to lookup linked parameters
58+
* by storage_id. If not given, linked parameters are looked-up using an internal implementation,
59+
* which is not compatible with RPCModules.
60+
*/
61+
void Init(const Array<tvm::runtime::Module>& modules, const std::string& pipeline_json);
62+
/*!
63+
* \brief Get member function to front-end
64+
* \param name The name of the function.
65+
* \param sptr_to_self The pointer to the module node.
66+
* \return The corresponding member function.
67+
*/
68+
virtual PackedFunc GetFunction(const std::string& name, const ObjectPtr<Object>& sptr_to_self);
69+
};
70+
} // namespace runtime
71+
} // namespace tvm
72+
#endif // TVM_RUNTIME_PIPELINE_PIPELINE_EXECUTOR_H_

0 commit comments

Comments
 (0)