未验证 提交 62712aec 编写于 作者: Y YuanRisheng 提交者: GitHub

[PHI Decoupling]Add PHI init for extension (#51511)

* remove init

* delete fluid in context pool

* fix custom op bugs

* fix profiler bugs

* fix ci bugs

* fix window compile bugs

* fix windows bugs

* fix window bugs
上级 802a81d0
......@@ -20,3 +20,7 @@ limitations under the License. */
#if !defined(PADDLE_ON_INFERENCE) && !defined(PADDLE_NO_PYTHON)
#include "paddle/utils/pybind.h"
#endif
// For initialization of DeviceContextPool and MemoryMethod
#include "paddle/fluid/platform/init_phi.h"
static paddle::InitPhi g_init_phi;
......@@ -43,6 +43,7 @@
*paddle::CustomOpKernelContext*;
*paddle::RegisterSymbolsFor*;
*paddle::from_blob*;
*paddle::InitPhi*;
/* ut needs the following symbol, we need to modify all the ut to hidden such symbols */
......
......@@ -140,7 +140,8 @@ nv_test(
nv_test(
test_custom_plugin_creater
SRCS test_custom_plugin_creater.cc
DEPS paddle_framework tensorrt_converter op_meta_info custom_operator)
DEPS paddle_framework tensorrt_converter op_meta_info custom_operator
init_phi)
if(WITH_ONNXRUNTIME AND WIN32)
# Copy onnxruntime for some c++ test in Windows, since the test will
......
......@@ -412,3 +412,13 @@ if(WITH_CUSTOM_DEVICE)
${DEVICE_EVENT_LIBS} device_event_custom_device
CACHE INTERNAL "device event libs")
endif()
cc_library(
init_phi
SRCS init_phi.cc
DEPS init)
cc_test(
init_phi_test
SRCS init_phi_test.cc
DEPS phi_tensor init_phi)
......@@ -472,6 +472,7 @@ void InitMemoryMethod() {
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
memory_method->gpu_memory_usage = paddle::platform::GpuMemoryUsage;
#endif
memory_method->init_devices = InitDevices;
memory_utils.Init(std::move(memory_method));
});
}
......
/* Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/fluid/platform/init_phi.h"
#include "glog/logging.h"
#include "paddle/fluid/platform/init.h"
REGISTER_FILE_SYMBOLS(init_phi)
namespace paddle {
InitPhi::InitPhi() {
paddle::framework::InitMemoryMethod();
LOG(INFO) << "Init MemoryMethod success.";
}
} // namespace paddle
/* Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
#include "paddle/phi/api/include/dll_decl.h"
namespace paddle {
class PADDLE_API InitPhi {
public:
InitPhi();
};
#define REGISTER_FILE_SYMBOLS(name) \
int RegisterSymbolsFor##name() { return 0; }
#define DECLARE_FILE_SYMBOLS(name) \
extern int RegisterSymbolsFor##name(); \
UNUSED static int use_file_##name = RegisterSymbolsFor##name()
} // namespace paddle
/* Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include <gtest/gtest.h>
#include "paddle/extension.h"
#include "paddle/phi/backends/context_pool.h"
#include "paddle/phi/common/memory_utils.h"
TEST(InitPhi, InitPhi) {
phi::MemoryUtils::Instance().CheckMemoryMethod();
phi::MemoryUtils::Instance().InitDevices();
ASSERT_EQ(phi::DeviceContextPool::IsInitialized(), true);
}
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
set(PYBIND_DEPS
init
init_phi
pybind
python
proto_desc
......
......@@ -88,6 +88,7 @@ limitations under the License. */
#include "paddle/fluid/platform/dynload/dynamic_loader.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/init.h"
#include "paddle/fluid/platform/init_phi.h"
#include "paddle/fluid/platform/monitor.h"
#include "paddle/fluid/platform/place.h"
#include "paddle/fluid/platform/profiler.h"
......@@ -216,6 +217,7 @@ PYBIND11_MAKE_OPAQUE(paddle::framework::FetchUnmergedList);
PYBIND11_MAKE_OPAQUE(paddle::framework::FetchList);
PYBIND11_MAKE_OPAQUE(paddle::framework::FetchType);
DECLARE_FILE_SYMBOLS(init_phi);
namespace paddle {
namespace pybind {
......
......@@ -15,6 +15,7 @@ limitations under the License. */
#include "paddle/phi/api/include/context_pool.h"
#include "paddle/phi/backends/context_pool.h"
#include "paddle/phi/common/memory_utils.h"
#include "paddle/phi/core/allocator.h"
#include "paddle/phi/core/enforce.h"
......@@ -22,8 +23,6 @@ limitations under the License. */
#include "paddle/phi/core/cuda_stream.h"
#endif
#include "paddle/fluid/platform/init.h"
namespace paddle {
namespace experimental {
......@@ -36,7 +35,7 @@ const phi::DeviceContext* DeviceContextPool::Get(const Place& place) {
auto it = context_map_.find(place);
if (it == context_map_.end()) {
if (!phi::DeviceContextPool::IsInitialized()) {
paddle::framework::InitDevices();
phi::memory_utils::InitDevices();
}
// only when we need the specific DeviceContext, get and cache it
auto* dev_ctx = phi::DeviceContextPool::Instance().Get(place);
......
......@@ -75,6 +75,8 @@ void GpuMemoryUsage(size_t* available, size_t* total) {
}
#endif
void InitDevices() { MemoryUtils::Instance().InitDevices(); }
} // namespace memory_utils
} // namespace phi
......@@ -123,6 +123,11 @@ struct MemoryInterface {
*/
void (*gpu_memory_usage)(size_t* available, size_t* total);
#endif
/**
* @brief init devices info and device context
*/
void (*init_devices)();
};
class MemoryUtils {
......@@ -256,6 +261,16 @@ class MemoryUtils {
}
#endif
void InitDevices() {
CheckMemoryMethod();
PADDLE_ENFORCE_NE(
memory_method_->init_devices,
nullptr,
phi::errors::Unavailable("init_devices method in memory_method_ is not "
"initiazed yet. You need init it first."));
memory_method_->init_devices();
}
void CheckMemoryMethod() {
PADDLE_ENFORCE_NE(
memory_method_.get(),
......@@ -317,6 +332,8 @@ int64_t DeviceMemoryStatCurrentValue(const std::string& stat_type, int dev_id);
void GpuMemoryUsage(size_t* available, size_t* total);
#endif
void InitDevices();
} // namespace memory_utils
} // namespace phi
......@@ -17,12 +17,10 @@
#include <random>
#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/common/memory_utils.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/funcs/distribution_helper.h"
// See Note [ Why still include the fluid headers? ]
#include "paddle/phi/common/memory_utils.h"
namespace phi {
template <typename T, typename Context>
......
......@@ -19,13 +19,11 @@
#include "paddle/phi/common/bfloat16.h"
#include "paddle/phi/common/complex.h"
#include "paddle/phi/common/float16.h"
#include "paddle/phi/common/memory_utils.h"
#include "paddle/phi/common/scalar.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/core/visit_type.h"
// See Note [ Why still include the fluid headers? ]
#include "paddle/phi/common/memory_utils.h"
namespace phi {
template <typename T, typename Context>
......
......@@ -714,10 +714,12 @@ headers = (
list(find_files('*.h', '@PADDLE_SOURCE_DIR@/paddle/phi/kernels/primitive')) + # phi kernel primitive api headers
# capi headers
list(find_files('*.h', '@PADDLE_SOURCE_DIR@/paddle/phi/capi', recursive=True)) + # phi capi headers
# profiler headers
list(find_files('trace_event.h', '@PADDLE_SOURCE_DIR@/paddle/fluid/platform/profiler')) + # phi profiler headers
# phi profiler headers
list(find_files('*.h', '@PADDLE_SOURCE_DIR@/paddle/phi/api/profiler')) +
# utils api headers
list(find_files('*.h', '@PADDLE_SOURCE_DIR@/paddle/utils', recursive=True))) # paddle utils headers
list(find_files('*.h', '@PADDLE_SOURCE_DIR@/paddle/utils', recursive=True)) + # paddle utils headers
# init headers
list(find_files('init_phi.h', '@PADDLE_SOURCE_DIR@/paddle/fluid/platform'))) # phi init headers
jit_layer_headers = ['layer.h', 'serializer.h', 'serializer_utils.h', 'all.h', 'function.h']
for f in jit_layer_headers:
......@@ -775,9 +777,6 @@ class InstallHeaders(Command):
if 'fluid/jit' in install_dir:
install_dir = re.sub('fluid/jit', 'jit', install_dir)
print('fluid/jit install_dir: ', install_dir)
if 'trace_event.h' in install_dir:
install_dir = re.sub('fluid/platform/profiler', 'phi/backends/custom', install_dir)
print('trace_event.h install_dir: ', install_dir)
else:
# third_party
install_dir = re.sub('${THIRD_PARTY_PATH}', 'third_party', header)
......
......@@ -141,13 +141,6 @@ def get_header_install_dir(header):
if 'fluid/jit' in install_dir:
install_dir = re.sub('fluid/jit', 'jit', install_dir)
print('fluid/jit install_dir: ', install_dir)
if 'trace_event.h' in install_dir:
install_dir = re.sub(
'fluid/platform/profiler',
'phi/backends/custom',
install_dir,
)
print('trace_event.h install_dir: ', install_dir)
else:
# third_party
install_dir = re.sub(
......@@ -1217,23 +1210,28 @@ def get_headers():
'*.h', paddle_source_dir + '/paddle/phi/kernels/primitive'
)
)
+ list( # phi kernel primitive api headers
# capi headers
+ list( # capi headers
find_files(
'*.h', paddle_source_dir + '/paddle/phi/capi', recursive=True
)
)
+ list( # phi capi headers
# profiler headers
+ list( # utils api headers
find_files(
'trace_event.h',
paddle_source_dir + '/paddle/fluid/platform/profiler',
'*.h', paddle_source_dir + '/paddle/utils', recursive=True
)
)
+ list( # phi profiler headers
# utils api headers
find_files(
'*.h', paddle_source_dir + '/paddle/utils', recursive=True
'*.h',
paddle_source_dir + '/paddle/phi/api/profiler',
recursive=True,
)
)
+ list( # phi init headers
find_files(
'init_phi.h',
paddle_source_dir + '/paddle/fluid/platform',
recursive=True,
)
)
) # paddle utils headers
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册