8000 [DEBUG] REmove has CUDA by mzzchy · Pull Request #153349 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

[DEBUG] REmove has CUDA #153349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion aten/src/ATen/detail/MTIAHooksInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ struct TORCH_API MTIAHooksInterface : AcceleratorHooksInterface {
FAIL_MTIAHOOKS_FUNC(__func__);
}

virtual PyObject* memorySnapshot() const {
virtual PyObject* memorySnapshot(const std::optional<std::string>& local_path) const {
FAIL_MTIAHOOKS_FUNC(__func__);
return nullptr;
}
Expand Down
2 changes: 2 additions & 0 deletions buckbuild.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ THIRD_PARTY_LIBS = {
"ruy": ["//third-party/ruy:ruy_xplat_lib", "//third_party:ruy_lib"],
"sleef_arm": ["//third-party/sleef:sleef_arm", "//third_party:sleef_arm"],
"typing-extensions": ["//third-party/typing-extensions:typing-extensions", "//third_party:typing-extensions"],
"nlohmann-json": ["fbsource//third-party/nlohmann-json:nlohmann-json", "//third_party:nlohmann-json"],
}

def third_party(name):
Expand Down Expand Up @@ -1736,6 +1737,7 @@ def define_buck_targets(
deps = [
third_party("glog"),
third_party("kineto"),
third_party("nlohmann-json"),
],
exported_deps = [
":aten_cpu",
Expand Down
1 change: 1 addition & 0 deletions build_variables.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ libtorch_profiler_sources = [
"torch/csrc/profiler/collection.cpp",
"torch/csrc/profiler/data_flow.cpp",
"torch/csrc/profiler/kineto_shim.cpp",
"torch/csrc/mtia/profiler/MTIAMemoryProfiler.cpp",
"torch/csrc/profiler/kineto_client_interface.cpp",
"torch/csrc/profiler/orchestration/observer.cpp",
"torch/csrc/profiler/orchestration/python_tracer.cpp",
Expand Down
4 changes: 2 additions & 2 deletions torch/csrc/autograd/profiler_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,7 @@ class PythonMemoryTracer final : public python_tracer::PythonMemoryTracerBase {
~PythonMemoryTracer() override = default;
void start() override;
void stop() override;
void export_memory_history(const std::string path) override;
void export_memory_history(const std::string& path) override;
};

static void toggle_memory_tracing(bool enable) {
Expand Down Expand Up @@ -1197,7 +1197,7 @@ void PythonMemoryTracer::start() {
toggle_memory_tracing(true);
}

void PythonMemoryTracer::export_memory_history(const std::string path) {
void PythonMemoryTracer::export_memory_history(const std::string& path) {
PyGILState_STATE gil_state = PyGILState_Ensure();
THPObjectPtr torch_cuda_memory_module(
PyImport_ImportModule("torch.cuda.memory"));
Expand Down
3 changes: 2 additions & 1 deletion torch/csrc/mtia/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ void initModule(PyObject* module) {
});

m.def("_mtia_memorySnapshot", []() {
PyObject* raw_pyobject = at::detail::getMTIAHooks().memorySnapshot();
PyObject* raw_pyobject =
at::detail::getMTIAHooks().memorySnapshot(std::nullopt);
return py::reinterpret_steal<py::object>(raw_pyobject);
});

Expand Down
35 changes: 35 additions & 0 deletions torch/csrc/mtia/profiler/MTIAMemoryProfiler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <ATen/Context.h>
#include <ATen/detail/MTIAHooksInterface.h>
#include <nlohmann/json.hpp>
#include <torch/csrc/mtia/profiler/MTIAMemoryProfiler.h>

using json = nlohmann::json;

namespace torch::mtia {

void MTIAMemoryProfiler::start() {
at::detail::getMTIAHooks().recordMemoryHistory("all", "all", 150000);
}

void MTIAMemoryProfiler::export_memory_history(const std::string& path) {
at::detail::getMTIAHooks().memorySnapshot(path);
return;
}

void MTIAMemoryProfiler::stop() {
at::detail::getMTIAHooks().recordMemoryHistory(std::nullopt, "all", 0);
}

std::unique_ptr<torch::profiler::impl::python_tracer::PythonMemoryTracerBase>
getMemoryTracer() {
return std::make_unique<MTIAMemoryProfiler>();
}

void initMemoryProfiler() {
if (at::detail::isMTIAHooksBuilt()) {
fprintf(stderr, "Initializing MTIA Memory Tracer\n");
torch::profiler::impl::python_tracer::registerMemoryTracer(
&getMemoryTracer);
}
}
} // namespace torch::mtia
20 changes: 20 additions & 0 deletions torch/csrc/mtia/profiler/MTIAMemoryProfiler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once
#include <torch/csrc/profiler/orchestration/python_tracer.h>

namespace torch::mtia {
using namespace torch::profiler::impl::python_tracer;

void initMemoryProfiler();

std::unique_ptr<PythonMemoryTracerBase> getMemoryTracer();

class MTIAMemoryProfiler final : public PythonMemoryTracerBase {
public:
explicit MTIAMemoryProfiler() = default;
~MTIAMemoryProfiler() override = default;
void start() override;
void stop() override;
void export_memory_history(const std::string& path) override;
};

} // namespace torch::mtia
5 changes: 4 additions & 1 deletion torch/csrc/profiler/kineto_client_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <ATen/Context.h>
#include <libkineto.h>
#include <torch/csrc/autograd/profiler_kineto.h>
#include <torch/csrc/mtia/profiler/MTIAMemoryProfiler.h>
#include <torch/csrc/profiler/kineto_client_interface.h>
#include <chrono>
#include <thread>
Expand All @@ -23,7 +24,9 @@ using namespace torch::autograd::profiler;

class LibKinetoClient : public libkineto::ClientInterface {
public:
void init() override {}
void init() override {
::torch::mtia::initMemoryProfiler();
}

void prepare(
bool report_input_shapes = false,
Expand Down
2 changes: 1 addition & 1 deletion torch/csrc/profiler/orchestration/python_tracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct NoOpMemoryPythonTracer : public PythonMemoryTracerBase {
~NoOpMemoryPythonTracer() override = default;
void start() override {}
void stop() override {}
void export_memory_history(const std::string path) override {}
void export_memory_history(const std::string&) override {}
};

} // namespace
Expand Down
2 changes: 1 addition & 1 deletion torch/csrc/profiler/orchestration/python_tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ struct TORCH_API PythonMemoryTracerBase {

virtual void start() = 0;
virtual void stop() = 0;
virtual void export_memory_history(const std::string path) = 0;
virtual void export_memory_history(const std::string& path) = 0;
};

using MakeMemoryFn = std::unique_ptr<PythonMemoryTracerBase> (*)();
Expand Down
43 changes: 43 additions & 0 deletions torch/csrc/profiler/python/combined_traceback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,49 @@ struct PythonTraceback : public CapturedTraceback::Python {

} // namespace

std::vector<nlohmann::json> json_symbolize(
std::vector<CapturedTraceback*>& to_symbolize) {
std::unordered_map<CapturedTraceback*, uint64_t> cached_frames;
std::vector<CapturedTraceback*> unique_frames;
for (const auto& sc : to_symbolize) {
auto it = cached_frames.find(sc);
if (it == cached_frames.end()) {
cached_frames.insert({sc, unique_frames.size()});
unique_frames.push_back(sc);
}
}
auto s = symbolize(unique_frames);

std::string line_s = "line";
std::string name_s = "name";
std::string filename_s = "filename";
std::vector<nlohmann::json> all_frames;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a reserve here to preallocate length


for (const auto& f : s.all_frames) {
nlohmann::json d;
d[name_s] = f.funcname;
d[filename_s] = f.filename;
d[line_s] = f.lineno;
all_frames.emplace_back(std::move(d));
}

std::vector<nlohmann::json> py_unique_frames;
for (const auto& t : s.tracebacks) {
nlohmann::json l;
for (const auto& e : t) {
l.emplace_back(all_frames.at(e));
}
py_unique_frames.push_back(std::move(l));
}

std::vector<nlohmann::json> result;
result.reserve(to_symbolize.size());
for (const auto& sc : to_symbolize) {
result.push_back(py_unique_frames.at(cached_frames.at(sc)));
}
return result;
}

std::vector<py::object> py_symbolize(
std::vector<CapturedTraceback*>& to_symbolize) {
// we dedup repeated to_symbolize objects to prevent
Expand Down
5 changes: 5 additions & 0 deletions torch/csrc/profiler/python/combined_traceback.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <torch/csrc/profiler/combined_traceback.h>

#include <nlohmann/json.hpp>
#include <pybind11/pybind11.h>
#include <torch/csrc/utils/pybind.h>

Expand All @@ -14,6 +15,10 @@ namespace torch {
TORCH_API std::vector<pybind11::object> py_symbolize(
std::vector<CapturedTraceback*>& to_symbolize);

// Return the callback in json format so that it can be used within cpp
TORCH_API std::vector<nlohmann::json> json_symbolize(
std::vector<CapturedTraceback*>& to_symbolize);

// requires GIL to be held, frees any pending free frames
TORCH_PYTHON_API void freeDeadCapturedTracebackFrames();

Expand Down
Loading
0