From 727a3271628d1f60ea3fd686f1e550be61289eb8 Mon Sep 17 00:00:00 2001 From: Max Ren Date: Thu, 28 Jul 2022 23:08:52 +0000 Subject: [PATCH] Back out "Back out "[profiling] Adding targets file for test_mobile_profiler"" (#82243) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Originally reverted this diff D37116110 (https://github.com/pytorch/pytorch/commit/c9aa74a37f6adaa6840696a956d0c7b563f1b9fb) because ``` > /usr/local/bin/buck build //caffe2/test/cpp/lite_interpreter_runtime/... BUILD FAILED The rule //caffe2:backend_interface_libAndroid could not be found. Please check the spelling and whether it is one of the 1866 targets in /data/users/batanasov/fbsource/fbcode/caffe2/TARGETS. (52107 bytes) 1 similar targets in /data/users/batanasov/fbsource/fbcode/caffe2/TARGETS are: //caffe2:backend_interface_lib This error happened while trying to get dependency '//caffe2:backend_interface_libAndroid' of target '//caffe2/test/cpp/lite_interpreter_runtime:test_mobile_profilerAndroid' At //caffe2:backend_interface_libAndroid (ovr_config//platform/linux:x86_64-fbcode) At //caffe2/test/cpp/lite_interpreter_runtime:test_mobile_profilerAndroid (ovr_config//platform/linux:x86_64-fbcode) ``` The add test_mobile_profiler was not meant to be built with Android or other mobile platforms, so we are changing the test to a cpp_unittest Test Plan: ``` buck test //caffe2/test/cpp/lite_interpreter_runtime:test_mobile_profiler Parsing buck files: finished in 0.9 sec Creating action graph: finished in 26.5 sec Downloaded 2/2 artifacts, 1.30 Mbytes, 0.0% cache miss (for updated rules) Building: finished in 16.5 sec (100%) 18451/18451 jobs, 3/18451 updated Total time: 44.0 sec More details at https://www.internalfb.com/intern/buck/build/8bee82c1-66a9-4fae-805f-e4ef5505d25d BUILD SUCCEEDED Tpx test run coordinator for Facebook. See https://fburl.com/tpx for details. Running with tpx session id: 6904f989-5c17-4c5b-9a4f-ffb643dfcc43 Trace available for this run at /tmp/tpx-20220726-114727.001729-6904f989-5c17-4c5b-9a4f-ffb643dfcc43/trace.log RemoteExecution session id: reSessionID-6904f989-5c17-4c5b-9a4f-ffb643dfcc43-tpx Started reporting to test run: https://www.internalfb.com/intern/testinfra/testrun/844425183404951 ✓ ListingSuccess: caffe2/test/cpp/lite_interpreter_runtime:test_mobile_profiler : 3 tests discovered (17.640) ✓ Pass: caffe2/test/cpp/lite_interpreter_runtime:test_mobile_profiler - MobileProfiler.Backend (0.206) ✓ Pass: caffe2/test/cpp/lite_interpreter_runtime:test_mobile_profiler - MobileProfiler.BackendMemoryEvents (0.271) ✓ Pass: caffe2/test/cpp/lite_interpreter_runtime:test_mobile_profiler - MobileProfiler.ModuleHierarchy (0.268) Summary Pass: 3 ListingSuccess: 1 Finished test run: https://www.internalfb.com/intern/testinfra/testrun/844425183404951 ``` Differential Revision: D38166171 Pull Request resolved: https://github.com/pytorch/pytorch/pull/82243 Approved by: https://github.com/salilsdesai --- test/cpp/jit/test_backend_compiler_lib.cpp | 9 +++++ .../test_mobile_profiler.cpp | 37 ++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/test/cpp/jit/test_backend_compiler_lib.cpp b/test/cpp/jit/test_backend_compiler_lib.cpp index 078c405195f28..baa54b0024e45 100644 --- a/test/cpp/jit/test_backend_compiler_lib.cpp +++ b/test/cpp/jit/test_backend_compiler_lib.cpp @@ -145,6 +145,15 @@ class BackendWithCompiler : public PyTorchBackendInterface { auto x_ptr = float_data_ptr(x); auto h_ptr = float_data_ptr(h); auto y_ptr = float_data_ptr(y); +#ifndef NO_PROFILING + RECORD_BACKEND_MEMORY_EVENT_TO_EDGE_PROFILER( + x_ptr, + x.numel() * sizeof(float), + x.numel() * sizeof(float), + x.numel() * sizeof(float) + y.numel() * sizeof(float) + + h.numel() * sizeof(float), + c10::Device(c10::kCPU)); +#endif if (instruction == "aten::add") { y_ptr[0] = x_ptr[0] + h_ptr[0]; } else { diff --git a/test/cpp/lite_interpreter_runtime/test_mobile_profiler.cpp b/test/cpp/lite_interpreter_runtime/test_mobile_profiler.cpp index 95ba2b7b853ed..867b775c1adb4 100644 --- a/test/cpp/lite_interpreter_runtime/test_mobile_profiler.cpp +++ b/test/cpp/lite_interpreter_runtime/test_mobile_profiler.cpp @@ -25,7 +25,9 @@ bool checkMetaData( if (line.find(op_name) != std::string::npos) { while (std::getline(trace_file, line)) { if (line.find(metadata_name) != std::string::npos) { - return (line.find(metadata_val) != std::string::npos); + if (line.find(metadata_val) != std::string::npos) { + return true; + } } } } @@ -122,6 +124,39 @@ TEST(MobileProfiler, Backend) { checkMetaData("aten::add", metadata_name, "test_backend", trace_file)); } +TEST(MobileProfiler, BackendMemoryEvents) { + std::string filePath(__FILE__); + auto testModelFile = filePath.substr(0, filePath.find_last_of("/\\") + 1); + testModelFile.append("test_backend_for_profiling.ptl"); + + std::vector 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); + { + 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)); +} + } // namespace mobile } // namespace jit } // namespace torch