Skip to content

Commit b92ad06

Browse files
【custom】add loadCustomEnginelib method (#70366)
* modify assign inplace * delete floor_divide decomp * delete floor_divide decomp * modify prim test * modify prim test * add chunk_id guard * add engine decomp * modify flag * tmp customengine * move loadcustomlib and add base func for loadcustomenginelib * add loadcustomEnginelib test * add test * rollback noneed test * rollback * modify declare * modify declare * add cmake cond * windows bug * reply comment
1 parent c2ee0b0 commit b92ad06

15 files changed

+448
-39
lines changed

paddle/fluid/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
add_subdirectory(custom_engine)
12
add_subdirectory(platform)
23
add_subdirectory(distributed)
34
add_subdirectory(framework)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
if(WITH_CUSTOM_DEVICE AND NOT WIN32)
2+
cc_library(
3+
custom_device_load SRCS custom_device_load.cc custom_engine_load.cc
4+
custom_engine_manager.cc)
5+
endif()
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <glog/logging.h>
16+
17+
#
18+
#include "paddle/fluid/custom_engine/custom_device_load.h"
19+
namespace paddle {
20+
21+
typedef bool (*RegisterDevicePluginFn)(CustomRuntimeParams* runtime_params);
22+
23+
typedef bool (*RegisterDevicePluginEngineFn)(CustomEngineParams* engine_params);
24+
25+
void LoadCustomLib(const std::string& dso_lib_path, void* dso_handle) {
26+
CustomRuntimeParams runtime_params;
27+
std::memset(&runtime_params, 0, sizeof(CustomRuntimeParams));
28+
runtime_params.size = sizeof(CustomRuntimeParams);
29+
auto device_interface = std::make_unique<C_DeviceInterface>();
30+
runtime_params.interface = device_interface.get();
31+
std::memset(runtime_params.interface, 0, sizeof(C_DeviceInterface));
32+
runtime_params.interface->size = sizeof(C_DeviceInterface);
33+
34+
RegisterDevicePluginFn init_plugin_fn =
35+
reinterpret_cast<RegisterDevicePluginFn>(dlsym(dso_handle, "InitPlugin"));
36+
37+
if (init_plugin_fn == nullptr) {
38+
LOG(WARNING) << "Skipped lib [" << dso_lib_path << "]: fail to find "
39+
<< "InitPlugin symbol in this lib.";
40+
return;
41+
}
42+
43+
init_plugin_fn(&runtime_params);
44+
if (runtime_params.device_type == nullptr) {
45+
LOG(WARNING) << "Skipped lib [" << dso_lib_path
46+
<< "]: InitPlugin failed, please check the version "
47+
"compatibility between PaddlePaddle and Custom Runtime.";
48+
return;
49+
}
50+
phi::LoadCustomRuntimeLib(
51+
runtime_params, std::move(device_interface), dso_lib_path, dso_handle);
52+
LOG(INFO) << "Succeed in loading custom runtime in lib: " << dso_lib_path;
53+
54+
RegisterDevicePluginEngineFn init_plugin_engine_fn =
55+
reinterpret_cast<RegisterDevicePluginEngineFn>(
56+
dlsym(dso_handle, "InitPluginCustomEngine"));
57+
58+
if (init_plugin_engine_fn == nullptr) {
59+
LOG(INFO) << "Skipped lib [" << dso_lib_path << "]: no custom engine "
60+
<< "Plugin symbol in this lib.";
61+
} else {
62+
CustomEngineParams engine_params;
63+
std::memset(&engine_params, 0, sizeof(CustomEngineParams));
64+
engine_params.size = sizeof(CustomEngineParams);
65+
66+
auto engine_interface = new (C_CustomEngineInterface);
67+
engine_params.interface = engine_interface;
68+
69+
std::memset(engine_params.interface, 0, sizeof(C_CustomEngineInterface));
70+
engine_params.interface->size = sizeof(C_CustomEngineInterface);
71+
72+
init_plugin_engine_fn(&engine_params);
73+
if (engine_params.interface == nullptr) {
74+
LOG(WARNING) << "Skipped lib [" << dso_lib_path
75+
<< "]: InitPluginEngine failed, please check the version "
76+
"compatibility between PaddlePaddle and Custom Runtime.";
77+
} else {
78+
paddle::custom_engine::LoadCustomEngineLib(dso_lib_path, &engine_params);
79+
LOG(INFO) << "Succeed in loading custom engine in lib: " << dso_lib_path;
80+
}
81+
}
82+
}
83+
} // namespace paddle
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
#include "paddle/phi/backends/device_ext.h"
17+
#include "paddle/phi/backends/device_manager.h"
18+
19+
#include "paddle/fluid/custom_engine/custom_engine_ext.h"
20+
#include "paddle/fluid/custom_engine/custom_engine_manager.h"
21+
namespace paddle {
22+
23+
void LoadCustomLib(const std::string& dso_lib_path, void* dso_handle);
24+
} // namespace paddle
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
#if !defined(_WIN32)
17+
#include <cstddef>
18+
#include <cstring>
19+
20+
#include "paddle/phi/backends/device_ext.h"
21+
22+
#ifdef __cplusplus
23+
extern "C" {
24+
#endif
25+
26+
typedef struct C_Operation_st* C_Operation;
27+
28+
struct C_CustomEngineInterface {
29+
size_t size;
30+
C_Status (*graph_engine_build)();
31+
C_Status (*graph_engine_execute)();
32+
C_Status (*custom_engine_op_lower)();
33+
};
34+
35+
struct CustomEngineParams {
36+
size_t size;
37+
C_CustomEngineInterface* interface;
38+
};
39+
40+
// Plugin implement it and fill CustomEngineParams
41+
void InitPluginCustomEngine(CustomEngineParams*);
42+
43+
#ifdef __cplusplus
44+
} // extern "C"
45+
#endif
46+
#endif
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
#include <glog/logging.h>
15+
16+
#include "paddle/fluid/custom_engine/custom_engine_manager.h"
17+
#include "paddle/fluid/pir/dialect/operator/ir/op_dialect.h"
18+
#include "paddle/pir/include/core/ir_context.h"
19+
20+
namespace paddle {
21+
namespace custom_engine {
22+
bool ValidCustomCustomEngineParams(const CustomEngineParams* params) {
23+
#define CHECK_INTERFACE(ptr, required) \
24+
if (params->interface->ptr == nullptr && required) { \
25+
LOG(WARNING) << "CustomEngine pointer: " << #ptr << " is not set."; \
26+
return false; \
27+
}
28+
29+
CHECK_INTERFACE(graph_engine_build, true);
30+
CHECK_INTERFACE(graph_engine_execute, true);
31+
CHECK_INTERFACE(custom_engine_op_lower, true);
32+
33+
return true;
34+
#undef CHECK_INTERFACE
35+
}
36+
37+
void LoadCustomEngineLib(const std::string& dso_lib_path,
38+
CustomEngineParams* engine_params) {
39+
if (ValidCustomCustomEngineParams(engine_params)) {
40+
paddle::custom_engine::CustomEngineManager::Instance()
41+
->SetCustomEngineInterface(engine_params->interface);
42+
43+
} else {
44+
LOG(WARNING) << "Skipped lib [" << dso_lib_path
45+
<< "]. Wrong engine parameters!!! please check the version "
46+
"compatibility between PaddlePaddle and Custom Engine.";
47+
}
48+
}
49+
50+
} // namespace custom_engine
51+
} // namespace paddle
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "paddle/fluid/custom_engine/custom_engine_manager.h"
16+
17+
namespace paddle {
18+
namespace custom_engine {
19+
20+
CustomEngineManager* CustomEngineManager::Instance() {
21+
static CustomEngineManager manager;
22+
return &manager;
23+
}
24+
25+
CustomEngineManager::CustomEngineManager() : interface_(nullptr) {}
26+
27+
CustomEngineManager::~CustomEngineManager() {
28+
if (!interface_) {
29+
delete interface_;
30+
}
31+
}
32+
C_CustomEngineInterface* CustomEngineManager::GetCustomEngineInterface() {
33+
return interface_;
34+
}
35+
36+
void CustomEngineManager::SetCustomEngineInterface(
37+
C_CustomEngineInterface* custom_engine_interface) {
38+
if (custom_engine_interface) {
39+
interface_ = custom_engine_interface;
40+
}
41+
}
42+
43+
} // namespace custom_engine
44+
} // namespace paddle
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
#include <cstdint>
17+
#include <string>
18+
19+
#include "paddle/fluid/custom_engine/custom_engine_ext.h"
20+
21+
namespace paddle {
22+
namespace custom_engine {
23+
24+
class CustomEngineManager {
25+
public:
26+
static CustomEngineManager* Instance();
27+
28+
CustomEngineManager();
29+
~CustomEngineManager();
30+
31+
C_CustomEngineInterface* GetCustomEngineInterface();
32+
33+
void SetCustomEngineInterface(
34+
C_CustomEngineInterface* custom_engine_interface);
35+
36+
private:
37+
C_CustomEngineInterface* interface_;
38+
};
39+
40+
void LoadCustomEngineLib(const std::string& dso_lib_path,
41+
CustomEngineParams* engine_params);
42+
43+
} // namespace custom_engine
44+
} // namespace paddle
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
#include "paddle/fluid/custom_engine/custom_engine_ext.h"
17+
18+
C_Status GraphEngineBuild() { return C_SUCCESS; }
19+
C_Status GraphEngineExecute() { return C_SUCCESS; }
20+
C_Status CustomEngineOpLower() { return C_SUCCESS; }
21+
22+
void InitFakeCPUEngine(CustomEngineParams *params) {
23+
memset(reinterpret_cast<void *>(params->interface),
24+
0,
25+
sizeof(C_CustomEngineInterface));
26+
27+
params->interface->graph_engine_build = GraphEngineBuild;
28+
params->interface->graph_engine_execute = GraphEngineExecute;
29+
params->interface->custom_engine_op_lower = CustomEngineOpLower;
30+
}

paddle/fluid/platform/CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@ if(WITH_GLOO)
1414
endif()
1515

1616
# separate init from device_context to avoid cycle dependencies
17+
if(WITH_CUSTOM_DEVICE AND NOT WIN32)
18+
set(INIT_DEPS phi common custom_device_load)
19+
else()
20+
set(INIT_DEPS phi common)
21+
endif()
22+
1723
cc_library(
1824
init
1925
SRCS init.cc
20-
DEPS phi common)
26+
DEPS ${INIT_DEPS})
2127

2228
#fluid_memory depends on device_context, here add deps individually for
2329
# avoiding cycle dependencies

0 commit comments

Comments
 (0)