Skip to content
2 changes: 1 addition & 1 deletion paddle/fluid/pybind/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ set(PYBIND_DEPS init pybind python proto_desc memory executor fleet_wrapper box_
feed_fetch_method pass generate_pass pass_builder parallel_executor profiler layer tracer engine scope_pool
analysis_predictor imperative_profiler imperative_flag save_load_util dlpack_tensor device_context
gloo_wrapper infer_io_utils heter_wrapper generator op_version_registry ps_gpu_wrapper custom_operator
cost_model cuda_graph_with_memory_pool fleet_executor global_utils phi_utils tcp_store)
cost_model cuda_graph_with_memory_pool fleet_executor global_utils phi_utils tcp_store new_profiler)

if (WITH_PSCORE)
set(PYBIND_DEPS ${PYBIND_DEPS} ps_service)
Expand Down
82 changes: 82 additions & 0 deletions paddle/fluid/pybind/pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ limitations under the License. */
#include "paddle/fluid/platform/monitor.h"
#include "paddle/fluid/platform/place.h"
#include "paddle/fluid/platform/profiler.h"
#include "paddle/fluid/platform/profiler/event_python.h"
#include "paddle/fluid/platform/profiler/event_tracing.h"
#include "paddle/fluid/platform/profiler/profiler.h"
#include "paddle/fluid/pybind/cuda_streams_py.h"
#include "paddle/fluid/pybind/distributed_py.h"
#include "paddle/fluid/pybind/eager.h"
Expand Down Expand Up @@ -2913,6 +2916,85 @@ All parameter, weight, gradient are variables in Paddle.
});

m.def("size_of_dtype", framework::SizeOfType);
py::class_<paddle::platform::ProfilerResult>(m, "_ProfilerResult")
.def(py::init<>())
.def("get_data", &paddle::platform::ProfilerResult::GetData,
py::return_value_policy::automatic_reference)
.def("save", &paddle::platform::ProfilerResult::Save)
.def("get_extra_info", &paddle::platform::ProfilerResult::GetExtraInfo);

py::class_<paddle::platform::DevicePythonNode>(m, "DevicePythonNode")
.def(py::init<>())
.def_readwrite("name", &paddle::platform::DevicePythonNode::name)
.def_readwrite("type", &paddle::platform::DevicePythonNode::type)
.def_readwrite("start_ns", &paddle::platform::DevicePythonNode::start_ns)
.def_readwrite("end_ns", &paddle::platform::DevicePythonNode::end_ns)
.def_readwrite("device_id",
&paddle::platform::DevicePythonNode::device_id)
.def_readwrite("context_id",
&paddle::platform::DevicePythonNode::context_id)
.def_readwrite("stream_id",
&paddle::platform::DevicePythonNode::stream_id);

py::class_<paddle::platform::HostPythonNode>(m, "HostPythonNode")
.def(py::init<>())
.def_readwrite("name", &paddle::platform::HostPythonNode::name)
.def_readwrite("type", &paddle::platform::HostPythonNode::type)
.def_readwrite("start_ns", &paddle::platform::HostPythonNode::start_ns)
.def_readwrite("end_ns", &paddle::platform::HostPythonNode::end_ns)
.def_readwrite("process_id",
&paddle::platform::HostPythonNode::process_id)
.def_readwrite("thread_id", &paddle::platform::HostPythonNode::thread_id)
.def_readwrite("children_node",
&paddle::platform::HostPythonNode::children_node_ptrs)
.def_readwrite("runtime_node",
&paddle::platform::HostPythonNode::runtime_node_ptrs)
.def_readwrite("device_node",
&paddle::platform::HostPythonNode::device_node_ptrs);

py::class_<paddle::platform::Profiler>(m, "_Profiler")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这块的Profiler相关调用函数首字母大写,这块是有调研过竞品,都是大写?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改为小写

.def("Create", &paddle::platform::Profiler::Create)
.def("Prepare",
[](paddle::platform::Profiler *profiler) {
platform::EnableHostEventRecorder();
profiler->Prepare();
})
.def("Start", &paddle::platform::Profiler::Start)
.def("Stop", &paddle::platform::Profiler::Stop,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stop时disable HostEventRecorder

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已添加

py::return_value_policy::automatic_reference);

py::class_<paddle::platform::ProfilerOptions>(m, "ProfilerOptions")
.def(py::init<>())
.def_readwrite("trace_level",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个level暴漏吗?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已改

&paddle::platform::ProfilerOptions::trace_level)
.def_readwrite("trace_switch",
&paddle::platform::ProfilerOptions::trace_switch);

py::class_<platform::RecordEvent>(m, "_RecordEvent")
.def(py::init([](std::string name, platform::TracerEventType type) {
return std::unique_ptr<platform::RecordEvent>(new platform::RecordEvent(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里使用make_unique的性能是不是更好一点?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改

name, type, 1, paddle::platform::EventRole::kOrdinary));
}))
.def("end", [](platform::RecordEvent *event) { event->End(); });

py::enum_<paddle::platform::TracerEventType>(m, "TracerEventType")
.value("Operator", paddle::platform::TracerEventType::Operator)
.value("Dataloader", paddle::platform::TracerEventType::Dataloader)
.value("ProfileStep", paddle::platform::TracerEventType::ProfileStep)
.value("CudaRuntime", paddle::platform::TracerEventType::CudaRuntime)
.value("Kernel", paddle::platform::TracerEventType::Kernel)
.value("Memcpy", paddle::platform::TracerEventType::Memcpy)
.value("Memset", paddle::platform::TracerEventType::Memset)
.value("UserDefined", paddle::platform::TracerEventType::UserDefined)
.value("OperatorInner", paddle::platform::TracerEventType::OperatorInner)
.value("Forward", paddle::platform::TracerEventType::Forward)
.value("Backward", paddle::platform::TracerEventType::Backward)
.value("Optimization", paddle::platform::TracerEventType::Optimization)
.value("Communication", paddle::platform::TracerEventType::Communication)
.value("PythonOp", paddle::platform::TracerEventType::PythonOp)
.value("PythonUserDefined",
paddle::platform::TracerEventType::PythonUserDefined);
m.def("LoadProfilerResult", &paddle::platform::LoadProfilerResult);

#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
m.def("set_cublas_switch", platform::SetAllowTF32Cublas);
Expand Down
2 changes: 2 additions & 0 deletions python/paddle/fluid/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ def to_list(s):
from .core_avx import _set_cached_executor_build_strategy
from .core_avx import _device_synchronize
from .core_avx import _get_current_stream
from .core_avx import _Profiler, _ProfilerResult, _RecordEvent
from .core_avx import _set_current_stream
if sys.platform != 'win32':
from .core_avx import _set_process_pids
Expand Down Expand Up @@ -344,6 +345,7 @@ def to_list(s):
from .core_noavx import _device_synchronize
from .core_noavx import _get_current_stream
from .core_noavx import _set_current_stream
from .core_avx import _Profiler, _ProfilerResult, _RecordEvent
if sys.platform != 'win32':
from .core_noavx import _set_process_pids
from .core_noavx import _erase_process_pids
Expand Down
26 changes: 26 additions & 0 deletions python/paddle/profiler/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright (c) 2022 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.

from .profiler import ProfilerState, ProfilerTarget
from .profiler import make_scheduler, export_chrome_tracing, export_protobuf
from .profiler import Profiler
from .profiler import TracerEventType
from .utils import RecordEvent
from .profiler_statistic import SortedKeys

__all__ = [
'ProfilerState', 'ProfilerTarget', 'TracerEventType', 'make_scheduler',
'export_chrome_tracing', 'export_protobuf', 'Profiler', 'RecordEvent',
'SortedKeys'
]
Loading