forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mobile_bytecode.fbs
225 lines (187 loc) · 3.89 KB
/
mobile_bytecode.fbs
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// To regenerate header, use:
// flatc --cpp --gen-mutable --no-prefix --scoped-enums mobile_bytecode.fbs
file_identifier "PTMF";
namespace torch.jit.mobile.serialization;
struct Int {
int_val:long;
}
struct Bool {
bool_val:bool;
}
struct Double{
double_val:double;
}
struct PerTensorAffineSchema {
q_scale:double;
q_zero_point:int;
}
table QuantizedSchema {
qscheme:byte;
scale:double;
zero_point:int;
scales:TensorMetadata;
zero_points:TensorMetadata;
axis:int;
}
table TensorMetadata {
// torch._utils _rebuild_tensor_v2
storage_location_index:uint;
// enum ScalarType
scalar_type:byte;
storage_offset:int;
sizes:[int];
strides:[int];
requires_grad:bool;
// only set for quantized tensors
quantized_schema:QuantizedSchema;
}
table String {
data:string;
}
table Device {
str:string;
}
table List {
items:[uint];
annotation_str:string; // to recover key/val type
}
table IntList {
items:[long];
}
table DoubleList {
items:[double];
}
table BoolList {
items:[bool];
}
table Tuple {
items:[uint];
}
table Dict {
keys:[uint];
values:[uint];
annotation_str:string; // to recover key/val type
}
enum TypeType :ubyte {
UNSET,
CLASS_WITH_FIELD,
CUSTOM_CLASS,
CLASS_WITH_SETSTATE,
NON_OBJ,
}
table ObjectType {
type_name:string;
type:TypeType;
// Below fields are optional
attr_names:[string];
}
table Object {
type_index:uint;
state:uint;
attrs:[uint];
setstate_func:uint;
}
struct ComplexDouble {
real:double;
imag:double;
}
table EnumValue {
type_name:string;
value:uint; // index to ivalues;
}
struct Instruction {
// Should op be enum instead?
op:byte;
n:ushort;
x:int;
}
table Operator {
name:string;
overload_name:string;
num_args_serialized:int = -1;
}
table Arg {
name:string;
// Why do we use string to represent types
// rather than index into Code.types?
type:string;
default_value:uint; // position into ivalues
}
table Schema {
arguments:[Arg];
returns:[Arg];
}
table DebugInfo {
debug_handle:[long];
}
table Function {
qn:string;
instructions:[Instruction];
operators:[Operator];
constants:[uint]; // index to ivalue
type_annotations:[string];
register_size:int;
schema:Schema;
debug_info:DebugInfo;
class_type:uint; // index into type table
}
table StorageData {
data:[ubyte] (force_align:16);
}
// Is it needed to represent other types?
union IValueUnion {
Int,
Bool,
Double,
ComplexDouble,
TensorMetadata,
String,
List,
Tuple,
Dict,
Object,
IntList,
DoubleList,
BoolList,
Device,
EnumValue,
Function,
}
table IValue {
val:IValueUnion;
}
table ExtraFile {
name:string;
content:string;
}
table Module {
// denotes the bytecode version of the mobile Module
// this starts from 9 for a flatbuffer file
// versions 8 and below are reserved pickle
// Version is bumped when changes in model serialization/execution
// can no longer work in current version
// To read more:
// https://github.com/pytorch/pytorch/blob/master/caffe2/serialize/versions.h#L96
bytecode_version:uint;
extra_files:[ExtraFile];
methods:[uint]; // index to ivalues
state_obj:uint; // index to ivalues
ivalues:[IValue];
storage_data_size:int; // number of storage data;
storage_data:[StorageData];
object_types:[ObjectType];
jit_sources:[ExtraFile];
jit_constants:[uint]; // index to ivalues
// version of operator
// Version is bumped when changes in operator
// can no longer work in current version
// To read more:
// https://github.com/pytorch/rfcs/blob/master/RFC-0017-PyTorch-Operator-Versioning.md
operator_version:uint;
// Size of ivalue that comes from the mobile module.
// Because the ivalues array above can also have ivalues that cames from
// the jit::Module that got it's source attached to flatbuffer file.
// this should be smaller than ivalues.size()
mobile_ivalue_size:uint;
}
root_type Module;