forked from ROCm/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscope.h
69 lines (59 loc) · 1.9 KB
/
scope.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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