Concat namespaces of torch/csrc/profiler code and other fixes (#128606)

Improve namespaces and modernize codebase of torch/csrc/profiler code.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/128606
Approved by: https://github.com/Skylion007, https://github.com/aaronenyeshi
This commit is contained in:
cyy
2024-06-13 16:46:31 +00:00
committed by PyTorch MergeBot
parent 7c370d2fb0
commit e2a72313e8
11 changed files with 28 additions and 66 deletions

View File

@ -226,7 +226,7 @@ PyObject* RecordFunctionFast_enter(PyObject* selfGeneric, PyObject* unused) {
bool profiler_need_input = torch::autograd::profiler::profilerEnabled() &&
torch::autograd::profiler::getProfilerConfig().report_input_shapes;
// parse through args if they exist
if (self->input_values != NULL && profiler_need_input) {
if (self->input_values != nullptr && profiler_need_input) {
THPObjectPtr input_fast(
PySequence_Fast(self->input_values, "input must be a sequence"));
PyObject** input_items = PySequence_Fast_ITEMS(input_fast.get());
@ -240,9 +240,9 @@ PyObject* RecordFunctionFast_enter(PyObject* selfGeneric, PyObject* unused) {
}
// parse through kwargs if they exist
if (self->keyword_values != NULL && profiler_need_input) {
if (self->keyword_values != nullptr && profiler_need_input) {
Py_ssize_t pos = 0;
PyObject *key, *value;
PyObject *key = nullptr, *value = nullptr;
while (PyDict_Next(self->keyword_values, &pos, &key, &value)) {
// Get the string representation of the key and value
std::string key_str = THPUtils_unpackString(key);

View File

@ -5,8 +5,7 @@
#include <torch/csrc/profiler/collection.h>
#include <torch/csrc/profiler/python/pybind.h>
namespace pybind11 {
namespace detail {
namespace pybind11::detail {
using torch::profiler::impl::TensorID;
#define STRONG_POINTER_TYPE_CASTER(T) \
@ -23,13 +22,10 @@ STRONG_POINTER_TYPE_CASTER(torch::profiler::impl::PyOptimizerSelf);
template <>
struct type_caster<TensorID> : public strong_uint_type_caster<TensorID> {};
} // namespace detail
} // namespace pybind11
} // namespace pybind11::detail
namespace torch {
namespace profiler {
namespace torch::profiler {
void initPythonBindings(PyObject* module);
} // namespace profiler
} // namespace torch
} // namespace torch::profiler

View File

@ -6,8 +6,7 @@
#include <torch/csrc/utils/pybind.h>
#include <torch/csrc/utils/python_numbers.h>
namespace pybind11 {
namespace detail {
namespace pybind11::detail {
// Strong typedefs don't make much sense in Python since everything is duck
// typed. So instead we simply extract the underlying value and let the caller
// handle correctness.
@ -46,5 +45,4 @@ struct strong_uint_type_caster {
PYBIND11_TYPE_CASTER(T, _("strong_uint"));
};
} // namespace detail
} // namespace pybind11
} // namespace pybind11::detail

View File

@ -49,9 +49,7 @@ constexpr auto kETProcessGroupName = "pg_name";
constexpr auto kETProcessGroupDesc = "pg_desc";
#endif // USE_DISTRIBUTED
namespace torch {
namespace profiler {
namespace impl {
namespace torch::profiler::impl {
//******************************************************************************
// JSON output utility functions. To be merged with PyTorch profiler.
@ -567,7 +565,7 @@ inline std::string getCommsNodeAttrs(const RecordFunction& fn) {
#endif // USE_DISTRIBUTED
// XXX consider using as string stream?
return attrs.size() == 0 ? "" : fmt::format(", {}", fmt::join(attrs, ", "));
return attrs.empty() ? "" : fmt::format(", {}", fmt::join(attrs, ", "));
}
static void recordOperatorStart(
@ -601,7 +599,7 @@ static void recordOperatorStart(
auto num_inputs = fn.num_inputs();
const auto inputs = fn.inputs();
VLOG(2) << "inputs: " << num_inputs << " " << inputs.size() << std::endl;
VLOG(2) << "inputs: " << num_inputs << " " << inputs.size() << '\n';
// We have two cases: for unboxed kernel, we have num_inputs ==
// inputs.size() for boxed kernel using stack, there could be more elements
// on the stack from previous ops.
@ -699,7 +697,7 @@ static void onFunctionExit(const RecordFunction& fn, ObserverContext* ctx_ptr) {
// We have two cases: for unboxed kernel, we have num_outputs ==
// outputs.size() for boxed kernel using stack, there could be more elements
// on the stack from previous ops.
VLOG(2) << "outputs: " << num_outputs << " " << outputs.size() << std::endl;
VLOG(2) << "outputs: " << num_outputs << " " << outputs.size() << '\n';
// TORCH_INTERNAL_ASSERT(num_outputs <= outputs.size());
if (num_outputs > outputs.size()) {
LOG(WARNING) << "RecordFunction " << fc.name
@ -837,6 +835,4 @@ void disableExecutionTraceObserver() {
<< "Trying to disable Execution Trace Observer when it's already disabled.";
}
}
} // namespace impl
} // namespace profiler
} // namespace torch
} // namespace torch::profiler::impl

View File

@ -3,9 +3,7 @@
#include <c10/macros/Export.h>
#include <string>
namespace torch {
namespace profiler {
namespace impl {
namespace torch::profiler::impl {
// Adds the execution trace observer as a global callback function, the data
// will be written to output file path.
@ -20,6 +18,4 @@ TORCH_API void enableExecutionTraceObserver();
// Disables execution trace observer.
TORCH_API void disableExecutionTraceObserver();
} // namespace impl
} // namespace profiler
} // namespace torch
} // namespace torch::profiler::impl

View File

@ -3,9 +3,7 @@
#include <torch/csrc/profiler/stubs/base.h>
#include <torch/csrc/profiler/util.h>
namespace torch {
namespace profiler {
namespace impl {
namespace torch::profiler::impl {
struct ITTThreadLocalState : ProfilerStateBase {
explicit ITTThreadLocalState(const ProfilerConfig& config)
@ -68,6 +66,4 @@ void pushITTCallbacks(
state_ptr->setCallbackHandle(handle);
}
} // namespace impl
} // namespace profiler
} // namespace torch
} // namespace torch::profiler::impl

View File

@ -1,13 +1,9 @@
#include <torch/csrc/profiler/api.h>
namespace torch {
namespace profiler {
namespace impl {
namespace torch::profiler::impl {
void pushITTCallbacks(
const ProfilerConfig& config,
const std::unordered_set<at::RecordScope>& scopes);
} // namespace impl
} // namespace profiler
} // namespace torch
} // namespace torch::profiler::impl

View File

@ -3,9 +3,7 @@
#include <torch/csrc/profiler/stubs/base.h>
#include <torch/csrc/profiler/util.h>
namespace torch {
namespace profiler {
namespace impl {
namespace torch::profiler::impl {
struct NVTXThreadLocalState : ProfilerStateBase {
explicit NVTXThreadLocalState(const ProfilerConfig& config)
@ -174,6 +172,4 @@ void pushNVTXCallbacks(
state_ptr->setCallbackHandle(handle);
}
} // namespace impl
} // namespace profiler
} // namespace torch
} // namespace torch::profiler::impl

View File

@ -1,13 +1,9 @@
#include <torch/csrc/profiler/api.h>
namespace torch {
namespace profiler {
namespace impl {
namespace torch::profiler::impl {
void pushNVTXCallbacks(
const ProfilerConfig& config,
const std::unordered_set<at::RecordScope>& scopes);
} // namespace impl
} // namespace profiler
} // namespace torch
} // namespace torch::profiler::impl

View File

@ -1,11 +1,7 @@
#include <torch/csrc/profiler/standalone/privateuse1_observer.h>
namespace torch {
namespace profiler {
namespace impl {
namespace torch::profiler::impl {
PushPRIVATEUSE1CallbacksStub pushPRIVATEUSE1CallbacksStub;
} // namespace impl
} // namespace profiler
} // namespace torch
} // namespace torch::profiler::impl

View File

@ -1,9 +1,7 @@
#pragma once
#include <torch/csrc/profiler/api.h>
namespace torch {
namespace profiler {
namespace impl {
namespace torch::profiler::impl {
using CallBackFnPtr = void (*)(
const ProfilerConfig& config,
@ -41,6 +39,4 @@ struct RegisterPRIVATEUSE1Observer {
#define REGISTER_PRIVATEUSE1_OBSERVER(name, fn) \
static RegisterPRIVATEUSE1Observer name##__register(name, fn);
} // namespace impl
} // namespace profiler
} // namespace torch
} // namespace torch::profiler::impl