Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PHI Decoupling]Remove Profiler header (Part3) #50721

Merged
merged 2 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 0 additions & 27 deletions paddle/fluid/platform/profiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ PADDLE_DEFINE_EXPORTED_bool(enable_rpc_profiler,
false,
"Enable rpc profiler or not.");

DEFINE_bool(enable_record_op_info,
false,
"enable operator supplement info recorder");

DEFINE_bool(enable_record_memory, false, "enable memory recorder");

namespace paddle {
Expand Down Expand Up @@ -110,25 +106,6 @@ RecordOpInfoSupplement::RecordOpInfoSupplement(
PosixInNsec(), type, input_shapes, dtypes, attrs, op_id);
}

RecordOpInfoSupplement::RecordOpInfoSupplement(
const std::string &type,
const std::vector<std::pair<const char *, std::vector<framework::DDim>>>
&input_shapes,
const framework::AttributeMap &attrs) {
if (FLAGS_enable_host_event_recorder_hook == false) {
return;
}
if (IsEnabled() == false) {
return;
}
std::map<std::string, std::vector<framework::proto::VarType::Type>> dtypes;
uint64_t op_id = 0;
HostEventRecorder<OperatorSupplementOriginEvent>::GetInstance().RecordEvent(
PosixInNsec(), type, input_shapes, dtypes, attrs, op_id);
}

bool RecordOpInfoSupplement::IsEnabled() { return FLAGS_enable_record_op_info; }

bool RecordMemEvent::IsEnabled() { return FLAGS_enable_record_memory; }

std::map<const char *, std::map<uint64_t, std::vector<uint64_t>>>
Expand Down Expand Up @@ -873,10 +850,6 @@ void DisableHostEventRecorder() {
FLAGS_enable_host_event_recorder_hook = false;
}

void EnableOpInfoRecorder() { FLAGS_enable_record_op_info = true; }

void DisableOpInfoRecorder() { FLAGS_enable_record_op_info = false; }

void EnableMemoryRecorder() { FLAGS_enable_record_memory = true; }

void DisableMemoryRecorder() { FLAGS_enable_record_memory = false; }
Expand Down
3 changes: 0 additions & 3 deletions paddle/fluid/platform/profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,6 @@ void DisableHostEventRecorder();
void EnableMemoryRecorder();
void DisableMemoryRecorder();

void EnableOpInfoRecorder();
void DisableOpInfoRecorder();

// Defined for UT
std::string PrintHostEvents();

Expand Down
22 changes: 1 addition & 21 deletions paddle/fluid/platform/profiler/common_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,7 @@ struct OperatorSupplementOriginEvent {
strncpy(buf, type_name.c_str(), type_name.length() + 1);
op_type = buf;
}
OperatorSupplementOriginEvent(
std::function<void *(size_t)> arena_allocator,
uint64_t timestamp_ns,
const std::string &type_name,
const std::vector<std::pair<const char *, std::vector<framework::DDim>>>
&shapes,
const std::map<std::string, std::vector<framework::proto::VarType::Type>>
&dtypes,
const framework::AttributeMap &attributes,
uint64_t op_id)
: timestamp_ns(timestamp_ns),
dtypes(dtypes),
attributes(attributes),
op_id(op_id) {
auto buf = static_cast<char *>(arena_allocator(type_name.length() + 1));
strncpy(buf, type_name.c_str(), type_name.length() + 1);
op_type = buf;
for (auto it = shapes.begin(); it != shapes.end(); it++) {
input_shapes[std::string((*it).first)] = (*it).second;
}
}

uint64_t timestamp_ns;
const char *op_type = nullptr; // not owned, designed for performance
// input shapes
Expand Down
14 changes: 2 additions & 12 deletions paddle/fluid/platform/profiler/supplement_tracing.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ limitations under the License. */
#include "paddle/fluid/framework/shape_inference.h"
#include "paddle/fluid/framework/type_defs.h"
#include "paddle/fluid/platform/profiler/trace_event.h"
#include "paddle/phi/api/profiler/supplement_tracing.h"
#include "paddle/phi/core/compat/arg_map_context.h"

namespace paddle {
Expand All @@ -30,10 +31,8 @@ class RuntimeContext;
}
namespace platform {

class RecordOpInfoSupplement {
class RecordOpInfoSupplement : public phi::RecordOpInfoSupplement {
Copy link
Contributor

Choose a reason for hiding this comment

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

这里用同名类做继承有什么作用吗?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

实际上不同名,因为命名空间不一样,继承是为了能够使用phi::RecordOpInfoSupplement当中的函数

public:
static bool IsEnabled();

/**
* @param type: Operator type name.
* @param attrs: Attribute map of op.
Expand All @@ -55,15 +54,6 @@ class RecordOpInfoSupplement {
const framework::AttributeMap& attrs,
const framework::InferShapeContext& shape_ctx,
const phi::KernelSignature& kernel_signature);

/**
*
*/
explicit RecordOpInfoSupplement(
const std::string& type,
const std::vector<std::pair<const char*, std::vector<framework::DDim>>>&
input_shapes,
const framework::AttributeMap& attrs);
};

} // namespace platform
Expand Down
4 changes: 2 additions & 2 deletions paddle/fluid/pybind/pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2452,8 +2452,8 @@ All parameter, weight, gradient are variables in Paddle.
m.def("load_profiler_result", &paddle::platform::LoadProfilerResult);
m.def("enable_memory_recorder", &paddle::platform::EnableMemoryRecorder);
m.def("disable_memory_recorder", &paddle::platform::DisableMemoryRecorder);
m.def("enable_op_info_recorder", &paddle::platform::EnableOpInfoRecorder);
m.def("disable_op_info_recorder", &paddle::platform::DisableOpInfoRecorder);
m.def("enable_op_info_recorder", &phi::EnableOpInfoRecorder);
m.def("disable_op_info_recorder", &phi::DisableOpInfoRecorder);

#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
m.def("set_cublas_switch", platform::SetAllowTF32Cublas);
Expand Down
28 changes: 28 additions & 0 deletions paddle/phi/api/profiler/common_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "paddle/phi/api/profiler/event.h" // import EventRole, TODO(TIEXING): remove later
#include "paddle/phi/api/profiler/trace_event.h"
#include "paddle/phi/core/attribute.h"
#include "paddle/phi/core/ddim.h"

namespace phi {
Expand Down Expand Up @@ -104,4 +105,31 @@ struct CommonMemEvent {
uint64_t peak_reserved;
};

struct OperatorSupplementOriginEvent {
public:
OperatorSupplementOriginEvent(
std::function<void *(size_t)> arena_allocator,
uint64_t timestamp_ns,
const std::string &type_name,
const std::vector<std::pair<const char *, std::vector<DDim>>> &shapes,
const AttributeMap &attributes,
uint64_t op_id)
: timestamp_ns(timestamp_ns), attributes(attributes), op_id(op_id) {
auto buf = static_cast<char *>(arena_allocator(type_name.length() + 1));
strncpy(buf, type_name.c_str(), type_name.length() + 1);
op_type = buf;
for (auto it = shapes.begin(); it != shapes.end(); it++) {
input_shapes[std::string((*it).first)] = (*it).second;
}
}
uint64_t timestamp_ns;
const char *op_type = nullptr; // not owned, designed for performance
// input shapes
std::map<std::string, std::vector<DDim>> input_shapes;
// op attributes
AttributeMap attributes;
// op id
uint64_t op_id;
};

} // namespace phi
25 changes: 25 additions & 0 deletions paddle/phi/api/profiler/profiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ DEFINE_bool(enable_host_event_recorder_hook,
false,
"enable HostEventRecorder, hook Profiler");

DEFINE_bool(enable_record_op_info,
false,
"enable operator supplement info recorder");

namespace phi {

ProfilerState ProfilerHelper::g_state = ProfilerState::kDisabled;
Expand Down Expand Up @@ -265,4 +269,25 @@ bool RecordEvent::IsEnabled() {
ProfilerHelper::g_state != ProfilerState::kDisabled;
}

RecordOpInfoSupplement::RecordOpInfoSupplement(
const std::string &type,
const std::vector<std::pair<const char *, std::vector<DDim>>> &input_shapes,
const AttributeMap &attrs) {
if (FLAGS_enable_host_event_recorder_hook == false) {
return;
}
if (IsEnabled() == false) {
return;
}
uint64_t op_id = 0;
HostEventRecorder<OperatorSupplementOriginEvent>::GetInstance().RecordEvent(
PosixInNsec(), type, input_shapes, attrs, op_id);
}

bool RecordOpInfoSupplement::IsEnabled() { return FLAGS_enable_record_op_info; }

void EnableOpInfoRecorder() { FLAGS_enable_record_op_info = true; }

void DisableOpInfoRecorder() { FLAGS_enable_record_op_info = false; }

} // namespace phi
4 changes: 4 additions & 0 deletions paddle/phi/api/profiler/profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ limitations under the License. */
#include "gflags/gflags.h"

#include "paddle/phi/api/profiler/event_tracing.h"
#include "paddle/phi/api/profiler/supplement_tracing.h"

DECLARE_bool(enable_host_event_recorder_hook);

Expand Down Expand Up @@ -88,4 +89,7 @@ void PopEvent(const std::string& name,
const EventRole role,
const std::string attr = "none");

void EnableOpInfoRecorder();
void DisableOpInfoRecorder();

} // namespace phi
39 changes: 39 additions & 0 deletions paddle/phi/api/profiler/supplement_tracing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
Copy link
Contributor

Choose a reason for hiding this comment

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

2021 -> 2023

Copy link
Contributor Author

@YuanRisheng YuanRisheng Feb 22, 2023

Choose a reason for hiding this comment

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

下个解耦pr修改


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 <map>
#include <string>
#include <utility>

#include "paddle/phi/core/attribute.h"
#include "paddle/phi/core/ddim.h"

namespace phi {

class RecordOpInfoSupplement {
public:
static bool IsEnabled();

RecordOpInfoSupplement() = default;

explicit RecordOpInfoSupplement(
const std::string& type,
const std::vector<std::pair<const char*, std::vector<DDim>>>&
input_shapes,
const AttributeMap& attrs);
};

} // namespace phi
6 changes: 3 additions & 3 deletions paddle/phi/api/yaml/generator/api_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ def get_kernel_args(self, kernel_tensor_type=None, code_indent=''):
input_tensor_code = (
input_tensor_code
+ f"""
{code_indent} if(platform::RecordOpInfoSupplement::IsEnabled()){{"""
{code_indent} if(phi::RecordOpInfoSupplement::IsEnabled()){{"""
)
single_tensor_names = []
list_tensor_names = []
Expand Down Expand Up @@ -1030,7 +1030,7 @@ def get_kernel_args(self, kernel_tensor_type=None, code_indent=''):
)

input_tensor_code += f"""
{code_indent} framework::AttributeMap attrs;"""
{code_indent} phi::AttributeMap attrs;"""

for attr_name in self.attrs['names']:
if 'IntArray' in self.attrs['attr_info'][attr_name][0]:
Expand Down Expand Up @@ -1096,7 +1096,7 @@ def get_kernel_args(self, kernel_tensor_type=None, code_indent=''):
input_tensor_code = (
input_tensor_code
+ f"""
{code_indent} platform::RecordOpInfoSupplement("{self.api}", input_shapes, attrs);
{code_indent} phi::RecordOpInfoSupplement("{self.api}", input_shapes, attrs);
{code_indent} }}"""
)
kernel_args = ["*dev_ctx"]
Expand Down
2 changes: 1 addition & 1 deletion paddle/phi/api/yaml/generator/api_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def source_include(header_file_path):
#include "paddle/phi/infermeta/ternary.h"

#include "paddle/phi/api/profiler/event_tracing.h"
#include "paddle/fluid/platform/profiler/supplement_tracing.h"
#include "paddle/phi/api/profiler/supplement_tracing.h"

DECLARE_bool(conv2d_disable_cudnn);
DECLARE_int32(low_precision_op_list);
Expand Down
2 changes: 1 addition & 1 deletion paddle/phi/api/yaml/generator/backward_api_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def source_include(header_file_path):
#include "paddle/phi/infermeta/unary.h"

#include "paddle/phi/api/profiler/event_tracing.h"
#include "paddle/fluid/platform/profiler/supplement_tracing.h"
#include "paddle/phi/api/profiler/supplement_tracing.h"

DECLARE_bool(conv2d_disable_cudnn);
DECLARE_int32(low_precision_op_list);
Expand Down
2 changes: 1 addition & 1 deletion paddle/phi/api/yaml/generator/intermediate_api_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def source_include(header_file_path):
#include "paddle/phi/infermeta/sparse/multiary.h"

#include "paddle/phi/api/profiler/event_tracing.h"
#include "paddle/fluid/platform/profiler/supplement_tracing.h"
#include "paddle/phi/api/profiler/supplement_tracing.h"

DECLARE_int32(low_precision_op_list);
"""
Expand Down