Skip to content

Commit 24bc3be

Browse files
Taylor Robiefacebook-github-bot
Taylor Robie
authored andcommitted
[Profiler] Clean up profiler includes. (pytorch#69421)
Summary: Pull Request resolved: pytorch#69421 I've hit a lot of build issues in D32671972, and I've come to realize that a lot of it boils down to header hygene. `function.h` includes `profiler.h` *solely* to transitively include `record_function.h` which winds up leaking the profiler symbols. Moreover several files are relying on transitive includes to get access to `getTime`. As long as I have to touch all the places that use `getTime`, I may as well also move them to the new namespace. Test Plan: Unit tests and CI. Reviewed By: aaronenyeshi, albanD Differential Revision: D32865907 fbshipit-source-id: f87d6fd5afb784dca2146436e72c69e34623020e
1 parent 587f8d9 commit 24bc3be

File tree

13 files changed

+96
-67
lines changed

13 files changed

+96
-67
lines changed

android/pytorch_android/src/main/cpp/pytorch_jni_jit.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
#include <android/log.h>
2020
#endif
2121

22-
using namespace torch::autograd::profiler;
23-
2422
namespace pytorch_jni {
2523

2624
namespace {

test/cpp/jit/test_backend_compiler_lib.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,13 @@ class BackendWithCompiler : public PyTorchBackendInterface {
9898
op_runtimes_us.reserve(handle.toList().size());
9999

100100
c10::List<at::Tensor> output_list;
101-
auto start_us = autograd::profiler::getTime() / 1000;
101+
auto start_us = torch::profiler::impl::getTime() / 1000;
102102
for (const auto& token : handle.toList()) {
103103
IValue val = token;
104104
auto instruction = val.toTupleRef().elements()[0].toStringRef();
105105
auto debug_handle = val.toTupleRef().elements()[1].toInt();
106106
double const_val = 1.0;
107-
auto start_time_us = autograd::profiler::getTime() / 1000;
107+
auto start_time_us = torch::profiler::impl::getTime() / 1000;
108108
try {
109109
if (instruction.rfind("prim::Constant", 0) == 0) {
110110
TORCH_CHECK(
@@ -147,7 +147,7 @@ class BackendWithCompiler : public PyTorchBackendInterface {
147147
} catch (c10::Error& e) {
148148
TORCH_DELEGATED_BACKEND_THROW(false, e.what(), debug_handle);
149149
}
150-
auto end_time_us = autograd::profiler::getTime() / 1000;
150+
auto end_time_us = torch::profiler::impl::getTime() / 1000;
151151
auto duration = end_time_us - start_time_us;
152152
op_runtimes_us.emplace_back(duration, debug_handle, instruction);
153153
}

test/cpp/jit/test_misc.cpp

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <torch/csrc/autograd/engine.h>
1212
#include <torch/csrc/autograd/generated/variable_factories.h>
13+
#include <torch/csrc/autograd/profiler.h>
1314
#include <torch/csrc/autograd/variable.h>
1415
#include <torch/csrc/jit/api/function_impl.h>
1516
#include <torch/csrc/jit/api/module.h>
@@ -76,8 +77,6 @@
7677
#include <utility>
7778
#include <vector>
7879

79-
using namespace torch::autograd::profiler;
80-
8180
namespace torch {
8281
namespace jit {
8382
inline c10::AliasAnalysisKind aliasAnalysisFromSchema() {
@@ -2664,7 +2663,8 @@ TEST(ComputeFlopsTest, Basic) {
26642663

26652664
// Test unknown operator
26662665
std::unordered_map<std::string, c10::IValue> extra_args;
2667-
flops = computeFlops(std::string("aten::unknown"), extra_args);
2666+
flops = torch::profiler::impl::computeFlops(
2667+
std::string("aten::unknown"), extra_args);
26682668
ASSERT_EQ(flops, 0);
26692669

26702670
// Test aten::conv2d
@@ -2680,30 +2680,34 @@ TEST(ComputeFlopsTest, Basic) {
26802680
extra_args["padding"] = at::IValue(at::IntArrayRef(padding));
26812681
extra_args["stride"] = at::IValue(at::IntArrayRef(stride));
26822682
extra_args["dilation"] = at::IValue(at::IntArrayRef(dilation));
2683-
flops = computeFlops(std::string("aten::conv2d"), extra_args);
2683+
flops = torch::profiler::impl::computeFlops(
2684+
std::string("aten::conv2d"), extra_args);
26842685
ASSERT_EQ(flops, 13440);
26852686

26862687
// Test aten::conv2d fail
26872688
input_size = {4, 5, 6, 7};
26882689
weight_size = {4, 5, 6};
26892690
extra_args["input_size"] = at::IValue(at::IntArrayRef(input_size));
26902691
extra_args["weight_size"] = at::IValue(at::IntArrayRef(weight_size));
2691-
flops = computeFlops(std::string("aten::conv2d"), extra_args);
2692+
flops = torch::profiler::impl::computeFlops(
2693+
std::string("aten::conv2d"), extra_args);
26922694
ASSERT_EQ(flops, 0);
26932695

26942696
// Test aten::conv2d fail 2
26952697
weight_size = {3, 5, 2, 1};
26962698
stride = {0, 0};
26972699
extra_args["weight_size"] = at::IValue(at::IntArrayRef(input_size));
26982700
extra_args["stride"] = at::IValue(at::IntArrayRef(stride));
2699-
flops = computeFlops(std::string("aten::conv2d"), extra_args);
2701+
flops = torch::profiler::impl::computeFlops(
2702+
std::string("aten::conv2d"), extra_args);
27002703
ASSERT_EQ(flops, 0);
27012704

27022705
// Test aten::conv2d fail 3
27032706
extra_args.clear();
27042707
input_size = {4, 5, 6, 7};
27052708
extra_args["input_size"] = at::IValue(at::IntArrayRef(input_size));
2706-
flops = computeFlops(std::string("aten::conv2d"), extra_args);
2709+
flops = torch::profiler::impl::computeFlops(
2710+
std::string("aten::conv2d"), extra_args);
27072711
ASSERT_EQ(flops, 0);
27082712

27092713
// Test aten::mm
@@ -2712,11 +2716,13 @@ TEST(ComputeFlopsTest, Basic) {
27122716
std::vector<int64_t> mat2_sizes = {6, 5, 4, 3};
27132717
extra_args["mat1_size"] = at::IValue(at::IntArrayRef(mat1_sizes));
27142718
extra_args["mat2_size"] = at::IValue(at::IntArrayRef(mat2_sizes));
2715-
flops = computeFlops(std::string("aten::mm"), extra_args);
2719+
flops =
2720+
torch::profiler::impl::computeFlops(std::string("aten::mm"), extra_args);
27162721
ASSERT_EQ(flops, 43200);
27172722

27182723
// Test aten::addmm
2719-
flops = computeFlops(std::string("aten::addmm"), extra_args);
2724+
flops = torch::profiler::impl::computeFlops(
2725+
std::string("aten::addmm"), extra_args);
27202726
ASSERT_EQ(flops, 43200);
27212727

27222728
// Test aten::bmm
@@ -2725,30 +2731,35 @@ TEST(ComputeFlopsTest, Basic) {
27252731
mat2_sizes = {7, 6, 3};
27262732
extra_args["mat1_size"] = at::IValue(at::IntArrayRef(mat1_sizes));
27272733
extra_args["mat2_size"] = at::IValue(at::IntArrayRef(mat2_sizes));
2728-
flops = computeFlops(std::string("aten::bmm"), extra_args);
2734+
flops =
2735+
torch::profiler::impl::computeFlops(std::string("aten::bmm"), extra_args);
27292736
ASSERT_EQ(flops, 1260);
27302737

27312738
// Test aten::baddbmm
2732-
flops = computeFlops(std::string("aten::baddbmm"), extra_args);
2739+
flops = torch::profiler::impl::computeFlops(
2740+
std::string("aten::baddbmm"), extra_args);
27332741
ASSERT_EQ(flops, 1260);
27342742

27352743
// Test mm out of range
27362744
extra_args.clear();
2737-
flops = computeFlops(std::string("aten::mm"), extra_args);
2745+
flops =
2746+
torch::profiler::impl::computeFlops(std::string("aten::mm"), extra_args);
27382747
ASSERT_EQ(flops, 0);
27392748

27402749
// Test aten::add.Tensor
27412750
extra_args.clear();
27422751
std::vector<int64_t> mat_sizes = {3, 4, 5, 6};
27432752
extra_args["mat_size"] = at::IValue(at::IntArrayRef(mat_sizes));
2744-
flops = computeFlops(std::string("aten::add"), extra_args);
2753+
flops =
2754+
torch::profiler::impl::computeFlops(std::string("aten::add"), extra_args);
27452755
ASSERT_EQ(flops, 360);
27462756

27472757
// Test aten::mul.Tensor
27482758
extra_args.clear();
27492759
mat_sizes = {3, 4, 5, 6};
27502760
extra_args["mat_size"] = at::IValue(at::IntArrayRef(mat_sizes));
2751-
flops = computeFlops(std::string("aten::mul"), extra_args);
2761+
flops =
2762+
torch::profiler::impl::computeFlops(std::string("aten::mul"), extra_args);
27522763
ASSERT_EQ(flops, 360);
27532764
}
27542765

torch/csrc/api/include/torch/utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <ATen/Parallel.h>
44
#include <ATen/record_function.h>
55
#include <torch/csrc/autograd/grad_mode.h>
6+
#include <torch/csrc/autograd/profiler.h>
67
#include <torch/csrc/api/include/torch/types.h>
78
#include <torch/csrc/utils/crash_handler.h>
89
#include <cstdint>

torch/csrc/autograd/function.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
#include <torch/csrc/autograd/edge.h>
44
#include <torch/csrc/autograd/grad_mode.h>
55
#include <torch/csrc/autograd/anomaly_mode.h>
6-
#include <torch/csrc/autograd/profiler.h>
76
#include <torch/csrc/autograd/saved_variable.h>
87
#include <torch/csrc/autograd/input_metadata.h>
98
#include <torch/csrc/autograd/variable.h>
109
#include <torch/csrc/utils/python_stub.h>
1110
#include <torch/csrc/utils/variadic.h>
1211

1312
#include <ATen/ATen.h>
13+
#include <ATen/record_function.h>
1414
#include <ATen/SequenceNumber.h>
1515
#include <c10/util/Exception.h>
1616

torch/csrc/autograd/profiler_cuda.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct CUDAMethods : public CUDAStubs {
4545
});
4646
auto stream = at::cuda::getCurrentCUDAStream();
4747
if (cpu_ns) {
48-
*cpu_ns = getTime();
48+
*cpu_ns = torch::profiler::impl::getTime();
4949
}
5050
TORCH_CUDA_CHECK(cudaEventRecord(cuda_event_ptr, stream));
5151
}

torch/csrc/autograd/profiler_kineto.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ inline int64_t getTimeUs() {
4141
#ifdef USE_KINETO
4242
return libkineto::timeSinceEpoch(std::chrono::system_clock::now());
4343
#else
44-
return getTime() / 1000;
44+
return torch::profiler::impl::getTime() / 1000;
4545
#endif // USE_KINETO
4646
}
4747
} // namespace

torch/csrc/autograd/profiler_legacy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ void LegacyEvent::record(bool record_cuda) {
568568
cuda_stubs()->record(&device_, &cuda_event, &cpu_ns_);
569569
return;
570570
}
571-
cpu_ns_ = getTime();
571+
cpu_ns_ = torch::profiler::impl::getTime();
572572
}
573573

574574
/* static */ LegacyEvent LegacyEvent::fromIValue(const at::IValue& eventIValue) {

torch/csrc/autograd/profiler_legacy.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,3 +598,15 @@ struct TORCH_API ProfilerThreadLocalState : public c10::MemoryReportingInfoBase
598598

599599
} // namespace profiler
600600
}} // namespace torch::autograd
601+
602+
// Mirror symbols in new namespace for transition.
603+
namespace torch {
604+
namespace profiler {
605+
namespace impl {
606+
using torch::autograd::profiler::computeFlops;
607+
using torch::autograd::profiler::getTime;
608+
using torch::autograd::profiler::ProfilerConfig;
609+
using torch::autograd::profiler::ProfilerState;
610+
} // impl
611+
} // profiler
612+
} // torch

torch/csrc/distributed/c10d/reducer.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <c10d/comm.hpp>
1616
#include <c10d/default_comm_hooks.hpp>
1717
#include <torch/csrc/autograd/function.h>
18+
#include <torch/csrc/autograd/profiler.h>
1819
#include <torch/csrc/autograd/variable.h>
1920
#ifndef _WIN32
2021
#include <torch/csrc/distributed/autograd/context/context.h>
@@ -29,7 +30,7 @@ constexpr int kDDPRuntimeLoggingSampleRate = 100;
2930
constexpr int kUnsetTime = -1;
3031

3132
inline int64_t current_time_in_nanos() {
32-
return torch::autograd::profiler::getTime();
33+
return torch::profiler::impl::getTime();
3334
}
3435

3536
// Forward declaration

torch/csrc/jit/mobile/profiler_edge.cpp

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,49 +18,52 @@ KinetoEdgeCPUProfiler::KinetoEdgeCPUProfiler(
1818
const bool with_flops,
1919
const bool with_modules)
2020
: m_(m), trace_file_name_(fname) {
21-
profiler::ProfilerConfig config(
22-
profiler::ProfilerState::KINETO,
21+
torch::profiler::impl::ProfilerConfig config(
22+
torch::profiler::impl::ProfilerState::KINETO,
2323
report_input_shapes,
2424
profile_memory,
2525
with_stack,
2626
with_flops,
2727
with_modules);
28-
profiler::prepareProfiler(config, {profiler::ActivityType::CPU});
28+
torch::autograd::profiler::prepareProfiler(
29+
config, {torch::autograd::profiler::ActivityType::CPU});
2930
if (with_modules || with_stack) {
30-
auto post_processing = [this, with_stack, with_modules](
31-
std::vector<profiler::KinetoEvent>& events) {
32-
std::string no_debug_info("Model was not saved with debug information");
33-
for (auto& e : events) {
34-
if (with_modules) {
35-
// Since KinetoEvents's module hierarchy takes vector of strings we
36-
// just construct a temporary vector using one string element
37-
if (this->m_.hasDebugHandles()) {
38-
e.moduleHierarchy(std::vector<std::string>(
39-
{this->m_.getModuleHierarchy(e.debugHandle())}));
40-
} else {
41-
e.moduleHierarchy(std::vector<std::string>({no_debug_info}));
31+
auto post_processing =
32+
[this, with_stack, with_modules](
33+
std::vector<torch::autograd::profiler::KinetoEvent>& events) {
34+
std::string no_debug_info(
35+
"Model was not saved with debug information");
36+
for (auto& e : events) {
37+
if (with_modules) {
38+
// Since KinetoEvents's module hierarchy takes vector of strings
39+
// we just construct a temporary vector using one string element
40+
if (this->m_.hasDebugHandles()) {
41+
e.moduleHierarchy(std::vector<std::string>(
42+
{this->m_.getModuleHierarchy(e.debugHandle())}));
43+
} else {
44+
e.moduleHierarchy(std::vector<std::string>({no_debug_info}));
45+
}
46+
} else if (with_stack) {
47+
// Since KinetoEvents's stack trace takes vector of strings we
48+
// just construct a temporary vector using one string element
49+
if (this->m_.hasDebugHandles()) {
50+
e.stack(std::vector<std::string>(
51+
{this->m_.getCallStack(e.debugHandle())}));
52+
} else {
53+
e.stack(std::vector<std::string>({no_debug_info}));
54+
}
55+
}
4256
}
43-
} else if (with_stack) {
44-
// Since KinetoEvents's stack trace takes vector of strings we just
45-
// construct a temporary vector using one string element
46-
if (this->m_.hasDebugHandles()) {
47-
e.stack(std::vector<std::string>(
48-
{this->m_.getCallStack(e.debugHandle())}));
49-
} else {
50-
e.stack(std::vector<std::string>({no_debug_info}));
51-
}
52-
}
53-
}
54-
};
55-
profiler::enableProfilerWithEventPostProcess(
57+
};
58+
torch::autograd::profiler::enableProfilerWithEventPostProcess(
5659
config,
57-
{profiler::ActivityType::CPU},
60+
{torch::autograd::profiler::ActivityType::CPU},
5861
post_processing,
5962
{at::RecordScope::LITE_INTERPRETER});
6063
} else {
61-
profiler::enableProfiler(
64+
torch::autograd::profiler::enableProfiler(
6265
config,
63-
{profiler::ActivityType::CPU},
66+
{torch::autograd::profiler::ActivityType::CPU},
6467
{at::RecordScope::LITE_INTERPRETER});
6568
}
6669
trace_file_name_ = fname;
@@ -75,7 +78,7 @@ void KinetoEdgeCPUProfiler::recordBackendEvent(
7578
const int64_t debug_handle,
7679
const std::string& event_name,
7780
const std::string& backend_name) {
78-
profiler::reportBackendEventToActiveKinetoProfiler(
81+
torch::autograd::profiler::reportBackendEventToActiveKinetoProfiler(
7982
start_time_us,
8083
end_time_us,
8184
debug_handle,
@@ -84,18 +87,18 @@ void KinetoEdgeCPUProfiler::recordBackendEvent(
8487
backend_name);
8588
}
8689

87-
const std::unique_ptr<profiler::ProfilerResult>& KinetoEdgeCPUProfiler::
88-
disableProfiler() {
90+
const std::unique_ptr<torch::autograd::profiler::ProfilerResult>&
91+
KinetoEdgeCPUProfiler::disableProfiler() {
8992
TORCH_CHECK(
9093
!profiler_result_,
9194
"KinetoEdgeCPUProfiler already disabled. "
9295
"To get list of events use getProfilerResults()");
93-
profiler_result_ = profiler::disableProfiler();
96+
profiler_result_ = torch::autograd::profiler::disableProfiler();
9497
return profiler_result_;
9598
}
9699

97-
const std::unique_ptr<profiler::ProfilerResult>& KinetoEdgeCPUProfiler::
98-
getProfilerResult() {
100+
const std::unique_ptr<torch::autograd::profiler::ProfilerResult>&
101+
KinetoEdgeCPUProfiler::getProfilerResult() {
99102
TORCH_CHECK(
100103
profiler_result_,
101104
"KinetoEdgeCPUProfiler has not been disabled. "
@@ -108,7 +111,7 @@ KinetoEdgeCPUProfiler::~KinetoEdgeCPUProfiler() {
108111
if (profiler_result_) {
109112
profiler_result_->save(trace_file_name_);
110113
} else {
111-
profiler::disableProfiler()->save(trace_file_name_);
114+
torch::autograd::profiler::disableProfiler()->save(trace_file_name_);
112115
}
113116
}
114117
tls_edge_profiler = nullptr;

torch/csrc/jit/mobile/profiler_edge.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include <torch/csrc/autograd/profiler_kineto.h>
33
#include <torch/csrc/jit/mobile/module.h>
44

5-
namespace profiler = torch::autograd::profiler;
65
namespace torch {
76
namespace jit {
87
namespace mobile {
@@ -58,8 +57,10 @@ class TORCH_API KinetoEdgeCPUProfiler {
5857
const bool with_flops = false,
5958
const bool with_modules = false);
6059

61-
const std::unique_ptr<profiler::ProfilerResult>& disableProfiler();
62-
const std::unique_ptr<profiler::ProfilerResult>& getProfilerResult();
60+
const std::unique_ptr<torch::autograd::profiler::ProfilerResult>&
61+
disableProfiler();
62+
const std::unique_ptr<torch::autograd::profiler::ProfilerResult>&
63+
getProfilerResult();
6364
void recordBackendEvent(
6465
const int64_t start_time_us,
6566
const int64_t end_time_us,
@@ -76,7 +77,7 @@ class TORCH_API KinetoEdgeCPUProfiler {
7677
*/
7778
const mobile::Module& m_;
7879
std::string trace_file_name_;
79-
std::unique_ptr<profiler::ProfilerResult> profiler_result_;
80+
std::unique_ptr<torch::autograd::profiler::ProfilerResult> profiler_result_;
8081
};
8182

8283
TORCH_API KinetoEdgeCPUProfiler* getCurrentEdgeProfiler();

0 commit comments

Comments
 (0)