Skip to content

Commit 5e77e40

Browse files
authored
Merge pull request #1387 from TianShaoqing/develop
[Backend] update poros
2 parents 08aa209 + f212f5c commit 5e77e40

File tree

175 files changed

+256
-72
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

175 files changed

+256
-72
lines changed

.gitmodules

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[submodule "poros/third_party/googletest"]
2+
path = poros/third_party/googletest
3+
url = https://github.com/google/googletest.git
4+
[submodule "poros/third_party/gflags"]
5+
path = poros/third_party/gflags
6+
url = https://github.com/gflags/gflags.git

poros/CMakeLists.txt

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
cmake_minimum_required(VERSION 3.21)
22
project(poros)
33
set(CMAKE_CXX_STANDARD 14)
4-
add_definitions(-D_GLIBCXX_USE_CXX11_ABI=1)
5-
64

75
option(BUILD_STATIC "build lib${PROJECT_NAME}.a static lib" OFF)
86
option(BUILD_KERNEL "build lib${PROJECT_NAME}-kernel.so shared lib" OFF)
@@ -11,7 +9,17 @@ option(BUILD_TOOL "build ${PROJECT_NAME}-tool, an executable binary output" OFF)
119
option(TEST "build for test. copy '.so' to site-packages automatically after compile" OFF)
1210
option(DEBUG "build for debug. add '-g' flag to gcc for detailed debug information" ON)
1311
option(UT "build for unit test" OFF)
12+
option(ABI "build ${PROJECT_NAME} with application binary interface (ABI) is on" OFF)
1413

14+
# abi configuration
15+
if (NOT ABI)
16+
add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)
17+
else ()
18+
if (BUILD_TOOL OR UT)
19+
message(FATAL_ERROR "${PROJECT_NAME}-tool or unit test are not supported when abi is on.")
20+
endif()
21+
add_definitions(-D_GLIBCXX_USE_CXX11_ABI=1)
22+
endif ()
1523

1624
# minimum requirements
1725
set(PYTHON_MINIMUM_VERSION 3.6)
@@ -49,42 +57,42 @@ find_package(CUDNN ${CUDNN_MINIMUM_VERSION} REQUIRED)
4957

5058
## release headers
5159
# engine
52-
file(GLOB headers "${PROJECT_SOURCE_DIR}/src/poros/engine/iengine.h" "${PROJECT_SOURCE_DIR}/src/poros/engine/engine_context.h")
60+
file(GLOB headers "${PROJECT_SOURCE_DIR}/poros/engine/iengine.h" "${PROJECT_SOURCE_DIR}/poros/engine/engine_context.h")
5361
file(COPY ${headers} DESTINATION "${PROJECT_SOURCE_DIR}/build/include/poros/engine")
5462
# compile
55-
file(GLOB headers "${PROJECT_SOURCE_DIR}/src/poros/compile/poros_module.h")
63+
file(GLOB headers "${PROJECT_SOURCE_DIR}/poros/compile/poros_module.h")
5664
file(COPY ${headers} DESTINATION "${PROJECT_SOURCE_DIR}/build/include/poros/compile")
57-
file(GLOB headers "${PROJECT_SOURCE_DIR}/src/poros/compile/compile.h")
65+
file(GLOB headers "${PROJECT_SOURCE_DIR}/poros/compile/compile.h")
5866
file(COPY ${headers} DESTINATION "${PROJECT_SOURCE_DIR}/build/include/poros/compile")
5967
# converter
60-
file(GLOB headers "${PROJECT_SOURCE_DIR}/src/poros/converter/iconverter.h")
68+
file(GLOB headers "${PROJECT_SOURCE_DIR}/poros/converter/iconverter.h")
6169
file(COPY ${headers} DESTINATION "${PROJECT_SOURCE_DIR}/build/include/poros/converter")
6270
# iplugin
63-
file(GLOB headers "${PROJECT_SOURCE_DIR}/src/poros/iplugin/*.h")
71+
file(GLOB headers "${PROJECT_SOURCE_DIR}/poros/iplugin/*.h")
6472
file(COPY ${headers} DESTINATION "${PROJECT_SOURCE_DIR}/build/include/poros/iplugin")
6573
## context
66-
file(GLOB headers "${PROJECT_SOURCE_DIR}/src/poros/context/*.h")
74+
file(GLOB headers "${PROJECT_SOURCE_DIR}/poros/context/*.h")
6775
file(COPY ${headers} DESTINATION "${PROJECT_SOURCE_DIR}/build/include/poros/context")
6876
## context
69-
file(GLOB headers "${PROJECT_SOURCE_DIR}/src/poros/context/*.h")
77+
file(GLOB headers "${PROJECT_SOURCE_DIR}/poros/context/*.h")
7078
file(COPY ${headers} DESTINATION "${PROJECT_SOURCE_DIR}/build/include/poros/context")
7179
## lowering
72-
file(GLOB headers "${PROJECT_SOURCE_DIR}/src/poros/lowering/*.h")
80+
file(GLOB headers "${PROJECT_SOURCE_DIR}/poros/lowering/*.h")
7381
file(COPY ${headers} DESTINATION "${PROJECT_SOURCE_DIR}/build/include/poros/lowering")
7482
## util
75-
file(GLOB headers "${PROJECT_SOURCE_DIR}/src/poros/util/*.h")
83+
file(GLOB headers "${PROJECT_SOURCE_DIR}/poros/util/*.h")
7684
file(COPY ${headers} DESTINATION "${PROJECT_SOURCE_DIR}/build/include/poros/util")
7785
## log
78-
file(GLOB headers "${PROJECT_SOURCE_DIR}/src/poros/log/*.h")
86+
file(GLOB headers "${PROJECT_SOURCE_DIR}/poros/log/*.h")
7987
file(COPY ${headers} DESTINATION "${PROJECT_SOURCE_DIR}/build/include/poros/log")
8088

8189

8290
include_directories(${TORCH_INCLUDE_DIRS})
8391
include_directories(${TensorRT_INCLUDE_DIRS})
8492
include_directories(${CUDA_INCLUDE_DIRS})
8593
include_directories(${CUDNN_INCLUDE_PATH})
86-
include_directories(src)
87-
include_directories(src/poros/compile)
94+
include_directories(${PROJECT_SOURCE_DIR})
95+
include_directories(poros/compile)
8896

8997

9098
add_compile_options(-D__const__= -D_GNU_SOURCE)
@@ -109,9 +117,9 @@ add_compile_options(
109117

110118
file(
111119
GLOB POROS_CPP_FILES
112-
"./src/poros/*/*.cpp"
113-
"./src/poros/converter/*/*.cpp"
114-
"./src/poros/converter/gpu/plugins/*.cpp"
120+
"./poros/*/*.cpp"
121+
"./poros/converter/*/*.cpp"
122+
"./poros/converter/gpu/plugins/*.cpp"
115123
)
116124

117125

@@ -183,13 +191,13 @@ endif ()
183191
# kernel
184192
file(
185193
GLOB POROS_KERNEL_CPP_FILES
186-
./src/poros/compile/*.cpp
187-
./src/poros/context/*.cpp
188-
./src/poros/iplugin/*.cpp
189-
./src/poros/log/*.cpp
190-
./src/poros/lowering/*.cpp
191-
./src/poros/util/*.cpp
192-
./src/poros/engine/engine.cpp
194+
./poros/compile/*.cpp
195+
./poros/context/*.cpp
196+
./poros/iplugin/*.cpp
197+
./poros/log/*.cpp
198+
./poros/lowering/*.cpp
199+
./poros/util/*.cpp
200+
./poros/engine/engine.cpp
193201
)
194202

195203
# kernel SHARED

poros/README.md

Lines changed: 1 addition & 1 deletion

poros/src/poros/compile/compile.cpp renamed to poros/poros/compile/compile.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,15 @@ int Compiler::compile(const torch::jit::Module& origin_module,
150150
opt_graph = graph_and_ivalues.first;
151151
}
152152

153-
//cpu的话 过了预处理就返回
153+
std::shared_ptr<torch::jit::Graph> prewarm_graph = graph_prewarm(opt_graph, prewarm_datas);
154+
GRAPH_DUMP("prewarmed_module graph:", prewarm_graph);
155+
156+
//cpu的话,预热后就返回
154157
if (_options.device == Device::CPU) {
155-
merge_graph_to_module(opt_graph, *optimized_module, true);
158+
merge_graph_to_module(prewarm_graph, *optimized_module, true);
156159
return 0;
157160
}
158161

159-
std::shared_ptr<torch::jit::Graph> prewarm_graph = graph_prewarm(opt_graph, prewarm_datas);
160-
GRAPH_DUMP("prewarmed_module graph:", prewarm_graph);
161-
162162
//step2: try to find segments in unfold module
163163
//划分完子图的模型
164164
int ret = segment_graph(prewarm_graph);
File renamed without changes.

poros/src/poros/compile/graph_segment.cpp renamed to poros/poros/compile/graph_segment.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,24 @@ struct PorosGraphSegment {
102102
}
103103
}
104104

105+
// aten::__getitem__ idx参数不支持非constant类型
106+
if (node->kind() == torch::jit::aten::__getitem__) {
107+
if (node->inputs().size() == 2 &&
108+
node->input(1)->node()->kind() != torch::jit::prim::Constant) {
109+
LOG(WARNING) << "The index input of aten::__getitem__ is not supported as non-constant type.";
110+
return false;
111+
}
112+
}
113+
114+
// aten::_set_item idx参数不支持非constant类型
115+
if (node->kind() == torch::jit::aten::_set_item) {
116+
if (node->inputs().size() == 3 &&
117+
node->input(1)->node()->kind() != torch::jit::prim::Constant) {
118+
LOG(WARNING) << "The index input of aten::_set_item is not supported as non-constant type.";
119+
return false;
120+
}
121+
}
122+
105123
if (node->kind() == kind_ || engine_->is_node_supported(node)) {
106124
return true;
107125
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

poros/src/poros/converter/gpu/aten_eval.cpp renamed to poros/poros/converter/gpu/aten_eval.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ bool AppendConverter::converter(TensorrtEngine* engine, const torch::jit::Node *
6666
bool GetitemConverter::converter(TensorrtEngine* engine, const torch::jit::Node *node) {
6767
at::ArrayRef<const torch::jit::Value*> inputs = node->inputs();
6868
POROS_CHECK_TRUE((inputs.size() == 2), "invaid inputs size for GetitemConverter");
69+
POROS_CHECK_TRUE((inputs[1]->node()->kind() == torch::jit::prim::Constant),
70+
"inputs[1] for GetitemConverter is not come from prim::Constant as expected");
6971

7072
if (node->outputs()[0]->type()->str() == "Tensor") {
7173
//extract list
@@ -126,6 +128,8 @@ bool GetitemConverter::converter(TensorrtEngine* engine, const torch::jit::Node
126128
bool SetitemConverter::converter(TensorrtEngine* engine, const torch::jit::Node *node) {
127129
at::ArrayRef<const torch::jit::Value*> inputs = node->inputs();
128130
POROS_CHECK_TRUE((inputs.size() == 3), "invaid inputs size for SetitemConverter");
131+
POROS_CHECK_TRUE((inputs[1]->node()->kind() == torch::jit::prim::Constant),
132+
"inputs[1] for SetitemConverter is not come from prim::Constant as expected");
129133

130134
size_t idx = engine->context().get_constant(inputs[1]).toInt();
131135

poros/src/poros/converter/gpu/reduce.cpp renamed to poros/poros/converter/gpu/reduce.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,75 @@ bool MaxMinConverter::converter(TensorrtEngine* engine, const torch::jit::Node *
271271
return true;
272272
}
273273

274+
/*
275+
"aten::argmax(Tensor self, int? dim=None, bool keepdim=False) -> (Tensor)"*/
276+
bool ArgmaxArgminConverter::converter(TensorrtEngine* engine, const torch::jit::Node *node) {
277+
at::ArrayRef<const torch::jit::Value*> inputs = node->inputs();
278+
POROS_CHECK_TRUE((inputs[0]->type()->isSubtypeOf(c10::TensorType::get())),
279+
"input[0] for ArgmaxArgminConverter is not Tensor as expected");
280+
281+
// TODO: to imp dim=None
282+
POROS_CHECK_TRUE((inputs[1]->type()->isSubtypeOf(c10::IntType::get())),
283+
"input[1] for ArgmaxArgminConverter is not int as expected");
284+
285+
//extract self
286+
auto in_tensor = engine->context().get_tensor(inputs[0]);
287+
POROS_CHECK_TRUE((in_tensor != nullptr), "Unable to init input tensor for node: " << *node);
288+
auto in_dims = nvdim_to_sizes(in_tensor->getDimensions());
289+
290+
bool is_dynamic = check_nvtensor_is_dynamic(in_tensor);
291+
292+
POROS_CHECK_TRUE((in_dims.size() > 1),
293+
"Converter aten::argmax error: At least 2 dimensions are required for input[0].");
294+
nvinfer1::ITensor* output_indices = nullptr;
295+
296+
int64_t dim = 0;
297+
dim = engine->context().get_constant(inputs[1]).toInt();
298+
dim = dim < 0 ? in_dims.size() + dim : dim;
299+
bool keep_dim = engine->context().get_constant(inputs[2]).toBool();
300+
uint32_t shiftDim = 1 << dim;
301+
302+
// nvinfer1::TopKOperation noly support kFLOAT, so this is transfer kINT32 to kFLOAT
303+
if (in_tensor->getType() == nvinfer1::DataType::kINT32) {
304+
auto id_layer = engine->network()->addIdentity(*in_tensor);
305+
id_layer->setOutputType(0, nvinfer1::DataType::kFLOAT);
306+
id_layer->setName((layer_info(node) + "_IIdentityLayer_int32_to_float").c_str());
307+
in_tensor = id_layer->getOutput(0);
308+
}
309+
310+
nvinfer1::TopKOperation topk_option = (node->kind() == torch::jit::aten::argmax) ?
311+
nvinfer1::TopKOperation::kMAX :
312+
nvinfer1::TopKOperation::kMIN;
313+
nvinfer1::ITopKLayer* topk_layer = engine->network()->addTopK(*in_tensor, topk_option, 1, shiftDim);
314+
POROS_CHECK(topk_layer, "Unable to create TopK layer from node: " << *node);
315+
topk_layer->setName((layer_info(node) + "_ITopKLayer").c_str());
316+
output_indices = topk_layer->getOutput(1);
317+
318+
// squeeze output dim
319+
if (in_tensor->getDimensions().nbDims > 1 && !keep_dim) {
320+
auto shuffle_layer = engine->network()->addShuffle(*output_indices);
321+
if (is_dynamic) {
322+
nvinfer1::ITensor* self_shape_tensor = engine->network()->addShape(*in_tensor)->getOutput(0);
323+
nvinfer1::ITensor* squeeze_output_shape = squeeze_nv_shapetensor(engine, self_shape_tensor, dim);
324+
shuffle_layer->setInput(1, *squeeze_output_shape);
325+
} else {
326+
in_dims.erase(in_dims.begin() + dim);
327+
nvinfer1::Dims squeeze_output_dims = sizes_to_nvdim(in_dims);
328+
shuffle_layer->setReshapeDimensions(squeeze_output_dims);
329+
}
330+
output_indices = shuffle_layer->getOutput(0);
331+
}
332+
engine->context().set_tensor(node->outputs()[0], output_indices);
333+
LOG(INFO) << "Output tensor shape: " << output_indices->getDimensions();
334+
return true;
335+
}
336+
337+
274338
POROS_REGISTER_CONVERTER(TensorrtEngine, MeanConverter);
275339
POROS_REGISTER_CONVERTER(TensorrtEngine, SumConverter);
276340
POROS_REGISTER_CONVERTER(TensorrtEngine, ProdConverter);
277341
POROS_REGISTER_CONVERTER(TensorrtEngine, MaxMinConverter);
342+
POROS_REGISTER_CONVERTER(TensorrtEngine, ArgmaxArgminConverter);
278343

279344
} // namespace poros
280345
} // namespace mirana

poros/src/poros/converter/gpu/reduce.h renamed to poros/poros/converter/gpu/reduce.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,26 @@ class MaxMinConverter : public GpuConverter {
135135
}
136136
};
137137

138+
class ArgmaxArgminConverter : public GpuConverter {
139+
public:
140+
ArgmaxArgminConverter() {}
141+
virtual ~ArgmaxArgminConverter() {}
142+
143+
bool converter(TensorrtEngine* engine, const torch::jit::Node *node);
144+
145+
const std::vector<std::string> schema_string() {
146+
return {"aten::argmax(Tensor self, int dim, bool keepdim=False) -> (Tensor)",
147+
"aten::argmax(Tensor self, int? dim=None, bool keepdim=False) -> (Tensor)",
148+
"aten::argmin(Tensor self, int dim, bool keepdim=False) -> (Tensor)",
149+
};
150+
}
151+
152+
const std::vector<torch::jit::NodeKind> node_kind() {
153+
return {torch::jit::aten::argmax,
154+
torch::jit::aten::argmin};
155+
}
156+
};
157+
138158
} // namespace poros
139159
} // namespace mirana
140160
} // namespace baidu
File renamed without changes.

poros/src/poros/converter/iconverter.h renamed to poros/poros/converter/iconverter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,9 @@ class ConvertersMap {
360360
void init_unsupport_op_set() {
361361
try {
362362
std::vector<std::string> unsupport_op_vec = PorosGlobalContext::instance().get_poros_options().unsupport_op_list;
363+
// 每次设置unsupport_op_list时刷新schema和nodekind set,避免用户下次编译想重新设置还保留之前不支持的op
364+
_unsupport_schema_set.clear();
365+
_unsupport_nodekind_set.clear();
363366
for (size_t i = 0; i < unsupport_op_vec.size(); i++) {
364367
std::string line = unsupport_op_vec[i];
365368
if (line.size() == 0) {
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

poros/python/poros/_compile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def compile(module, prewarm_inputs, poros_options):
7373
Args:
7474
module (torch.nn.Module / torch.jit.ScriptModule): Source module
7575
input (list of tensor input): prewarmed data.
76-
poros_options(PorosOptions / Dict of settings): compile settings for poros
76+
poros_options(PorosOptions): compile settings for poros
7777
Returns:
7878
PorosModule: Compiled Module of poros,
7979
when run it will partially execute via inlined engine (which is TensorRT)

poros/python/poros/_input_convert.py

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ def convert_prewarm_inputs(prewarm_inputs):
6161
else:
6262
raise TypeError("prewarm_inputs for poros should be torch.Tensor or wraped as tuple or inputs-lists, fix it")
6363
return wraped_prewarm_inputs
64+
# info = poros._C.PreWarmDatas()
65+
# info.set_data(prewarm_inputs)
6466

6567
def convert_poros_option(poros_option):
6668
# type: Dict[str, Any] -> poros._C.PorosOptions
@@ -70,39 +72,6 @@ def convert_poros_option(poros_option):
7072
option = poros._C.PorosOptions()
7173
if poros_option is None:
7274
#default situation. if user do not set the poros_option
73-
return option
74-
elif isinstance(poros_option, dict):
75-
if "debug" in poros_option:
76-
assert isinstance(poros_option["debug"], bool)
77-
option.debug = poros_option["debug"]
78-
79-
if "use_fp16" in poros_option:
80-
assert isinstance(poros_option["use_fp16"], bool)
81-
option.use_fp16 = poros_option["use_fp16"]
82-
83-
if "max_workspace_size" in poros_option:
84-
assert type(poros_option["max_workspace_size"]) is int
85-
option.max_workspace_size = poros_option["max_workspace_size"]
86-
87-
if "device" in poros_option:
88-
option.device = _parse_device(poros_option["device"])
89-
90-
if "is_dynamic" in poros_option:
91-
assert isinstance(poros_option["is_dynamic"], bool)
92-
option.is_dynamic = poros_option["is_dynamic"]
93-
94-
if "long_to_int" in poros_option:
95-
assert isinstance(poros_option["long_to_int"], bool)
96-
option.long_to_int = poros_option["long_to_int"]
97-
98-
if "device_id" in poros_option:
99-
assert type(poros_option["device_id"]) is int
100-
option.device_id = poros_option["device_id"]
101-
102-
if "preprocess_mode" in poros_option:
103-
assert type(poros_option["preprocess_mode"]) is int
104-
option.preprocess_mode= poros_option["preprocess_mode"]
105-
10675
return option
10776
elif isinstance(poros_option, PorosOptions):
10877
return poros_option.to_internal()

poros/third_party/gflags

Submodule gflags added at a738fdf

poros/third_party/googletest

Submodule googletest added at 7a7231c

poros/unittest/CMakeLists.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ set(GRAPHTEST "graph_test" )
1010
file(
1111
GLOB UT_FILES
1212
"./op_fuser/*.cpp"
13-
"../src/poros/lowering/fuse_*.cpp"
13+
"../poros/lowering/fuse_*.cpp"
1414
)
1515
list(APPEND UT_FILES
16-
"../src/poros/lowering/op_fuse_pass.cpp"
17-
"../src/poros/util/graph_test_helper.cpp")
16+
"../poros/lowering/op_fuse_pass.cpp"
17+
"../poros/util/graph_test_helper.cpp")
1818

1919
add_executable(${GRAPHTEST} ${UT_FILES})
2020
target_link_libraries(${GRAPHTEST} gtest_main)
@@ -28,8 +28,8 @@ set(UNITTEST "unit_test" )
2828

2929
file(
3030
GLOB UT_FILES
31-
"../src/poros/*/*.cpp"
32-
"../src/poros/converter/*/*.cpp"
31+
"../poros/*/*.cpp"
32+
"../poros/converter/*/*.cpp"
3333
"./converter/*.cpp"
3434
)
3535

0 commit comments

Comments
 (0)