Skip to content

Commit 150da5c

Browse files
author
Feiyu Chan
authored
fix conflicts and merge upstream (#49)
fix conflicts and merge upstream
1 parent cc9d3a0 commit 150da5c

File tree

18 files changed

+363
-77
lines changed

18 files changed

+363
-77
lines changed

paddle/fluid/framework/proto_desc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ constexpr int kNoneProcessMeshIndex = -1;
2727

2828
// If a attribute name has a certain suffix, it means that the
2929
// atrribute is a distributed-related attribute for auto parallel.
30-
// e.g., "mesh_id@PARALLEL".
31-
constexpr char kAutoParallelSuffix[] = "@PARALLEL";
30+
// e.g., "mesh_id@AUTO_PARALLEL".
31+
constexpr char kAutoParallelSuffix[] = "@AUTO_PARALLEL";
3232

3333
} // namespace framework
3434
} // namespace paddle

paddle/fluid/inference/capi_exp/pd_config.cc

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,48 @@ void PD_ConfigSetTrtDynamicShapeInfo(__pd_keep PD_Config* pd_config,
231231
optim_input_shapes, disable_trt_plugin_fp16);
232232
}
233233

234+
PD_Bool PD_ConfigTensorRtDynamicShapeEnabled(__pd_keep PD_Config* pd_config) {
235+
CHECK_AND_CONVERT_PD_CONFIG;
236+
return config->tensorrt_dynamic_shape_enabled();
237+
}
238+
239+
void PD_ConfigEnableTunedTensorRtDynamicShape(__pd_keep PD_Config* pd_config,
240+
const char* shape_range_info_path,
241+
PD_Bool allow_build_at_runtime) {
242+
CHECK_AND_CONVERT_PD_CONFIG;
243+
config->EnableTunedTensorRtDynamicShape(shape_range_info_path,
244+
allow_build_at_runtime);
245+
}
246+
247+
PD_Bool PD_ConfigTunedTensorRtDynamicShape(__pd_keep PD_Config* pd_config) {
248+
CHECK_AND_CONVERT_PD_CONFIG;
249+
return config->tuned_tensorrt_dynamic_shape();
250+
}
251+
252+
PD_Bool PD_ConfigTrtAllowBuildAtRuntime(__pd_keep PD_Config* pd_config) {
253+
CHECK_AND_CONVERT_PD_CONFIG;
254+
return config->trt_allow_build_at_runtime();
255+
}
256+
257+
void PD_ConfigCollectShapeRangeInfo(__pd_keep PD_Config* pd_config,
258+
const char* shape_range_info_path) {
259+
CHECK_AND_CONVERT_PD_CONFIG;
260+
config->CollectShapeRangeInfo(shape_range_info_path);
261+
}
262+
263+
const char* PD_ConfigShapeRangeInfoPath(__pd_keep PD_Config* pd_config) {
264+
CHECK_AND_CONVERT_PD_CONFIG;
265+
auto shape_str = config->shape_range_info_path();
266+
char* c = reinterpret_cast<char*>(malloc(shape_str.length() + 1));
267+
snprintf(c, shape_str.length() + 1, "%s", shape_str.c_str());
268+
return c;
269+
}
270+
271+
PD_Bool PD_ConfigShapeRangeInfoCollected(__pd_keep PD_Config* pd_config) {
272+
CHECK_AND_CONVERT_PD_CONFIG;
273+
return config->shape_range_info_collected();
274+
}
275+
234276
void PD_ConfigDisableTensorRtOPs(__pd_keep PD_Config* pd_config, size_t ops_num,
235277
const char** ops_name) {
236278
CHECK_AND_CONVERT_PD_CONFIG;
@@ -358,9 +400,9 @@ PD_Bool PD_ConfigModelFromMemory(__pd_keep PD_Config* pd_config) {
358400
CHECK_AND_CONVERT_PD_CONFIG;
359401
return config->model_from_memory();
360402
}
361-
void PD_ConfigEnableMemoryOptim(__pd_keep PD_Config* pd_config) {
403+
void PD_ConfigEnableMemoryOptim(__pd_keep PD_Config* pd_config, PD_Bool x) {
362404
CHECK_AND_CONVERT_PD_CONFIG;
363-
config->EnableMemoryOptim();
405+
config->EnableMemoryOptim(x);
364406
}
365407
PD_Bool PD_ConfigMemoryOptimEnabled(__pd_keep PD_Config* pd_config) {
366408
CHECK_AND_CONVERT_PD_CONFIG;

paddle/fluid/inference/capi_exp/pd_config.h

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,69 @@ PADDLE_CAPI_EXPORT extern void PD_ConfigSetTrtDynamicShapeInfo(
324324
size_t* shapes_num, int32_t** min_shape, int32_t** max_shape,
325325
int32_t** optim_shape, PD_Bool disable_trt_plugin_fp16);
326326
///
327+
/// \brief A boolean state telling whether the trt dynamic_shape is used.
328+
///
329+
/// \param[in] pd_onfig config
330+
///
331+
PADDLE_CAPI_EXPORT extern PD_Bool PD_ConfigTensorRtDynamicShapeEnabled(
332+
__pd_keep PD_Config* pd_config);
333+
///
334+
/// \brief Enable tuned tensorrt dynamic shape.
335+
///
336+
/// \param[in] pd_onfig config
337+
/// \param[in] shape_range_info_path the path to shape_info file got in
338+
/// CollectShapeInfo mode.
339+
/// \param[in] allow_build_at_runtime allow build trt engine at runtime.
340+
///
341+
PADDLE_CAPI_EXPORT extern void PD_ConfigEnableTunedTensorRtDynamicShape(
342+
__pd_keep PD_Config* pd_config, const char* shape_range_info_path,
343+
PD_Bool allow_build_at_runtime);
344+
345+
///
346+
/// \brief A boolean state telling whether to use tuned tensorrt dynamic
347+
/// shape.
348+
///
349+
/// \param[in] pd_onfig config
350+
///
351+
PADDLE_CAPI_EXPORT extern PD_Bool PD_ConfigTunedTensorRtDynamicShape(
352+
__pd_keep PD_Config* pd_config);
353+
354+
///
355+
/// \brief A boolean state telling whether to allow building trt engine at
356+
/// runtime.
357+
///
358+
/// \param[in] pd_onfig config
359+
///
360+
PADDLE_CAPI_EXPORT extern PD_Bool PD_ConfigTrtAllowBuildAtRuntime(
361+
__pd_keep PD_Config* pd_config);
362+
363+
///
364+
/// \brief Collect shape info of all tensors in compute graph.
365+
///
366+
/// \param[in] pd_onfig config
367+
/// \param[in] shape_range_info_path the path to save shape info.
368+
///
369+
PADDLE_CAPI_EXPORT extern void PD_ConfigCollectShapeRangeInfo(
370+
__pd_keep PD_Config* pd_config, const char* shape_range_info_path);
371+
372+
///
373+
/// \brief the shape info path in CollectShapeInfo mode.
374+
/// Attention, Please release the string manually.
375+
///
376+
/// \param[in] pd_onfig config
377+
///
378+
PADDLE_CAPI_EXPORT extern const char* PD_ConfigShapeRangeInfoPath(
379+
__pd_keep PD_Config* pd_config);
380+
381+
///
382+
/// \brief A boolean state telling whether to collect shape info.
383+
///
384+
/// \param[in] pd_onfig config
385+
///
386+
PADDLE_CAPI_EXPORT extern PD_Bool PD_ConfigShapeRangeInfoCollected(
387+
__pd_keep PD_Config* pd_config);
388+
389+
///
327390
/// \brief Prevent ops running in Paddle-TRT
328391
/// NOTE: just experimental, not an official stable API, easy to be broken.
329392
///
@@ -542,7 +605,7 @@ PADDLE_CAPI_EXPORT extern PD_Bool PD_ConfigModelFromMemory(
542605
/// \param[in] pd_onfig config
543606
///
544607
PADDLE_CAPI_EXPORT extern void PD_ConfigEnableMemoryOptim(
545-
__pd_keep PD_Config* pd_config);
608+
__pd_keep PD_Config* pd_config, PD_Bool x);
546609
///
547610
/// \brief A boolean state telling whether the memory optimization is
548611
/// activated.

paddle/fluid/inference/goapi/config.go

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,71 @@ func (config *Config) SetTRTDynamicShapeInfo(minInputShape map[string][]int32, m
383383
cvtGoBoolToPD(disableTrtPluginFp16))
384384
}
385385

386+
///
387+
/// \brief A boolean state telling whether the trt dynamic_shape is used.
388+
///
389+
func (config *Config) TensorRtDynamicShapeEnabled() bool {
390+
return cvtPDBoolToGo(C.PD_ConfigTensorRtDynamicShapeEnabled(config.c))
391+
}
392+
393+
///
394+
/// \brief Enable tuned tensorrt dynamic shape.
395+
///
396+
/// \param shapeRangeInfoPath the path to shape_info file got in
397+
/// CollectShapeInfo mode.
398+
/// \param allowBuildAtRuntime allow build trt engine at runtime.
399+
///
400+
func (config *Config) EnableTunedTensorRtDynamicShape(shapeRangeInfoPath string, allowBuildAtRuntime bool) {
401+
cstr := C.CString(shapeRangeInfoPath)
402+
C.PD_ConfigEnableTunedTensorRtDynamicShape(config.c, cstr, cvtGoBoolToPD(allowBuildAtRuntime))
403+
defer C.free(unsafe.Pointer(cstr))
404+
}
405+
406+
///
407+
/// \brief A boolean state telling whether to use tuned tensorrt dynamic
408+
/// shape.
409+
///
410+
func (config *Config) TunedTensorRtDynamicShape() bool {
411+
return cvtPDBoolToGo(C.PD_ConfigTunedTensorRtDynamicShape(config.c))
412+
}
413+
414+
///
415+
/// \brief A boolean state telling whether to allow building trt engine at
416+
/// runtime.
417+
///
418+
func (config *Config) TrtAllowBuildAtRuntime() bool {
419+
return cvtPDBoolToGo(C.PD_ConfigTrtAllowBuildAtRuntime(config.c))
420+
}
421+
422+
///
423+
/// \brief Collect shape info of all tensors in compute graph.
424+
///
425+
/// \param shapeRangeInfoPath the path to save shape info.
426+
///
427+
func (config *Config) CollectShapeRangeInfo(shapeRangeInfoPath string) {
428+
cstr := C.CString(shapeRangeInfoPath)
429+
C.PD_ConfigCollectShapeRangeInfo(config.c, cstr)
430+
defer C.free(unsafe.Pointer(cstr))
431+
}
432+
433+
///
434+
/// \brief the shape info path in CollectShapeInfo mode.
435+
/// Attention, Please release the string manually.
436+
///
437+
func (config *Config) ShapeRangeInfoPath() string {
438+
cstr := C.PD_ConfigShapeRangeInfoPath(config.c)
439+
str := C.GoString(cstr)
440+
C.free(unsafe.Pointer(cstr))
441+
return str
442+
}
443+
444+
///
445+
/// \brief A boolean state telling whether to collect shape info.
446+
///
447+
func (config *Config) ShapeRangeInfoCollected() bool {
448+
return cvtPDBoolToGo(C.PD_ConfigShapeRangeInfoCollected(config.c))
449+
}
450+
386451
///
387452
/// \brief Prevent ops running in Paddle-TRT
388453
/// NOTE: just experimental, not an official stable API, easy to be broken.
@@ -649,8 +714,8 @@ func (config *Config) ModelFromMemory() bool {
649714
/// \brief Turn on memory optimize
650715
/// NOTE still in development.
651716
///
652-
func (config *Config) EnableMemoryOptim() {
653-
C.PD_ConfigEnableMemoryOptim(config.c)
717+
func (config *Config) EnableMemoryOptim(x bool) {
718+
C.PD_ConfigEnableMemoryOptim(config.c, cvtGoBoolToPD(x))
654719
}
655720

656721
///

paddle/fluid/inference/goapi/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func TestNewConfig(t *testing.T) {
6969

7070
config.EnableMKLDNN()
7171

72-
config.EnableMemoryOptim()
72+
config.EnableMemoryOptim(true)
7373
t.Logf("MemoryOptimEnabled:%+v", config.MemoryOptimEnabled())
7474

7575
config.EnableProfile()

paddle/fluid/inference/goapi/predictor_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ package paddle
1717
import (
1818
"io/ioutil"
1919
"os"
20+
"runtime"
2021
"testing"
22+
"time"
2123
)
2224

2325
func TestNewPredictor(t *testing.T) {
@@ -106,6 +108,52 @@ func TestFromBuffer(t *testing.T) {
106108
t.Log(outData)
107109
}
108110

111+
func TestCollectShapeInfo(t *testing.T) {
112+
config := NewConfig()
113+
config.SetModel("./mobilenetv1/inference.pdmodel", "./mobilenetv1/inference.pdiparams")
114+
config.CollectShapeRangeInfo("shape_range_info.pbtxt")
115+
config.EnableUseGpu(100, 0)
116+
t.Logf("ShapeRangeInfoCollected:%+v", config.ShapeRangeInfoCollected())
117+
t.Logf("ShapeRangeInfoPath:%+v", config.ShapeRangeInfoPath())
118+
predictor := NewPredictor(config)
119+
inNames := predictor.GetInputNames()
120+
outNames := predictor.GetOutputNames()
121+
inHandle := predictor.GetInputHandle(inNames[0])
122+
inHandle.Reshape([]int32{1, 3, 224, 224})
123+
124+
data := make([]float32, numElements([]int32{1, 3, 224, 224}))
125+
for i := 0; i < int(numElements([]int32{1, 3, 224, 224})); i++ {
126+
data[i] = float32(i%255) * 0.1
127+
}
128+
inHandle.CopyFromCpu(data)
129+
130+
predictor.Run()
131+
132+
outHandle := predictor.GetOutputHandle(outNames[0])
133+
outShape := outHandle.Shape()
134+
outData := make([]float32, numElements(outShape))
135+
outHandle.CopyToCpu(outData)
136+
// Go is a GC language, so we must wait for gc to get shape_range_info.pbtxt
137+
predictor = nil
138+
runtime.GC()
139+
time.Sleep(2 * time.Second)
140+
141+
trt_config := NewConfig()
142+
trt_config.SetModel("./mobilenetv1/inference.pdmodel", "./mobilenetv1/inference.pdiparams")
143+
trt_config.EnableUseGpu(100, 0)
144+
trt_config.EnableTensorRtEngine(102400, 4, 3, PrecisionFloat32, false, false)
145+
trt_config.EnableTunedTensorRtDynamicShape("shape_range_info.pbtxt", true)
146+
trt_predictor := NewPredictor(trt_config)
147+
trt_inNames := trt_predictor.GetInputNames()
148+
trt_inHandle := trt_predictor.GetInputHandle(trt_inNames[0])
149+
trt_inHandle.Reshape([]int32{1, 3, 224, 224})
150+
151+
trt_inHandle.CopyFromCpu(data)
152+
153+
trt_predictor.Run()
154+
155+
}
156+
109157
func numElements(shape []int32) int32 {
110158
n := int32(1)
111159
for _, v := range shape {

paddle/fluid/inference/goapi/test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ fi
2424
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PWD/paddle_inference_c/third_party/install/mklml/lib/:$PWD/paddle_inference_c/third_party/install/mkldnn/lib/:$PWD/paddle_inference_c/paddle/lib/
2525

2626
# 3. go test
27+
go clean -testcache
2728
go test -v ./...

paddle/fluid/inference/tests/api/analyzer_capi_exp_gpu_tester.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ TEST(PD_Config, use_gpu) {
120120
FALSE, FALSE);
121121
bool trt_enable = PD_ConfigTensorRtEngineEnabled(config);
122122
EXPECT_TRUE(trt_enable);
123-
PD_ConfigEnableMemoryOptim(config);
123+
PD_ConfigEnableMemoryOptim(config, true);
124124
bool memory_optim_enable = PD_ConfigMemoryOptimEnabled(config);
125125
EXPECT_TRUE(memory_optim_enable);
126126
PD_ConfigEnableProfile(config);

paddle/fluid/inference/tests/api/analyzer_capi_exp_pd_config_tester.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ TEST(PD_Config, interface) {
8383
EXPECT_TRUE(mkldnn_bf16_enabled);
8484
#endif
8585

86-
PD_ConfigEnableMemoryOptim(config);
86+
PD_ConfigEnableMemoryOptim(config, true);
8787
bool memory_enabled = PD_ConfigMemoryOptimEnabled(config);
8888
EXPECT_TRUE(memory_enabled);
8989

paddle/fluid/inference/tests/api/analyzer_capi_exp_xpu_tester.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ TEST(PD_Config, use_xpu) {
4242
PD_ConfigSwitchIrOptim(config, TRUE);
4343
bool ir_optim = PD_IrOptim(config);
4444
EXPECT_TRUE(ir_optim);
45-
PD_ConfigEnableMemoryOptim(config);
45+
PD_ConfigEnableMemoryOptim(config, true);
4646
bool memory_optim_enable = PD_ConfigMemoryOptimEnabled(config);
4747
EXPECT_TRUE(memory_optim_enable);
4848
PD_ConfigEnableProfile(config);

0 commit comments

Comments
 (0)