Skip to content

Commit 77a6436

Browse files
kimishpatelfacebook-github-bot
authored andcommitted
[Pytorch Mobile] Combing instructions and debug hanles in single struct (pytorch#62418)
Summary: Pull Request resolved: pytorch#62418 Debug handles have one to one correspondence with instruction, so just combine them in one. Test Plan: CI Imported from OSS Reviewed By: raziel Differential Revision: D29993661 fbshipit-source-id: 125c7163174cf66624dd95f110fdc8208fea8a07
1 parent 1b04d99 commit 77a6436

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

torch/csrc/jit/mobile/function.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ void Function::append_instruction(OpCode op, int X, int N, int64_t dbg_handle) {
2727
isOpSupportedInMobile(op),
2828
toString(op),
2929
" is not supported in mobile module.");
30-
code_->instructions_.emplace_back(op, X, N);
31-
code_->debug_handles_.emplace_back(dbg_handle);
30+
code_->instructions_with_handles_.emplace_back(
31+
Instruction(op, X, N), dbg_handle);
3232
}
3333

3434
bool Function::append_operator(
@@ -143,9 +143,9 @@ void Function::set_register_size(size_t size) {
143143
int64_t Function::get_debug_handle(size_t pc) const {
144144
TORCH_CHECK(code_, "Valid code must exist.");
145145
TORCH_CHECK(
146-
pc < code_->debug_handles_.size(),
146+
pc < code_->instructions_with_handles_.size(),
147147
"Module debug info index out of boundary.");
148-
return code_->debug_handles_[pc];
148+
return code_->instructions_with_handles_[pc].debug_handle;
149149
}
150150

151151
void Function::setSchema(c10::FunctionSchema schema) {
@@ -177,7 +177,11 @@ const std::shared_ptr<Code> Function::get_code() const {
177177

178178
int64_t Function::getExceptionDebugHandle() const {
179179
size_t pc = getInterpretersExceptionPC();
180-
return (pc < code_->debug_handles_.size()) ? code_->debug_handles_[pc] : -1;
180+
// we dont do bounds check given that pc is obtained
181+
// via internal method of getInterpretersExceptionPC
182+
// which returns the PC of where the interpreter is.
183+
// Although .at will do bounds check anyway.
184+
return code_->instructions_with_handles_.at(pc).debug_handle;
181185
}
182186

183187
} // namespace mobile

torch/csrc/jit/mobile/interpreter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ bool InterpreterState::run(Stack& stack) {
5454
size_t pc = 0;
5555
while (true) {
5656
try {
57-
Instruction inst = code_->instructions_[pc];
57+
Instruction inst = code_->instructions_with_handles_[pc].instruction;
5858

5959
// std::cout << "RUNNING " << pc << " " << code_->instructions_[pc];
6060
// if (inst.op == OP) {

torch/csrc/jit/mobile/interpreter.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@ namespace jit {
99
namespace mobile {
1010
using Stack = std::vector<c10::IValue>;
1111
using DebugHandle = int64_t;
12+
struct InstructionWithDebugHandle {
13+
InstructionWithDebugHandle(Instruction inst, DebugHandle handle)
14+
: instruction(inst), debug_handle(handle) {}
15+
Instruction instruction;
16+
DebugHandle debug_handle;
17+
};
18+
1219
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
1320
struct Code {
1421
// TODO: Combine instructions and debug handles vector
1522
// into std::vector<<std::pair<Instruction, DebugHandle>>
16-
std::vector<Instruction> instructions_;
17-
std::vector<DebugHandle> debug_handles_;
23+
std::vector<InstructionWithDebugHandle> instructions_with_handles_;
1824
std::vector<c10::OperatorName> op_names_;
1925
std::vector<std::function<void(Stack&)>> operators_;
2026
std::vector<c10::IValue> constants_;

0 commit comments

Comments
 (0)