1
#include <torch/csrc/jit/mobile/module.h>
3
#include <torch/csrc/jit/backends/backend_exception.h>
4
#include <torch/csrc/jit/mobile/interpreter.h>
5
#include <torch/csrc/jit/mobile/observer.h>
6
#include <torch/csrc/jit/mobile/type_parser.h>
7
#include <torch/csrc/jit/runtime/jit_exception.h>
10
#include <ATen/record_function.h>
11
#include <c10/util/ScopeExit.h>
12
#include <c10/util/irange.h>
16
std::ostream& operator<<(std::ostream& out, Instruction inst);
19
void CompilationUnit::register_function(std::unique_ptr<Function> fn) {
20
methods_.emplace_back(std::move(fn));
23
const Function* CompilationUnit::find_function(
24
const c10::QualifiedName& qn) const {
25
for (auto& fn : methods_) {
26
if (fn->qualname() == qn) {
33
Function* CompilationUnit::find_function(const c10::QualifiedName& qn) {
35
return const_cast<Function*>(
36
static_cast<const CompilationUnit*>(this)->find_function(qn));
39
Method Module::get_method(const std::string& name) const {
40
if (auto method = find_method(name)) {
43
AT_ERROR("Method '", name, "' is not defined.");
46
bool Module::compareMethodSchemas(
47
const std::string& name_1,
48
const std::string& name_2) {
49
c10::optional<c10::FunctionSchema> schema_1, schema_2;
50
for (const auto& fn : cu_->methods()) {
51
if (fn->name() == name_1) {
52
schema_1 = fn->getSchema();
54
if (fn->name() == name_2) {
55
schema_2 = fn->getSchema();
58
if (schema_1.has_value() && schema_2.has_value()) {
59
return (schema_1 == schema_2);
64
void Module::unsafeRemoveMethod(const std::string& basename) {
66
for (; i < static_cast<int64_t>(cu_->methods().size()); ++i) {
67
if ((cu_->methods()[i])->name() == basename) {
71
object_->type()->unsafeRemoveMethod(basename);
72
cu_->unsafeRemoveFunction(i);
75
void Module::unsafeCopyMethod(
76
const std::string& new_method_name,
77
const Function& to_be_copied) {
79
!find_method(new_method_name).has_value(),
80
"Trying to replace existing method.");
81
const c10::QualifiedName& tobe_copied_name = to_be_copied.qualname();
82
c10::QualifiedName qualified_method_name(
83
tobe_copied_name.prefix(), new_method_name);
84
std::unique_ptr<Function> new_fn = std::make_unique<Function>(
85
qualified_method_name, to_be_copied.get_code(), to_be_copied.getSchema());
86
object_->type()->addMethod(new_fn.get());
87
cu_->register_function(std::move(new_fn));
90
c10::optional<Method> Module::find_method(const std::string& basename) const {
91
for (const auto& fn : cu_->methods()) {
92
if (fn->name() == basename) {
93
return c10::make_optional<Method>(Method(this, fn.get()));
104
void set_train_recurse(
105
const c10::intrusive_ptr<c10::ivalue::Object>& obj,
107
if (auto slot = obj->type()->findAttributeSlot("training")) {
108
obj->setSlot(*slot, on);
110
TORCH_INTERNAL_ASSERT(
112
"'training' attribute not found. Did you accidentally "
113
"call .eval() before saving your model?");
115
for (const auto& slot : obj->slots()) {
118
if (slot.isObject() && slot.toObjectRef().type()->is_module()) {
119
set_train_recurse(slot.toObject(), on);
124
void slot_params_recurse(
125
const c10::intrusive_ptr<c10::ivalue::Object>& obj,
126
std::vector<at::Tensor>* params) {
127
for (const auto& slot : obj->slots()) {
128
if (slot.isTensor()) {
129
params->emplace_back(slot.toTensor());
130
} else if (slot.isObject()) {
131
slot_params_recurse(slot.toObject(), params);
136
void slot_named_params_recurse(
137
const c10::intrusive_ptr<c10::ivalue::Object>& obj,
138
std::map<std::string, at::Tensor>* params,
139
const std::string& parent_name) {
140
auto slots = obj->slots();
141
size_t nslots = slots.size();
142
for (const auto i : c10::irange(nslots)) {
143
auto slot = slots[i];
144
std::string name = parent_name.empty() ? parent_name : parent_name + ".";
145
name += obj->type()->getAttributeName(i);
151
if (slot.isTensor() && slot.toTensor().requires_grad()) {
152
(*params)[name] = slot.toTensor();
153
} else if (slot.isObject()) {
154
slot_named_params_recurse(slot.toObject(), params, name);
159
#if defined(SYMBOLICATE_MOBILE_DEBUG_HANDLE)
160
std::string getTopModuleTypeName(const Module& m) {
162
if (m._ivalue()->type() && m._ivalue()->type()->name()) {
163
name = m._ivalue()->type()->name().value().name();
171
const std::vector<at::Tensor> Module::parameters() const {
172
std::vector<at::Tensor> params;
173
slot_params_recurse(object_, ¶ms);
181
const std::map<std::string, at::Tensor> Module::named_parameters() const {
182
std::map<std::string, at::Tensor> params;
183
const std::string name = "";
184
slot_named_params_recurse(object_, ¶ms, name);
188
std::string Module::getModuleHierarchy(const int64_t debug_handle) const {
189
#if defined(SYMBOLICATE_MOBILE_DEBUG_HANDLE)
190
return getDebugTable().getModuleHierarchyInfo(
191
debug_handle, getTopModuleTypeName(*this));
197
std::string Module::getCallStack(const int64_t debug_handle) const {
198
#if defined(SYMBOLICATE_MOBILE_DEBUG_HANDLE)
199
return getDebugTable().getSourceDebugString(
200
debug_handle, getTopModuleTypeName(*this));
211
std::string Module::get_forward_method_debug_info(int64_t debug_handle) const {
212
#if defined(SYMBOLICATE_MOBILE_DEBUG_HANDLE)
213
return getDebugTable().getModuleHierarchyInfo(
214
debug_handle, getTopModuleTypeName(*this));
220
void Module::train(bool on) {
221
set_train_recurse(object_, on);
224
bool Module::is_training() const {
225
if (auto slot = object_->type()->findAttributeSlot("training")) {
226
return object_->getSlot(*slot).toBool();
231
const std::vector<Method> Module::get_methods() const {
232
std::vector<Method> methods;
233
for (std::unique_ptr<Function>& fn : cu_->methods()) {
234
methods.emplace_back(this, fn.get());
239
Method::Method(const Module* owner, Function* function)
240
: owner_(owner), function_(function) {}
242
void Method::run(Stack& stack) const {
243
auto observer = torch::observerConfig().getModuleObserver();
245
auto instance_key = std::rand();
248
std::unordered_map<std::string, std::string> copied_metadata =
249
owner_->getMetadata();
252
observer->onEnterRunMethod(instance_key);
255
auto debug_info = std::make_shared<MobileDebugInfo>();
256
std::string name = copied_metadata["model_name"];
257
debug_info->setModelName(name);
258
debug_info->setMethodName(function_->name());
259
at::DebugInfoGuard guard(at::DebugInfoKind::MOBILE_RUNTIME_INFO, debug_info);
261
std::string error_message;
262
auto failure_guard = c10::make_scope_exit([&]() {
267
#if defined(SYMBOLICATE_MOBILE_DEBUG_HANDLE)
268
if (error_message.empty()) {
269
error_message = owner_->getDebugTable().getSourceDebugString(
270
function_->getExceptionDebugHandles(), getTopModuleTypeName(*owner_));
274
observer->onFailRunMethod(
278
error_message.empty() ? "Unknown exception" : error_message.c_str());
282
stack.insert(stack.begin(), owner_->_ivalue());
283
function_->run(stack);
285
observer->onExitRunMethod(
286
copied_metadata, function_->name(), instance_key);
288
failure_guard.release();
290
} catch (c10::BackendRuntimeException& e) {
291
#if defined(SYMBOLICATE_MOBILE_DEBUG_HANDLE)
292
for (auto handle : function_->getExceptionDebugHandles()) {
293
e.pushDebugHandle(handle);
296
auto debug_string = owner_->getDebugTable().getSourceDebugString(
297
e.getDebugHandles(), getTopModuleTypeName(*owner_));
298
e.add_context(debug_string);
300
error_message = e.what();
302
} catch (c10::Error& error) {
303
#if defined(SYMBOLICATE_MOBILE_DEBUG_HANDLE)
304
auto debug_string = owner_->getDebugTable().getSourceDebugString(
305
function_->getExceptionDebugHandles(), getTopModuleTypeName(*owner_));
306
error.add_context(debug_string);
308
error_message = error.what();
309
TORCH_RETHROW(error);
313
c10::IValue Method::operator()(std::vector<c10::IValue> stack) const {
315
TORCH_INTERNAL_ASSERT(!stack.empty());
316
return stack.front();
319
static c10::optional<std::string> print_type(const c10::Type& t) {
320
auto namedType = t.cast<c10::NamedType>();
321
if (namedType && namedType->name()) {
322
return namedType->name().value().qualifiedName();
324
if (auto dyn = t.castRaw<c10::DynamicType>()) {
325
return dyn->fallback()->annotation_str();
330
TORCH_API ModuleInfo get_module_info(const mobile::Module& module) {
332
minfo.operator_version = module.min_operator_version();
333
minfo.bytecode_version = module.bytecode_version();
334
std::vector<std::string> type_name_list;
335
for (const auto& func_ptr : module.compilation_unit().methods()) {
336
const auto& function = *func_ptr;
337
for (const auto i : c10::irange(function.get_code().op_names_.size())) {
338
const auto& op = function.get_code().op_names_[i];
339
minfo.opname_to_num_args[mobile::operator_str(op)] =
340
function.get_code().operator_input_sizes_[i];
342
for (const c10::TypePtr& tp : function.get_code().types_) {
343
type_name_list.push_back(tp->annotation_str(print_type));
345
minfo.function_names.insert(function.qualname().qualifiedName());
347
c10::TypeParser parser(type_name_list);
349
minfo.type_names = parser.getContainedTypes();