1
#include <torch/csrc/jit/ir/scope.h>
3
#include <ATen/core/class_type.h>
4
#include <ATen/core/function.h>
10
std::string get_module_info(const ModuleInstanceInfo& module_instance_info) {
11
std::string module_info;
12
const auto& class_type = module_instance_info.class_type();
13
std::string instance_name = module_instance_info.instance_name();
14
std::string type_name;
16
type_name += class_type->name()->qualifiedName();
17
type_name = type_name.substr(type_name.find_last_of('.') + 1);
19
if (type_name.empty()) {
20
type_name = "UNKNOWN_TYPE";
22
if (instance_name.empty()) {
23
instance_name = "UNKNOWN_INSTANCE";
25
module_info.append(instance_name).append("(").append(type_name).append(")");
30
ScopePtr Scope::intrusive_from_this() {
31
c10::raw::intrusive_ptr::incref(this);
35
return c10::intrusive_ptr<Scope>::reclaim(this);
38
Scope::Scope() : name_(Symbol::scope("")) {}
40
Scope::Scope(ScopePtr parent, Symbol name)
41
: parent_(std::move(parent)), name_(name) {}
43
ScopePtr Scope::push(Symbol name) {
44
return c10::make_intrusive<Scope>(intrusive_from_this(), name);
47
ScopePtr Scope::parent() {
49
throw std::runtime_error("Cannot get parent from Scope with no parent");
54
bool Scope::isRoot() const {
58
bool Scope::isBlank() const {
59
static const Symbol blank = Symbol::scope("");
60
return isRoot() && name() == blank;
63
ScopePtr Scope::getRoot() {
64
ScopePtr current = intrusive_from_this();
65
while (current->parent_) {
66
current = current->parent_;
71
size_t Scope::getDepth() {
73
ScopePtr current = intrusive_from_this();
74
while (current->parent_) {
75
current = current->parent_;
81
Symbol Scope::name() const {
85
std::string Scope::namesFromRoot(const std::string& separator) const {
87
std::string out = this->name_.toUnqualString();
91
ScopePtr parent = this->parent_;
92
while (!parent->isRoot()) {
94
out = std::string(parent->name_.toUnqualString()) + separator + out;
95
parent = parent->parent_;
100
InlinedCallStackPtr InlinedCallStack::intrusive_from_this() {
101
c10::raw::intrusive_ptr::incref(this);
105
return c10::intrusive_ptr<InlinedCallStack>::reclaim(this);
108
InlinedCallStack::InlinedCallStack(Function* fn, SourceRange source_range)
110
fn_name_(fn_ ? fn_->name() : ""),
111
source_range_(std::move(source_range)) {}
113
InlinedCallStack::InlinedCallStack(
115
SourceRange source_range,
116
c10::optional<ModuleInstanceInfo> module_instance_info)
118
fn_name_(fn_ ? fn_->name() : ""),
119
source_range_(std::move(source_range)),
120
module_instance_info_(std::move(module_instance_info)) {}
122
InlinedCallStack::InlinedCallStack(
124
SourceRange source_range,
125
c10::optional<ModuleInstanceInfo> module_instance_info,
126
std::string& function_name)
128
fn_name_(std::move(function_name)),
129
source_range_(std::move(source_range)),
130
module_instance_info_(std::move(module_instance_info)) {}
132
InlinedCallStack::InlinedCallStack(
133
InlinedCallStackPtr callee,
135
SourceRange source_range)
136
: callee_(std::move(callee)),
138
fn_name_(fn_ ? fn_->name() : ""),
139
source_range_(std::move(source_range)) {}
141
InlinedCallStack::InlinedCallStack(
142
InlinedCallStackPtr callee,
144
SourceRange source_range,
145
c10::optional<ModuleInstanceInfo> module_instance_info,
146
std::string& function_name)
147
: callee_(std::move(callee)),
149
fn_name_(std::move(function_name)),
150
source_range_(std::move(source_range)),
151
module_instance_info_(std::move(module_instance_info)) {}
153
InlinedCallStack::InlinedCallStack(
154
InlinedCallStackPtr callee,
156
SourceRange source_range,
157
c10::optional<ModuleInstanceInfo> module_instance_info)
158
: callee_(std::move(callee)),
160
fn_name_(fn_ ? fn_->name() : ""),
161
source_range_(std::move(source_range)),
162
module_instance_info_(std::move(module_instance_info)) {}
164
c10::optional<InlinedCallStackPtr> InlinedCallStack::callee() const {
168
void InlinedCallStack::setCallee(c10::optional<InlinedCallStackPtr> callee) {
169
callee_ = std::move(callee);
172
c10::optional<ModuleInstanceInfo> InlinedCallStack::module_instance() const {
173
return module_instance_info_;
176
SourceRange InlinedCallStack::source_range() const {
177
return source_range_;
180
Function* InlinedCallStack::function() const {
184
const std::string& InlinedCallStack::function_name() const {
188
std::vector<InlinedCallStackEntry> InlinedCallStack::vec() {
189
std::vector<InlinedCallStackEntry> r;
190
c10::optional<InlinedCallStackPtr> current = intrusive_from_this();
194
(*current)->source_range_,
195
(*current)->module_instance_info_);
196
current = (*current)->callee_;
201
ModuleInstanceInfo::ModuleInstanceInfo(
202
c10::ClassTypePtr module_type,
203
std::string instance_name)
204
: module_type_(std::move(module_type)),
205
instance_name_(std::move(instance_name)) {}