mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 12:21:27 +01:00
Summary:
This is an automatic change generated by the following script:
```
#!/usr/bin/env python3
from subprocess import check_output, check_call
import os
def get_compiled_files_list():
import json
with open("build/compile_commands.json") as f:
data = json.load(f)
files = [os.path.relpath(node['file']) for node in data]
for idx, fname in enumerate(files):
if fname.startswith('build/') and fname.endswith('.DEFAULT.cpp'):
files[idx] = fname[len('build/'):-len('.DEFAULT.cpp')]
return files
def run_clang_tidy(fname):
check_call(["python3", "tools/clang_tidy.py", "-c", "build", "-x", fname,"-s"])
changes = check_output(["git", "ls-files", "-m"])
if len(changes) == 0:
return
check_call(["git", "commit","--all", "-m", f"NOLINT stubs for {fname}"])
def main():
git_files = check_output(["git", "ls-files"]).decode("ascii").split("\n")
compiled_files = get_compiled_files_list()
for idx, fname in enumerate(git_files):
if fname not in compiled_files:
continue
if fname.startswith("caffe2/contrib/aten/"):
continue
print(f"[{idx}/{len(git_files)}] Processing {fname}")
run_clang_tidy(fname)
if __name__ == "__main__":
main()
```
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56892
Reviewed By: H-Huang
Differential Revision: D27991944
Pulled By: malfet
fbshipit-source-id: 5415e1eb2c1b34319a4f03024bfaa087007d7179
100 lines
3.1 KiB
C++
100 lines
3.1 KiB
C++
#include <torch/csrc/autograd/function.h>
|
|
|
|
#include <torch/csrc/autograd/engine.h>
|
|
#include <torch/csrc/autograd/variable.h>
|
|
|
|
#include <ATen/ATen.h>
|
|
|
|
#include <algorithm>
|
|
#include <cstdint>
|
|
#include <memory>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace torch { namespace autograd {
|
|
|
|
// The current evaluating node. This is useful to assign the current node as a
|
|
// parent of new nodes created during the evaluation of this node in anomaly
|
|
// mode.
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
|
static thread_local std::shared_ptr<Node> current_evaluating_node = nullptr;
|
|
|
|
NodeGuard::NodeGuard(std::shared_ptr<Node> node) {
|
|
last_evaluating_node_ = std::move(current_evaluating_node);
|
|
current_evaluating_node = std::move(node);
|
|
}
|
|
NodeGuard::~NodeGuard() {
|
|
// restore the previous evaluating node
|
|
current_evaluating_node = std::move(last_evaluating_node_);
|
|
}
|
|
|
|
void Node::assign_parent() {
|
|
metadata()->assign_parent(current_evaluating_node);
|
|
}
|
|
|
|
auto Node::name() const -> std::string {
|
|
return c10::demangle(typeid(*this).name());
|
|
}
|
|
|
|
AnomalyMetadata* Node::metadata() noexcept {
|
|
if (!anomaly_metadata_) {
|
|
anomaly_metadata_ = Engine::get_default_engine().make_anomaly_metadata();
|
|
}
|
|
return anomaly_metadata_.get();
|
|
}
|
|
|
|
static void gatherFunctions(
|
|
Node* func,
|
|
std::vector<std::shared_ptr<Node>>& stack) {
|
|
func->release_variables();
|
|
|
|
for (auto& edge : func->next_edges()) {
|
|
if (edge.function.use_count() == 1) {
|
|
stack.emplace_back(std::move(edge.function));
|
|
} else {
|
|
edge.function.reset();
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Fix for #5534: prevent stack overflow on deletion of deep computation graph
|
|
*
|
|
* Sometimes one can end up with a very big computation graph of Nodes
|
|
* and Edges. Each std::shared_ptr<Node> contains a list of Edge, and
|
|
* each Edge contains a std::shared_ptr<Node>. Deleting a
|
|
* std::shared_ptr<Node> can trigger the recursive deletion of other
|
|
* std::shared_ptr<Node>'s: this can stack overflow if the graph
|
|
* is deep enough. Here is an example of such a graph:
|
|
*
|
|
* shared_ptr<Node> -> Edge -> shared_ptr<Node> -> Edge -> ... -> shared_ptr<Node>
|
|
*
|
|
* The solution here is to detect when we are decrementing away the last
|
|
* reference to a Node, and when doing so to buffer up the Node's
|
|
* that will be recursively decremented. We can then decrement (and free)
|
|
* the original Node without causing a recursive cascade, before
|
|
* draining the buffer applying the same behavior. This is, in effect,
|
|
* converting recursion to a loop, using a heap buffer in place of the
|
|
* recursive call stack.
|
|
*/
|
|
void deleteNode(Node* function) {
|
|
// To avoid stack overflow on large computational graphs,
|
|
// we need to track reference decrementing and freeing
|
|
// on the heap.
|
|
function->release_variables();
|
|
std::vector<std::shared_ptr<Node>> stack;
|
|
gatherFunctions(function, stack);
|
|
delete function;
|
|
|
|
while (!stack.empty()) {
|
|
auto func = std::move(stack.back());
|
|
stack.pop_back();
|
|
gatherFunctions(func.get(), stack);
|
|
// Reference count is decremented on the loop backedge.
|
|
}
|
|
}
|
|
|
|
}} // namespace torch::autograd
|