1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
#include <ATen/Functions.h>
#include <gtest/gtest.h>
#include <test/cpp/jit/test_utils.h>
#include <torch/csrc/jit/api/module.h>
#include <torch/csrc/jit/frontend/resolver.h>
#include <torch/csrc/jit/mobile/import.h>
#include <torch/csrc/jit/mobile/module.h>
#include <torch/csrc/jit/mobile/profiler_edge.h>
#include <fstream>
#include <unordered_set>
#include <torch/csrc/profiler/events.h>
#include "test/cpp/lite_interpreter_runtime/resources.h"
#ifdef EDGE_PROFILER_USE_KINETO
namespace torch {
namespace jit {
namespace mobile {
namespace {
bool checkMetaData(
const std::string& op_name,
const std::string& metadata_name,
const std::string& metadata_val,
std::ifstream& trace_file) {
std::string line;
while (std::getline(trace_file, line)) {
if (line.find(op_name) != std::string::npos) {
while (std::getline(trace_file, line)) {
if (line.find(metadata_name) != std::string::npos) {
if (line.find(metadata_val) != std::string::npos ||
!metadata_val.size()) {
/* if found the right metadata_val OR if expected
* metadata value is an empty string then ignore the metadata_val */
return true;
}
}
}
}
}
return false;
}
} // namespace
TEST(MobileProfiler, ModuleHierarchy) {
auto testModelFile = torch::testing::getResourcePath(
"test/cpp/lite_interpreter_runtime/to_be_profiled_module.ptl");
std::vector<IValue> inputs;
inputs.emplace_back(at::rand({64, 64}));
inputs.emplace_back(at::rand({64, 64}));
std::string trace_file_name("/tmp/test_trace.trace");
mobile::Module bc = _load_for_mobile(testModelFile.string());
{
KinetoEdgeCPUProfiler profiler(
bc,
trace_file_name,
false, // record input_shapes
false, // profile memory
true, // record callstack
false, // record flops
true, // record module hierarchy
{}, // events
false); // adjust_vulkan_timestamps
bc.forward(inputs);
} // End of profiler
std::ifstream trace_file(trace_file_name);
std::string line;
ASSERT_TRUE(trace_file.is_open());
trace_file.seekg(0, std::ios_base::beg);
const std::string metadata_name("Module Hierarchy");
ASSERT_TRUE(checkMetaData(
"aten::sub",
metadata_name,
"top(C)::<unknown>.A0(A)::forward.aten::sub",
trace_file));
trace_file.seekg(0, std::ios_base::beg);
ASSERT_TRUE(checkMetaData(
"aten::mul",
metadata_name,
"top(C)::<unknown>.A0(A)::forward.SELF(A)::forward_impl_.SELF(A)::my_new_method.aten::mul",
trace_file));
trace_file.seekg(0, std::ios_base::beg);
ASSERT_TRUE(checkMetaData(
"aten::add",
metadata_name,
"top(C)::<unknown>.A0(A)::forward.SELF(A)::forward_impl_.aten::add",
trace_file));
ASSERT_TRUE(checkMetaData(
"aten::add",
metadata_name,
"top(C)::<unknown>.SELF(C)::call_b.B0(B)::forward.aten::add",
trace_file));
ASSERT_TRUE(checkMetaData(
"aten::add", metadata_name, "top(C)::<unknown>.aten::add", trace_file));
}
TEST(MobileProfiler, Backend) {
auto testModelFile = torch::testing::getResourcePath(
"test/cpp/lite_interpreter_runtime/test_backend_for_profiling.ptl");
std::vector<IValue> inputs;
inputs.emplace_back(at::rand({64, 64}));
inputs.emplace_back(at::rand({64, 64}));
std::string trace_file_name("/tmp/test_trace_backend.trace");
mobile::Module bc = _load_for_mobile(testModelFile.string());
{
KinetoEdgeCPUProfiler profiler(
bc,
trace_file_name,
false, // record input_shapes
false, // profile memory
true, // record callstack
false, // record flops
true); // record module hierarchy
bc.forward(inputs);
} // End of profiler
std::ifstream trace_file(trace_file_name);
std::string line;
ASSERT_TRUE(trace_file.is_open());
trace_file.seekg(0, std::ios_base::beg);
std::string metadata_name("Module Hierarchy");
ASSERT_TRUE(checkMetaData(
"aten::add", metadata_name, "top(m)::<unknown>.aten::add", trace_file));
trace_file.seekg(0, std::ios_base::beg);
metadata_name = "Backend";
ASSERT_TRUE(
checkMetaData("aten::add", metadata_name, "test_backend", trace_file));
}
TEST(MobileProfiler, BackendMemoryEvents) {
auto testModelFile = torch::testing::getResourcePath(
"test/cpp/lite_interpreter_runtime/test_backend_for_profiling.ptl");
std::vector<IValue> inputs;
inputs.emplace_back(at::rand({64, 64}));
inputs.emplace_back(at::rand({64, 64}));
std::string trace_file_name("/tmp/test_trace_backend_memory.trace");
mobile::Module bc = _load_for_mobile(testModelFile.string());
{
mobile::KinetoEdgeCPUProfiler profiler(
bc,
trace_file_name,
false, // record input_shapes
true, // profile memory
true, // record callstack
false, // record flops
true); // record module hierarchy
bc.forward(inputs);
}
std::ifstream trace_file(trace_file_name);
std::string line;
ASSERT_TRUE(trace_file.is_open());
trace_file.seekg(0, std::ios_base::beg);
std::string metadata_name("Bytes");
ASSERT_TRUE(checkMetaData("[memory]", metadata_name, "16384", trace_file));
trace_file.seekg(0, std::ios_base::beg);
metadata_name = "Total Reserved";
ASSERT_TRUE(checkMetaData("[memory]", metadata_name, "49152", trace_file));
}
TEST(MobileProfiler, ProfilerEvent) {
auto testModelFile = torch::testing::getResourcePath(
"test/cpp/lite_interpreter_runtime/test_backend_for_profiling.ptl");
std::vector<IValue> inputs;
inputs.emplace_back(at::rand({64, 64}));
inputs.emplace_back(at::rand({64, 64}));
std::string trace_file_name("/tmp/test_trace_profiler_event.trace");
std::vector<std::string> events(
torch::profiler::ProfilerPerfEvents.begin(),
torch::profiler::ProfilerPerfEvents.end());
mobile::Module bc = _load_for_mobile(testModelFile.string());
{
// Bail if something goes wrong here
try {
KinetoEdgeCPUProfiler profiler(
bc,
trace_file_name,
false, // record input_shapes
false, // profile memory
true, // record callstack
false, // record flops
true, // record module hierarchy
events); // performance events
bc.forward(inputs);
} catch (...) {
return;
}
} // End of profiler
std::ifstream trace_file(trace_file_name);
std::string line;
ASSERT_TRUE(trace_file.is_open());
for (auto& event : events) {
trace_file.seekg(0, std::ios_base::beg);
/*
* Just checking if the event entry exists in the chrometrace.
* Checking the value in a hardware independent matter is tricky.
*/
ASSERT_TRUE(checkMetaData("aten::__getitem__", event, "", trace_file));
}
}
} // namespace mobile
} // namespace jit
} // namespace torch
#endif
|