mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-06 12:20:52 +01:00
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/72596 debug_pkl file inside of pytorch's .pt file consists of a list of SourceRanges. Each SourceRange points to a Source which is a stack track, filename, and start, end numbers. Those are emitted in debug_pkl file as strings. Since many SourceRange shares the same source, the string for trace can be deduped. The newer format saves a set of unique traces in a tuple, then each SourceRange will save the offset of it's trace w.r.t. position in that tuple. (i.e. manually applying dictionary compression). The above helps with smaller file size. On loading, if we copy each trace to Source as string the runtime memory would still blowup. To mitigate this, we use SourceView directly instead of source which will take the reference of string inside of Deserializer and make that into string_view. This is safe because Deserializer is hold by Unpickler by shared_ptr, and Unpickler is also hold by shared_ptr by another Source object. That Source object will be alive during the model construction. Test Plan: unit test Took original file (312271638_930.predictor.disagg.local); loaded with `torch.jit.load` save again with `torch.jit.save`. Unzip both, look at contents: ``` [qihan@devvm5585.vll0 ~]$ du archive -h 4.0K archive/xl_model_weights 3.7M archive/extra 8.0K archive/code/__torch__/caffe2/torch/fb/model_transform/splitting 8.0K archive/code/__torch__/caffe2/torch/fb/model_transform 8.0K archive/code/__torch__/caffe2/torch/fb 8.0K archive/code/__torch__/caffe2/torch 8.0K archive/code/__torch__/caffe2 20M archive/code/__torch__/torch/fx/graph_module 20M archive/code/__torch__/torch/fx 8.0K archive/code/__torch__/torch/classes 20M archive/code/__torch__/torch 20M archive/code/__torch__ 20M archive/code 2.7M archive/constants 35M archive [qihan@devvm5585.vll0 ~]$ du resaved -h 4.0K resaved/extra 8.0K resaved/code/__torch__/caffe2/torch/fb/model_transform/splitting 8.0K resaved/code/__torch__/caffe2/torch/fb/model_transform 8.0K resaved/code/__torch__/caffe2/torch/fb 8.0K resaved/code/__torch__/caffe2/torch 8.0K resaved/code/__torch__/caffe2 1.3M resaved/code/__torch__/torch/fx/graph_module 1.3M resaved/code/__torch__/torch/fx 8.0K resaved/code/__torch__/torch/classes 1.4M resaved/code/__torch__/torch 1.4M resaved/code/__torch__ 1.4M resaved/code 2.7M resaved/constants 13M resaved [qihan@devvm5585.vll0 ~]$ ``` Reviewed By: JasonHanwen Differential Revision: D33994011 fbshipit-source-id: 8e6224c6e942e91c3403f686c8f0937d1002ed41 (cherry picked from commit a7014dd4029308c95007f362a57c31796d686647)
178 lines
4.8 KiB
C++
178 lines
4.8 KiB
C++
#include <torch/csrc/jit/runtime/script_profile.h>
|
|
|
|
#include <atomic>
|
|
#include <chrono>
|
|
#include <mutex>
|
|
#include <unordered_set>
|
|
|
|
#include <c10/util/Exception.h>
|
|
#include <c10/util/intrusive_ptr.h>
|
|
#include <torch/csrc/jit/api/function_impl.h>
|
|
|
|
namespace torch {
|
|
namespace jit {
|
|
|
|
namespace {
|
|
|
|
class ProfilesRegistry {
|
|
public:
|
|
bool empty() {
|
|
return empty_.load(std::memory_order_relaxed);
|
|
}
|
|
|
|
void addProfile(ScriptProfile& p) {
|
|
std::lock_guard<std::mutex> g(mutex_);
|
|
enabledProfiles_.emplace(&p);
|
|
empty_.store(false, std::memory_order_relaxed);
|
|
}
|
|
|
|
void removeProfile(ScriptProfile& p) {
|
|
std::lock_guard<std::mutex> g(mutex_);
|
|
enabledProfiles_.erase(&p);
|
|
if (enabledProfiles_.empty()) {
|
|
empty_.store(true, std::memory_order_relaxed);
|
|
}
|
|
}
|
|
|
|
void send(std::unique_ptr<profiling::Datapoint> datapoint) {
|
|
auto shared = std::shared_ptr<profiling::Datapoint>(std::move(datapoint));
|
|
std::lock_guard<std::mutex> g(mutex_);
|
|
for (auto* p : enabledProfiles_) {
|
|
p->addDatapoint(shared);
|
|
}
|
|
}
|
|
|
|
private:
|
|
std::atomic<bool> empty_{true};
|
|
std::mutex mutex_;
|
|
std::unordered_set<ScriptProfile*> enabledProfiles_;
|
|
};
|
|
|
|
ProfilesRegistry& getProfilesRegistry() {
|
|
static auto registry = std::ref(*new ProfilesRegistry{});
|
|
return registry;
|
|
}
|
|
|
|
auto initBindings() {
|
|
torch::class_<SourceRef>("profiling", "SourceRef")
|
|
.def(
|
|
"starting_lineno",
|
|
[](const c10::intrusive_ptr<SourceRef>& self) {
|
|
return static_cast<int64_t>((*self)->starting_line_no());
|
|
})
|
|
.def("text", [](const c10::intrusive_ptr<SourceRef>& self) {
|
|
return (*self)->text_str().str();
|
|
});
|
|
|
|
torch::class_<InstructionStats>("profiling", "InstructionStats")
|
|
.def(
|
|
"count",
|
|
[](const c10::intrusive_ptr<InstructionStats>& self) {
|
|
return self->count;
|
|
})
|
|
.def("duration_ns", [](const c10::intrusive_ptr<InstructionStats>& self) {
|
|
return static_cast<int64_t>(self->duration.count());
|
|
});
|
|
|
|
torch::class_<SourceStats>("profiling", "SourceStats")
|
|
.def(
|
|
"source",
|
|
[](const c10::intrusive_ptr<SourceStats>& self) {
|
|
return c10::make_intrusive<SourceRef>(self->getSourceRef());
|
|
})
|
|
.def("line_map", &SourceStats::getLineMap);
|
|
|
|
torch::class_<ScriptProfile>("profiling", "_ScriptProfile")
|
|
.def(torch::init<>())
|
|
.def("enable", &ScriptProfile::enable)
|
|
.def("disable", &ScriptProfile::disable)
|
|
.def("_dump_stats", [](const c10::intrusive_ptr<ScriptProfile>& self) {
|
|
const auto& stats = self->dumpStats();
|
|
c10::List<c10::intrusive_ptr<SourceStats>> ret;
|
|
for (const auto& source : stats) {
|
|
SourceStats::LineMap lineMap;
|
|
for (const auto& line : source.second) {
|
|
lineMap.insert(
|
|
line.first, c10::make_intrusive<InstructionStats>(line.second));
|
|
}
|
|
ret.push_back(c10::make_intrusive<SourceStats>(
|
|
source.first, std::move(lineMap)));
|
|
}
|
|
return ret;
|
|
});
|
|
return nullptr;
|
|
}
|
|
|
|
const auto C10_UNUSED torchBindInitializer = initBindings();
|
|
|
|
} // namespace
|
|
|
|
namespace profiling {
|
|
|
|
InstructionSpan::InstructionSpan(Node& node) {
|
|
if (getProfilesRegistry().empty()) {
|
|
return;
|
|
}
|
|
|
|
datapoint_ = std::make_unique<Datapoint>(node.sourceRange());
|
|
}
|
|
|
|
InstructionSpan::~InstructionSpan() {
|
|
if (!datapoint_) {
|
|
return;
|
|
}
|
|
|
|
datapoint_->end = std::chrono::steady_clock::now();
|
|
getProfilesRegistry().send(std::move(datapoint_));
|
|
}
|
|
|
|
} // namespace profiling
|
|
|
|
void ScriptProfile::enable() {
|
|
if (!std::exchange(enabled_, true)) {
|
|
getProfilesRegistry().addProfile(*this);
|
|
}
|
|
}
|
|
|
|
void ScriptProfile::disable() {
|
|
if (std::exchange(enabled_, false)) {
|
|
getProfilesRegistry().removeProfile(*this);
|
|
}
|
|
}
|
|
|
|
void ScriptProfile::addDatapoint(
|
|
std::shared_ptr<profiling::Datapoint> datapoint) {
|
|
TORCH_CHECK(enabled_, "Cannot only add datapoint to disabled profilers.");
|
|
datapoints_.push_back(std::move(datapoint));
|
|
}
|
|
|
|
const ScriptProfile::SourceMap& ScriptProfile::dumpStats() {
|
|
TORCH_CHECK(!enabled_, "Only disabled profilers are allowed to dump stats.");
|
|
|
|
for (const auto& datapoint : datapoints_) {
|
|
if (const auto& source = datapoint->sourceRange.source()) {
|
|
if (auto fileLineCol = datapoint->sourceRange.file_line_col()) {
|
|
auto it = sourceMap_.find(*source.get());
|
|
if (it == sourceMap_.end()) {
|
|
it = sourceMap_.emplace(SourceRef{source}, LineMap{}).first;
|
|
}
|
|
auto& stats = it->second[std::get<1>(*fileLineCol)];
|
|
stats.count++;
|
|
stats.duration += datapoint->end - datapoint->start;
|
|
}
|
|
}
|
|
}
|
|
datapoints_.clear();
|
|
|
|
return sourceMap_;
|
|
}
|
|
|
|
ScriptProfile::~ScriptProfile() {
|
|
if (enabled_) {
|
|
getProfilesRegistry().removeProfile(*this);
|
|
}
|
|
}
|
|
|
|
} // namespace jit
|
|
} // namespace torch
|