xref: /aosp_15_r20/external/pytorch/torch/csrc/jit/serialization/mobile_bytecode.fbs (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1// To regenerate header, use:
2// flatc --cpp --gen-mutable --no-prefix --scoped-enums mobile_bytecode.fbs
3
4file_identifier "PTMF";
5
6namespace torch.jit.mobile.serialization;
7
8struct Int {
9  int_val:long;
10}
11
12struct Bool {
13  bool_val:bool;
14}
15
16struct Double{
17  double_val:double;
18}
19
20struct PerTensorAffineSchema {
21  q_scale:double;
22  q_zero_point:int;
23}
24
25table QuantizedSchema {
26  qscheme:byte;
27  scale:double;
28  zero_point:int;
29  scales:TensorMetadata;
30  zero_points:TensorMetadata;
31  axis:int;
32}
33
34table TensorMetadata {
35  // torch._utils _rebuild_tensor_v2
36  storage_location_index:uint;
37  // enum ScalarType
38  scalar_type:byte;
39  storage_offset:int;
40  sizes:[int];
41  strides:[int];
42  requires_grad:bool;
43
44  // only set for quantized tensors
45  quantized_schema:QuantizedSchema;
46}
47
48table String {
49  data:string;
50}
51
52table Device {
53  str:string;
54}
55
56table List {
57  items:[uint];
58  annotation_str:string;  // to recover key/val type
59}
60
61table IntList {
62  items:[long];
63}
64
65table DoubleList {
66  items:[double];
67}
68
69table BoolList {
70  items:[bool];
71}
72
73table Tuple {
74  items:[uint];
75}
76
77table Dict {
78  keys:[uint];
79  values:[uint];
80  annotation_str:string;  // to recover key/val type
81}
82
83enum TypeType :ubyte {
84  UNSET,
85  CLASS_WITH_FIELD,
86  CUSTOM_CLASS,
87  CLASS_WITH_SETSTATE,
88  NON_OBJ,
89}
90
91table ObjectType {
92  type_name:string;
93  type:TypeType;
94  // Below fields are optional
95  attr_names:[string];
96}
97
98table Object {
99  type_index:uint;
100  state:uint;
101  attrs:[uint];
102  setstate_func:uint;
103}
104
105struct ComplexDouble {
106  real:double;
107  imag:double;
108}
109
110table EnumValue {
111  type_name:string;
112  value:uint;  // index to ivalues;
113}
114
115
116struct Instruction {
117  // Should op be enum instead?
118  op:byte;
119  n:ushort;
120  x:int;
121}
122
123table Operator {
124  name:string;
125  overload_name:string;
126  num_args_serialized:int = -1;
127}
128
129table Arg {
130  name:string;
131  // Why do we use string to represent types
132  // rather than index into Code.types?
133  type:string;
134  default_value:uint;  // position into ivalues
135}
136
137table Schema {
138  arguments:[Arg];
139  returns:[Arg];
140}
141
142table DebugInfo {
143  debug_handle:[long];
144}
145
146table Function {
147  qn:string;
148  instructions:[Instruction];
149  operators:[Operator];
150  constants:[uint];  // index to ivalue
151  type_annotations:[string];
152  register_size:int;
153  schema:Schema;
154  debug_info:DebugInfo;
155  class_type:uint; // index into type table
156}
157
158table StorageData {
159  data:[ubyte] (force_align:16);
160}
161
162// Is it needed to represent other types?
163union IValueUnion {
164  Int,
165  Bool,
166  Double,
167  ComplexDouble,
168  TensorMetadata,
169  String,
170  List,
171  Tuple,
172  Dict,
173  Object,
174  IntList,
175  DoubleList,
176  BoolList,
177  Device,
178  EnumValue,
179  Function,
180}
181
182table IValue {
183  val:IValueUnion;
184}
185
186table ExtraFile {
187  name:string;
188  content:string;
189}
190
191table Module {
192  // denotes the bytecode version of the mobile Module
193  // this starts from 9 for a flatbuffer file
194  // versions 8 and below are reserved pickle
195  // Version is bumped when changes in model serialization/execution
196  // can no longer work in current version
197  // To read more:
198  // https://github.com/pytorch/pytorch/blob/master/caffe2/serialize/versions.h#L96
199  bytecode_version:uint;
200
201  extra_files:[ExtraFile];
202  methods:[uint];  // index to ivalues
203  state_obj:uint; // index to ivalues
204  ivalues:[IValue];
205  storage_data_size:int;  // number of storage data;
206  storage_data:[StorageData];
207  object_types:[ObjectType];
208  jit_sources:[ExtraFile];
209  jit_constants:[uint];  // index to ivalues
210
211  // version of operator
212  // Version is bumped when changes in operator
213  // can no longer work in current version
214  // To read more:
215  // https://github.com/pytorch/rfcs/blob/master/RFC-0017-PyTorch-Operator-Versioning.md
216  operator_version:uint;
217
218  // Size of ivalue that comes from the mobile module.
219  // Because the ivalues array above can also have ivalues that cames from
220  // the jit::Module that got it's source attached to flatbuffer file.
221  // this should be smaller than ivalues.size()
222  mobile_ivalue_size:uint;
223}
224
225root_type Module;
226