Skip to content

Commit

Permalink
reland "support running test_mobile_profiler with buck1/buck2 and OSS (
Browse files Browse the repository at this point in the history
…#89001)" (#89091)

We modify this to no longer use std::experimental::filesystem::path
and use our own custom type instead.

This reverts commit c53a5ac.
Pull Request resolved: #89091
Approved by: https://github.com/r-barnes, https://github.com/malfet
  • Loading branch information
mikey dagitses authored and pytorchmergebot committed Nov 17, 2022
1 parent e856a4d commit f057a45
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 20 deletions.
41 changes: 41 additions & 0 deletions test/cpp/lite_interpreter_runtime/resources.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

#include <string>

namespace torch {
namespace testing {

namespace detail {
class Path;
}

/// Gets the path to the resource identified by name.
///
/// @param name identifies a resource, relative path starting from the
/// repo root
inline auto getResourcePath(std::string name) -> detail::Path;

// End interface: implementation details follow.

namespace detail {

class Path {
public:
explicit Path(std::string rep) : rep_(std::move(rep)) {}

auto string() const -> std::string const& {
return rep_;
}

private:
std::string rep_;
};

} // namespace detail

inline auto getResourcePath(std::string name) -> detail::Path {
return detail::Path(std::move(name));
}

} // namespace testing
} // namespace torch
34 changes: 14 additions & 20 deletions test/cpp/lite_interpreter_runtime/test_mobile_profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#include <torch/csrc/profiler/events.h>

#include "test/cpp/lite_interpreter_runtime/resources.h"

#ifdef EDGE_PROFILER_USE_KINETO
namespace torch {
namespace jit {
Expand Down Expand Up @@ -42,16 +44,15 @@ bool checkMetaData(
} // namespace

TEST(MobileProfiler, ModuleHierarchy) {
std::string filePath(__FILE__);
auto testModelFile = filePath.substr(0, filePath.find_last_of("/\\") + 1);
testModelFile.append("to_be_profiled_module.ptl");
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);
mobile::Module bc = _load_for_mobile(testModelFile.string());
{
KinetoEdgeCPUProfiler profiler(
bc,
Expand Down Expand Up @@ -95,16 +96,15 @@ TEST(MobileProfiler, ModuleHierarchy) {
}

TEST(MobileProfiler, Backend) {
std::string filePath(__FILE__);
auto testModelFile = filePath.substr(0, filePath.find_last_of("/\\") + 1);
testModelFile.append("test_backend_for_profiling.ptl");
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);
mobile::Module bc = _load_for_mobile(testModelFile.string());
{
KinetoEdgeCPUProfiler profiler(
bc,
Expand All @@ -130,16 +130,15 @@ TEST(MobileProfiler, Backend) {
}

TEST(MobileProfiler, BackendMemoryEvents) {
std::string filePath(__FILE__);
auto testModelFile = filePath.substr(0, filePath.find_last_of("/\\") + 1);
testModelFile.append("test_backend_for_profiling.ptl");
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);
mobile::Module bc = _load_for_mobile(testModelFile.string());
{
mobile::KinetoEdgeCPUProfiler profiler(
bc,
Expand All @@ -163,13 +162,8 @@ TEST(MobileProfiler, BackendMemoryEvents) {
}

TEST(MobileProfiler, ProfilerEvent) {
/*
* TODO: Using __FILE__ is unreliable e.g. it fails to resolve correctly when
* using buck2, works ok with buck1
*/
std::string filePath(__FILE__);
auto testModelFile = filePath.substr(0, filePath.find_last_of("/\\") + 1);
testModelFile.append("test_backend_for_profiling.ptl");
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}));
Expand All @@ -180,7 +174,7 @@ TEST(MobileProfiler, ProfilerEvent) {
torch::profiler::ProfilerPerfEvents.begin(),
torch::profiler::ProfilerPerfEvents.end());

mobile::Module bc = _load_for_mobile(testModelFile);
mobile::Module bc = _load_for_mobile(testModelFile.string());
{
// Bail if something goes wrong here
try {
Expand Down

0 comments on commit f057a45

Please sign in to comment.