|
| 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 |
0 commit comments