mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
[Mem Snapshot] Add Metadata Field (#165490)
Summary: The implementation adds the ability to: Set custom metadata strings that will be attached to all subsequent allocations Clear or change the metadata at any point View the metadata in memory snapshots via _dump_snapshot() Test Plan: Added test in test_cuda.py and check manually in snapshot to see that metadata was added. Differential Revision: D84654933 Pull Request resolved: https://github.com/pytorch/pytorch/pull/165490 Approved by: https://github.com/yushangdi
This commit is contained in:
committed by
PyTorch MergeBot
parent
69c33898fa
commit
a25a649e70
@ -2081,6 +2081,8 @@ def _cuda_hostMemoryStats() -> dict[str, Any]: ...
|
||||
def _cuda_resetAccumulatedHostMemoryStats() -> None: ...
|
||||
def _cuda_resetPeakHostMemoryStats() -> None: ...
|
||||
def _cuda_memorySnapshot(mempool_id: tuple[_int, _int] | None) -> dict[str, Any]: ...
|
||||
def _cuda_setMemoryMetadata(metadata: str) -> None: ...
|
||||
def _cuda_getMemoryMetadata() -> str: ...
|
||||
def _cuda_record_memory_history_legacy(
|
||||
enabled: _bool,
|
||||
record_context: _bool,
|
||||
|
@ -765,6 +765,7 @@ PyObject* THCPModule_memorySnapshot(PyObject* _unused, PyObject* arg) {
|
||||
py::str frames_s = "frames";
|
||||
py::str time_us_s = "time_us";
|
||||
py::str compile_context_s = "compile_context";
|
||||
py::str user_metadata_s = "user_metadata";
|
||||
|
||||
py::list empty_frames;
|
||||
std::vector<CapturedTraceback*> to_gather_frames;
|
||||
@ -882,6 +883,7 @@ PyObject* THCPModule_memorySnapshot(PyObject* _unused, PyObject* arg) {
|
||||
trace_entry[stream_s] = int64_t(te.stream_);
|
||||
trace_entry[time_us_s] = te.time_.t_;
|
||||
trace_entry[compile_context_s] = te.compile_context_;
|
||||
trace_entry[user_metadata_s] = te.user_metadata_;
|
||||
trace.append(trace_entry);
|
||||
}
|
||||
traces.append(trace);
|
||||
@ -1137,6 +1139,14 @@ static void registerCudaDeviceProperties(PyObject* module) {
|
||||
return c10::cuda::CUDACachingAllocator::isHistoryEnabled();
|
||||
});
|
||||
|
||||
m.def("_cuda_setMemoryMetadata", [](const std::string& metadata) {
|
||||
c10::cuda::CUDACachingAllocator::setUserMetadata(metadata);
|
||||
});
|
||||
|
||||
m.def("_cuda_getMemoryMetadata", []() {
|
||||
return c10::cuda::CUDACachingAllocator::getUserMetadata();
|
||||
});
|
||||
|
||||
m.def("_cuda_get_conv_benchmark_empty_cache", []() {
|
||||
return at::native::_cudnn_get_conv_benchmark_empty_cache();
|
||||
});
|
||||
|
@ -311,6 +311,7 @@ std::string _memory_snapshot_pickled() {
|
||||
IValue is_expandable_s = "is_expandable";
|
||||
IValue time_us_s = "time_us";
|
||||
IValue compile_contexts_s = "compile_context";
|
||||
IValue user_metadata_s = "user_metadata";
|
||||
|
||||
auto empty_frames = new_list();
|
||||
|
||||
@ -428,6 +429,7 @@ std::string _memory_snapshot_pickled() {
|
||||
trace_entry.insert(size_s, (int64_t)te.size_);
|
||||
trace_entry.insert(stream_s, int64_t(te.stream_));
|
||||
trace_entry.insert(compile_contexts_s, te.compile_context_);
|
||||
trace_entry.insert(user_metadata_s, te.user_metadata_);
|
||||
if (te.context_) {
|
||||
auto sc = getFromContext(te.context_);
|
||||
frame_tracebacks.push_back(sc);
|
||||
|
@ -1063,6 +1063,30 @@ def _dump_snapshot(filename="dump_snapshot.pickle"):
|
||||
pickle.dump(s, f)
|
||||
|
||||
|
||||
def _set_memory_metadata(metadata: str):
|
||||
"""
|
||||
Set custom metadata that will be attached to all subsequent CUDA memory allocations.
|
||||
|
||||
This metadata will be recorded in the memory snapshot for all allocations made
|
||||
after this call until the metadata is cleared or changed.
|
||||
|
||||
Args:
|
||||
metadata (str): Custom metadata string to attach to allocations.
|
||||
Pass an empty string to clear the metadata.
|
||||
"""
|
||||
torch._C._cuda_setMemoryMetadata(metadata)
|
||||
|
||||
|
||||
def _get_memory_metadata() -> str:
|
||||
"""
|
||||
Get the current custom metadata that is being attached to CUDA memory allocations.
|
||||
|
||||
Returns:
|
||||
str: The current metadata string, or empty string if no metadata is set.
|
||||
"""
|
||||
return torch._C._cuda_getMemoryMetadata()
|
||||
|
||||
|
||||
def _save_segment_usage(filename="output.svg", snapshot=None):
|
||||
if snapshot is None:
|
||||
snapshot = _snapshot()
|
||||
|
Reference in New Issue
Block a user