Skip to content

Commit 62712ae

Browse files
authored
[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
1 parent 802a81d commit 62712ae

17 files changed

+148
-31
lines changed

paddle/extension.h

+4
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@ limitations under the License. */
2020
#if !defined(PADDLE_ON_INFERENCE) && !defined(PADDLE_NO_PYTHON)
2121
#include "paddle/utils/pybind.h"
2222
#endif
23+
// For initialization of DeviceContextPool and MemoryMethod
24+
#include "paddle/fluid/platform/init_phi.h"
25+
26+
static paddle::InitPhi g_init_phi;

paddle/fluid/inference/paddle_inference.map

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
*paddle::CustomOpKernelContext*;
4444
*paddle::RegisterSymbolsFor*;
4545
*paddle::from_blob*;
46+
*paddle::InitPhi*;
4647

4748
/* ut needs the following symbol, we need to modify all the ut to hidden such symbols */
4849

paddle/fluid/inference/tensorrt/convert/CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ nv_test(
140140
nv_test(
141141
test_custom_plugin_creater
142142
SRCS test_custom_plugin_creater.cc
143-
DEPS paddle_framework tensorrt_converter op_meta_info custom_operator)
143+
DEPS paddle_framework tensorrt_converter op_meta_info custom_operator
144+
init_phi)
144145

145146
if(WITH_ONNXRUNTIME AND WIN32)
146147
# Copy onnxruntime for some c++ test in Windows, since the test will

paddle/fluid/platform/CMakeLists.txt

+10
Original file line numberDiff line numberDiff line change
@@ -412,3 +412,13 @@ if(WITH_CUSTOM_DEVICE)
412412
${DEVICE_EVENT_LIBS} device_event_custom_device
413413
CACHE INTERNAL "device event libs")
414414
endif()
415+
416+
cc_library(
417+
init_phi
418+
SRCS init_phi.cc
419+
DEPS init)
420+
421+
cc_test(
422+
init_phi_test
423+
SRCS init_phi_test.cc
424+
DEPS phi_tensor init_phi)

paddle/fluid/platform/init.cc

+1
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ void InitMemoryMethod() {
472472
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
473473
memory_method->gpu_memory_usage = paddle::platform::GpuMemoryUsage;
474474
#endif
475+
memory_method->init_devices = InitDevices;
475476
memory_utils.Init(std::move(memory_method));
476477
});
477478
}

paddle/fluid/platform/init_phi.cc

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* Copyright (c) 2023 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 "paddle/fluid/platform/init_phi.h"
15+
#include "glog/logging.h"
16+
#include "paddle/fluid/platform/init.h"
17+
18+
REGISTER_FILE_SYMBOLS(init_phi)
19+
20+
namespace paddle {
21+
22+
InitPhi::InitPhi() {
23+
paddle::framework::InitMemoryMethod();
24+
LOG(INFO) << "Init MemoryMethod success.";
25+
}
26+
27+
} // namespace paddle

paddle/fluid/platform/init_phi.h

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* Copyright (c) 2023 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+
#pragma once
15+
#include "paddle/phi/api/include/dll_decl.h"
16+
namespace paddle {
17+
18+
class PADDLE_API InitPhi {
19+
public:
20+
InitPhi();
21+
};
22+
23+
#define REGISTER_FILE_SYMBOLS(name) \
24+
int RegisterSymbolsFor##name() { return 0; }
25+
26+
#define DECLARE_FILE_SYMBOLS(name) \
27+
extern int RegisterSymbolsFor##name(); \
28+
UNUSED static int use_file_##name = RegisterSymbolsFor##name()
29+
30+
} // namespace paddle
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* Copyright (c) 2023 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 <gtest/gtest.h>
15+
16+
#include "paddle/extension.h"
17+
#include "paddle/phi/backends/context_pool.h"
18+
#include "paddle/phi/common/memory_utils.h"
19+
20+
TEST(InitPhi, InitPhi) {
21+
phi::MemoryUtils::Instance().CheckMemoryMethod();
22+
phi::MemoryUtils::Instance().InitDevices();
23+
ASSERT_EQ(phi::DeviceContextPool::IsInitialized(), true);
24+
}
25+
26+
int main(int argc, char** argv) {
27+
::testing::InitGoogleTest(&argc, argv);
28+
return RUN_ALL_TESTS();
29+
}

paddle/fluid/pybind/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
set(PYBIND_DEPS
22
init
3+
init_phi
34
pybind
45
python
56
proto_desc

paddle/fluid/pybind/pybind.cc

+2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ limitations under the License. */
8888
#include "paddle/fluid/platform/dynload/dynamic_loader.h"
8989
#include "paddle/fluid/platform/enforce.h"
9090
#include "paddle/fluid/platform/init.h"
91+
#include "paddle/fluid/platform/init_phi.h"
9192
#include "paddle/fluid/platform/monitor.h"
9293
#include "paddle/fluid/platform/place.h"
9394
#include "paddle/fluid/platform/profiler.h"
@@ -216,6 +217,7 @@ PYBIND11_MAKE_OPAQUE(paddle::framework::FetchUnmergedList);
216217
PYBIND11_MAKE_OPAQUE(paddle::framework::FetchList);
217218
PYBIND11_MAKE_OPAQUE(paddle::framework::FetchType);
218219

220+
DECLARE_FILE_SYMBOLS(init_phi);
219221
namespace paddle {
220222
namespace pybind {
221223

paddle/phi/api/lib/context_pool.cc

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@ limitations under the License. */
1515
#include "paddle/phi/api/include/context_pool.h"
1616

1717
#include "paddle/phi/backends/context_pool.h"
18+
#include "paddle/phi/common/memory_utils.h"
1819
#include "paddle/phi/core/allocator.h"
1920
#include "paddle/phi/core/enforce.h"
2021

2122
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
2223
#include "paddle/phi/core/cuda_stream.h"
2324
#endif
2425

25-
#include "paddle/fluid/platform/init.h"
26-
2726
namespace paddle {
2827
namespace experimental {
2928

@@ -36,7 +35,7 @@ const phi::DeviceContext* DeviceContextPool::Get(const Place& place) {
3635
auto it = context_map_.find(place);
3736
if (it == context_map_.end()) {
3837
if (!phi::DeviceContextPool::IsInitialized()) {
39-
paddle::framework::InitDevices();
38+
phi::memory_utils::InitDevices();
4039
}
4140
// only when we need the specific DeviceContext, get and cache it
4241
auto* dev_ctx = phi::DeviceContextPool::Instance().Get(place);

paddle/phi/common/memory_utils.cc

+2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ void GpuMemoryUsage(size_t* available, size_t* total) {
7575
}
7676
#endif
7777

78+
void InitDevices() { MemoryUtils::Instance().InitDevices(); }
79+
7880
} // namespace memory_utils
7981

8082
} // namespace phi

paddle/phi/common/memory_utils.h

+17
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ struct MemoryInterface {
123123
*/
124124
void (*gpu_memory_usage)(size_t* available, size_t* total);
125125
#endif
126+
127+
/**
128+
* @brief init devices info and device context
129+
*/
130+
void (*init_devices)();
126131
};
127132

128133
class MemoryUtils {
@@ -256,6 +261,16 @@ class MemoryUtils {
256261
}
257262
#endif
258263

264+
void InitDevices() {
265+
CheckMemoryMethod();
266+
PADDLE_ENFORCE_NE(
267+
memory_method_->init_devices,
268+
nullptr,
269+
phi::errors::Unavailable("init_devices method in memory_method_ is not "
270+
"initiazed yet. You need init it first."));
271+
memory_method_->init_devices();
272+
}
273+
259274
void CheckMemoryMethod() {
260275
PADDLE_ENFORCE_NE(
261276
memory_method_.get(),
@@ -317,6 +332,8 @@ int64_t DeviceMemoryStatCurrentValue(const std::string& stat_type, int dev_id);
317332
void GpuMemoryUsage(size_t* available, size_t* total);
318333
#endif
319334

335+
void InitDevices();
336+
320337
} // namespace memory_utils
321338

322339
} // namespace phi

paddle/phi/kernels/gpu/randint_kernel.cu

+1-3
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@
1717
#include <random>
1818

1919
#include "paddle/phi/backends/gpu/gpu_context.h"
20+
#include "paddle/phi/common/memory_utils.h"
2021
#include "paddle/phi/core/kernel_registry.h"
2122
#include "paddle/phi/kernels/funcs/distribution_helper.h"
2223

23-
// See Note [ Why still include the fluid headers? ]
24-
#include "paddle/phi/common/memory_utils.h"
25-
2624
namespace phi {
2725

2826
template <typename T, typename Context>

paddle/phi/kernels/xpu/full_kernel.cc

+1-3
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@
1919
#include "paddle/phi/common/bfloat16.h"
2020
#include "paddle/phi/common/complex.h"
2121
#include "paddle/phi/common/float16.h"
22+
#include "paddle/phi/common/memory_utils.h"
2223
#include "paddle/phi/common/scalar.h"
2324
#include "paddle/phi/core/kernel_registry.h"
2425
#include "paddle/phi/core/visit_type.h"
2526

26-
// See Note [ Why still include the fluid headers? ]
27-
#include "paddle/phi/common/memory_utils.h"
28-
2927
namespace phi {
3028

3129
template <typename T, typename Context>

python/setup.py.in

+5-6
Original file line numberDiff line numberDiff line change
@@ -714,10 +714,12 @@ headers = (
714714
list(find_files('*.h', '@PADDLE_SOURCE_DIR@/paddle/phi/kernels/primitive')) + # phi kernel primitive api headers
715715
# capi headers
716716
list(find_files('*.h', '@PADDLE_SOURCE_DIR@/paddle/phi/capi', recursive=True)) + # phi capi headers
717-
# profiler headers
718-
list(find_files('trace_event.h', '@PADDLE_SOURCE_DIR@/paddle/fluid/platform/profiler')) + # phi profiler headers
717+
# phi profiler headers
718+
list(find_files('*.h', '@PADDLE_SOURCE_DIR@/paddle/phi/api/profiler')) +
719719
# utils api headers
720-
list(find_files('*.h', '@PADDLE_SOURCE_DIR@/paddle/utils', recursive=True))) # paddle utils headers
720+
list(find_files('*.h', '@PADDLE_SOURCE_DIR@/paddle/utils', recursive=True)) + # paddle utils headers
721+
# init headers
722+
list(find_files('init_phi.h', '@PADDLE_SOURCE_DIR@/paddle/fluid/platform'))) # phi init headers
721723

722724
jit_layer_headers = ['layer.h', 'serializer.h', 'serializer_utils.h', 'all.h', 'function.h']
723725
for f in jit_layer_headers:
@@ -775,9 +777,6 @@ class InstallHeaders(Command):
775777
if 'fluid/jit' in install_dir:
776778
install_dir = re.sub('fluid/jit', 'jit', install_dir)
777779
print('fluid/jit install_dir: ', install_dir)
778-
if 'trace_event.h' in install_dir:
779-
install_dir = re.sub('fluid/platform/profiler', 'phi/backends/custom', install_dir)
780-
print('trace_event.h install_dir: ', install_dir)
781780
else:
782781
# third_party
783782
install_dir = re.sub('${THIRD_PARTY_PATH}', 'third_party', header)

setup.py

+13-15
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,6 @@ def get_header_install_dir(header):
141141
if 'fluid/jit' in install_dir:
142142
install_dir = re.sub('fluid/jit', 'jit', install_dir)
143143
print('fluid/jit install_dir: ', install_dir)
144-
if 'trace_event.h' in install_dir:
145-
install_dir = re.sub(
146-
'fluid/platform/profiler',
147-
'phi/backends/custom',
148-
install_dir,
149-
)
150-
print('trace_event.h install_dir: ', install_dir)
151144
else:
152145
# third_party
153146
install_dir = re.sub(
@@ -1217,23 +1210,28 @@ def get_headers():
12171210
'*.h', paddle_source_dir + '/paddle/phi/kernels/primitive'
12181211
)
12191212
)
1220-
+ list( # phi kernel primitive api headers
1221-
# capi headers
1213+
+ list( # capi headers
12221214
find_files(
12231215
'*.h', paddle_source_dir + '/paddle/phi/capi', recursive=True
12241216
)
12251217
)
1226-
+ list( # phi capi headers
1227-
# profiler headers
1218+
+ list( # utils api headers
12281219
find_files(
1229-
'trace_event.h',
1230-
paddle_source_dir + '/paddle/fluid/platform/profiler',
1220+
'*.h', paddle_source_dir + '/paddle/utils', recursive=True
12311221
)
12321222
)
12331223
+ list( # phi profiler headers
1234-
# utils api headers
12351224
find_files(
1236-
'*.h', paddle_source_dir + '/paddle/utils', recursive=True
1225+
'*.h',
1226+
paddle_source_dir + '/paddle/phi/api/profiler',
1227+
recursive=True,
1228+
)
1229+
)
1230+
+ list( # phi init headers
1231+
find_files(
1232+
'init_phi.h',
1233+
paddle_source_dir + '/paddle/fluid/platform',
1234+
recursive=True,
12371235
)
12381236
)
12391237
) # paddle utils headers

0 commit comments

Comments
 (0)