Skip to content

Commit 0a921ba

Browse files
zhxchen17facebook-github-bot
authored andcommitted
[jit][edge] Do not reuse mobile type parser for all unpicklers. (pytorch#70338)
Summary: Pull Request resolved: pytorch#70338 Today Unpickler is used by both server and mobile for deserializing model, and it always fallback to mobile parser when there's no type resolver provided by user. However this is not intended as server and mobile type parser supports different things. In this diff we provide a default fallback using script parser and opt it out for all mobile cases. ghstack-source-id: 146727330 (Note: this ignores all push blocking failures!) Test Plan: CI Reviewed By: iseeyuan Differential Revision: D33284352 fbshipit-source-id: 997c4f110b36eee6596e8f23f6a87bf91a4197ed
1 parent 3f3eae6 commit 0a921ba

20 files changed

+79
-52
lines changed

test/cpp/jit/test_mobile_type_parser.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
#include <test/cpp/jit/test_utils.h>
33

44
#include <ATen/core/jit_type.h>
5-
6-
namespace c10 {
7-
TypePtr parseType(const std::string& pythonStr);
8-
std::vector<TypePtr> parseType(std::vector<std::string>& pythonStr);
9-
} // namespace c10
5+
#include <torch/csrc/jit/mobile/type_parser.h>
106

117
namespace torch {
128
namespace jit {

test/mobile/test_upgrader_bytecode_table_example.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
* cd ~/pytorch && python torch/csrc/jit/mobile/upgrader_mobile.cpp
66
*/
77

8-
#include <caffe2/serialize/versions.h>
98
#include <torch/csrc/jit/mobile/upgrader_mobile.h>
10-
#include <ATen/core/ivalue.h>
119

12-
namespace c10 {
13-
TypePtr parseType(const std::string& pythonStr);
14-
} // namespace c10
10+
#include <ATen/core/ivalue.h>
11+
#include <caffe2/serialize/versions.h>
12+
#include <torch/csrc/jit/mobile/type_parser.h>
1513

1614
namespace torch {
1715
namespace jit {

tools/codegen/operator_versions/gen_mobile_upgraders.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,11 @@ class ByteCode(Enum):
101101
* cd ~/pytorch && python torch/csrc/jit/mobile/upgrader_mobile.cpp
102102
*/
103103
104-
#include <caffe2/serialize/versions.h>
105104
#include <torch/csrc/jit/mobile/upgrader_mobile.h>
106-
#include <ATen/core/ivalue.h>
107105
108-
namespace c10 {
109-
TypePtr parseType(const std::string& pythonStr);
110-
} // namespace c10
106+
#include <ATen/core/ivalue.h>
107+
#include <caffe2/serialize/versions.h>
108+
#include <torch/csrc/jit/mobile/type_parser.h>
111109
112110
namespace torch {
113111
namespace jit {

torch/csrc/jit/frontend/tree.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ struct Tree;
2929
using TreeRef = c10::intrusive_ptr<Tree>;
3030
using TreeList = at::SmallVector<TreeRef, 4>;
3131

32-
static const TreeList empty_trees = {};
33-
3432
struct Tree : c10::intrusive_ptr_target {
3533
Tree(int kind_) : kind_(kind_) {}
3634
int kind() const {
@@ -46,6 +44,7 @@ struct Tree : c10::intrusive_ptr_target {
4644
throw std::runtime_error("stringValue can only be called on TK_STRING");
4745
}
4846
virtual const TreeList& trees() const {
47+
static const TreeList empty_trees = {};
4948
return empty_trees;
5049
}
5150
const TreeRef& tree(size_t i) const {
@@ -149,11 +148,11 @@ struct Compound : public Tree {
149148
return false;
150149
}
151150
TreeRef map(const std::function<TreeRef(TreeRef)>& fn) override {
152-
TreeList trees_;
151+
TreeList ret;
153152
for (auto& t : trees()) {
154-
trees_.push_back(fn(t));
153+
ret.push_back(fn(t));
155154
}
156-
return Compound::create(kind(), range(), std::move(trees_));
155+
return Compound::create(kind(), range(), std::move(ret));
157156
}
158157

159158
const SourceRange& range() const override {

torch/csrc/jit/mobile/debug_info.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <torch/csrc/jit/frontend/source_range.h>
22
#include <torch/csrc/jit/mobile/debug_info.h>
3+
#include <torch/csrc/jit/mobile/type_parser.h>
34
#include <torch/csrc/jit/serialization/callstack_debug_info_serialization.h>
45
#include <torch/csrc/jit/serialization/source_range_serialization.h>
56

@@ -122,10 +123,13 @@ MobileDebugTable::MobileDebugTable(
122123
size_t debug_size{0};
123124
std::tie(debug_data, debug_size) = reader->getRecord(record_name);
124125
auto ivalues =
125-
std::move(
126-
*jit::unpickle(
127-
reinterpret_cast<const char*>(debug_data.get()), debug_size)
128-
.toTuple())
126+
std::move(*jit::unpickle(
127+
reinterpret_cast<const char*>(debug_data.get()),
128+
debug_size,
129+
nullptr,
130+
{},
131+
c10::parseType)
132+
.toTuple())
129133
.elements();
130134
SourceRangeDeserializer deserializer;
131135
for (auto& val : ivalues) {

torch/csrc/jit/mobile/import.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <torch/csrc/jit/api/compilation_unit.h>
1313
#include <torch/csrc/jit/mobile/interpreter.h>
1414
#include <torch/csrc/jit/mobile/observer.h>
15+
#include <torch/csrc/jit/mobile/type_parser.h>
1516
#include <torch/csrc/jit/mobile/upgrader_mobile.h>
1617
#include <torch/csrc/jit/runtime/instruction.h>
1718
#include <torch/csrc/jit/serialization/import_export_constants.h>
@@ -78,11 +79,6 @@
7879
// - Argument::{known_length_,kwarg_only_}
7980
// - FunctionSchema::{overload_name_, is_vararg_, is_varret_}
8081

81-
namespace c10 {
82-
// std::string serializeType(const Type &t);
83-
TypePtr parseType(const std::string& pythonStr);
84-
} // namespace c10
85-
8682
namespace torch {
8783
namespace jit {
8884
using caffe2::serialize::IStreamAdapter;
@@ -502,7 +498,8 @@ c10::IValue BytecodeDeserializer::readArchive(
502498
type_resolver,
503499
obj_loader,
504500
device_,
505-
*reader_.get());
501+
*reader_.get(),
502+
nullptr);
506503
return ivalues;
507504
}
508505

torch/csrc/jit/mobile/import_data.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <caffe2/serialize/inline_container.h>
66
#include <torch/csrc/jit/api/compilation_unit.h>
77
#include <torch/csrc/jit/mobile/observer.h>
8+
#include <torch/csrc/jit/mobile/type_parser.h>
89
#include <torch/csrc/jit/runtime/instruction.h>
910
#include <torch/csrc/jit/serialization/unpickler.h>
1011
#include <torch/custom_class.h>
@@ -14,11 +15,6 @@
1415
#include <string>
1516
#include <vector>
1617

17-
namespace c10 {
18-
// std::string serializeType(const Type &t);
19-
TypePtr parseType(const std::string& pythonStr);
20-
} // namespace c10
21-
2218
namespace torch {
2319
namespace jit {
2420
using caffe2::serialize::IStreamAdapter;
@@ -151,7 +147,9 @@ c10::IValue BytecodeDeserializer::readArchive(
151147
std::move(obj_loader),
152148
std::move(read_record),
153149
// NOLINTNEXTLINE(performance-move-const-arg)
154-
std::move(device));
150+
std::move(device),
151+
false,
152+
nullptr);
155153
return unpickler.parse_ivalue();
156154
}
157155

torch/csrc/jit/mobile/model_compatibility.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ c10::IValue readArchive(
5353
type_resolver,
5454
obj_loader,
5555
device,
56-
stream_reader);
56+
stream_reader,
57+
nullptr);
5758
return ivalues;
5859
}
5960

torch/csrc/jit/mobile/type_parser.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <torch/csrc/jit/mobile/type_parser.h>
2+
13
#include <ATen/core/jit_type.h>
24
#include <c10/util/string_view.h>
35
#include <torch/csrc/jit/frontend/parser_constants.h>

torch/csrc/jit/mobile/type_parser.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#pragma once
2+
13
#include <ATen/core/dynamic_type.h>
24
#include <ATen/core/jit_type.h>
35

0 commit comments

Comments
 (0)