xref: /aosp_15_r20/external/executorch/schema/README.md (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1*523fa7a6SAndroid Build Coastguard WorkerThe `schema.fbs` file in this directory describes the
2*523fa7a6SAndroid Build Coastguard Worker[Flatbuffers](https://google.github.io/flatbuffers/) schema used to serialize
3*523fa7a6SAndroid Build Coastguard WorkerExecuTorch programs.
4*523fa7a6SAndroid Build Coastguard Worker
5*523fa7a6SAndroid Build Coastguard WorkerThe `scalar_type.fbs` file contains schema for scalar types.
6*523fa7a6SAndroid Build Coastguard Worker
7*523fa7a6SAndroid Build Coastguard Worker## Rules to ensure forward/backward compatibility
8*523fa7a6SAndroid Build Coastguard Worker
9*523fa7a6SAndroid Build Coastguard WorkerA serialized program never knows which version of the code will try to read it.
10*523fa7a6SAndroid Build Coastguard WorkerIt's important that old code can read serialized programs generated by new code
11*523fa7a6SAndroid Build Coastguard Worker(forward compatibility; FC), and that new code can read serialized programs
12*523fa7a6SAndroid Build Coastguard Workergenerated by old code (backward compatibility; BC).
13*523fa7a6SAndroid Build Coastguard Worker
14*523fa7a6SAndroid Build Coastguard WorkerFor background on the rules below, see the Flatbuffers document [Writing a
15*523fa7a6SAndroid Build Coastguard Workerschema](https://google.github.io/flatbuffers/md__schemas.html), especially the
16*523fa7a6SAndroid Build Coastguard Worker"Tables" and "Schema evolution examples" sections.
17*523fa7a6SAndroid Build Coastguard Worker
18*523fa7a6SAndroid Build Coastguard WorkerTo ensure binary FC/BC:
19*523fa7a6SAndroid Build Coastguard Worker
20*523fa7a6SAndroid Build Coastguard Worker- Use `table`, not `struct`, for structured data.
21*523fa7a6SAndroid Build Coastguard Worker  - `struct` cannot handle optional fields; changes to `struct` are typically
22*523fa7a6SAndroid Build Coastguard Worker    not FC/BC.
23*523fa7a6SAndroid Build Coastguard Worker- When adding new fields, do not modify the ID of existing fields.
24*523fa7a6SAndroid Build Coastguard Worker  - The easiest way to do this is to add new fields to the bottom of a `table`.
25*523fa7a6SAndroid Build Coastguard Worker  - If it's important to reorder the declaration of fields for readability,
26*523fa7a6SAndroid Build Coastguard Worker    all fields in the `table` must be explicitly annotated with `(ID: #)` fields
27*523fa7a6SAndroid Build Coastguard Worker    that preserve their original zero-based indices.
28*523fa7a6SAndroid Build Coastguard Worker- Fields must never be removed; they can only be deprecated using the
29*523fa7a6SAndroid Build Coastguard Worker  `(deprecated)` annotation.
30*523fa7a6SAndroid Build Coastguard Worker- Semantics for existing fields must not change without carefully auditing older
31*523fa7a6SAndroid Build Coastguard Worker  versions of the code to understand the implications.
32*523fa7a6SAndroid Build Coastguard Worker- Default values for fields must be very carefully managed.
33*523fa7a6SAndroid Build Coastguard Worker  - Ideally, the default value for a field will never change.
34*523fa7a6SAndroid Build Coastguard Worker  - If they do change, audit old and new code to understand the implications of
35*523fa7a6SAndroid Build Coastguard Worker    the change.
36*523fa7a6SAndroid Build Coastguard Worker  - Note that fields with unspecified defaults will default to the zero value of
37*523fa7a6SAndroid Build Coastguard Worker    their types.
38*523fa7a6SAndroid Build Coastguard Worker- Ideally the types of fields must not change. If they do, ensure that they are
39*523fa7a6SAndroid Build Coastguard Worker  compatible: e.g., a `uint` could change to `int` if we are confident that no
40*523fa7a6SAndroid Build Coastguard Worker  program in existence stored a value in that field with the most significant
41*523fa7a6SAndroid Build Coastguard Worker  bit set.
42*523fa7a6SAndroid Build Coastguard Worker
43*523fa7a6SAndroid Build Coastguard WorkerNote that these rules do not ensure source code FC/BC. E.g., deprecating a field
44*523fa7a6SAndroid Build Coastguard Workerwill tell Flatbuffer's `flatc` to stop generating getters/setters for it, so any
45*523fa7a6SAndroid Build Coastguard Workercode using those functions will fail to build, and will need to be fixed.
46*523fa7a6SAndroid Build Coastguard Worker
47*523fa7a6SAndroid Build Coastguard WorkerHowever, this serialization format and the Flatbuffer types that are generated
48*523fa7a6SAndroid Build Coastguard Workerfrom it are private to ExecuTorch, so we do not need to worry about updating
49*523fa7a6SAndroid Build Coastguard Workerexternal client code when the Flatbuffer API changes. This also means that we
50*523fa7a6SAndroid Build Coastguard Workercan more easily upgrade to new versions of the Flatbuffers tools/library.
51*523fa7a6SAndroid Build Coastguard Worker
52*523fa7a6SAndroid Build Coastguard WorkerIf we are forced to make a FC/BC-breaking change, it may make sense to create a
53*523fa7a6SAndroid Build Coastguard Workernew `.fbs` file with a different `file_identifier`, and adding higher-level
54*523fa7a6SAndroid Build Coastguard Workerlogic to check the file magic before parsing the binary file with one schema or
55*523fa7a6SAndroid Build Coastguard Workerthe other.
56