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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
#include <torch/csrc/jit/mobile/module.h>
#include <torch/csrc/jit/mobile/interpreter.h>
#include <torch/csrc/jit/mobile/observer.h>
#include <torch/csrc/jit/runtime/jit_exception.h>
#include <exception>
#include <ATen/record_function.h>
namespace torch {
namespace jit {
std::ostream& operator<<(std::ostream& out, Instruction inst);
namespace mobile {
void CompilationUnit::register_function(std::unique_ptr<Function> fn) {
methods_.emplace_back(std::move(fn));
}
Function* CompilationUnit::find_function(const c10::QualifiedName& qn) {
for (auto& fn : methods_) {
if (fn->qualname() == qn) {
return fn.get();
}
}
return nullptr;
}
Method Module::get_method(const std::string& name) const {
if (auto method = find_method(name)) {
return *method;
}
AT_ERROR("Method '", name, "' is not defined.");
}
c10::optional<Method> Module::find_method(const std::string& basename) const {
for (auto& fn : cu_->methods()) {
if (fn->name() == basename) {
return c10::make_optional<Method>(Method(this, fn.get()));
}
}
return c10::nullopt;
}
namespace {
void set_train_recurse(
const c10::intrusive_ptr<c10::ivalue::Object>& obj,
bool on) {
if (auto slot = obj->type()->findAttributeSlot("training")) {
obj->setSlot(*slot, on);
} else {
TORCH_INTERNAL_ASSERT(false, "'training' attribute not found");
}
for (const auto& slot : obj->slots()) {
if (slot.isObject()) {
set_train_recurse(slot.toObject(), on);
}
}
}
void slot_params_recurse(
const c10::intrusive_ptr<c10::ivalue::Object>& obj,
std::vector<at::Tensor>* params) {
for (const auto& slot : obj->slots()) {
if (slot.isTensor()) {
params->emplace_back(slot.toTensor());
} else if (slot.isObject()) {
slot_params_recurse(slot.toObject(), params);
}
}
}
void slot_named_params_recurse(
const c10::intrusive_ptr<c10::ivalue::Object>& obj,
std::map<std::string, at::Tensor>* params,
const std::string& parent_name) {
auto slots = obj->slots();
size_t nslots = slots.size();
for (size_t i = 0; i < nslots; ++i) {
auto slot = slots[i];
std::string name =
parent_name.size() == 0 ? parent_name : parent_name + ".";
name += obj->type()->getAttributeName(i);
if (slot.isTensor()) {
(*params)[name] = slot.toTensor();
} else if (slot.isObject()) {
slot_named_params_recurse(slot.toObject(), params, name);
}
}
}
} // namespace
const std::vector<at::Tensor> Module::parameters() const {
std::vector<at::Tensor> params;
slot_params_recurse(object_, ¶ms);
return params;
}
const std::map<std::string, at::Tensor> Module::named_parameters() const {
std::map<std::string, at::Tensor> params;
const std::string name = "";
slot_named_params_recurse(object_, ¶ms, name);
return params;
}
std::string Module::get_forward_method_debug_info(size_t pc) const {
return find_method("forward")->get_module_debug_info(pc);
}
void Module::train(bool on) {
set_train_recurse(object_, on);
}
bool Module::is_training() const {
if (auto slot = object_->type()->findAttributeSlot("training")) {
return object_->getSlot(*slot).toBool();
}
return true;
}
Method::Method(const Module* owner, Function* function)
: owner_(owner), function_(function) {}
void Method::run(Stack& stack) {
auto observer = torch::observerConfig().getModuleObserver();
/* if the metadata dict doesn't contain "model_name", copy the metadata and
set the value of "model_name" as name() */
std::unordered_map<std::string, std::string> copied_metadata =
owner_->metadata();
if (owner_->metadata().find("model_name") == owner_->metadata().end()) {
copied_metadata["model_name"] = owner_->name();
}
if (observer) {
observer->onEnterRunMethod(copied_metadata, function_->name());
}
auto debug_info = std::make_shared<MobileDebugInfo>();
std::string name = copied_metadata["model_name"];
debug_info->setModelName(name);
debug_info->setMethodName(function_->name());
at::DebugInfoGuard guard(at::DebugInfoKind::MOBILE_RUNTIME_INFO, debug_info);
try {
stack.insert(stack.begin(), owner_->_ivalue());
function_->run(stack);
if (observer) {
observer->onExitRunMethod();
}
} catch (c10::Error& error) {
if (observer) {
observer->onFailRunMethod(error.what());
}
TORCH_RETHROW(error);
} catch (...) {
auto currentException = std::current_exception();
try {
if (!currentException) {
TORCH_CHECK(false, "Unknown exception");
} else {
try {
std::rethrow_exception(currentException);
} catch (const std::exception& e) {
TORCH_CHECK(false, e.what());
}
}
} catch (c10::Error& error) {
if (observer) {
observer->onFailRunMethod(error.what());
}
TORCH_RETHROW(error);
}
}
}
c10::IValue Method::operator()(std::vector<IValue> stack) {
run(stack);
TORCH_INTERNAL_ASSERT(!stack.empty());
return stack.front();
}
} // namespace mobile
} // namespace jit
} // namespace torch
|