forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set proper scope on nodes added by JIT (pytorch#12400)
Summary: In order to support tensorboardX and other visualization tools, we need to make sure a non-empty scope is set on all nodes added by the JIT. This attempts to do this, but is still a WIP. This is a new version of pytorch#10749 Pull Request resolved: pytorch#12400 Reviewed By: ezyang Differential Revision: D10224380 Pulled By: orionr fbshipit-source-id: d1bccd0eee9ef7c4354112c6a39a5987bfac2994
- Loading branch information
1 parent
cf235e0
commit 046672e
Showing
10 changed files
with
163 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include "ir.h" | ||
|
||
|
||
#include "torch/csrc/jit/operator.h" | ||
#include "torch/csrc/autograd/function.h" | ||
#include "torch/csrc/jit/constants.h" | ||
#include "torch/csrc/jit/assertions.h" | ||
#include "torch/csrc/jit/script/compiler.h" | ||
#include "torch/csrc/jit/passes/pretty_print.h" | ||
|
||
#include <iostream> | ||
#include <unordered_map> | ||
#include <unordered_set> | ||
#include <set> | ||
#include <stack> | ||
#include <sstream> | ||
#include <algorithm> | ||
#include <string> | ||
|
||
namespace torch { namespace jit { | ||
|
||
ScopePtr Scope::push(Symbol name) { | ||
return c10::make_intrusive<Scope>(intrusive_from_this(), name); | ||
} | ||
|
||
ScopePtr Scope::getRoot() { | ||
ScopePtr current = intrusive_from_this(); | ||
while (current->parent_) { | ||
current = current->parent_; | ||
} | ||
return current; | ||
} | ||
|
||
size_t Scope::getDepth() { | ||
size_t d = 1; | ||
ScopePtr current = intrusive_from_this(); | ||
while (current->parent_) { | ||
current = current->parent_; | ||
d += 1; | ||
} | ||
return d; | ||
} | ||
|
||
std::string Scope::namesFromRoot(const std::string& separator) const { | ||
// TODO: I think the answer is we shouldn't have used Symbol here | ||
std::string out = this->name_.toUnqualString(); | ||
if (this->isRoot()) { | ||
return out; | ||
} | ||
ScopePtr parent = this->parent_; | ||
while (!parent->isRoot()) { | ||
// NOLINTNEXTLINE(performance-inefficient-string-concatenation) | ||
out = std::string(parent->name_.toUnqualString()) + separator + out; | ||
parent = parent->parent_; | ||
} | ||
return out; | ||
} | ||
|
||
}} // namespace torch::jit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#pragma once | ||
#include "torch/csrc/jit/interned_strings.h" | ||
#include "torch/csrc/jit/assertions.h" | ||
#include "torch/csrc/WindowsTorchApiMacro.h" | ||
#include "c10/macros/Macros.h" | ||
|
||
#include <memory> | ||
|
||
namespace torch { | ||
namespace jit { | ||
|
||
// Scope is a node of a trie that represents the tree of nested scopes. | ||
// Individual scopes are pushed and popped from Graph, which holds a | ||
// pointer to the current scope. Each Node in Graph holds a pointer | ||
// to the scope that was current when the node was created. | ||
// The trie never needs to shrink, it only grows until it is disposed | ||
// of when Graph is deallocated. Hence, pointers to scopes held by nodes | ||
// will always be valid as long as Graph is alive. | ||
struct Scope; | ||
using ScopePtr = c10::intrusive_ptr<Scope>; | ||
|
||
struct TORCH_API Scope : public c10::intrusive_ptr_target { | ||
private: | ||
ScopePtr parent_; | ||
Symbol name_; | ||
ScopePtr intrusive_from_this() { | ||
c10::raw::intrusive_ptr::incref(this); // we are creating a new pointer | ||
// from a raw `this` pointer | ||
// so we need to bump the refcount | ||
// to account for this ownership | ||
return c10::intrusive_ptr<Scope>::reclaim(this); | ||
} | ||
public: | ||
Scope() { | ||
name_ = Symbol::scope(""); | ||
} | ||
Scope(ScopePtr parent, Symbol name) { | ||
name_ = name; | ||
parent_ = parent; | ||
} | ||
ScopePtr push(Symbol name); | ||
|
||
ScopePtr parent() { | ||
if (!parent_) { | ||
throw std::runtime_error("Cannot get parent from Scope with no parent"); | ||
} | ||
return parent_; | ||
} | ||
bool isRoot() const { | ||
return !parent_; | ||
} | ||
bool isBlank() const { | ||
static const Symbol blank = Symbol::scope(""); | ||
return isRoot() && name() == blank; | ||
} | ||
|
||
ScopePtr getRoot(); | ||
|
||
size_t getDepth(); | ||
|
||
Symbol name() const { | ||
return name_; | ||
} | ||
|
||
std::string namesFromRoot(const std::string& separator="/") const; | ||
}; | ||
|
||
} // namespace jit | ||
} // namespace torch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters